728x90
중요핵심
1. 여러개 상자 만들기
2. Random으로 이용해서 크기, 위치 지정
-> D3DXMatrixScaling, D3DXMatrixTranslation
3. index용 ImGui 만들고 해당 인덱스 조종 및 크기 조종
4. 특정키 누르면 모든 네모들 회전
->D3DXMatrixRotation
CPP
더보기
#include "stdafx.h" #include "WorldDemo.h" void WorldDemo::Initialize() { shader = new Shader(L"04_World.fxo"); { 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); vertices[4].Position = Vector3(-0.5f, +0.5f, +0.0f); vertices[5].Position = Vector3(+0.5f, +0.5f, +0.0f); D3D11_BUFFER_DESC desc; ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC)); desc.ByteWidth = sizeof(Vertex) * 6; desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; D3D11_SUBRESOURCE_DATA data = { 0 }; data.pSysMem = vertices; Check(D3D::GetDevice()->CreateBuffer(&desc, &data, &vertexBuffer)); } //9개 설정 for (UINT i = 0; i < 9; i++) { D3DXMatrixIdentity(&world[i]); Vector3 val; Matrix S, T; val = Math::RandomVec3(0.5f, 2.0f); D3DXMatrixScaling(&S, val.x, val.y, val.z); val = Math::RandomVec3(-2.0f, 2.0f); D3DXMatrixTranslation(&T, val.x, val.y, val.z); world[i] = S * T; colors[i] = Math::RandomColor4(); } } void WorldDemo::Destroy() { SafeDelete(shader); SafeRelease(vertexBuffer); } void WorldDemo::Update() { if (Keyboard::Get()->Press(VK_LSHIFT)) { if (Keyboard::Get()->Press(VK_RIGHT)) world[index]._11 += 2.0f * Time::Delta(); else if (Keyboard::Get()->Press(VK_LEFT)) world[index]._11 -= 2.0f * Time::Delta(); if (Keyboard::Get()->Press(VK_UP)) world[index]._22 += 2.0f * Time::Delta(); else if (Keyboard::Get()->Press(VK_DOWN)) world[index]._22 -= 2.0f * Time::Delta(); } else { if (Keyboard::Get()->Press(VK_RIGHT)) world[index]._41 += 2.0f * Time::Delta(); else if (Keyboard::Get()->Press(VK_LEFT)) world[index]._41 -= 2.0f * Time::Delta(); if (Keyboard::Get()->Press(VK_UP)) world[index]._42 += 2.0f * Time::Delta(); else if (Keyboard::Get()->Press(VK_DOWN)) world[index]._42 -= 2.0f * Time::Delta(); } //Rotation 변경 if (Keyboard::Get()->Press(VK_LCONTROL)) { for (auto& W : world) { Vector3 scale, rotation, position; Math::MatrixDecompose(W, scale, rotation, position); rotation.z += 10.0f * Time::Delta(); Matrix S, R, T; D3DXMatrixScaling(&S, scale.x, scale.y, scale.z); D3DXMatrixTranslation(&T, position.x, position.y, position.z); D3DXMatrixRotationZ(&R, rotation.z); W = S * R * T; } } shader->AsMatrix("View")->SetMatrix(Context::Get()->View()); shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection()); } void WorldDemo::Render() { UINT stride = sizeof(Vertex); UINT offset = 0; D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset); D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); static UINT Pass = 0; ImGui::InputInt("Pass", (int*)&Pass); Pass %= 2; ImGui::InputInt("Index", (int*)&index); index %= 9; //9개 월드, 컬러 배치 for (UINT i = 0; i < 9; i++) { shader->AsMatrix("World")->SetMatrix(world[i]); shader->AsVector("Color")->SetFloatVector(colors[i]); shader->Draw(0, Pass, 6); } }
실행
'DirectX > 응용' 카테고리의 다른 글
등산하는 큐브 만들어 보기 (0) | 2021.07.05 |
---|---|
STRIP 이용한 원 그려보기 (0) | 2021.06.29 |