Advertisement

3rd person view makes my eys bleed

Started by April 07, 2005 04:23 AM
4 comments, last by traiger 19 years, 10 months ago
Bit new to coding 3D and I'm having trouble getting a 3rd person view to work. So far I've managed 1 dimension the Z axis. Download www.cls-productions.net/files/aarons pong.zip to see what I have so far. Use the cursor keys to control. Now I want to implement the Y-Axis so the view is alway from directly behind the player object. Can anyone help with this. Here are the drawing routines so far:-

void DrawScene()
{
	
// Clear the screen 
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//3D STUFF=================================================================================================================
	glDisable(GL_TEXTURE_2D);
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,800.0f/600.0f,0.1f,700.0f);
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Test To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();	

	current_obj=first_obj;
	while(current_obj)
	{
		current_obj->Draw();
		current_obj=current_obj->next;
	}
     //2d stuff here
}
The following draws each object

void cls_OBJECT::Draw(void)
{
	if(!this->visible) return;
	
	glLoadIdentity();

	if(!this->target)
	{		
		//glRotatef(180+global_y, 0.0, 1.0, 0.0);	doesnt work as it just fixes everything in front of the ship: SO IT CANT TURN
		glTranslatef(0.0f, 0.0f, global_z);		
	}	
	glTranslatef(this->x, this->y, this->z);

	glRotatef(this->rx, 1.0, 0.0, 0.0);
	glRotatef(this->ry, 0.0, 1.0, 0.0);	
	glRotatef(this->rz, 0.0, 0.0, 1.0);

	glBegin(GL_TRIANGLES);
	//draw the triangles
	for( int t_loop=0; t_loop<this->number_of_triangles; t_loop++)
	{
		glColorMaterial(GL_FRONT,GL_DIFFUSE);
		glMaterialfv(GL_FRONT,GL_DIFFUSE, this->triangle[t_loop].diffuse);
		glMaterialfv(GL_FRONT,GL_AMBIENT, this->triangle[t_loop].ambient);
		glMaterialfv(GL_FRONT,GL_SPECULAR, this->triangle[t_loop].specular);

		glNormal3fv(this->triangle[t_loop].norm);
		//one vertex at a time
		for( int v_loop=0; v_loop<3; v_loop++)
		{
			glVertex3f(this->triangle[t_loop].vertex[v_loop].x,
				this->triangle[t_loop].vertex[v_loop].y,
				this->triangle[t_loop].vertex[v_loop].z);
		}//end of vertex loop
		
	}//end of triangle llop
	glEnd();
}
Thanks
Come to think of it I'm out of my depth.

How about transforming the matrix the direction the player object is pointing? How do I do that?
Advertisement
I solved my problem with this lot. Thanks for responding though

if(!this->visible) return;
glLoadIdentity();

//move the camera
if( this!=target_obj)
{
glRotatef(heading , 0.0f, 1.0f, 0.0f);
glTranslatef(global_x+this->x, 0.0f+this->y, global_z+this->z);
}
else
{
glTranslatef(this->x, this->y, this->z);
}

//rotate the object along its specified centre
glTranslatef(this->centre_x, this->centre_y, this->centre_z);
glRotatef(this->ry, 0.0f, 1.0f, 0.0f);
glRotatef(this->rx, 1.0f, 0.0f, 0.0f);
glRotatef(this->rz, 0.0f, 0.0f, 1.0f);
glTranslatef(-this->centre_x, -this->centre_y, -this->centre_z);

glBegin(GL_TRIANGLES);
Er, yeah I don't know of a better way of doing it or that there was even a problem with this method.

Could you show me otherwise?

Thanks.

This topic is closed to new replies.

Advertisement