• Create Account

# Wrath87

Member Since 01 Mar 2011
Offline Last Active Feb 02 2013 10:36 AM

### Deriving the matrix form of the Rodrigues formula

02 February 2013 - 04:35 AM

Greetings,

I'm trying to derive the matrix form of Rodrigues rotation formula but I do something really bad.

R{N}(V) = cos(a)*V+(1-cos(a))*(N*V)*N+sin(a)*(NxV)

Where,

• N: Unit vector of the rotation angle.
• V: The vector to be rotated around N
• a: The angle of rotation (deg).
• (N*V): The dot product of N and V vectors.
• (NxV): The cross product of N and V vectors.
What I have found is that I have to apply the i(1,0,0) j(0,1,0) and k(0,0,1) to this formula to get the desired form. So I did chose to express the first row vector of the matrix form which should be:

R{1,*} = (cos(a)+(1-cos(a))*x2 , (1-cos(a))*x*y-sin(a)*z , (1-cos(a))*x*z-sin(a)*y)

Here's my best try so far:

Let:

n = i => n(1,0,0)

v(x,y,z)

N*V = 1*x+0*y+0*z = x

NxV = (0,-z,y)

|cos(a)*x+(1-cos(a))*x+0 | => cos(a)*x + (1-cos(a))*x ?= cos(a)+(1-cos(a))*x2

|cos(a)*y+(1-cos(a))*x-sin(a)*z | => how will cos(a) disappear?

| cos(a)*z+(1-cos(a))*x+sin(a)*y | => again no cos(a) in the final matrix :-/

I can't really understand the concept of the first element of this vector. How can I actually calculate cos(a)+(1-cos(a))*x2 from the actual cos(a)*x+(1-cos(a))*x? Everytime I stuck at:

cos(a)*x+(1-cos(a))*x+0 = cos(a)*x+(1-cos(a))*x = (cos(a) + (1 - cos(a)) * x

which does not look equals to me.

### Input handling system

04 July 2012 - 01:49 PM

Greetings,

I know there has been qute a few topics like this but none of them turned out to be useful for my case. I'm in the middle of the development of my simple engine but I'm struggling with creating a fast reliable input manager. To illustrate my case let me show you my choose of design.

Main Entry -> Instantiates a SystemClass that encapsulates the following factories:
• InputManager
• GraphicsManager - Handles Direct3D related code.
• CameraManager
• TextureManager
• LightManager
• MeshManager
• TimerManager
• Demo Projects - Derives from DirectXManager
To handle user input I'm using the WINAPI Message handler like:
[source lang="cpp"]LRESULT CALLBACK SystemClass::MsgHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ switch (uMsg) { case WM_KEYDOWN: case WM_KEYUP: case WM_LBUTTONDOWN: case WM_MBUTTONDOWN: case WM_RBUTTONDOWN: case WM_LBUTTONUP: case WM_MBUTTONUP: case WM_RBUTTONUP: case WM_MOUSEMOVE: m_GraphicsHandler->OnMouseMove(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));break;}[/source]
You can see the basic idea. The GraphicsHandler has the necessary functions which handles the user Events. My last attempt was something like this:

[source lang="cpp"]class InputFactory{ static InputFactory* Instance; InputFactory(void);public: static InputFactory* GetInstance(void); IHandler* CreateHandler(HandlerType hType = HandlerType::MouseHandler); ~InputFactory(void)};class IHandler{ friend class InputFactory;protected: struct Event { private: EventType eType; void (*Handler)(MappedKey mKey, State* hState); };private: std::queue<Event> Interactions;public: void RegisterEvent(EventType eType = EventType::MouseEvent, ...); void ProcessEvent(void);};[/source]
I think you get the main idea. My problem with this approach is that it's way too complex and hard to add new features. Not to mention that to modify the keybindings it's a nightmare. I found an article about inputmanager here on gamedev.net but I can't apply it to my case.

### Quaternion Camera rotation

02 July 2012 - 02:57 PM

Greetings,

My whole day ran off while I tried to implement a quaternion based camera system with the following requirements: With LeftmouseDown allow the camera to rotate around it's focus on a sphere trackball. I'm okay with the mathematics part, I have problems with the implementation. Currently I've achieved the following:

I have the necessary handling functions:
[source lang="cpp"]XMFLOAT2 Previous_MousePosition;void OnMouseDown(WPARAM KeyState, int x, int y){ if (KeyState & VM_LBUTTON) Previous_MousePosition = XMFLOAT2(x, y);}void OnMouseMove(WPARAM KeyState, int x, int y){ if(KeyState & VM_LBUTTON) { XMFLOAT2 NewPosition(x, y); XMFLOAT2 SphereLocation2D(x / (m_Width >> 1), y / (m_Height >> 1)); SphereLocation2D.x -= 1; SphereLocation2D.y = 1 - SphereLocation2D.y; double z = 1 - x * x - y * y; XMFLOAT3 SphereLocation3D(SphereLocation2D.x, SphereLocation2D.y, z > 0 ? z : 0); // no idea where to go from here... }}[/source]
And I have my camera currently defined like:
[source lang="java"]class Camera{ XMFLOAT3 m_Position; // If I'm not mistaken no need for rotation since my camera will always look at the same point. XMFLOAT4X4 m_ViewMatrix; XMFLOAT4X4 m_ProjectionMatrix;public: Camera(void) : m_Position(XMVectorZero()) { m_ViewMatrix = XMMatrixIdentity(); m_ProjectionMatrix = XMMatrixIdentity(); } ~Camera(void) { } void Update(XMFLOAT3 NewPosition, float Dt = 1.0f) { // Calc Axis between m_Position & NewPosition // FLOAT Angle(m_Position, NewPosition); // No idea how to continue from here // m_Position = NewPosition; } void BuildViewMatrix(void) { XMVECTOR Eye = XMVectorSet(m_Position.x, m_Position.y, m_Position.z, 0.0f); XMVECTOR At = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX ViewMatrix = XMMatrixLookAtLH(Eye, At, Up); XMStoreFloat4x4(&m_ViewMatrix, ViewMatrix); }} [/source]

So I really think I already messed up everything in here.

### CameraProblems

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.

### Update ConstantBuffers using Context::MAP

30 June 2012 - 09:22 AM

Greetings,

I've some problems updating my constant buffers. If I don't use cb in my Vertex Shader, then my Triangle is drawn else nothing is drawn on the screen. No errors but blank d3d window.

HLSL:
[source lang="cpp"]cbuffer PerFrame{ float4x4 WorldMatrix; float4x4 ViewMatrix; float4x4 ProjectionMatrix;};struct VS_OUT{ float4 Position : SV_POSITION; float4 Color : COLOR;};VS_OUT MainVS(float3 Position : POSITION, float4 Color : COLOR){ VS_OUT output = (VS_OUT)0; output.Position = mul(mul(mul(float4(Position, 1.0f), WorldMatrix), ViewMatrix), ProjectionMatrix); output.Color = Color; return output;}[/source]

As you can see its as simple as it can be in this case.

Cpp side cBuffer struct:
[source lang="cpp"] struct VS_CONSTANTS { private: XMFLOAT4X4 World; XMFLOAT4X4 View; XMFLOAT4X4 Projection; public: VS_CONSTANTS(void) { } VS_CONSTANTS(XMFLOAT4X4 _World, XMFLOAT4X4 _View, XMFLOAT4X4 _Projection) { World = _World; View = _View; Projection = _Projection; } }; VS_CONSTANTS m_cPerObject;[/source]

CONSTANT Buffer Creation:

[source lang="cpp"]D3D11_BUFFER_DESC Cb_DESCRIPTOR;ZeroMemory(&Cb_DESCRIPTOR, sizeof(D3D11_BUFFER_DESC));Cb_DESCRIPTOR.ByteWidth = sizeof(VS_CONSTANTS);Cb_DESCRIPTOR.BindFlags = D3D11_BIND_CONSTANT_BUFFER;Cb_DESCRIPTOR.Usage = D3D11_USAGE_DYNAMIC;Cb_DESCRIPTOR.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;HRESULT Result(E_FAIL);Result = m_Device->CreateBuffer(Buffer_DESCRIPTOR, InitialData, m_ConstantBuffer));if (FAILED(Result)) return Result;[/source]

And finally here's how I try to update the contents of the buffer:
[source lang="cpp"]template <class Type>HRESULT Demo::SetShaderResource(ID3D11Buffer** Buffer, const Type* Data){ D3D11_MAPPED_SUBRESOURCE BufferContents; assert(m_Context); HRESULT Result(E_FAIL); m_Context->Map(*Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &BufferContents); if (FAILED(Result)) return Result; BufferContents.pData = &m_cPerObject; m_Context->Unmap(*Buffer, 0); return S_OK;}[/source]

Data contains the following values:
[source lang="cpp"] m_SimpleCamera = new CameraManager; XMStoreFloat4x4(&m_WorldMatrix, XMMatrixIdentity()); XMStoreFloat4x4(&m_ViewMatrix, m_SimpleCamera->GetViewMatrix()); XMStoreFloat4x4(&m_ProjectionMatrix, XMMatrixIdentity()); m_cPerObject = VS_CONSTANTS(m_WorldMatrix, m_ViewMatrix, m_ProjectionMatrix);[/source]

Phew, that's quite a lot of code right here. I hope I gave every information needed. The method that should be used it this but I can't seem to find the answer what I did wrong, but I searched for nearly 3 hours now...

Any help would be appreciated!

PARTNERS