Jump to content
  • Advertisement
Sign in to follow this  
k.pedersen

OpenGL [SOLVED]Perspective Correct Texture Coordinates (Frag Shader)

This topic is 2340 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 been attempting to replace all fixed functionality from an OpenGL project I wrote a while ago and have run into a snag which I havn't been able to resolve in the many hours I have spent on it.

After much research I have found it is due to the texture coordinates not being perspective correct. What I have not been able to find is a way to fix this in the shader.

(I have attached a screenshot demonstrating the issue and as you can see, the brick texture is not drawing correctly, creating a seam.)

The line of code in my frag shader which I believe to be causing the issue is as follows.

#version 120
uniform sampler2D tex;
varying vec2 pass_tex_coord;
...
gl_FragColor = texture2D(tex, pass_tex_coord);


So far I am under the impression that I need to obtain the w value from the pass_tex_coord s and t values.
I am unsure of how to do this because I am simply passing in the uv texture coordinates from my model (VBO). I have also noticed that the model of the cat has the correct texture coord mapping (because the triangles are smaller?)

If anyone could help shed some light on this or offer some example shader code to do this, I would be very greatful!

Best Regards,

Karsten

Share this post


Link to post
Share on other sites
Advertisement
Hello NumberXaero,

Yes, I can confirm that the brick texture draws correctly when using the fixed function shader.

Also, my same code is loading the brick structure as the cat, so I know the texture coordinates are not the issue here.

Any other suggestions to this?

Share this post


Link to post
Share on other sites
Here is a simplified version of my vertex shader.


#version 120

uniform mat4 in_projection;
uniform mat4 in_modelview;
attribute vec2 in_tex_coord;
attribute vec3 in_position;
varying vec2 pass_tex_coord;

void main()
{
gl_Position = in_projection * in_modelview * vec4(in_position, 1);
pass_tex_coord = in_tex_coord;
}



All I am really doing is passing through the texture coordinates.

Share this post


Link to post
Share on other sites
What does it look like if you display the texture coord as a color

gl_FragColor = vec4(pass_tex_coord.x, pass_tex_coord.y, 0.0, 1.0);

Share this post


Link to post
Share on other sites
I have attached the result.

(Edit: Submitted new screenshot)

Tbh, I wasn't expecting the cat model or the brick platform model to be completely red.

However, I also have exactly the same problem with the skybox and as you can see in the screenshot, that looks about what I would expect.

The cat model gets it's texture from a single unwrapped texture sheet, the brick platform model gets its coordinates from gtkradiant.

All 3 models (cat, bricks, skybox) seems to work fine when using no shader at all (fixed function) so I really don't think there is anything wrong with my texture coordinates. However I am really not sure why 2 of them are showing up as red.

The cat's texture coordinates are usually between -1 and 1
The brick platform's texture coordinates can usually be between -10 and 10.

Could this have anything to do with it? Again, both these models also work with Blender too.

When I increase the size of the cat (like glScale) it also begins to get the strange coordinate skew (i.e when the faces become large enough to notice).

Share this post


Link to post
Share on other sites
I'm very new to shaders, but when I was researching how to write a texture shader, this is what I found:

Pulled from Lighthouse3D.com...

vertex shader:
[source]
void main() {

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
[/source]
fragment shader:
[source]
uniform sampler2D tex;

void main()
{
vec4 color = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = color;
}
[/source]
Again, I am new to this, so you are probably doing this in a manor that I am unfamiliar with and most likely correct, but I have never seen a texture shader written like yours.

Share this post


Link to post
Share on other sites
Hello MarkS,

Thanks for the info and this probably does work but is unfortunately no longer the correct way to do this.

This uses the fixed function pipeline in OpenGL such as the ftransform() and is also using the built in and deprecated matrix system provided by OpenGL.

The way the texture coordinates are passed in also uses the gl_TexCoord[0] keyword which is also deprecated but also will not work on the OpenGL ES 2.0 or 4.0 Core profiles.

Though AFAIK those texture coordinates are probably still not perspective correct either. I cannot test this because I am using the GLM matrix code rather than the OpenGL provided one and thus ftransform will not work.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!