I try to render a simple 2d quad using DirectX12, but somehow my perspective camera is not displaying the quad, orthographic camera works though. The reason of the perspective camera is to prepare for 3d rendering.
EDIT: Solved it, using glm
: I would need to call glm::transpose
before assigning the matrixes and as view matrix, I would need something like glm::lookAtRH({0.0f, 0.7f, 1.5f} /*<- eye*/, {0.0f, -0.1f, 0.0f} /*<- at*/, {0.0f, 1.0f, 0.0f} /*<- up*/);
. But I decided to go with DirectX's Math Library, the code would be:
ConstantBufferData cb_data{};
XMStoreFloat4x4(
&cb_data.model,
XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(64.0f))));
XMMATRIX proj_matrix = XMMatrixPerspectiveFovRH(
XMConvertToRadians(45.0f), (float)width / height, 0.01f, 100.0f);
static const XMVECTORF32 eye = {0.0f, 0.7f, 1.5f, 0.0f};
static const XMVECTORF32 at = {0.0f, -0.1f, 0.0f, 0.0f};
static const XMVECTORF32 up = {0.0f, 1.0f, 0.0f, 0.0f};
XMStoreFloat4x4(
&cb_data.view_proj,
XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up) * proj_matrix));
Here is my perspective camera code:
ConstantBufferData cb_data{};
cb_data.model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); // = glm::mat4(1.0f)
glm::mat4 view_matrix = glm::mat4(1.0f);
glm::mat4 proj_matrix =
glm::perspective(45.0f, (float)width / height, 0.1f, 100.f);
cb_data.view_proj = view_matrix * proj_matrix;
width and height are the window's width and height and they are correct.
if I now use a orthographic camera, everything works:
glm::mat4 proj_matrix = glm::ortho(-1.6f, 1.6f, -0.9f, 0.9f, -1.0f, 1.0f);
I am not sure, if I missed something that has to be done, in order to get the perspective camera to work?
Here are my vertices and indices:
Vertex vertices[] = {
{glm::vec3(0.5f, 0.5f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)},
{glm::vec3(0.5f, -0.5f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)},
{glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)},
{glm::vec3(-0.5f, 0.5f, 0.0f), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)},
};
UINT16 indices[] = {
0, 1, 2, 2, 3, 0,
};
and my vertex shader:
cbuffer ModelConstantBuffer : register(b0)
{
matrix modelMatrix;
matrix viewProjectionMatrix;
};
struct VSInput
{
float3 position : POSITION;
float4 color : COLOR;
};
struct VSOutput
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VSOutput main(VSInput input)
{
VSOutput output;
output.color = input.color;
float4 pos = float4(input.position, 1.0f);
pos = mul(pos, modelMatrix);
pos = mul(pos, viewProjectionMatrix);
output.position = pos;
output.position = pos;
return output;
}
Solved it, using glm: I would need to call glm::transpose before assigning the matrixes and as view matrix, I would need something like glm::lookAtRH({0.0f, 0.7f, 1.5f} /* <- eye /, {0.0f, -0.1f, 0.0f} / <- at /, {0.0f, 1.0f, 0.0f} / <- up */);. But I decided to go with DirectX's Math Library, the code would be:
ConstantBufferData cb_data{};
XMStoreFloat4x4(
&cb_data.model,
XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(64.0f))));
XMMATRIX proj_matrix = XMMatrixPerspectiveFovRH(
XMConvertToRadians(45.0f), (float)width / height, 0.01f, 100.0f);
static const XMVECTORF32 eye = {0.0f, 0.7f, 1.5f, 0.0f};
static const XMVECTORF32 at = {0.0f, -0.1f, 0.0f, 0.0f};
static const XMVECTORF32 up = {0.0f, 1.0f, 0.0f, 0.0f};
XMStoreFloat4x4(
&cb_data.view_proj,
XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up) * proj_matrix));