PDA

View Full Version : Unidentified problem... Help please



firkraag
01-20-2010, 12:38 AM
I am trying to create 4 objects (2 circles and 2 cubes) and also add the ability to move between them. I have succed in creating them an positioning them properly and also the movement code works. My problem is that when I uncomment the glutPostRedisplay call from the Display function the objects either do not appear at all or starting appearing and disappering... I have tried many different ways to make the scene redraw but all of them have the same result. Any help would be appreciatted.

My code is pasted below it runs as is. It is in c++.

#include <iostream>
#include <stdlib.h>

#include <windows.h>
#include <math.h>
#include <gl\glut.h>

using namespace std;

#define PI 3.1415926535898

GLfloat _size=1.5;

typedef struct point
{
GLfloat x;
GLfloat y;
GLfloat z;
} point;

point camera;
point focus;

void MySphere(GLfloat radius){

GLdouble inc = PI/8;
GLdouble theta, phi;
bool even = true;
for (theta=-PI/2; theta<(PI/2-inc);theta+=inc){
for (phi = 0; phi < 2*PI;phi+=inc) {
glBegin(GL_POLYGON);
glVertex3f(radius*cos(theta)*cos(phi), radius*sin(theta),radius*cos(theta)*sin(phi));
glVertex3f(radius*cos(theta+inc)*cos(phi), radius*sin(theta+inc),radius*cos(theta+inc)*sin(ph i));
glVertex3f(radius*cos(theta+inc)*cos(phi+inc), radius*sin(theta+inc),radius*cos(theta+inc)*sin(ph i+inc));
glVertex3f(radius*cos(theta)*cos(phi+inc), radius*sin(theta),radius*cos(theta)*sin(phi+inc));
glEnd();
}
}
glFlush();
}

void MyCube( GLfloat length, GLfloat height, GLfloat depth){

// base
glBegin(GL_POLYGON);
glVertex3f(-length/2, -height/2, depth/2);
glVertex3f(-length/2, -height/2, -depth/2);
glVertex3f(length/2, -height/2, -depth/2);
glVertex3f(length/2, -height/2, depth/2);
glEnd();

// front face
glBegin(GL_POLYGON);
glVertex3f(-length/2, -height/2, depth/2);
glVertex3f(length/2, -height/2, depth/2);
glVertex3f(length/2, height/2, depth/2);
glVertex3f(-length/2, height/2, depth/2);
glEnd();

// right side face
glBegin(GL_POLYGON);
glVertex3f(length/2, -height/2, depth/2);
glVertex3f(length/2, -height/2, -depth/2);
glVertex3f(length/2, height/2, -depth/2);
glVertex3f(length/2, height/2, depth/2);
glEnd();
// back side face
glBegin(GL_POLYGON);
glVertex3f(-length/2, -height/2, -depth/2);
glVertex3f(-length/2, height/2, -depth/2);
glVertex3f(length/2, height/2, -depth/2);
glVertex3f(length/2, -height/2, -depth/2);
glEnd();
// left side face
glBegin(GL_POLYGON);
glVertex3f(-length/2, -height/2, depth/2);
glVertex3f(-length/2, height/2, depth/2);
glVertex3f(-length/2, height/2, -depth/2);
glVertex3f(-length/2, -height/2, -depth/2);
glEnd();

// top
glBegin(GL_POLYGON);
glVertex3f(-length/2, height/2, depth/2);
glVertex3f(length/2, height/2, depth/2);
glVertex3f(length/2, height/2, -depth/2);
glVertex3f(-length/2, height/2, -depth/2);
glEnd();

}

void Display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
gluLookAt(camera.x,camera.y,camera.z,focus.x,focus .y,focus.z,0.,1.,0.);//create the camera and point it at the proper focus point
glTranslatef(0.,0.,-5.); //move to the first line of objects

glColor3f (1.0, 0.0, 0.0);
glTranslatef(-5,0.,0.); //move to the first row of objects
MySphere(_size); //and create the first sphere

glColor3f (0.0, 1.0, 0.0);
glTranslatef(5,0.,0); //move to the second row of objects
MyCube(2*_size,2*_size,2*_size); //and create a cube

glTranslatef(0.,0.,-10.); //move to the second line of objects

glColor3f (0.0, 0.0, 1.0);
glTranslatef(-5,0.,0); //move to the first row
MyCube(2*_size,2*_size,2*_size); //and create a cube

glColor3f (0.0, 1.0, 1.0);
glTranslatef(5,0.,0); //move to the second row
MySphere(_size); //and create a sphere

//glTranslatef(0.0,0.0,0.0);

//glTranslatef(-5,0.0,0.0);
//glTranslatef(5,0.0,0.0);
//glTranslatef(0,0.0,10.0);
//glTranslatef(-5,0.0,0.0);
//glTranslatef(5,0.0,0.0);
//glTranslatef(0,0.0,5.0);

//glFlush();
glFinish();
//glutSwapBuffers();
glutPostRedisplay(); //The problem is in this call if it is uncommented then the objects flash for less than a second and then disappear
//if it stays commented then the only way to redraw and animate the scene is by resizing the window
}

void keyboard( unsigned char key, int x, int y )
{
switch( key ) {
case 'x' : case 'X' :
exit( EXIT_SUCCESS );
break;
case 'w':case'W':
camera.z--;
break;
case 's':case'S':
camera.z++;
break;
case 'a':case'A':
camera.x--;
break;
case'd':case'D':
camera.x++;
break;
case'q':case'Q':
focus.x--;
break;
case'e':case'E':
focus.x++;
break;
default:
break;
}
}
void reshape (int w, int h)
{
// on reshape and on startup, keep the viewport to be the entire size of the window
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
//glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5,0.5, 1., 200.0);
gluPerspective(60., (GLfloat)w/h, 1., 200.);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}


void init(void){

//set the clear color to be black
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);

camera.x=0.0;
camera.y=0.0;
camera.z=10.0;

focus.x=0.0;
focus.y=0.0;
focus.z=-50.0;
}

//void idle(void)
//{
// glutPostRedisplay();
//}

int main(int argc, char* argv[])
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow("A night at the museum");
init();
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
//glutIdleFunc(idle);
glutKeyboardFunc(keyboard);

glutMainLoop();
}

ZbuffeR
01-20-2010, 05:29 AM
The problem is that all your transformations accumulate at each Display() call, and things move too fast, especially if vsync defaults to off.

surround Display code with :

Display () {
glPushMatrix();
... current code ...
glPopMatrix();
}

You should read and do some tutorials about transformations, projections, modelview, until you are more confident about this.
http://www.falloutsoftware.com/tutorials/gl/gl5.htm
http://www.opengl.org/resources/faq/technical/transformations.htm

More keywords : homogeneous transformation, projection matrices