views:

58

answers:

2

alt textAfter struggling all weekend, I finally have a sphere reflecting its environment in OpenGL. It almost looks good. Problem is, certain features don't line up. I can't find much information on the topic of OpenGL sphere mapping, outside of a two page section in the Red Book and a few scattered, and mostly unfinished forum topics. Not sure it's necessary, but I included my code where I load the textures. After trial and error, I found that having symmetrical dimensions freom 0 to 512 gets the best results, but they still aren't perfect (and the dimensions have to be powers of two, or it crashes). Does any one know of any strategies to get textures that line up more correctly?

void loadCubemapTextures(){

  glGenTextures(1, &cubemap);
  glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  glDrawBuffer(GL_AUX1);
  glReadBuffer(GL_AUX1);

  glMatrixMode(GL_MODELVIEW);

  //Generate cube map textures
  for(int i = 0; i < 6; i++){

      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();
      //Viewing Transformation
      gluLookAt(pos[0], pos[1], pos[2],
           view_pos[i][0], view_pos[i][1], view_pos[i][2],
         top[i][0], top[i][1], top[i][2]);

      glMatrixMode(GL_PROJECTION);
      glLoadIdentity(); 
      gluPerspective(90.0, 1.0, cubemapRadius + 1, 200.0);
      glMatrixMode(GL_MODELVIEW);

      render();

      glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 0, 0, 256, 256, 0);
  }
+1  A: 

Your walls lack enough features to make their orientation and location clear, but I would bet those are the issue.

The values you have in view_pos and top are what define those. Among the things that look suspicious:

  • the black at the bottom of the shere comes from the face that captured the front view. It should be at the top (ie it's either flipped, or rotated 180).
  • likewise, the black that comes from the back of the view (in the center of the sphere) looks turned by a quarter of a circle (as black goes on the left rather than being on the top).
  • the reflection of the map sphere looks weird. It lacks resolution to figure out exactly what this is.

Anyways, to fix it, you can either make sure your values are what is expected from a proper cube-map definition (as in check all the specification), or just do a bunch of trial and error, after adding enough geometry to figure out what the orientation of each face is.

Bahbar
yeah, I was able to fix the negative z axis by simply negating the value I had for its top. that looks better, but the others seem to get worse when I mess with them... is there a resource you could point me to that specifies the expected orientations? my book has nothing on that.
JakeVA
@JakeVA: the table of the specification describing the faces and how texture coordinates map to the faces:POSITIVE_X [-rz -ry] NEGATIVE_X [+rz -ry] POSITIVE_Y [+rx +rz] NEGATIVE_Y [+rx -rz] POSITIVE_Z [+rx -ry] NEGATIVE_Z [-rx -ry]. CopyTexture maps the bottom left of your source to the 0,0 texture coordinate. That's all that matters for your problem.
Bahbar
A: 

The problem had nothing to do with coordinates, thanks for the input though. I had forgotten to set the view matrix... a simple call to gluViewport inside the for-loop fixed it for me.

JakeVA