Add lighting
This commit is contained in:
+5
-1
@@ -1,7 +1,9 @@
|
|||||||
#include "Light.h"
|
#include "Light.h"
|
||||||
|
|
||||||
UINT Light::init()
|
UINT Light::init(D3DLIGHT9& lightData)
|
||||||
{
|
{
|
||||||
|
_lightData = lightData;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,6 +13,8 @@ void Light::update(FLOAT dt)
|
|||||||
|
|
||||||
void Light::render(IDirect3DDevice9* pD3DDevice)
|
void Light::render(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
|
pD3DDevice->SetLight(0, &_lightData);
|
||||||
|
pD3DDevice->LightEnable(0, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::deInit()
|
void Light::deInit()
|
||||||
|
|||||||
+4
-1
@@ -4,9 +4,12 @@
|
|||||||
class Light
|
class Light
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init();
|
UINT init(D3DLIGHT9& lightData);
|
||||||
void update(FLOAT dt);
|
void update(FLOAT dt);
|
||||||
void render(IDirect3DDevice9* pD3DDevice);
|
void render(IDirect3DDevice9* pD3DDevice);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
private:
|
||||||
|
D3DLIGHT9 _lightData = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,16 @@ void Material::render(IDirect3DDevice9* pD3DDevice)
|
|||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
||||||
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
|
||||||
|
|
||||||
|
// set material
|
||||||
|
D3DMATERIAL9 material = {};
|
||||||
|
material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
material.Specular = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
material.Power = 100.0f;
|
||||||
|
material.Ambient = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
material.Emissive = { 0.0f, 0.0f, 0.0f, 1.0f };
|
||||||
|
|
||||||
|
pD3DDevice->SetMaterial(&material);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Material::deInit()
|
void Material::deInit()
|
||||||
|
|||||||
+8
-6
@@ -29,8 +29,10 @@ void Mesh::update(FLOAT dt)
|
|||||||
static float x = 0.0f;
|
static float x = 0.0f;
|
||||||
static float y = 0.0f;
|
static float y = 0.0f;
|
||||||
static float z = 0.0f;
|
static float z = 0.0f;
|
||||||
|
static float rotY = 0.0f;
|
||||||
static float rotZ = 0.0f;
|
static float rotZ = 0.0f;
|
||||||
|
|
||||||
|
rotY += 1.0f * dt;
|
||||||
rotZ += 0.0f * dt;
|
rotZ += 0.0f * dt;
|
||||||
|
|
||||||
float move = 1.0f * dt;
|
float move = 1.0f * dt;
|
||||||
@@ -43,7 +45,7 @@ void Mesh::update(FLOAT dt)
|
|||||||
if ((GetAsyncKeyState(VK_ADD) & 0x8000) || (GetAsyncKeyState('E') & 0x8000)) z += move;
|
if ((GetAsyncKeyState(VK_ADD) & 0x8000) || (GetAsyncKeyState('E') & 0x8000)) z += move;
|
||||||
|
|
||||||
XMMATRIX translation = XMMatrixTranslation(x, y, z);
|
XMMATRIX translation = XMMatrixTranslation(x, y, z);
|
||||||
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ);
|
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, rotY, rotZ);
|
||||||
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
XMMATRIX scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
XMFLOAT4X4 w;
|
XMFLOAT4X4 w;
|
||||||
@@ -119,11 +121,11 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
//pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0, 0, 255);
|
//pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0, 0, 255);
|
||||||
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0);
|
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0);
|
||||||
|
|
||||||
// quad with uvs
|
// quad with uvs & normals
|
||||||
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 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, 1.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, 1.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, 1.0f);
|
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
hr = _pVertexBuffer->Unlock();
|
hr = _pVertexBuffer->Unlock();
|
||||||
CheckFailed(hr, 320);
|
CheckFailed(hr, 320);
|
||||||
|
|||||||
+16
-5
@@ -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
|
||||||
{
|
{
|
||||||
@@ -10,6 +10,11 @@ struct Vertex
|
|||||||
float y;
|
float y;
|
||||||
float z;
|
float z;
|
||||||
|
|
||||||
|
// normal
|
||||||
|
float nx;
|
||||||
|
float ny;
|
||||||
|
float nz;
|
||||||
|
|
||||||
// color
|
// color
|
||||||
D3DCOLOR color;
|
D3DCOLOR color;
|
||||||
|
|
||||||
@@ -17,8 +22,14 @@ struct Vertex
|
|||||||
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) : 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) {}
|
Vertex(float x, float y, float z, UINT r, UINT g, UINT b) :
|
||||||
Vertex(float x, float y, float z, float u, float v) : x(x), y(y), z(z), color(0xffffffff), u(u), v(v) {}
|
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) {}
|
||||||
};
|
};
|
||||||
+17
-2
@@ -32,7 +32,15 @@ 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);
|
d3d.getDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
||||||
|
|
||||||
|
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);
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
|
||||||
|
|
||||||
// 3. create a mesh
|
// 3. create a mesh
|
||||||
Mesh mesh = {};
|
Mesh mesh = {};
|
||||||
@@ -56,7 +64,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 = { -1.0f, -1.0f, 1.0f };
|
||||||
|
lightData.Diffuse = { 1.0f, 1.0f, 1.0f };
|
||||||
|
lightData.Specular = { 1.0f, 1.0f, 1.0f };
|
||||||
|
|
||||||
|
error = light.init(lightData);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
// 8. run application
|
// 8. run application
|
||||||
|
|||||||
Reference in New Issue
Block a user