Archived

This topic is now archived and is closed to further replies.

Crispy

Texture coordinates

Recommended Posts

Crispy    556
Problem: I''ve calculated texture coordinates for each of the polys in the little virtual world of mine. Now I need to draw those polys with the textures mapped on them correctly. I''ve never done this in an ''automated'' fashion (autocalculating tex coords) and with randomly shaped polys. I gather that I''d need to know the orientation of each poly to map the texture correctly: for example a poly on the XY plane would require something like glTexCoord3f(u, v, w), but a poly on the YZ plane would need glTexCoord3f(v, w, w) or something. I''ve no idea how to texture map an arbitrarily banked poly... Probably these calls are wrong, but that''s why I''m posting this - how do I define how the textures need to be mapped on a poly? Erm... incidentally - does this make sense at any point at all? crispy

Share this post


Link to post
Share on other sites
Crispy    556
I suppose I did forget to say please, so...

pleeeez

I can''t find any info on it on the Net either (maybe it''s just me) so I''d really really appreciate any help...

thanks,
crispy

Share this post


Link to post
Share on other sites
alargeduck    122
Im going to assume you are not using 3d textures.

You only need to specify two texture coordinates. saying glTexCoord2f(0, 0); will tell openGL to "attach" the bottom left corner of the texture map to whatever vertex you specify. If you had a quad you needed to texture map, you would specifty glTexCoord2f(0,0); then (0,1), (1,1), and so on. Same goes for triangles and other shapes.



Sorry if im stating the obvious, but thats the best i can do at this time of day. email me @ demosh@attbi.com if you want/need further explanation

Share this post


Link to post
Share on other sites
Crispy    556
I have already calculated the texture coords for everything. After mapping them, though, OpenGL doesn't draw the textures "correctly" (hehe...). Here's the code I'm using to calculate the texture coordinates for each vertex:



void SetTextureCoords(TPolygon * p)
{
float lowest = p->v[0].x, highest = p->v[0].x, range = 0, offset = 0;

for(int j = 0; j < 4; j++)
{
if(p->v[j].x < lowest) lowest = p->v[j].x;
if(p->v[j].x > highest) highest = p->v[j].x;
}

range = (lowest - highest) * -1;
offset = 0 - lowest;
int abspos;

for(int j = 0; j < 4; j++)
{
abspos = p->v[j].x + offset;
if(range) p->u.push_back((float)abspos / (float)range);
else p->u.push_back(0);
}

//...
//Repeat for y and z coordinates of the poly to calculate v and w, respectively
//...
}


As I figure it, I need to calculate the texture coordinates in three dimensions because if I only used the XY plane, I'd get wrong results if the polygon didn't lie on the same plane.

I know there's somthing wrong with this - I just don't know what.
For example, using the above method, mapping every polygon that isn't on the YZ plane is texture mapped correctly (if I use glTexCoord2f(p->v[j], p->u[j]) ), everything else is all messed up. v in TPolygon is a vector of structs called TVector (standard structure), u, v and w are vectors of floats each.

thanks - more help needed though

crispy

EDIT: darn tags...



[edited by - crispy on May 4, 2002 7:28:36 AM]

Share this post


Link to post
Share on other sites
Sander    1332
I think you are just doing it waaaay to difficult. If you use 2D textures you only need to set u and v. If you set w I think it will scale the u and v by w. Just enter the pixel coordinates in glTexCoord(); after using the following formula:

pX and pY are the x and y pixel coords on the texture. w and h are the texures width and height:

u = pX/w;
v = 1.0f - (pY/h);
glTexCoords(u, v);

OpenGL will transform everything to the 3D polygon and map the texture correctly.

- An eye for an eye will make the world go blind -

Share this post


Link to post
Share on other sites