Files
GP1-DirectX/project/src/Scenes/DioramaScene.cpp

98 lines
3.6 KiB
C++

//
// Created by Bram on 25/12/2024.
//
#include "DioramaScene.h"
#include "../Utils.h"
#include "../Effects/Effect.h"
#include "../Effects/FireEffect.h"
#include <iostream>
void DioramaScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) {
std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes;
Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr);
for (const auto &mesh: materialMeshes) {
if (!mesh->vertices.empty()) {
std::shared_ptr<Material> material = std::make_shared<Material>();
std::unique_ptr<BaseEffect> effect{};
if (!mesh->opacity_map.empty()) {
effect = std::make_unique<FireEffect>(DevicePtr, L"resources/Fire.fx");
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr);
} else {
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr);
effect = std::make_unique<Effect>(DevicePtr, L"resources/SimpleDiffuse.fx");
}
auto newMesh = std::make_unique<Mesh>(DevicePtr, mesh->vertices, mesh->indices, material, std::move(effect));
m_meshes.push_back(std::move(newMesh));
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateScale(2.f, 2.f, 2.f);
worldMatrix *= Matrix::CreateScale(-1.f, 1.f, 1.f); // Mirror the model (Possible loading fault)
m_meshes.back()->SetWorldMatrix(worldMatrix);
}
}
//Load brok
materialMeshes.clear();
Utils::LoadObjWithMaterials("resources/brok/brok.obj", materialMeshes, true, DevicePtr);
for (const auto &mesh: materialMeshes) {
if (!mesh->vertices.empty()) {
std::shared_ptr<Material> material = std::make_shared<Material>();
auto effect = std::make_unique<Effect>(DevicePtr, L"resources/SimpleDiffuse.fx");
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/brok/" + mesh->diffuse_texture, DevicePtr);
auto brokMesh = std::make_unique<Mesh>(DevicePtr, mesh->vertices, mesh->indices, material, std::move(effect));
m_brokMeshses.push_back(brokMesh.get());
m_meshes.push_back(std::move(brokMesh));
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateRotationY(3.14f / 2.f);
worldMatrix *= Matrix::CreateScale(0.021f, 0.021f, 0.021f);
worldMatrix *= Matrix::CreateScale(-1.f, 1.f, 1.f);
worldMatrix *= Matrix::CreateTranslation(-8.55f, 12.33f, 0.69f);
m_meshes.back()->SetWorldMatrix(worldMatrix);
}
}
}
void DioramaScene::Update() {
m_brokTimer += 0.01f;
for(auto* mesh : m_brokMeshses){
if(mesh == nullptr)
continue;
Matrix worldMatrix = mesh->GetWorldMatrix();
float YOffset = sin(m_brokTimer) / 2000.f;
worldMatrix *= Matrix::CreateTranslation(0.f, YOffset, 0.f);
mesh->SetWorldMatrix(worldMatrix);
}
}
void DioramaScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
}
std::vector<std::unique_ptr<Mesh>> &DioramaScene::GetMeshes() {
return m_meshes;
}
std::vector<std::shared_ptr<Material>> &DioramaScene::GetMaterials() {
return m_materials;
}
void DioramaScene::Cleanup() {
m_meshes.clear();
m_materials.clear();
}