Major cleanup.

Moved to namespace
Cleanup includes
This commit is contained in:
Antoine Pilote
2021-07-11 18:56:44 -04:00
parent f77bef6b28
commit 0c6ed48bbd
174 changed files with 11772 additions and 11296 deletions

View File

@@ -6,23 +6,26 @@
#include "../Rendering/Mesh/Mesh.h"
#include <src/Core/Physics/Rigibody.h>
class BSPBrushComponent {
public:
std::vector<Ref<Mesh>> Meshes;
std::vector< Ref<Material>> Materials;
std::vector<Ref<Physics::RigidBody>> Rigidbody;
namespace Nuake
{
class BSPBrushComponent {
public:
std::vector<Ref<Mesh>> Meshes;
std::vector< Ref<Material>> Materials;
std::vector<Ref<Physics::RigidBody>> Rigidbody;
std::string target = "";
std::vector<Entity> Targets;
std::string target = "";
std::vector<Entity> Targets;
bool IsSolid = true;
bool IsTrigger = false;
bool IsTransparent = false;
bool IsFunc = false;
bool IsSolid = true;
bool IsTrigger = false;
bool IsTransparent = false;
bool IsFunc = false;
BSPBrushComponent() {
Meshes = std::vector<Ref<Mesh>>();
Materials = std::vector<Ref<Material>>();
Rigidbody = std::vector<Ref<Physics::RigidBody>>();
}
};
BSPBrushComponent() {
Meshes = std::vector<Ref<Mesh>>();
Materials = std::vector<Ref<Material>>();
Rigidbody = std::vector<Ref<Physics::RigidBody>>();
}
};
}

View File

@@ -1,11 +1,13 @@
#pragma once
#include "../Core/Physics/PhysicsShapes.h"
#include "../Core/Core.h"
#include "src/Core/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
class BoxColliderComponent
{
public:
Ref<Physics::PhysicShape> Box;
glm::vec3 Size = glm::vec3(0.5f, 0.5f, 0.5f);
bool IsTrigger;
};
namespace Nuake {
class BoxColliderComponent
{
public:
Ref<Physics::PhysicShape> Box;
glm::vec3 Size = glm::vec3(0.5f, 0.5f, 0.5f);
bool IsTrigger;
};
}

View File

@@ -1,16 +1,19 @@
#pragma once
#include "CameraComponent.h"
#include <src/Rendering/Camera.h>
#include "src/Rendering/Camera.h"
#include "src/Scene/Entities/ImGuiHelper.h"
CameraComponent::CameraComponent()
{
CameraInstance = CreateRef<Camera>();
}
namespace Nuake {
CameraComponent::CameraComponent()
{
CameraInstance = CreateRef<Camera>();
}
void CameraComponent::DrawEditor() {
ImGui::Text("Camera");
ImGui::SliderFloat("Exposure", &CameraInstance->Exposure, 0.0f, 2.0f, "%.2f", 1.0f);
ImGui::SliderFloat("FOV", &CameraInstance->Fov, 1.0f, 180.0f, "%.2f", 1.0f);
ImGui::SliderFloat("Speed", &CameraInstance->Speed, 0.1f, 5.0f, "%.2f", 1.0f);
}
void CameraComponent::DrawEditor()
{
ImGui::Text("Camera");
ImGui::SliderFloat("Exposure", &CameraInstance->Exposure, 0.0f, 2.0f, "%.2f", 1.0f);
ImGui::SliderFloat("FOV", &CameraInstance->Fov, 1.0f, 180.0f, "%.2f", 1.0f);
ImGui::SliderFloat("Speed", &CameraInstance->Speed, 0.1f, 5.0f, "%.2f", 1.0f);
}
}

View File

@@ -1,29 +1,33 @@
#pragma once
#include "TransformComponent.h"
#include "../Core/Core.h"
#include "../Resource/Serializable.h"
#include "../Rendering/Camera.h"
#include "src/Core/Core.h"
#include "src/Resource/Serializable.h"
#include "src/Rendering/Camera.h"
class CameraComponent {
public:
Ref<Camera> CameraInstance;
TransformComponent* transformComponent;
CameraComponent();
void DrawEditor();
json Serialize()
namespace Nuake
{
class CameraComponent
{
BEGIN_SERIALIZE();
SERIALIZE_OBJECT(CameraInstance);
END_SERIALIZE();
}
public:
Ref<Camera> CameraInstance;
TransformComponent* transformComponent;
bool Deserialize(std::string str)
{
CameraInstance = CreateRef<Camera>();
CameraComponent();
return CameraInstance->Deserialize(str);
}
};
void DrawEditor();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_OBJECT(CameraInstance);
END_SERIALIZE();
}
bool Deserialize(std::string str)
{
CameraInstance = CreateRef<Camera>();
return CameraInstance->Deserialize(str);
}
};
}

View File

@@ -1,40 +1,42 @@
#pragma once
#include "src/Core/Physics/CharacterController.h"
#include "../Core/Physics/CharacterController.h"
class CharacterControllerComponent
{
public:
Ref < Physics::CharacterController> CharacterController;
float Height = 1.0f;
float Radius = 0.2f;
float Mass = 25.0f;
CharacterControllerComponent()
namespace Nuake {
class CharacterControllerComponent
{
}
public:
Ref < Physics::CharacterController> CharacterController;
json Serialize() {
BEGIN_SERIALIZE();
SERIALIZE_VAL(Height);
SERIALIZE_VAL(Radius);
SERIALIZE_VAL(Mass);
END_SERIALIZE();
}
float Height = 1.0f;
float Radius = 0.2f;
float Mass = 25.0f;
bool Deserialize(const std::string str) {
BEGIN_DESERIALIZE();
Height = j["Height"];
Radius = j["Radius"];
Mass = j["Mass"];
return true;
}
void SyncWithTransform(TransformComponent& tc)
{
btVector3 pos = CharacterController->m_motionTransform.getOrigin();
glm::vec3 finalPos = glm::vec3(pos.x(), pos.y(), pos.z());
CharacterControllerComponent()
{
tc.Translation = finalPos;
}
};
}
json Serialize() {
BEGIN_SERIALIZE();
SERIALIZE_VAL(Height);
SERIALIZE_VAL(Radius);
SERIALIZE_VAL(Mass);
END_SERIALIZE();
}
bool Deserialize(const std::string str) {
BEGIN_DESERIALIZE();
Height = j["Height"];
Radius = j["Radius"];
Mass = j["Mass"];
return true;
}
void SyncWithTransform(TransformComponent& tc)
{
btVector3 pos = CharacterController->m_motionTransform.getOrigin();
glm::vec3 finalPos = glm::vec3(pos.x(), pos.y(), pos.z());
tc.Translation = finalPos;
}
};
}

View File

@@ -6,141 +6,144 @@
#include <GL\glew.h>
#include "../Core/Core.h"
#include <src/Scene/Entities/ImGuiHelper.h>
LightComponent::LightComponent()
{
Color = glm::vec3(1, 1, 1);
Strength = 10.0f;
Direction = glm::vec3(0, -1, 0);
//m_Framebuffers = std::vector<Ref<FrameBuffer>>();
//mViewProjections = std::vector<glm::mat4>();
//mCascadeSplitDepth = std::vector<float>();
//mCascadeSplits = std::vector<float>();
// Framebuffer used for shadow mapping.
//for (int i = 0; i < 4; i++)
//{
// m_Framebuffers[i] = CreateRef<FrameBuffer>(false, glm::vec2(4096, 4096));
// m_Framebuffers[i]->SetTexture(CreateRef<Texture>(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT);
//}
}
void LightComponent::SetCastShadows(bool toggle)
{
CastShadows = toggle;
if (CastShadows)
namespace Nuake {
LightComponent::LightComponent()
{
for (int i = 0; i < 4; i++)
{
m_Framebuffers[i] = CreateRef<FrameBuffer>(false, glm::vec2(4096, 4096));
m_Framebuffers[i]->SetTexture(CreateRef<Texture>(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT);
}
Color = glm::vec3(1, 1, 1);
Strength = 10.0f;
Direction = glm::vec3(0, -1, 0);
//m_Framebuffers = std::vector<Ref<FrameBuffer>>();
//mViewProjections = std::vector<glm::mat4>();
//mCascadeSplitDepth = std::vector<float>();
//mCascadeSplits = std::vector<float>();
// Framebuffer used for shadow mapping.
//for (int i = 0; i < 4; i++)
//{
// m_Framebuffers[i] = CreateRef<FrameBuffer>(false, glm::vec2(4096, 4096));
// m_Framebuffers[i]->SetTexture(CreateRef<Texture>(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT);
//}
}
else {
for (int i = 0; i < 4; i++)
{
m_Framebuffers[i] = nullptr;
}
}
}
glm::mat4 LightComponent::GetProjection()
{
return glm::ortho(-25.0f, 25.0f, -25.0f, 25.0f, -25.0f, 25.0f);
}
void LightComponent::SetDirection(glm::vec3 dir)
{
}
glm::vec3 LightComponent::GetDirection()
{
//glm::mat4 start = glm::mat4(1.0f);
//glm::vec3 defaultDirection(0, 0, 1); // forward
//
//start = glm::rotate(start, glm::radians(Direction.x), glm::vec3(1, 0, 0));
//start = glm::rotate(start, glm::radians(Direction.y), glm::vec3(0, 1, 0));
//start = glm::rotate(start, glm::radians(Direction.z), glm::vec3(0, 0, 1));
//return glm::vec3(start * glm::vec4(defaultDirection, 1.0f));
return Direction;
}
void LightComponent::BeginDrawShadow()
{
Renderer::m_ShadowmapShader->Bind();
//m_Framebuffer->Bind();
// Render scene...
}
void LightComponent::EndDrawShadow()
{
//m_Framebuffer->Unbind();
}
void LightComponent::DrawShadow()
{
if (Type != Directional)
return;
Renderer::m_ShadowmapShader->Bind();
}
void LightComponent::Draw(TransformComponent transformComponent, Ref<Camera> cam)
{
Renderer::RegisterLight(transformComponent, *this, cam);
}
void LightComponent::DrawDeferred(TransformComponent transformComponent, Camera* cam)
{
Renderer::RegisterDeferredLight(transformComponent, *this, cam);
}
void LightComponent::DrawEditor() {
ImGui::TextColored(ImGui::GetStyleColorVec4(1), "Light properties");
ImGui::ColorEdit3("Light Color", &Color.r);
ImGui::SliderFloat("Strength", &Strength, 0.0f, 50.0f);
bool before = CastShadows;
ImGui::Checkbox("Cast shadows", &CastShadows);
if (CastShadows && before == false)
SetCastShadows(true);
const char* types[] = { "Directional", "Point", "Spot" };
static const char* current_item = types[Type];
if (ImGui::BeginCombo("Type", current_item)) // The second parameter is the label previewed before opening the combo.
void LightComponent::SetCastShadows(bool toggle)
{
for (int n = 0; n < IM_ARRAYSIZE(types); n++)
CastShadows = toggle;
if (CastShadows)
{
bool is_selected = (current_item == types[n]); // You can store your selection however you want, outside or inside your objects
if (ImGui::Selectable(types[n], is_selected)) {
current_item = types[n];
Type = (LightType)n;
for (int i = 0; i < 4; i++)
{
m_Framebuffers[i] = CreateRef<FrameBuffer>(false, glm::vec2(4096, 4096));
m_Framebuffers[i]->SetTexture(CreateRef<Texture>(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT);
}
}
else {
for (int i = 0; i < 4; i++)
{
m_Framebuffers[i] = nullptr;
}
if (is_selected)
ImGui::SetItemDefaultFocus(); // You may set the initial focus when opening the combo (scrolling + for keyboard navigation support)
}
ImGui::EndCombo();
}
if (Type == Directional) {
ImGui::Checkbox("Sync with sky", &SyncDirectionWithSky);
ImGui::Checkbox("Volumetric?", &IsVolumetric);
ImGuiHelper::DrawVec3("Direction", &Direction);
glm::mat4 LightComponent::GetProjection()
{
return glm::ortho(-25.0f, 25.0f, -25.0f, 25.0f, -25.0f, 25.0f);
}
void LightComponent::SetDirection(glm::vec3 dir)
{
}
glm::vec3 LightComponent::GetDirection()
{
//glm::mat4 start = glm::mat4(1.0f);
//glm::vec3 defaultDirection(0, 0, 1); // forward
//
//start = glm::rotate(start, glm::radians(Direction.x), glm::vec3(1, 0, 0));
//start = glm::rotate(start, glm::radians(Direction.y), glm::vec3(0, 1, 0));
//start = glm::rotate(start, glm::radians(Direction.z), glm::vec3(0, 0, 1));
//return glm::vec3(start * glm::vec4(defaultDirection, 1.0f));
return Direction;
}
void LightComponent::BeginDrawShadow()
{
Renderer::m_ShadowmapShader->Bind();
//m_Framebuffer->Bind();
// Render scene...
}
void LightComponent::EndDrawShadow()
{
//m_Framebuffer->Unbind();
}
void LightComponent::DrawShadow()
{
if (Type != Directional)
return;
Renderer::m_ShadowmapShader->Bind();
}
void LightComponent::Draw(TransformComponent transformComponent, Ref<Camera> cam)
{
Renderer::RegisterLight(transformComponent, *this, cam);
}
void LightComponent::DrawDeferred(TransformComponent transformComponent, Camera* cam)
{
Renderer::RegisterDeferredLight(transformComponent, *this, cam);
}
void LightComponent::DrawEditor() {
ImGui::TextColored(ImGui::GetStyleColorVec4(1), "Light properties");
ImGui::ColorEdit3("Light Color", &Color.r);
ImGui::SliderFloat("Strength", &Strength, 0.0f, 50.0f);
bool before = CastShadows;
ImGui::Checkbox("Cast shadows", &CastShadows);
if (CastShadows && before == false)
SetCastShadows(true);
const char* types[] = { "Directional", "Point", "Spot" };
static const char* current_item = types[Type];
if (ImGui::BeginCombo("Type", current_item)) // The second parameter is the label previewed before opening the combo.
{
for (int n = 0; n < IM_ARRAYSIZE(types); n++)
{
bool is_selected = (current_item == types[n]); // You can store your selection however you want, outside or inside your objects
if (ImGui::Selectable(types[n], is_selected)) {
current_item = types[n];
Type = (LightType)n;
}
if (is_selected)
ImGui::SetItemDefaultFocus(); // You may set the initial focus when opening the combo (scrolling + for keyboard navigation support)
}
ImGui::EndCombo();
}
if (Type == Directional) {
ImGui::Checkbox("Sync with sky", &SyncDirectionWithSky);
ImGui::Checkbox("Volumetric?", &IsVolumetric);
ImGuiHelper::DrawVec3("Direction", &Direction);
}
//if (Type == 1) {
// ImGui::SliderFloat("Attenuation", &Attenuation, 0.0f, 1.0f);
// ImGui::SliderFloat("Linear attenuation", &LinearAttenuation, 0.0f, 1.0f);
// ImGui::SliderFloat("Quadratic attenuation", &QuadraticAttenuation, 0.0f, 1.0f);
//}
//Direction = glm::normalize(Direction);
}
//if (Type == 1) {
// ImGui::SliderFloat("Attenuation", &Attenuation, 0.0f, 1.0f);
// ImGui::SliderFloat("Linear attenuation", &LinearAttenuation, 0.0f, 1.0f);
// ImGui::SliderFloat("Quadratic attenuation", &QuadraticAttenuation, 0.0f, 1.0f);
//}
//Direction = glm::normalize(Direction);
}

View File

@@ -3,204 +3,209 @@
#include <glm\ext\vector_float2.hpp>
#include "TransformComponent.h"
#include "../Rendering/Camera.h"
#include "../Rendering/Framebuffer.h"
#include "src/Rendering/Buffers/Framebuffer.h"
#include "BaseComponent.h"
#include "../Resource/Serializable.h"
#include <glm\ext\matrix_clip_space.hpp>
enum LightType {
Directional, Point, Spot
};
class LightComponent {
public:
LightType Type = Point;
glm::vec3 Direction = glm::vec3(0, -1, 0);
glm::vec3 Color;
bool IsVolumetric = false;
float Strength;
bool SyncDirectionWithSky = false;
Ref<FrameBuffer> m_Framebuffer;
bool CastShadows = false;
float Attenuation = 0.0f;
float LinearAttenuation = 0.0f;
float QuadraticAttenuation = 0.0f;
Ref<FrameBuffer> m_Framebuffers[4];
glm::mat4 mViewProjections[4];
float mCascadeSplitDepth[4];
LightComponent();
void SetCastShadows(bool toggle);
glm::mat4 GetProjection();
glm::mat4 GetLightTransform();
void SetDirection(glm::vec3 dir);
glm::vec3 GetDirection();
void BeginDrawShadow();
void EndDrawShadow();
void DrawShadow();
void Draw(TransformComponent transformComponent, Ref<Camera> cam);
void DrawDeferred(TransformComponent transformComponent, Camera* cam);
void DrawEditor();
void SetType(LightType type);
float mCascadeSplits[4];
void CalculateViewProjection(glm::mat4& view, const glm::mat4& projection)
namespace Nuake
{
enum LightType
{
glm::mat4 viewProjection = projection * view;
glm::mat4 inverseViewProjection = glm::inverse(viewProjection);
Directional, Point, Spot
};
// TODO: Automate this
const float nearClip = 0.01f;
const float farClip = 1000.0f;
const float clipRange = farClip - nearClip;
class LightComponent
{
public:
LightType Type = Point;
glm::vec3 Direction = glm::vec3(0, -1, 0);
glm::vec3 Color;
bool IsVolumetric = false;
float Strength;
bool SyncDirectionWithSky = false;
Ref<FrameBuffer> m_Framebuffer;
const float mCascadeNearPlaneOffset = 0.0;
const float mCascadeFarPlaneOffset = 0.0;
bool CastShadows = false;
float Attenuation = 0.0f;
float LinearAttenuation = 0.0f;
float QuadraticAttenuation = 0.0f;
// Calculate the optimal cascade distances
const float minZ = nearClip;
const float maxZ = nearClip + clipRange;
const float range = maxZ - minZ;
const float ratio = maxZ / minZ;
for (int i = 0; i < 4; i++)
Ref<FrameBuffer> m_Framebuffers[4];
glm::mat4 mViewProjections[4];
float mCascadeSplitDepth[4];
LightComponent();
void SetCastShadows(bool toggle);
glm::mat4 GetProjection();
glm::mat4 GetLightTransform();
void SetDirection(glm::vec3 dir);
glm::vec3 GetDirection();
void BeginDrawShadow();
void EndDrawShadow();
void DrawShadow();
void Draw(TransformComponent transformComponent, Ref<Camera> cam);
void DrawDeferred(TransformComponent transformComponent, Camera* cam);
void DrawEditor();
void SetType(LightType type);
float mCascadeSplits[4];
void CalculateViewProjection(glm::mat4& view, const glm::mat4& projection)
{
const float p = (i + 1) / static_cast<float>(4);
const float log = minZ * glm::pow(ratio, p);
const float uniform = minZ + range * p;
const float d = 0.91f * (log - uniform) + uniform;
mCascadeSplits[i] = (d - nearClip) / clipRange;
}
glm::mat4 viewProjection = projection * view;
glm::mat4 inverseViewProjection = glm::inverse(viewProjection);
//mCascadeSplits[0] = 0.2f;
//mCascadeSplits[1] = 0.45f;
//mCascadeSplits[2] = 1.0f;
// TODO: Automate this
const float nearClip = 0.01f;
const float farClip = 1000.0f;
const float clipRange = farClip - nearClip;
float lastSplitDist = 0.0f;
// Calculate Orthographic Projection matrix for each cascade
for (int cascade = 0; cascade < 4; cascade++)
{
float splitDist = mCascadeSplits[cascade];
glm::vec4 frustumCorners[8] =
{
//Near face
{ 1.0f, 1.0f, -1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f },
const float mCascadeNearPlaneOffset = 0.0;
const float mCascadeFarPlaneOffset = 0.0;
//Far face
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f },
};
// Project frustum corners into world space from clip space
for (int i = 0; i < 8; i++)
{
glm::vec4 invCorner = inverseViewProjection * frustumCorners[i];
frustumCorners[i] = invCorner / invCorner.w;
}
// Calculate the optimal cascade distances
const float minZ = nearClip;
const float maxZ = nearClip + clipRange;
const float range = maxZ - minZ;
const float ratio = maxZ / minZ;
for (int i = 0; i < 4; i++)
{
glm::vec4 dist = frustumCorners[i + 4] - frustumCorners[i];
frustumCorners[i + 4] = frustumCorners[i] + (dist * splitDist);
frustumCorners[i] = frustumCorners[i] + (dist * lastSplitDist);
const float p = (i + 1) / static_cast<float>(4);
const float log = minZ * glm::pow(ratio, p);
const float uniform = minZ + range * p;
const float d = 0.91f * (log - uniform) + uniform;
mCascadeSplits[i] = (d - nearClip) / clipRange;
}
// Get frustum center
glm::vec3 frustumCenter = glm::vec3(0.0f);
for (int i = 0; i < 8; i++)
frustumCenter += glm::vec3(frustumCorners[i]);
frustumCenter /= 8.0f;
//mCascadeSplits[0] = 0.2f;
//mCascadeSplits[1] = 0.45f;
//mCascadeSplits[2] = 1.0f;
// Get the minimum and maximum extents
float radius = 0.0f;
for (int i = 0; i < 8; i++)
float lastSplitDist = 0.0f;
// Calculate Orthographic Projection matrix for each cascade
for (int cascade = 0; cascade < 4; cascade++)
{
float distance = glm::length(glm::vec3(frustumCorners[i]) - frustumCenter);
radius = glm::max(radius, distance);
float splitDist = mCascadeSplits[cascade];
glm::vec4 frustumCorners[8] =
{
//Near face
{ 1.0f, 1.0f, -1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f },
//Far face
{ 1.0f, 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f },
};
// Project frustum corners into world space from clip space
for (int i = 0; i < 8; i++)
{
glm::vec4 invCorner = inverseViewProjection * frustumCorners[i];
frustumCorners[i] = invCorner / invCorner.w;
}
for (int i = 0; i < 4; i++)
{
glm::vec4 dist = frustumCorners[i + 4] - frustumCorners[i];
frustumCorners[i + 4] = frustumCorners[i] + (dist * splitDist);
frustumCorners[i] = frustumCorners[i] + (dist * lastSplitDist);
}
// Get frustum center
glm::vec3 frustumCenter = glm::vec3(0.0f);
for (int i = 0; i < 8; i++)
frustumCenter += glm::vec3(frustumCorners[i]);
frustumCenter /= 8.0f;
// Get the minimum and maximum extents
float radius = 0.0f;
for (int i = 0; i < 8; i++)
{
float distance = glm::length(glm::vec3(frustumCorners[i]) - frustumCenter);
radius = glm::max(radius, distance);
}
radius = std::ceil(radius * 16.0f) / 16.0f;
glm::vec3 maxExtents = glm::vec3(radius);
glm::vec3 minExtents = -maxExtents;
// Calculate the view and projection matrix
glm::vec3 lightDir = -this->Direction;
glm::mat4 lightViewMatrix = glm::lookAt(frustumCenter - lightDir * -minExtents.z, frustumCenter, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 lightProjectionMatrix = glm::ortho(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f + mCascadeNearPlaneOffset, maxExtents.z - minExtents.z + mCascadeFarPlaneOffset);
// Offset to texel space to avoid shimmering ->(https://stackoverflow.com/questions/33499053/cascaded-shadow-map-shimmering)
glm::mat4 shadowMatrix = lightProjectionMatrix * lightViewMatrix;
const float ShadowMapResolution = 4096;
glm::vec4 shadowOrigin = (shadowMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)) * ShadowMapResolution / 2.0f;
glm::vec4 roundedOrigin = glm::round(shadowOrigin);
glm::vec4 roundOffset = roundedOrigin - shadowOrigin;
roundOffset = roundOffset * 2.0f / ShadowMapResolution;
roundOffset.z = 0.0f;
roundOffset.w = 0.0f;
lightProjectionMatrix[3] += roundOffset;
// Store SplitDistance and ViewProjection-Matrix
mCascadeSplitDepth[cascade] = (nearClip + splitDist * clipRange) * 1.0f;
mViewProjections[cascade] = lightProjectionMatrix * lightViewMatrix;
lastSplitDist = mCascadeSplits[cascade];
// -----------------------Debug only-----------------------
// RendererDebug::BeginScene(viewProjection);
// RendererDebug::SubmitCameraFrustum(frustumCorners, glm::mat4(1.0f), GetColor(cascade)); // Draws the divided camera frustums
// RendererDebug::SubmitLine(glm::vec3(0.0f, 0.0f, 0.0f), frustumCenter, GetColor(cascade)); // Draws the center of the frustum (A line pointing from origin to the center)
// RendererDebug::EndScene();
}
radius = std::ceil(radius * 16.0f) / 16.0f;
glm::vec3 maxExtents = glm::vec3(radius);
glm::vec3 minExtents = -maxExtents;
// Calculate the view and projection matrix
glm::vec3 lightDir = -this->Direction;
glm::mat4 lightViewMatrix = glm::lookAt(frustumCenter - lightDir * -minExtents.z, frustumCenter, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 lightProjectionMatrix = glm::ortho(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f + mCascadeNearPlaneOffset, maxExtents.z - minExtents.z + mCascadeFarPlaneOffset);
// Offset to texel space to avoid shimmering ->(https://stackoverflow.com/questions/33499053/cascaded-shadow-map-shimmering)
glm::mat4 shadowMatrix = lightProjectionMatrix * lightViewMatrix;
const float ShadowMapResolution = 4096;
glm::vec4 shadowOrigin = (shadowMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)) * ShadowMapResolution / 2.0f;
glm::vec4 roundedOrigin = glm::round(shadowOrigin);
glm::vec4 roundOffset = roundedOrigin - shadowOrigin;
roundOffset = roundOffset * 2.0f / ShadowMapResolution;
roundOffset.z = 0.0f;
roundOffset.w = 0.0f;
lightProjectionMatrix[3] += roundOffset;
// Store SplitDistance and ViewProjection-Matrix
mCascadeSplitDepth[cascade] = (nearClip + splitDist * clipRange) * 1.0f;
mViewProjections[cascade] = lightProjectionMatrix * lightViewMatrix;
lastSplitDist = mCascadeSplits[cascade];
// -----------------------Debug only-----------------------
// RendererDebug::BeginScene(viewProjection);
// RendererDebug::SubmitCameraFrustum(frustumCorners, glm::mat4(1.0f), GetColor(cascade)); // Draws the divided camera frustums
// RendererDebug::SubmitLine(glm::vec3(0.0f, 0.0f, 0.0f), frustumCenter, GetColor(cascade)); // Draws the center of the frustum (A line pointing from origin to the center)
// RendererDebug::EndScene();
}
}
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Type);
SERIALIZE_VEC3(Direction);
SERIALIZE_VEC3(Color);
SERIALIZE_VAL(IsVolumetric);
SERIALIZE_VAL(Strength);
SERIALIZE_VAL(SyncDirectionWithSky);
SERIALIZE_VAL(CastShadows);
END_SERIALIZE();
}
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Type);
SERIALIZE_VEC3(Direction);
SERIALIZE_VEC3(Color);
SERIALIZE_VAL(IsVolumetric);
SERIALIZE_VAL(Strength);
SERIALIZE_VAL(SyncDirectionWithSky);
SERIALIZE_VAL(CastShadows);
END_SERIALIZE();
}
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
if (j.contains("Type"))
Type = (LightType)j["Type"];
if (j.contains("IsVolumetric"))
IsVolumetric = j["IsVolumetric"];
if (j.contains("Strength"))
Strength = j["Strength"];
if (j.contains("SyncDirectionWithSky"))
SyncDirectionWithSky = j["SyncDirectionWithSky"];
if (j.contains("CastShadows"))
SetCastShadows(j["CastShadows"]);
if (j.contains("Direction"))
{
float x = j["Direction"]["x"];
float y = j["Direction"]["y"];
float z = j["Direction"]["z"];
this->Direction = Vector3(x, y, z);
}
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
if (j.contains("Type"))
Type = (LightType)j["Type"];
if (j.contains("IsVolumetric"))
IsVolumetric = j["IsVolumetric"];
if (j.contains("Strength"))
Strength = j["Strength"];
if (j.contains("SyncDirectionWithSky"))
SyncDirectionWithSky = j["SyncDirectionWithSky"];
if (j.contains("CastShadows"))
SetCastShadows(j["CastShadows"]);
if (j.contains("Direction"))
{
float x = j["Direction"]["x"];
float y = j["Direction"]["y"];
float z = j["Direction"]["z"];
this->Direction = Vector3(x, y, z);
}
return true;
}
};
return true;
}
};
}

View File

@@ -1,11 +1,13 @@
#pragma once
#include "../Core/Physics/PhysicsShapes.h"
#include "../Core/Core.h"
#include "src/Core/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
class MeshColliderComponent
{
public:
Ref<Physics::MeshShape> MeshShape;
Ref<Mesh> Mesh;
bool IsTrigger;
};
namespace Nuake {
class MeshColliderComponent
{
public:
Ref<Physics::MeshShape> MeshShape;
Ref<Mesh> Mesh;
bool IsTrigger;
};
}

View File

@@ -1,278 +1,279 @@
#include "MeshComponent.h"
#include <GL\glew.h>
#include "../../../Rendering/Renderer.h"
#include "../../../Rendering/Vertex.h"
#include "../../../Core/MaterialManager.h"
#include "src/Rendering/Renderer.h"
#include "src/Rendering/Vertex.h"
#include "src/Core/MaterialManager.h"
#include <imgui\imgui.h>
// TODO: This is a pile of crap.
// TODO: MOVE TO PRIMITIVE
Vertex vertices[] = {
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
namespace Nuake{
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
// TODO: MOVE TO PRIMITIVE
Vertex vertices[] = {
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0), glm::vec3(-1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 0, 1), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
};
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, -1), 1.0f },
void MeshComponent::LoadModel(const std::string path) {
//Assimp::Importer import;
//const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
//
//if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
//{
// printf("ERROR::ASSIMP::");
// return;
//}
//
//ProcessNode(scene->mRootNode, scene);
}
unsigned int sphereVAO = 0;
unsigned int indexCount;
void MeshComponent::RenderSphere() {
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, -0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, -1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
if (sphereVAO == 0)
{
glGenVertexArrays(1, &sphereVAO);
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, -0.5f), glm::vec2(1.0f, 1.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(0.5f, 0.5f, 0.5f), glm::vec2(1.0f, 0.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec2(0.0f, 0.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
Vertex{ glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec2(0.0f, 1.0f), glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::vec3(0, 0, 1), 1.0f },
};
unsigned int vbo, ebo;
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
void MeshComponent::LoadModel(const std::string path) {
//Assimp::Importer import;
//const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
//
//if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
//{
// printf("ERROR::ASSIMP::");
// return;
//}
//
//ProcessNode(scene->mRootNode, scene);
}
unsigned int sphereVAO = 0;
unsigned int indexCount;
void MeshComponent::RenderSphere() {
std::vector<glm::vec3> positions;
std::vector<glm::vec2> uv;
std::vector<glm::vec3> normals;
std::vector<unsigned int> indices;
const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
if (sphereVAO == 0)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS;
float xPos = std::cos(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
float yPos = std::cos(ySegment * PI);
float zPos = std::sin(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
glGenVertexArrays(1, &sphereVAO);
positions.push_back(glm::vec3(xPos, yPos, zPos));
uv.push_back(glm::vec2(xSegment, ySegment));
normals.push_back(glm::vec3(xPos, yPos, zPos));
}
}
unsigned int vbo, ebo;
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
bool oddRow = false;
for (unsigned int y = 0; y < Y_SEGMENTS; ++y)
{
if (!oddRow) // even rows: y == 0, y == 2; and so on
std::vector<glm::vec3> positions;
std::vector<glm::vec2> uv;
std::vector<glm::vec3> normals;
std::vector<unsigned int> indices;
const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359;
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y)
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
indices.push_back(y * (X_SEGMENTS + 1) + x);
indices.push_back((y + 1) * (X_SEGMENTS + 1) + x);
float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS;
float xPos = std::cos(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
float yPos = std::cos(ySegment * PI);
float zPos = std::sin(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
positions.push_back(glm::vec3(xPos, yPos, zPos));
uv.push_back(glm::vec2(xSegment, ySegment));
normals.push_back(glm::vec3(xPos, yPos, zPos));
}
}
else
bool oddRow = false;
for (unsigned int y = 0; y < Y_SEGMENTS; ++y)
{
for (int x = X_SEGMENTS; x >= 0; --x)
if (!oddRow) // even rows: y == 0, y == 2; and so on
{
indices.push_back((y + 1) * (X_SEGMENTS + 1) + x);
indices.push_back(y * (X_SEGMENTS + 1) + x);
for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
indices.push_back(y * (X_SEGMENTS + 1) + x);
indices.push_back((y + 1) * (X_SEGMENTS + 1) + x);
}
}
else
{
for (int x = X_SEGMENTS; x >= 0; --x)
{
indices.push_back((y + 1) * (X_SEGMENTS + 1) + x);
indices.push_back(y * (X_SEGMENTS + 1) + x);
}
}
oddRow = !oddRow;
}
indexCount = indices.size();
std::vector<float> data;
for (std::size_t i = 0; i < positions.size(); ++i)
{
data.push_back(positions[i].x);
data.push_back(positions[i].y);
data.push_back(positions[i].z);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0)
{
data.push_back(normals[i].x);
data.push_back(normals[i].y);
data.push_back(normals[i].z);
}
}
oddRow = !oddRow;
}
indexCount = indices.size();
glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
std::vector<float> data;
for (std::size_t i = 0; i < positions.size(); ++i)
{
data.push_back(positions[i].x);
data.push_back(positions[i].y);
data.push_back(positions[i].z);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 3));
glEnableVertexAttribArray(1);
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
if (normals.size() > 0)
{
data.push_back(normals[i].x);
data.push_back(normals[i].y);
data.push_back(normals[i].z);
}
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 5));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 8));
glEnableVertexAttribArray(3);
}
glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
float stride = (3 + 2 + 3) * sizeof(float);
glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_INT, 0);
}
//void MeshComponent::ProcessNode(aiNode * node, const aiScene* scene)
//{
/// process all the node's meshes (if any)
//or (unsigned int i = 0; i < node->mNumMeshes; i++)
//
// aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
// meshes.push_back(ProcessNode(mesh, scene));
//
/// then do the same for each of its children
//or (unsigned int i = 0; i < node->mNumChildren; i++)
//
// ProcessNode(node->mChildren[i], scene);
//
//}
MeshComponent::MeshComponent() {
//BuildTangents();
// Setup buffers
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
// UV
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 3));
glEnableVertexAttribArray(1);
// Normal
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 5));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 8));
// Tangent
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 8));
glEnableVertexAttribArray(3);
}
glBindVertexArray(sphereVAO);
glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_INT, 0);
// Bitangent
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 11));
glEnableVertexAttribArray(4);
}
// Texture
glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 14));
glEnableVertexAttribArray(5);
//void MeshComponent::ProcessNode(aiNode * node, const aiScene* scene)
//{
/// process all the node's meshes (if any)
//or (unsigned int i = 0; i < node->mNumMeshes; i++)
//
// aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
// meshes.push_back(ProcessNode(mesh, scene));
//
/// then do the same for each of its children
//or (unsigned int i = 0; i < node->mNumChildren; i++)
//
// ProcessNode(node->mChildren[i], scene);
//
//}
//m_Material = MaterialManager::Get()->LoadMaterial("Planks");
MeshComponent::MeshComponent() {
//BuildTangents();
}
// Setup buffers
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
void MeshComponent::BuildTangents()
{
for (int i = 0; i < 36; i += 3) {
glm::vec3 pos1 = vertices[i].position;
glm::vec3 pos2 = vertices[i + 1].position;
glm::vec3 pos3 = vertices[i + 2].position;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glm::vec2 uv1 = vertices[i].uv;
glm::vec2 uv2 = vertices[i + 1].uv;
glm::vec2 uv3 = vertices[i + 2].uv;
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glm::vec3 edge1 = pos2 - pos1;
glm::vec3 edge2 = pos3 - pos1;
glm::vec2 deltaUV1 = uv2 - uv1;
glm::vec2 deltaUV2 = uv3 - uv1;
// Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(0);
// UV
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 3));
glEnableVertexAttribArray(1);
float f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
// Normal
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 5));
glEnableVertexAttribArray(2);
for (int j = 0; i < 3; i++)
{
vertices[i + j].tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
vertices[i + j].tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
vertices[i + j].tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
// Tangent
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 8));
glEnableVertexAttribArray(3);
// Bitangent
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 11));
glEnableVertexAttribArray(4);
// Texture
glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(GL_FLOAT) * 14));
glEnableVertexAttribArray(5);
//m_Material = MaterialManager::Get()->LoadMaterial("Planks");
}
void MeshComponent::BuildTangents()
{
for (int i = 0; i < 36; i += 3) {
glm::vec3 pos1 = vertices[i].position;
glm::vec3 pos2 = vertices[i + 1].position;
glm::vec3 pos3 = vertices[i + 2].position;
glm::vec2 uv1 = vertices[i].uv;
glm::vec2 uv2 = vertices[i + 1].uv;
glm::vec2 uv3 = vertices[i + 2].uv;
glm::vec3 edge1 = pos2 - pos1;
glm::vec3 edge2 = pos3 - pos1;
glm::vec2 deltaUV1 = uv2 - uv1;
glm::vec2 deltaUV2 = uv3 - uv1;
float f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
for (int j = 0; i < 3; i++)
{
vertices[i + j].tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
vertices[i + j].tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
vertices[i + j].tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
vertices[i + j].bitangent.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
vertices[i + j].bitangent.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
vertices[i + j].bitangent.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
vertices[i + j].bitangent.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x);
vertices[i + j].bitangent.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y);
vertices[i + j].bitangent.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z);
}
}
}
}
void MeshComponent::SetMaterial(const std::string materialName)
{
m_Material = MaterialManager::Get()->LoadMaterial(materialName);
}
void MeshComponent::Draw(glm::mat4 projection, glm::mat4 view, glm::mat4 transform) {
Renderer::m_Shader->SetUniformMat4f("u_Model", transform);
m_Material->Bind();
//RenderSphere();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
void MeshComponent::DrawEditor() {
int choice = 0;
ImGui::Combo("Material", (int*)&choice, "Marble\0Copper\0Gold\0Paving\0Planks\0Default Material");
void MeshComponent::SetMaterial(const std::string materialName)
{
m_Material = MaterialManager::Get()->LoadMaterial(materialName);
}
void MeshComponent::Draw(glm::mat4 projection, glm::mat4 view, glm::mat4 transform) {
Renderer::m_Shader->SetUniformMat4f("u_Model", transform);
m_Material->Bind();
//RenderSphere();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
void MeshComponent::DrawEditor() {
int choice = 0;
ImGui::Combo("Material", (int*)&choice, "Marble\0Copper\0Gold\0Paving\0Planks\0Default Material");
}
}

View File

@@ -3,22 +3,24 @@
#include <glm\ext\matrix_float4x4.hpp>
#include "BaseComponent.h"
class MeshComponent {
namespace Nuake {
class MeshComponent {
private:
unsigned int VAO;
unsigned int VBO;
Ref<Material> m_Material;
private:
unsigned int VAO;
unsigned int VBO;
Ref<Material> m_Material;
void BuildTangents();
public:
void LoadModel(const std::string path);
//void ProcessNode(aiNode* node, const aiScene* scene);
MeshComponent();
void BuildTangents();
public:
void LoadModel(const std::string path);
//void ProcessNode(aiNode* node, const aiScene* scene);
MeshComponent();
void SetMaterial(const std::string materialName);
void Draw(glm::mat4 projection, glm::mat4 view, glm::mat4 transform);
void DrawEditor();
void SetMaterial(const std::string materialName);
void Draw(glm::mat4 projection, glm::mat4 view, glm::mat4 transform);
void DrawEditor();
void RenderSphere();
};
void RenderSphere();
};
}

View File

@@ -1,136 +1,139 @@
#include "ModelComponent.h"
#include "../../../Rendering/Textures/Material.h"
#include "../../../Rendering/Renderer.h"
#include "../../../Core/TextureManager.h"
#include "src/Rendering/Textures/Material.h"
#include "src/Rendering/Renderer.h"
#include "src/Core/TextureManager.h"
void ModelComponent::Draw()
{
for (auto m : meshes) {
m.Draw();
}
}
void ModelComponent::LoadModel()
{
this->meshes.clear();
Assimp::Importer import;
import.SetPropertyFloat("PP_GSN_MAX_SMOOTHING_ANGLE", 90);
const aiScene* scene = import.ReadFile(FileSystem::Root + ModelPath, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
namespace Nuake {
void ModelComponent::Draw()
{
Logger::Log("ASSIMP! Failed to load model" + std::string(import.GetErrorString()), CRITICAL);
return;
}
ProcessNode(scene->mRootNode, scene);
}
void ModelComponent::ProcessNode(aiNode* node, const aiScene* scene)
{
// process all the node's meshes (if any)
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(ProcessMesh(mesh, scene));
}
// then do the same for each of its children
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
ProcessNode(node->mChildren[i], scene);
}
}
Mesh ModelComponent::ProcessMesh(aiMesh* mesh, const aiScene* scene)
{
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
vertex.texture = 1.0f;
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.position = vector;
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.normal = vector;
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertex.tangent = vector;
vector.x = mesh->mBitangents[i].x;
vector.y = mesh->mBitangents[i].y;
vector.z = mesh->mBitangents[i].z;
vertex.bitangent = vector;
if (mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
{
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.uv = vec;
for (auto m : meshes) {
m.Draw();
}
else
vertex.uv = glm::vec2(0.0f, 0.0f);
vertices.push_back(vertex);
}
// process indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
void ModelComponent::LoadModel()
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
this->meshes.clear();
Assimp::Importer import;
import.SetPropertyFloat("PP_GSN_MAX_SMOOTHING_ANGLE", 90);
const aiScene* scene = import.ReadFile(FileSystem::Root + ModelPath, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
Logger::Log("ASSIMP! Failed to load model" + std::string(import.GetErrorString()), CRITICAL);
return;
}
ProcessNode(scene->mRootNode, scene);
}
// process material
if (mesh->mMaterialIndex >= 0)
void ModelComponent::ProcessNode(aiNode* node, const aiScene* scene)
{
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
aiString str;
std::string directory = FileSystem::Root + this->ModelPath + "/../";
material->GetTexture(aiTextureType_DIFFUSE, 0, &str);
Ref<Material> newMaterial = CreateRef<Material>(TextureManager::Get()->GetTexture(directory + str.C_Str()));
// process all the node's meshes (if any)
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(ProcessMesh(mesh, scene));
}
// then do the same for each of its children
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
ProcessNode(node->mChildren[i], scene);
}
}
//material->GetTexture(aiTextureType_NORMALS, 0, &str);
//newMaterial->SetNormal(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//
//material->GetTexture(aiTextureType_METALNESS, 0, &str);
//newMaterial->SetMetalness(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//
//material->GetTexture(aiTextureType_DIFFUSE_ROUGHNESS, 0, &str);
//newMaterial->SetRoughness(TextureManager::Get()->GetTexture(directory + str.C_Str()));
Mesh ModelComponent::ProcessMesh(aiMesh* mesh, const aiScene* scene)
{
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
//material->GetTexture(aiTextureType_DISPLACEMENT, 0, &str);
//newMaterial->SetDisplacement(TextureManager::Get()->GetTexture(directory + str.C_Str()));
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
vertex.texture = 1.0f;
//material->GetTexture(aiTextureType_AMBIENT_OCCLUSION, 0, &str);
//newMaterial->SetAO(TextureManager::Get()->GetTexture(directory + str.C_Str()));
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.position = vector;
return Mesh(vertices, indices, newMaterial);
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.normal = vector;
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertex.tangent = vector;
vector.x = mesh->mBitangents[i].x;
vector.y = mesh->mBitangents[i].y;
vector.z = mesh->mBitangents[i].z;
vertex.bitangent = vector;
if (mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
{
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.uv = vec;
}
else
vertex.uv = glm::vec2(0.0f, 0.0f);
vertices.push_back(vertex);
}
// process indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
// process material
if (mesh->mMaterialIndex >= 0)
{
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
aiString str;
std::string directory = FileSystem::Root + this->ModelPath + "/../";
material->GetTexture(aiTextureType_DIFFUSE, 0, &str);
Ref<Material> newMaterial = CreateRef<Material>(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//material->GetTexture(aiTextureType_NORMALS, 0, &str);
//newMaterial->SetNormal(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//
//material->GetTexture(aiTextureType_METALNESS, 0, &str);
//newMaterial->SetMetalness(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//
//material->GetTexture(aiTextureType_DIFFUSE_ROUGHNESS, 0, &str);
//newMaterial->SetRoughness(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//material->GetTexture(aiTextureType_DISPLACEMENT, 0, &str);
//newMaterial->SetDisplacement(TextureManager::Get()->GetTexture(directory + str.C_Str()));
//material->GetTexture(aiTextureType_AMBIENT_OCCLUSION, 0, &str);
//newMaterial->SetAO(TextureManager::Get()->GetTexture(directory + str.C_Str()));
return Mesh(vertices, indices, newMaterial);
}
}
std::vector<Texture*> ModelComponent::LoadMaterialTextures(aiMaterial* mat, aiTextureType type)
{
std::vector<Texture*> textures;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
std::string fixedStr = std::string(str.C_Str());
Texture* texture = new Texture(directory + fixedStr);
textures.push_back(texture);
}
return textures;
}
}
std::vector<Texture*> ModelComponent::LoadMaterialTextures(aiMaterial* mat, aiTextureType type)
{
std::vector<Texture*> textures;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
std::string fixedStr = std::string(str.C_Str());
Texture* texture = new Texture(directory + fixedStr);
textures.push_back(texture);
}
return textures;
}

View File

@@ -1,30 +1,32 @@
#pragma once
#include <glm\ext\matrix_float4x4.hpp>
#include <vector>
#include "../Rendering/Mesh/Mesh.h"
#include "src/Rendering/Mesh/Mesh.h"
#include "assimp/Importer.hpp"
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
class ModelComponent
namespace Nuake
{
public:
std::string ModelPath;
ModelComponent()
class ModelComponent
{
//loadModel(path);
}
void LoadModel();
void Draw();
private:
// model data
std::vector<Mesh> meshes;
std::string directory;
public:
std::string ModelPath;
void ProcessNode(aiNode* node, const aiScene* scene);
Mesh ProcessMesh(aiMesh* mesh, const aiScene* scene);
std::vector<Texture*> LoadMaterialTextures(aiMaterial* mat, aiTextureType type);
};
ModelComponent()
{
//loadModel(path);
}
void LoadModel();
void Draw();
private:
std::vector<Mesh> meshes;
std::string directory;
void ProcessNode(aiNode* node, const aiScene* scene);
Mesh ProcessMesh(aiMesh* mesh, const aiScene* scene);
std::vector<Texture*> LoadMaterialTextures(aiMaterial* mat, aiTextureType type);
};
}

View File

@@ -1,28 +1,33 @@
#pragma once
#include "../Resource/Serializable.h"
#include "../Core/OS.h"
class NameComponent {
public:
std::string Name = "Entity";
int ID;
#include "src/Resource/Serializable.h"
#include "src/Core/OS.h"
json Serialize()
namespace Nuake {
class NameComponent
{
BEGIN_SERIALIZE();
public:
std::string Name = "Entity";
int ID;
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Name);
SERIALIZE_VAL(ID);
END_SERIALIZE();
}
END_SERIALIZE();
}
bool Deserialize(const std::string& str)
{
BEGIN_DESERIALIZE();
Name = j["Name"];
if (j.contains("ID"))
ID = j["ID"];
else
ID = OS::GetTime();
bool Deserialize(const std::string& str)
{
BEGIN_DESERIALIZE();
Name = j["Name"];
return true;
}
};
if (j.contains("ID"))
ID = j["ID"];
else
ID = OS::GetTime();
return true;
}
};
}

View File

@@ -2,17 +2,18 @@
#include <functional>
#include "../Core/Timestep.h"
#include "../Entities/ScriptableEntity.h"
struct NativeScriptComponent
{
ScriptableEntity* Instance = nullptr;
ScriptableEntity *(*InstantiateScript)();
void (*DestroyScript)(NativeScriptComponent*);
template<typename T>
void Bind()
{
InstantiateScript = []() { return static_cast<ScriptableEntity*>(new T()); };
DestroyScript = [](NativeScriptComponent* nsc) { delete nsc->Instance; nsc->Instance = nullptr; };
}
//ScriptableEntity* Instance = nullptr;
//
//ScriptableEntity *(*InstantiateScript)();
//void (*DestroyScript)(NativeScriptComponent*);
//
//template<typename T>
//void Bind()
//{
// InstantiateScript = []() { return static_cast<ScriptableEntity*>(new T()); };
// DestroyScript = [](NativeScriptComponent* nsc) { delete nsc->Instance; nsc->Instance = nullptr; };
//}
};

View File

@@ -1,53 +1,55 @@
#pragma once
#include "../Entities/Entity.h"
struct ParentComponent
namespace Nuake
{
int ParentID;
Entity Parent;
bool HasParent = false;
std::vector<Entity> Children = std::vector<Entity>();
bool RemoveChildren(Entity ent)
struct ParentComponent
{
for (int i = 0; i < Children.size(); i++)
int ParentID;
Entity Parent;
bool HasParent = false;
std::vector<Entity> Children = std::vector<Entity>();
bool RemoveChildren(Entity ent)
{
if (Children[i].GetHandle() == ent.GetHandle())
for (int i = 0; i < Children.size(); i++)
{
Children.erase(Children.begin() + i);
return true;
if (Children[i].GetHandle() == ent.GetHandle())
{
Children.erase(Children.begin() + i);
return true;
}
}
return false;
}
return false;
}
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(HasParent);
if (HasParent)
SERIALIZE_VAL_LBL("ParentID", Parent.GetID());
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(HasParent);
if(HasParent)
SERIALIZE_VAL_LBL("ParentID", Parent.GetID());
//int i = 0;
//for (auto& c : Children) {
// j["Children"][0] = c.GetHandle();
// i++;
//}
//int i = 0;
//for (auto& c : Children) {
// j["Children"][0] = c.GetHandle();
// i++;
//}
END_SERIALIZE();
}
END_SERIALIZE();
}
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
this->HasParent = j["HasParent"];
if(HasParent)
this->ParentID = j["ParentID"];
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
this->HasParent = j["HasParent"];
if (HasParent)
this->ParentID = j["ParentID"];
//this->Parent = Entity{ j["Parent"], Engine::GetCurrentScene().get() };
return true;
}
};
//this->Parent = Entity{ j["Parent"], Engine::GetCurrentScene().get() };
return true;
}
};
}

View File

@@ -1,25 +1,25 @@
#include "QuakeMap.h"
#include "../Core/Core.h"
#include "../Core/MaterialManager.h"
#include "src/Core/Core.h"
#include "src/Core/MaterialManager.h"
namespace Nuake {
void QuakeMapComponent::Draw()
{
for (auto m : m_Meshes)
m->Draw();
}
void QuakeMapComponent::Load(std::string path, bool collisions)
{
if (Path == path)
return;
Path = path;
}
void QuakeMapComponent::Draw()
{
for (auto m : m_Meshes) {
m->Draw();
}
}
void QuakeMapComponent::Load(std::string path, bool collisions)
{
if (Path == path)
return;
Path = path;
}
void QuakeMapComponent::DrawEditor()
{
void QuakeMapComponent::DrawEditor()
{
}
}

View File

@@ -6,33 +6,36 @@
#include "../Resource/Serializable.h"
#include <src/Scene/Systems/QuakeMapBuilder.h>
class QuakeMapComponent {
private:
public:
std::vector<Ref<Mesh>> m_Meshes;
Ref<TrenchbroomMap> Map;
std::string Path;
bool HasCollisions = false;
void Load(std::string path, bool collisions);
void Draw();
void DrawEditor();
json Serialize()
namespace Nuake {
class QuakeMapComponent
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(HasCollisions);
SERIALIZE_VAL(Path);
END_SERIALIZE();
}
private:
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
this->Path = j["Path"];
this->HasCollisions = j["HasCollisions"];
public:
std::vector<Ref<Mesh>> m_Meshes;
Ref<TrenchbroomMap> Map;
std::string Path;
bool HasCollisions = false;
void Load(std::string path, bool collisions);
return true;
}
};
void Draw();
void DrawEditor();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(HasCollisions);
SERIALIZE_VAL(Path);
END_SERIALIZE();
}
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
this->Path = j["Path"];
this->HasCollisions = j["HasCollisions"];
return true;
}
};
}

View File

@@ -1,77 +1,79 @@
#pragma once
#include "RigidbodyComponent.h"
#include "../Core/Physics/Rigibody.h"
#include "../../../Core/Physics/PhysicsManager.h"
#include "../Rendering/Renderer.h"
#include "src/Core/Physics/Rigibody.h"
#include "src/Core/Physics/PhysicsManager.h"
#include "src/Rendering/Renderer.h"
namespace Nuake {
RigidBodyComponent::RigidBodyComponent()
{
//m_Rigidbody = CreateRef<Physics::RigidBody>();
}
Ref<Physics::RigidBody> RigidBodyComponent::GetRigidBody() const
{
return m_Rigidbody;
}
void RigidBodyComponent::SetRigidBody(Ref<Physics::RigidBody> rb)
{
m_Rigidbody = rb;
PhysicsManager::Get()->RegisterBody(rb);
}
bool RigidBodyComponent::HasRigidBody() const
{
return m_Rigidbody != nullptr;
}
float RigidBodyComponent::GetMass() {
if (m_Rigidbody)
return m_Rigidbody->GetMass();
return 0.0f;
}
void RigidBodyComponent::SetMass(float m)
{
if (!m_Rigidbody)
return;
m_Rigidbody->SetMass(m);
}
RigidBodyComponent::RigidBodyComponent()
{
//m_Rigidbody = CreateRef<Physics::RigidBody>();
void RigidBodyComponent::SyncTransformComponent(TransformComponent* tc)
{
if (!m_Rigidbody)
return;
glm::vec3 newPosition = m_Rigidbody->GetPosition();
glm::vec3 newRotation = m_Rigidbody->GetRotation();
tc->Translation = newPosition;
tc->Rotation = newRotation;
}
void RigidBodyComponent::SyncWithTransform(TransformComponent* tc)
{
if (!m_Rigidbody)
return;
btTransform newTransform;
newTransform.setIdentity();
newTransform.setOrigin(btVector3(tc->Translation.x, tc->Translation.t, tc->Translation.z));
btQuaternion quat;
quat.setEulerZYX(tc->Rotation.x, tc->Rotation.y, tc->Rotation.z);
newTransform.setRotation(quat);
m_Rigidbody->UpdateTransform(newTransform);
}
void RigidBodyComponent::DrawShape(TransformComponent* tc)
{
}
void RigidBodyComponent::DrawEditor() {
}
}
Ref<Physics::RigidBody> RigidBodyComponent::GetRigidBody() const
{
return m_Rigidbody;
}
void RigidBodyComponent::SetRigidBody(Ref<Physics::RigidBody> rb)
{
m_Rigidbody = rb;
PhysicsManager::Get()->RegisterBody(rb);
}
bool RigidBodyComponent::HasRigidBody() const
{
return m_Rigidbody != nullptr;
}
float RigidBodyComponent::GetMass() {
if (m_Rigidbody)
return m_Rigidbody->GetMass();
return 0.0f;
}
void RigidBodyComponent::SetMass(float m)
{
if (!m_Rigidbody)
return;
m_Rigidbody->SetMass(m);
}
void RigidBodyComponent::SyncTransformComponent(TransformComponent* tc)
{
if (!m_Rigidbody)
return;
glm::vec3 newPosition = m_Rigidbody->GetPosition();
glm::vec3 newRotation = m_Rigidbody->GetRotation();
tc->Translation = newPosition;
tc->Rotation = newRotation;
}
void RigidBodyComponent::SyncWithTransform(TransformComponent* tc)
{
if (!m_Rigidbody)
return;
btTransform newTransform;
newTransform.setIdentity();
newTransform.setOrigin(btVector3(tc->Translation.x, tc->Translation.t, tc->Translation.z));
btQuaternion quat;
quat.setEulerZYX(tc->Rotation.x, tc->Rotation.y, tc->Rotation.z);
newTransform.setRotation(quat);
m_Rigidbody->UpdateTransform(newTransform);
}
void RigidBodyComponent::DrawShape(TransformComponent* tc)
{
}
void RigidBodyComponent::DrawEditor() {
}

View File

@@ -1,32 +1,34 @@
#pragma once
#include "TransformComponent.h"
#include "BaseComponent.h"
#include "../Core/Core.h"
namespace Physics{
class RigidBody;
};
#include "src/Core/Core.h"
namespace Nuake {
namespace Physics
{
class RigidBody;
};
class RigidBodyComponent
{
public:
float mass = 0.0f;
Ref<Physics::RigidBody> m_Rigidbody;
bool IsKinematic = false;
RigidBodyComponent();
Ref<Physics::RigidBody> GetRigidBody() const;
void SetRigidBody(Ref<Physics::RigidBody> rb);
bool HasRigidBody() const;
void SetMass(float m);
float GetMass();
void SyncTransformComponent(TransformComponent* tc);
void SyncWithTransform(TransformComponent* tc);
class RigidBodyComponent
{
public:
float mass = 0.0f;
Ref<Physics::RigidBody> m_Rigidbody;
bool IsKinematic = false;
RigidBodyComponent();
Ref<Physics::RigidBody> GetRigidBody() const;
void SetRigidBody(Ref<Physics::RigidBody> rb);
bool HasRigidBody() const;
void SetMass(float m);
float GetMass();
void SyncTransformComponent(TransformComponent* tc);
void SyncWithTransform(TransformComponent* tc);
void DrawShape(TransformComponent* tc);
void DrawEditor();
};
void DrawShape(TransformComponent* tc);
void DrawEditor();
};
}

View File

@@ -1,11 +1,13 @@
#pragma once
#include "../Core/Physics/PhysicsShapes.h"
#include "../Core/Core.h"
#include "src/Core/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
class SphereColliderComponent
{
public:
Ref<Physics::PhysicShape> Sphere;
float Radius = 0.5f;
bool IsTrigger;
};
namespace Nuake {
class SphereColliderComponent
{
public:
Ref<Physics::PhysicShape> Sphere;
float Radius = 0.5f;
bool IsTrigger;
};
}

View File

@@ -1,21 +1,24 @@
#pragma once
#include "TransformComponent.h"
TransformComponent::TransformComponent()
namespace Nuake
{
GlobalTranslation = Vector3(0, 0, 0);
Translation = Vector3(0, 0, 0);
Rotation = Vector3(0, 0, 0);
Scale = Vector3(1, 1, 1);
}
TransformComponent::TransformComponent()
{
GlobalTranslation = Vector3(0, 0, 0);
Translation = Vector3(0, 0, 0);
Rotation = Vector3(0, 0, 0);
Scale = Vector3(1, 1, 1);
}
glm::mat4 TransformComponent::GetTransform()
{
Matrix4 transform = Matrix4(1.0f);
transform = glm::translate(transform, GlobalTranslation);
transform = glm::rotate(transform, glm::radians(Rotation.x), Vector3(1, 0, 0));
transform = glm::rotate(transform, glm::radians(Rotation.y), Vector3(0, 1, 0));
transform = glm::rotate(transform, glm::radians(Rotation.z), Vector3(0, 0, 1));
transform = glm::scale(transform, Scale);
return transform;
}
glm::mat4 TransformComponent::GetTransform()
{
Matrix4 transform = Matrix4(1.0f);
transform = glm::translate(transform, GlobalTranslation);
transform = glm::rotate(transform, glm::radians(Rotation.x), Vector3(1, 0, 0));
transform = glm::rotate(transform, glm::radians(Rotation.y), Vector3(0, 1, 0));
transform = glm::rotate(transform, glm::radians(Rotation.z), Vector3(0, 0, 1));
transform = glm::scale(transform, Scale);
return transform;
}
}

View File

@@ -2,33 +2,36 @@
#include "../Core/Maths.h"
#include "../Resource/Serializable.h"
class TransformComponent {
public:
Vector3 GlobalTranslation;
Vector3 Translation;
Vector3 Rotation; // TODO: Should use quaternions.
Vector3 Scale;
namespace Nuake
{
class TransformComponent {
public:
Vector3 GlobalTranslation;
Vector3 Translation;
Vector3 Rotation; // TODO: Should use quaternions.
Vector3 Scale;
TransformComponent();
TransformComponent();
Matrix4 GetTransform();
Matrix4 GetTransform();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL_LBL("Type", "TransformComponent");
SERIALIZE_VEC3(Translation);
SERIALIZE_VEC3(Rotation);
SERIALIZE_VEC3(Scale);
END_SERIALIZE();
}
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL_LBL("Type", "TransformComponent");
SERIALIZE_VEC3(Translation);
SERIALIZE_VEC3(Rotation);
SERIALIZE_VEC3(Scale);
END_SERIALIZE();
}
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
this->Translation = Vector3(j["Translation"]["x"], j["Translation"]["y"], j["Translation"]["z"]);
this->Rotation = Vector3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]);
this->Scale = Vector3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]);
return true;
}
};
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
this->Translation = Vector3(j["Translation"]["x"], j["Translation"]["y"], j["Translation"]["z"]);
this->Rotation = Vector3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]);
this->Scale = Vector3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]);
return true;
}
};
}

View File

@@ -1,37 +1,38 @@
#pragma once
#include "src/Core//Physics/GhostObject.h"
#include "src/Core/Physics/GhostObject.h"
#include <vector>
#
class TriggerZone {
public:
Ref<GhostObject> GhostObject;
std::string target = "";
std::vector<Entity> Targets;
bool Enabled = true;
namespace Nuake {
class TriggerZone {
public:
Ref<GhostObject> GhostObject;
std::string target = "";
TriggerZone() {
Targets = std::vector<Entity>();
}
std::vector<Entity> Targets;
bool Enabled = true;
TriggerZone()
{
Targets = std::vector<Entity>();
}
int GetOverLappingCount()
{
if (!Enabled) return 0;
int GetOverLappingCount()
{
if (!Enabled) return 0;
return GhostObject->OverlappingCount();
}
return GhostObject->OverlappingCount();
}
std::vector<Entity> GetTargets() {
return Targets;
}
std::vector<Entity> GetTargets() {
return Targets;
}
std::vector<Entity> GetOverlappingBodies()
{
if (!Enabled)
return std::vector<Entity>();
std::vector<Entity> GetOverlappingBodies()
{
if (!Enabled)
return std::vector<Entity>();
return GhostObject->GetOverlappingEntities();
}
};
return GhostObject->GetOverlappingEntities();
}
};
}

View File

@@ -1,32 +1,34 @@
#pragma once
#include "../Scripting/WrenScript.h"
#include <src/Resource/Serializable.h>
#include "src/Scripting/WrenScript.h"
#include "src/Resource/Serializable.h"
class WrenScriptComponent
{
public:
std::string Script;
std::string Class;
Ref<WrenScript> WrenScript;
json Serialize()
namespace Nuake {
class WrenScriptComponent
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Script);
SERIALIZE_VAL(Class);
END_SERIALIZE();
}
public:
std::string Script;
std::string Class;
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
if (j.contains("Script"))
Script = j["Script"];
if (j.contains("Class"))
Class = j["Class"];
Ref<WrenScript> WrenScript;
return true;
}
};
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Script);
SERIALIZE_VAL(Class);
END_SERIALIZE();
}
bool Deserialize(std::string str)
{
BEGIN_DESERIALIZE();
if (j.contains("Script"))
Script = j["Script"];
if (j.contains("Class"))
Class = j["Class"];
return true;
}
};
}