add light

This commit is contained in:
2025-11-19 13:50:33 +01:00
parent 4910894bef
commit 770c3f790b
6 changed files with 49 additions and 13 deletions
+6 -1
View File
@@ -1,7 +1,10 @@
#include "Light.h" #include "Light.h"
UINT Light::init() UINT Light::init(D3DLIGHT9& lightData, UINT id)
{ {
_lightData = lightData;
_id = id;
return 0; return 0;
} }
@@ -11,6 +14,8 @@ void Light::update(FLOAT dt)
void Light::render(IDirect3DDevice9* pD3DDevice) void Light::render(IDirect3DDevice9* pD3DDevice)
{ {
pD3DDevice->SetLight(_id, &_lightData);
pD3DDevice->LightEnable(_id, TRUE);
} }
void Light::deInit() void Light::deInit()
+5 -1
View File
@@ -4,9 +4,13 @@
class Light class Light
{ {
public: public:
UINT init(); UINT init(D3DLIGHT9& lightData, UINT id = 0);
void update(FLOAT dt); void update(FLOAT dt);
void render(IDirect3DDevice9* pD3DDevice); void render(IDirect3DDevice9* pD3DDevice);
void deInit(); void deInit();
private:
D3DLIGHT9 _lightData = {};
UINT _id = 0;
}; };
+10
View File
@@ -11,6 +11,16 @@ UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCWSTR textureFileName)
HRESULT hr = CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, WIC_LOADER_MIP_AUTOGEN); HRESULT hr = CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, WIC_LOADER_MIP_AUTOGEN);
CheckFailed(hr, 600); 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 = 1024.0f;
pD3DDevice->SetMaterial(&material);
return 0; return 0;
} }
+5 -4
View File
@@ -123,10 +123,11 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
//// quad with trianglefan //// quad with trianglefan
//// quad with trianglelist & index buffer //// quad with trianglelist & index buffer
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f); // uv & normal
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 1.0f, 0.0f); pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 1.0f, 1.0f); pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 1.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 //// cube with color
//// front //// front
+11 -5
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <d3d9.h> #include <d3d9.h>
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 #define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_NORMAL
struct Vertex struct Vertex
{ {
@@ -9,13 +9,19 @@ struct Vertex
FLOAT y; FLOAT y;
FLOAT z; FLOAT z;
FLOAT nx;
FLOAT ny;
FLOAT nz;
D3DCOLOR color; D3DCOLOR color;
FLOAT u; FLOAT u;
FLOAT v; FLOAT v;
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xFFFFFFFF), u(0.0f), v(0.0f) {} 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) {}
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) : 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, FLOAT _u, FLOAT _v) : x(_x), y(_y), z(_z), color(0xFFFFFFFF), u(_u), v(_v) {} 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) {}
}; };
+12 -2
View File
@@ -34,7 +34,10 @@ int WINAPI WinMain(
// set render states // set render states
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, FALSE); d3d.getDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);
d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
d3d.getDevice()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
// 3. create a mesh // 3. create a mesh
Mesh mesh = {}; Mesh mesh = {};
@@ -58,7 +61,14 @@ int WINAPI WinMain(
// 7. create light source // 7. create light source
Light light = {}; Light light = {};
error = light.init(); D3DLIGHT9 lightData = {};
lightData.Type = D3DLIGHT_DIRECTIONAL;
lightData.Direction = { 0.0f, 0.0f, 1.0f };
lightData.Diffuse = { 0.5f, 0.5f, 0.5f, 1.0f };
lightData.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
lightData.Ambient = { 0.2f, 0.2f, 0.2f, 1.0f };
error = light.init(lightData);
CheckError(error); CheckError(error);
// 8. run application // 8. run application