   Confused About Cube Mapping
  I've been trying to teach myself cube mapping and have written a small app which applies a cube map to a sphere. However, I'm really confused by a couple of things that are happening and I'm hoping someone can help me.Firstly, it doesn't seem like the cube map is actually "applied" to an object because when the object is rotated, the cube map stays still.Secondly, I'm not sure whether it's just my code, but the cube map works fine for spheres but when a cube is rendered it doesn't work properly. Each side of the cube seems to be coloured entirely from the pixels in the image rather than displaying the texture.What is the main purpose of a cube map? Maybe I'm just confused about their use.My code is listed below with the cube section commented out. I got my tga.h from  http://gpwiki.org/index.php/OpenGL_Tutorial_Framework:Texture_Mapping because I'm having trouble getting DevIL working in OS X at the minute.// A simple application to demonstrate the usage of a cube map.#include <iostream>#include "SDL.h"#include "SDL_OpenGL.h"#include "tga.h"float rotXSpeed = 0.0f;float rotYSpeed = 0.0f;float rotX = 0.0f;float rotY = 0.0f;GLuint texID;TGAImg image;bool SetupEnvironment() {if (SDL_Init(SDL_INIT_VIDEO) < 0)return false;  if (SDL_SetVideoMode(800, 600, 16, SDL_OPENGL | SDL_DOUBLEBUF | SDL_HWSURFACE) == 0)return false;SDL_WM_SetCaption("Cube Map", NULL);glViewport(0, 0, 800, 600);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glClearColor(0.0f, 0.0f, 0.0f, 1.0f);glShadeModel(GL_SMOOTH);glClearDepth(1.0f);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LEQUAL);glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);glEnable(GL_CULL_FACE);return true;}int main(int argc, char **argv) {if (!SetupEnvironment()) {std::cout << "Unable to setup the environment: " << SDL_GetError() << "\n";SDL_Quit();}if (image.Load("image.tga") == IMG_OK)std::cout << "Image loaded!\n";elsestd::cout << "Image not loaded!\n";    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, image.GetWidth(),  image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImg());    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, image.GetWidth(),  image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImg());    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImg());    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImg());    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImg());    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImg());    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);    glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP); glEnable(GL_TEXTURE_GEN_S);    glEnable(GL_TEXTURE_GEN_T);    glEnable(GL_TEXTURE_GEN_R);    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);    glEnable(GL_TEXTURE_CUBE_MAP);    glEnable(GL_LIGHTING);    glEnable(GL_LIGHT0);    glEnable(GL_AUTO_NORMAL);    glEnable(GL_NORMALIZE);GLfloat diffuse[4] = {1.0, 1.0, 1.0, 1.0};glMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);bool running = true;SDL_Event event;//Enter main loop.while (running) {if (SDL_PollEvent(&event)) {switch (event.type) {case SDL_QUIT:running = false;break;case SDL_KEYDOWN:if (event.key.keysym.sym == SDLK_ESCAPE)running = false;else if (event.key.keysym.sym == SDLK_UP)rotXSpeed -= 0.1f;else if (event.key.keysym.sym == SDLK_DOWN)rotXSpeed += 0.1f;else if (event.key.keysym.sym == SDLK_RIGHT)rotYSpeed += 0.1f;else if (event.key.keysym.sym == SDLK_LEFT)rotYSpeed -= 0.1f;break;default:break;}}glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glRotatef(rotX, 1.0f, 0.0f, 0.0f);glRotatef(rotY, 0.0f, 1.0f, 0.0f);/*glBegin(GL_QUADS);glVertex3f(-1.0f, -1.0f,  1.0f);glVertex3f( 1.0f, -1.0f,  1.0f);glVertex3f( 1.0f,  1.0f,  1.0f);glVertex3f(-1.0f,  1.0f,  1.0f);glVertex3f(-1.0f, -1.0f, -1.0f);glVertex3f(-1.0f,  1.0f, -1.0f);glVertex3f( 1.0f,  1.0f, -1.0f);glVertex3f( 1.0f, -1.0f, -1.0f);glVertex3f(-1.0f,  1.0f, -1.0f);glVertex3f(-1.0f,  1.0f,  1.0f);glVertex3f( 1.0f,  1.0f,  1.0f);glVertex3f( 1.0f,  1.0f, -1.0f);glVertex3f(-1.0f, -1.0f, -1.0f);glVertex3f( 1.0f, -1.0f, -1.0f);glVertex3f( 1.0f, -1.0f,  1.0f);glVertex3f(-1.0f, -1.0f,  1.0f);glVertex3f( 1.0f, -1.0f, -1.0f);glVertex3f( 1.0f,  1.0f, -1.0f);glVertex3f( 1.0f,  1.0f,  1.0f);glVertex3f( 1.0f, -1.0f,  1.0f);glVertex3f(-1.0f, -1.0f, -1.0f);glVertex3f(-1.0f, -1.0f,  1.0f);glVertex3f(-1.0f,  1.0f,  1.0f);glVertex3f(-1.0f,  1.0f, -1.0f);glEnd();*/GLUquadricObj *sphereObj = gluNewQuadric();gluSphere(sphereObj, 1.0, 50, 50);rotX += rotXSpeed;rotY += rotYSpeed;SDL_GL_SwapBuffers();}return 0;}
  Cube maps are often used for environment mapping. I think what you where thinking about was texture projection, where a 3D image texture is "applied" on an object.A cube map is usually some sort of environment, and most often is a cross, or 6 seperate images. (top, bottom, left, right, front, back.) The reflection of the cube is computed per texel, and then rendered. This allows you to render things such asreflections of the surrounding environment appear on your objects. A more advanced technique has each object render the top/bootom etc... images in real time. That's how car racing games have cars and environments properly reflecting on other cars.As for the code, I have never done cube mapping in fixed function OpenGL (just in the shader) so I can't really help you code wise.   ;I'd say your main problem is using the right texgen.Quote:Firstly, it doesn't seem like the cube map is actually "applied" to an object because when the object is rotated, the cube map stays still.Nope that sounds right, depending upon the texgen and whether you account for viewpoint.Quote: Secondly, I'm not sure whether it's just my code, but the cube map works fine for spheres but when a cube is rendered it doesn't work properly. Each side of the cube seems to be coloured entirely from the pixels in the image rather than displaying the texture.Again that sounds like you've not set up the 'correct' texgenHere are some snippets from some code that works for me (although its been a while since i've looked at this)// Allows for correct mapping onto a cube and any other object// Textures remain attached to faces during rotatationfloat X[4] = { 1,0,0,0 };float Y[4] = { 0,0,1,0 };float Z[4] = { 0,1,0,0 };glTexGeni(GL_S, GL_TEXTURE_GEN_MODE,  GL_OBJECT_LINEAR);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,  GL_OBJECT_LINEAR);glTexGeni(GL_R, GL_TEXTURE_GEN_MODE,  GL_OBJECT_LINEAR);glTexGenfv( GL_S, GL_OBJECT_PLANE, X );glTexGenfv( GL_T, GL_OBJECT_PLANE, Y );glTexGenfv( GL_R, GL_OBJECT_PLANE, Z );// Standard reflectionfloat mt[16];glGetFloatv( GL_MODELVIEW_MATRIX, mt );m_InverseViewMtx[0] =mt[0]; m_InverseViewMtx[1] =mt[8];  m_InverseViewMtx[2] =mt[4]; m_InverseViewMtx[3] =0;m_InverseViewMtx[4] =mt[1]; m_InverseViewMtx[5] =mt[9];  m_InverseViewMtx[6] =mt[5]; m_InverseViewMtx[7] =0;m_InverseViewMtx[8] =mt[2]; m_InverseViewMtx[9] =mt[10]; m_InverseViewMtx[10]=mt[6]; m_InverseViewMtx[11]=0;m_InverseViewMtx[12]=    0; m_InverseViewMtx[13]=0; m_InverseViewMtx[14]=    0; m_InverseViewMtx[15]=1;// Load InverseViewMtx into texture matrixglMatrixMode(GL_TEXTURE);glLoadMatrixf(m_InverseViewMtx);glMatrixMode(GL_MODELVIEW);// Set Tex Gen modesglTexGeni(GL_S, GL_TEXTURE_GEN_MODE,  GL_REFLECTION_MAP);glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,  GL_REFLECTION_MAP);glTexGeni(GL_R, GL_TEXTURE_GEN_MODE,  GL_REFLECTION_MAP);;Quote:What is the main purpose of a cube map? Maybe I'm just confused about their use.u know how 2d textures choose with texel to sample right,well with cubemaps its similar but instead of texcoord2f(x,y) u use texcoord3f(x,y,z)the x,y,z is a direction a vectorso imagine your'e inside the cube and u get the vector (0,1,0) so u look straight up + return the color above u ( center +Y side of cubemap ) (0.1,1,0) will return the texel slightly to the right of it etc   