Shadows Test 1

This commit is contained in:
OusmBlueNinja 2024-11-27 11:51:31 -06:00
parent 864b0d60b9
commit 85fe3dbbe1
42 changed files with 1123 additions and 607 deletions

View File

@ -2,84 +2,61 @@ Entities:
- ID: 0 - ID: 0
Name: Light Name: Light
Components: Components:
Transform:
Position: [-8.23400021, -0.379575908, 0.520573974]
Rotation: [1.5, 2.70000005, 342.5]
Scale: [1, 1, 1]
Light: Light:
Type: 0 Type: 0
Intensity: 1 Intensity: 1
Color: [0, 1, 0] Color: [1, 1, 1]
Constant: 1 Constant: 1
Linear: 0.0900000036 Linear: 0.0900000036
Quadratic: 0.0320000015 Quadratic: 0.0320000015
cutOff: 0 cutOff: 0
outerCutOff: 39.7900009 outerCutOff: 39.7900009
Direction: [-0.200000003, -1, -0.300000012] Direction: [-0.200000003, -1, -0.300000012]
Transform:
Position: [-1.6726234, -0.228918433, 0.638042808]
Rotation: [1.5, 2.70000005, 342.5]
Scale: [1, 1, 1]
- ID: 1 - ID: 1
Name: Cube Name: Cube
Components: Components:
Render:
MeshName: model
Color: [1, 1, 1]
Transform: Transform:
Position: [0, 0, 0] Position: [-1.21370173, -0.400000006, -8.60000038]
Rotation: [0, -166.600006, 0] Rotation: [1.34287302e-05, -41.9440002, 1.62548586e-05]
Scale: [0.100000001, 0.100000001, 0.100000001] Scale: [1, 1, 1]
Render:
MeshName: shelf
Color: [1, 1, 1]
- ID: 2 - ID: 2
Name: Environment Name: Light
Components: Components:
Transform:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [18.7000008, 7.4000001, 10]
Render:
MeshName: WhiteCube
Color: [1, 1, 1]
- ID: 3
Name: LightBlue
Components:
Transform:
Position: [0, 1.8090117, 3.07234383]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
Light:
Type: 0
Intensity: 2
Color: [0, 0, 1]
Constant: 1
Linear: 0
Quadratic: 0.200000003
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
- ID: 4
Name: LightRed
Components:
Transform:
Position: [-2.94120026, 1.60229158, -1.5770874]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
Light: Light:
Type: 0 Type: 0
Intensity: 1 Intensity: 1
Color: [1, 0, 0] Color: [1, 1, 1]
Constant: 1 Constant: 1
Linear: 0.0900000036 Linear: 0.0900000036
Quadratic: 0.0320000015 Quadratic: 0.0320000015
cutOff: 12.5 cutOff: 12.5
outerCutOff: 17.5 outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012] Direction: [-0.200000003, -1, -0.300000012]
- ID: 5
Name: Camera
Components:
Transform: Transform:
Position: [-4.12095118, 0.57130301, -1.16569459] Position: [0, -2.21429968, 4.51696587]
Rotation: [0, 106.199997, 0] Rotation: [0, 0, 0]
Scale: [1, 1, 1] Scale: [1, 1, 1]
Camera: - ID: 3
Zoom: 10 Name: Light
FOV: 45 Components:
Orthographic: false Light:
NearPlane: 0.100000001 Type: 0
FarPlane: 1000 Intensity: 1
Color: [1, 1, 1]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
Transform:
Position: [-4.97912979, 7.21913528, 4.91323614]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]

85
assets/Example.polys Normal file
View File

@ -0,0 +1,85 @@
Entities:
- ID: 0
Name: Light
Components:
Transform:
Position: [-1.6726234, -0.228918433, 0.638042808]
Rotation: [1.5, 2.70000005, 342.5]
Scale: [1, 1, 1]
Light:
Type: 0
Intensity: 1
Color: [0, 1, 0]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 0
outerCutOff: 39.7900009
Direction: [-0.200000003, -1, -0.300000012]
- ID: 1
Name: Cube
Components:
Transform:
Position: [-1.21370173, 1.20000005, 0]
Rotation: [55.3640938, 313.355988, 2.4872179e-05]
Scale: [1, 1, 1]
Render:
MeshName: WhiteCube
Color: [1, 1, 1]
- ID: 2
Name: Environment
Components:
Render:
MeshName: WhiteCube
Color: [1, 1, 1]
Transform:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [18.7000008, 7.4000001, 10]
- ID: 3
Name: LightBlue
Components:
Light:
Type: 0
Intensity: 2
Color: [0, 0, 1]
Constant: 1
Linear: 0
Quadratic: 0.200000003
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
Transform:
Position: [0, 1.8090117, 3.07234383]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
- ID: 4
Name: LightRed
Components:
Light:
Type: 0
Intensity: 1
Color: [1, 0, 0]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
Transform:
Position: [-2.94120026, 1.60229158, -1.5770874]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
- ID: 5
Name: Camera
Components:
Camera:
Zoom: 10
FOV: 45
Orthographic: false
NearPlane: 0.100000001
FarPlane: 1000
Transform:
Position: [-7.4000001, 0.899999976, -1.60000002]
Rotation: [0, 106.199997, 0]
Scale: [1, 1, 1]

18
assets/light.polys Normal file
View File

@ -0,0 +1,18 @@
Entities:
- ID: 0
Name: Entity 0
Components:
Light:
Type: 0
Intensity: 1
Color: [1, 1, 1]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
Transform:
Position: [0, 0, 0]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -15,40 +15,40 @@ Collapsed=0
DockId=0x00000005,0 DockId=0x00000005,0
[Window][Game View] [Window][Game View]
Pos=312,28 Pos=311,28
Size=1213,680 Size=1214,704
Collapsed=0 Collapsed=0
DockId=0x00000009,0 DockId=0x00000009,0
[Window][Asset Panel] [Window][Asset Panel]
Pos=8,519 Pos=311,734
Size=302,670 Size=625,455
Collapsed=0 Collapsed=0
DockId=0x00000008,0 DockId=0x00000007,0
[Window][Inspector] [Window][Inspector]
Pos=1527,28 Pos=1527,28
Size=385,779 Size=385,700
Collapsed=0 Collapsed=0
DockId=0x0000000B,0 DockId=0x0000000B,0
[Window][Scene] [Window][Scene]
Pos=8,28 Pos=8,28
Size=302,489 Size=301,1161
Collapsed=0 Collapsed=0
DockId=0x00000007,0 DockId=0x00000001,0
[Window][Info] [Window][Info]
Pos=312,710 Pos=938,734
Size=1213,479 Size=587,455
Collapsed=0 Collapsed=0
DockId=0x0000000A,0 DockId=0x00000008,0
[Window][Settings] [Window][Settings]
Pos=1527,809 Pos=1527,730
Size=385,380 Size=385,149
Collapsed=0 Collapsed=0
DockId=0x0000000C,0 DockId=0x0000000D,0
[Window][Camera Preview] [Window][Camera Preview]
Pos=312,28 Pos=312,28
@ -56,18 +56,32 @@ Size=1213,779
Collapsed=0 Collapsed=0
DockId=0x00000009,1 DockId=0x00000009,1
[Docking][Data] [Window][Drop Target]
DockSpace ID=0xC746EC7D Window=0x9DF47A72 Pos=8,28 Size=1904,1161 Split=X Selected=0x642CEEBB Pos=938,734
DockNode ID=0x00000003 Parent=0xC746EC7D SizeRef=1517,1161 Split=X Size=587,455
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=302,1161 Split=Y Selected=0xE192E354 Collapsed=0
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=302,489 Selected=0xE192E354 DockId=0x00000008,1
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=302,670 Selected=0x9D571615
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1213,1161 Split=X [Window][Shadow Map]
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=197,1161 Selected=0x4E0EE8E4 Pos=1527,881
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=1035,1161 Split=Y Selected=0x642CEEBB Size=385,308
DockNode ID=0x00000009 Parent=0x00000006 SizeRef=1266,680 CentralNode=1 Selected=0x642CEEBB Collapsed=0
DockNode ID=0x0000000A Parent=0x00000006 SizeRef=1266,479 Selected=0xE534E588 DockId=0x0000000E,0
DockNode ID=0x00000004 Parent=0xC746EC7D SizeRef=385,1161 Split=Y Selected=0xE7039252
DockNode ID=0x0000000B Parent=0x00000004 SizeRef=385,779 Selected=0xE7039252 [Docking][Data]
DockNode ID=0x0000000C Parent=0x00000004 SizeRef=385,380 Selected=0x54723243 DockSpace ID=0xC746EC7D Window=0x9DF47A72 Pos=8,28 Size=1904,1161 Split=X Selected=0x642CEEBB
DockNode ID=0x00000001 Parent=0xC746EC7D SizeRef=301,1161 Selected=0xE192E354
DockNode ID=0x00000002 Parent=0xC746EC7D SizeRef=1601,1161 Split=X
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=1517,1161 Split=X
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=197,1161 Selected=0x4E0EE8E4
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1035,1161 Split=Y Selected=0x642CEEBB
DockNode ID=0x00000009 Parent=0x00000006 SizeRef=1266,704 CentralNode=1 Selected=0x642CEEBB
DockNode ID=0x0000000A Parent=0x00000006 SizeRef=1266,455 Split=X Selected=0x9D571615
DockNode ID=0x00000007 Parent=0x0000000A SizeRef=625,479 Selected=0x9D571615
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=587,479 Selected=0xE534E588
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=385,1161 Split=Y Selected=0xE7039252
DockNode ID=0x0000000B Parent=0x00000004 SizeRef=385,700 Selected=0xE7039252
DockNode ID=0x0000000C Parent=0x00000004 SizeRef=385,459 Split=Y Selected=0x54723243
DockNode ID=0x0000000D Parent=0x0000000C SizeRef=385,149 Selected=0x54723243
DockNode ID=0x0000000E Parent=0x0000000C SizeRef=385,308 Selected=0x77A73815

View File

@ -1,62 +0,0 @@
Entities:
- ID: 0
Name: Light
Components:
Light:
Type: 0
Intensity: 1
Color: [1, 0, 0]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 0
outerCutOff: 39.7900009
Direction: [-0.200000003, -1, -0.300000012]
Transform:
Position: [-8.23400021, -0.379575908, 0.520573974]
Rotation: [1.5, 2.70000005, 342.5]
Scale: [1, 1, 1]
- ID: 1
Name: Cube
Components:
Render:
MeshName: shelf
Color: [1, 1, 1]
Transform:
Position: [-1.21370173, 1.20000005, 0]
Rotation: [1.34287302e-05, -41.9440002, 1.62548586e-05]
Scale: [1, 1, 1]
- ID: 2
Name: Light
Components:
Transform:
Position: [0, -2.21429968, 4.51696587]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
Light:
Type: 0
Intensity: 1
Color: [0.186046124, 0, 1]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
- ID: 3
Name: Light
Components:
Transform:
Position: [-4.97912979, 7.21913528, 4.91323614]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
Light:
Type: 0
Intensity: 1
Color: [0, 1, 0]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]

198
shaders/cube_shader.fs Normal file
View File

@ -0,0 +1,198 @@
// cube_fragment_shader.fs
#version 330 core
// Outputs
out vec4 FragColor;
// Inputs from Vertex Shader
in VS_OUT {
vec3 FragPos; // Fragment position in world space
vec3 Normal; // Fragment normal in world space
vec2 TexCoords; // Texture coordinates
vec4 FragPosLightSpace; // Fragment position in light space
} fs_in;
// Structures for different light types
struct DirLight {
vec3 direction; // Direction of the light
vec3 color; // Color/intensity of the light
};
struct PointLight {
vec3 position; // Position of the light
vec3 color; // Color/intensity of the light
float constant; // Attenuation constant
float linear; // Attenuation linear factor
float quadratic; // Attenuation quadratic factor
};
struct SpotLight {
vec3 position; // Position of the light
vec3 direction; // Direction the spotlight is pointing
vec3 color; // Color/intensity of the light
float cutOff; // Inner cutoff angle (in degrees)
float outerCutOff; // Outer cutoff angle (in degrees)
float constant; // Attenuation constant
float linear; // Attenuation linear factor
float quadratic; // Attenuation quadratic factor
};
// Maximum number of each light type
#define MAX_POINT_LIGHTS 10
#define MAX_DIR_LIGHTS 10
#define MAX_SPOT_LIGHTS 10
// Uniforms for lights
uniform DirLight dirLights[MAX_DIR_LIGHTS];
uniform int numDirLights;
uniform PointLight pointLights[MAX_POINT_LIGHTS];
uniform int numPointLights;
uniform SpotLight spotLights[MAX_SPOT_LIGHTS];
uniform int numSpotLights;
// Camera position
uniform vec3 viewPos;
// Shadow map
uniform sampler2D shadowMap;
// Texture samplers
uniform sampler2D diffuseMap;
uniform sampler2D specularMap;
// Function to calculate shadow with PCF
float ShadowCalculation(vec4 fragPosLightSpace)
{
// Perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// Get closest depth value from light's perspective (using [0,1] range fragPos as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r;
// Get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// Bias to prevent shadow acne
float bias = 0.005;
// PCF parameters
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
int samples = 2; // Number of samples per axis (total samples = (2*samples +1)^2)
for(int x = -samples; x <= samples; ++x)
{
for(int y = -samples; y <= samples; ++y)
{
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
shadow /= float((2 * samples + 1) * (2 * samples + 1));
// Keep the shadow at 0.0 when outside the far_plane region of light's orthographic projection
if(projCoords.z > 1.0)
shadow = 0.0;
return shadow;
}
void main()
{
// Sample textures
vec3 diffuseColor = texture(diffuseMap, fs_in.TexCoords).rgb;
vec3 specularColor = texture(specularMap, fs_in.TexCoords).rgb;
// Properties
vec3 color = diffuseColor; // Use texture color
vec3 normal = normalize(fs_in.Normal);
vec3 viewDir = normalize(viewPos - fs_in.FragPos);
// Initialize ambient component
vec3 ambient = 0.1 * color; // Adjust ambient strength as needed
vec3 lighting = ambient;
// Iterate over directional lights
for(int i = 0; i < numDirLights; ++i)
{
// Directional light properties
vec3 lightDir = normalize(-dirLights[i].direction);
// Diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * dirLights[i].color * color;
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // Shininess factor
vec3 specular = spec * specularColor * dirLights[i].color;
// Calculate shadow
float shadow = ShadowCalculation(fs_in.FragPosLightSpace);
// Accumulate lighting with shadow influence
lighting += (1.0 - shadow) * (diffuse + specular);
}
// Iterate over point lights
for(int i = 0; i < numPointLights; ++i)
{
// Point light properties
vec3 lightDir = normalize(pointLights[i].position - fs_in.FragPos);
// Diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * pointLights[i].color * color;
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
vec3 specular = spec * specularColor * pointLights[i].color;
// Attenuation
float distance = length(pointLights[i].position - fs_in.FragPos);
float attenuation = 1.0 / (pointLights[i].constant + pointLights[i].linear * distance +
pointLights[i].quadratic * (distance * distance));
// Accumulate lighting with attenuation
lighting += attenuation * (diffuse + specular);
}
// Iterate over spotlights
for(int i = 0; i < numSpotLights; ++i)
{
// Spotlight properties
vec3 lightDir = normalize(spotLights[i].position - fs_in.FragPos);
float theta = dot(lightDir, normalize(-spotLights[i].direction));
float epsilon = spotLights[i].cutOff - spotLights[i].outerCutOff;
float intensity = clamp((theta - spotLights[i].outerCutOff) / epsilon, 0.0, 1.0);
// Diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * spotLights[i].color * color;
// Specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
vec3 specular = spec * specularColor * spotLights[i].color;
// Attenuation
float distance = length(spotLights[i].position - fs_in.FragPos);
float attenuation = 1.0 / (spotLights[i].constant + spotLights[i].linear * distance +
spotLights[i].quadratic * (distance * distance));
// Accumulate lighting with attenuation and spotlight intensity
lighting += attenuation * intensity * (diffuse + specular);
}
// Final fragment color
FragColor = vec4(lighting, 1.0);
}

39
shaders/cube_shader.vs Normal file
View File

@ -0,0 +1,39 @@
// cube_vertex_shader.vs
#version 330 core
// Vertex Attributes
layout(location = 0) in vec3 aPos; // Vertex position
layout(location = 1) in vec3 aNormal; // Vertex normal
layout(location = 2) in vec2 aTexCoords; // Texture coordinates
// Uniform Matrices
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 lightSpaceMatrix;
// Output to Fragment Shader
out VS_OUT {
vec3 FragPos; // Fragment position in world space
vec3 Normal; // Fragment normal in world space
vec2 TexCoords; // Texture coordinates
vec4 FragPosLightSpace; // Fragment position in light space
} fs_in;
void main()
{
// Calculate fragment position in world space
fs_in.FragPos = vec3(model * vec4(aPos, 1.0));
// Calculate and normalize the normal vector in world space
fs_in.Normal = mat3(transpose(inverse(model))) * aNormal;
// Pass through texture coordinates
fs_in.TexCoords = aTexCoords;
// Calculate fragment position in light space for shadow mapping
fs_in.FragPosLightSpace = lightSpaceMatrix * vec4(fs_in.FragPos, 1.0);
// Final vertex position in clip space
gl_Position = projection * view * vec4(fs_in.FragPos, 1.0);
}

12
shaders/depth_shader.fs Normal file
View File

@ -0,0 +1,12 @@
// depth_fragment_shader.fs
#version 330 core
// Inputs from Vertex Shader
in vec4 FragPosLightSpace;
// Output
void main()
{
// The depth value is automatically written to the depth buffer
// No need to output any color
}

24
shaders/depth_shader.vs Normal file
View File

@ -0,0 +1,24 @@
// depth_vertex_shader.vs
#version 330 core
// Vertex Attributes
layout(location = 0) in vec3 aPos;
// Uniform Matrices
uniform mat4 model;
uniform mat4 lightSpaceMatrix;
// Output to Fragment Shader
out vec4 FragPosLightSpace;
void main()
{
// Calculate fragment position in world space
vec3 FragPos = vec3(model * vec4(aPos, 1.0));
// Calculate fragment position in light space
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);
// Final vertex position in clip space
gl_Position = FragPosLightSpace;
}

View File

@ -32,7 +32,7 @@ std::string GetCurrentTimestamp() {
} }
// Function to add a new log message with timestamp // Function to add a new log message with timestamp
void AddLogMessage(const std::string& message) { void LogMessage(const std::string& message) {
std::lock_guard<std::mutex> lock(g_LogMutex); std::lock_guard<std::mutex> lock(g_LogMutex);
// Create the log entry with timestamp // Create the log entry with timestamp
@ -61,7 +61,7 @@ void DrawLogTerminal(const char* title, bool* p_open) {
ImGui::Separator(); ImGui::Separator();
// Child region for scrolling // Child region for scrolling
ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); ImGui::BeginChild("ScrollingRegion", ImVec2(-1, -1), false, ImGuiWindowFlags_HorizontalScrollbar);
// Lock the mutex to safely access the log buffer // Lock the mutex to safely access the log buffer
{ {

View File

@ -14,7 +14,7 @@ extern std::mutex g_LogMutex;
constexpr size_t MAX_LOG_MESSAGES = 1000; constexpr size_t MAX_LOG_MESSAGES = 1000;
// Function to add a new log message // Function to add a new log message
void AddLogMessage(const std::string& message); void LogMessage(const std::string& message);
// Function to draw the log terminal // Function to draw the log terminal
void DrawLogTerminal(const char* title = "Terminal", bool* p_open = nullptr); void DrawLogTerminal(const char* title = "Terminal", bool* p_open = nullptr);

View File

@ -7,6 +7,7 @@
#include <fstream> #include <fstream>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <GL/glew.h>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
class RenderComponent : public Component class RenderComponent : public Component
@ -14,6 +15,10 @@ class RenderComponent : public Component
public: public:
std::string meshName; std::string meshName;
glm::vec3 color; glm::vec3 color;
// Texture IDs
GLuint diffuseMap;
GLuint specularMap;
RenderComponent(); RenderComponent();
virtual const std::string& GetName() const override; virtual const std::string& GetName() const override;

View File

@ -1,14 +1,13 @@
// Shader.cpp
#include "Shader.h" #include "Shader.h"
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <filesystem> // C++17 feature for file existence
#include <glm/gtc/type_ptr.hpp> namespace fs = std::filesystem;
#include <GL/glew.h>
#include <glm/glm.hpp>
// Shader constructor Shader::Shader(const char* vertexPath, const char* fragmentPath)
Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
{ {
// 1. Retrieve the vertex/fragment source code from filePath // 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode; std::string vertexCode;
@ -16,107 +15,122 @@ Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
std::ifstream vShaderFile; std::ifstream vShaderFile;
std::ifstream fShaderFile; std::ifstream fShaderFile;
// Ensure ifstream objects can throw exceptions // Check if shader files exist
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); if (!fs::exists(vertexPath)) {
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); std::cerr << "ERROR::SHADER::VERTEX_SHADER_FILE_NOT_FOUND: " << vertexPath << "\n";
try return;
}
if (!fs::exists(fragmentPath)) {
std::cerr << "ERROR::SHADER::FRAGMENT_SHADER_FILE_NOT_FOUND: " << fragmentPath << "\n";
return;
}
// Ensure ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{ {
// Open files // Open files
vShaderFile.open(vertexPath); vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath); fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream; std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams // Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf(); vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf(); fShaderStream << fShaderFile.rdbuf();
// Close file handlers // Close file handlers
vShaderFile.close(); vShaderFile.close();
fShaderFile.close(); fShaderFile.close();
// Convert stream into string // Convert stream into string
vertexCode = vShaderStream.str(); vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str(); fragmentCode = fShaderStream.str();
} }
catch (std::ifstream::failure& e) catch (std::ifstream::failure& e)
{ {
std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ\n"; std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ\n";
std::cerr << "Failed to read shader files: " << vertexPath << " and " << fragmentPath << "\n";
return;
} }
const GLchar* vShaderCode = vertexCode.c_str(); const char* vShaderCode = vertexCode.c_str();
const GLchar* fShaderCode = fragmentCode.c_str(); const char * fShaderCode = fragmentCode.c_str();
// 2. Compile shaders // 2. Compile shaders
GLuint vertex, fragment; GLuint vertex, fragment;
GLint success; GLint success;
GLchar infoLog[512]; GLchar infoLog[1024];
// Vertex Shader // Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER); vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL); glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex); glCompileShader(vertex);
// Check compile errors // Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success) if(!success)
{ {
glGetShaderInfoLog(vertex, 512, NULL, infoLog); glGetShaderInfoLog(vertex, 1024, NULL, infoLog);
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << '\n'; std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
} }
// Fragment Shader // Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER); fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL); glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment); glCompileShader(fragment);
// Check compile errors // Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success) if(!success)
{ {
glGetShaderInfoLog(fragment, 512, NULL, infoLog); glGetShaderInfoLog(fragment, 1024, NULL, infoLog);
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << '\n'; std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
} }
// Shader Program // Shader Program
Program = glCreateProgram(); ID = glCreateProgram();
glAttachShader(Program, vertex); glAttachShader(ID, vertex);
glAttachShader(Program, fragment); glAttachShader(ID, fragment);
glLinkProgram(Program); glLinkProgram(ID);
// Check linking errors // Print linking errors if any
glGetProgramiv(Program, GL_LINK_STATUS, &success); glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success) if(!success)
{ {
glGetProgramInfoLog(Program, 512, NULL, infoLog); glGetProgramInfoLog(ID, 1024, NULL, infoLog);
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << '\n'; std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << "\n";
} }
// Delete the shaders // Delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
} }
// Activate the shader
void Shader::Use()
{
glUseProgram(Program);
}
// Set integer uniform
void Shader::setInt(const std::string& name, int value) const void Shader::Use()
{ {
glUniform1i(glGetUniformLocation(Program, name.c_str()), value); glUseProgram(ID);
} }
void Shader::SetBool(const std::string &name, bool value) const void Shader::SetBool(const std::string &name, bool value) const
{ {
glUniform1i(glGetUniformLocation(Program, name.c_str()), (int)value); glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
} }
void Shader::SetInt(const std::string &name, int value) const void Shader::SetInt(const std::string &name, int value) const
{ {
glUniform1i(glGetUniformLocation(Program, name.c_str()), value); glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
} }
void Shader::SetFloat(const std::string &name, float value) const void Shader::SetFloat(const std::string &name, float value) const
{ {
glUniform1f(glGetUniformLocation(Program, name.c_str()), value); glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
} }
void Shader::SetVec3(const std::string &name, const glm::vec3 &value) const void Shader::SetVec3(const std::string &name, const glm::vec3 &value) const
{ {
glUniform3fv(glGetUniformLocation(Program, name.c_str()), 1, glm::value_ptr(value)); glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void Shader::SetMat4(const std::string &name, const glm::mat4 &mat) const
{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
} }

View File

@ -1,30 +1,26 @@
#ifndef SHADER_H // Shader.h
#define SHADER_H #pragma once
#include <GL/glew.h>
#include <string> #include <string>
#include <GL/glew.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
class Shader { class Shader {
public: public:
GLuint Program; // The program ID GLuint ID;
// Constructor reads and builds the shader // Constructor reads and builds the shader
Shader(const GLchar* vertexPath, const GLchar* fragmentPath); Shader(const char* vertexPath, const char* fragmentPath);
// Use/activate the shader // Use the shader
void Use(); void Use();
// Utility uniform functions // Utility functions to set uniforms
void setInt(const std::string& name, int value) const;
// Utility uniform functions
void SetBool(const std::string &name, bool value) const; void SetBool(const std::string &name, bool value) const;
void SetInt(const std::string &name, int value) const; void SetInt(const std::string &name, int value) const;
void SetFloat(const std::string &name, float value) const;
void SetFloat(const std::string &name, float value) const;
void SetVec3(const std::string &name, const glm::vec3 &value) const; void SetVec3(const std::string &name, const glm::vec3 &value) const;
void SetMat4(const std::string &name, const glm::mat4 &mat) const; void SetMat4(const std::string &name, const glm::mat4 &mat) const;
}; };
#endif // SHADER_H

File diff suppressed because it is too large Load Diff

View File

@ -103,14 +103,14 @@ void Model::render(std::shared_ptr<TransformComponent> transform, std::shared_pt
// Set shader uniforms // Set shader uniforms
shaderProgram->Use(); shaderProgram->Use();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram->Program, "model"), 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(glGetUniformLocation(shaderProgram->ID, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(glGetUniformLocation(shaderProgram->Program, "objectColor"), 1, glm::value_ptr(render->color)); glUniform3fv(glGetUniformLocation(shaderProgram->ID, "objectColor"), 1, glm::value_ptr(render->color));
glUniform1i(glGetUniformLocation(shaderProgram->Program, "useTexture"), textureID != 0); glUniform1i(glGetUniformLocation(shaderProgram->ID, "useTexture"), textureID != 0);
if (textureID) { if (textureID) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID); glBindTexture(GL_TEXTURE_2D, textureID);
shaderProgram->setInt("texture1", 0); // Bind texture unit 0 shaderProgram->SetInt("texture1", 0); // Bind texture unit 0
} }
// Bind VAO and render // Bind VAO and render