add cube mesh
add world matrix add view matrix add projection matrix
This commit is contained in:
+23
-1
@@ -1,12 +1,34 @@
|
||||
#include "Camera.h"
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
UINT Camera::init(UINT width, UINT height)
|
||||
{
|
||||
// view matrix
|
||||
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); // camera position
|
||||
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // camera forward vector
|
||||
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // camera up vector
|
||||
|
||||
XMMATRIX viewMatrix = XMMatrixLookToLH(position, forward, up);
|
||||
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_viewMatrix), viewMatrix);
|
||||
|
||||
// projection matrix
|
||||
FLOAT fov = XM_PI * 0.3333333f; // field of view angle in radians
|
||||
FLOAT aspectRatio = static_cast<FLOAT>(width) / static_cast<FLOAT>(height); // CAUTION: UINT / UINT => UINT
|
||||
FLOAT nearClippingPlane = 0.3f;
|
||||
FLOAT farClippingPlane = 20.0f;
|
||||
|
||||
XMMATRIX projectionMatrix = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
|
||||
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_projectionMatrix), projectionMatrix);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Camera::update(FLOAT dt)
|
||||
void Camera::update(FLOAT dt, IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
|
||||
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
|
||||
}
|
||||
|
||||
void Camera::deInit()
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ class Camera
|
||||
{
|
||||
public:
|
||||
UINT init(UINT width, UINT height);
|
||||
void update(FLOAT dt);
|
||||
void update(FLOAT dt, IDirect3DDevice9* pD3DDevice);
|
||||
void deInit();
|
||||
|
||||
private:
|
||||
|
||||
+76
-11
@@ -2,6 +2,10 @@
|
||||
#include "Utilities.h"
|
||||
#include "Vertex.h"
|
||||
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
UINT error = 0;
|
||||
@@ -12,11 +16,31 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||
error = createIndexBuffer(pD3DDevice);
|
||||
CheckError(error);
|
||||
|
||||
// identity world matrix
|
||||
XMMATRIX identityMatrix = XMMatrixIdentity();
|
||||
XMFLOAT4X4 worldMatrix;
|
||||
XMStoreFloat4x4(&worldMatrix, identityMatrix);
|
||||
D3DMATRIX* pWorldMatrix = reinterpret_cast<D3DMATRIX*>(&worldMatrix);
|
||||
_worldMatrix = *pWorldMatrix;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Mesh::update(FLOAT dt)
|
||||
{
|
||||
static FLOAT pitch = XM_PIDIV4; // x axis
|
||||
static FLOAT yaw = XM_PIDIV4; // y axis
|
||||
static FLOAT roll = XM_PIDIV4; // z axis
|
||||
static FLOAT scale = 1.0f;
|
||||
|
||||
yaw += 0.01f;
|
||||
//scale += 0.001f;
|
||||
|
||||
XMMATRIX translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.0f);
|
||||
XMMATRIX scaleMatrix = XMMatrixScaling(scale, scale, scale);
|
||||
XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
|
||||
|
||||
XMStoreFloat4x4(reinterpret_cast<XMFLOAT4X4*>(&_worldMatrix), scaleMatrix * rotationMatrix * translationMatrix);
|
||||
}
|
||||
|
||||
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
||||
@@ -26,7 +50,10 @@ void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
||||
pD3DDevice->SetFVF(FVF);
|
||||
pD3DDevice->SetIndices(_pIndexBuffer);
|
||||
|
||||
// 2. draw call
|
||||
// 2. set transform data
|
||||
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
|
||||
|
||||
// 3. draw call
|
||||
// vertex buffer only
|
||||
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, _vertexCount / 3);
|
||||
//pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, _vertexCount - 2);
|
||||
@@ -45,7 +72,7 @@ void Mesh::deInit()
|
||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
// 1. define mesh parameters
|
||||
_vertexCount = 4;
|
||||
_vertexCount = 8;
|
||||
_vertexStride = sizeof(Vertex);
|
||||
|
||||
// 2. create vertex buffer object WITHOUT data
|
||||
@@ -87,10 +114,23 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
|
||||
// quad with triangle fan, vertex buffer only
|
||||
// quad with triangle list & index buffer
|
||||
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
|
||||
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
|
||||
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f);
|
||||
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||
//pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f);
|
||||
//pVertices[1] = Vertex(0.5f, 0.5f, 0.0f);
|
||||
//pVertices[2] = Vertex(0.5f, -0.5f, 0.0f);
|
||||
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||
|
||||
// cube
|
||||
// front
|
||||
pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f);
|
||||
pVertices[1] = Vertex(0.5f, 0.5f, -0.5f);
|
||||
pVertices[2] = Vertex(0.5f, -0.5f, -0.5f);
|
||||
pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f);
|
||||
|
||||
// front
|
||||
pVertices[4] = Vertex(0.5f, 0.5f, 0.5f);
|
||||
pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f);
|
||||
pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f);
|
||||
pVertices[7] = Vertex(0.5f, -0.5f, 0.5f);
|
||||
|
||||
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
|
||||
CheckFailed(hr, 320);
|
||||
@@ -102,7 +142,7 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
|
||||
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
{
|
||||
_indexCount = 6; // triangle count * 3
|
||||
_indexCount = 36; // triangle count * 3
|
||||
|
||||
HRESULT hr = pD3DDevice->CreateIndexBuffer(
|
||||
_indexCount * sizeof(WORD),
|
||||
@@ -119,13 +159,38 @@ UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
|
||||
CheckFailed(hr, 340);
|
||||
|
||||
// quad
|
||||
// triangle 1 - 0, 1, 2
|
||||
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||
//// quad
|
||||
//// triangle 1 - 0, 1, 2
|
||||
//pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||
|
||||
// triangle 2 - 0, 2, 3
|
||||
//// triangle 2 - 0, 2, 3
|
||||
//pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
||||
|
||||
// cube
|
||||
// front
|
||||
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
||||
|
||||
// back
|
||||
pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6;
|
||||
pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7;
|
||||
|
||||
// left
|
||||
pIndices[12] = 5; pIndices[13] = 0; pIndices[14] = 3;
|
||||
pIndices[15] = 5; pIndices[16] = 3; pIndices[17] = 6;
|
||||
|
||||
// right
|
||||
pIndices[18] = 1; pIndices[19] = 4; pIndices[20] = 7;
|
||||
pIndices[21] = 1; pIndices[22] = 7; pIndices[23] = 2;
|
||||
|
||||
// top
|
||||
pIndices[24] = 5; pIndices[25] = 4; pIndices[26] = 1;
|
||||
pIndices[27] = 5; pIndices[28] = 1; pIndices[29] = 0;
|
||||
|
||||
// bottom
|
||||
pIndices[30] = 3; pIndices[31] = 2; pIndices[32] = 7;
|
||||
pIndices[33] = 3; pIndices[34] = 7; pIndices[35] = 6;
|
||||
|
||||
hr = _pIndexBuffer->Unlock();
|
||||
CheckFailed(hr, 350);
|
||||
|
||||
|
||||
+5
-2
@@ -20,13 +20,16 @@ INT Window::init(HINSTANCE hInstance, UINT width, UINT height)
|
||||
style = WS_OVERLAPPEDWINDOW; // window with border & title
|
||||
//style = WS_POPUP; // borderless window
|
||||
|
||||
RECT rect = { 100, 100, width + 100, height + 100 }; // left, top, right, bottom
|
||||
AdjustWindowRect(&rect, style, FALSE);
|
||||
|
||||
// 4. create window instance
|
||||
_hWnd = CreateWindow(
|
||||
wc.lpszClassName, // class name for window instance
|
||||
wc.lpszClassName, // window title
|
||||
style, // visual window style
|
||||
100, 100, // window position (left-top corner)
|
||||
width, height, // window size
|
||||
rect.left, rect.top, // window position (left-top corner)
|
||||
rect.right - rect.left, rect.bottom - rect.top, // window size
|
||||
nullptr, // handle to parent window
|
||||
nullptr, // handle to menu instance
|
||||
hInstance,
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ int WINAPI WinMain(
|
||||
// 8.1. update objects
|
||||
time.update();
|
||||
mesh.update(time.getDeltaTime());
|
||||
camera.update(time.getDeltaTime());
|
||||
camera.update(time.getDeltaTime(), d3d.getDevice());
|
||||
light.update(time.getDeltaTime());
|
||||
|
||||
// 8.2. render objects
|
||||
|
||||
Reference in New Issue
Block a user