Sign in to follow this  
Erondial

2D Texture Mapping

Recommended Posts

Erondial    172
Okay, I read the tutorial on-site, as well as several offsite, and I just don't get why this isn't working.

typedef struct {
	unsigned short int w;
	unsigned short int h;
	unsigned char colour_type;
	unsigned char bit_depth;
	unsigned char* data;
	unsigned int textureid;
} ImageData;

void GenerateTexture(ImageData* structure)
{
	GLuint texture;
	glGenTextures( 1, &texture );
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, structure->w, structure->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, structure->data);
	structure->textureid = texture;
    delete[] structure->data;
}
Now for some reason, when I try to map that to a 2d quad, it doesn't want to work. The image is loaded properly, the data field of the structure is filled, so are width and height.
glBindTexture( GL_TEXTURE_2D, structure->textureid );
	glBegin(GL_QUADS);
		glVertex2d(x,y);
		glVertex2d(x+structure->w,y);
		glVertex2d(x+structure->w,y+structure->h);
		glVertex2d(x,y+structure->h);
	glEnd();
That's how I'm trying to draw the quad... it works, but I only get a white square... any idea why the texture's not being applied? And yes, I did call glEnable(GL_TEXTURE_2D);. So yes. Any help would be greatly appreciated :)

Share this post


Link to post
Share on other sites
AndyEsser    394
Not sure if it's the same because I program in VB but don't you have to specify the TExture coords you want to map to the quad?

eg

glBegin bmQuads
glNormal3f 1, 0, 0
glTexCoord2f 1, 0: glVertex3f 1, 0.5, -0.5
glTexCoord2f 0, 0: glVertex3f 1, 0.5, -1
glTexCoord2f 0, 1: glVertex3f 1, 0, -1
glTexCoord2f 1, 1: glVertex3f 1, 0, -0.5
glEnd

Share this post


Link to post
Share on other sites
Thunder_Hawk    314
Quote:
Original post by AndyEsser
Not sure if it's the same because I program in VB but don't you have to specify the TExture coords you want to map to the quad?

eg

glBegin bmQuads
glNormal3f 1, 0, 0
glTexCoord2f 1, 0: glVertex3f 1, 0.5, -0.5
glTexCoord2f 0, 0: glVertex3f 1, 0.5, -1
glTexCoord2f 0, 1: glVertex3f 1, 0, -1
glTexCoord2f 1, 1: glVertex3f 1, 0, -0.5
glEnd


Yes, you are correct...the drawing code should look something like this


glBindTexture( GL_TEXTURE_2D, structure->textureid );

glBegin(GL_QUADS);

glTexCoord2f (0.0f, 0.0f); glVertex2d(x,y);

glTexCoord2f (1.0f, 0.0f); glVertex2d(x+structure->w,y);

glTexCoord2f (1.0f, 1.0f); glVertex2d(x+structure->w,y+structure->h);

glTexCoord2f (0.0f, 1.0f); glVertex2d(x,y+structure->h);

glEnd();



The purpose of glTexCoord* is to tell the graphics card what part of the image maps to which vertex of (in the case) the quad.

(0, 0) maps to the bottom left corner of the source image and (1, 1) maps to the top right corner of the source image...any values in between are equally valid inputs

Also check to make sure that the dimensions of the picture are powers of two (ex. 64x128, 1024x1024, 512x256, etc.), as OpenGL makes that size requirement. There are ways to get around this, but you should probably get used to the concept of texture coordinates before delving into that topic.

Share this post


Link to post
Share on other sites
Erondial    172
AHA! YES! I now have a thin red and white bar instead of a white quad. Granted, it's not what I wanted, but damnit, it's a start! Thank you all, very much!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this