choblocken

02-15-2018, 11:17 AM

Hello,

I am currently having an issue with mapping my texture correctly to fit the way I would like it on the object I am defining. I know the problem lies within my texture co-ordinates but for the life of me I cant figure out why.

I want the texture to wrap along with the triangles so it is in the middle and warping around it like so:

2671

I'm using two different triangles together to form squares which act as the individual boxes in the grid from the image above.

However, when I run my code mode my texture is in the middle of the window instead of wrapping around the object like so:

2670

Again I know the problem lies with the glTexCoords but I cant figure out the correct co-ordinates to get the texture to behave like the first image.

Below is my code defining the "grid" and texture co-ordinates:

float Cx = 0.5,Cy = 0.5; //x & y co-ord of center of cylinder

float r1 = 0.17,r2 = 0.47,r3 = r2-r1; //radius of first & last row of pixels

float n=20,m=10; //steps across and up

float dr=(r3)/(2.0*m); //delta radius (up)

float dt=3.14159/n;//delta theta (across)

for(float j=0;j<=(r2-r1);j+=dr) // Up

{

for(float theta=0; theta<=3.14159; theta+=dt)

{

float Px1=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py1=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta);

float Px2=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py2=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta);

float Px3=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py3=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta)

float Px4=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py4=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta);

//glColor3f(0,1,0);

glBegin(GL_TRIANGLES);

glTexCoord2f(Px1-0.007+(r3*j)*cos(theta),Py1+0.007+(r3*j)*sin(theta ));

glVertex2f(Px1-0.007,Py1+0.007);

glTexCoord2f(Px2+0.007+(r3*j)*cos(theta),Py2+0.007 +(r3*j)*sin(theta));

glVertex2f(Px2+0.007,Py2+0.007);

glTexCoord2f(Px3+0.007+(r3*j)*cos(theta),Py3-0.007+(r3*j)*sin(theta));

glVertex2f(Px3+0.007,Py3-0.007);

glBegin(GL_TRIANGLES);

glTexCoord2f(Px1-0.007+(r3*j)*cos(theta),Py1+0.007+(r3*j)*sin(theta ));

glVertex2f(Px1-0.007,Py1+0.007);

glTexCoord2f(Px3+0.007+(r3*j)*cos(theta),Py3-0.007+(r3*j)*sin(theta));

glVertex2f(Px3+0.007,Py3-0.007);

glTexCoord2f(Px4-0.007+(r3*j)*cos(theta),Py4-0.007+(r3*j)*sin(theta));

glVertex2f(Px4-0.007,Py4-0.007);

glEnd();

}

}

Any help would be appreciated to point me in the correct direction!

I am currently having an issue with mapping my texture correctly to fit the way I would like it on the object I am defining. I know the problem lies within my texture co-ordinates but for the life of me I cant figure out why.

I want the texture to wrap along with the triangles so it is in the middle and warping around it like so:

2671

I'm using two different triangles together to form squares which act as the individual boxes in the grid from the image above.

However, when I run my code mode my texture is in the middle of the window instead of wrapping around the object like so:

2670

Again I know the problem lies with the glTexCoords but I cant figure out the correct co-ordinates to get the texture to behave like the first image.

Below is my code defining the "grid" and texture co-ordinates:

float Cx = 0.5,Cy = 0.5; //x & y co-ord of center of cylinder

float r1 = 0.17,r2 = 0.47,r3 = r2-r1; //radius of first & last row of pixels

float n=20,m=10; //steps across and up

float dr=(r3)/(2.0*m); //delta radius (up)

float dt=3.14159/n;//delta theta (across)

for(float j=0;j<=(r2-r1);j+=dr) // Up

{

for(float theta=0; theta<=3.14159; theta+=dt)

{

float Px1=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py1=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta);

float Px2=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py2=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta);

float Px3=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py3=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta)

float Px4=Cx+((r1+j)*cos(theta));//+(r1+(dr)*i)*cos(theta);

float Py4=Cy+((r1+j)*sin(theta));//+(r1+(dr)*j)*sin(theta);

//glColor3f(0,1,0);

glBegin(GL_TRIANGLES);

glTexCoord2f(Px1-0.007+(r3*j)*cos(theta),Py1+0.007+(r3*j)*sin(theta ));

glVertex2f(Px1-0.007,Py1+0.007);

glTexCoord2f(Px2+0.007+(r3*j)*cos(theta),Py2+0.007 +(r3*j)*sin(theta));

glVertex2f(Px2+0.007,Py2+0.007);

glTexCoord2f(Px3+0.007+(r3*j)*cos(theta),Py3-0.007+(r3*j)*sin(theta));

glVertex2f(Px3+0.007,Py3-0.007);

glBegin(GL_TRIANGLES);

glTexCoord2f(Px1-0.007+(r3*j)*cos(theta),Py1+0.007+(r3*j)*sin(theta ));

glVertex2f(Px1-0.007,Py1+0.007);

glTexCoord2f(Px3+0.007+(r3*j)*cos(theta),Py3-0.007+(r3*j)*sin(theta));

glVertex2f(Px3+0.007,Py3-0.007);

glTexCoord2f(Px4-0.007+(r3*j)*cos(theta),Py4-0.007+(r3*j)*sin(theta));

glVertex2f(Px4-0.007,Py4-0.007);

glEnd();

}

}

Any help would be appreciated to point me in the correct direction!