Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


CameraProblems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Wrath87   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 June 2012 - 12:11 PM

Greetings,

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;public: CameraManager(void); 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);};[/source]

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));}[/source]

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_Camera->BuildViewMatrix();

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.

Attached Thumbnails

  • err.png


Sponsor:

#2 MJP   Moderators   -  Reputation: 11584

Like
1Likes
Like

Posted 30 June 2012 - 12:44 PM

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:
  • 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, 30 June 2012 - 12:48 PM.


#3 Hornsj3   Members   -  Reputation: 191

Like
0Likes
Like

Posted 30 June 2012 - 01:15 PM

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.

#4 Wrath87   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 June 2012 - 02:04 PM

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), 0.01f, 100.0f)));//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...

@Hornsj3:
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, 30 June 2012 - 02:08 PM.


#5 Hornsj3   Members   -  Reputation: 191

Like
0Likes
Like

Posted 30 June 2012 - 02:23 PM

Another way to do that would be to use policy classes (see Andrei Alexandrescu's book modern C++ design http://www.amazon.co...dern c++ design)


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, 30 June 2012 - 02:23 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS