0% found this document useful (0 votes)
6 views3 pages

Full DX11 OBJ Rendering Setup

The document contains a C++ implementation of a DirectX 11 renderer using SDL for window management and ImGui for GUI. It includes a class definition for DX11Renderer with methods for initialization, rendering shapes, and handling window resizing. The main function sets up the SDL window, initializes the renderer, and runs a loop to handle events and render selected shapes based on user input.

Uploaded by

elieeee1009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Full DX11 OBJ Rendering Setup

The document contains a C++ implementation of a DirectX 11 renderer using SDL for window management and ImGui for GUI. It includes a class definition for DX11Renderer with methods for initialization, rendering shapes, and handling window resizing. The main function sets up the SDL window, initializes the renderer, and runs a loop to handle events and render selected shapes based on user input.

Uploaded by

elieeee1009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#pragma once

#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);

ID3D11Device* device = nullptr;


ID3D11DeviceContext* context = nullptr;
IDXGISwapChain* swapChain = nullptr;
ID3D11RenderTargetView* renderTargetView = nullptr;
ID3D11VertexShader* vertexShader = nullptr;
ID3D11PixelShader* pixelShader = nullptr;
ID3D11InputLayout* inputLayout = nullptr;
ID3D11Buffer* viewMatrixBuffer = nullptr;
ID3D11Buffer* vertexBuffers[3] = { nullptr }; // 0=pyramid, 1=cube, 2=cylinder
ID3D11Buffer* indexBuffers[3] = { nullptr };
UINT indexCounts[3] = { 0 };
};
#include <SDL.h>
#include <SDL_syswm.h>
#include <DirectXMath.h>
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_dx11.h"
#include "DX11Renderer.h"

int main(int argc, char* argv[]) {


SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("DX11 Shapes",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

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());

bool running = true;


SDL_Event event;
float yaw = 0.0f;
int selectedShape = 0;
int width = 1280, height = 720;

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);
}
}

io.DisplaySize = ImVec2((float)width, (float)height);


io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);

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);

DirectX::XMMATRIX view = DirectX::XMMatrixLookAtLH(eye, target, up);


DirectX::XMMATRIX proj = DirectX::XMMatrixPerspectiveFovLH(DirectX::XM_PIDIV4,
(float)width / height, 0.1f, 100.0f);

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;
}

You might also like