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
Name: Light
Components:
Transform:
Position: [-8.23400021, -0.379575908, 0.520573974]
Rotation: [1.5, 2.70000005, 342.5]
Scale: [1, 1, 1]
Light:
Type: 0
Intensity: 1
Color: [0, 1, 0]
Color: [1, 1, 1]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 0
outerCutOff: 39.7900009
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
Name: Cube
Components:
Render:
MeshName: model
Color: [1, 1, 1]
Transform:
Position: [0, 0, 0]
Rotation: [0, -166.600006, 0]
Scale: [0.100000001, 0.100000001, 0.100000001]
Position: [-1.21370173, -0.400000006, -8.60000038]
Rotation: [1.34287302e-05, -41.9440002, 1.62548586e-05]
Scale: [1, 1, 1]
Render:
MeshName: shelf
Color: [1, 1, 1]
- ID: 2
Name: Environment
Name: Light
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:
Type: 0
Intensity: 1
Color: [1, 0, 0]
Color: [1, 1, 1]
Constant: 1
Linear: 0.0900000036
Quadratic: 0.0320000015
cutOff: 12.5
outerCutOff: 17.5
Direction: [-0.200000003, -1, -0.300000012]
- ID: 5
Name: Camera
Components:
Transform:
Position: [-4.12095118, 0.57130301, -1.16569459]
Rotation: [0, 106.199997, 0]
Position: [0, -2.21429968, 4.51696587]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
- ID: 3
Name: Light
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: [-4.97912979, 7.21913528, 4.91323614]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]
Camera:
Zoom: 10
FOV: 45
Orthographic: false
NearPlane: 0.100000001
FarPlane: 1000

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
[Window][Game View]
Pos=312,28
Size=1213,680
Pos=311,28
Size=1214,704
Collapsed=0
DockId=0x00000009,0
[Window][Asset Panel]
Pos=8,519
Size=302,670
Pos=311,734
Size=625,455
Collapsed=0
DockId=0x00000008,0
DockId=0x00000007,0
[Window][Inspector]
Pos=1527,28
Size=385,779
Size=385,700
Collapsed=0
DockId=0x0000000B,0
[Window][Scene]
Pos=8,28
Size=302,489
Size=301,1161
Collapsed=0
DockId=0x00000007,0
DockId=0x00000001,0
[Window][Info]
Pos=312,710
Size=1213,479
Pos=938,734
Size=587,455
Collapsed=0
DockId=0x0000000A,0
DockId=0x00000008,0
[Window][Settings]
Pos=1527,809
Size=385,380
Pos=1527,730
Size=385,149
Collapsed=0
DockId=0x0000000C,0
DockId=0x0000000D,0
[Window][Camera Preview]
Pos=312,28
@ -56,18 +56,32 @@ Size=1213,779
Collapsed=0
DockId=0x00000009,1
[Docking][Data]
DockSpace ID=0xC746EC7D Window=0x9DF47A72 Pos=8,28 Size=1904,1161 Split=X Selected=0x642CEEBB
DockNode ID=0x00000003 Parent=0xC746EC7D SizeRef=1517,1161 Split=X
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=302,1161 Split=Y Selected=0xE192E354
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=302,489 Selected=0xE192E354
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=302,670 Selected=0x9D571615
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1213,1161 Split=X
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=197,1161 Selected=0x4E0EE8E4
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=1035,1161 Split=Y Selected=0x642CEEBB
DockNode ID=0x00000009 Parent=0x00000006 SizeRef=1266,680 CentralNode=1 Selected=0x642CEEBB
DockNode ID=0x0000000A Parent=0x00000006 SizeRef=1266,479 Selected=0xE534E588
DockNode ID=0x00000004 Parent=0xC746EC7D SizeRef=385,1161 Split=Y Selected=0xE7039252
DockNode ID=0x0000000B Parent=0x00000004 SizeRef=385,779 Selected=0xE7039252
DockNode ID=0x0000000C Parent=0x00000004 SizeRef=385,380 Selected=0x54723243
[Window][Drop Target]
Pos=938,734
Size=587,455
Collapsed=0
DockId=0x00000008,1
[Window][Shadow Map]
Pos=1527,881
Size=385,308
Collapsed=0
DockId=0x0000000E,0
[Docking][Data]
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
void AddLogMessage(const std::string& message) {
void LogMessage(const std::string& message) {
std::lock_guard<std::mutex> lock(g_LogMutex);
// Create the log entry with timestamp
@ -61,7 +61,7 @@ void DrawLogTerminal(const char* title, bool* p_open) {
ImGui::Separator();
// 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
{

View File

@ -14,7 +14,7 @@ extern std::mutex g_LogMutex;
constexpr size_t MAX_LOG_MESSAGES = 1000;
// 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
void DrawLogTerminal(const char* title = "Terminal", bool* p_open = nullptr);

View File

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

View File

@ -1,14 +1,13 @@
// Shader.cpp
#include "Shader.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <filesystem> // C++17 feature for file existence
#include <glm/gtc/type_ptr.hpp>
#include <GL/glew.h>
#include <glm/glm.hpp>
namespace fs = std::filesystem;
// Shader constructor
Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
@ -16,21 +15,34 @@ Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// Ensure ifstream objects can throw exceptions
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
// Check if shader files exist
if (!fs::exists(vertexPath)) {
std::cerr << "ERROR::SHADER::VERTEX_SHADER_FILE_NOT_FOUND: " << vertexPath << "\n";
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
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// Close file handlers
vShaderFile.close();
fShaderFile.close();
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
@ -38,85 +50,87 @@ Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
catch (std::ifstream::failure& e)
{
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 GLchar* fShaderCode = fragmentCode.c_str();
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
// 2. Compile shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
GLchar infoLog[1024];
// Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// Check compile errors
// Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
if(!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << '\n';
glGetShaderInfoLog(vertex, 1024, NULL, infoLog);
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
}
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
// Check compile errors
// Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
if(!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << '\n';
glGetShaderInfoLog(fragment, 1024, NULL, infoLog);
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
}
// Shader Program
Program = glCreateProgram();
glAttachShader(Program, vertex);
glAttachShader(Program, fragment);
glLinkProgram(Program);
// Check linking errors
glGetProgramiv(Program, GL_LINK_STATUS, &success);
if (!success)
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
// Print linking errors if any
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(Program, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << '\n';
glGetProgramInfoLog(ID, 1024, NULL, infoLog);
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(fragment);
}
// Activate the shader
void Shader::Use()
{
glUseProgram(Program);
}
// Set integer uniform
void Shader::setInt(const std::string& name, int value) const
{
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
glUseProgram(ID);
}
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
{
glUniform1i(glGetUniformLocation(Program, name.c_str()), value);
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
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
{
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
#define SHADER_H
// Shader.h
#pragma once
#include <GL/glew.h>
#include <string>
#include <GL/glew.h>
#include <glm/glm.hpp>
class Shader {
public:
GLuint Program; // The program ID
GLuint ID;
// 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();
// Utility uniform functions
void setInt(const std::string& name, int value) const;
// Utility uniform functions
// Utility functions to set uniforms
void SetBool(const std::string &name, bool value) const;
void SetInt(const std::string &name, int value) const;
void SetFloat(const std::string &name, float value) const;
void SetVec3(const std::string &name, const glm::vec3 &value) 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
shaderProgram->Use();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram->Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(glGetUniformLocation(shaderProgram->Program, "objectColor"), 1, glm::value_ptr(render->color));
glUniform1i(glGetUniformLocation(shaderProgram->Program, "useTexture"), textureID != 0);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram->ID, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(glGetUniformLocation(shaderProgram->ID, "objectColor"), 1, glm::value_ptr(render->color));
glUniform1i(glGetUniformLocation(shaderProgram->ID, "useTexture"), textureID != 0);
if (textureID) {
glActiveTexture(GL_TEXTURE0);
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