PDA

View Full Version : cannot map texture to a polygon



shapeare
07-04-2011, 06:11 AM
I got some problem when I tried to map a texture to a polygon, here is the code:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, opacityTexture);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );


glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(50, 1, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(0, 0, 1,
0, 0, 0,
0, 1, 0);

glEnable(GL_TEXTURE_2D);
glColor4f(1.0, 0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.45, -0.45, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.45, -0.45, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.45, 0.45, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.45, 0.45, 0.0);
glEnd();

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

glDisable(GL_TEXTURE_2D);



If I change the third parameter in glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) to GL_ADD, I can get a rectangle with the color defined by glColor().
If the third paramter is GL_REPLACE, then I get a black rectangle, the texture does not appear.
Is there any obvious problems with the code above?

Snow_56767
08-20-2011, 06:27 PM
I would recommend starting simpler when learning texturing. For instance, just create a simple quad then texture it. then you can see what you did wrong / what isn't happening without having to wade through a lot of code.

It also looks as if you are trying to do many things all at once, try to simplify your code by breaking up each operation into a separate function. An example would be like, have a single function you use to setup the camera / viewport, another would load all the textures, another would apply said textures to geometry, and a last would render it all.

I hope that helps.