// // Created by Bram on 28/12/2024. // #include "InstancedScene.h" #include "../Utils.h" #include "../Effects/Effect.h" void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) { m_DeviceContextPtr = DeviceContextPtr; std::vector vertices{}; std::vector 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 cubeMaterial = std::make_shared(); cubeMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/grass_block.png", DevicePtr); // std::vector 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); // } // } std::vector instanceData; for (int x = 0; x < 100; ++x) { for (int y = 0; y < 100; ++y) { InstancedData data; float scale = 2; //use the perlin noise to generate the YOffset float currentTick = static_cast(SDL_GetTicks()); float YOffset = sin(x * 0.5f +currentTick * 0.001f) + cos(y * 0.5f + currentTick * 0.001f); 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 = std::make_unique(DevicePtr, L"resources/InstancedSimpleDiffuse.fx"); effect->NextSamplingState(); effect->NextSamplingState(); //Dirty hack auto cubeMesh = std::make_unique(DevicePtr, vertices, indices, cubeMaterial, std::move(effect), instanceData); m_instancedMeshes.push_back(std::move(cubeMesh)); } void InstancedScene::Cleanup() { m_instancedMeshes.clear(); m_meshes.clear(); } std::vector> &InstancedScene::GetMeshes() { return m_meshes; } std::vector> &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 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 currentTick = static_cast(SDL_GetTicks()); float YOffset = sin(static_cast(x) * 0.5f + currentTick * 0.001f) + cos(static_cast(y) * 0.5f + currentTick * 0.001f); YOffset += (static_cast(x) * 0.1f + static_cast(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); } }