Help with loading and using textures

KidTsunami
Unregistered
 
Post: #1
Hey, I've finally gotten to loading and actually using the textures but now, when I draw, all I get is a white square. What am I doing wrong? (the white square has the right height and width)

Code:
int GameGraphics :: LoadTexture (string fileName){
    // load into SDL_surface
    SDL_Surface * tempSurface = NULL;
    tempSurface = IMG_Load (fileName.c_str());
    
    if (tempSurface == NULL){
        cout << "holy crap" << endl;
    }
    
    // generate the texture
    glGenTextures (1,&theTextures[curTextureID]);
    glBindTexture (GL_TEXTURE_2D,theTextures [curTextureID]);
    
    // setup pixel data
    int width  = tempSurface -> w;
    int height = tempSurface -> h;
    
    theSprites [curTextureID].height = height;
    theSprites [curTextureID].width = width;
    
    unsigned char * curData = (unsigned char *) (tempSurface -> pixels);    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, width, height, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, curData);
    cout << "The ID: " << curTextureID << endl;
    SDL_FreeSurface (tempSurface);
    ++ curTextureID;
    return 1;
}

void GameGraphics :: DrawTexture (int x, int y, int layer, int textureID){
    int height = theSprites [textureID].height;
    int width = theSprites [textureID].width;
    glClear( GL_COLOR_BUFFER_BIT );
    glPushMatrix(); //Save matrix
    glTranslatef(x, y, layer); //Position Sprite
//    glRotate(rotation, 0, 0, 1); //Rotate Sprite
    glBindTexture(GL_TEXTURE_2D, theTextures[textureID]); //Bind a preloaded texture to quad
  
    glBegin(GL_QUADS);

    glTexCoord2f(0.0,0.0);
    glVertex2f(width/2, height/2);
    glTexCoord2f(1.0, 0.0);
    glVertex2f(-1*(width/2), height/2);
    glTexCoord2f(1.0, 1.0);
    glVertex2f(-1*(width/2), -1*(height/2));
    glTexCoord2f(0.0, 1.0);
    glVertex2f(width/2, -1*(height/2));
    glEnd();
  
    glPopMatrix(); //Restore Matrix
    SDL_GL_SwapBuffers();
    SDL_Delay (2000);

    return;
}
Quote this message in a reply
w_reade
Unregistered
 
Post: #2
perhaps a "glEnable(GL_TEXTURE_2D);" somewhere?
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #3
Nope, that wasn't it, here's my GL_init stuff
[SOURCECODE]void GameApp :: InitOGL (){
// Current Video Settings
const SDL_VideoInfo* curVidSettings = NULL;
// Width and Height
int width = kScreenWidth;
int height = kScreenHeight;
// Color depth
int colorDepth = 0;
// Flags for SDL_SetVideoMode
int flags = 0;

curVidSettings = SDL_GetVideoInfo ();

colorDepth = curVidSettings->vfmt->BitsPerPixel;

// Set up OpenGL Window Attributes
SDL_GL_SetAttribute (SDL_GL_RED_SIZE,5);
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE,5);
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE,5);
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE,kColorDepth);
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER,1);

flags = SDL_OPENGL | SDL_FULLSCREEN;

if( SDL_SetVideoMode( width, height, colorDepth, flags ) == 0 ) {
/*
* This could happen for a variety of reasons,
* including DISPLAY not being set, the specified
* resolution not being available, etc.
*/
fprintf( stderr, "Video mode set failed: %s\n",
SDL_GetError( ) );
}

gluOrtho2D (0,kScreenWidth,0,kScreenHeight);
glEnable(GL_TEXTURE_2D);
}[/SOURCECODE]
Quote this message in a reply
w_reade
Unregistered
 
Post: #4
hmm, not sure. Are you definitely calling DrawTexture() with the correct textureID parameter?

In LoadTexture(), there seems to be some confusion at to what curTextureID means; when you cout, it looks like you're confusing the texture's position in the theTextures array with its value.

And why does it always return 1, anyway? surely you may as well make it void...
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #5
Yeah, my code takes a text file and reads in the file names and loads each of the textures in. I had that cout to check if it was doing the correct ID, the index in the array.
This is my call to DrawTexture:
[SOURCECODE]theGraphics.DrawTexture (200,490,0,0);[/SOURCECODE]
Oh and the returning 1 thing, yeah that was stupid, I was going to put in something that dealt with failing if the texture didn't load correctly but haven't gotten around to it.
I'm almost positive that that is the correct ID because it is drawing the height and width correctly.
Currently my test .png is one of the textures that carlos recently put up (bambomat.png) to be specific.
Quote this message in a reply
Mark C
Unregistered
 
Post: #6
Make sure your texture is in the right directory and is named right. Every-time I got just a white space instead of a texture it was because of something stupid like that.
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #7
Yeah, its named right and is in the right place. I'm almost positive that its the right file because the height and width part is working, the square is the right size, its just white...

One thing, in my build window I do have this warning, would this have something to do with it?

Quote:ld: warning prebinding disabled because dependent library: libpng12.0.1.2.5.dylib is not prebound
Quote this message in a reply
Zoldar256
Unregistered
 
Post: #8
Few simple questions:
Does your image have square dimensions? Is it doing GL_REPLACE and not GL_MODULATE or something?
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #9
The dimensions are 256x256 and I do not see GL_REPLACE or GL_MODULATE in my code.

Narrowing the problem down possibly, I think I just haven't included libpng correctly...
Earlier I was trying to find out how to include libpng and OSC said
Quote:If you followed the instructions on that page, you have a static library in /usr/local/lib.

Right here is the contents of that folder for me
Quote:libpng.
libpng.3.
libpng.3.1.2.5.dylib
libpng.a
libpng12.0.1.2.5.dylib
libpng12.0.dylib
libpng12.a
libpng12.dylib


I added the file "libpng12.0.1.2.5.dylib". While trying different things to identify the problem, I removed said file from the project and everything still stayed the same. So, I don't know what to do, should I try including all of those files.

(You should note that SDL_Image documentation says that libpng is required for loading pngs)
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #10
Do you guys think I should just scrap PNG and use the tga loading stuff from the Nehe tutorial. Or do you think that my problem isn't related to my loading in of the file?
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #11
Look for the 'QT Value Pak' hosted on somewhere on iDGĂ– that has a few examples of texture-related stuff.

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #12
WOW, sealfin thanks a BUNCH! The problem was the types that I was using in my glTexImage2D function...
Heres the new one that works...

[SOURCECODE]glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA_EXT,
GL_UNSIGNED_BYTE, curData);[/SOURCECODE]

and the old one...
[SOURCECODE]glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, curData);[/SOURCECODE]

Thanks everyone! Ninja
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Loading and using textures with alpha in OpenGL with Cocoa corporatenewt 4 5,899 Dec 8, 2007 02:06 PM
Last Post: Malarkey
  loading textures - cocoa openGL mDmarco 20 9,265 Aug 28, 2007 08:48 PM
Last Post: OneSadCookie
  Best way to deal with loading textures? bronxbomber92 14 5,272 May 4, 2007 05:31 PM
Last Post: OneSadCookie
  libpng loading junk at bottom of my OpenGL textures...? BinarySpike 6 5,918 Apr 19, 2007 12:20 PM
Last Post: BinarySpike
  Loading textures from bitmaps tehqin 5 3,874 Feb 26, 2007 01:58 AM
Last Post: unknown