add input
add time add vertex color
This commit is contained in:
+24
-12
@@ -28,15 +28,27 @@ UINT Mesh::init(IDirect3DDevice9* pD3DDevice)
|
||||
|
||||
void Mesh::update(FLOAT dt)
|
||||
{
|
||||
static FLOAT x = 0.0f;
|
||||
static FLOAT y = 0.0f;
|
||||
static FLOAT z = 0.0f;
|
||||
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;
|
||||
yaw += 1.0f * dt;
|
||||
//scale += 0.001f * dt;
|
||||
|
||||
XMMATRIX translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.0f);
|
||||
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('E') & 0x8000) z -= move;
|
||||
if (GetAsyncKeyState('Q') & 0x8000) z += move;
|
||||
|
||||
XMMATRIX translationMatrix = XMMatrixTranslation(x, y, z);
|
||||
XMMATRIX scaleMatrix = XMMatrixScaling(scale, scale, scale);
|
||||
XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
|
||||
|
||||
@@ -121,16 +133,16 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||
|
||||
// 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);
|
||||
pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 255, 0, 0);
|
||||
pVertices[1] = Vertex(0.5f, 0.5f, -0.5f, 0, 255, 0);
|
||||
pVertices[2] = Vertex(0.5f, -0.5f, -0.5f, 0, 0, 255);
|
||||
pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 255, 0, 255);
|
||||
|
||||
// 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);
|
||||
// back
|
||||
pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 255, 255, 0);
|
||||
pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f, 0, 255, 255);
|
||||
pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 0, 0, 0);
|
||||
pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 200, 200, 200);
|
||||
|
||||
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
|
||||
CheckFailed(hr, 320);
|
||||
|
||||
@@ -2,11 +2,19 @@
|
||||
|
||||
UINT Time::init()
|
||||
{
|
||||
_lastFrame = CLOCK::now();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Time::update()
|
||||
{
|
||||
TIMEPOINT now = CLOCK::now();
|
||||
SECONDS diff = now - _lastFrame;
|
||||
_lastFrame = now;
|
||||
_deltaTime = diff.count();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Time::deInit()
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef chrono::steady_clock CLOCK;
|
||||
typedef CLOCK::time_point TIMEPOINT;
|
||||
typedef chrono::duration<FLOAT> SECONDS;
|
||||
|
||||
class Time
|
||||
{
|
||||
public:
|
||||
@@ -15,5 +19,6 @@ public:
|
||||
|
||||
private:
|
||||
FLOAT _deltaTime = 0.0f;
|
||||
TIMEPOINT _lastFrame = {};
|
||||
};
|
||||
|
||||
|
||||
+6
-3
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include <d3d9.h>
|
||||
|
||||
#define FVF D3DFVF_XYZ
|
||||
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
@@ -9,6 +9,9 @@ struct Vertex
|
||||
FLOAT y;
|
||||
FLOAT z;
|
||||
|
||||
Vertex() : x(0.0f), y(0.0f), z(0.0f) {}
|
||||
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)) {}
|
||||
};
|
||||
@@ -38,6 +38,7 @@ int WINAPI WinMain(
|
||||
|
||||
// set render states
|
||||
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
||||
d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, FALSE);
|
||||
|
||||
// 3. create a mesh
|
||||
Mesh mesh = {};
|
||||
@@ -67,6 +68,13 @@ int WINAPI WinMain(
|
||||
// 8. run application
|
||||
while (window.run())
|
||||
{
|
||||
if (GetAsyncKeyState(VK_F1) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
||||
if (GetAsyncKeyState(VK_F2) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
||||
if (GetAsyncKeyState(VK_F3) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT);
|
||||
if (GetAsyncKeyState(VK_F4) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
||||
if (GetAsyncKeyState(VK_F5) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
|
||||
if (GetAsyncKeyState(VK_F6) & 0x8000) d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
||||
|
||||
// 8.1. update objects
|
||||
time.update();
|
||||
mesh.update(time.getDeltaTime());
|
||||
|
||||
Reference in New Issue
Block a user