Archived

This topic is now archived and is closed to further replies.

aker_jus

Camera code problems..

Recommended Posts

Hello, my camera class sucks, here is my code. I hope you can help me make a better one, because for some reason, clip planes arent computed easily. [cpp] void CRTCamera::SetupCamera(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ) { rtCamera.vEye = Vec3(eyeX, eyeY, eyeZ); rtCamera.vLookAt = Vec3(targetX, targetY, targetZ); rtCamera.fAspect = 1.0f; D3DXMatrixPerspectiveFovLH(&rtCamera.matProj, rtCamera.fFov, rtCamera.fAspect, rtCamera.fNear, rtCamera.fFar); D3DXMatrixLookAtLH(&rtCamera.matView, &rtCamera.vEye, &rtCamera.vLookAt, &rtCamera.vUp); } /* Method/Function name: SetPosition --------------------------------- DESC: Updates the camera eye position. --------------------------------- */ void CRTCamera::SetPosition(float x, float y, float z) { rtCamera.vEye = Vec3(x,y,z); D3DXMatrixLookAtLH(&rtCamera.matView, &rtCamera.vEye, &rtCamera.vLookAt, &rtCamera.vUp); } /* Method/Function name: SetLookAt ------------------------------- DESC: Updates the camera looking at vector. ------------------------------- */ void CRTCamera::SetLookAt(float x, float y, float z) { rtCamera.vLookAt = Vec3(x,y,z); D3DXMatrixLookAtLH(&rtCamera.matView, &rtCamera.vEye, &rtCamera.vLookAt, &rtCamera.vUp); } [/cpp] then, I set the view and projection matrices for that camera in the start of my loop. But this doesnt work well. What is your camera code, or maybe you can point me to some nice camera tutorial for d3d? Thanks!

Share this post


Link to post
Share on other sites
Okay, for the Aspect Ratio...it should be

rtCamera.fAspect = XRES / YRES;

Or whatever the backbuffer width and height are. It''s a good idea to store them. Because if you are windowed mode, you can''t be sure of what the width and height of your buffer is. That will make things like cubes more square like and spheres more like circles instead of the ellipse and stuff.

Anyway, what do you mean your clip planes aren''t computed easily? To me it looks like you aren''t computing any clipping planes...it automatically does that when you set up the VIEW and PROJECTION matrices...unless you need additional clipping planes.

Are you having problems with objects being clipped too close or far away? What are your rtCamera.fNear and fFar values? What about the rtCamera.fFov? A nice value to use for FOV is D3DX_PI/4. Yeah, that''s nice.

The tutorials that come with the SDK documentation seem to work somewhat well. Tutorial number 3 is about using Matrices, which includes setting up the camera and stuff.

Heh, camera stuff for a real game can be a pain. Good luck.

~Aeroum
www.aeroum.com

Share this post


Link to post
Share on other sites
Cool, thanks for your reply. fFar and fNear is obvious that are they near and far clipping planes, no?

for Fov, i use d3dx_pi/4 yes, it is good. About the Up vector.. For making a 6DOF camera, should I change it or keep it always at vec3(0,1,0)? what would happen if i change the up vector and when do i need to change it?

Share this post


Link to post
Share on other sites
Well, actually I meant ''what were the near and far values you were using?''

If you want all the degrees of freedom. It is necessary to change the camera''s up vector. If you wanted everything on its side, like the direction ''right'' would be up, you would of course make it 1,0,0 and everything be rotated 90 degrees, or ''roll''ed.

Now if you keep the up vector at 0,1,0 always, you''ll have some problems even if you don''t want that freedom. Like if the At object is at 0,0,0 and the Eye, or camera is at 0, 10, 0. Then with the standard up vector all of the sudden, everything disappears, and that''s really bad, and sometimes hard to debug. But you could fix it by making the up vector 0,0,1. Then it could be looking down and it''d work correctly (with up being the along the z axis).

Besides those problems, until you''re really familiar with the camera operations or whatever, I''d recommend keeping the up vector 0,1,0. But in the future, you would want to change it if you want to look up or down, or roll (like tilt).

You may also want to create your own matrix instead of using D3DXMatrixLookAtLH() in the future. If you want to see how that works. The source for that function in the SDK is (for DirectX 8.0)
DirectX 8.0 - DirectX Graphics - Understanding DirectX Graphics - Direct3D Rendering Pipeline - Fixed Function Vertex and Pixel Processing - Transformation and Lighting Engine - The View Matrix - Setting Up A View Matrix

That shows how the matrix is actually built.

Aeroum

Share this post


Link to post
Share on other sites
Ohhhh, my near and far are 0.1f and 1000.0f respectively.

Is there a way to automaticcaly calculate the up vector from the view matrix? So I wont have to manually set it everytime I change the lookat?

The problem you mentioned about the 0,10,0 lookat point and everything disappears, I have noticed it, yes.

Share this post


Link to post
Share on other sites
In response to Aeorum. If you are at 0, 10, 0 and you object is at 0, 0, 0, and you have a first person camera (we''ll even assume without collision detection), then you are over the object, and if you have it capped, you probably don''t wish for it to move that far anyways. I always cap mine to one radian up or down, so I can''t see where with a normal first person style cam (unless you want a descent style camera or something), that that would even become a problem, as most people cap their rotations.

Share this post


Link to post
Share on other sites