Jump to content


SDL/openGL Texturing Problem

opengl sdl textures

4 replies to this topic

#1 Darir

    New Member

  • Members
  • Pip
  • 2 posts

Posted 07 January 2012 - 05:58 AM

I'm writing a program that needs to display a textured 32x32 square from a 32x32 section of a tilemap. I'm using sdl to divide up the tilesheet and then register each individual tile as a separate texture.The problem is that nothing renders at all. I've checked the code out a good number of times, so I was hoping someone else would spot the problem. Thanks.

struct Texture {
SDL_Surface* tex;
GLuint texHandle;
GLint colorNum;
GLenum format;
};
//class render holds a tilesheet and manages it
class renderer {
private:
short width, height;
SDL_Surface* tilesheet;
std::vector<Texture*> tile;

//null will render to the screen
SDL_Surface* destination;
SDL_Surface* loadOptimized(std::string filename);
bool divideTilesheet();
public:
//regular loaded constructor
renderer(std::string filename, SDL_Surface* udestination);
//for creating a renderer with a procedurally generated tilesheet
renderer(SDL_Surface* utilesheet, SDL_Surface* udestination);

//tilex and tiley are both zero-based, so the upper-left tile would be (0, 0)
void render(int tilex, int tiley, int locx, int locy);

SDL_Surface* getResult() {return destination;}
};
renderer::renderer(std::string filename, SDL_Surface* udestination) {
tilesheet = loadOptimized(filename);
destination = udestination;
width = tilesheet->w/tileSize;
height = tilesheet->h/tileSize;
divideTilesheet();
}
renderer::renderer(SDL_Surface* utilesheet, SDL_Surface* udestination) {
tilesheet = utilesheet;
destination = udestination;
width = tilesheet->w/tileSize;
height = tilesheet->h/tileSize;
divideTilesheet();
}
bool renderer::divideTilesheet() {
//make sure we don't leave any previous tiles
tile.clear();
for(int y = 0; y < height; y++) {
  for(int x = 0; x < width; x++) {
   SDL_Surface* newTile = NULL;
   newTile = SDL_CreateRGBSurface(0, tileSize, tileSize, tileSize, 0, 0, 0, 0);
   SDL_Rect clip;
   clip.x = x*tileSize;
   clip.y = y*tileSize;
   clip.w = tileSize;
   clip.h = tileSize;
   SDL_Rect dest;
   dest.x = 0;
   dest.y = 0;
   SDL_BlitSurface(tilesheet, &clip, newTile, &dest);
   //the texture is created, so now we have to register it
   Texture* newTex = new Texture();
   newTex->tex = newTile;
   newTex->colorNum = newTex->tex->format->BytesPerPixel;
   if(newTex->colorNum == 4) {
	if(newTex->tex->format->Rmask == 0x000000ff)
	 newTex->format = GL_RGBA;
	else
	 newTex->format = GL_BGRA;
   }
   else if(newTex->colorNum == 3) {
	if(newTex->tex->format->Rmask == 0x000000ff)
	 newTex->format = GL_RGB;
	else
	 newTex->format = GL_BGR;
   }
   glGenTextures(1, &(newTex->texHandle));
   glBindTexture(GL_TEXTURE_2D, newTex->texHandle);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   SDL_LockSurface(newTex->tex);
   glTexImage2D(GL_TEXTURE_2D, 0, newTex->colorNum, newTex->tex->w, newTex->tex->h, 0, newTex->format, GL_UNSIGNED_BYTE, newTex->tex->pixels);
   SDL_UnlockSurface(newTex->tex);
  
   tile.push_back(newTex);
  }
}
return true;
}
SDL_Surface* renderer::loadOptimized(std::string filename) {
SDL_Surface* loaded = NULL;
SDL_Surface* optimized = NULL;
loaded = SDL_LoadBMP(filename.c_str());
if(loaded != NULL) {
  optimized = SDL_DisplayFormat(loaded);
  SDL_FreeSurface(loaded);
}
else return NULL;
return optimized;
}
void renderer::render(int tilex, int tiley, int locx, int locy) {
if(destination) {
  SDL_Rect dest;
  dest.x = locx;
  dest.y = locy;
  SDL_BlitSurface(tile[tiley*width + tilex]->tex, NULL, destination, &dest);
}
else {
  //render to the screen with opengl
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, tile[tiley*width + tilex]->texHandle);
  glBegin(GL_QUADS);
   glTexCoord2f(0, 0);
   glVertex2f(float(locx), float(locy));
   glTexCoord2f(1, 0);
   glVertex2f(float(locx + tileSize), float(locy));
   glTexCoord2f(1, 1);
   glVertex2f(float(locx + tileSize), float(locy + tileSize));
   glTexCoord2f(0, 1);
   glVertex2f(float(locx), float(locy + tileSize));
  glEnd();
  glDisable(GL_TEXTURE_2D);
}
}

It looks like the problem is somewhere in renderer::divideTilesheet.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 07 January 2012 - 08:13 AM

The SDL_CreateRGBSurface call looks suspicious to me. The fourth parameter is supposed to be the bit depth but you're passing tileSize a third time? I guess that would happen to work if tileSize == 32; still, you're not saying what you mean there.

The only other thing I thought of was that you're not setting glColor anywhere; by default, the current color is multiplied with the texture when rendering, so if the color is black you'd get all black tiles. Try inserting glColor4f(1, 1, 1, 1) before your GL rendering code.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Darir

    New Member

  • Members
  • Pip
  • 2 posts

Posted 07 January 2012 - 04:49 PM

The SDL_CreateRGBSurface call was a mistake. Thanks for pointing it out, but it doesn't look like it's helped at all. I've tried the glColor method, but that wasn't working, either; I removed it before I originally posted here. I even tried rendering a regular square without texturing just below the textured one in the code to make sure that it hadn't somehow been transformed out of the view of the screen. It rendered, but I still can't get the textured square to render at all. It won't render black, white, or any other color; I've changed the background color several times to make sure of that. I've also checked to make sure that every tile gets a texture handle and they do, which is making me think that the mistake is either with generating the tiles with SDL or with the rendering code.

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 07 January 2012 - 07:39 PM

Hmm. So an untextured square rendered fine, but as soon as you turned on texturing, it vanishes altogether? You might try disabling blending and alpha testing, in case by some mischance one of those is being turned on and you're getting a zero alpha in your texture. What if you make a 1x1 pure white texture? E.g.
unsigned char white[4] = { 255, 255, 255, 255 };
glTexImage2D(GL_TEXTURE_2D, 0, 4, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, white);

Also try sprinkling some glGetError() calls around your code, to see if OpenGL is generating any error codes.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 08 January 2012 - 09:19 AM

I would also check the value returned from SDL_BlitSurface.
"Stupid bug! You go squish now!!" - Homer Simpson





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users