Advertisement Jump to content
Sign in to follow this  

OpenGL Rotating several 3D rectangles

This topic is 4592 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:
glClearColor( 1.0f, 1.0f, 1.0f, 0.5f);

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




And finally when im rendering the rectangles (On Paint) it looks something like this:
//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
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:



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

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);




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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!