PDA

View Full Version : Calculating texture Coordinates



WillsB
03-18-2009, 05:13 AM
Hello all,

I seem to be having a bit of trouble understaning how to calculate texture coordinates for triangle strips, Im trying to texture a flat plane (made of triangle strips), and later I want a shader to use these tex coordiantes (I think i may have managed this but I wont know properly until I can get the correct initial texture coordinates)

So what I have is


for (int x = 0; x < SIZE - 1; x++)
{
// Draw A Triangle Strip For Each Column Of Our Mesh
gl.glBegin(GL.GL_TRIANGLE_STRIP);
for (int z = 0; z < SIZE - 1; z++) {
// Set The Wave Parameter Of Our Shader To The Incremented Wave Value From Our Main Program
if (vertexShaderEnabled)
{
gl.glVertexAttrib1f(waveAttrib, wave_movement);
}
gl.glTexCoord2f(x/SIZE,z/SIZE);
gl.glVertex3f(mesh[x][z][0], mesh[x][z][1], mesh[x][z][2]); // Draw Vertex
gl.glTexCoord2f(x,z);
gl.glVertex3f(mesh[x + 1][z][0], mesh[x + 1][z][1], mesh[x + 1][z][2]); // Draw Vertex
wave_movement += 0.00001f; // Increment Our Wave Movement
if (wave_movement > TWO_PI) { // Prevent Crashing
wave_movement = 0.0f;
}
}
gl.glEnd();

As you may have noticed its an adaption from one of NeHe's lessons, however i'm having difficulty visualising how i need the texture coordinates to map onto the vertex coordinates :(.
Im fine with understaning mapping texture coords onto a quad (it seems much easier)!

Can anyone shed any light on this for me please :)

Thanks,

Regards
Wills

BionicBytes
03-18-2009, 06:37 AM
I wonder if you correctly understand what kind of primative you are generating texture coords for?
It looks like you are creating n columns of vertical triangle strips. To generate such Triangle strips, you specify two vertex positions each time. The second time you specify the two vertex positions, a quad (of 2 triangles) is formed between the newly specified points and the previous iteraton.
In your case, you need to specify the texture coords at each vertex going down the strip and then for each column of strips.
The texture coords are going to be in the 0..1 range and so you must use a fraction of the total with or height when calculating them at each vertex. Thankfully this is easy:

the first vextex will use (as you already have)
gl.glTexCoord2f(x/SIZE,z/Size);
and the second
gl.glTexCoord2f((x+1)/SIZE,(z+1)/Size);

The only complication is the polygon winding order (clockwise or anti) and if you can't see the texture, you'll need to change the order specified here.

WillsB
03-18-2009, 07:40 AM
Thank you for the help, I'm very grateful :)

However as you said it looks like the polgon winding order is causing issues. Does this mean that I can easily reverse it with an openGL command, or do I need to somehow rejig the order in which I specify the verticies, I tried just swapping the verticies (as a shot in the dark really, I sort of figured it wouldn't work because of the loops). So it looks like I need to change the loops structure in a more drastic way (I can't imagine how yet!)


After re-reading the code and your helpful post, I sort of see whats happening more clearly now. Im making a column for the X direction of 2 points each time I loop round a vertex in the Z (plane?). Next time I incriment the z value i draw 2 more point so for the first iteration of the X loop i end up with:

+x-> +z = ^
|

* (x=0,z=0)
|
|
* (x=0,z=1)
|
|
* (x=0,z=2)
|
|
..
|
|
* (x=0,z=SIZE-1)

so i have a full column in x=0, next iteration of x becomes:


+x-> +z = ^
|

* *(x=1,z=0)
| /|
|/ |
* *(x=1,z=1)
| /|
|/ |
* *(x=1,z=2)
|
|
..
| /|
|/ |
* *(x=1,z=SIZE-1)


Actually, looking at that yet again, i've got a feeling I may have got something slightly wrong in my diagram - unless im still completely off :(

Again, thanks for the explanations - :)

Cheers
Regards
Wills

WillsB
03-18-2009, 08:14 AM
One More thing, in my current scene, some I have rendered a box which moves up and down, the front face goes from


gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);

however I had to scale it with values of gl.glScalef(2.0f,5.0f, 2.0f); to make it actually look square, not a cuboid (as it did before the scale) This points to my matricies being messed up somewhere doesnt it? and perhaps as a result of this the perspective of the object is completely off, its like OpenGL is over compenstating for the perspective. I tried doing a PushMatrix and then loadIdentity before I draw my cube but then my view goes strange, (i tried to draw a huge teapot after doing loadIdentity so I could see where the origin of the new matrix was in relation to all my other objects, however when this is done my room box(like skybox) texture seems to be wrapping around something and moving the view (with gluLookAt) doesnt work as it did before (if i remove the teaport the view control works again as normal) but I still cant see my cube.

Whats going on :o

Thanks
Wills

BionicBytes
03-18-2009, 12:18 PM
The polygon winding order can be changed within the loop so that the vertex and texture coords are ordered in reverse.

change

the first vextex will use (as you already have)
gl.glTexCoord2f(x/SIZE,z/Size);
and the second
gl.glTexCoord2f((x+1)/SIZE,(z+1)/Size);

to

the first vextex will use (as you already have)
gl.glTexCoord2f((x+1)/SIZE,(z+1)/Size);
and the second
gl.glTexCoord2f(x/SIZE,z/Size);


Generally speaking you should try and order the texture coordinates and vertex positions to fit in with the rest of Open GL's expected winding order.
You can change it however, with

glFontFace (GL_CW); //swap the order to anti clockwise
or
glFrontFace (GL_CCW); //default

BionicBytes
03-18-2009, 12:56 PM
Visit a site like http://nehe.gamedev.net and look up the basics on setting up your projection & view matricies.
The screen resolution also plays a part in what you are seeing as well because you may well have set the perspective projection using an aspect ratio using screenwidth/screenheight - which on a wide screen monitor will give a letter box projection.
HeNE is a good site with lots of info, so take your time and try to understand the exampes one at a time!

WillsB
03-18-2009, 01:22 PM
Humn, odd - as I mentioned in my previous post (I probably wasnt clear enough :o ) I did try swapping the coords as you suggested and it didnt work, Ive just tried it again and the sames happened, just cant see any texture atall even attampting to be mapped (i tested that it was loading the texture correctly and that everything else was in order by using just the 0 and 1 coords so that i ended up with the plane which has one copy of the texture per triangle pair, and that worked - but obviously looked really wierd)).

It's odd - grey water is ugly.

WillsB
03-18-2009, 01:41 PM
Ah ha!

Im getting somewhere, i had a search around, and found that because i was doing


gl.glTexCoord2f((x+1)/SIZE,(z+1)/SIZE);
and
gl.glTexCoord2f(x/SIZE,z/SIZE);

I was using x and z as ints, and read that when dividing by another int (in this case SIZE) it sometimes causes a rounding error, so i chaged what i had to


gl.glTexCoord2f((xAsFloat+1)/SIZE,(zAsFloat+1)/SIZE);
and
gl.glTexCoord2f(xAsFloat/SIZE,zAsFloat/SIZE);

where i declare xAsFloat = x at the start of each loop for x, and zAsFloat = z at the start of each loop for z.

This change results in this result:

http://img11.imageshack.us/img11/8735/frostedglasswater.th.jpg (http://img11.imageshack.us/my.php?image=frostedglasswater.jpg)

As you can see its sort of a frosted glass effect on the water texture, Is seems that theres some sort of offset missing or something.

WillsB
03-18-2009, 01:51 PM
Think ive fixed it! will post back in a minute!

WillsB
03-18-2009, 04:57 PM
All done!

I just played with the x/z coords and its working perfectly - thanks BionicBytes for your help :)