please help with translation problem, code and screenshots provided

Started by
-1 comments, last by 3dnewbie 16 years, 4 months ago
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;
	

}

This topic is closed to new replies.

Advertisement