109 lines
4.3 KiB
C++
109 lines
4.3 KiB
C++
//
|
|
// 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<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);
|
|
// }
|
|
// }
|
|
|
|
std::vector<InstancedData> 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<float>(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<Effect>(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
|
|
effect->NextSamplingState();
|
|
effect->NextSamplingState(); //Dirty hack
|
|
|
|
auto cubeMesh = std::make_unique<InstancedMesh>(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<std::unique_ptr<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 currentTick = static_cast<float>(SDL_GetTicks());
|
|
float YOffset = sin(static_cast<float>(x) * 0.5f + currentTick * 0.001f) + cos(static_cast<float>(y) * 0.5f + currentTick * 0.001f);
|
|
YOffset += (static_cast<float>(x) * 0.1f + static_cast<float>(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);
|
|
}
|
|
|
|
}
|