please help with rotation/translation problem, screenshot and code provided

Started by
4 comments, last by 3dnewbie 16 years, 4 months ago
(mods, please move the other post in opengl forum to nothingness, this is more relevant forum i think) Hi, I've posted questions about this before but i was busy with other projects since then, and now i would like to get back into it and fix some outstanding problems. I hope gamedev community can answer my questions, i'll provide relevant code and screenshots. Please, be aware that i don't know that much about ogl code, i'm a average c programmer and i use GLUT for compatibility. My program displays a 2d gridmatrix in a 3d environment where i can move with either the keyboard or the mouse (used for fps freeview, still using forward and backward keys to move). The keyboard seems to work fine, but the mouse function although it seems ok, really isn't. The problem is that it always rotates around it's axis from where the matrix was created from (with matrix i mean the grid i created). So if i move all the way to the left of the grid, when i rotate around myself with the mouse, the whole grid moves around me. I added code as some requested, hope it is not to much and it's understandable. screenshot : screen main rendering code:

GLfloat fps;

GLvoid render(void)
{
	static GLint frame=0;
	static GLint timebase=0;
	GLint time=0;
	pthread_t tid_network;
	static GLint init=1;
	static GLfloat user_x_prev=0;
	static GLfloat user_y_prev=0;
	
	/* Clear color and depth buffer */
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	/* Reset MODELVIEW and PROJECTION matrices */
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	/* We move -6 into the z-axis */
	glTranslatef(0,0,-6.0);
	
	/* We rotate the matrix with 95.0 on the x-axis */
	glRotatef(95.0,1.0f,0.0f,0.0);
	
	/* Transformations for FREELOOK */
	if(FREELOOK) {
	if(init) {
	user_x=user_x_prev; 
	user_y=user_y_prev;
	init=0;}

	glRotatef(user_x,1.0f,0.0,0.0f);
	glRotatef(-user_y,0.0f,0.0,1.0f);

	glTranslatef(0.0f,0.0,a);			
	glTranslatef(b,0.0f,0.0f);			
	
	user_x_prev=user_x;
	user_y_prev=user_y;}
	else {
	init=1;}
	
	/* 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();
	}
my display_grid() code: (it creates quads and overlays them with lines to create a nice matrix)

GLint x,y,z;

GLint sector_size=SECTOR_SIZE; /* 2 */
GLint grid_size=GRID_SIZE; /* variable say 30 */
GLint grid_start=GRID_START; /*currently -5 */
GLint grid_object=GRID_OBJECT;

GLvoid display_grid(GLvoid)
{           
	glBegin(GL_QUADS);
	for(x=grid_start;x<grid_size;x+=sector_size) {
	for(y=grid_start;y<grid_size;y+=sector_size) {
	/* Set color for QUADS */
	glColor4f(0.0f,0.0f,0.2,1.0f);
	/* Draw QUADS */
	glVertex3f(x,y,0); /* Left bottom */
	glVertex3f(x+sector_size,y,0); /* Right bottom */
	glVertex3f(x+sector_size,y+sector_size,0); /* Right upper */
	glVertex3f(x,y+sector_size,0); /* Left upper  */
			}
	}
	glEnd();
	
	glBegin(GL_LINES);
	for(x=grid_start;x<grid_size;x+=sector_size) {
	for(y=grid_start;y<grid_size;y+=sector_size) {
	/* Set color for LINES */
	glColor4f(0.0f,0.2f,0.0,0.5f);
	/* Draw lines */
	glVertex3f(x,y,0);
	glVertex3f(x+sector_size,y,0);
	glVertex3f(x+sector_size,y,0);
	glVertex3f(x+sector_size,y+sector_size,0);
	glVertex3f(x+sector_size,y+sector_size,0);
	glVertex3f(x,y+sector_size,0);
	glVertex3f(x,y+sector_size,0);
	glVertex3f(x,y,0);
		}
	}
	glEnd();

}
my idle() function:

GLvoid idle(GLvoid)
{

	/* Transformations for CONTROL */
	
	glRotatef(c,1.0f,0.0f,0.0f);		/* Tilt x-axis */
	glRotatef(d,0.0f,1.0f,0.0f);		/* Tilt y-axis */
	glRotatef(e,0.0f,0.0f,1.0f);		/* Tilt z-axis */	
	
	glTranslatef(0.0f,0.0,a);			/* Forward/Backward */
	glTranslatef(b,0.0f,0.0f);			/* Left/Right */
		
	glutPostRedisplay();
}
my mouse_move() code:

GLfloat user_x,user_y,user_z;

GLvoid mouse_move(GLint x,GLint y)
{

	user_x+=(y-user_pos_y);
	user_pos_y=y;
	
	user_y+=(x-user_pos_x);
	user_pos_x=x;
	
	if(user_x>360.0f)
	user_x-=360.0f;
	
	if(user_x<0.0f)
	user_x+=360.0f;
	
	if(user_y>360.0f)
	user_y-=360.0f;
	
	if(user_y<0.0f);
	user_y+=360.0f;
	

}
Advertisement
anyone ?
One of the things that always irritates me about opengl is that it combines the view and world matrices into the single modelview matrix, even though it's much more intuitive to think of them separately. Having the two together also makes it easier for bugs like this to appear.

Look up gluLookAt.

I won't go into details as there's many resources online about it.

But basically, when rendering your scene:

Set ModelView to identity
Call gluLookAt with your current camera orientation info.

For each object you're rendering:
push the modelview matrix
Apply the object's transformations (rotation, translation, scale)
render object
pop the modelview matrix
I'll probably post a short clip to demonstrate what's going wrong. Let me make clear, i can rotate around myself no problem with the mouse, and i can look everywhere, but when i move to the left or wherever, it's like i translate, but still rotate around the original axis (0,0). The 0,0 never moves, i move around it. Like a line that keeps me centered there. It's like i rotate, but not translate. Hope i don't sound to confusing here.
Are you saying that you move forward in terms of the grid and not forward in terms of the direction you're facing?
I don't think so, no. It's not a translate first, then rotate problem (i think). I can move wherever i want, no problem, the grid doesn't get in my way. But when i have a grid of say 40^2, and i move from the center (which rotates fine) all the way to the left, and i rotate, the rest of the grid i left 'behind' is still moving around 0,0. I'll post a vid this afternoon.

This topic is closed to new replies.

Advertisement