[DirectX11] 011 Index
목차
Index
Index를 활용한 사각형 그리기
- 0 1 2 2 1 3 순서로 그린다.
IndexDemo 코드
10_World.fx 코드는 이전글과 동일
IndexDemo.h
더보기
#pragma once
#include "Systems/IExecute.h"
class IndexDemo : public IExecute
{
public:
virtual void Initialize() override;
virtual void Ready() override {}
virtual void Destroy() override;
virtual void Update() override;
virtual void PreRender() override {}
virtual void Render() override;
virtual void PostRender() override {}
virtual void ResizeScreen() override {}
private:
Shader* shader;
Vertex vertices[4];
ID3D11Buffer* vertexBuffer;
UINT indices[6];
ID3D11Buffer* indexBuffer;
Matrix world;
};
IndexDemo.cpp
더보기
#include "stdafx.h"
#include "IndexDemo.h"
void IndexDemo::Initialize()
{
shader = new Shader(L"10_World.fx");
vertices[0].Position = Vector3(-0.5f, -0.5f, 0.0f);
vertices[1].Position = Vector3(-0.5f, +0.5f, 0.0f);
vertices[2].Position = Vector3(+0.5f, -0.5f, 0.0f);
vertices[3].Position = Vector3(+0.5f, +0.5f, 0.0f);
//Create Vertex Buffer
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.ByteWidth = sizeof(Vertex) * 4;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA subResource = { 0 };
subResource.pSysMem = vertices;
Check(D3D::GetDevice()->CreateBuffer(&desc, &subResource, &vertexBuffer));
}
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 1;
indices[5] = 3;
//Create Index Buffer
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.ByteWidth = sizeof(UINT) * 6;
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA subResource = { 0 };
subResource.pSysMem = indices;
Check(D3D::GetDevice()->CreateBuffer(&desc, &subResource, &indexBuffer));
}
D3DXMatrixIdentity(&world);
}
void IndexDemo::Destroy()
{
SafeDelete(shader);
SafeDelete(vertexBuffer);
SafeDelete(indexBuffer);
}
void IndexDemo::Update()
{
}
void IndexDemo::Render()
{
shader->AsScalar("Index")->SetInt(0);
shader->AsMatrix("World")->SetMatrix(world);
shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
UINT stride = sizeof(Vertex);
UINT offset = 0;
D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
D3D::GetDC()->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
shader->DrawIndexed(0, 1, 6);
}
Main.cpp
더보기
#include "stdafx.h"
#include "Main.h"
#include "Systems/Window.h"
#include "IndexDemo.h"
void Main::Initialize()
{
Push(new IndexDemo());
}
void Main::Ready()
{
}
void Main::Destroy()
{
for (IExecute* exe : executes)
{
exe->Destroy();
SafeDelete(exe);
}
}
void Main::Update()
{
for (IExecute* exe : executes)
exe->Update();
}
void Main::PreRender()
{
for (IExecute* exe : executes)
exe->PreRender();
}
void Main::Render()
{
for (IExecute* exe : executes)
exe->Render();
}
void Main::PostRender()
{
for (IExecute* exe : executes)
exe->PostRender();
}
void Main::ResizeScreen()
{
for (IExecute* exe : executes)
exe->ResizeScreen();
}
void Main::Push(IExecute * execute)
{
executes.push_back(execute);
execute->Initialize();
}
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR param, int command)
{
D3DDesc desc;
desc.AppName = L"D3D Game";
desc.Instance = instance;
desc.bFullScreen = false;
desc.bVsync = false;
desc.Handle = NULL;
desc.Width = 1280;
desc.Height = 720;
desc.Background = Color(0.3f, 0.3f, 0.3f, 1.0f);
D3D::SetDesc(desc);
Main* main = new Main();
WPARAM wParam = Window::Run(main);
SafeDelete(main);
return wParam;
}
실행화면
'⭐ DirectX > DirectX11 3D' 카테고리의 다른 글
[DirectX11] 013~014 Camera (0) | 2023.01.11 |
---|---|
[DirectX11] 012 Grid (1) | 2023.01.10 |
[DirectX11] 010 World (0) | 2023.01.09 |
[DirectX11] 009 World Matrix (0) | 2023.01.08 |
[DirectX11] 008 User Interface (1) | 2023.01.07 |
댓글
이 글 공유하기
다른 글
-
[DirectX11] 013~014 Camera
[DirectX11] 013~014 Camera
2023.01.11 -
[DirectX11] 012 Grid
[DirectX11] 012 Grid
2023.01.10 -
[DirectX11] 010 World
[DirectX11] 010 World
2023.01.09 -
[DirectX11] 009 World Matrix
[DirectX11] 009 World Matrix
2023.01.08