# jozsef.horvath

Member

15

110 Neutral

• Rank
Member
1. ## Deriving the matrix form of the Rodrigues formula

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 Na: 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.
2. ## Input handling system

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:[list] [*]InputManager [*]GraphicsManager - Handles Direct3D related code. [list] [*]CameraManager [*]TextureManager [*]EffectManader [*]LightManager [*]MeshManager [/list][*]TimerManager [*]Demo Projects - Derives from DirectXManager [/list] 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.
3. ## Quaternion Camera rotation

Actually you helped me quite a lot. Now the only one question that remains and that none of the sites I've read explains is the Orientation of the Camera. What exactly is the camera orientation? O.o
4. ## Quaternion Camera rotation

Okay, so I've cleared everything and a fresh start. So the main idea if I get right would be this: (In a very simple case) We have a position for the camera (Eye) We have a focus point where the camera looks (LookAt) And finally we have the World's Up vector (Up XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f)) From this we create the View Matrix which is a 4x4 matrix: XMStoreFloat4x4(&m_ViewMatrix, XMMatrixLookAtLH(m_Position, m_Target, XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f)); Now to have a quaternion based camera we need the actual direction (orientation) of the camera. m_Direction = XMQuaternionNormalize(XMQuaternionRotationMatrix(XMLoadFloat4x4(&m_ViewMatrix))); Aaand here I get lost. As I've watched over tutorials I found that to rebuild the viewMatrix they to a MatrixRotationQuaternion like: XMStoreFloat4x4(&m_ViewMatrix, XMMatrixRotationQuaternion(m_Direction)); Zero movement or rotation for the camera yet, but if I do this nothing is drawn on my screen. On a second note to actually move or rotate the camera I thought something like this: void UpdatePosition(const XMVECTOR Direction, float dt = 1.0f); -> Update the position (Eye) of the camera based on deltaTime. void Move(float dx, float dy, float dz); Calculate the direction XMVectorSet(dx, dy, dz, 0.0f) - m_Position. Call UpdatePosition. (I may be wrong tho) void Rotate(float Pitch, float Yaw, float Roll); -y No idea yet but I'll eventually find the answer
5. ## Quaternion Camera rotation

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.
6. ## CameraProblems

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... @[url="http://www.gamedev.net/user/196305-hornsj3/"][color="#284b72"]Hornsj3[/color][/url]: 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.
7. ## CameraProblems

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.
8. ## Update ConstantBuffers using Context::MAP

Awesome! I knew it'll be something like this... Thanks a lot mate, you saved my day! ;)
9. ## Update ConstantBuffers using Context::MAP

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!
10. ## CreateDepthStencilView() return E_INVALIDARG

Greetings, Actually I have a double problem with the DepthStencilView creation. On one side when I try to create the depth stencil view with a given D3D11_DEPTH_STENCIL_VIEW_DESC structure I get an "An invalid argument was passed to the returning function." error mesage; and on the other side if I set this parameter to 0, nothing is drawn onto my screen. Here's the correspoding code snipet: [source lang="cpp"]D3D11_TEXTURE2D_DESC depthStencil_DESCRIPTOR; ZeroMemory(&depthStencil_DESCRIPTOR, sizeof(D3D11_TEXTURE2D_DESC)); depthStencil_DESCRIPTOR.Width = ClientWidth; depthStencil_DESCRIPTOR.Height = ClientHeight; depthStencil_DESCRIPTOR.MipLevels = 1; depthStencil_DESCRIPTOR.ArraySize = 1; depthStencil_DESCRIPTOR.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthStencil_DESCRIPTOR.SampleDesc.Count = 4; depthStencil_DESCRIPTOR.SampleDesc.Quality = m4xMsaaQuality - 1; depthStencil_DESCRIPTOR.Usage = D3D11_USAGE_DEFAULT; depthStencil_DESCRIPTOR.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthStencil_DESCRIPTOR.CPUAccessFlags = 0; depthStencil_DESCRIPTOR.MiscFlags = 0; ID3D11Texture2D* depthStencilBuffer; result = m_Device->CreateTexture2D(&depthStencil_DESCRIPTOR, 0, &depthStencilBuffer); if (FAILED(result)) return false; D3D11_DEPTH_STENCIL_DESC DepthStencilState_DESCRIPTOR; ZeroMemory(&DepthStencilState_DESCRIPTOR, sizeof(D3D11_DEPTH_STENCIL_DESC)); DepthStencilState_DESCRIPTOR.DepthEnable = true; DepthStencilState_DESCRIPTOR.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; DepthStencilState_DESCRIPTOR.DepthFunc = D3D11_COMPARISON_LESS; DepthStencilState_DESCRIPTOR.StencilEnable = true; DepthStencilState_DESCRIPTOR.StencilReadMask = 0xFF; DepthStencilState_DESCRIPTOR.StencilWriteMask = 0xFF; // Stencil Operations on Front facing pixels DepthStencilState_DESCRIPTOR.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; DepthStencilState_DESCRIPTOR.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; DepthStencilState_DESCRIPTOR.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; DepthStencilState_DESCRIPTOR.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Stencil operations on Back facint pixels DepthStencilState_DESCRIPTOR.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; DepthStencilState_DESCRIPTOR.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; DepthStencilState_DESCRIPTOR.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; DepthStencilState_DESCRIPTOR.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; result = m_Device->CreateDepthStencilState(&DepthStencilState_DESCRIPTOR, &m_DepthStencilState); if (FAILED(result)) return false; m_Context->OMSetDepthStencilState(m_DepthStencilState, 1); D3D11_DEPTH_STENCIL_VIEW_DESC DepthStencilView_DESCRIPTOR; ZeroMemory(&DepthStencilView_DESCRIPTOR, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC)); DepthStencilView_DESCRIPTOR.Format = depthStencil_DESCRIPTOR.Format; DepthStencilView_DESCRIPTOR.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; DepthStencilView_DESCRIPTOR.Texture2D.MipSlice = 0; result = m_Device->CreateDepthStencilView(depthStencilBuffer, &DepthStencilView_DESCRIPTOR, &m_DepthStencilView); if (FAILED(result)) return false;[/source]
11. ## Unable to draw triangle...

Ah thanks, but changing those to positive 0.25 doesn't change anything. Only the blue d3d client area is visible but the triangle is nowhere.