Texture Coordinates Problem

Started by
7 comments, last by f12dude 11 years ago

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.

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.

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?

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

Then try inverting the U coordinate. Have you tried that?

Shogun.

Is each glyph a seperate quad? Are you sure you're being consistent with your coordinate system handidness between the two implementations?

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

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.

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

This topic is closed to new replies.

Advertisement