fix: D3D12 sphere rendering - correct matrix math, enable depth test, add texture sampling
- Fix TranslationMatrix to use correct indices (m12/m13/m14 for translation) - Fix PerspectiveMatrix projection formula - Enable depth testing (was disabled) - Add texture sampling in pixel shader - Adjust sphere radius to 1.0 and position to z=5 - Fix D3D12Buffer copy size calculation
This commit is contained in:
@@ -73,9 +73,6 @@ UINT gIndexCount = 0;
|
||||
float gProjectionMatrix[16];
|
||||
float gViewMatrix[16];
|
||||
float gModelMatrix[16];
|
||||
float gMVPMatrix[16];
|
||||
float gTempMatrix[16];
|
||||
float gTransposedMatrix[16];
|
||||
|
||||
HWND gHWND = nullptr;
|
||||
int gWidth = 1280;
|
||||
@@ -104,14 +101,23 @@ void IdentityMatrix(float* m) {
|
||||
m[0] = m[5] = m[10] = m[15] = 1.0f;
|
||||
}
|
||||
|
||||
void TranslationMatrix(float* m, float x, float y, float z) {
|
||||
memset(m, 0, 16 * sizeof(float));
|
||||
m[0] = 1.0f; m[12] = x;
|
||||
m[5] = 1.0f; m[13] = y;
|
||||
m[10] = 1.0f; m[14] = z;
|
||||
m[15] = 1.0f;
|
||||
}
|
||||
|
||||
void PerspectiveMatrix(float* m, float fov, float aspect, float nearZ, float farZ) {
|
||||
memset(m, 0, 16 * sizeof(float));
|
||||
float tanHalfFov = tanf(fov / 2.0f);
|
||||
m[0] = 1.0f / (aspect * tanHalfFov);
|
||||
m[5] = 1.0f / tanHalfFov;
|
||||
m[10] = (farZ + nearZ) / (nearZ - farZ);
|
||||
m[11] = -1.0f;
|
||||
m[14] = (2.0f * farZ * nearZ) / (nearZ - farZ);
|
||||
m[10] = farZ / (farZ - nearZ);
|
||||
m[11] = 1.0f;
|
||||
m[14] = -(farZ * nearZ) / (farZ - nearZ);
|
||||
m[15] = 0.0f;
|
||||
}
|
||||
|
||||
void LookAtMatrix(float* m, const float* eye, const float* target, const float* up) {
|
||||
@@ -360,7 +366,7 @@ bool InitD3D12() {
|
||||
psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
|
||||
psoDesc.RasterizerState.FrontCounterClockwise = FALSE;
|
||||
psoDesc.RasterizerState.DepthClipEnable = TRUE;
|
||||
psoDesc.DepthStencilState.DepthEnable = FALSE;
|
||||
psoDesc.DepthStencilState.DepthEnable = TRUE;
|
||||
psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
|
||||
psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
|
||||
psoDesc.BlendState.RenderTarget[0].BlendEnable = FALSE;
|
||||
@@ -396,19 +402,18 @@ bool InitD3D12() {
|
||||
}
|
||||
gIndexBuffer.SetBufferType(BufferType::Index);
|
||||
|
||||
PerspectiveMatrix(gProjectionMatrix, 45.0f * 3.14159f / 180.0f, (float)gWidth / (float)gHeight, 0.1f, 100.0f);
|
||||
float aspect = 1280.0f / 720.0f;
|
||||
PerspectiveMatrix(gProjectionMatrix, 45.0f * 3.141592f / 180.0f, aspect, 0.1f, 1000.0f);
|
||||
IdentityMatrix(gViewMatrix);
|
||||
TranslationMatrix(gModelMatrix, 0.0f, 0.0f, 5.0f);
|
||||
|
||||
float eye[3] = { 0.0f, 0.0f, 3.0f };
|
||||
float target[3] = { 0.0f, 0.0f, 0.0f };
|
||||
float up[3] = { 0.0f, 1.0f, 0.0f };
|
||||
LookAtMatrix(gViewMatrix, eye, target, up);
|
||||
float matrices[64];
|
||||
memcpy(matrices, gProjectionMatrix, 64);
|
||||
memcpy(matrices + 16, gViewMatrix, 64);
|
||||
memcpy(matrices + 32, gModelMatrix, 64);
|
||||
memcpy(matrices + 48, gModelMatrix, 64);
|
||||
|
||||
IdentityMatrix(gModelMatrix);
|
||||
MultiplyMatrix(gTempMatrix, gViewMatrix, gModelMatrix);
|
||||
MultiplyMatrix(gMVPMatrix, gProjectionMatrix, gTempMatrix);
|
||||
TransposeMatrix(gTransposedMatrix, gMVPMatrix);
|
||||
|
||||
gMVPBuffer.InitializeWithData(device, gCommandList.GetCommandList(), gTransposedMatrix, sizeof(gTransposedMatrix), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
|
||||
gMVPBuffer.InitializeWithData(device, gCommandList.GetCommandList(), matrices, sizeof(matrices), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
|
||||
|
||||
if (!LoadTexture("Res/Image/earth.png", gDiffuseTexture, gDiffuseSRV, device, gSRVHeap, gCommandList.GetCommandList(), gCommandAllocator, gCommandQueue)) {
|
||||
Log("[ERROR] Failed to load texture");
|
||||
|
||||
Reference in New Issue
Block a user