Skip to content

Commit

Permalink
Merge pull request #277 from adriengivry/rework/better-renderer
Browse files Browse the repository at this point in the history
`OvRendering` Rework: Better Rendering capabilities, less redundancy, and code clean-up
  • Loading branch information
adriengivry authored Mar 19, 2024
2 parents 0f12f6d + ad2f53f commit d1c5592
Show file tree
Hide file tree
Showing 190 changed files with 7,088 additions and 4,749 deletions.
Binary file modified Resources/Editor/Models/Arrow_Picking.fbx
Binary file not shown.
69 changes: 69 additions & 0 deletions Resources/Editor/Shaders/Billboard.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#shader vertex
#version 430 core

layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;
layout (location = 2) in vec3 geo_Normal;

layout (std140) uniform EngineUBO
{
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
};

out VS_OUT
{
vec2 TexCoords;
} vs_out;

uniform float u_Scale = 1.0f;

void main()
{
vs_out.TexCoords = geo_TexCoords;

mat4 model = ubo_Model;
float distanceToCamera = distance(ubo_ViewPos, model[3].xyz);

mat4 modelView = ubo_View * model;

// Column 0:
modelView[0][0] = 1;
modelView[0][1] = 0;
modelView[0][2] = 0;

// Column 1:
modelView[1][0] = 0;
modelView[1][1] = 1;
modelView[1][2] = 0;

// Column 2:
modelView[2][0] = 0;
modelView[2][1] = 0;
modelView[2][2] = 1;

gl_Position = ubo_Projection * modelView * vec4(geo_Pos * distanceToCamera * u_Scale, 1.0);
}

#shader fragment
#version 430 core

out vec4 FRAGMENT_COLOR;

in VS_OUT
{
vec2 TexCoords;
} fs_in;

uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0);
uniform sampler2D u_DiffuseMap;
uniform vec2 u_TextureTiling = vec2(1.0, 1.0);
uniform vec2 u_TextureOffset = vec2(0.0, 0.0);

void main()
{
FRAGMENT_COLOR = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse;
}
113 changes: 113 additions & 0 deletions Resources/Editor/Shaders/Gizmo.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#shader vertex
#version 430 core

layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;
layout (location = 2) in vec3 geo_Normal;

layout (std140) uniform EngineUBO
{
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
};

out VS_OUT
{
vec3 Color;
} vs_out;

uniform bool u_IsBall;
uniform bool u_IsPickable;
uniform int u_HighlightedAxis;

mat4 rotationMatrix(vec3 axis, float angle)
{
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;

return mat4
(
oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0
);
}

void main()
{
mat4 instanceModel = ubo_Model;

if (gl_InstanceID == 1)
instanceModel *= rotationMatrix(vec3(0, 1, 0), radians(-90)); /* X axis */
else if (gl_InstanceID == 2)
instanceModel *= rotationMatrix(vec3(1, 0, 0), radians(90)); /* Y axis */

float distanceToCamera = distance(ubo_ViewPos, instanceModel[3].xyz);

vec3 pos = geo_Pos;

vec3 fragPos = vec3(instanceModel * vec4(pos * distanceToCamera * 0.1f, 1.0));

if (u_IsPickable)
{
int blueComponent = 0;

if (gl_InstanceID == 1)
blueComponent = 252;

if (gl_InstanceID == 2)
blueComponent = 253;

if (gl_InstanceID == 0)
blueComponent = 254;

vs_out.Color = vec3(1.0f, 1.0f, blueComponent / 255.0f);
}
else
{
if (u_IsBall)
{
vs_out.Color = vec3(1.0f);
}
else
{
float red = float(gl_InstanceID == 1); // X
float green = float(gl_InstanceID == 2); // Y
float blue = float(gl_InstanceID == 0); // Z

if (!u_IsPickable && ((gl_InstanceID == 1 && u_HighlightedAxis == 0) || (gl_InstanceID == 2 && u_HighlightedAxis == 1) || (gl_InstanceID == 0 && u_HighlightedAxis == 2)))
{
vs_out.Color = vec3(1.0f, 1.0f, 0.0f);
}
else
{
vs_out.Color = vec3(red, green, blue);
}
}
}

gl_Position = ubo_Projection * ubo_View * vec4(fragPos, 1.0);
}

#shader fragment
#version 430 core

out vec4 FRAGMENT_COLOR;

in VS_OUT
{
vec3 Color;
} fs_in;

uniform bool u_IsPickable;

void main()
{
FRAGMENT_COLOR = vec4(fs_in.Color, 1.0f);
}
98 changes: 98 additions & 0 deletions Resources/Editor/Shaders/Grid.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#shader vertex
#version 430 core

layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;
layout (location = 2) in vec3 geo_Normal;

layout (std140) uniform EngineUBO
{
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
};

out VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
} vs_out;

void main()
{
vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0));
vs_out.TexCoords = vs_out.FragPos.xz;

gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0);
}

#shader fragment
#version 430 core

out vec4 FRAGMENT_COLOR;

layout (std140) uniform EngineUBO
{
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
};

in VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
} fs_in;

uniform vec3 u_Color;

float MAG(float p_lp)
{
const float lineWidth = 1.0f;

const vec2 coord = fs_in.TexCoords / p_lp;
const vec2 grid = abs(fract(coord - 0.5) - 0.5) / fwidth(coord);
const float line = min(grid.x, grid.y);
const float lineResult = lineWidth - min(line, lineWidth);

return lineResult;
}

float Grid(float height, float a, float b, float c)
{
const float cl = MAG(a);
const float ml = MAG(b);
const float fl = MAG(c);

const float cmit = 10.0f;
const float cmet = 40.0f;
const float mfit = 80.0f;
const float mfet = 160.0f;

const float df = clamp((height - cmit) / (cmet - cmit), 0.0f, 1.0f);
const float dff = clamp((height - mfit) / (mfet - mfit), 0.0f, 1.0f);

const float inl = mix(cl, ml, df);
const float fnl = mix(inl, fl, dff);

return fnl;
}

void main()
{
const float height = distance(ubo_ViewPos.y, fs_in.FragPos.y);

const float gridA = Grid(height, 1.0f, 4.0f, 8.0f);
const float gridB = Grid(height, 4.0f, 16.0f, 32.0f);

const float grid = gridA * 0.5f + gridB;

const vec2 viewdirW = ubo_ViewPos.xz - fs_in.FragPos.xz;
const float viewdist = length(viewdirW);

FRAGMENT_COLOR = vec4(u_Color, grid);
}
2 changes: 1 addition & 1 deletion Sources/Overload/OvCore/include/OvCore/ECS/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ namespace OvCore::ECS
* Try to get the given component (Returns nullptr on failure)
*/
template<typename T>
T* GetComponent();
T* GetComponent() const;

/**
* Returns a reference to the vector of components
Expand Down
2 changes: 1 addition & 1 deletion Sources/Overload/OvCore/include/OvCore/ECS/Actor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace OvCore::ECS
}

template<typename T>
inline T* Actor::GetComponent()
inline T* Actor::GetComponent() const
{
static_assert(std::is_base_of<Components::AComponent, T>::value, "T should derive from AComponent");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include "OvRendering/LowRenderer/Camera.h"
#include "OvRendering/Entities/Camera.h"
#include "AComponent.h"

namespace OvCore::ECS { class Actor; }
Expand Down Expand Up @@ -126,7 +126,7 @@ namespace OvCore::ECS::Components
/**
* Returns the OvRendering camera instance attached to this component
*/
OvRendering::LowRenderer::Camera& GetCamera();
OvRendering::Entities::Camera& GetCamera();

/**
* Serialize the component
Expand All @@ -149,6 +149,6 @@ namespace OvCore::ECS::Components
virtual void OnInspector(OvUI::Internal::WidgetContainer& p_root) override;

private:
OvRendering::LowRenderer::Camera m_camera;
OvRendering::Entities::Camera m_camera;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "OvCore/Resources/Material.h"
#include "OvCore/ECS/Components/AComponent.h"

#define MAX_MATERIAL_COUNT 255
constexpr uint8_t kMaxMaterialCount = 0xFF;

namespace OvCore::ECS { class Actor; }

Expand All @@ -27,8 +27,8 @@ namespace OvCore::ECS::Components
class CMaterialRenderer : public AComponent
{
public:
using MaterialList = std::array<OvCore::Resources::Material*, MAX_MATERIAL_COUNT>;
using MaterialField = std::array<std::array<OvUI::Widgets::AWidget*, 3>, MAX_MATERIAL_COUNT>;
using MaterialList = std::array<OvCore::Resources::Material*, kMaxMaterialCount>;
using MaterialField = std::array<std::array<OvUI::Widgets::AWidget*, 3>, kMaxMaterialCount>;

/**
* Constructor
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace OvCore::ECS::Components
private:
MaterialList m_materials;
MaterialField m_materialFields;
std::array<std::string, MAX_MATERIAL_COUNT> m_materialNames;
std::array<std::string, kMaxMaterialCount> m_materialNames;
OvMaths::FMatrix4 m_userMatrix;
};
}
Loading

0 comments on commit d1c5592

Please sign in to comment.