Add Instanced Rendering / 2 more scenes

This commit is contained in:
2024-12-28 17:24:11 +01:00
parent 033909656a
commit 5e28e5fff4
30 changed files with 13730 additions and 100 deletions

View File

@@ -0,0 +1,95 @@
//
// Created by Bram on 28/12/2024.
//
#include "InstancedScene.h"
#include "../Utils.h"
#include "../Effects/Effect.h"
void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) {
m_DeviceContextPtr = DeviceContextPtr;
std::vector<VertexIn> vertices{};
std::vector<uint32_t> indices{};
if (!Utils::ParseOBJNew("resources/cube.obj", vertices, indices, false)) {
std::cout << "Model failed to load" << std::endl;
assert(true && "Model failed to load");
}
std::shared_ptr<Material> cubeMaterial = std::make_shared<Material>();
cubeMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/grass_block.png", DevicePtr);
std::vector<InstancedData> instanceData;
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
InstancedData data;
float scale = 2;
//Generate sine wave based on x and y
float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f);
//Add random but predictable randomness
YOffset += (x * 0.1f + y * 0.1f) * 0.1f;
data.worldMatrix = Matrix::CreateTranslation(x * scale, YOffset, y * scale);
data.worldMatrix *= Matrix::CreateScale(0.5f, 0.5f, 0.5f);
data.color = Vector4(float(x) / 255.f, float(y) / 255.f, 1.f, 1.0f);
instanceData.push_back(data);
}
}
auto *effect = new Effect(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
effect->NextSamplingState();
effect->NextSamplingState(); //Dirty hack
m_instancedMeshes.push_back(new InstancedMesh(DevicePtr, vertices, indices, cubeMaterial, effect, instanceData));
}
void InstancedScene::Cleanup() {
for (auto mesh: m_instancedMeshes) {
delete mesh;
}
m_instancedMeshes.clear();
for (auto mesh: m_meshes) {
delete mesh;
}
m_meshes.clear();
}
std::vector<Mesh *> &InstancedScene::GetMeshes() {
return m_meshes;
}
std::vector<std::shared_ptr<Material>> &InstancedScene::GetMaterials() {
return m_materials;
}
void InstancedScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
Matrix viewProjMatrix = camera.GetViewProjectionMatrix();
for (auto mesh: m_instancedMeshes) {
Matrix modelMatrix = mesh->GetWorldMatrix();
Matrix worldViewProjMatrix = modelMatrix * viewProjMatrix;
mesh->Render(devicePtr, worldViewProjMatrix);
}
}
void InstancedScene::Update() {
if (SDL_GetTicks() % 3 == 0) {
std::vector<InstancedData> instanceData;
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
InstancedData data;
float scale = 2;
//Generate sine wave based on x and y and the SDL_GetTicks
float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f);
YOffset += (x * 0.1f + y * 0.1f) * 0.1f;
data.worldMatrix = Matrix::CreateTranslation(x * scale, YOffset, y * scale);
data.worldMatrix *= Matrix::CreateScale(0.5f, 0.5f, 0.5f);
data.color = Vector4(float(x) / 255.f, float(y) / 255.f, 1.f, 1.0f);
instanceData.push_back(data);
}
}
m_instancedMeshes[0]->UpdateInstanceData(m_DeviceContextPtr, instanceData);
}
}