Full DX11 OBJ Rendering Setup
Full DX11 OBJ Rendering Setup
#include <windows.h>
#include <d3d11.h>
#include <DirectXMath.h>
class DX11Renderer {
public:
bool Initialize(HWND hwnd);
void ClearScreen(float r, float g, float b, float a);
void DrawShape(int shapeId);
void Present();
void Shutdown();
void Resize(int width, int height);
void UpdateViewMatrix(const DirectX::XMMATRIX& view, const DirectX::XMMATRIX& proj);
ID3D11Device* GetDevice() const { return device; }
ID3D11DeviceContext* GetContext() const { return context; }
private:
void CreateBuffer(const void* data, UINT size, ID3D11Buffer** out);
void LoadOBJ(const char* filename, int bufferIndex);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;
DX11Renderer renderer;
if (!renderer.Initialize(hwnd)) return -1;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForD3D(window);
ImGui_ImplDX11_Init(renderer.GetDevice(), renderer.GetContext());
while (running) {
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT) running = false;
if (event.type == SDL_WINDOWEVENT && event.window.event ==
SDL_WINDOWEVENT_SIZE_CHANGED) {
width = event.window.data1;
height = event.window.data2;
renderer.Resize(width, height);
}
}
yaw += 0.01f;
DirectX::XMVECTOR eye = DirectX::XMVectorSet(sinf(yaw) * 2.0f, 1.0f, cosf(yaw) * 2.0f,
0.0f);
DirectX::XMVECTOR target = DirectX::XMVectorZero();
DirectX::XMVECTOR up = DirectX::XMVectorSet(0, 1, 0, 0);
renderer.UpdateViewMatrix(view, proj);
ImGui_ImplDX11_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Shape Selector");
if (ImGui::Button("Pyramid")) selectedShape = 0;
if (ImGui::Button("Cube")) selectedShape = 1;
if (ImGui::Button("Cylinder")) selectedShape = 2;
ImGui::End();
ImGui::Render();
renderer.ClearScreen(0.1f, 0.1f, 0.1f, 1.0f);
renderer.DrawShape(selectedShape);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
renderer.Present();
}
ImGui_ImplDX11_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
renderer.Shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}