how do i focus on a object in 3d space

Started by
5 comments, last by Adam Hamilton 17 years, 2 months ago
Hi. I would like that with every rendering pass my program focusses on one of the many objects in my 3d space. Hmm maybe not though. If i later have a lot of objects, it would slow down to much. Just when adding an object i mean. I would like it to focus on that object. How would i go about achieving this ? I use this code to display all the objects (3d quads) in the 3d space:
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 information of the object. */
	objects *tmp=o_head;
	
	while(tmp!=NULL) {
	glPushMatrix();
	glTranslatef(atof(tmp->x),atof(tmp->y),atof(tmp->z));
		
	glBegin(GL_QUADS);
		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);			
			
		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);		
					
		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);		
				
		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);			
			
		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);		
				
		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();
	}
}
I tried to use the same method to do what i would like, but i can't get it to work.
Advertisement
Take a look at gluLookAt.
I did that, but it doesn't make much sense to me. It scares me :-)
There's nothing scary about that function. ;)

The first 3 parameters are the x, y and z coördinates of your camera. The second set of 3 are the coördinates of the point you're looking at. The last set of 3 form the up vector of the camera. With this vector you can control the roll of your camera, for example, by inverting this vector your camera will look up-side down.
Create-ivity - a game development blog Mouseover for more information.
alright, but how do i get the coordinates of my camera ? i know the coordinates of the objects
That depends on how you want to look at those objects. If it's a sort of RTS-like top-down view, then you can simply use the location of the object, and up the height somewhat, so you'll look straight down at it. Try fiddling with some values, it really isn't difficult. :)
Create-ivity - a game development blog Mouseover for more information.
I think that if you set the matrix mode to GL_PROJECTION and retrieve the matrix (if such a function exists) you can get the camera XYZ coords, target XYZ coords and the UP vector.

I would personally make a camera class that handled the camera stuff.


This topic is closed to new replies.

Advertisement