[2D] Doesn't draw texture

Started by
4 comments, last by SharQueDo 18 years, 4 months ago
Hi! :D I'm making a 2d game, and I want to use OpenGL. I want to load and draw a texture, but it doesn't draw the texture. It does draw the right image size (32x32size at 100x100location draws a 32x32 white square at 100x100), though. Initialization:

	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
class CSprite
{
public:
	...
	bool Load(const char *file); // Load sprite
	...
	bool Draw(int x, int y); // Draw the sprite
private:
	GLuint texture; // Texture data
	AUX_RGBImageRec *g_picture; // OGL image data
};
bool CSprite::Load(const char *file)
{
	g_picture = auxDIBImageLoad(file);

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);

	glTexImage2D(GL_TEXTURE_2D, 0, 4, g_picture->sizeX, g_picture->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, g_picture->data);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	// Some debug info
	printf("Done loading %s\n", file);
	return true;
}
bool CSprite::Draw(int x, int y)
{
	glTranslatef(x, y, 0.0f);

	glBindTexture(GL_TEXTURE_2D, texture);

	glBegin(GL_QUADS);
		glTexCoord2f(0,0); glVertex2d(0.0f, 0.0f); // top left
		glTexCoord2f(1,0); glVertex2d(g_picture->sizeX, 0.0f); // top right
		glTexCoord2f(1,1); glVertex2d(g_picture->sizeX, g_picture->sizeY); // bottom left
		glTexCoord2f(0,1); glVertex2d(0.0f, g_picture->sizeY); // bottom right
	glEnd();

	return true;
}
Advertisement
make sure you have texturemapping enabled. (glEnable(GL_TEXTURE_2D))
maybe also make sure you have lighting disabled.
Doesn't work... :(

Quick question: Do I need to free g_picture and g_picture->data? I saw that on NeHe's tuts, but when I try to free those the program just quits with an unknown error.
yes, you can free() the picture (in the order of g_picture->data, then g_picture itself) after you have uploaded it to the video card.

What format is the image you are loading?
I tried 24b and 8b BMPs, they were both loaded, but neither were blitted.
DOH!!

My texture wasn't in the power of 2 (16x16, 32x32, 64x64, etc), it was 15x15.

Thnx guys. <D

This topic is closed to new replies.

Advertisement