• Advertisement
Sign in to follow this  

[SDL/OGL] Textures

This topic is 3796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble loading an SDL Surface and converting it to a texture for OpenGL. When I try to draw a quad textured with the loaded texture, it just shows up as white. If I draw the quad colored, without a texture, it shows up as the color, so I know it's working to some extent. Anyways, here's the OpenGL initialization code:
// Enable texture mapping.
glEnable(GL_TEXTURE_2D);

// Enable smooth shading.
glShadeModel(GL_SMOOTH);

// Set the clear color.
glClearColor(0, 0, 0, 0);

// Setup the projection matrix.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_W, SCREEN_H, 0, -1, 1);

// Setup the model matrix.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Check for errors.
if (glGetError() != GL_NO_ERROR) {
	return false;
}

I load textures with this:
// The temporary surface to load.
SDL_Surface *temp = NULL;

// Load the specified file.
if (temp = SDL_LoadBMP("data/gfx/test.bmp"))
{
	// Create space for the texture.
	glGenTextures(1, &m_textures[target]);

	// Set as the active texture.
	glBindTexture(GL_TEXTURE_2D, m_textures[target]);

	// Generate the texture.
	glTexImage2D(GL_TEXTURE_2D, 0, 3, temp->w, temp->h, 0, GL_BGR,
			GL_UNSIGNED_BYTE, temp->pixels);
} else {
	return false;
}

// Free temporary surface.
SDL_FreeSurface(temp);
return true;

And finally, I do the actual drawing like this:
// Clear the screen.
glClear(GL_COLOR_BUFFER_BIT);

// Draw background texture.
glBindTexture(GL_TEXTURE_2D, m_textures[0]);
glBegin(GL_QUADS);
	glTexCoord2f( 0.0f, 1.0f ); glVertex3f(0, 0, 1);
	glTexCoord2f( 1.0f, 1.0f ); glVertex3f(SCREEN_W, 0, 1);
	glTexCoord2f( 1.0f, 0.0f ); glVertex3f(SCREEN_W, SCREEN_H, 1);
	glTexCoord2f( 0.0f, 0.0f ); glVertex3f(0, SCREEN_H, 1);
glEnd();

// Swap buffers.
SDL_GL_SwapBuffers();

NOTE: the texture coordinates are strange because of the way SDL loads the images.

Share this post


Link to post
Share on other sites
Advertisement
OK, I figured it out. Since I was loading a PNG (I'm not in the above example, but that was my goal), I had to specify GL_RGBA, because of the alpha channel I guess.

Anyways, this thread isn't wasted, because I have another question. Do textures HAVE to be powers of 2? I have a 640x480 texture that is showing up just fine, but will I run into problems later? Every example I've seen so far treats a texture that isn't a power of 2 like it's an error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement