From 5e31638fb8858cf24a7c824f6ebb4ea603fbe9e5 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Wed, 12 Nov 2025 12:24:37 +0100 Subject: [PATCH] add sampler states add light add normals --- GDP/D3D9/Light.cpp | 7 +++- GDP/D3D9/Light.h | 6 ++- GDP/D3D9/Material.cpp | 21 ++++++++++- GDP/D3D9/Mesh.cpp | 86 +++++++++++++++++++++---------------------- GDP/D3D9/Vertex.h | 15 +++++--- GDP/D3D9/main.cpp | 13 ++++++- 6 files changed, 95 insertions(+), 53 deletions(-) diff --git a/GDP/D3D9/Light.cpp b/GDP/D3D9/Light.cpp index e40b6a1..9f7f251 100644 --- a/GDP/D3D9/Light.cpp +++ b/GDP/D3D9/Light.cpp @@ -1,7 +1,10 @@ #include "Light.h" -UINT Light::init() +UINT Light::init(D3DLIGHT9& light, INT id) { + _light = light; + _id = id; + return 0; } @@ -11,6 +14,8 @@ void Light::update(FLOAT dt) void Light::render(IDirect3DDevice9* pD3DDevice) { + pD3DDevice->SetLight(_id, &_light); + pD3DDevice->LightEnable(_id, TRUE); } void Light::deInit() diff --git a/GDP/D3D9/Light.h b/GDP/D3D9/Light.h index 51f6010..544da0a 100644 --- a/GDP/D3D9/Light.h +++ b/GDP/D3D9/Light.h @@ -4,9 +4,13 @@ class Light { public: - UINT init(); + UINT init(D3DLIGHT9& light, INT id = 0); void update(FLOAT dt); void render(IDirect3DDevice9* pD3DDevice); void deInit(); + +private: + D3DLIGHT9 _light; + INT _id; }; diff --git a/GDP/D3D9/Material.cpp b/GDP/D3D9/Material.cpp index 7c5614e..311db33 100644 --- a/GDP/D3D9/Material.cpp +++ b/GDP/D3D9/Material.cpp @@ -2,19 +2,38 @@ #include "Utilities.h" #include "WICTextureLoader9.h" +using namespace DirectX; + UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCWSTR textureFileName) { // load texture // https://github.com/microsoft/DirectXTex/tree/main/WICTextureLoader - HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture); + HRESULT hr = CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, WIC_LOADER_MIP_AUTOGEN); CheckFailed(hr, 600); + // set material + D3DMATERIAL9 material = {}; + material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f }; + material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f }; + material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f }; + material.Emissive = { 0.0f, 0.0f, 0.0f, 1.0f }; + material.Power = 512.0f; + + pD3DDevice->SetMaterial(&material); + return 0; } void Material::render(IDirect3DDevice9* pD3DDevice) { + // set texture pD3DDevice->SetTexture(0, _pTexture); + + // set sampler state + pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); + pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(255, 0, 255)); + pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); + pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0); } void Material::deInit() diff --git a/GDP/D3D9/Mesh.cpp b/GDP/D3D9/Mesh.cpp index ff220f4..4e0577a 100644 --- a/GDP/D3D9/Mesh.cpp +++ b/GDP/D3D9/Mesh.cpp @@ -31,9 +31,9 @@ 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 pitch = 0.2f; // x axis + static FLOAT yaw = 0.0f; // y axis + static FLOAT roll = 0.4f; // z axis static FLOAT scale = 1.0f; yaw += 1.0f * dt; @@ -84,7 +84,7 @@ void Mesh::deInit() UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) { // 1. define mesh parameters - _vertexCount = 8; + _vertexCount = 4; _vertexStride = sizeof(Vertex); // 2. create vertex buffer object WITHOUT data @@ -126,10 +126,10 @@ 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, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f); + pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f); + pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f); + pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f); //// cube with color //// front @@ -145,17 +145,17 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) //pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 200, 200, 200); // cube with uv - // front - pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f); - pVertices[1] = Vertex(0.5f, 0.5f, -0.5f, 1.0f, 0.0f); - pVertices[2] = Vertex(0.5f, -0.5f, -0.5f, 1.0f, 1.0f); - pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 0.0f, 1.0f); + //// front + //pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f); + //pVertices[1] = Vertex(0.5f, 0.5f, -0.5f, 1.0f, 0.0f); + //pVertices[2] = Vertex(0.5f, -0.5f, -0.5f, 1.0f, 1.0f); + //pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 0.0f, 1.0f); - // back - pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 0.0f, 0.0f); - pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f, 1.0f, 0.0f); - pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 1.0f, 1.0f); - pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 0.0f, 1.0f); + //// back + //pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 0.0f, 0.0f); + //pVertices[5] = Vertex(-0.5f, 0.5f, 0.5f, 3.0f, 0.0f); + //pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 3.0f, 3.0f); + //pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 0.0f, 3.0f); hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory CheckFailed(hr, 320); @@ -167,7 +167,7 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice) UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice) { - _indexCount = 36; // triangle count * 3 + _indexCount = 6; // triangle count * 3 HRESULT hr = pD3DDevice->CreateIndexBuffer( _indexCount * sizeof(WORD), @@ -184,37 +184,37 @@ UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice) hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast(&pIndices), 0); CheckFailed(hr, 340); - //// quad - //// triangle 1 - 0, 1, 2 - //pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2; - - //// triangle 2 - 0, 2, 3 - //pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3; - - // cube - // front + // quad + // triangle 1 - 0, 1, 2 pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2; + + // triangle 2 - 0, 2, 3 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; + //// cube + //// front + //pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2; + //pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3; - // left - pIndices[12] = 5; pIndices[13] = 0; pIndices[14] = 3; - pIndices[15] = 5; pIndices[16] = 3; pIndices[17] = 6; + //// back + //pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6; + //pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7; - // right - pIndices[18] = 1; pIndices[19] = 4; pIndices[20] = 7; - pIndices[21] = 1; pIndices[22] = 7; pIndices[23] = 2; + //// left + //pIndices[12] = 5; pIndices[13] = 0; pIndices[14] = 3; + //pIndices[15] = 5; pIndices[16] = 3; pIndices[17] = 6; - // top - pIndices[24] = 5; pIndices[25] = 4; pIndices[26] = 1; - pIndices[27] = 5; pIndices[28] = 1; pIndices[29] = 0; + //// right + //pIndices[18] = 1; pIndices[19] = 4; pIndices[20] = 7; + //pIndices[21] = 1; pIndices[22] = 7; pIndices[23] = 2; - // bottom - pIndices[30] = 3; pIndices[31] = 2; pIndices[32] = 7; - pIndices[33] = 3; pIndices[34] = 7; pIndices[35] = 6; + //// 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); diff --git a/GDP/D3D9/Vertex.h b/GDP/D3D9/Vertex.h index b7dce7b..5c32294 100644 --- a/GDP/D3D9/Vertex.h +++ b/GDP/D3D9/Vertex.h @@ -1,7 +1,7 @@ #pragma once #include -#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 +#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_NORMAL struct Vertex { @@ -9,13 +9,18 @@ struct Vertex FLOAT y; FLOAT z; + FLOAT nx; + FLOAT ny; + FLOAT nz; + D3DCOLOR color; FLOAT u; FLOAT v; - Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xFFFFFFFF), u(0.0f), v(0.0f) {} - Vertex(FLOAT _x, FLOAT _y, FLOAT _z) : x(_x), y(_y), z(_z), color(0xFFFFFFFF), u(0.0f), v(0.0f) {} - 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)), u(0.0f), v(0.0f) {} - Vertex(FLOAT _x, FLOAT _y, FLOAT _z, FLOAT _u, FLOAT _v) : x(_x), y(_y), z(_z), color(0xFFFFFFFF), u(_u), v(_v) {} + Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xFFFFFFFF), u(0.0f), v(0.0f), nx(0.0f), ny(0.0f), nz(0.0f) {} + Vertex(FLOAT _x, FLOAT _y, FLOAT _z) : x(_x), y(_y), z(_z), color(0xFFFFFFFF), u(0.0f), v(0.0f), nx(0.0f), ny(0.0f), nz(0.0f) {} + 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)), u(0.0f), v(0.0f), nx(0.0f), ny(0.0f), nz(0.0f) {} + Vertex(FLOAT _x, FLOAT _y, FLOAT _z, FLOAT _u, FLOAT _v) : x(_x), y(_y), z(_z), color(0xFFFFFFFF), u(_u), v(_v), nx(0.0f), ny(0.0f), nz(0.0f) {} + Vertex(FLOAT _x, FLOAT _y, FLOAT _z, FLOAT _nx, FLOAT _ny, FLOAT _nz, FLOAT _u, FLOAT _v) : x(_x), y(_y), z(_z), color(0xFFFFFFFF), u(_u), v(_v), nx(_nx), ny(_ny), nz(_nz) {} }; \ No newline at end of file diff --git a/GDP/D3D9/main.cpp b/GDP/D3D9/main.cpp index 0086aed..9fb76cf 100644 --- a/GDP/D3D9/main.cpp +++ b/GDP/D3D9/main.cpp @@ -38,7 +38,10 @@ int WINAPI WinMain( // set render states d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); - d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, FALSE); + d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE); + d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL); + d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL); + d3d.getDevice()->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL); // 3. create a mesh Mesh mesh = {}; @@ -62,7 +65,13 @@ int WINAPI WinMain( // 7. create light source Light light = {}; - error = light.init(); + D3DLIGHT9 data = {}; + data.Type = D3DLIGHT_DIRECTIONAL; + data.Direction = { 0.0f, 0.0f, 1.0f }; + data.Diffuse = { 0.0f, 0.0f, 0.0f, 1.0f }; + data.Specular = { 1.0f, 1.0f, 1.0f, 1.0f }; + data.Ambient = { 0.1f, 0.1f, 0.5f, 1.0f }; + error = light.init(data); CheckError(error); // 8. run application