• 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.

// Enable smooth shading.

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

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

// Setup the model matrix.

// 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.
return true;

And finally, I do the actual drawing like this:
// Clear the screen.

// Draw background texture.
glBindTexture(GL_TEXTURE_2D, m_textures[0]);
	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);

// Swap buffers.

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

Share this post

Link to post
Share on other sites
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