add sampler states
add light add normals
This commit is contained in:
+6
-1
@@ -1,7 +1,10 @@
|
|||||||
#include "Light.h"
|
#include "Light.h"
|
||||||
|
|
||||||
UINT Light::init()
|
UINT Light::init(D3DLIGHT9& light, INT id)
|
||||||
{
|
{
|
||||||
|
_light = light;
|
||||||
|
_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, &_light);
|
||||||
|
pD3DDevice->LightEnable(_id, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::deInit()
|
void Light::deInit()
|
||||||
|
|||||||
+5
-1
@@ -4,9 +4,13 @@
|
|||||||
class Light
|
class Light
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UINT init();
|
UINT init(D3DLIGHT9& light, INT id = 0);
|
||||||
void update(FLOAT dt);
|
void update(FLOAT dt);
|
||||||
void render(IDirect3DDevice9* pD3DDevice);
|
void render(IDirect3DDevice9* pD3DDevice);
|
||||||
void deInit();
|
void deInit();
|
||||||
|
|
||||||
|
private:
|
||||||
|
D3DLIGHT9 _light;
|
||||||
|
INT _id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+20
-1
@@ -2,19 +2,38 @@
|
|||||||
#include "Utilities.h"
|
#include "Utilities.h"
|
||||||
#include "WICTextureLoader9.h"
|
#include "WICTextureLoader9.h"
|
||||||
|
|
||||||
|
using namespace DirectX;
|
||||||
|
|
||||||
UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCWSTR textureFileName)
|
UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCWSTR textureFileName)
|
||||||
{
|
{
|
||||||
// load texture
|
// load texture
|
||||||
// https://github.com/microsoft/DirectXTex/tree/main/WICTextureLoader
|
// 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);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Material::render(IDirect3DDevice9* pD3DDevice)
|
void Material::render(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
|
// set texture
|
||||||
pD3DDevice->SetTexture(0, _pTexture);
|
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()
|
void Material::deInit()
|
||||||
|
|||||||
+43
-43
@@ -31,9 +31,9 @@ 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 pitch = XM_PIDIV4; // x axis
|
static FLOAT pitch = 0.2f; // x axis
|
||||||
static FLOAT yaw = XM_PIDIV4; // y axis
|
static FLOAT yaw = 0.0f; // y axis
|
||||||
static FLOAT roll = XM_PIDIV4; // z axis
|
static FLOAT roll = 0.4f; // z axis
|
||||||
static FLOAT scale = 1.0f;
|
static FLOAT scale = 1.0f;
|
||||||
|
|
||||||
yaw += 1.0f * dt;
|
yaw += 1.0f * dt;
|
||||||
@@ -84,7 +84,7 @@ void Mesh::deInit()
|
|||||||
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
// 1. define mesh parameters
|
// 1. define mesh parameters
|
||||||
_vertexCount = 8;
|
_vertexCount = 4;
|
||||||
_vertexStride = sizeof(Vertex);
|
_vertexStride = sizeof(Vertex);
|
||||||
|
|
||||||
// 2. create vertex buffer object WITHOUT data
|
// 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 fan, vertex buffer only
|
||||||
// quad with triangle list & index buffer
|
// quad with triangle list & index buffer
|
||||||
//pVertices[0] = 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);
|
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);
|
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);
|
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
|
||||||
@@ -145,17 +145,17 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
//pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 200, 200, 200);
|
//pVertices[7] = Vertex(0.5f, -0.5f, 0.5f, 200, 200, 200);
|
||||||
|
|
||||||
// cube with uv
|
// cube with uv
|
||||||
// front
|
//// front
|
||||||
pVertices[0] = Vertex(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f);
|
//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[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[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);
|
//pVertices[3] = Vertex(-0.5f, -0.5f, -0.5f, 0.0f, 1.0f);
|
||||||
|
|
||||||
// back
|
//// back
|
||||||
pVertices[4] = Vertex(0.5f, 0.5f, 0.5f, 0.0f, 0.0f);
|
//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[5] = Vertex(-0.5f, 0.5f, 0.5f, 3.0f, 0.0f);
|
||||||
pVertices[6] = Vertex(-0.5f, -0.5f, 0.5f, 1.0f, 1.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, 1.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
|
hr = _pVertexBuffer->Unlock(); // transfer vertex buffer back from cpu memory to video memory
|
||||||
CheckFailed(hr, 320);
|
CheckFailed(hr, 320);
|
||||||
@@ -167,7 +167,7 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
|
|
||||||
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
||||||
{
|
{
|
||||||
_indexCount = 36; // triangle count * 3
|
_indexCount = 6; // triangle count * 3
|
||||||
|
|
||||||
HRESULT hr = pD3DDevice->CreateIndexBuffer(
|
HRESULT hr = pD3DDevice->CreateIndexBuffer(
|
||||||
_indexCount * sizeof(WORD),
|
_indexCount * sizeof(WORD),
|
||||||
@@ -184,37 +184,37 @@ UINT Mesh::createIndexBuffer(IDirect3DDevice9* pD3DDevice)
|
|||||||
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
|
hr = _pIndexBuffer->Lock(0, 0, reinterpret_cast<void**>(&pIndices), 0);
|
||||||
CheckFailed(hr, 340);
|
CheckFailed(hr, 340);
|
||||||
|
|
||||||
//// quad
|
// quad
|
||||||
//// triangle 1 - 0, 1, 2
|
// 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
|
|
||||||
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||||
|
|
||||||
|
// triangle 2 - 0, 2, 3
|
||||||
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
||||||
|
|
||||||
// back
|
//// cube
|
||||||
pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6;
|
//// front
|
||||||
pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7;
|
//pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
|
||||||
|
//pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
|
||||||
|
|
||||||
// left
|
//// back
|
||||||
pIndices[12] = 5; pIndices[13] = 0; pIndices[14] = 3;
|
//pIndices[6] = 4; pIndices[7] = 5; pIndices[8] = 6;
|
||||||
pIndices[15] = 5; pIndices[16] = 3; pIndices[17] = 6;
|
//pIndices[9] = 4; pIndices[10] = 6; pIndices[11] = 7;
|
||||||
|
|
||||||
// right
|
//// left
|
||||||
pIndices[18] = 1; pIndices[19] = 4; pIndices[20] = 7;
|
//pIndices[12] = 5; pIndices[13] = 0; pIndices[14] = 3;
|
||||||
pIndices[21] = 1; pIndices[22] = 7; pIndices[23] = 2;
|
//pIndices[15] = 5; pIndices[16] = 3; pIndices[17] = 6;
|
||||||
|
|
||||||
// top
|
//// right
|
||||||
pIndices[24] = 5; pIndices[25] = 4; pIndices[26] = 1;
|
//pIndices[18] = 1; pIndices[19] = 4; pIndices[20] = 7;
|
||||||
pIndices[27] = 5; pIndices[28] = 1; pIndices[29] = 0;
|
//pIndices[21] = 1; pIndices[22] = 7; pIndices[23] = 2;
|
||||||
|
|
||||||
// bottom
|
//// top
|
||||||
pIndices[30] = 3; pIndices[31] = 2; pIndices[32] = 7;
|
//pIndices[24] = 5; pIndices[25] = 4; pIndices[26] = 1;
|
||||||
pIndices[33] = 3; pIndices[34] = 7; pIndices[35] = 6;
|
//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();
|
hr = _pIndexBuffer->Unlock();
|
||||||
CheckFailed(hr, 350);
|
CheckFailed(hr, 350);
|
||||||
|
|||||||
+10
-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
|
||||||
{
|
{
|
||||||
@@ -9,13 +9,18 @@ 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) : 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) : 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) {}
|
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) {}
|
||||||
};
|
};
|
||||||
+11
-2
@@ -38,7 +38,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_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
d3d.getDevice()->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
|
||||||
|
|
||||||
// 3. create a mesh
|
// 3. create a mesh
|
||||||
Mesh mesh = {};
|
Mesh mesh = {};
|
||||||
@@ -62,7 +65,13 @@ int WINAPI WinMain(
|
|||||||
|
|
||||||
// 7. create light source
|
// 7. create light source
|
||||||
Light light = {};
|
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);
|
CheckError(error);
|
||||||
|
|
||||||
// 8. run application
|
// 8. run application
|
||||||
|
|||||||
Reference in New Issue
Block a user