PDA

View Full Version : Translatef Problem in dynamic cube env mapping scene

raoulduke
10-18-2004, 02:38 AM
hi folks.
i must have an error in reasoning about the coord stuff in my scene.

this scene simply consists of a reflecting sphere that should be placed at the origin (0.0f,0.0f,0.0f) but since i wanted to move through the scene with my camera, i used xtrans, ytrans and ztrans instead, like i learned from nehe.gamedev.net

then next to the sphere should be a cube which should be reflected by the sphere. but somehow i don't get correct reflections.

here's my code:

void initCubePos(int cpos, GLfloat xtrans, GLfloat ytrans, GLfloat ztrans)
{
switch (cpos)
{
case 0:
{
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glRotatef( 90.0f, 0.0f, 0.0f, 1.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(xtrans, ytrans, ztrans);
}

case 1:
{
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(xtrans, ytrans, ztrans);
}

case 2:
{
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(xtrans, ytrans, ztrans);
}

case 3:
{
glRotatef( 90.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(xtrans, ytrans, ztrans);
}

case 4:
{
glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(xtrans, ytrans, ztrans);
}

case 5:
{
glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
glTranslatef(xtrans, ytrans, ztrans);
}
}
}

int DrawGLScene(GLvoid)
{
GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = -0.25f;
GLfloat sceneroty = 360.0f - ymrot;

glMatrixMode(GL_MODELVIEW);
for (int i=0; i<6; i++)
{
// init camera
initCubePos(i, xtrans, ytrans, ztrans);

// render scene without sphere
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glTranslatef(sp_pos[0], sp_pos[1], sp_pos[2]);
DrawGLCube();

glBindTexture(faceTarget[i], texsphere);
glCopyTexSubImage2D(faceTarget[i], 0, 0, 0, 0, 0, 256, 256);
}

// set up camera at viewing position
glTranslatef(xtrans, ytrans, ztrans-10.0f);

glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

// render scene with sphere
glTranslatef(sp_pos[0], sp_pos[1], sp_pos[2]);
DrawGLCube();

glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, texsphere);

glEnable(GL_TEXTURE_CUBE_MAP_EXT);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);

glRotatef(sceneroty, 0, 1.0f, 0);

glPushMatrix();

glTranslatef(xtrans, ytrans, ztrans-10.0f);

glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 1.0f, 0.0f, 0.0f);

glDisable(GL_TEXTURE_CUBE_MAP_EXT);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);

glPopMatrix();

xrot+=xspeed;
yrot+=yspeed;

return TRUE;
}

i know that the code after the for-while is a bit chaotic, which is because i did some experimenting and got confused about when i have to use glLoadIdentity() and glTranslatef(...).
I hope i was able to explain what i wanted to achieve.
any ideas what i have to do?

thanx

dvm
10-18-2004, 05:03 AM
Could you post a picture or describe better what you mean by "not reflecting correctly?"

raoulduke
10-18-2004, 05:30 AM
hi.

Here is a picture of the test-textures i've put on my sphere by default:
http://www.markuslanser.com/shot2.jpg

And this is what i get in my dynamic cube env mapping scene:

http://www.markuslanser.com/shot1.jpg

you can see that the lower (light-blue) texture on the sphere turns green for a reason that i don't understand.
the cube's colors also don't seem to be correctly reflected, but that seems to be caused either by not correctly rendering to the textures, or by not putting them correctly on the sphere. I think i'm not rendering correctly to the textures. This would also explain, that the reflections of the cube doesn't match up at the borders of each texture. You know what i mean?

raoulduke
10-19-2004, 04:14 AM
ok, i made some corrections by myself. the perspectives are correct now - at least for the front texture of the sphere.

but can anybody tell me, why the cube's colours in the reflection aren't the same like the real cube's colours?

raoulduke
10-19-2004, 05:05 AM
new hint:
the cubemap textures aren't able to display blue colours. that's why the upper and the lower texture of the sphere are both green and not green and light blue. to front texture should be blue but is black.

this is caused by the colour i've set for the last face of the cube in my DrawGLCube() function:

void DrawGLCube()
{
// Front Face
glColor3f( 0.0f, 1.0f, 0.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);

// Back Face
glColor3f( 1.0f, 0.0f, 0.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);

// Top Face
glColor3f( 0.0f, 0.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);

// Bottom Face
glColor3f( 0.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);

// Right Face
glColor3f( 1.0f, 0.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);

// Left Face
glColor3f( 1.0f, 1.0f, 0.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();
} i've tested, what happens if i set the colour of the last cube-face (the left face) into white, et voila: everything looks fine.

but that can't be a real solution. any idea, what i might have initialised wrong or sth?

Carjay
10-19-2004, 06:18 AM
Hmm, from a quick glance I see you enable automatic texture generation but you never set up the generation function. Do you do that in some other part of your code?

Like this:

glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_REFLECTION_M AP);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_REFLECTION_M AP);
glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_REFLECTION_M AP);

raoulduke
10-19-2004, 06:25 AM
hi. yes this is done in my InitGL function, which calls updateTexGen()

:

</font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">

int InitGL(GLvoid)
{
// Create font bitmaps for displaying text
text_list_base = glGenLists(255);
wglUseFontBitmaps(hDC, 0, 255, text_list_base);

sphere_angle = 0.0f;

makeCubeMap();
updateTexgen();

glEnable(GL_TEXTURE_CUBE_MAP_EXT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

return TRUE;
}

void updateTexgen(void)
{
assert(mode == GL_NORMAL_MAP_EXT

raoulduke
10-20-2004, 01:24 AM
my method should look like this of course:

void updateTexgen(void)
{
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, mode);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, mode);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, mode);
}