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 on other sites
Your problem lies in this code area:

[source lang=c#] .... from the Display() routineif (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

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

• Partner Spotlight

• Forum Statistics

• Total Topics
627677
• Total Posts
2978589
• Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
Thank you in advance!
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 12
• 10
• 12
• 22