Jump to content
  • Advertisement
Sign in to follow this  
SharQueDo

OpenGL [2D] Doesn't draw texture

This topic is 4620 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

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;
}

Share this post


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

Share this post


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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!