Public Group

# how do i focus on a object in 3d space

This topic is 4157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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. */

while(tmp!=NULL) {
glPushMatrix();
glTranslatef(atof(tmp->x),atof(tmp->y),atof(tmp->z));

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.

##### Share on other sites
Take a look at gluLookAt.

##### Share on other sites
I did that, but it doesn't make much sense to me. It scares me :-)

##### Share on other sites
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.

##### Share on other sites
alright, but how do i get the coordinates of my camera ? i know the coordinates of the objects

##### Share on other sites
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. :)

##### Share on other sites
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.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631365
• Total Posts
2999581
×