CubeMap of a perfectly reflective sphere

Hi all, I’m new to opengl and have been able to learn a lot on my own, but i am stuck on this one.

I have a room rendered, but I need a sphere that hovers in the center reflecting everything else perfectly. So far, I’ve been able to cube map to it with pretty good results, but the 6 textures are all the same: namely, the view I have of the room. I am calling gluLookAt to reposition the camera to where the sphere is going to be, and look at its surroundings, then I make a call to glCopyTexImage2D to get that loaded into the appropriate texture, but I can’t get 6 different perspectives loaded… just the one, six times.

here’s the relevant code if you can look through it and help me out, i will love you forever :slight_smile:

void drawReflectiveSphere(){
glEnable(GL_TEXTURE_CUBE_MAP);

float pos = {10.0, 1.0, -40.0};
float x_pos = {pos[0] + 1, pos[1], pos[2]};
float x_neg = {pos[0] - 1, pos[1], pos[2]};
float y_pos = {pos[0], pos[1] + 1, pos[2]};
float y_neg = {pos[0], pos[1] - 1, pos[2]};
float z_pos = {pos[0], pos[1], pos[2] + 1};
float z_neg = {pos[0], pos[1], pos[2] - 1};
float view_pos[3] = {x_pos[0], x_neg[0], y_pos[0], y_neg[0], z_pos[0], z_neg[0]};

glGenTextures(1, &cubemap);

glReadBuffer(GL_BACK);

//Use different frustum
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glFrustum(-5.0, 5.0, -5.0, 5.0, 0.0, width + 10.0);

glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, 128, 128);

//Generate cube map textures
for(int i = 0; i < 6; i++){
glPushMatrix();
gluLookAt(pos[0], pos[1], pos[2],
view_pos[i][0], view_pos[i][1], view_pos[i][2],
0.0, -1.0, 0.0);
glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 0, 0, 128, 128, 0);
glPopMatrix();
}

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexGeni(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);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);

//Restore viewport
glViewport(0, 0, W, H);

//Draw cube-mapped sphere
glPushMatrix();
glTranslatef(pos[0], pos[1], pos[2]);
gluSphere(quadratic, 5.0f, 128, 128);
glPopMatrix();

//Restore projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_CUBE_MAP);
}

I don’t think your declarations and initializations are working the way you think they are. You have:

...

float  pos[] = {10.0, 1.0, -40.0};
float  x_pos [] = {pos[0] + 1, pos[1], pos[2]};
float  x_neg [] = {pos[0] - 1, pos[1], pos[2]};
float  y_pos [] = {pos[0], pos[1] + 1, pos[2]};
float  y_neg [] = {pos[0], pos[1] - 1, pos[2]};
float  z_pos [] = {pos[0], pos[1], pos[2] + 1};
float  z_neg [] = {pos[0], pos[1], pos[2] - 1};
float  view_pos[][3] = {x_pos[0], x_neg[0], y_pos[0],
                        y_neg[0], z_pos[0], z_neg[0]};

...

//Generate cube map textures
for (int i = 0;   i < 6;   i++) {
  ...
  gluLookAt (pos[0], pos[1], pos[2],
             view_pos[i][0], view_pos[i][1], view_pos[i][2],
             0.0, -1.0, 0.0);
  ...
}

...

view_pos[][] consists of a total of six elements, not 18 as you seem to think (or your code references). In other words, it is declared (and initialized) as:
float view_pos[2][3];
So after the second iteration of your loop, when i > 1, view_pos[i][] is undefined.

ah you’re right. those array positions aren’t holding sensible values… but if i just include the array names (ie, view_pos[][3] = {x_pos, x_neg, etc} ) then it tells me “cannot convert ‘float*’ to float”

ok, fixed it with:

float view_pos[][3] = {{pos[0] + 1, pos[1], pos[2]},
{pos[0] - 1, pos[1], pos[2]},
{pos[0], pos[1] + 1, pos[2]},
{pos[0], pos[1] - 1, pos[2]},
{pos[0], pos[1], pos[2] + 1},
{pos[0], pos[1], pos[2] - 1}};