Jump to content
  • Advertisement
Sign in to follow this  
fsole

Generating texture coordinates

This topic is 3645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have some problems with this small routine. All it does is generate texture coordinates for a given model, but it is not working properly...Here is the code: void texturize(model3D* m) { int i; float modul,u,v; vector3f vec,vecNorm; for(i=0;i<m->numVertex3D;i++) //Loop for each vertex on the model { //Calculate the vector from the center of the model to the vertex vec.x=m->vertex3DList.x - m->minBox.centre.x; vec.y=m->vertex3DList.y - m->minBox.centre.y; vec.z=m->vertex3DList.z - m->minBox.centre.z; //Normalize the vector modul=modulVector(&vec); //Obtain the modul vecNorm.x=(float)(vec.x/modul); vecNorm.y=(float)(vec.y/modul); vecNorm.z=(float)(vec.z/modul); //Calculate angles u=(float) (atan2(vecNorm.x,vecNorm.z)); v=(float) (acos (vecNorm.y)); //Obtain texture coordinate u=(float) ((auxu + PI) / (2*PI)) ; v=(float)(auxv / PI); m->vertex3DList.u=u; m->vertex3DList.v=v; } } When I try to apply texture to a cube, the texture is applied correctly on the side faces but it is not on the top and bottom face. Does anyone know what is going wrong?? Thanks! PD: I feel sorry because my english is not very good, I hope you can understand... [Edited by - fsole on December 22, 2008 6:01:19 PM]

Share this post


Link to post
Share on other sites
Advertisement
How many faces do you use on the cube? When unwrapping a texture to spherical coordinates you get a seam where the coordinates wraps fro 1 to 0. My guess is that is what's interfering with your cube. Try tesselating your cube to 10x10 sqares per face and you should be able to what's hapening more clearly.

Share this post


Link to post
Share on other sites
Sorry but I did not understand...It's a cube so it has 6 faces...I have tried other formula to change cartesian coordinates to spherical but nothing change...What else can I try?? Has anyone a diferent idea to generate texture coordinates?? any suggestion is accepted :)

Share this post


Link to post
Share on other sites
What are you trying to do - application wise. By this i mean , i know from your post you're trying to programatically generate texture coordinates, but for your model why not use a modeling program to create the coordinates. It will look much better! Alternatively - if you must progamatically generate them consider using OpenGL to generate the texture coordinates automatically using TexGen - you can even make it generate spherical texture coordinates for you.

Share this post


Link to post
Share on other sites
My program has to be able to load any model, with or without texture coordinates, and display it with any texture the user can choose...I can't use glTexGen function because the program is part of a graphics course in the university, and it was specified to generate coordinate texture using this method...So I'm at the same point...Any other idea?

I'm almost sure it has to be a trigonometric problem, this are the mathematical formulas to translate cartesian coordinate to spherical coordinates:

x=r*sin(alpha)*sin(beta)
y=r*cos(alpha)
z=r*sin(alpha)*cos(beta)

so:
alpha=atan(x/z)
beta=acos(y)

as far as I know atan return values between -PI and PI and acos between 0 and PI and we need coordinates between [0,1] and this is exactly what my code is doing but it doesn't work...

PD: Thanks BionicBytes and mzeo77 for the attention :)

[Edited by - fsole on December 22, 2008 5:12:43 PM]

Share this post


Link to post
Share on other sites
Have you tried debugging the values produced for each of the 6-faces on the cube. Perhaps the values are being generated - but are two small to be noticeable. It would be interesting to see what the values are for each of the faces. It's that or the values have been wiped out with other values....
Also, When do you call this method. It would seem to me that this method must be called each frame so that the current face(s) are computed with the spherical coordinates - this is what texgen would have done.

How many verticies are you using in your cube? In an optimal arrangement, only 8 verticies are needed, but you might need more.... Because in your arrangement, its possible that the texture coordinates needed for one face are not good for another face - since the verticies are shared between the faces.

In a general model, you can't generate more verticies so... perhaps regenerating the viewable texture coordinates for a given view point is the best solution. That way the currently viewable verticies will have been updated with current suitable texture coordinates.
So .. i think there is nothing wrong with your maths ... more the application of how you are applying it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!