Sign in to follow this  


Recommended Posts

jozsef.horvath    110

So right one problem after another. After successfully been able to update the contents of the cBuffer, I found that my simple Camera does not work correctly.
[source lang="cpp"]#pragma once

#include <Windows.h>
#include <xnamath.h>

class CameraManager
XMFLOAT3 m_Position;
XMFLOAT3 m_Rotation;
XMFLOAT4X4 m_ViewMatrix;
virtual ~CameraManager(void);

void SetPosition(float, float, float);
void SetRotation(float, float, float);

XMVECTOR GetPosition(void) const;
XMVECTOR GetRotation(void) const;
XMMATRIX GetViewMatrix(void) const;

void BuildViewMatrix(void);

I'm using this simple class to render the scene from camera perspective but well this isn't going well... The BuildViewMatrix function looks like this:
[source lang="cpp"]void CameraManager::BuildViewMatrix(void)
// *******************
// World Up Direction.
// *******************
XMFLOAT3 Up(0.0f, 1.0f, 0.0f);

// ************************
// The Observer's position.
// ************************
XMFLOAT3 Eye(m_Position);

// *************************
// The LookAt (Focus) point.
// *************************
XMFLOAT3 LookAt(0.0f, 0.0f, 1.0f);

// ************************
// Set the Camera Rotation.
// ************************
XMFLOAT3 Rotation_VEC(m_Rotation);
float Pitch = Rotation_VEC.x * 0.0174532925f;
float Yaw = Rotation_VEC.y * 0.0174532925f;
float Roll = Rotation_VEC.z * 0.0174532925f;

// ****************************************
// Build Rotation matrix upon the rotation.
// ****************************************
XMMATRIX Rotation = XMMatrixIdentity();
Rotation = XMMatrixRotationRollPitchYaw(Pitch, Yaw, Roll);

XMVECTOR Look = XMVector3TransformCoord(XMLoadFloat3(&LookAt), Rotation);
XMVECTOR Up_D = XMVector3TransformCoord(XMLoadFloat3(&Up), Rotation);

Look = XMLoadFloat3(&Eye) + Look;
XMStoreFloat4x4(&m_ViewMatrix, XMMatrixLookAtLH(XMLoadFloat3(&Eye), Look, Up_D));

And well my problem is simple. Whenever I set the CameraPosition on other than the Depth(Z) axis, the scene gets demorfed. Like basically:
m_Camera->SetPosition(3.0f, 2.0f, -5.0f);

m_ViewMatrix = m_Camera->GetViewMatrix();
// Update the cb.
// HLSL:
output.Position = mul(mul(mul(float4(input.Position, 1.0f), WorldMatrix),ViewMatrix), ProjectionMatrix);

The camera seems like is nowhere near the -15.0f coordinate. The Triangle goes out of the screen like -I'm close to it. The screen talks for screenshot talks for itself. Honestly I think I've done everything correctly when building the camera's ViewMatrix but I may be nissing something.

Share this post

Link to post
Share on other sites
MJP    19786
Making a view matrix is simple: it's just the inverse of a matrix representing the camera's orientation and position. If you're already going through the trouble of creating a rotation matrix for the camera then there's no point in using that to transform lookAt or Up directions. Just do this:[list=1]
[*]Create the rotation matrix from your yaw/pitch/roll values
[*]Set the camera's XYZ position to the first 3 components of the 4th row of the matrix:
Rotation.r[3] = XMVectorSet(m_Position.x, m_Position.y, m_Position.z, 1.0f);
Alternatively, you can create a translation matrix with translation == m_Position and then transform your rotation matrix by that
[*]Invert the matrix to get your view matrix

Also you need to be careful about row-major vs. column-major layout of matrices in a constant buffer. XNAMath will create row-major matrices, but by default shaders will expect column-major matrices in constant buffers. This means that you either need to transpose your matrices when setting them into your constant buffer, or you need to compile your shader with the D3D10_SHADER_PACK_MATRIX_ROW_MAJOR flag. Or, you can declare your matrices with the "row_major" modifier in your HLSL constant buffer definition. Edited by MJP

Share this post

Link to post
Share on other sites
Hornsj3    195
Another thing to note. The above camera is transformed with respect to the world coordinate system. It will work great for a 1st person shooter, or 3rd person camera like you would see in an MMO.

If you want to create a fly-by camera, like one which would be in a flight simulator, you will have to transform the camera with respect to its own coordinate system. For that, I recommend a quaternion-based solution.

Share this post

Link to post
Share on other sites
jozsef.horvath    110
thanks a lot. you helped me implement my view matrix creation a lot more efficient. My problem however was the missing of projection matrix. So the Correct Projection matrix would be:

[source lang="cpp"]XMStoreFloat4x4(&m_ProjectionMatrix, XMMatrixTranspose(XMMatrixPerspectiveFovLH(XM_PIDIV2,
static_cast<float>(ClientWidth) / static_cast<float>(ClientHeight),

//instead of:
XMStoreFloat4x4(&m_ProjectionMatrix, XMMatrixIdentity());[/source]

I should just get some sleep maybe because I'm going nuts of silly things that I don't notice...

Thanks for your recommendation. I'm aware of these, and actually CameraClass will serve as an abstract Camera for the other specific cameras I'm going to implement in the near future. Actually the next would be the Quaternion based camera for a free-look system controlled with mouse from the inherited:

[source lang="cpp"]void MouseDown(WPARAM, int, int) = 0;
void MouseUp(WPARAM, int. int) = 0;
void MouseMove(WPARAM, int, int) = 0;[/source]
functions. I'm not sure if this is the best way to go but for the first look it seems promising. Edited by Wrath87

Share this post

Link to post
Share on other sites
Hornsj3    195
Another way to do that would be to use policy classes (see Andrei Alexandrescu's book modern C++ design [url=""] c++ design[/url])

In that way you could create a transformation policy to handle the specifics, so you don't have to deal with run-time polymorphism or inheritance hierarchies at all.

Just an idea. In fact now that I mention it I think I'm going to take a stab at it. Edited by Hornsj3

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this