Sign in to follow this  

Texture space question

Recommended Posts

I've been coding with openGL for a while, and I'd often take long breaks and forget lots of stuff, then relearn it; I'm crazy. Maybe I'm forgetting something really important about texture space. How does the linear memory space of the data buffers passed to glTexImage2D map to texture coordinate uv space? If I wrote my linear memory buffer to the screen, the top left coordinate in screen space is (0,0) and in memory, that's offset 0, right? The bottom left of the screen would be (0, h - 1) and in memory, that's offset (h - 1) * h, right? I create a memory buffer, char* buffer = new char[256 * 256 * 4] let's say, to hold RGBA values for a 256x256 texture. If I want to then fill the first 10 rows of pixels with grey (127, 127, 127), I'd do memset(buffer, 0x7F, 256 * 10 * 4), right? 256 pixels per row, 10 rows, 4 bytes per pixel. Now to make it a texture and draw it:
unsigned int id;
glGenTextures(GL_TEXTURE_2D, &id);
glBindTexture(GL_TEXTURE_2D, id);

glTexImage2D(GL_TEXTURE_2D,    // normal target
             0,                // 0th mipmap level
             GL_RGBA,          // internal format
             256,              // width
             256,              // height
             0,                // no border
             GL_RGBA,          // buffer's format
             GL_UNSIGNED_BYTE, // buffer's data type
// then normal linear filtering

// now down in drawing code
    // bottom left
    glTexCoord2f(0.0f, 0.0f);  glVertex3f(-1.0f, -1.0f, 0.0f);
    // bottom right
    glTexCoord2f(1.0f, 0.0f);  glVertex3f(1.0f, -1.0f, 0.0f);
    // top right
    glTexCoord2f(1.0f, 1.0f);  glVertex3f(1.0f, 1.0f, 0.0f);
    // top left
    glTexCoord2f(0.0f, 1.0f);  glVertex3f(-1.0f, 1.0f, 0.0f);
So then I expect to see a square on the screen with the top 10 rows of pixel being grey, but they're at the bottom instead. I'm endlessly confused, why is that? They should be up top! My idea of linear memory/screen space is X-axis going positive to the right, Y-axis going positive downwards. My idea of texture space is X-axis (or u or s) going positive to the right, and Y-axis (or v or t) going positive upwards. Where did I go wrong? Pardon the long post.

Share this post

Link to post
Share on other sites
In windows (GDI, direct x, etc), images are stored where 0,0 is the top left, in openGL (and unix I think?), images are stored where 0,0 is the bottom left... So basically the only difference is that in openGL you have to copy your rows in reverse order. Personally I prefer top-left, just seems to make more sense.

it gets more confusing in that in Direct3D, the z-axis goes into the screen, where in openGL, it comes out of the screen.... Again, it's preference what 'feels' right...

It's all wonderful how the world lacks standards.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this