PDA

View Full Version : Problems with glTexCoord2f(...)



TheBlackAdder
11-17-2005, 12:46 AM
Hi.

Question: "How should i place my (s,t) values in the texel/vertex mapping ?"

I am doing a very simple texture mapping of a chessboard onto a 5-vertex polygon.

Everything seems to work as well, only I am having problems figuring out the mapping between the texels and the vertices.
The results I am getting are looking a bit odd, so my question is how i should arrange the glTexCoord2f().
The results I am getting are:
First here is on top the initial polygon, and below a poor attempt.
http://www.student.dtu.dk/~s030102/texture.bmp
Then i tried a different approach with this result:
http://www.student.dtu.dk/~s030102/texture1.bmp
And now finally this is the best i did:

http://www.student.dtu.dk/~s030102/texture2.bmp

Here is a codefragment that does the mapping of the last pic:


void texture (void) {

glBegin(GL_POLYGON);//GL_LINE_STRIP GL_QUADS
glTexCoord2f(0.0, 0.0); glVertex3fv(&ctrlpoints[0][0]);
glTexCoord2f(0.0, 0.5); glVertex3fv(&ctrlpoints[1][0]);
glTexCoord2f(0.5, 1.0); glVertex3fv(&ctrlpoints[2][0]);
glTexCoord2f(0.5, 0.5); glVertex3fv(&ctrlpoints[3][0]);
glTexCoord2f(0.5, 0.0); glVertex3fv(&ctrlpoints[4][0]);

glEnd();
}
And my whole program code:



#include <GL/glut.h>

GLfloat ctrlpoints [5][3] = {{0.,0.,0.},{0.,0.,5.},{5.,0.,10.},{10.,0.,5.},{10 .,0.,0.} };

#define checkImageWidth 64
#define checkImageHeight 64
GLubyte checkImage[checkImageWidth][checkImageHeight][3];

void makeCheckImage(void)
{
int i, j, c;

for (i = 0; i < checkImageWidth; i++) {
for (j = 0; j < checkImageHeight; j++) {
c = ((((i&amp;0x8)==0)^((j&amp;0x8))==0))*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
}
}
}


void texture (void);

void init (void);
void display (void);
void reshape (int w, int h);
int main(int argc, char** argv);
void initTex(void);

void init(void) {
glClearColor(1.0, 1.0, 1.0, 1.0);
glShadeModel(GL_SMOOTH);
glEnable (GL_DEPTH_TEST);
glEnable (GL_NORMALIZE);
initTex();
}


void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity ();
gluLookAt (5, 5., 5., 5., 0., 5., 0., 0., 1.);

glPushMatrix ();

texture ();

glPopMatrix ();
glFlush();
}


void reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-30.0, 30.0, -30.0*(GLfloat)h/(GLfloat)w,
30.0*(GLfloat)h/(GLfloat)w , -30., 30.);
else
glOrtho(-30.0*(GLfloat)w/(GLfloat)h,
30.0*(GLfloat)w/(GLfloat)h, -30.0, 30.0, -30., 30.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

void initTex()
{
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, checkImageWidth,
checkImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
&amp;checkImage[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_2D);
}

void texture (void) {

glBegin(GL_POLYGON);//GL_LINE_STRIP GL_QUADS
glTexCoord2f(0.0, 0.0); glVertex3fv(&amp;ctrlpoints[0][0]);
glTexCoord2f(0.0, 0.5); glVertex3fv(&amp;ctrlpoints[1][0]);
glTexCoord2f(0.5, 1.0); glVertex3fv(&amp;ctrlpoints[2][0]);
glTexCoord2f(0.5, 0.5); glVertex3fv(&amp;ctrlpoints[3][0]);
glTexCoord2f(0.5, 0.0); glVertex3fv(&amp;ctrlpoints[4][0]);

glEnd();
}

Relic
11-17-2005, 12:55 AM
Hard to say if you don't tell how it should look like.

TheBlackAdder
11-17-2005, 01:01 AM
Ohh of course hehe :)
I am sorry.
I want it to be like a perfekt chessboard square, and then on top a trianglethat has been like cut out of another perfect square chessboard.

So that the pattern looks the same all over, and on the triangle in the top the sides has been kinda cut out.

Is that at all possible?

Anders
11-17-2005, 02:41 AM
Assuming that your first vertex is the lower left corner of the polygon, and that you then proceed counter-clock-wise, I think this would do the trick:

glBegin(GL_POLYGON);//GL_LINE_STRIP GL_QUADS
glTexCoord2f(0.0, 0.0); glVertex3fv(&ctrlpoints[0][0]);
glTexCoord2f(1.0 0.0; glVertex3fv(&ctrlpoints[1][0]);
glTexCoord2f(1.0, 0.5); glVertex3fv(&ctrlpoints[2][0]);
glTexCoord2f(0.5 1.0); glVertex3fv(&ctrlpoints[3][0]);
glTexCoord2f(0.0, 0.5); glVertex3fv(&ctrlpoints[4][0]);