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

Started by
12 comments, last by Hodgman 9 years, 9 months ago
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
Advertisement

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.

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.

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.

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...

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
My current game project Platform RPG

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

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
Note that the final image does the trianglulation thing and not mapping the texture based off of the UVs.

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).

Dietary supplements (suplementy diety)

This topic is closed to new replies.

Advertisement