Sign in to follow this  
3dnewbie

rotation problem - please assist

Recommended Posts

Hi, I'm having a problem with rotation. The problem is that my main view, which is comprised of a 2d grid 3d objects on them, and object properties (such as names, lines that describe the 3d objects). Now, i recently implemented some sort of arcball rotation, so that i can move the mouse and rotate the view. The grid rotates, the properties rotate, but the objects don't. Well they rotate, but only about their local world coordinates. I suspect it has something to do with me having to do some push/pop in the object display code. Because that's the only difference with the other functions. Funny thing is, that they keyboard function works fine. They keyboard moving is handled in idle() though, whereas the mouse freelook is done in the main rendering function. Code is using GLUT library. This is the display_objects() code:
GLvoid display_objects(GLvoid)
{
	/* Here we draw the objects that our in our database on the main grid. We
	search through our database for xyz coordinates and we use a bitmapfont to
	draw some basic info */
	objects *tmp=o_head;
	
	while(tmp!=NULL) {
	glPushMatrix();
	
	glTranslatef(atof(tmp->x),atof(tmp->y),atof(tmp->z));
		
	glBegin(GL_QUADS);
				glNormal3f(0.0f,0.0f,1.0f);
                glVertex3f( -OBJECT_SIZE_X, OBJECT_SIZE_Y,-0.2f);
                glVertex3f(-OBJECT_SIZE_X,OBJECT_SIZE_Y,-0.2f);
                glVertex3f(OBJECT_SIZE_X,OBJECT_SIZE_Y, 0.2f);
                glVertex3f(OBJECT_SIZE_X,OBJECT_SIZE_Y, 0.2);
                glNormal3f(0.0f,0.0f,-1.0f);
                glVertex3f( OBJECT_SIZE_X,-OBJECT_SIZE_Y,0.2f);
                glVertex3f(-OBJECT_SIZE_X,-OBJECT_SIZE_Y,0.2f);
                glVertex3f(-OBJECT_SIZE_X,-OBJECT_SIZE_Y,-0.2f);
                glVertex3f( OBJECT_SIZE_X,-OBJECT_SIZE_Y,-0.2f);
                glNormal3f(0.0f,1.0f,0.0f);
                glVertex3f(OBJECT_SIZE_X,OBJECT_SIZE_Y, 0.2f);
                glVertex3f(-OBJECT_SIZE_X,OBJECT_SIZE_Y,0.2);
                glVertex3f(-OBJECT_SIZE_X,-OBJECT_SIZE_Y,0.2f);
                glVertex3f(OBJECT_SIZE_X,-OBJECT_SIZE_Y,0.2f);
                glNormal3f(0.0f,-1.0f,0.0f);
                glVertex3f(OBJECT_SIZE_X,-OBJECT_SIZE_Y,-0.2f);
                glVertex3f(-OBJECT_SIZE_X,-OBJECT_SIZE_Y,-0.2f);
                glVertex3f(-OBJECT_SIZE_X, OBJECT_SIZE_Y,-0.2f);
                glVertex3f( OBJECT_SIZE_X, OBJECT_SIZE_Y,-0.2f);
                glNormal3f(1.0f,0.0f,0.0f);
                glVertex3f(-OBJECT_SIZE_X,OBJECT_SIZE_Y, 0.2f);
                glVertex3f(-OBJECT_SIZE_X,OBJECT_SIZE_Y,-0.2f);
                glVertex3f(-OBJECT_SIZE_X,-OBJECT_SIZE_Y,-0.2f);
                glVertex3f(-OBJECT_SIZE_X,-OBJECT_SIZE_Y, 0.2f);
                glNormal3f(-1.0,0.0f,0.0f);
                glVertex3f( OBJECT_SIZE_X,OBJECT_SIZE_Y,-0.2f);
                glVertex3f(OBJECT_SIZE_X,OBJECT_SIZE_Y, 0.2f);
                glVertex3f(OBJECT_SIZE_X,-OBJECT_SIZE_Y,0.2f);
                glVertex3f(OBJECT_SIZE_X,-OBJECT_SIZE_Y,-0.2f);
	glEnd();						
	
	/* Set title of objects to a white color so we can see it */
	glColor4f(1.0f,1.0f,1.0f,0.1f);
	/* If you're annoyed by the title of the object being obscured
	by the object itself when blending is off, increase x by 0.4 */
	renderbitmapfont(0.0,0,0,"%s",tmp->hostname);

	tmp=tmp->next;

	glPopMatrix();
	}

}
and this is the main rendering loop:
GLvoid render(void)
{
	static GLint frame=0;
	static GLint timebase=0;
	GLint time=0;
	pthread_t tid_network;
	
	/* Clear color and depth buffer */
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	/* Reset MODELVIEW and PROJECTION matrices */
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	/* Initialization mode. Here we load our display lists
	for drawing and maintain our grid system. */
	
	/* We move -6 into the z-axis */
	glTranslatef(0.0,0.0,-6.0);
	
	/* We rotate the matrix with 95.0 on the x-axis */
	glRotatef(95.0,1.0f,0.0f,0.0);
	
	/* Rotations for FREELOOK */
	if(FREELOOK) {
	/* We always rotate from the center of the grid 0,0 */
	glRotatef(user_x,1.0f,0.0f,0.0f);
	glRotatef(-user_y,0.0f,0.0f,1.0f);}

	/* We perform zoom function if requested. This is the
	only navigational control that is put here. Zoom does
	not work properly in the idle() function. */
	glTranslatef(0.0f,0.0f,f);			/* Zoom in/out */
	glMatrixMode(GL_PROJECTION);
	
	/* Calculate our frame rate */
	frame++;
	time=glutGet(GLUT_ELAPSED_TIME);
	
	if ((time-timebase) > 1000) {
		fps = frame*1000.0/(time-timebase);
	 	timebase = time;		
		frame = 0;
	}
	
	/* Display options */
	
	if(GRID)
	display_grid();
	
	if(OBJECTS)
	display_objects();

	if(OBJECT_PROPERTIES)
	display_object_properties();

	if(COORDINATE)
	display_coordinate();
	
	if(STATISTICS)
	display_stats();
	
	if(EVENTVIEW) 
	display_events();
	
	/* Engine options */
	engine_options();
	
	glFlush();
	glutSwapBuffers();
	}

Share this post


Link to post
Share on other sites
The rotation transform should be applied after the individual object transforms. This is because all rotations occur by default around the origin, so rotating before translating rotates objects around their center.

Also, it would be best for your code if input was handled on its own, and provided the (separate) rendering code with clean angular values.

For large amounts of code, like in your post, I would suggest using [source] tags instead of [code] ones.

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