Jump to content
  • Advertisement
Sign in to follow this  
f12dude

OpenGL Texture Coordinates Problem

This topic is 2011 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

I'm converting my DirectX text renderer to OpenGL, and I've run into an issue with the texture coordinates. My problem is that the text that gets rendered in OpenGL is flipped horizontally. I was wondering how this came to be as after reading about how OpenGL texture corrdinates work, I would have thought that I would have needed to invert the V component of the texture coordinates, but this seems to be not the case.

Share this post


Link to post
Share on other sites
Advertisement

All you have to do to is take the V component and subtract it from 1.0.  This will properly invert your texture coordinates

 

float s = u;

float t = 1.0f - v;

 

Have you tried this before?  If this doesn't work, you might want to share a bit of code with us.

 

Shogun.

Share this post


Link to post
Share on other sites

At a superficial glance DirectX and OpenGL appear to have different texture space origins but this isn't necessarily the case, the pointer you pass to function glTexImage2D points to the lower-left texel of the texture so to flip the origin you need only upload a flipped version of your texture data (probably the best for GL/D3D uniformity) or transform the origin in your shader.

 

However, this does not address the fact that your textures are being flipped horizontally. What is flipped? The individual characters or the texture itself?

Share this post


Link to post
Share on other sites

This is what the flipping looks like when rendering "1234567890"

 

The code is essentially the same as the DirectX version which treats the origin in the top left.

 

wu1gsm.png

Share this post


Link to post
Share on other sites
Is each glyph a seperate quad? Are you sure you're being consistent with your coordinate system handidness between the two implementations?

Share this post


Link to post
Share on other sites

I figured a solution to the problem by changing my view matrix, but I'm not sure if this is the correct way to go about this:

 

glm::lookAt(glm::vec3(0.0f,0.0f,-10.0f),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));

 

 

Changed to:

 

glm::lookAt(glm::vec3(0.0f,0.0f,10.0f),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));
Edited by f12dude

Share this post


Link to post
Share on other sites

You need to be consistent with your conventions between APIs. By inverting the direction of your view matrix you are effectively changing the handedness of your coordinate system, hence the reason why your text was being "flipped" back round the right way. It appears that you have culling disabled which is why you could still see the text in your original "flipped" image (albeit incorrectly rendered), otherwise you would be staring at an empty frame buffer (the glyph quads would have been culled as technically they were facing away from the camera). There's other subtle issue to consider so get things consistent otherwise you'll trip yourself up again in the not-so-distant future.

Share this post


Link to post
Share on other sites

I've enabled back face culling and only half of the triangles get rendered.  I've also reverted my view matrix back to what I originally had it set to.  I'm still new to this but in DirectX, I would specify CullMode= CCW; in the shader to do backface culling which worked correctly. I still have no idea how to get the text rendered correctly without flipping the view matrix. Inverting the U component of the texture coordinates does this(without culling):

 

int x = character % 16; // Column
int y = (character / 16); // Row

glm::vec2 topLeft(1.0f - (x / 16.0f),y / 16.0f);
glm::vec2 bottomRight(1.0f - ((x+1) / 16.0f),(y+1) / 16.0f);

 

 

 

23r1lb7.jpg

Edited by f12dude

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!