Jump to content
  • Advertisement
Sign in to follow this  
jjohn

OpenGL Rotating several 3D rectangles

This topic is 4497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am building an application that visualizes rectangles (with width, height, length properties and x, y, z positions) in a 3d space. The problem I have is that I want to be able to rotate the entire view of all rectangles at the same time, while keeping them in view. I tried calling glLoadIdentity and glRotatef before glBegin on each rectangle, however that made them rotate differently in perspective to each other. Now im calling glLoadIndentiy and glRotate once before rendering all the rectangles and the result is that they disappear out of my view after a while (I am increasing the rotation for each OnPaint event) This probably has something to do with the fact that im using -10.0f on the Z-axis on every rectangle I render. My initialization code looks something like this:
glShadeModel(GL_SMOOTH);
glClearColor( 1.0f, 1.0f, 1.0f, 0.5f);
glClearDepth(1.0f);                   
glEnable(GL_DEPTH_TEST);              
glDepthFunc(GL_LEQUAL);       
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

My resizing code looks like this:
glViewport( 0,0,width,height ); 

glMatrixMode(GL_PROJECTION);
glLoadIdentity();           

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); 

glMatrixMode(GL_MODELVIEW); 
glLoadIdentity();

And finally when im rendering the rectangles (On Paint) it looks something like this:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glLoadIdentity(); 
//Testing: just rotate the rectangles
glRotatef( _xrot-=0.1f, 1.0f, 0.0f, 0.0f ); 
glRotatef( _yrot+=0.1f, 0.0f, 1.0f, 0.0f ); 
glRotatef( _zrot, 0.0f, 0.0f, 1.0f ); 
          
/****** DRAW RECTANGLES *******/ 
glBegin( GL_QUADS )
...
...

I suppose this is a really problem to solve for anybody with a little OpenGL experience (unlike myself) Any ideas? (I'll be happy to post more code of the problem if its needed) Regards, John

Share this post


Link to post
Share on other sites
Advertisement
Are you trying to rotate them all around their own center point? Or are you trying to rotate them all, like they are the ends of your fingers, and you are turning your hand around?

If you're attempting to do the latter, the code that you have should be fine.

If it's the former, then you want to be drawing every rectangle around the point 0,0,0. That is, if you had a cube which had sides of length 2, the coords would be (-1,-1,-1), (1,-1,-1), (1,-1,1), (-1,-1,1), (-1,1,-1), (1,1,-1), (1,1,1), and (-1,1,1).

So, to apply a transformation to each rectange seperately you'd do something like this:

glLoadIdentity();

glBegin(GL_QUADS);

for(int i=0; i < num_rectangles; ++i){
glPushMatrix();

glRotatef(xrot, 1.0, 0.0, 0.0);
glRotatef(yrot, 1.0, 0.0, 0.0);
glRotatef(zrot, 1.0, 0.0, 0.0);
glTranslatef(xtrans, ytrans, ztrans);

drawRectangle(i);

glPopMatrix();
}

glEnd();


Something like that should do it.

Hope I was helpful.

Share this post


Link to post
Share on other sites
Hello Endar,

Thanks for your quick reply. Yes you are right, im trying to accomplish the latter. The code I have does just that but the rectangels don't stay centered on the screen, they instead disappears out of view.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!