Add input layout

This commit is contained in:
2026-11-27 11:27:52 +01:00
parent 204c55e915
commit ead7463b8e
6 changed files with 81 additions and 33 deletions
+67 -20
View File
@@ -6,39 +6,57 @@
UINT Material::init(ID3D11Device* pD3DDevice, LPCTSTR textureFileName)
{
HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
CheckFailed(hr, 600);
//HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
//CheckFailed(hr, 600);
UINT error = 0;
error = createVertexShader(pD3DDevice);
CheckError(error);
error = createPixelShader(pD3DDevice);
CheckError(error);
return 0;
}
void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
{
// set texture
pD3DDevice->SetTexture(0, _pTexture);
//// set texture
//pD3DDevice->SetTexture(0, _pTexture);
// set sampler state
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(200, 0, 200));
pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
//// set sampler state
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
//pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
//pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(200, 0, 200));
//pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
//pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
//pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
//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 };
//// 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);
//pD3DDevice->SetMaterial(&material);
// set shaders
pD3DDeviceContext->VSSetShader(_pVertexShader, nullptr, 0);
pD3DDeviceContext->PSSetShader(_pPixelShader, nullptr, 0);
// set input layoyt
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
}
void Material::deInit()
{
safeRelease(_pInputLayout);
safeRelease(_pPixelShader);
safeRelease(_pVertexShader);
}
UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
@@ -100,5 +118,34 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pVertexShaderCode)
{
// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
D3D11_INPUT_ELEMENT_DESC inputs[4] = {};
// position
inputs[0].SemanticName = "POSITION";
inputs[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
// normal
inputs[1].SemanticName = "NORMAL";
inputs[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
inputs[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
// uv
inputs[2].SemanticName = "TEXCOORD";
inputs[2].Format = DXGI_FORMAT_R32G32_FLOAT;
inputs[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
// color
inputs[3].SemanticName = "COLOR";
inputs[3].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
inputs[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
HRESULT hr = pD3DDevice->CreateInputLayout(
inputs, std::size(inputs), // element description array and array size
pVertexShaderCode->GetBufferPointer(), pVertexShaderCode->GetBufferSize(), // vertex shader code to check compatability with vertex
&_pInputLayout
);
CheckFailed(hr, 640);
return 0;
}
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
float4 main() : SV_TARGET
{
return float4(1.0f, 1.0f, 1.0f, 1.0f);
return float4(1.0f, 0.0f, 1.0f, 1.0f);
}
+11 -10
View File
@@ -1,7 +1,7 @@
#pragma once
#include <d3d9.h>
#include <DirectXMath.h>
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_NORMAL
using namespace DirectX;
struct Vertex
{
@@ -15,21 +15,22 @@ struct Vertex
float ny;
float nz;
// color
D3DCOLOR color;
// texture coordinates
float u;
float 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) {}
// color
XMFLOAT4 color;
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(1.0f, 1.0f, 1.0f, 1.0f), 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(1.0f, 1.0f, 1.0f, 1.0f), 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) {}
x(x), y(y), z(z), color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f), 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) {}
x(x), y(y), z(z), color(1.0f, 1.0f, 1.0f, 1.0f), 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) {}
x(x), y(y), z(z), color(1.0f, 1.0f, 1.0f, 1.0f), u(u), v(v), nx(nx), ny(ny), nz(nz) {}
};
Binary file not shown.
+2 -2
View File
@@ -68,7 +68,7 @@ int WINAPI WinMain(
while (window.run())
{
// 8.1. update objects
//time.update();
time.update();
mesh.update(time.getDeltaTime());
//camera.update(time.getDeltaTime());
//light.update(time.getDeltaTime());
@@ -77,7 +77,7 @@ int WINAPI WinMain(
d3d.beginScene();
//light.render(d3d.getDevice());
material.render(d3d.getDevice());
material.render(d3d.getDeviceContext());
mesh.render(d3d.getDeviceContext());
d3d.endScene();