Jump to content
  • Advertisement
Sign in to follow this  
vpk

viewpoint distance

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

how to calculate the distance of a triangle/object from the camera??

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by vpk
how to calculate the distance of a triangle/object from the camera??


You need the position vector of the object and the camera.. in the format 3vector(x,y,z)..the two vectors must be in the same space as well...normally they are in world space.

Then simply do this:
distance=length(objectpos-campos);

This is one of the most basic vector operations...

Share this post


Link to post
Share on other sites
You don't have to retreive the camera position, you should already have it. Your camera starts at position xyz {0,0,0}. Once you move, you should apply that on the camera position. A typical camera class could like this:

// Pseudo code
camera = class
position : vector3f; // = x, y and z component
viewDir : vector3f; // Direction you lookat. Often the same as "moveDir"
moveDir : vector3f; // Direction you move to when going forward
upVector : vector3f; // typically x=0, y=1, z=0

void moveForward( float speed );
{
position.x += moveDir.x * speed;
position.y += moveDir.y * speed;
position.z += moveDir.z * speed;
}
void moveBackward( float speed );
{
position.x -= moveDir.x * speed;
position.y -= moveDir.y * speed;
position.z -= moveDir.z * speed;
}
void strafeLeft( float speed );
void strafeRight( float speed );
void lookAt( vector3f targetPosition );
void rotateAroundTarget( float deltaX, float deltaY );
...
void applyCamera; // Set OpenGL view. Call this before rendering
{
glLoadIdentity;
gluLookAt( position.x, position.y, position.z,
position.x - viewDir.x, position.y - viewDir.y, position.z - viewDir.z,
upVector.x, upVector.y, upVector.z );
}


Almost every OpenGL demo program has some sort of a camera system. I'd suggest you take a look at that.

Anyway, the "position" vector you calculate here when moving around, can be used to calculate the distance between the camera and any other point in space, like Matt said.

Greetings,
Rick

Share this post


Link to post
Share on other sites
like spek said any camera class should have a component that gives position.

A camera will typically be a class or a struct...so it have a variable that is like camera->position.xyz or camera.pos or something like this...

Share this post


Link to post
Share on other sites
Original post by spek
You don't have to retreive the camera position, you should already have it. Your camera starts at position xyz {0,0,0}.

void applyCamera; // Set OpenGL view. Call this before rendering
{
glLoadIdentity;
gluLookAt( position.x, position.y, position.z,
position.x - viewDir.x, position.y - viewDir.y, position.z - viewDir.z,
upVector.x, upVector.y, upVector.z );
}


the position.x,position.y values have to be specified first by me in the gluLookAt command, isnt it??? then how do i calculate the viewdir and movedir values???

Share this post


Link to post
Share on other sites
That depends on how you like to move around/navigate. Usually I use a first or third person camera. I ussually use a invisible point 'dummy' where the camera turns around. When pressing a button, the dummy moves into a certain direction, and the camera follows. Compare it to the earth and the moon. The earth moves, the moon follows and rotates around the earth. In a first person camera system, this point is VERY close to the camera point, distance = 0.1 or something. In a third person camera system, the distance can be much bigger, a few meters for example.

If you decide to use your mouse to "look", you must turn around that dummy point. I always do it like this:
1.- Centrate the mouse cursor on the center of the screen each frame (set the mouse cursor xy to screenWidth / 2, screenHeight / 2)
2.- When the mouse moves, it should be away from the center. You can calculate the difference between the current mouseCoord and the center:
mx = mouse.x - center.x;
my = mouse.y - center.y;
After that, you can reset your mouse to the center again. You don't have to do this, but it prevents the cursor moving outside the screen.
3.- With mx and my, you can rotate around your dummy. Use a certain distance between the dummy and the camera (near zero for first person, a bigger distance for third person)

The move direction can be set equal to the view direction to start with. This is ussually the case in first person games, except for the up vector (your character does not fly or sink through the floor when looking up/down) and when strafing to the left or the right.

Because the camera orbits around a dummy point, the camera position is not exactly equal to the dummy position. In case of a first person game, you can still use the dummy position for gluLookAt. Otherwise you muse make a function that calculates the absolute position of the camera. Basically the result is the dummy.position + camera.localPosition. The local position is inside the dummy domain where the camera rotates. The local position is the negated view direction, multiplied with the distance.

camera.absPos.x = dummy.pos.x - camera.viewDir.x * distance;
camera.absPos.y = dummy.pos.y - camera.viewDir.y * distance;
camera.absPos.z = dummy.pos.z - camera.viewDir.z * distance;


As for gluLookAt, you could use
glLookAt( <pos>, <direction>, <upvector> )
gluLookAt( <camera.absPos> , <camera.viewDir>, <0,1,0> );

I don't know if this is the best way to do it, but it works. I suggest you look into some of the vector operations. I'm not good with mathematics, but the basic vector operations really aren't that hard. A couple of simple but important ones:

vectorSubtract = vector A - B
Can be used to calculate direction between point A and B
vectorAdd = A + B
Can be used to move A into direction B
vectorMultiply = A * B
normalize = Scale a vector into the [-1..+1] range. A lot of operations that deal with directions must use "normalized" directions.

direction.xyz = normalize( targetPosition.xyz - sourcePosition.xyz );


Rick

Share this post


Link to post
Share on other sites
thanks for your reply

but i think i have to get the basics of the camera func cleared first.

i) in opengl, there is a camera by default which is at (0,0,0), isnt it??
then when you navigate by add/subtract along x/z axis using gtranslateor grotate you move the camera, is it??
how do i get the current camera position? is it the value of x at that point of time?

ii) then what does gluLookAt and gluPerspective do??

regards

Share this post


Link to post
Share on other sites
>> in opengl, there is a camera by default which is at (0,0,0), isnt it??

Yep. As long as you don't move, you are "at the center of the universe".

>>then when you navigate by add/subtract along x/z axis using gtranslateor >>grotate you move the camera, is it??
Not exactly. The camera stays at position {0,0,0}. Instead, the entire world around you moves. Compare it when sitting in the train and looking outside. Sometimes it's as if the station is moving, instead of the train itself.
glScale/Translate/Rotate are not explicitely used for the camera itself. Basically it transforms everything that follows. When I call glTranslate(1,0,0) in GL_MODELVIEW_MODE, all the vertices that will follow are moved into the X axis with 1 unit.

>> then what does gluLookAt and gluPerspective do??
Some systems move/rotate the world instead of the camera itself, but I like to transform the camera itself instead of the world, since it makes more sence. And this is where gluLookAt kicks in. gluLookAt transforms/rotates the camera for you in a relative simple way. In fact, you don't need any glRotate or glTransform functions at all in your program (well, for some special tricks later on, but not for basic rendering). Just render the world at a static position, and use gluLookAt to navigate. The input parameters for gluLookAt are your position, view direction and up vector. You can calculate them like I told in the earlier posts.

gluPerspective is not the same as gluLookAt. It adjusts your viewing frustum, not your camera position or rotation. Compare it with adjusting the lens on your foto camera. With a different lens, you can focus/zoom in a different way (zNear, zFar, field of view, screen ratio, parameters). You don't need this function if the default OpenGL settings are fine for you.

BTW, don't forget that there is OpenGL forum on gamedev for specific OpenGL questions. The OpenGL guru's hang out there.

Share this post


Link to post
Share on other sites
Quote:
Original post by spek
>> then what does gluLookAt and gluPerspective do??
gluLookAt transforms/rotates the camera for you in a relative simple way. In fact, you don't need any glRotate or glTransform functions at all in your program (well, for some special tricks later on, but not for basic rendering). Just render the world at a static position, and use gluLookAt to navigate. The input parameters for gluLookAt are your position, view direction and up vector. You can calculate them like I told in the earlier posts.



so if i am using gluLookAt, the first position.x etc values have to be specified at initialisation?? and then as explained by you earlier i can change the position as i navigate??

gluLookAt( position.x, position.y, position.z,position.x - viewDir.x, position.y - viewDir.y, position.z - viewDir.z,upVector.x, upVector.y, upVector.z );

but how do i calculate the initial position and viewdir values??

regards

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!