Jump to content

  • Log In with Google      Sign In   
  • Create Account


Working with UV coordinates when they are not in the range of 0 - 1

  • You cannot reply to this topic
13 replies to this topic

#1 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 09 July 2014 - 05:52 AM

Hello,

I am using a loading library which supplies UV coordinates as shown below.

Note that these coordinates are not in the range of 0.0 - 1.0

Therefore the textures aren't mapping properly and the end results suck.

I tried using GLSL's clamp as shown below in the fragment shader but that doesn't work.

How do I work with these UV coordinates?

Thank you for your time:

GLSL shader snippet:
 
MaterialTextureColor = texture2D(MainTextureSampler, clamp(TextureCoordinates, 0, 1));
UV Coordinates:

Note that this is just a textual representation, the coordinates are floats within C++/OpenGL when loaded.
 
UV Point: S: -53.0558 T: -15.3896
UV Point: S: 5.99937 T: -15.3896
UV Point: S: -3.84315 T: 2.00539
UV Point: S: -3.84315 T: 2.00539
UV Point: S: 5.99937 T: -15.3896
UV Point: S: 5.99937 T: 2.00539
UV Point: S: -53.0558 T: -15.3896
UV Point: S: -3.84315 T: 2.00539
UV Point: S: -53.0558 T: 4.29541
UV Point: S: -3.84315 T: 4.29541
UV Point: S: -53.0558 T: 4.29541
UV Point: S: -3.84315 T: 2.00539
UV Point: S: -3.84315 T: -2.00539
UV Point: S: -53.0558 T: -4.29541
UV Point: S: -3.84315 T: -4.29541
UV Point: S: -3.84315 T: -2.00539
UV Point: S: 5.99937 T: 15.3896
UV Point: S: -53.0558 T: 15.3896
UV Point: S: 5.99937 T: 15.3896
UV Point: S: -3.84315 T: -2.00539
UV Point: S: 5.99937 T: -2.00539
UV Point: S: -3.84315 T: -2.00539
UV Point: S: -53.0558 T: 15.3896
UV Point: S: -53.0558 T: -4.29541
UV Point: S: -2.00539 T: -0.492126
UV Point: S: 15.3896 T: -0.492126
UV Point: S: 15.3896 T: 3.75462e^-009
UV Point: S: 15.3896 T: 3.75462e^-009
UV Point: S: -2.00539 T: 3.75462e^-009
UV Point: S: -2.00539 T: -0.492126
UV Point: S: -3.84315 T: -0.492126
UV Point: S: 5.99937 T: -0.492126
UV Point: S: 5.99937 T: 3.75462e^-009
UV Point: S: 5.99937 T: 3.75462e^-009
UV Point: S: -3.84315 T: 3.75462e^-009
UV Point: S: -3.84315 T: -0.492126
UV Point: S: -4.29541 T: -0.492126
UV Point: S: -2.00539 T: -0.492126
UV Point: S: -2.00539 T: 3.75462e^-009
UV Point: S: -2.00539 T: 3.75462e^-009
UV Point: S: -4.29541 T: 3.75462e^-009
UV Point: S: -4.29541 T: -0.492126
UV Point: S: -53.0557 T: -0.492126
UV Point: S: -3.84315 T: -0.492126
UV Point: S: -53.0557 T: 3.75462e^-009
UV Point: S: -3.84315 T: 3.75462e^-009
UV Point: S: -53.0557 T: 3.75462e^-009
UV Point: S: -3.84315 T: -0.492126
UV Point: S: -15.3896 T: -0.492126
UV Point: S: 4.29541 T: -0.492126
UV Point: S: 4.29541 T: 3.75462e^-009
UV Point: S: 4.29541 T: 3.75462e^-009
UV Point: S: -15.3896 T: 3.75462e^-009
UV Point: S: -15.3896 T: -0.492126
UV Point: S: 5.99937 T: 0.492126
UV Point: S: -53.0558 T: 0.492126
UV Point: S: -53.0558 T: -3.75462e^-009
UV Point: S: -53.0558 T: -3.75462e^-009
UV Point: S: 5.99937 T: -3.75462e^-009
UV Point: S: 5.99937 T: 0.492126

Edited by tmason, 09 July 2014 - 05:56 AM.


Sponsor:

#2 JordanBonser   Members   -  Reputation: 326

Like
0Likes
Like

Posted 09 July 2014 - 06:31 AM

That seems like a pretty strange representation, are you sure there is not a problem with the file or the way it is being exported from the program it is being created in?

 

The only other thing I could think of is finding out what the number range is and then doing a pre-process step to convert the UV  co-ordinates in the 0 to 1 range. 



#3 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 09 July 2014 - 06:35 AM

JordanBonser, on 09 Jul 2014 - 08:31 AM, said:JordanBonser, on 09 Jul 2014 - 08:31 AM, said:

That seems like a pretty strange representation, are you sure there is not a problem with the file or the way it is being exported from the program it is being created in?

 

The only other thing I could think of is finding out what the number range is and then doing a pre-process step to convert the UV  co-ordinates in the 0 to 1 range. 

 

 

I am using AutoDESK's FBX library; and I am using their example code like so:

bool lUnmappedUV;
pMesh->GetPolygonVertexUV(lPolygonIndex, lVerticeIndex, lUVName, lCurrentUV, lUnmappedUV);
lUVs[lVertexCount * UV_STRIDE] = static_cast<float>(lCurrentUV[0]);
lUVs[lVertexCount * UV_STRIDE + 1] = static_cast<float>(lCurrentUV[1]);

I'll look for the number range, that might help.

 

Thank you.


Edited by tmason, 09 July 2014 - 06:35 AM.


#4 JordanBonser   Members   -  Reputation: 326

Like
1Likes
Like

Posted 09 July 2014 - 07:35 AM

I'm not familiar with the FBX SDK but a quick look up shows me that the last parameter says whether the UV's are actually mapped to the vertex and also that the GetPolygonVertexUV returns a bool to say whether it succeeds. 

 

I would check both of these to ensure that there isn't something going wrong and that the data is being filled correctly. 



#5 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 09 July 2014 - 07:46 AM

I'm not familiar with the FBX SDK but a quick look up shows me that the last parameter says whether the UV's are actually mapped to the vertex and also that the GetPolygonVertexUV returns a bool to say whether it succeeds. 
 
I would check both of these to ensure that there isn't something going wrong and that the data is being filled correctly.


Thank you for checking; I can verify that (1) the UVs are mapped to the vertex (lUnmappedUV equals false in my sample above) and (2) the function returns a success (true) value.

Not sure what else is happening...

#6 HappyCoder   Members   -  Reputation: 2285

Like
3Likes
Like

Posted 09 July 2014 - 04:33 PM

It is fine to have uv coordinates outside the range of 0-1. When you create the texture you are sampling, you can specify how samples outside the range are handled.

glTexParameter

gTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

What do you mean that the texture aren't mapping properly? You may need to flip your v texture coordinate. This is because opengl has the origin at the bottom left for textures and most other APIs use the top left. To switch between the two all you need to do is subract the vertical coordinate from one.

texCoord.v = 1 - texCoord.v


#7 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 10 July 2014 - 07:41 AM

It is fine to have uv coordinates outside the range of 0-1. When you create the texture you are sampling, you can specify how samples outside the range are handled.
glTexParameter

gTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

Tried that; same result. Picture will be posted below of what I get.
 

What do you mean that the texture aren't mapping properly? You may need to flip your v texture coordinate. This is because opengl has the origin at the bottom left for textures and most other APIs use the top left. To switch between the two all you need to do is subract the vertical coordinate from one.

texCoord.v = 1 - texCoord.v

I tried this as well.
 

bool lUnmappedUV;
bool SuccessFail = false;
SuccessFail = pMesh->GetPolygonVertexUV(lPolygonIndex, lVerticeIndex, lUVName, lCurrentUV, lUnmappedUV);
lUVs[lVertexCount * UV_STRIDE] = 1 - static_cast<float>(lCurrentUV[0]);
lUVs[lVertexCount * UV_STRIDE + 1] = static_cast<float>(lCurrentUV[1]);

Note my original post though; coordinates vary all over the place.

I get ranges all over the place which I suspect is why clamping doesn't work as well.

Anyway, here is an image of what I mean when I say it doesn't work:

 

Bad_Texture_Coordinates.png



#8 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 11 July 2014 - 02:42 AM

I thought it might be easier to see a simpler example of what I have going on.

Here is a very simple model's coordinates for vertices, normals, and UV's:

Vertex:  X: -1 Y: -1 Z: -1 Normal:  X: 0 Y: 0 Z: -1 UV Point:  S: 0.998895 T: 0.992545
Vertex:  X: 1 Y: 1 Z: -1 Normal:  X: 0 Y: 0 Z: -1 UV Point:  S: 0.0232662 T: 0.0169165
Vertex:  X: 1 Y: -1 Z: -1 Normal:  X: 0 Y: 0 Z: -1 UV Point:  S: 0.998895 T: 0.0169165
Vertex:  X: 1 Y: 1 Z: -1 Normal:  X: 0 Y: 0 Z: -1 UV Point:  S: 0.0232662 T: 0.0169165
Vertex:  X: -1 Y: -1 Z: -1 Normal:  X: 0 Y: 0 Z: -1 UV Point:  S: 0.998895 T: 0.992545
Vertex:  X: -1 Y: 1 Z: -1 Normal:  X: 0 Y: 0 Z: -1 UV Point:  S: 0.023266 T: 0.992545
Vertex:  X: -1 Y: 1 Z: 1 Normal:  X: 2.98023e-008 Y: 0 Z: 1 UV Point:  S: 0.333333 T: 0.666667
Vertex:  X: 0.999999 Y: -1 Z: 1 Normal:  X: 2.98023e-008 Y: 0 Z: 1 UV Point:  S: 0 T: 0.333333
Vertex:  X: 1 Y: 0.999999 Z: 1 Normal:  X: 2.98023e-008 Y: 0 Z: 1 UV Point:  S: 0.333333 T: 0.333333
Vertex:  X: 0.999999 Y: -1 Z: 1 Normal:  X: 2.98023e-008 Y: 0 Z: 1 UV Point:  S: 0 T: 0.333333
Vertex:  X: -1 Y: 1 Z: 1 Normal:  X: 2.98023e-008 Y: 0 Z: 1 UV Point:  S: 0.333333 T: 0.666667
Vertex:  X: -1 Y: -1 Z: 1 Normal:  X: 2.98023e-008 Y: 0 Z: 1 UV Point:  S: 4.96705e-008 T: 0.666667
Vertex:  X: 1 Y: 0.999999 Z: 1 Normal:  X: 1 Y: -2.98023e-007 Z: 4.47034e-008 UV Point:  S: 0.333333 T: 0.333333
Vertex:  X: 1 Y: -1 Z: -1 Normal:  X: 1 Y: -2.98023e-007 Z: 4.47034e-008 UV Point:  S: 0.666667 T: 1.98682e-008
Vertex:  X: 1 Y: 1 Z: -1 Normal:  X: 1 Y: -2.98023e-007 Z: 4.47034e-008 UV Point:  S: 0.666667 T: 0.333333
Vertex:  X: 1 Y: -1 Z: -1 Normal:  X: 1 Y: -2.98023e-007 Z: 4.47034e-008 UV Point:  S: 0.666667 T: 1.98682e-008
Vertex:  X: 1 Y: 0.999999 Z: 1 Normal:  X: 1 Y: -2.98023e-007 Z: 4.47034e-008 UV Point:  S: 0.333333 T: 0.333333
Vertex:  X: 0.999999 Y: -1 Z: 1 Normal:  X: 1 Y: -2.98023e-007 Z: 4.47034e-008 UV Point:  S: 0.333333 T: 0
Vertex:  X: 0.999999 Y: -1 Z: 1 Normal:  X: -2.83122e-007 Y: -1 Z: -1.78814e-007 UV Point:  S: 0 T: 1.29143e-007
Vertex:  X: -1 Y: -1 Z: -1 Normal:  X: -2.83122e-007 Y: -1 Z: -1.78814e-007 UV Point:  S: 0.333333 T: 0.333333
Vertex:  X: 1 Y: -1 Z: -1 Normal:  X: -2.83122e-007 Y: -1 Z: -1.78814e-007 UV Point:  S: 2.98023e-008 T: 0.333333
Vertex:  X: -1 Y: -1 Z: -1 Normal:  X: -2.83122e-007 Y: -1 Z: -1.78814e-007 UV Point:  S: 0.333333 T: 0.333333
Vertex:  X: 0.999999 Y: -1 Z: 1 Normal:  X: -2.83122e-007 Y: -1 Z: -1.78814e-007 UV Point:  S: 0 T: 1.29143e-007
Vertex:  X: -1 Y: -1 Z: 1 Normal:  X: -2.83122e-007 Y: -1 Z: -1.78814e-007 UV Point:  S: 0.333333 T: 0
Vertex:  X: -1 Y: 1 Z: 1 Normal:  X: -1 Y: 2.38419e-007 Z: -1.3411e-007 UV Point:  S: 0.666667 T: 8.9407e-008
Vertex:  X: -1 Y: -1 Z: -1 Normal:  X: -1 Y: 2.38419e-007 Z: -1.3411e-007 UV Point:  S: 1 T: 0.333333
Vertex:  X: -1 Y: -1 Z: 1 Normal:  X: -1 Y: 2.38419e-007 Z: -1.3411e-007 UV Point:  S: 0.666667 T: 0.333333
Vertex:  X: -1 Y: -1 Z: -1 Normal:  X: -1 Y: 2.38419e-007 Z: -1.3411e-007 UV Point:  S: 1 T: 0.333333
Vertex:  X: -1 Y: 1 Z: 1 Normal:  X: -1 Y: 2.38419e-007 Z: -1.3411e-007 UV Point:  S: 0.666667 T: 8.9407e-008
Vertex:  X: -1 Y: 1 Z: -1 Normal:  X: -1 Y: 2.38419e-007 Z: -1.3411e-007 UV Point:  S: 1 T: 0
Vertex:  X: -1 Y: 1 Z: 1 Normal:  X: 2.38419e-007 Y: 1 Z: 1.63913e-007 UV Point:  S: 0.333333 T: 0.666667
Vertex:  X: 1 Y: 1 Z: -1 Normal:  X: 2.38419e-007 Y: 1 Z: 1.63913e-007 UV Point:  S: 0.666667 T: 0.333333
Vertex:  X: -1 Y: 1 Z: -1 Normal:  X: 2.38419e-007 Y: 1 Z: 1.63913e-007 UV Point:  S: 0.666667 T: 0.666667
Vertex:  X: 1 Y: 1 Z: -1 Normal:  X: 2.38419e-007 Y: 1 Z: 1.63913e-007 UV Point:  S: 0.666667 T: 0.333333
Vertex:  X: -1 Y: 1 Z: 1 Normal:  X: 2.38419e-007 Y: 1 Z: 1.63913e-007 UV Point:  S: 0.333333 T: 0.666667
Vertex:  X: 1 Y: 0.999999 Z: 1 Normal:  X: 2.38419e-007 Y: 1 Z: 1.63913e-007 UV Point:  S: 0.333333 T: 0.333333
The MVP Matrix for this model:

x: 1,000.0 y: 0.0, z: 0.0, w: 0.0

x: 1,000.0 y: -0.00162920682 , z: -1000.0, w: 0.0

x: 0.0 y: 200.0, z: -3.25841356e-005, w: 0.0

x: 0.0 y: 0.0, z: 0.0, w: 1.0
The wireframe of the model:

Wire_Frame.png

The texture:

Seamless-Dark-Wood-Texturevertical-Woode

The resultant final image with texture:

Final_Image.png

#9 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 11 July 2014 - 02:43 AM

Note that the final image does the trianglulation thing and not mapping the texture based off of the UVs.

#10 Rootus   Members   -  Reputation: 210

Like
0Likes
Like

Posted 12 July 2014 - 05:35 AM

Hello!

 

Since the geometry data of your simple model seem to be correct, the texturing problem revealed by your final image may be caused by a faulty OpenGL code specifying this geometry (I think maybe somewhere in glTexCoord calls).


Edited by Rootus, 12 July 2014 - 05:36 AM.

Dietary supplements (suplementy diety)


#11 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 13 July 2014 - 02:43 PM

Hello!

 

Since the geometry data of your simple model seem to be correct, the texturing problem revealed by your final image may be caused by a faulty OpenGL code specifying this geometry (I think maybe somewhere in glTexCoord calls).

 

Thank you for your reply. I have created forum posts on AutoDESK's FBX SDK forums which is the library I use but they haven't replied back.

 

The code is based on their example so I will see if there is another example out there somewhere.



#12 L. Spiro   Crossbones+   -  Reputation: 12299

Like
1Likes
Like

Posted 13 July 2014 - 04:24 PM

How did you print those new set of vertices?
The original UV’s you printed are very obviously not correct.
The second set has the correct values but in the wrong places.

Are you correctly indexing into the pools of normals, control points, and UV’s?
Because you know that there are 4 ways to determine which UV to use and the file can use any of them, and it appears that you have valid values, but mismatched between vertices (correct value, wrong place).


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#13 tmason   Members   -  Reputation: 200

Like
0Likes
Like

Posted 15 July 2014 - 04:23 AM

How did you print those new set of vertices?
The original UV’s you printed are very obviously not correct.
The second set has the correct values but in the wrong places.

Are you correctly indexing into the pools of normals, control points, and UV’s?
Because you know that there are 4 ways to determine which UV to use and the file can use any of them, and it appears that you have valid values, but mismatched between vertices (correct value, wrong place).


L. Spiro

 

You hinted at the correct problem to solve.

 

I fixed this by actually setting the correct offset for my normals and UV's in the call to glVertexAttribPointer() and now everything works.

 

Thanks to everyone for all of your help.



#14 Hodgman   Moderators   -  Reputation: 27838

Like
0Likes
Like

Posted 15 July 2014 - 08:02 PM

What does the model look like in the art tool that was used to create it?





PARTNERS