Hi,
I’m new to OpenGL (experienced D3D), and have a simple quick question I hope to clear up.
OpenGL ST co-ordinates use the Cartesian co-ordinate system, with (0,0) at the bottom left and (1,1) at the top right of texture space.
The trouble is whenever I see a texturing example in a book, it’s always got some image looking correct (upright) with a ST axis in the bottom left, with S going left->right and T going bottom->top. However, the MEMORY representation is the opposite. My understanding is the first texel is at memory address 0, and the last texel is at the end of memory.
This means if you were to use the typical example of (i.e. 1 with v0=bl, v1=br, v2=tr):
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f( 0.5f, -0.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f( 0.5f, 0.5f);
glEnd();
it would mean the texture is flipped in Y. Just to be sure, I built a texture in memory like such (just a texture which starts at black at T=0 and increases in blue to T=1:
{
GLushort *imageData;
imageData =(GLushort *)malloc(kTestWidth * kTestHeight * 2);
for (int y=0; y<kTestHeight; ++y) {
for (int x=0; x<kTestWidth; ++x) {
imageData[y*kTestWidth+x] = MAKE_5551_F(0,0,(float)y/kTestHeight,1);
}
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTestWidth, kTestHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, imageData);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
free(imageData);
}
This produces:
Which is what I would expect. I either have to flip the texture in memory (upon create if code created, or on load if loaded from a file), or flip the T co-ordinates.
So why do I see so many examples with 0,0 used in the texture co-ordinates at the bottom left, yet still render right way up? Do people tend to flip the texture in memory or the T co-ordinates?
Why is this not mentioned with respect to physical memory in books? Are my assumptions correct?
Cheers.