Add transformation
Add input Add time Add vertex colors
This commit is contained in:
+28
-1
@@ -1,7 +1,34 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
UINT Camera::init(UINT width, UINT height)
|
using namespace DirectX;
|
||||||
|
|
||||||
|
UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
|
||||||
{
|
{
|
||||||
|
// view matrix
|
||||||
|
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f);
|
||||||
|
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
|
||||||
|
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
XMMATRIX lookTo = XMMatrixLookToLH(position, forward, up);
|
||||||
|
XMFLOAT4X4 v;
|
||||||
|
XMStoreFloat4x4(&v, lookTo);
|
||||||
|
_viewMatrix = *reinterpret_cast<D3DMATRIX*>(&v);
|
||||||
|
|
||||||
|
// projection matrix
|
||||||
|
float fov = XMConvertToRadians(60.0f);
|
||||||
|
float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
|
||||||
|
float nearClippingPlane = 0.3f;
|
||||||
|
float farClippingPlane = 100.0f;
|
||||||
|
|
||||||
|
XMMATRIX perspective = XMMatrixPerspectiveFovLH(fov, aspectRatio, nearClippingPlane, farClippingPlane);
|
||||||
|
XMFLOAT4X4 p;
|
||||||
|
XMStoreFloat4x4(&p, perspective);
|
||||||
|
_projectionMatrix = *reinterpret_cast<D3DMATRIX*>(&p);
|
||||||
|
|
||||||
|
pD3DDevice->SetTransform(D3DTS_VIEW, &_viewMatrix);
|
||||||
|
pD3DDevice->SetTransform(D3DTS_PROJECTION, &_projectionMatrix);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init(UINT width, UINT height);
|
UINT init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height);
|
||||||
void update(FLOAT dt);
|
void update(FLOAT dt);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
|||||||
+44
-4
@@ -2,6 +2,10 @@
|
|||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
|
|
||||||
|
#include <DirectXMath.h>
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
UINT error = 0;
|
UINT error = 0;
|
||||||
@@ -12,11 +16,40 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
|||||||
error = createIndexBuffer(pD3DDevice);
|
error = createIndexBuffer(pD3DDevice);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
|
XMMATRIX identity = XMMatrixIdentity();
|
||||||
|
XMFLOAT4X4 w;
|
||||||
|
XMStoreFloat4x4(&w, identity);
|
||||||
|
_worldMatrix = *reinterpret_cast<D3DMATRIX*>(&w);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::update(FLOAT dt)
|
void Mesh::update(FLOAT dt)
|
||||||
{
|
{
|
||||||
|
static float x = 0.0f;
|
||||||
|
static float y = 0.0f;
|
||||||
|
static float z = 0.0f;
|
||||||
|
static float rotZ = 0.0f;
|
||||||
|
|
||||||
|
rotZ += 0.0f * dt;
|
||||||
|
|
||||||
|
float move = 1.0f * dt;
|
||||||
|
|
||||||
|
if (GetAsyncKeyState(VK_LEFT) & 0x8000) x -= move;
|
||||||
|
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) x += move;
|
||||||
|
if (GetAsyncKeyState(VK_DOWN) & 0x8000) y -= move;
|
||||||
|
if (GetAsyncKeyState(VK_UP) & 0x8000) y += move;
|
||||||
|
if (GetAsyncKeyState(VK_SUBTRACT) & 0x8000) z -= move;
|
||||||
|
if (GetAsyncKeyState(VK_ADD) & 0x8000) z += move;
|
||||||
|
|
||||||
|
XMMATRIX translation = XMMatrixTranslation(x, y, z);
|
||||||
|
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ);
|
||||||
|
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
|
XMFLOAT4X4 w;
|
||||||
|
XMStoreFloat4x4(&w, scale * rotation * translation);
|
||||||
|
|
||||||
|
_worldMatrix = *reinterpret_cast<D3DMATRIX*>(&w);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
||||||
@@ -25,6 +58,7 @@ void Mesh::render(IDirect3DDevice9* pD3DDevice)
|
|||||||
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
pD3DDevice->SetStreamSource(0, _pVertexBuffer, 0, _vertexStride);
|
||||||
pD3DDevice->SetIndices(_pIndexBuffer);
|
pD3DDevice->SetIndices(_pIndexBuffer);
|
||||||
pD3DDevice->SetFVF(FVF);
|
pD3DDevice->SetFVF(FVF);
|
||||||
|
pD3DDevice->SetTransform(D3DTS_WORLD, &_worldMatrix);
|
||||||
|
|
||||||
// draw call
|
// draw call
|
||||||
pD3DDevice->DrawIndexedPrimitive(
|
pD3DDevice->DrawIndexedPrimitive(
|
||||||
@@ -74,10 +108,16 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
//pVertices[2] = Vertex(-1.0f, -1.0f, 0.0f);
|
//pVertices[2] = Vertex(-1.0f, -1.0f, 0.0f);
|
||||||
|
|
||||||
// quad
|
// quad
|
||||||
pVertices[0] = 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[1] = Vertex(0.5f, 0.5f, 0.0f);
|
||||||
pVertices[2] = 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[3] = Vertex(-0.5f, -0.5f, 0.0f);
|
||||||
|
|
||||||
|
// quad with colors
|
||||||
|
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0);
|
||||||
|
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0);
|
||||||
|
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0, 0, 255);
|
||||||
|
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0);
|
||||||
|
|
||||||
hr = _pVertexBuffer->Unlock();
|
hr = _pVertexBuffer->Unlock();
|
||||||
CheckFailed(hr, 320);
|
CheckFailed(hr, 320);
|
||||||
|
|||||||
@@ -1,12 +1,26 @@
|
|||||||
#include "Time.h"
|
#include "Time.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
UINT Time::init()
|
UINT Time::init()
|
||||||
{
|
{
|
||||||
|
_lastFrame = CLOCK::now();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Time::update()
|
void Time::update()
|
||||||
{
|
{
|
||||||
|
TIMEPOINT now = CLOCK::now();
|
||||||
|
SECONDS diff = now - _lastFrame;
|
||||||
|
_deltaTime = diff.count();
|
||||||
|
_lastFrame = now;
|
||||||
|
|
||||||
|
#if _DEBUG && UNICODE
|
||||||
|
wstring output = L"fps: " + to_wstring(1.0f / _deltaTime) + L"\n";
|
||||||
|
OutputDebugString(output.c_str());
|
||||||
|
#endif // _DEBUG && UNICODE
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Time::deInit()
|
void Time::deInit()
|
||||||
|
|||||||
@@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
typedef chrono::steady_clock CLOCK;
|
||||||
|
typedef chrono::duration<FLOAT> SECONDS;
|
||||||
|
typedef CLOCK::time_point TIMEPOINT;
|
||||||
|
|
||||||
class Time
|
class Time
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -15,5 +19,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
FLOAT _deltaTime = 0.0f;
|
FLOAT _deltaTime = 0.0f;
|
||||||
|
TIMEPOINT _lastFrame = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <d3d9.h>
|
#include <d3d9.h>
|
||||||
|
|
||||||
#define FVF D3DFVF_XYZ
|
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE
|
||||||
|
|
||||||
struct Vertex
|
struct Vertex
|
||||||
{
|
{
|
||||||
@@ -10,6 +10,10 @@ struct Vertex
|
|||||||
float y;
|
float y;
|
||||||
float z;
|
float z;
|
||||||
|
|
||||||
Vertex() : x(0.0f), y(0.0f), z(0.0f) {}
|
// color
|
||||||
Vertex(float x, float y, float z) : x(x), y(y), z(z) {}
|
D3DCOLOR color;
|
||||||
|
|
||||||
|
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xffffffff) {}
|
||||||
|
Vertex(float x, float y, float z) : x(x), y(y), z(z), color(0xffffffff) {}
|
||||||
|
Vertex(float x, float y, float z, UINT r, UINT g, UINT b) : x(x), y(y), z(z), color(D3DCOLOR_XRGB(r, g, b)) {}
|
||||||
};
|
};
|
||||||
+3
-1
@@ -32,6 +32,8 @@ int WINAPI WinMain(
|
|||||||
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
error = d3d.init(window.getWindowHandle(), width, height, isFullscreen);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, FALSE);
|
||||||
|
|
||||||
// 3. create a mesh
|
// 3. create a mesh
|
||||||
Mesh mesh = {};
|
Mesh mesh = {};
|
||||||
error = mesh.init(d3d.getDevice());
|
error = mesh.init(d3d.getDevice());
|
||||||
@@ -39,7 +41,7 @@ int WINAPI WinMain(
|
|||||||
|
|
||||||
// 4. create a camera
|
// 4. create a camera
|
||||||
Camera camera = {};
|
Camera camera = {};
|
||||||
error = camera.init(width, height);
|
error = camera.init(d3d.getDevice(), width, height);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
// 5. create time
|
// 5. create time
|
||||||
|
|||||||
Reference in New Issue
Block a user