I'm loading my textures from SDL, saving them in a texture class and then displaying them with an inventively named 'display' function. But instead of displaying the images, it displays a white box the size of the image. I'll include all the code I think is relevant.
Initialization:
if(SDL_Init(SDL_INIT_EVERYTHING) == -1) throw("Failed to initialize"); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); if(bWindowed) m_sdlScreen = SDL_SetVideoMode(nScreenWidth, nScreenHeight, nScreenBPP, SDL_OPENGL | SDL_SWSURFACE | SDL_RESIZABLE); else m_sdlScreen = SDL_SetVideoMode(nScreenWidth, nScreenHeight, nScreenBPP, SDL_OPENGL | SDL_SWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN); if(!m_sdlScreen) throw("Failed screen creation"); glEnable(GL_TEXTURE_2D); // Set clear colour glClearColor(0.5, 0.5, 0.5, 0); glViewport(0, 0, nScreenWidth, nScreenHeight); glClear(GL_COLOR_BUFFER_BIT); // Set projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, nScreenWidth, nScreenHeight, 0, -1, 1); // Set model view matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if(glGetError() != GL_NO_ERROR){throw("Error initializing OpenGL");} SDL_WM_SetCaption(chHeader, NULL);
Texture class constructor: it takes a file name, which it uses to load an SDL_Surface which it then converts to a texture (or not, as the case may be).
The Texture class has width, height and a GLuint pointer as members.
strFilename is the string passed, btw.
SDL_Surface *p_sdlLoadedImage = LoadImage(strFilename); // Check for errors if(p_sdlLoadedImage == NULL) throw("Loaded image is NULL"); if((p_sdlLoadedImage->w & (p_sdlLoadedImage->w - 1)) != 0) throw("Texture image width is not a power of 2"); if((p_sdlLoadedImage->h & (p_sdlLoadedImage->h - 1)) != 0) throw("Texture image height is not a power of 2"); SDL_Surface *sdlImage = SDL_CreateRGBSurface(SDL_SWSURFACE, p_sdlLoadedImage->w, p_sdlLoadedImage->h, WINDOW.GetScreenBPP(), #if SDL_BYTEORDER == SDL_LIL_ENDIAN // OpenGL RGBA masks 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); m_nWidth = sdlImage->w; m_nHeight = sdlImage->h; if(sdlImage == NULL){throw("Error creating surface code: 666");} // Save the alpha blending attributes Uint32 uSavedFlags = p_sdlLoadedImage->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); Uint8 uSavedAlpha = p_sdlLoadedImage->format->alpha; if((uSavedFlags & SDL_SRCALPHA) == SDL_SRCALPHA){SDL_SetAlpha(p_sdlLoadedImage, 0, 0);} // Copy the surface into the GL texture image SDL_Rect sdlOffset; sdlOffset.x = 0; sdlOffset.y = 0; SDL_BlitSurface(p_sdlLoadedImage, NULL , sdlImage, &sdlOffset); // Restore the alpha blending attributes if((uSavedFlags & SDL_SRCALPHA) == SDL_SRCALPHA){SDL_SetAlpha(p_sdlLoadedImage, uSavedFlags, uSavedAlpha);} GLuint *glTexture; //Create an OpenGL texture for the image glBindTexture(GL_TEXTURE_2D, *glTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_sdlLoadedImage->w, p_sdlLoadedImage->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, sdlImage->pixels); SDL_FreeSurface(sdlImage); SDL_FreeSurface(p_sdlLoadedImage); mp_glTexture = glTexture;
Then in my main class' constructor there's this:
for(int aaa = 0; aaa < nNumFiles; aaa++) { mp_vsdlResources.push_back(new Texture(astrFiles[aaa])); }
The image display function:
void DisplayTexture(const int &nX, const int &nY, Texture *p_sdlSource, SDL_Rect *p_sdlClip){ bool bClip = true; if(p_sdlClip->x < 0 && p_sdlClip->y < 0) // Show whole thing bClip = false; //Bind the texture to which subsequent calls refer to glBindTexture(GL_TEXTURE_2D, *p_sdlSource->GetTexture()); glBegin(GL_QUADS); //Top-left vertex (corner) if(bClip) glTexCoord2f(p_sdlClip->x / p_sdlSource->GetWidth(), p_sdlClip->y / p_sdlSource->GetHeight()); else glTexCoord2i(0, 0); glVertex3f(nX, nY, 0); //Top-right vertex (corner) if(bClip) glTexCoord2f((p_sdlClip->x + p_sdlClip->w) / p_sdlSource->GetWidth(), p_sdlClip->y / p_sdlSource->GetHeight()); else glTexCoord2i(1, 0); glVertex3f(nX + p_sdlClip->w, nY, 0); //Bottom-right vertex (corner) if(bClip) glTexCoord2f((p_sdlClip->x + p_sdlClip->w) / p_sdlSource->GetWidth(), (p_sdlClip->y + p_sdlClip->h) / p_sdlSource->GetHeight()); else glTexCoord2i(1, 0); glVertex3f(nX + p_sdlClip->w, nY + p_sdlClip->h, 0); //Bottom-left vertex (corner) if(bClip) glTexCoord2f(p_sdlClip->x / p_sdlSource->GetWidth(), (p_sdlClip->y + p_sdlClip->h) / p_sdlSource->GetHeight()); else glTexCoord2i(1, 0); glVertex3f(nX, nY + p_sdlClip->h, 0); glEnd(); }