Skip to main content
gamedev.net
🔒 Locked

Camera Creation Question

Started by lucky6969b 5 replies 1.9k views
Original Post
lucky6969b
lucky6969b

When I grab the position of the camera and the target position of the camera, I can create a view matrix.

These figures are exported out along with the axe or kwxporter in an .x file.

But as I cannot export the aspect ratio and far clip distance. I have to make them up by myself.

Currently, I use an aspect ratio of 0.33 and far clip distance of 10000.0f

And I set the visible object at 0,0,0 (identity) and unity scale (didn't move it or scale it, just same to the original scene recorded in max)

and I see nothing in the viewport.


#include <string>
#include "..\tinyxml\tinyxml.h"
#include "CCamera.h"
#include "..\Utilities\Utilities.h"

CCamera::CCamera()
{
    m_vPos = D3DXVECTOR3(0.0f,0.0f,0.0f);    
    m_vLookAt = D3DXVECTOR3(0.0f,0.0f,0.0f);     
    D3DXMatrixIdentity(&m_matOrientation);
    m_bChangingView = false;  
}

void CCamera::Create(float fAspect, float fFar)
{
    D3DXMatrixPerspectiveFovLH(&m_matProj,
        D3DX_PI/4, fAspect, 1.0f, fFar );
}

void CCamera::Update(float dt, bool bUpdateFrustum)
{
    

    if(m_bChangingView)
    {
        m_vPos = m_vEndPos;
        m_vLookAt = m_vEndLookAt;
        m_bChangingView = false;
    }
    

    // Set the view matrix
    D3DXQUATERNION qR;
    D3DXQuaternionRotationYawPitchRoll(&qR, m_vLookAt.x, m_vLookAt.y, m_vLookAt.z);
    D3DXMatrixAffineTransformation(&m_matOrientation, 1.25f, NULL, &qR, &m_vPos );
    D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );    

  

    // Deceleating the yaw/pitch/roll velocities works great for keyboard control,
    // but it gives me motion sickness when used with mouselook, so I commented it out
    // and replaced it with multiplication by zero, which disables the velocities.  
    // Your mileage may vary.

    if(bUpdateFrustum)
        UpdateFrustum();
}

void CCamera::UpdateFrustum()
{
    D3DXMATRIX Matrix;
    D3DXMatrixMultiply(&Matrix, &m_matView, &m_matProj);

    // Calculate the planes
    m_FrustumPlanes[0].a = Matrix._14 + Matrix._13; // Near plane
    m_FrustumPlanes[0].b = Matrix._24 + Matrix._23;
    m_FrustumPlanes[0].c = Matrix._34 + Matrix._33;
    m_FrustumPlanes[0].d = Matrix._44 + Matrix._43;
    D3DXPlaneNormalize(&m_FrustumPlanes[0], &m_FrustumPlanes[0]);

    m_FrustumPlanes[1].a = Matrix._14 - Matrix._13; // Far plane
    m_FrustumPlanes[1].b = Matrix._24 - Matrix._23;
    m_FrustumPlanes[1].c = Matrix._34 - Matrix._33;
    m_FrustumPlanes[1].d = Matrix._44 - Matrix._43;
    D3DXPlaneNormalize(&m_FrustumPlanes[1], &m_FrustumPlanes[1]);

    m_FrustumPlanes[2].a = Matrix._14 + Matrix._11; // Left plane
    m_FrustumPlanes[2].b = Matrix._24 + Matrix._21;
    m_FrustumPlanes[2].c = Matrix._34 + Matrix._31;
    m_FrustumPlanes[2].d = Matrix._44 + Matrix._41;
    D3DXPlaneNormalize(&m_FrustumPlanes[2], &m_FrustumPlanes[2]);

    m_FrustumPlanes[3].a = Matrix._14 - Matrix._11; // Right plane
    m_FrustumPlanes[3].b = Matrix._24 - Matrix._21;
    m_FrustumPlanes[3].c = Matrix._34 - Matrix._31;
    m_FrustumPlanes[3].d = Matrix._44 - Matrix._41;
    D3DXPlaneNormalize(&m_FrustumPlanes[3], &m_FrustumPlanes[3]);

    m_FrustumPlanes[4].a = Matrix._14 - Matrix._12; // Top plane
    m_FrustumPlanes[4].b = Matrix._24 - Matrix._22;
    m_FrustumPlanes[4].c = Matrix._34 - Matrix._32;
    m_FrustumPlanes[4].d = Matrix._44 - Matrix._42;
    D3DXPlaneNormalize(&m_FrustumPlanes[4], &m_FrustumPlanes[4]);

    m_FrustumPlanes[5].a = Matrix._14 + Matrix._12; // Bottom plane
    m_FrustumPlanes[5].b = Matrix._24 + Matrix._22;
    m_FrustumPlanes[5].c = Matrix._34 + Matrix._32;
    m_FrustumPlanes[5].d = Matrix._44 + Matrix._42;
    D3DXPlaneNormalize(&m_FrustumPlanes[5], &m_FrustumPlanes[5]);
}

 
void CCamera::ChangeView(D3DXVECTOR3 *vPos, D3DXVECTOR3 *vAt)
{ 
    m_vEndPos            = *vPos;
    m_vEndLookAt        = *vAt;    
    m_bChangingView        = true;  
}

bool CCamera::CheckSphere(D3DXVECTOR3 *vCenter, float *fRadius)
{
  int i;

  for(i = 0; i < 6; i++)
    if(D3DXPlaneDotCoord(&m_FrustumPlanes[i], &D3DXVECTOR3(vCenter->x, vCenter->y, vCenter->z)) < -*fRadius)
      return false;  
  return true;
}

m_Cam[0].ChangeView(&m_Warehouse.GetPosition("Camera001"), &m_Warehouse.GetPosition("Camera001_Target"));
m_Cam[0].Create(0.33f, 10000.0f);
m_Cam[0].Update(0);

Any ideas I can fix that one up.

Thanks

Jack

Wh0p
Wh0p

D3DXMatrixPerspectiveFovLH(&m_matProj, D3DX_PI/4, fAspect, 1.0f, fFar );

Looks like your near clipping plane is 1.0f, obviously you wouldn't see any object that is closer to the camera than this value.

Stainless
Stainless

Stick a breakpoint on ChangeView and sanity check the values you have for camera position and target.

Could be you have a camera inside the object, or the camera is in the correct place but pointing away from the object.

Hell the camera could be called FRED in which case looking for it as Camera0001 will return nothing.

When you start moving meshes from one tool to another you often get issues with things not getting exported or getting exported, but not in the way that you expected.

lucky6969b
lucky6969b

D3DXVec3Cross(&m_vUp, &m_vLookAt, &m_vPos);
D3DXMatrixLookAtLH(&m_matView, &m_vPos, &m_vLookAt, &m_vUp);

Seems this is heaps easier, but the orientation is bit off.

Don't know why, have been examining it....

Update:

I was silly, why I would cross the position with lookat, I should have crossed the lookat with the right vector, right? smile.png

How do I get the up or right vector if I possess the position and lookat vectors to convert to one another?

I also wondering how I can retrieve the orientation of the camera if there is already a "matrix" inside the Camera node of the x file, like this


Frame Camera001 {
 

 FrameTransformMatrix relative {
  -0.694684,0.000000,-0.719315,0.000000,-0.371042,0.856693,0.358337,0.000000,0.616232,0.515827,-0.595131,0.000000,-155.795725,335.976909,141.821658,1.000000;;
 }

 ObjectMatrixComment object {
  1.000000,-0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,-0.000015,0.000000,0.000008,1.000000;;
 }
}

Update2:

Wow, the view matrix looks like the inverse of this matrix?!

Another try:

http://stackoverflow.com/questions/6030933/get-eye-target-and-up-vectors-from-view-matrix

The thing is how do I work out the Camera matrix from the x file to calculate the view matrix and even the lookat, up and right from the data above?

Thanks

Jack

Burnt_Fyr
Burnt_Fyr

RE your update:

you usually need a temp vector, such as one of the primary axiis to do this. so say you are x+ right, y+ up, z+ forward. take your lookatpoint and subtract position. cross(world.up, forward)with WORLD up(not local) to get right, then cross(forward, right) to get a true up vector.

as you already discovered, Mview = Mcamera-1

lucky6969b
lucky6969b

Yes, I've fixed that, That's the world up I need to process, I succeeded in producing the correct max camera in Direct3D

But I don't know why this code snippet won't work (The camera should be on the right side, it is on the left side now)

The lookat vector is the same as the one I calculated previously

and the position is the same


D3DXQUATERNION qR;
D3DXQuaternionRotationYawPitchRoll(&qR, m_vLook.x, m_vLook.y, m_vLook.z);
D3DXMatrixAffineTransformation(&m_matOrientation, 1.25f, NULL, &qR, &m_vPos );    
D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );   

Here, I have created a Camera Matrix and inverse it to produce the view matrix.,,,

Thanks
Jack

Burnt_Fyr
Burnt_Fyr

Yes, I've fixed that, That's the world up I need to process, I succeeded in producing the correct max camera in Direct3D

But I don't know why this code snippet won't work (The camera should be on the right side, it is on the left side now)

The lookat vector is the same as the one I calculated previously

and the position is the same


D3DXQUATERNION qR;
D3DXQuaternionRotationYawPitchRoll(&qR, m_vLook.x, m_vLook.y, m_vLook.z);
D3DXMatrixAffineTransformation(&m_matOrientation, 1.25f, NULL, &qR, &m_vPos );    
D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );   

Here, I have created a Camera Matrix and inverse it to produce the view matrix.,,,

Thanks
Jack

I don't understand what you mean by the 'side' the camera should be on. are you using a left handed or right handed basis?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.