UV coords flipped opengl?

213 Views Asked by At

I'm currently writing a simple 2D renderer using OpenGl and C and have been trying to implement a system that allows you to only render part of a texture for things such as animations with sprite sheets. However, I am having a bit of trouble with my uv coordinates for some reason.

I believe the reason for this is that stb_image, the library I'm using to load the image, sets 0,0 as the top left of the image while opengl has uv coords starting at the bottom left.

However, when I have the following uv coords the image is not flipped over or anything -

float texCoords[] = 
    {
        0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
        0.0, 1.0
    };

Here are my vertex positions as well:

float vertices[] = 
    {
        0.0, 0.0, 0.0,
        1.0, 0.0, 0.0,
        1.0, 1.0, 0.0,
        0.0, 1.0, 0.0
    };

Since 0,0 is the top left corner and it is being mapped to the 0,0 of my rectangle, I would think that the top left corner would be in the bottom left of my rectangle. However, this isn't the case and the image is rendered the correct way up.

However, when I flip the image with stbi_set_flip_vertically_on_load() then the image is rendered upside down.

To check my sanity, I tried to render a subsection of the texture but the y coords were flipped just as I expected with 0,0 being the top left corner. For example, I used these coords to try and render the bottom left corner but it rendererd the top left instead:

 float texCoords[] = 
    {
        0.0, 0.0,
        0.25, 0.0,
        0.25, 0.25,
        0.0, 0.25
    };

This makes no sense to me, especially because, as I have already said, if 0,0 is top left and is being mapped to the bottom right of my rectangle, then the image should be rendered upside down but it isn't being.

Does anybody know why this happens and does anybody know how to make the bottom left corner of the image be 0,0 using stb_image? Thanks.

Edit: I just wrote a little test program and got that to work as expected so it must be something I'm doing in my renderer that's making it go likes this. I might just rewrite the whole texture part of it. If you guys have any tips that would be welcome.

0

There are 0 best solutions below