From 3ee96cf6ec6b193470c0e74277a0f091503fc1d7 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Tue, 26 Mar 2024 11:31:04 +0100 Subject: [PATCH] Scaling + Rotation + Translation --- enginecustom/applicationclass.cpp | 52 ++++++++++++++++++++++++++----- enginecustom/applicationclass.h | 2 +- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index d4029d3..e5e17bb 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -51,8 +51,8 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) } // Set the initial position of the camera. - m_Camera->SetPosition(0.0f, 0.0f, -5.0f); - m_Camera->SetRotation(0.0f, 0.0f, 10.0f); + m_Camera->SetPosition(0.0f, 0.0f, -10.0f); + m_Camera->SetRotation(0.0f, 0.0f, 0.0f); // Set the file name of the model. strcpy_s(modelFilename, "sphere.obj"); @@ -138,6 +138,9 @@ void ApplicationClass::Shutdown() bool ApplicationClass::Frame() { static float rotation = 0.0f; + static float x = 2.f; + static float y = 0.f; + static float z = 0.f; bool result; @@ -148,8 +151,17 @@ bool ApplicationClass::Frame() rotation += 360.0f; } + // Update the x position variable each frame. + x -= 0.0174532925f * 0.54672f; + + y -= 0.0174532925f * 0.8972f; + + // Update the z position variable each frame. + z -= 0.0174532925f * 0.8972f; + + // Render the graphics scene. - result = Render(rotation); + result = Render(rotation, x, y, z); if (!result) { return false; @@ -159,9 +171,9 @@ bool ApplicationClass::Frame() } -bool ApplicationClass::Render(float rotation) +bool ApplicationClass::Render(float rotation, float x, float y, float z) { - XMMATRIX worldMatrix, viewMatrix, projectionMatrix; + XMMATRIX worldMatrix, viewMatrix, projectionMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix; bool result; @@ -175,8 +187,14 @@ bool ApplicationClass::Render(float rotation) m_Direct3D->GetWorldMatrix(worldMatrix); m_Camera->GetViewMatrix(viewMatrix); m_Direct3D->GetProjectionMatrix(projectionMatrix); - // Rotate the world matrix by the rotation value so that the triangle will spin. - worldMatrix = XMMatrixRotationY(rotation); + + scaleMatrix = XMMatrixScaling(0.5f, 0.5f, 0.5f); // Build the scaling matrix. + rotateMatrix = XMMatrixRotationY(rotation); // Build the rotation matrix. + translateMatrix = XMMatrixTranslation(x, y, z); // Build the translation matrix. + + // Multiply the scale, rotation, and translation matrices together to create the final world transformation matrix. + srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. m_Model->Render(m_Direct3D->GetDeviceContext()); @@ -188,6 +206,26 @@ bool ApplicationClass::Render(float rotation) { return false; } + + scaleMatrix = XMMatrixScaling(2.0f, 2.0f, 2.0f); // Build the scaling matrix. + rotateMatrix = XMMatrixRotationY(-rotation); // Build the rotation matrix. + translateMatrix = XMMatrixTranslation(-x, -y, -z); // Build the translation matrix. + + // Multiply the scale, rotation, and translation matrices together to create the final world transformation matrix. + srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); + worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); + + // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + // Render the model using the light shader. + result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(), + m_Light->GetDirection(), m_Light->GetDiffuseColor()); + if (!result) + { + return false; + } + // Present the rendered scene to the screen. m_Direct3D->EndScene(); diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index dbd6973..f954b28 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -35,7 +35,7 @@ public: bool Frame(); private: - bool Render(float); + bool Render(float, float, float, float); private: D3DClass* m_Direct3D;