bcmpinc
bcmpinc

Reputation: 3370

What is the orientation of OpenGL texture data?

Consider the following piece of code, which creates a 32bits 16x16 texture, which does not repeat:

int pixels[256];
glTexImage2D(GL_TEXTURE_2D, 0, 4, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

When rendering a triangles using this texture, texture coordinates have to be specified. However, which coordinate maps to which color?

For example, to which of pixels[0], pixels[15], pixels[240] or pixels[255] does glTexCoord2f(0,0) map? And what about glTexCoord2f(1,0)?

Edit: The texture matrix is (indeed) assumed to be the identity matrix.

Upvotes: 0

Views: 1039

Answers (1)

Christian Rau
Christian Rau

Reputation: 45948

By default (with an identity texture matrix) the texture coordinates start from the lower left corner and extend to the upper right corner of the texture space, thus they are layed out like this:

(0,1) ... (1,1)
  :         :
(0,0) ... (1,0)

And the texture data itself is layed out compoment after component, pixel after pixel, row after row, from the bottom left to the top right. Thus in your case like this:

[240] -> [255]
  :        :
 [0]  -> [15]

So as a conclusion you get this mapping (ignoring any filtering and clamping issues):

(0,0) -> pixels[0]
(1,0) -> pixels[15]
(0,1) -> pixels[240]
(1,1) -> pixels[255]

Upvotes: 2

Related Questions