Jump to content
  • Advertisement
Sign in to follow this  
Colossus_1

OpenGL Moving left and right without rotating

This topic is 5044 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

Hi, I have a 3d model into the scene. With a simple code called when the user presses Key_Left or Right I decrement / increment a flot variable:
void ItemDialogClass::Xpos_dec()
{
	GlWindowClass->Model [ ChoosedModel ].x -= 0.1f;
	Xpos_label->setText ( QString("%1").arg ( GlWindowClass->Model [ ChoosedModel ].x ) );
	GlWindowClass->updateGL();
}

void ItemDialogClass::Xpos_inc()
{
	GlWindowClass->Model [ ChoosedModel ].x += 0.1f; 
	Xpos_label->setText ( QString("%1").arg ( GlWindowClass->Model [ ChoosedModel ].x ) );
	GlWindowClass->updateGL();
}

and this is the OpenGL drawing routine:
void GLWidget::paintGL()
{
	switch (mode)
	{
		case Normal:
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();
		CameraLook();
		for ( std::map< unsigned int , string >::iterator i= GlWindowClass->DisplayList.begin(); i != GlWindowClass->DisplayList.end(); i++)
		{
			unsigned int index = i->first;
			glPushMatrix();
				glTranslatef ( Model[ index-1 ].x , Model[ index-1 ].y  , Model[ index-1 ].z );
				glScalef ( Model[ index-1 ].sx , Model[ index-1 ].sy  , Model[ index-1 ].sz );
				glCallList ( index );
			glPopMatrix();
			printf ("%d\t%f\t%f\t%f\n" , index-1, Model[index-1].x , Model[index-1].y , Model[ index-1 ].z);
		}
		break;
		
		case Select:
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();
		CameraLook();
		break;
	}
}

but the result is not a simple translation, while I move the model it rotates as soon as it reaches the border left or border right of the window. How to avoid the unwanted rotation ? Do I need some maths ?

Share this post


Link to post
Share on other sites
Advertisement
Yes, the rotation is a perspective thing.

Think about it this way...hold a cube in your hand in front of your face, all you can see is the front. Now move the cube to your left, and follow it with your eyes (not moving your head). You would be able to see the right side of the cube.

If you want to correct this, subtract the center of the object from the camera position. This will give you a vector pointing in the direction of the camera. Normalize it, then derive angles so you can rotate it to face the camera.

Share this post


Link to post
Share on other sites
or you could use glOrtho.... but then you would lose ALL the perspective in the scene until you enable glPerspective again... this is useful for 2D graphics etc, but also if you don't want a 3d object to shrink in distance etc...


peace!

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!