Compare commits

...

3 Commits

Author SHA1 Message Date
12cbbb4dcb Controller optimize thingy 2025-01-15 02:05:56 +01:00
2448bf3872 Stupid Controller Fix :p 2025-01-14 21:07:28 +01:00
a3a6011e43 Unique Ptrs, Const correctness 2025-01-14 20:51:17 +01:00
28 changed files with 130 additions and 133 deletions

View File

@@ -193,7 +193,7 @@ const dae::Vector3 & dae::Camera::GetPosition() const {
return origin;
}
const dae::Vector3 dae::Camera::GetRotation() const {
dae::Vector3 dae::Camera::GetRotation() const {
return {totalPitch, totalYaw, 0.f};
}

View File

@@ -11,7 +11,7 @@
#include "Math/Matrix.h"
namespace dae {
class Camera {
class Camera final {
public:
Camera() = default;
@@ -32,7 +32,7 @@ namespace dae {
const Vector3 & GetPosition() const;
void SetPosition(const Vector3& position);
const Vector3 GetRotation() const;
Vector3 GetRotation() const;
void SetRotation(const Vector3& rotation);
private:

View File

@@ -1,7 +1,3 @@
//
// Created by Bram on 20/12/2024.
//
#ifndef GP1_DIRECTX_BASEEFFECT_H
#define GP1_DIRECTX_BASEEFFECT_H

View File

@@ -12,7 +12,7 @@ enum class CullMode {
};
class Effect: public BaseEffect {
class Effect final: public BaseEffect {
public:
Effect(ID3D11Device *devicePtr, const std::wstring &filePath);

View File

@@ -9,7 +9,7 @@
#include <array>
#include "BaseEffect.h"
class FireEffect: public BaseEffect {
class FireEffect final: public BaseEffect {
public:
FireEffect(ID3D11Device *devicePtr, const std::wstring &filePath);

View File

@@ -21,7 +21,7 @@ void GamePadController::Init() {
SDL_GameController *pController = SDL_GameControllerOpen(i);
if (pController) {
m_pGameControllers.push_back(pController);
m_GamePads.push_back(GamePad());
m_GamePads.emplace_back();
}
}
}
@@ -46,7 +46,7 @@ void GamePadController::Update() {
SDL_GameController *pController = SDL_GameControllerOpen(i);
if (pController) {
m_pGameControllers.push_back(pController);
m_GamePads.push_back(GamePad());
m_GamePads.emplace_back();
std::cout << GREEN << "New Controller Connected" << RESET << std::endl;
}
@@ -64,7 +64,6 @@ void GamePadController::Update() {
std::cout << RED << "Controller Disconnected" << RESET << std::endl;
--i; // Adjust index after removing the controller
}
}

View File

@@ -2,16 +2,17 @@
#define GP1_DIRECTX_GAMEPADCONTROLLER_H
#include <vector>
#include <array>
#include "SDL_gamecontroller.h"
#include "SDL.h"
struct GamePad{
bool buttons[SDL_CONTROLLER_BUTTON_MAX];
float axis[SDL_CONTROLLER_AXIS_MAX];
std::array<bool, SDL_CONTROLLER_BUTTON_MAX> buttons{false};
std::array<float, SDL_CONTROLLER_AXIS_MAX> axis{0.0f};
bool prevButtons[SDL_CONTROLLER_BUTTON_MAX]{ false };
std::array<bool, SDL_CONTROLLER_BUTTON_MAX> prevButtons{ false };
};
enum class GamePadButton {
@@ -42,7 +43,7 @@ enum class GamePadButton {
enum Controllers {PLAYER1, PLAYER2, PLAYER3, PLAYER4};
class GamePadController {
class GamePadController final {
public:
static GamePadController& GetInstance()
@@ -66,7 +67,6 @@ private:
GamePadController();
std::vector<SDL_GameController*> m_pGameControllers;
std::vector<GamePad> m_GamePads;
};

View File

@@ -11,7 +11,7 @@
using namespace dae;
struct Sample
struct Sample final
{
Vector2 uv{};
Vector3 normal{};

View File

@@ -2,8 +2,8 @@
#include "InstancedMesh.h"
InstancedMesh::InstancedMesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices,
std::shared_ptr<Material> material, BaseEffect *effectPtr, const std::vector<InstancedData> &instanceData)
: m_EffectPtr(effectPtr),
std::shared_ptr<Material> material, std::unique_ptr<BaseEffect> effectPtr, const std::vector<InstancedData> &instanceData)
: m_EffectPtr(std::move(effectPtr)),
m_InputLayoutPtr(nullptr),
m_VertexBufferPtr(nullptr),
m_IndexBufferPtr(nullptr),
@@ -152,9 +152,6 @@ InstancedMesh::~InstancedMesh() {
m_InstanceBufferPtr = nullptr;
}
delete m_EffectPtr;
m_EffectPtr = nullptr;
m_Material.reset();
m_InstancedData.clear();

View File

@@ -13,10 +13,10 @@ struct alignas(16) InstancedData {
Vector4 color;
};
class InstancedMesh {
class InstancedMesh final {
public:
InstancedMesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices,
std::shared_ptr<Material> material, BaseEffect* effectPtr, const std::vector<InstancedData>& instanceData);
std::shared_ptr<Material> material, std::unique_ptr<BaseEffect> effectPtr, const std::vector<InstancedData>& instanceData);
~InstancedMesh();
@@ -26,7 +26,6 @@ public:
Matrix GetWorldMatrix() const;
Material* GetMaterial() const;
void SetWorldMatrix(const Matrix &matrix);
@@ -40,7 +39,7 @@ public:
void UpdateInstanceData(ID3D11DeviceContext* deviceContextPtr, const std::vector<InstancedData>& instanceData);
private:
BaseEffect *m_EffectPtr;
std::unique_ptr<BaseEffect> m_EffectPtr;
Matrix m_WorldMatrix{};

View File

@@ -3,7 +3,7 @@
#include "Texture.h"
struct Material {
struct Material final {
Texture *diffuseTexturePtr{nullptr};
Texture *normalTexturePtr{nullptr};
Texture *specularTexturePtr{nullptr};

View File

@@ -5,8 +5,8 @@
#include "Effects/Effect.h"
Mesh::Mesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices, std::shared_ptr<Material> material, BaseEffect* effectPtr) :
m_EffectPtr(effectPtr),
Mesh::Mesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices, std::shared_ptr<Material> material, std::unique_ptr<BaseEffect> effectPtr) :
m_EffectPtr(std::move(effectPtr)),
m_InputLayoutPtr(nullptr),
m_VertexBufferPtr(nullptr),
m_IndexBufferPtr(nullptr),
@@ -91,8 +91,6 @@ Mesh::~Mesh() {
m_IndexBufferPtr->Release();
m_IndexBufferPtr = nullptr;
delete m_EffectPtr;
m_EffectPtr = nullptr;
}
void Mesh::Render(ID3D11DeviceContext *deviceContextPtr, const Matrix &worldViewProj) const {

View File

@@ -19,12 +19,10 @@ enum class PrimitiveTopology {
};
class Mesh final {
public:
Mesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices,
std::shared_ptr<Material> material, BaseEffect* effectPtr);
std::shared_ptr<Material> material, std::unique_ptr<BaseEffect> effectPtr);
~Mesh();
@@ -54,7 +52,7 @@ public:
bool GetShouldRender() const { return m_ShouldRender; }
private:
BaseEffect *m_EffectPtr;
std::unique_ptr<BaseEffect> m_EffectPtr;
Matrix m_WorldMatrix{};

View File

@@ -36,14 +36,14 @@ namespace dae {
InitializeSDLRasterizer();
m_pScene = new MainScene();
m_pScene = std::make_unique<MainScene>();
m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr, nullptr);
if (!m_pScene->GetMeshes().empty()) {
m_pFireMesh = m_pScene->GetMeshes().back();
m_pFireMesh = m_pScene->GetMeshes().back().get();
}
float aspectRatio = static_cast<float>(m_Width) / static_cast<float>(m_Height);
const float aspectRatio = static_cast<float>(m_Width) / static_cast<float>(m_Height);
m_Camera = Camera({.0f, .0f, .0f}, 45.f);
m_Camera.Initialize(45.f, {.0f, .0f, .0f}, aspectRatio);
}
@@ -66,7 +66,6 @@ namespace dae {
m_DevicePtr->Release();
m_pScene->Cleanup();
delete m_pScene;
//SDL
SDL_FreeSurface(m_pBackBuffer);
@@ -79,7 +78,7 @@ namespace dae {
void Renderer::Update(const Timer *pTimer) {
m_Camera.Update(pTimer);
for (auto mesh: m_pScene->GetMeshes()) {
for (auto& mesh: m_pScene->GetMeshes()) {
mesh->SetCameraPos(m_Camera.GetPosition());
}
if (m_Rotating) {
@@ -89,7 +88,7 @@ namespace dae {
Matrix rotationMatrix = Matrix::CreateRotationY(rotationThisFrame);
for (auto mesh: m_pScene->GetMeshes()) {
for (auto& mesh: m_pScene->GetMeshes()) {
Matrix originalWorldMatrix = mesh->GetWorldMatrix();
Matrix world = rotationMatrix * originalWorldMatrix;
@@ -121,7 +120,7 @@ namespace dae {
m_DeviceContextPtr->ClearDepthStencilView(m_DepthStencilViewPtr, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
Matrix viewProjMatrix = m_Camera.GetViewProjectionMatrix();
for (auto mesh: m_pScene->GetMeshes()) {
for (auto& mesh: m_pScene->GetMeshes()) {
if (mesh->GetShouldRender()) {
Matrix modelMatrix = mesh->GetWorldMatrix();
Matrix worldViewProjMatrix = modelMatrix * viewProjMatrix;
@@ -149,14 +148,14 @@ namespace dae {
m_VerticiesScreenSpace.clear();
for (auto *currentMesh: m_pScene->GetMeshes()) {
for (auto& currentMesh: m_pScene->GetMeshes()) {
if (!currentMesh->GetShouldRender()) {
continue;
}
const Matrix worldViewProjectionMatrix{currentMesh->GetWorldMatrix() * m_Camera.GetViewProjectionMatrix()};
VertexTransformationFunction(worldViewProjectionMatrix, currentMesh, currentMesh->GetVertices(), m_VerticiesScreenSpace);
VertexTransformationFunction(worldViewProjectionMatrix, currentMesh.get(), currentMesh->GetVertices(), m_VerticiesScreenSpace);
int numTriangles{};
@@ -535,7 +534,7 @@ namespace dae {
}
void Renderer::CycleCullMode() {
for (auto mesh: m_pScene->GetMeshes()) {
for (auto& mesh: m_pScene->GetMeshes()) {
mesh->CycleCullMode();
}
@@ -604,7 +603,7 @@ namespace dae {
}
void Renderer::NextSamplingState() {
for (auto mesh: m_pScene->GetMeshes()) {
for (auto& mesh: m_pScene->GetMeshes()) {
mesh->NextSamplingState();
}
@@ -629,7 +628,7 @@ namespace dae {
void Renderer::ToggleNormals() {
m_useNormals = !m_useNormals;
for (auto mesh: m_pScene->GetMeshes()) {
for (auto& mesh: m_pScene->GetMeshes()) {
mesh->ToggleNormals();
}
std::string mode = m_useNormals ? "ON" : "OFF";
@@ -658,7 +657,6 @@ namespace dae {
void Renderer::NextScene() {
m_pScene->Cleanup();
delete m_pScene;
//Calculate the next scene
int index = static_cast<int>(m_CurrentScene);
@@ -667,27 +665,27 @@ namespace dae {
switch (m_CurrentScene) {
case SceneNames::Main:
m_pScene = new MainScene();
m_pScene = std::make_unique<MainScene>();
m_pFireMesh = nullptr;
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Main" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Diorama:
m_pScene = new DioramaScene();
m_pScene = std::make_unique<DioramaScene>();
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Diorama" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Instanced:
m_pScene = new InstancedScene();
m_pScene = std::make_unique<InstancedScene>();
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Instanced" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Planet:
m_pScene = new PlanetScene();
m_pScene = std::make_unique<PlanetScene>();
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Planet" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
@@ -705,7 +703,7 @@ namespace dae {
if (m_CurrentScene == SceneNames::Main) {
//Kind of sloppy fix but hey :p
m_pFireMesh = m_pScene->GetMeshes().back();
m_pFireMesh = m_pScene->GetMeshes().back().get();
}
}

View File

@@ -79,7 +79,7 @@ namespace dae
Backendtype m_backendType{ Backendtype::DirectX };
BaseScene* m_pScene{};
std::unique_ptr<BaseScene> m_pScene{};
SceneNames m_CurrentScene{ SceneNames::Main };
//Pos rot
std::unordered_map<SceneNames, std::pair<Vector3, Vector3>> m_SceneCameraPositions{};
@@ -90,7 +90,7 @@ namespace dae
float m_currentRotation{};
bool m_UseUniformClearColor{ false };
ColorRGB m_UniformClearColor{ ColorRGB{0.1f * 255.f, 0.1f * 255.f, 0.1 * 255.f} };
const ColorRGB m_UniformClearColor{ ColorRGB{0.1f * 255.f, 0.1f * 255.f, 0.1 * 255.f} };
//DirectX vars
@@ -100,7 +100,7 @@ namespace dae
HRESULT InitializeDirectX();
void RenderDirectX() const;
ColorRGB DirectXClearColor{ .39f * 255.f, .59f * 255.f, .93f * 255.f };
const ColorRGB DirectXClearColor{ .39f * 255.f, .59f * 255.f, .93f * 255.f };
//Storing the mesh so i can deactivate it;
Mesh* m_pFireMesh{};
@@ -134,7 +134,7 @@ namespace dae
bool m_isDepthBuffer{ false };
ColorRGB m_ClearColorSoftware{ .39f * 255.f, .39f * 255.f, .39f * 255.f };
const ColorRGB m_ClearColorSoftware{ .39f * 255.f, .39f * 255.f, .39f * 255.f };
float* m_pDepthBufferPixels{};

View File

@@ -25,7 +25,7 @@ public:
virtual void Cleanup() = 0;
virtual std::vector<Mesh*>& GetMeshes() = 0;
virtual std::vector<std::unique_ptr<Mesh>>& GetMeshes() = 0;
virtual std::vector<std::shared_ptr<Material>>& GetMaterials() = 0;
};

View File

@@ -15,19 +15,20 @@ void DioramaScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devi
std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes;
Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr);
for (const auto &mesh: materialMeshes) {
if (mesh->vertices.size() > 0) {
if (!mesh->vertices.empty()) {
std::shared_ptr<Material> material = std::make_shared<Material>();
BaseEffect *effect{nullptr};
std::unique_ptr<BaseEffect> effect{};
if (mesh->opacity_map != "") {
effect = new FireEffect(DevicePtr, L"resources/Fire.fx");
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 = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
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));
m_meshes.push_back(new Mesh(DevicePtr, mesh->vertices, mesh->indices, material, effect));
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateScale(2.f, 2.f, 2.f);
@@ -42,13 +43,15 @@ void DioramaScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devi
for (const auto &mesh: materialMeshes) {
if (!mesh->vertices.empty()) {
std::shared_ptr<Material> material = std::make_shared<Material>();
BaseEffect *effect{nullptr};
auto effect = std::make_unique<Effect>(DevicePtr, L"resources/SimpleDiffuse.fx");
effect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/brok/" + mesh->diffuse_texture, DevicePtr);
m_meshes.push_back(new Mesh(DevicePtr, mesh->vertices, mesh->indices, material, effect));
m_brokMeshses.push_back(m_meshes.back());
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);
@@ -81,7 +84,7 @@ void DioramaScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView
}
std::vector<Mesh *> &DioramaScene::GetMeshes() {
std::vector<std::unique_ptr<Mesh>> &DioramaScene::GetMeshes() {
return m_meshes;
}
@@ -90,9 +93,6 @@ std::vector<std::shared_ptr<Material>> &DioramaScene::GetMaterials() {
}
void DioramaScene::Cleanup() {
for (Mesh *mesh: m_meshes) {
delete mesh;
}
m_meshes.clear();
m_materials.clear();
}

View File

@@ -3,13 +3,13 @@
#include "BaseScene.h"
class DioramaScene : public BaseScene {
class DioramaScene final : public BaseScene {
public:
void Cleanup() override;
void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override;
std::vector<Mesh *> &GetMeshes() override;
std::vector<std::unique_ptr<Mesh>> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override;
@@ -18,7 +18,7 @@ public:
void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override;
private:
std::vector<Mesh *> m_meshes{};
std::vector<std::unique_ptr<Mesh>> m_meshes{};
std::vector<std::shared_ptr<Material>> m_materials{};
std::vector<Mesh*> m_brokMeshses{ nullptr };

View File

@@ -43,7 +43,9 @@ void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *De
float scale = 2;
//use the perlin noise to generate the YOffset
float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f);
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);
@@ -51,26 +53,20 @@ void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *De
}
}
auto *effect = new Effect(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
auto effect = std::make_unique<Effect>(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
effect->NextSamplingState();
effect->NextSamplingState(); //Dirty hack
m_instancedMeshes.push_back(new InstancedMesh(DevicePtr, vertices, indices, cubeMaterial, effect, instanceData));
auto cubeMesh = std::make_unique<InstancedMesh>(DevicePtr, vertices, indices, cubeMaterial, std::move(effect), instanceData);
m_instancedMeshes.push_back(std::move(cubeMesh));
}
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() {
std::vector<std::unique_ptr<Mesh>> &InstancedScene::GetMeshes() {
return m_meshes;
}
@@ -81,7 +77,7 @@ std::vector<std::shared_ptr<Material>> &InstancedScene::GetMaterials() {
void InstancedScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
Matrix viewProjMatrix = camera.GetViewProjectionMatrix();
for (auto mesh: m_instancedMeshes) {
for (auto& mesh: m_instancedMeshes) {
Matrix modelMatrix = mesh->GetWorldMatrix();
Matrix worldViewProjMatrix = modelMatrix * viewProjMatrix;
mesh->Render(devicePtr, worldViewProjMatrix);
@@ -96,8 +92,9 @@ void InstancedScene::Update() {
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;
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);

View File

@@ -6,7 +6,7 @@
#include "../InstancedMesh.h"
#include "../PerlinNoise.hpp"
class InstancedScene : public BaseScene {
class InstancedScene final : public BaseScene {
public:
void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override;
@@ -16,7 +16,7 @@ public:
void Cleanup() override;
std::vector<Mesh *> &GetMeshes() override;
std::vector<std::unique_ptr<Mesh>> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override;
@@ -24,9 +24,9 @@ private:
ID3D11DeviceContext* m_DeviceContextPtr{};
std::vector<Mesh*> m_meshes;
std::vector<std::unique_ptr<Mesh>> m_meshes;
//Kind of hack since InstancedMesh doesnt extend mesh
std::vector<InstancedMesh*> m_instancedMeshes;
std::vector<std::unique_ptr<InstancedMesh>> m_instancedMeshes;
std::vector<std::shared_ptr<Material>> m_materials;
const siv::PerlinNoise::seed_type seed = 123456u;

View File

@@ -26,8 +26,9 @@ void MainScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceC
vehicleMaterial->specularTexturePtr = Texture::LoadFromFile("resources/vehicle_specular.png", DevicePtr);
vehicleMaterial->glossTexturePtr = Texture::LoadFromFile("resources/vehicle_gloss.png", DevicePtr);
auto* effect = new Effect(DevicePtr, L"resources/PosCol3D.fx");
m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, vehicleMaterial, effect));
auto effect = std::make_unique<Effect>(DevicePtr, L"resources/PosCol3D.fx");
auto vehicleMesh = std::make_unique<Mesh>(DevicePtr, vertices, indices, vehicleMaterial, std::move(effect));
m_meshes.push_back(std::move(vehicleMesh));
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
@@ -45,8 +46,9 @@ void MainScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceC
std::shared_ptr<Material> FireMaterial = std::make_shared<Material>();
FireMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/fireFX_diffuse.png", DevicePtr);
auto* fireEffect = new FireEffect(DevicePtr, L"resources/Fire.fx");
m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, FireMaterial, fireEffect));
auto fireEffect = std::make_unique<FireEffect>(DevicePtr, L"resources/Fire.fx");
auto fireMesh = std::make_unique<Mesh>(DevicePtr, vertices, indices, FireMaterial, std::move(fireEffect));
m_meshes.push_back(std::move(fireMesh));
worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateTranslation(0, 0, 50.f);
@@ -55,13 +57,10 @@ void MainScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceC
void MainScene::Cleanup() {
for (Mesh* mesh : m_meshes) {
delete mesh;
}
m_meshes.clear();
}
std::vector<Mesh *> &MainScene::GetMeshes() {
std::vector<std::unique_ptr<Mesh>> &MainScene::GetMeshes() {
return m_meshes;
}

View File

@@ -10,13 +10,13 @@
#include <memory>
#include <vector>
class MainScene : public BaseScene {
class MainScene final : public BaseScene {
public:
void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override;
void Cleanup() override;
std::vector<Mesh *> &GetMeshes() override;
std::vector<std::unique_ptr<Mesh>> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override;
@@ -27,7 +27,7 @@ public:
private:
std::vector<Mesh*> m_meshes{};
std::vector<std::unique_ptr<Mesh>> m_meshes{};
std::vector<std::shared_ptr<Material>> m_materials{};
};

View File

@@ -60,8 +60,9 @@ void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devic
m_InstancedData.push_back(data);
}
auto *rockEffect = new Effect(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
m_instancedMeshes.push_back(new InstancedMesh(DevicePtr, vertices, indices, rockMaterial, rockEffect, m_InstancedData));
auto rockEffect = std::make_unique<Effect>(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
auto rockMesh = std::make_unique<InstancedMesh>(DevicePtr, vertices, indices, rockMaterial, std::move(rockEffect), m_InstancedData);
m_instancedMeshes.push_back(std::move(rockMesh));
indices.clear();
vertices.clear();
@@ -74,8 +75,10 @@ void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devic
std::shared_ptr<Material> planetMaterial = std::make_shared<Material>();
planetMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/planet/mars.png", DevicePtr);
auto* planetEffect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, planetMaterial, planetEffect));
auto planetEffect = std::make_unique<Effect>(DevicePtr, L"resources/SimpleDiffuse.fx");
auto planetMesh = std::make_unique<Mesh>(DevicePtr, vertices, indices, planetMaterial, std::move(planetEffect));
m_planetMesh = planetMesh.get();
m_meshes.push_back(std::move(planetMesh));
indices.clear();
vertices.clear();
@@ -88,9 +91,10 @@ void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devic
std::shared_ptr<Material> skyboxMaterial = std::make_shared<Material>();
skyboxMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/planet/skybox/space_nebula_6k.png", DevicePtr);
auto* skyboxEffect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, skyboxMaterial, skyboxEffect));
m_skyboxMesh = m_meshes.back();
auto skyboxEffect = std::make_unique<Effect>(DevicePtr, L"resources/SimpleDiffuse.fx");
auto skyBoxMesh = std::make_unique<Mesh>(DevicePtr, vertices, indices, skyboxMaterial, std::move(skyboxEffect));
m_meshes.push_back(std::move(skyBoxMesh));
m_skyboxMesh = m_meshes.back().get();
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateScale(0.1f, 0.1f, 0.1f);
@@ -98,19 +102,12 @@ void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devic
}
void PlanetScene::Cleanup() {
for (auto mesh: m_instancedMeshes) {
delete mesh;
}
m_instancedMeshes.clear();
for (auto mesh: m_meshes) {
delete mesh;
}
m_meshes.clear();
}
std::vector<Mesh *> &PlanetScene::GetMeshes() {
std::vector<std::unique_ptr<Mesh>> &PlanetScene::GetMeshes() {
return m_meshes;
}
@@ -121,7 +118,7 @@ std::vector<std::shared_ptr<Material>> &PlanetScene::GetMaterials() {
void PlanetScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
Matrix viewProjMatrix = camera.GetViewProjectionMatrix();
for (auto mesh: m_instancedMeshes) {
for (auto& mesh: m_instancedMeshes) {
Matrix modelMatrix = mesh->GetWorldMatrix();
Matrix worldViewProjMatrix = modelMatrix * viewProjMatrix;
mesh->Render(devicePtr, worldViewProjMatrix);
@@ -137,8 +134,13 @@ void PlanetScene::Update() {
//set the skybox to the camera
Matrix skyboxMatrix = m_skyboxMesh->GetWorldMatrix();
Vector3 cameraPos = m_Camera->GetPosition();
skyboxMatrix.SetTranslation(cameraPos);
m_skyboxMesh->SetWorldMatrix(skyboxMatrix);
//Rotate the planet slowly
Matrix planetMatrix = m_planetMesh->GetWorldMatrix();
planetMatrix *= Matrix::CreateRotationY(-0.0001f);
m_planetMesh->SetWorldMatrix(planetMatrix);
}

View File

@@ -6,7 +6,7 @@
#include "BaseScene.h"
#include "../InstancedMesh.h"
class PlanetScene : public BaseScene {
class PlanetScene final : public BaseScene {
public:
void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override;
@@ -16,7 +16,7 @@ public:
void Cleanup() override;
std::vector<Mesh *> &GetMeshes() override;
std::vector<std::unique_ptr<Mesh>> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override;
@@ -24,13 +24,14 @@ private:
ID3D11DeviceContext* m_DeviceContextPtr{};
std::vector<Mesh*> m_meshes;
std::vector<std::unique_ptr<Mesh>> m_meshes;
//Kind of hack since InstancedMesh doesnt extend mesh
std::vector<InstancedMesh*> m_instancedMeshes;
std::vector<std::unique_ptr<InstancedMesh>> m_instancedMeshes;
std::vector<InstancedData> m_InstancedData;
std::vector<std::shared_ptr<Material>> m_materials;
Mesh* m_skyboxMesh;
Mesh* m_planetMesh;
Camera* m_Camera;
};

View File

@@ -12,7 +12,7 @@
using namespace dae;
class Texture {
class Texture final {
public:
~Texture();
@@ -28,7 +28,7 @@ private:
ID3D11ShaderResourceView* m_TextureResourceViewPtr{};
ID3D11Texture2D* m_TexturePtr{};
SDL_Surface* m_SurfacePtr{ nullptr };
const SDL_Surface* m_SurfacePtr{ nullptr };
uint32_t* m_pSurfacePixels{ nullptr };
};

View File

@@ -5,7 +5,7 @@
namespace dae
{
class Timer
class Timer final
{
public:
Timer();

View File

@@ -396,3 +396,13 @@ namespace dae {
}
}
template<typename T>
class DirectXDeleter
{
public:
void operator()(T* p)
{
p->Release();
}
};

View File

@@ -129,8 +129,11 @@ int main(int argc, char *args[]) {
//Create window + surfaces
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
const uint32_t width = 640;
const uint32_t height = 480;
const uint32_t width = 1600;
const uint32_t height = 900;
//// const uint32_t width = 640;
//// const uint32_t height = 480;
SDL_Window *pWindow = SDL_CreateWindow(