Jump to content
  • Advertisement
Sign in to follow this  
bglanzer

DX11 Does this camera look correct? (SOLVED)

This topic is 2354 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

I've been putting this bug off for a while now but it is finally starting to get to me. For some reason my camera always targets the -z axis even if I call my SetViewLookAt method to set it to point down the +z axis. The rendering is all done in DX11 with an idendity world matrix. The model I test it with is placed at Vector zero and is not offset in anyway.

Below is my camera.cpp which I'm thinking where the bug is . Does anyone notice anything wrong with it?


/*
====================================================================
camera.cpp
Brendon S. Glanzer
01/03/2012
====================================================================
*/
#include"dirtyLib.h"
const TypeInfo Camera::TYPE( "Camera", &Object::TYPE );
const TypeInfo& Camera::GetType( void ) const { return TYPE; }
/*
===============================================================================
Camera::Camera() - Constructor
===============================================================================
*/
Camera::Camera( void ) : m_eye(), m_xAxis( Vector3d::RIGHT() ), m_yAxis( Vector3d::UP() ), m_zAxis( 0.0f, 0.0f, 1.0f ) ,
m_target( 0.0f, 0.0f, 0.0f ){
m_orientation = Quat::Identity();
m_viewMatrix = Matrix4::Identity();
m_projMatrix = Matrix4::Identity();
}
/*
===============================================================================
Camera::Camera() - Copy Constructor
===============================================================================
*/
Camera::Camera( const Camera& other )
: m_eye( other.m_eye ), m_xAxis( other.m_xAxis ), m_yAxis( other.m_yAxis ), m_zAxis( other.m_zAxis ), m_target( other.m_target ),
m_viewMatrix( other.m_viewMatrix ), m_projMatrix( other.m_projMatrix ), m_orientation() {
}
/*
===============================================================================
Camera::~Camera()
===============================================================================
*/
Camera::~Camera() {
}
/*
===============================================================================
Camera::SetViewLookAt() - Create view matrix
===============================================================================
*/
void Camera::SetViewLookAt( const Vector3d& position, const Vector3d& target, const Vector3d& up ) {
m_eye = position;
m_target = target;
m_zAxis = target - m_eye;
m_zAxis.Normalize();
m_xAxis = up.Cross( m_zAxis );
m_xAxis.Normalize();
m_yAxis = m_zAxis.Cross( m_xAxis );
m_viewMatrix[0][0] = m_xAxis.x;
m_viewMatrix[1][0] = m_xAxis.y;
m_viewMatrix[2][0] = m_xAxis.z;
m_viewMatrix[3][0] = -m_xAxis.Dot( m_eye );

m_viewMatrix[0][1] = m_yAxis.x;
m_viewMatrix[1][1] = m_yAxis.y;
m_viewMatrix[2][1] = m_yAxis.z;
m_viewMatrix[3][1] = -m_yAxis.Dot( m_eye );

m_viewMatrix[0][2] = m_zAxis.x;
m_viewMatrix[1][2] = m_zAxis.y;
m_viewMatrix[2][2] = m_zAxis.z;
m_viewMatrix[3][2] = -m_zAxis.Dot( m_eye );
m_viewMatrix[0][3] = 0;
m_viewMatrix[1][3] = 0;
m_viewMatrix[2][3] = 0;
m_viewMatrix[3][3] = 1.0f;
}
/*
===============================================================================
Camera::SetPerspectiveProjection() - Set Projection matrix
===============================================================================
*/
void Camera::SetPerspectiveProjection( const float fov, const float aspect, const float zNear, const float zFar ) {
MatrixPerspectiveFovLH( m_projMatrix, fov, aspect, zNear, zFar );
}
/*
===============================================================================
Camera::Move() - Move camera position
===============================================================================
*/
void Camera::Move( float x, float y, float z ) {
Vector3d forward;
forward = m_xAxis.Cross( m_yAxis );
m_eye += m_xAxis * x;
m_eye += m_yAxis * y;
m_eye += forward * z;
m_bUpdate = true;
}
/*
===============================================================================
Camera::Rotate() - Rotate Camera
===============================================================================
*/
void Camera::Rotate( float heading, float pitch, float roll ) {
Quat rotation;
//Add heading rotation
if( heading != 0.0f ) {
rotation.FromAxis( Vector3d( 0.0f, 1.0f, 0.0f ), heading );
m_orientation = m_orientation * rotation;
m_bUpdate = true;
}
//Add pitch rotation
if( pitch != 0.0f ) {
rotation.FromAxis( Vector3d( 1.0f, 0.0f, 0.0f ), pitch );
m_orientation = m_orientation * rotation;
m_bUpdate = true;
}
//Add roll rotation
if( roll != 0.0f ) {
rotation.FromAxis( Vector3d( 0.0f, 0.0f, 1.0f ), roll );
m_orientation = rotation * m_orientation;
m_bUpdate = true;
}
}
/*
===============================================================================
Camera::Update() - Update view matrix when needed
===============================================================================
*/
void Camera::Update() {
if( m_bUpdate ) {
//Create view matrix from orientation
m_orientation.Normalize();
m_viewMatrix = m_orientation.ToMatrix4();
m_xAxis = Vector3d( m_viewMatrix[0][0], m_viewMatrix[1][0], m_viewMatrix[2][0] );
m_yAxis = Vector3d( m_viewMatrix[0][1], m_viewMatrix[1][1], m_viewMatrix[2][1] );
m_zAxis = Vector3d( m_viewMatrix[0][2], m_viewMatrix[1][2], m_viewMatrix[2][2] );
m_viewMatrix[3][0] = -m_xAxis.Dot( m_eye );
m_viewMatrix[3][1] = -m_yAxis.Dot( m_eye );
m_viewMatrix[3][2] = -m_zAxis.Dot( m_eye );
m_bUpdate = false;
}
}


My dot and cross products work like such


/*
===============================================================================
Vector3d::Dot() - Returns dot product to other vector
===============================================================================
*/
inline float Vector3d::Dot( const Vector3d &a ) const {
return ( x * a.x + y * a.y + z * a.z );
}
/*
===============================================================================
Vector3d::Cross() const - Calculates cross product from passed vector3d
===============================================================================
*/
inline Vector3d Vector3d::Cross( const Vector3d &a ) const {
return Vector3d( y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x );
}

I'd be grateful of any help Edited by bglanzer

Share this post


Link to post
Share on other sites
Advertisement
Hello bglanzer

Could it be because the space (the clip space at least) is left-handed ? I mean (0,0,1) vector is going from the screen towards the back of the scene and not the opposite ?

Is there a reason why you don't use D3DX API ? Otherwise, 'I would suggest you' to use it instead, it would be much easier and probably safer (far

less, not to say 'no', risk to have bugs in their implementation)

Nico smile.png

Share this post


Link to post
Share on other sites
On this case though I'm setting the camera to face the orgin so it doesnt matter which direct +z is. I'm not using D3DX API so that my camera isn't tied to D3D. Thanks for the suggestion though.

Share this post


Link to post
Share on other sites
I found out what the problem is. My Update method is resetting the view matrix according to the Quaternions identity, which causes the camera to always look down the -z axis.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!