<code>
void App::onCreate()
{
Window::onCreate();
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Creating device and device context
if (!graphicsEngine.createDevices(hwnd, 1280, 720)) {
throw std::exception("Failed to create Direct3D device and swap chain");
}
g_pd3dDevice = graphicsEngine.device;
g_pd3dDeviceContext = graphicsEngine.deviceContext;
g_pSwapChain = graphicsEngine.swapChain->getSwapChain();
graphicsEngine.swapChain->createRenderTargetView();
g_mainRenderTargetView = graphicsEngine.swapChain->getRenderTargetView();
depth_stencil_view = graphicsEngine.depth_view;
// Ensure deviceContext is assigned correctly
deviceContext = new DeviceContext(g_pd3dDeviceContext);
if (deviceContext == nullptr) {
IMGUI_DEBUG_LOG("deviceContext is nullptr after creating devices");
}
// Load compiled vertex shader
std::vector<char> vsBytecode = LoadShader(L"VertexShader.cso");
vertex_shader = new VertexShader(vsBytecode.data(), vsBytecode.size(), g_pd3dDevice);
// Load compiled pixel shader
std::vector<char> psBytecode = LoadShader(L"PixelShader.cso");
pixel_shader = new PixelShader(psBytecode.data(), psBytecode.size(), g_pd3dDevice);
// Create vertex buffer
try {
vertex_buffer = new VertexBuffer(vertices, sizeof(Vertex), ARRAYSIZE(vertices), vsBytecode.data(), vsBytecode.size(), g_pd3dDevice);
if (vertex_buffer == nullptr) {
IMGUI_DEBUG_LOG("Vertex buffer is nullptr after creation");
}
IMGUI_DEBUG_LOG("Vertex buffer created successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to create Vertex buffer: %s", e.what());
return;
}
// Create index buffer
try {
index_buffer = new IndexBuffer(indices, ARRAYSIZE(indices), g_pd3dDevice);
if (index_buffer == nullptr) {
IMGUI_DEBUG_LOG("Index buffer is nullptr after creation");
}
IMGUI_DEBUG_LOG("Index buffer created successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to create Index buffer: %s", e.what());
return;
}
// Set the buffers in the device context
try {
deviceContext->setVertexBuffer(vertex_buffer);
IMGUI_DEBUG_LOG("Vertex buffer set successfully");
deviceContext->setIndexBuffer(index_buffer);
IMGUI_DEBUG_LOG("Index buffer set successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to set Vertex and Index buffers: %s", e.what());
}
//create the constant buffer
Constant c;
constant_buffer = new ConstantBuffer(&c, sizeof(Constant), g_pd3dDevice);
deviceContext->setConstantBuffer(vertex_shader, constant_buffer);
deviceContext->setConstantBuffer(pixel_shader, constant_buffer);
// Setup Platform/Renderer backends
if (!ImGui_ImplWin32_Init(hwnd)) {
throw std::exception("Failed to initialize ImGui Win32 backend");
}
if (!ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext)) {
throw std::exception("Failed to initialize ImGui DX11 backend");
}
// Create view and perspective matrices
CreateViewAndPerspective();
D3D11_RASTERIZER_DESC rasterDesc;
ZeroMemory(&rasterDesc, sizeof(rasterDesc));
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.DepthClipEnable = true;
ID3D11RasterizerState* rasterState;
g_pd3dDevice->CreateRasterizerState(&rasterDesc, &rasterState);
g_pd3dDeviceContext->RSSetState(rasterState);
}
void App::onUpdate()
{
// (Your code process and dispatch Win32 messages)
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(); // Show demo window! :)
// Rendering
render();
// (Your code clears your framebuffer, renders your other stuff etc.)
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
// Update the model transformation
updateModel();
}
void App::onDestroy()
{
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
if (vertex_buffer) {
vertex_buffer->~VertexBuffer();
}
if (index_buffer) {
index_buffer->~IndexBuffer();
}
if (vertex_shader) {
vertex_shader->~VertexShader();
}
if (pixel_shader) {
pixel_shader->~PixelShader();
}
graphicsEngine.release();
Window::onDestroy();
}
void App::render() {
deviceContext->clearRenderTargetColor(graphicsEngine.swapChain, 0.25f, 0.25f, 0.75f, 1.0f);
deviceContext->clearDepthStencilView(depth_stencil_view);
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, depth_stencil_view);
deviceContext->setViewportSize(1280, 720);
deviceContext->setVertexShader(vertex_shader);
deviceContext->setPixelShader(pixel_shader);
deviceContext->setVertexBuffer(vertex_buffer);
deviceContext->setIndexBuffer(index_buffer);
DirectX::XMMATRIX g_Player;
// Initialize the world matrix
g_Player = DirectX::XMMatrixIdentity();
DirectX::XMMATRIX mRotate = DirectX::XMMatrixRotationZ(m_rot_z);
DirectX::XMMATRIX mTranslate = DirectX::XMMatrixTranslation(0.0f, 0.0f, 1.0f);
DirectX::XMMATRIX mScale = DirectX::XMMatrixScaling(2.0f, 2.0f, 2.0f);
g_Player = mScale * mRotate * mTranslate;
// Combine with view and projection matrices
DirectX::XMMATRIX viewMatrix = DirectX::XMLoadFloat4x4(&view); // Load view matrix
DirectX::XMMATRIX projMatrix = DirectX::XMLoadFloat4x4(&proj); // Load projection matrix
DirectX::XMMATRIX worldViewProjMatrix = g_Player * viewMatrix * projMatrix; // Combine matrices
Constant cb1;
DirectX::XMStoreFloat4x4(&cb1.worldViewProj, DirectX::XMMatrixTranspose(worldViewProjMatrix));
constant_buffer->update(deviceContext, &cb1);
deviceContext->setConstantBuffer(vertex_shader, constant_buffer);
deviceContext->setConstantBuffer(pixel_shader, constant_buffer);
deviceContext->drawIndexedTriangleList(ARRAYSIZE(indices), 0, 0);
// Render ImGui UI
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0);
}
void App::update() {}
void App::updateModel() {
// Rotate the cube 1 degree per frame.
Constant cb;
DirectX::XMStoreFloat4x4(
&cb.worldViewProj,
DirectX::XMMatrixTranspose(
DirectX::XMMatrixRotationY(
DirectX::XMConvertToRadians(
(float)m_frame++
)
)
)
);
if (m_frame == MAXUINT) m_frame = 0;
}
void App::CreateViewAndPerspective()
{
// Use DirectXMath to create view and perspective matrices.
DirectX::XMVECTOR eye = DirectX::XMVectorSet(-2.0f, 1.5f, 1.5f, 0.0f);
DirectX::XMVECTOR at = DirectX::XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookAtRH(eye, at, up);
DirectX::XMStoreFloat4x4(&view, viewMatrix);
float aspectRatio = 1280.0f / 720.0f;
DirectX::XMMATRIX projMatrix = DirectX::XMMatrixPerspectiveFovRH(DirectX::XM_PIDIV4, aspectRatio, 0.01f, 100.0f);
DirectX::XMStoreFloat4x4(&proj, projMatrix);
}
</code>
<code>
void App::onCreate()
{
Window::onCreate();
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Creating device and device context
if (!graphicsEngine.createDevices(hwnd, 1280, 720)) {
throw std::exception("Failed to create Direct3D device and swap chain");
}
g_pd3dDevice = graphicsEngine.device;
g_pd3dDeviceContext = graphicsEngine.deviceContext;
g_pSwapChain = graphicsEngine.swapChain->getSwapChain();
graphicsEngine.swapChain->createRenderTargetView();
g_mainRenderTargetView = graphicsEngine.swapChain->getRenderTargetView();
depth_stencil_view = graphicsEngine.depth_view;
// Ensure deviceContext is assigned correctly
deviceContext = new DeviceContext(g_pd3dDeviceContext);
if (deviceContext == nullptr) {
IMGUI_DEBUG_LOG("deviceContext is nullptr after creating devices");
}
// Load compiled vertex shader
std::vector<char> vsBytecode = LoadShader(L"VertexShader.cso");
vertex_shader = new VertexShader(vsBytecode.data(), vsBytecode.size(), g_pd3dDevice);
// Load compiled pixel shader
std::vector<char> psBytecode = LoadShader(L"PixelShader.cso");
pixel_shader = new PixelShader(psBytecode.data(), psBytecode.size(), g_pd3dDevice);
// Create vertex buffer
try {
vertex_buffer = new VertexBuffer(vertices, sizeof(Vertex), ARRAYSIZE(vertices), vsBytecode.data(), vsBytecode.size(), g_pd3dDevice);
if (vertex_buffer == nullptr) {
IMGUI_DEBUG_LOG("Vertex buffer is nullptr after creation");
}
IMGUI_DEBUG_LOG("Vertex buffer created successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to create Vertex buffer: %s", e.what());
return;
}
// Create index buffer
try {
index_buffer = new IndexBuffer(indices, ARRAYSIZE(indices), g_pd3dDevice);
if (index_buffer == nullptr) {
IMGUI_DEBUG_LOG("Index buffer is nullptr after creation");
}
IMGUI_DEBUG_LOG("Index buffer created successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to create Index buffer: %s", e.what());
return;
}
// Set the buffers in the device context
try {
deviceContext->setVertexBuffer(vertex_buffer);
IMGUI_DEBUG_LOG("Vertex buffer set successfully");
deviceContext->setIndexBuffer(index_buffer);
IMGUI_DEBUG_LOG("Index buffer set successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to set Vertex and Index buffers: %s", e.what());
}
//create the constant buffer
Constant c;
constant_buffer = new ConstantBuffer(&c, sizeof(Constant), g_pd3dDevice);
deviceContext->setConstantBuffer(vertex_shader, constant_buffer);
deviceContext->setConstantBuffer(pixel_shader, constant_buffer);
// Setup Platform/Renderer backends
if (!ImGui_ImplWin32_Init(hwnd)) {
throw std::exception("Failed to initialize ImGui Win32 backend");
}
if (!ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext)) {
throw std::exception("Failed to initialize ImGui DX11 backend");
}
// Create view and perspective matrices
CreateViewAndPerspective();
D3D11_RASTERIZER_DESC rasterDesc;
ZeroMemory(&rasterDesc, sizeof(rasterDesc));
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.DepthClipEnable = true;
ID3D11RasterizerState* rasterState;
g_pd3dDevice->CreateRasterizerState(&rasterDesc, &rasterState);
g_pd3dDeviceContext->RSSetState(rasterState);
}
void App::onUpdate()
{
// (Your code process and dispatch Win32 messages)
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(); // Show demo window! :)
// Rendering
render();
// (Your code clears your framebuffer, renders your other stuff etc.)
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
// Update the model transformation
updateModel();
}
void App::onDestroy()
{
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
if (vertex_buffer) {
vertex_buffer->~VertexBuffer();
}
if (index_buffer) {
index_buffer->~IndexBuffer();
}
if (vertex_shader) {
vertex_shader->~VertexShader();
}
if (pixel_shader) {
pixel_shader->~PixelShader();
}
graphicsEngine.release();
Window::onDestroy();
}
void App::render() {
deviceContext->clearRenderTargetColor(graphicsEngine.swapChain, 0.25f, 0.25f, 0.75f, 1.0f);
deviceContext->clearDepthStencilView(depth_stencil_view);
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, depth_stencil_view);
deviceContext->setViewportSize(1280, 720);
deviceContext->setVertexShader(vertex_shader);
deviceContext->setPixelShader(pixel_shader);
deviceContext->setVertexBuffer(vertex_buffer);
deviceContext->setIndexBuffer(index_buffer);
DirectX::XMMATRIX g_Player;
// Initialize the world matrix
g_Player = DirectX::XMMatrixIdentity();
DirectX::XMMATRIX mRotate = DirectX::XMMatrixRotationZ(m_rot_z);
DirectX::XMMATRIX mTranslate = DirectX::XMMatrixTranslation(0.0f, 0.0f, 1.0f);
DirectX::XMMATRIX mScale = DirectX::XMMatrixScaling(2.0f, 2.0f, 2.0f);
g_Player = mScale * mRotate * mTranslate;
// Combine with view and projection matrices
DirectX::XMMATRIX viewMatrix = DirectX::XMLoadFloat4x4(&view); // Load view matrix
DirectX::XMMATRIX projMatrix = DirectX::XMLoadFloat4x4(&proj); // Load projection matrix
DirectX::XMMATRIX worldViewProjMatrix = g_Player * viewMatrix * projMatrix; // Combine matrices
Constant cb1;
DirectX::XMStoreFloat4x4(&cb1.worldViewProj, DirectX::XMMatrixTranspose(worldViewProjMatrix));
constant_buffer->update(deviceContext, &cb1);
deviceContext->setConstantBuffer(vertex_shader, constant_buffer);
deviceContext->setConstantBuffer(pixel_shader, constant_buffer);
deviceContext->drawIndexedTriangleList(ARRAYSIZE(indices), 0, 0);
// Render ImGui UI
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0);
}
void App::update() {}
void App::updateModel() {
// Rotate the cube 1 degree per frame.
Constant cb;
DirectX::XMStoreFloat4x4(
&cb.worldViewProj,
DirectX::XMMatrixTranspose(
DirectX::XMMatrixRotationY(
DirectX::XMConvertToRadians(
(float)m_frame++
)
)
)
);
if (m_frame == MAXUINT) m_frame = 0;
}
void App::CreateViewAndPerspective()
{
// Use DirectXMath to create view and perspective matrices.
DirectX::XMVECTOR eye = DirectX::XMVectorSet(-2.0f, 1.5f, 1.5f, 0.0f);
DirectX::XMVECTOR at = DirectX::XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookAtRH(eye, at, up);
DirectX::XMStoreFloat4x4(&view, viewMatrix);
float aspectRatio = 1280.0f / 720.0f;
DirectX::XMMATRIX projMatrix = DirectX::XMMatrixPerspectiveFovRH(DirectX::XM_PIDIV4, aspectRatio, 0.01f, 100.0f);
DirectX::XMStoreFloat4x4(&proj, projMatrix);
}
</code>
void App::onCreate()
{
Window::onCreate();
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Creating device and device context
if (!graphicsEngine.createDevices(hwnd, 1280, 720)) {
throw std::exception("Failed to create Direct3D device and swap chain");
}
g_pd3dDevice = graphicsEngine.device;
g_pd3dDeviceContext = graphicsEngine.deviceContext;
g_pSwapChain = graphicsEngine.swapChain->getSwapChain();
graphicsEngine.swapChain->createRenderTargetView();
g_mainRenderTargetView = graphicsEngine.swapChain->getRenderTargetView();
depth_stencil_view = graphicsEngine.depth_view;
// Ensure deviceContext is assigned correctly
deviceContext = new DeviceContext(g_pd3dDeviceContext);
if (deviceContext == nullptr) {
IMGUI_DEBUG_LOG("deviceContext is nullptr after creating devices");
}
// Load compiled vertex shader
std::vector<char> vsBytecode = LoadShader(L"VertexShader.cso");
vertex_shader = new VertexShader(vsBytecode.data(), vsBytecode.size(), g_pd3dDevice);
// Load compiled pixel shader
std::vector<char> psBytecode = LoadShader(L"PixelShader.cso");
pixel_shader = new PixelShader(psBytecode.data(), psBytecode.size(), g_pd3dDevice);
// Create vertex buffer
try {
vertex_buffer = new VertexBuffer(vertices, sizeof(Vertex), ARRAYSIZE(vertices), vsBytecode.data(), vsBytecode.size(), g_pd3dDevice);
if (vertex_buffer == nullptr) {
IMGUI_DEBUG_LOG("Vertex buffer is nullptr after creation");
}
IMGUI_DEBUG_LOG("Vertex buffer created successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to create Vertex buffer: %s", e.what());
return;
}
// Create index buffer
try {
index_buffer = new IndexBuffer(indices, ARRAYSIZE(indices), g_pd3dDevice);
if (index_buffer == nullptr) {
IMGUI_DEBUG_LOG("Index buffer is nullptr after creation");
}
IMGUI_DEBUG_LOG("Index buffer created successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to create Index buffer: %s", e.what());
return;
}
// Set the buffers in the device context
try {
deviceContext->setVertexBuffer(vertex_buffer);
IMGUI_DEBUG_LOG("Vertex buffer set successfully");
deviceContext->setIndexBuffer(index_buffer);
IMGUI_DEBUG_LOG("Index buffer set successfully");
}
catch (const std::exception& e) {
IMGUI_DEBUG_LOG("Failed to set Vertex and Index buffers: %s", e.what());
}
//create the constant buffer
Constant c;
constant_buffer = new ConstantBuffer(&c, sizeof(Constant), g_pd3dDevice);
deviceContext->setConstantBuffer(vertex_shader, constant_buffer);
deviceContext->setConstantBuffer(pixel_shader, constant_buffer);
// Setup Platform/Renderer backends
if (!ImGui_ImplWin32_Init(hwnd)) {
throw std::exception("Failed to initialize ImGui Win32 backend");
}
if (!ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext)) {
throw std::exception("Failed to initialize ImGui DX11 backend");
}
// Create view and perspective matrices
CreateViewAndPerspective();
D3D11_RASTERIZER_DESC rasterDesc;
ZeroMemory(&rasterDesc, sizeof(rasterDesc));
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.DepthClipEnable = true;
ID3D11RasterizerState* rasterState;
g_pd3dDevice->CreateRasterizerState(&rasterDesc, &rasterState);
g_pd3dDeviceContext->RSSetState(rasterState);
}
void App::onUpdate()
{
// (Your code process and dispatch Win32 messages)
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(); // Show demo window! :)
// Rendering
render();
// (Your code clears your framebuffer, renders your other stuff etc.)
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
// Update the model transformation
updateModel();
}
void App::onDestroy()
{
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
if (vertex_buffer) {
vertex_buffer->~VertexBuffer();
}
if (index_buffer) {
index_buffer->~IndexBuffer();
}
if (vertex_shader) {
vertex_shader->~VertexShader();
}
if (pixel_shader) {
pixel_shader->~PixelShader();
}
graphicsEngine.release();
Window::onDestroy();
}
void App::render() {
deviceContext->clearRenderTargetColor(graphicsEngine.swapChain, 0.25f, 0.25f, 0.75f, 1.0f);
deviceContext->clearDepthStencilView(depth_stencil_view);
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, depth_stencil_view);
deviceContext->setViewportSize(1280, 720);
deviceContext->setVertexShader(vertex_shader);
deviceContext->setPixelShader(pixel_shader);
deviceContext->setVertexBuffer(vertex_buffer);
deviceContext->setIndexBuffer(index_buffer);
DirectX::XMMATRIX g_Player;
// Initialize the world matrix
g_Player = DirectX::XMMatrixIdentity();
DirectX::XMMATRIX mRotate = DirectX::XMMatrixRotationZ(m_rot_z);
DirectX::XMMATRIX mTranslate = DirectX::XMMatrixTranslation(0.0f, 0.0f, 1.0f);
DirectX::XMMATRIX mScale = DirectX::XMMatrixScaling(2.0f, 2.0f, 2.0f);
g_Player = mScale * mRotate * mTranslate;
// Combine with view and projection matrices
DirectX::XMMATRIX viewMatrix = DirectX::XMLoadFloat4x4(&view); // Load view matrix
DirectX::XMMATRIX projMatrix = DirectX::XMLoadFloat4x4(&proj); // Load projection matrix
DirectX::XMMATRIX worldViewProjMatrix = g_Player * viewMatrix * projMatrix; // Combine matrices
Constant cb1;
DirectX::XMStoreFloat4x4(&cb1.worldViewProj, DirectX::XMMatrixTranspose(worldViewProjMatrix));
constant_buffer->update(deviceContext, &cb1);
deviceContext->setConstantBuffer(vertex_shader, constant_buffer);
deviceContext->setConstantBuffer(pixel_shader, constant_buffer);
deviceContext->drawIndexedTriangleList(ARRAYSIZE(indices), 0, 0);
// Render ImGui UI
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0);
}
void App::update() {}
void App::updateModel() {
// Rotate the cube 1 degree per frame.
Constant cb;
DirectX::XMStoreFloat4x4(
&cb.worldViewProj,
DirectX::XMMatrixTranspose(
DirectX::XMMatrixRotationY(
DirectX::XMConvertToRadians(
(float)m_frame++
)
)
)
);
if (m_frame == MAXUINT) m_frame = 0;
}
void App::CreateViewAndPerspective()
{
// Use DirectXMath to create view and perspective matrices.
DirectX::XMVECTOR eye = DirectX::XMVectorSet(-2.0f, 1.5f, 1.5f, 0.0f);
DirectX::XMVECTOR at = DirectX::XMVectorSet(0.0f, -0.1f, 0.0f, 0.0f);
DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookAtRH(eye, at, up);
DirectX::XMStoreFloat4x4(&view, viewMatrix);
float aspectRatio = 1280.0f / 720.0f;
DirectX::XMMATRIX projMatrix = DirectX::XMMatrixPerspectiveFovRH(DirectX::XM_PIDIV4, aspectRatio, 0.01f, 100.0f);
DirectX::XMStoreFloat4x4(&proj, projMatrix);
}
Basicallly I’m trying to render a cube alongside the ImGUI demo window.
I’ve been at this for two days and I honestly lost the ability to think clearly of what am I missing
Here’s the App.h
necessary content:
<code>#pragma once
class App : public Window
{
public:
App();
~App();
// Inherited via Window
virtual void onCreate() override;
virtual void onUpdate() override;
virtual void onDestroy() override;
virtual void onFocus() override;
virtual void onKillFocus() override;
virtual void onSize() override;
public:
void render();
void update();
void updateModel();
void updateCamera();
void updateSkybox();
void CreateViewAndPerspective();
private:
GraphicsEngine graphicsEngine;
IDXGISwapChain* g_pSwapChain = nullptr;
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
ID3D11Buffer* dx_index_buffer = nullptr;
ID3D11Buffer* dx_vertex_buffer = nullptr;
ID3D11Buffer* dx_constant_buffer = nullptr;
ID3D11InputLayout* vertexLayout = nullptr;
ID3D11DepthStencilView* depth_stencil_view = nullptr;
private:
SwapChain* swapChain = nullptr;
DeviceContext* deviceContext = nullptr;
const IndexBuffer* index_buffer = nullptr;
const VertexBuffer* vertex_buffer = nullptr;
ConstantBuffer* constant_buffer = nullptr;
VertexShader* vertex_shader = nullptr;
PixelShader* pixel_shader = nullptr;
private:
UINT g_ResizeWidth = 0, g_ResizeHeight = 0;
ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
private:
UINT m_frame = 0;
DirectX::XMFLOAT4X4 view = {};
DirectX::XMFLOAT4X4 proj = {};
float m_rot_x = 0.0f;
float m_rot_y = 0.0f;
float m_rot_z = 0.0f;
};
</code>
<code>#pragma once
class App : public Window
{
public:
App();
~App();
// Inherited via Window
virtual void onCreate() override;
virtual void onUpdate() override;
virtual void onDestroy() override;
virtual void onFocus() override;
virtual void onKillFocus() override;
virtual void onSize() override;
public:
void render();
void update();
void updateModel();
void updateCamera();
void updateSkybox();
void CreateViewAndPerspective();
private:
GraphicsEngine graphicsEngine;
IDXGISwapChain* g_pSwapChain = nullptr;
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
ID3D11Buffer* dx_index_buffer = nullptr;
ID3D11Buffer* dx_vertex_buffer = nullptr;
ID3D11Buffer* dx_constant_buffer = nullptr;
ID3D11InputLayout* vertexLayout = nullptr;
ID3D11DepthStencilView* depth_stencil_view = nullptr;
private:
SwapChain* swapChain = nullptr;
DeviceContext* deviceContext = nullptr;
const IndexBuffer* index_buffer = nullptr;
const VertexBuffer* vertex_buffer = nullptr;
ConstantBuffer* constant_buffer = nullptr;
VertexShader* vertex_shader = nullptr;
PixelShader* pixel_shader = nullptr;
private:
UINT g_ResizeWidth = 0, g_ResizeHeight = 0;
ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
private:
UINT m_frame = 0;
DirectX::XMFLOAT4X4 view = {};
DirectX::XMFLOAT4X4 proj = {};
float m_rot_x = 0.0f;
float m_rot_y = 0.0f;
float m_rot_z = 0.0f;
};
</code>
#pragma once
class App : public Window
{
public:
App();
~App();
// Inherited via Window
virtual void onCreate() override;
virtual void onUpdate() override;
virtual void onDestroy() override;
virtual void onFocus() override;
virtual void onKillFocus() override;
virtual void onSize() override;
public:
void render();
void update();
void updateModel();
void updateCamera();
void updateSkybox();
void CreateViewAndPerspective();
private:
GraphicsEngine graphicsEngine;
IDXGISwapChain* g_pSwapChain = nullptr;
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
ID3D11Buffer* dx_index_buffer = nullptr;
ID3D11Buffer* dx_vertex_buffer = nullptr;
ID3D11Buffer* dx_constant_buffer = nullptr;
ID3D11InputLayout* vertexLayout = nullptr;
ID3D11DepthStencilView* depth_stencil_view = nullptr;
private:
SwapChain* swapChain = nullptr;
DeviceContext* deviceContext = nullptr;
const IndexBuffer* index_buffer = nullptr;
const VertexBuffer* vertex_buffer = nullptr;
ConstantBuffer* constant_buffer = nullptr;
VertexShader* vertex_shader = nullptr;
PixelShader* pixel_shader = nullptr;
private:
UINT g_ResizeWidth = 0, g_ResizeHeight = 0;
ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
private:
UINT m_frame = 0;
DirectX::XMFLOAT4X4 view = {};
DirectX::XMFLOAT4X4 proj = {};
float m_rot_x = 0.0f;
float m_rot_y = 0.0f;
float m_rot_z = 0.0f;
};
What happened was: The App window is showing properly, but:
a) frames are acting weird, I cannot control my window properly because mouse cursor is acting strange
b) cube isn’t shown at all
New contributor
Abdul Sadek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.