PDA

View Full Version : Cube map Texture Coordinates



xerzi
07-28-2011, 12:35 AM
I'm trying to render each side of a cube map onto a flat texture but I don't know how cube map uv mapping works.



//bind cube map texture

glBegin(GL_QUADS);
glTexCoord3f(1, 0, 0); glVertex2f(0, 0);
glTexCoord3f(1, 0, 0); glVertex2f(0, 1);
glTexCoord3f(1, 0, 0); glVertex2f(1, 1);
glTexCoord3f(1, 0, 0); glVertex2f(1, 0);
glEnd();


I read somewhere that the coordinate is basically a normal, so that should be displaying one of the sides of the cubemap, except it isn't. Any help would be appreciated.

tksuoran
07-28-2011, 02:21 AM
If you want to visualize cube map as a 2D cross, then the following may be useful. The code doesn't use immediate mode but you should be able to see texture coordinates and vertex positions:


float z = 10.0f;
float a = 128.0f;
Vector2 O = new Vector2(4.0f, 30.0f);
Vector2 A;
Vector2 B;
Vector2 C;
Vector2 D;

// B C
// posy
// A D
// B CB CB CB C
// negx posz posx negz
// A DA DA DA D
// B C
// negy
// A D

// negx rz -ry rx
A = O + new Vector2(0.0f, a);
B = A + new Vector2(0.0f, a);
C = A + new Vector2(a, a);
D = A + new Vector2(a, 0);
indexBufferRange.Quad(
vertexBufferRange.CurrentIndex, vertexBufferRange.CurrentIndex + 1,
vertexBufferRange.CurrentIndex + 2, vertexBufferRange.CurrentIndex + 3
);
indexBufferRange.CurrentIndex += 6;
vertexBufferRange.Set(texcoord, -1.0f, -1.0f, -1.0f); vertexBufferRange.Set(position, A.X, A.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, 1.0f, -1.0f); vertexBufferRange.Set(position, B.X, B.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, 1.0f, 1.0f); vertexBufferRange.Set(position, C.X, C.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, -1.0f, 1.0f); vertexBufferRange.Set(position, D.X, D.Y, z); ++vertexBufferRange.CurrentIndex;

// posz rx -ry rz
A = O + new Vector2(a, a);
B = A + new Vector2(0.0f, a);
C = A + new Vector2(a, a);
D = A + new Vector2(a, 0);
indexBufferRange.Quad(
vertexBufferRange.CurrentIndex, vertexBufferRange.CurrentIndex + 1,
vertexBufferRange.CurrentIndex + 2, vertexBufferRange.CurrentIndex + 3
);
indexBufferRange.CurrentIndex += 6;
vertexBufferRange.Set(texcoord, -1.0f, -1.0f, 1.0f); vertexBufferRange.Set(position, A.X, A.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, 1.0f, 1.0f); vertexBufferRange.Set(position, B.X, B.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, 1.0f, 1.0f); vertexBufferRange.Set(position, C.X, C.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, -1.0f, 1.0f); vertexBufferRange.Set(position, D.X, D.Y, z); ++vertexBufferRange.CurrentIndex;

// posy rx rz ry
A = O + new Vector2(a, 2.0f * a);
B = A + new Vector2(0.0f, a);
C = A + new Vector2(a, a);
D = A + new Vector2(a, 0);
indexBufferRange.Quad(
vertexBufferRange.CurrentIndex, vertexBufferRange.CurrentIndex + 1,
vertexBufferRange.CurrentIndex + 2, vertexBufferRange.CurrentIndex + 3
);
indexBufferRange.CurrentIndex += 6;
vertexBufferRange.Set(texcoord, -1.0f, 1.0f, 1.0f); vertexBufferRange.Set(position, A.X, A.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, 1.0f, -1.0f); vertexBufferRange.Set(position, B.X, B.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, 1.0f, -1.0f); vertexBufferRange.Set(position, C.X, C.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, 1.0f, 1.0f); vertexBufferRange.Set(position, D.X, D.Y, z); ++vertexBufferRange.CurrentIndex;

// negy rx -rz ry
A = O + new Vector2(a, 0.0f);
B = A + new Vector2(0.0f, a);
C = A + new Vector2(a, a);
D = A + new Vector2(a, 0);
indexBufferRange.Quad(
vertexBufferRange.CurrentIndex, vertexBufferRange.CurrentIndex + 1,
vertexBufferRange.CurrentIndex + 2, vertexBufferRange.CurrentIndex + 3
);
indexBufferRange.CurrentIndex += 6;
vertexBufferRange.Set(texcoord, -1.0f, -1.0f, -1.0f); vertexBufferRange.Set(position, A.X, A.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, -1.0f, 1.0f); vertexBufferRange.Set(position, B.X, B.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, -1.0f, 1.0f); vertexBufferRange.Set(position, C.X, C.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, -1.0f, -1.0f); vertexBufferRange.Set(position, D.X, D.Y, z); ++vertexBufferRange.CurrentIndex;

// posx -rz -ry rx
A = O + new Vector2(2.0f * a, a);
B = A + new Vector2(0.0f, a);
C = A + new Vector2(a, a);
D = A + new Vector2(a, 0);
indexBufferRange.Quad(
vertexBufferRange.CurrentIndex, vertexBufferRange.CurrentIndex + 1,
vertexBufferRange.CurrentIndex + 2, vertexBufferRange.CurrentIndex + 3
);
indexBufferRange.CurrentIndex += 6;
vertexBufferRange.Set(texcoord, 1.0f, -1.0f, 1.0f); vertexBufferRange.Set(position, A.X, A.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, 1.0f, 1.0f); vertexBufferRange.Set(position, B.X, B.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, 1.0f, -1.0f); vertexBufferRange.Set(position, C.X, C.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, -1.0f, -1.0f); vertexBufferRange.Set(position, D.X, D.Y, z); ++vertexBufferRange.CurrentIndex;

// negz -rx -ry rz
A = O + new Vector2(3.0f * a, a);
B = A + new Vector2(0.0f, a);
C = A + new Vector2(a, a);
D = A + new Vector2(a, 0);
indexBufferRange.Quad(
vertexBufferRange.CurrentIndex, vertexBufferRange.CurrentIndex + 1,
vertexBufferRange.CurrentIndex + 2, vertexBufferRange.CurrentIndex + 3
);
indexBufferRange.CurrentIndex += 6;
vertexBufferRange.Set(texcoord, 1.0f, -1.0f, -1.0f); vertexBufferRange.Set(position, A.X, A.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, 1.0f, 1.0f, -1.0f); vertexBufferRange.Set(position, B.X, B.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, 1.0f, -1.0f); vertexBufferRange.Set(position, C.X, C.Y, z); ++vertexBufferRange.CurrentIndex;
vertexBufferRange.Set(texcoord, -1.0f, -1.0f, -1.0f); vertexBufferRange.Set(position, D.X, D.Y, z); ++vertexBufferRange.CurrentIndex;