PDA

View Full Version : Passing from one "scene" to another problem



Vincent22
04-28-2012, 05:01 AM
Hi everybody. I'm an Opengl beginner, I developed a simple program made up of two "different scene".
I have a start menu and a selection scene, If I load them individually they work fine.
The problem is passing from one scene to another: I load first the start menu and then pass to selection scene throught Main.cpp (e.g. pressing a button) the visualization of the second scene is not correct (and viceversa), if I resize the window the scene goes alright.

Here is the code:

Main:


glutReshapeFunc(reshape);
window1.render(&display);
window1.mouse(&mouseFunc);

void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();



switch (CURRENTSTATE) {
case STARTMENU:
if(start->initialize()) {
start->render();
}
break;

case SELECTION:
if(selection->initialize()) {
selection->render();
}
break;
case GAME:
if(game->initialize()) {
game->render();
}
break;
default:
break;
}


glFlush();

}


void reshape(int width, int height)
{
switch (CURRENTSTATE) {
case STARTMENU:
start->reshape(width, height);
break;

case SELECTION:
selection->reshape(width, height);
break;

case GAME:
game->reshape(width, height);
break;

default:
break;
}
}




Start Menu display:


void StartMenu::render()
{

gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -5.0f);



m_Background.render();



button1->render();
button2->render();
button3->render();




GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

// button1
gluProject(button1->position.x, button1->position.y, button1->position.z,
model_view, projection, viewport,
&b1_pos_x, &b1_pos_y, &b1_pos_z);

gluProject(button1->position.x+button1->width, button1->position.y+button1->height, button1->position.z,
model_view, projection, viewport,
&b1_posx_width, &b1_posy_height, &b1_pos_z2);

//button2
gluProject(button2->position.x, button2->position.y, button2->position.z,
model_view, projection, viewport,
&b2_pos_x, &b2_pos_y, &b2_pos_z);

gluProject(button2->position.x+button2->width, button2->position.y+button2->height, button2->position.z,
model_view, projection, viewport,
&b2_posx_width, &b2_posy_height, &b2_pos_z2);

//button3
gluProject(button3->position.x, button3->position.y, button3->position.z,
model_view, projection, viewport,
&b3_pos_x, &b3_pos_y, &b3_pos_z);

gluProject(button3->position.x+button3->width, button3->position.y+button3->height, button3->position.z,
model_view, projection, viewport,
&b3_posx_width, &b3_posy_height, &b3_pos_z2);

}



Selection Scene display:


void SelectionScene::render()
{

gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -5.0f);

m_Background->render();



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)wwidth / (GLfloat)wheight, 1.0, 100.0);
gluLookAt(0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -5.0f);


if(currCycle>0) {
left1->render();
}
if(currCycle<numCycles-1) {
right1->render();
}

play->render();

GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);

GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

// button1
gluProject(left1->position.x, left1->position.y, left1->position.z,
model_view, projection, viewport,
&l1_xpos, &l1_ypos, &l1_zpos);

gluProject(left1->position.x+left1->width, left1->position.y+left1->height, left1->position.z,
model_view, projection, viewport,
&l1_xpos_width, &l1_ypos_height, &l1_zpos2);

// button2
gluProject(right1->position.x, right1->position.y, right1->position.z,
model_view, projection, viewport,
&r1_xpos, &r1_ypos, &r1_zpos);

gluProject(right1->position.x+right1->width, right1->position.y+right1->height, right1->position.z,
model_view, projection, viewport,
&r1_xpos_width, &r1_ypos_height, &r1_zpos2);

// Play button
gluProject(play->position.x, play->position.y, play->position.z,
model_view, projection, viewport,
&p_xpos, &p_ypos, &p_zpos);

gluProject(play->position.x+play->width, play->position.y+play->height, play->position.z,
model_view, projection, viewport,
&p_xpos_width, &p_ypos_height, &p_zpos2);


glBegin(GL_QUADS);
glColor3f(0.1,0.1,0.4);
glVertex3f(-4.0,0.5,-1.0);
glVertex3f(-0.1,0.5,-1.0);
glVertex3f(-0.1,2.7,-1.0);
glVertex3f(-4.0,2.7,-1.0);

glVertex3f(0.1,0.5,-1.0);
glVertex3f(4.0,0.5,-1.0);
glVertex3f(4.0,2.7,-1.0);
glVertex3f(0.1,2.7,-1.0);
glEnd();


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)wwidth / (GLfloat)wheight, 1.0, 100.0);
gluLookAt(20.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
float lightPos[] = {1.0, 1, 1, 0.0};
float ambient[] = {0.0, 0.0, 0.0, 0.0};
//glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);


glPushMatrix();
glScalef(0.5, 0.5, 0.5);
glRotatef(180.0, 0.0, 1.0, 0.0);
glRotatef(-20.0, 0.0, 0.0, 1.0);
glTranslatef(0.0f, 10.0f, -17.0);

cycles[currCycle].drawCycle();

glPopMatrix();

glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);




}



I pass to one scene to another setting the global variable CURRENTSTATE in the mouse Func.

Someone can help me? Thank you.

Vincent22
04-28-2012, 10:30 AM
solved deleting "glLoad()" on GL_PROJECTION at the beginning of Selection Scene display func