728x90
Cube.cpp, h
더보기
#include "stdafx.h"
#include "CubeDemo.h"
void CubeDemo::Initialize()
{
//카메라 위치 셋팅
Context::Get()->GetCamera()->RotationDegree(25, 2, 0);
Context::Get()->GetCamera()->Position(65, 14, 42);
shader = new Shader(L"08_Cube.fxo");
vertexCount = 24;
vertices = new Vertex[vertexCount];
float w = 0.5f, h = 0.5f, d = 0.5f;
//Cube
{
//Front(-Z) (시계방향)
{
vertices[0].Position = Vector3(-w, -h, -d);
vertices[1].Position = Vector3(-w, +h, -d);
vertices[2].Position = Vector3(+w, -h, -d);
vertices[3].Position = Vector3(+w, +h, -d);
}
//Back(+Z) (반시계방향)
{
vertices[4].Position = Vector3(-w, -h, +d);
vertices[5].Position = Vector3(+w, -h, +d);
vertices[6].Position = Vector3(-w, +h, +d);
vertices[7].Position = Vector3(+w, +h, +d);
}
//Top(+Y) (시계방향)
{
vertices[8].Position = Vector3(-w, +h, -d);
vertices[9].Position = Vector3(-w, +h, +d);
vertices[10].Position = Vector3(+w, +h, -d);
vertices[11].Position = Vector3(+w, +h, +d);
}
//Bottom(-Y) (반시계방향)
{
vertices[12].Position = Vector3(-w, -h, -d);
vertices[13].Position = Vector3(+w, -h, -d);
vertices[14].Position = Vector3(-w, -h, +d);
vertices[15].Position = Vector3(+w, -h, +d);
}
//Left(-X) (반시계방향)
{
vertices[16].Position = Vector3(-w, -h, +d);
vertices[17].Position = Vector3(-w, +h, +d);
vertices[18].Position = Vector3(-w, -h, -d);
vertices[19].Position = Vector3(-w, +h, -d);
}
//Right(-Y) (시계방향)
{
vertices[20].Position = Vector3(+w, -h, -d);
vertices[21].Position = Vector3(+w, +h, -d);
vertices[22].Position = Vector3(+w, -h, +d);
vertices[23].Position = Vector3(+w, +h, +d);
}
}
//Create Vertex Buffer
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.ByteWidth = sizeof(Vertex) * vertexCount;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA data = { 0 };
data.pSysMem = vertices;
Check(D3D::GetDevice()->CreateBuffer(&desc, &data, &vertexBuffer));
}
//Index
{
indexCount = 36;
indices = new UINT[indexCount]
{
0,1,2,2,1,3,//Fornt
4,5,6,6,5,7,//Back
8,9,10,10,9,11,//Top
12,13,14,14,13,15,//Bottom
16,17,18,18,17,19,//Left
20,21,22,22,21,23//Right
};
}
//Create Index Buffer
{
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
desc.ByteWidth = sizeof(Vertex) * indexCount;
desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA data = { 0 };
data.pSysMem = indices;
Check(D3D::GetDevice()->CreateBuffer(&desc, &data, &indexBuffer));
}
SafeDelete(vertices);
SafeDelete(indices);
D3DXMatrixIdentity(&world);
}
void CubeDemo::Destroy()
{
SafeDelete(shader);
SafeRelease(vertexBuffer);
SafeRelease(indexBuffer);
}
void CubeDemo::Update()
{
Vector3 forward = Vector3(world._31, world._32, world._33);
D3DXVec3Normalize(&forward, &forward); //방향으로 전환후 자기자신한테 저장
Vector3 right = Vector3(world._11, world._12, world._13);
D3DXVec3Normalize(&right, &right);
if (Keyboard::Get()->Press(VK_LSHIFT)) //쉬프트 누르면 회전
{
if (Keyboard::Get()->Press(VK_RIGHT))
rotation.y += rotationSpeed * Time::Delta();
if (Keyboard::Get()->Press(VK_LEFT))
rotation.y -= rotationSpeed * Time::Delta();
}
else
{
if (Keyboard::Get()->Press(VK_RIGHT))
position += right * moveSpeed* Time::Delta();
if (Keyboard::Get()->Press(VK_LEFT))
position -= right * moveSpeed* Time::Delta();
if (Keyboard::Get()->Press(VK_UP))
position += forward * moveSpeed* Time::Delta();
if (Keyboard::Get()->Press(VK_DOWN))
position -= forward * moveSpeed* Time::Delta();
}
Matrix S, R, T;
D3DXMatrixScaling(&S, scale.x, scale.y, scale.z);
D3DXMatrixRotationYawPitchRoll(&R, rotation.y, rotation.x, rotation.z);
D3DXMatrixTranslation(&T, position.x, position.y, position.z);
world = S * R * T;
shader->AsMatrix("World")->SetMatrix(world);
shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
}
void CubeDemo::Render()
{
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);
static UINT Pass;
ImGui::InputInt("Cube Pass", (int*)&Pass);
Pass %= 2;
shader->DrawIndexed(0, Pass, indexCount);
}
#pragma once
#include "Systems/IExecute.h"
class CubeDemo : public IExecute
{
public:
virtual void Initialize() 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:
struct Vertex
{
Vector3 Position;
};
private:
Shader* shader;
UINT vertexCount;
Vertex* vertices;
ID3D11Buffer* vertexBuffer;
UINT indexCount;
UINT* indices;
ID3D11Buffer* indexBuffer;
float moveSpeed = 10.0f;
float rotationSpeed = 2.0f;
Vector3 position = Vector3(64.0f, 0.5f, 64.0f);
Vector3 rotation = Vector3(0, 0, 0);
Vector3 scale = Vector3(1, 1, 2); //<
Matrix world;
};
08_Cube.fx
더보기
matrix World, View, Projection;
struct VertexInput
{
float4 Position : Position;
};
struct VertexOutput
{
float4 Position : SV_Position;
};
RasterizerState FillMode_WireFrame
{
FillMode = WireFrame;
};
VertexOutput VS(VertexInput input)
{
VertexOutput output;
output.Position = mul(input.Position, World);
output.Position = mul(output.Position, View);
output.Position = mul(output.Position, Projection);
return output;
}
float4 PS(VertexOutput input) : SV_Target
{
return float4(0, 1, 0, 1);
}
technique11 T0
{
pass P0
{
SetVertexShader(CompileShader(vs_5_0, VS()));
SetPixelShader(CompileShader(ps_5_0, PS()));
}
pass P1
{
SetRasterizerState(FillMode_WireFrame);
SetVertexShader(CompileShader(vs_5_0, VS()));
SetPixelShader(CompileShader(ps_5_0, PS()));
}
}
실행
Front, Top, Right : 시계방향
Back, Bottom, Left : 반시계방향
이런 형식으로 Index 배열에 넣을때 방영해서 넣음
그이유는 뒷면은 안그리기 때문에 위에 Back, Bottom, Left 3개를 시계방향으로 그리게 되면 짤려서 나옴
'DirectX > DirectX 3D_(구)' 카테고리의 다른 글
10_Sampler (0) | 2021.07.02 |
---|---|
09_Texture (0) | 2021.06.30 |
07_Camera (0) | 2021.06.30 |
06_Grid (0) | 2021.06.30 |
05_Index (0) | 2021.06.30 |