Sign in to follow this  
Sylen

OpenGL Help! Code Interpretation...

Recommended Posts

Hello, sorta new to OpenGL and this forum, so go easy on me :). I have an OpenGL class where my Professor has recently assigned us a lab that requires a whole bunch of things, and he gave us half of the code to start us off... the only issue with starting from someone else's code is interpreting everything and figuring out what they did.... So... my question is this... I'm trying to get my objects that are rendered in the scene to rotate constantly without interruption. For a test, I simply used glRotatef(rotate, 0, 0, 1); and rotate =+ 1.0; As displayed in this code snippet: void Render() { glPushMatrix(); glColor3ubv(color); glTranslatef(pos.x(),pos.y(),pos.z()); glRotatef(rotate, 0, 0, 1); switch (type) { case CUBE : glutWireCube(0.5);break; case TEAPOT : glutWireTeapot(0.5);break; case SPHERE : glutWireSphere(0.5,10,10);break; } //end of switch rotate += 1.0; glPopMatrix(); } However, what happens is, the objects only continuously rotate if my camera is moving around in the scene. Below is posted of the parts of the code that are involved in this. class ObjectC { public: Vect3d pos; GLubyte color[3]; int type; void Set(Vect3d p, Vect3d c, int t) { pos.Set(p); color[0] = c.x();color[1] = c.y();color[2] = c.z(); type=t; } bool Collision(Vect3d p) { if ((pos-p).Length()<RAD) return true; else return false; } void Render() { glPushMatrix(); glColor3ubv(color); glTranslatef(pos.x(),pos.y(),pos.z()); glRotatef(rotate, 0, 0, 1); switch (type) { case CUBE : glutWireCube(0.5);break; case TEAPOT : glutWireTeapot(0.5);break; case SPHERE : glutWireSphere(0.5,10,10);break; } //end of switch rotate += 1.0; glPopMatrix(); } }; vector<ObjectC> objs; void InitScene(bool fixed) { ObjectC tmp; if (fixed) { tmp.Set(Vect3d(4,0,0), Vect3d(128,128,128),SPHERE); objs.push_back(tmp); tmp.Set(Vect3d(0,0,-4), Vect3d(0,0,255),CUBE); objs.push_back(tmp); tmp.Set(Vect3d(10,0,-4), Vect3d(0,0,255),TEAPOT); objs.push_back(tmp); tmp.Set(Vect3d(10,0,4), Vect3d(255,0,255),TEAPOT); objs.push_back(tmp); tmp.Set(Vect3d( 6,0,3), Vect3d(255,255,0),CUBE); objs.push_back(tmp); tmp.Set(Vect3d( 5,0,-4), Vect3d(0,0,255),CUBE); objs.push_back(tmp); } else for (int i=0; i<20; i++) { tmp.Set(Vect3d(rand()/(GLfloat)RAND_MAX*2*SCENESIZE-SCENESIZE,0,rand()/(GLfloat)RAND_MAX*2*SCENESIZE-SCENESIZE), Vect3d(rand()*255/RAND_MAX,rand()*255/RAND_MAX,rand()*255/RAND_MAX), rand()*3/RAND_MAX); objs.push_back(tmp); } } void Reshape(int w, int h) { glViewport(0,0,w, h); //remember the settings for the camera wWindow = w; hWindow = h; //no transformations are needed here, the camera is set elsewhere } void RenderObjects() { //draw the ground glBegin(GL_QUADS); glColor3ub(0,128,0); glVertex3f(-SCENESIZE,-0.6,-SCENESIZE); glVertex3f(SCENESIZE,-0.6,-SCENESIZE); glVertex3f(SCENESIZE,-0.6,SCENESIZE); glVertex3f(-SCENESIZE,-0.6,SCENESIZE); glEnd(); //draw the objects for (unsigned int i=0; i<objs.size(); i++) { objs[i].Render(); } } //camera is at cam[] //object is at obj[i].pos[] void Display(void) { static char needRedisplay; needRedisplay = 0; //the scene does not move glClearColor(0.3,0.3,0.3,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); if (deltaAlpha || deltaStep) { alpha+=deltaAlpha; cam.Set(cam.x()-(GLfloat)sin(alpha*3.14/180)*deltaStep,cam.y(),cam.z()-(GLfloat)cos(alpha*3.14/180)*deltaStep); CheckCollisions(); needRedisplay = 1; } //set the camera first glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40,(GLfloat)wWindow/(GLfloat)hWindow,0.01,100); //The coordinates of the transformations must be inverted!! glRotatef(-alpha,0,1,0); glTranslatef(-cam.x(),-cam.y(),-cam.z()); CheckCollisions(); //then the scene itself glMatrixMode(GL_MODELVIEW); RenderObjects(); //swap buffers glutSwapBuffers(); if (needRedisplay) glutPostRedisplay(); } //+ and 0 shifts the camera up and down void Kbd(unsigned char a, int x, int y) { switch(a) { case '+' : cam.Set(cam.x(),cam.y()+0.1,cam.z());break; case '-' : cam.Set(cam.x(),cam.y()-0.1,cam.z());break; case 27 : exit(0);break; } //end switch glutPostRedisplay(); } //special keys are responsible for the camera manipulation //to make the motion smooth, the PRESS sets the step and alpha //Release sets it to zero void SpecKbdPress(int a, int x, int y) { switch(a) { case GLUT_KEY_LEFT : deltaAlpha = 2.f;break; case GLUT_KEY_RIGHT : deltaAlpha = -2.f;break; case GLUT_KEY_DOWN : deltaStep = -0.1;break; case GLUT_KEY_UP : deltaStep = 0.1;break; } //end switch glutPostRedisplay(); } void SpecKbdRelease(int a, int x, int y) { switch(a) { case GLUT_KEY_LEFT : deltaAlpha = 0;break; case GLUT_KEY_RIGHT : deltaAlpha = 0;break; case GLUT_KEY_DOWN : deltaStep = 0;break; case GLUT_KEY_UP : deltaStep = 0;break; } //end switch // printf("\n%f %f",cam[0],cam[2]); glutPostRedisplay(); } From what it looks like, it seems that the keyboard presses themselves are directly related to the rotation of the object themselves, but in the code, I can't figure out exactly where this relation takes place... or perhaps I am setting up the glRotatef command in the wrong area... Any suggestions would be greatly appreciated!

Share this post


Link to post
Share on other sites
Your problem lies in this code area:

[source lang=c#]

.... from the Display() routine
if (needRedisplay) glutPostRedisplay();





Currently - the way its setup the code will only redraw when a key has been pressed - since in turn this called the glutPostDisplay message.

Solution: remove the if statement and simply call the glutPostDisplay() method at the end of the drawing loop

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this