Updated Shader with more functions

This commit is contained in:
OusmBlueNinja 2024-12-31 15:45:24 -06:00
parent 35bc1b0796
commit b6aa69210f
5 changed files with 65 additions and 28 deletions

View File

@ -0,0 +1,5 @@
#version 330 core
void main()
{
// No color output; only depth is recorded
}

10
assets/shaders/Depth.vert Normal file
View File

@ -0,0 +1,10 @@
#version 330 core
layout(location = 0) in vec3 aPos;
uniform mat4 lightSpaceMatrix;
uniform mat4 model;
void main()
{
gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0);
}

View File

@ -1,6 +1,6 @@
[Window][DockSpace] [Window][DockSpace]
Pos=0,0 Pos=0,0
Size=1920,1177 Size=1280,720
Collapsed=0 Collapsed=0
[Window][Debug##Default] [Window][Debug##Default]
@ -68,26 +68,26 @@ Size=1920,1177
Collapsed=0 Collapsed=0
[Window][Inspector##InspectorWindow] [Window][Inspector##InspectorWindow]
Pos=1536,27 Pos=896,27
Size=376,1142 Size=376,685
Collapsed=0 Collapsed=0
DockId=0x00000016,0 DockId=0x00000016,0
[Window][Editor##EditorWindow] [Window][Editor##EditorWindow]
Pos=332,27 Pos=332,27
Size=1202,776 Size=562,319
Collapsed=0 Collapsed=0
DockId=0x00000017,0 DockId=0x00000017,0
[Window][Performance##performance] [Window][Performance##performance]
Pos=8,761 Pos=8,468
Size=322,408 Size=322,244
Collapsed=0 Collapsed=0
DockId=0x0000001C,0 DockId=0x0000001C,0
[Window][Logger##logger] [Window][Logger##logger]
Pos=332,805 Pos=332,348
Size=1202,364 Size=562,364
Collapsed=0 Collapsed=0
DockId=0x00000019,0 DockId=0x00000019,0
@ -105,7 +105,7 @@ DockId=0x0000000F,0
[Window][Scene Window##SceneWindow] [Window][Scene Window##SceneWindow]
Pos=8,27 Pos=8,27
Size=322,732 Size=322,439
Collapsed=0 Collapsed=0
DockId=0x0000001B,0 DockId=0x0000001B,0
@ -121,6 +121,12 @@ Size=599,364
Collapsed=0 Collapsed=0
DockId=0x0000001A,0 DockId=0x0000001A,0
[Window][Depth Map Debug]
Pos=332,805
Size=1202,364
Collapsed=0
DockId=0x00000019,1
[Table][0xE9E836E4,4] [Table][0xE9E836E4,4]
Column 0 Weight=1.2999 Column 0 Weight=1.2999
Column 1 Weight=1.0439 Column 1 Weight=1.0439
@ -128,7 +134,7 @@ Column 2 Weight=0.6474
Column 3 Weight=1.0088 Column 3 Weight=1.0088
[Docking][Data] [Docking][Data]
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,50 Size=1904,1142 Split=X Selected=0xF7365A5A DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=42,84 Size=1264,685 Split=X Selected=0xF7365A5A
DockNode ID=0x00000013 Parent=0x14621557 SizeRef=322,1142 Split=Y Selected=0x818D04BB DockNode ID=0x00000013 Parent=0x14621557 SizeRef=322,1142 Split=Y Selected=0x818D04BB
DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,456 HiddenTabBar=1 Selected=0x1D5D92B6 DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,456 HiddenTabBar=1 Selected=0x1D5D92B6
DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,254 HiddenTabBar=1 Selected=0x818D04BB DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,254 HiddenTabBar=1 Selected=0x818D04BB

View File

@ -8,7 +8,7 @@
// Constructor implementations // Constructor implementations
Shader::Shader() : ID(0) {} Shader::Shader() : ID(0) {}
Shader::Shader(const char* vertexPath, const char* fragmentPath) Shader::Shader(const char *vertexPath, const char *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;
@ -17,19 +17,19 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath)
std::ifstream fShaderFile; std::ifstream fShaderFile;
// Ensure ifstream objects can throw exceptions // Ensure ifstream objects can throw exceptions
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try 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();
@ -37,15 +37,15 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath)
// 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: " << e.what() << std::endl; std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
} }
const char* vShaderCode = vertexCode.c_str(); const char *vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str(); const char *fShaderCode = fragmentCode.c_str();
// 2. Compile shaders // 2. Compile shaders
GLuint vertex, fragment; GLuint vertex, fragment;
@ -58,10 +58,11 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath)
glCompileShader(vertex); glCompileShader(vertex);
// Print compile errors if any // 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, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
<< infoLog << std::endl;
} }
// Fragment Shader // Fragment Shader
@ -70,10 +71,11 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath)
glCompileShader(fragment); glCompileShader(fragment);
// Print compile errors if any // 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, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"
<< infoLog << std::endl;
} }
// Shader Program // Shader Program
@ -83,10 +85,11 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath)
glLinkProgram(ID); glLinkProgram(ID);
// Print linking errors if any // Print linking errors if any
glGetProgramiv(ID, GL_LINK_STATUS, &success); glGetProgramiv(ID, GL_LINK_STATUS, &success);
if(!success) if (!success)
{ {
glGetProgramInfoLog(ID, 512, NULL, infoLog); glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n"
<< infoLog << std::endl;
} }
// Delete the shaders as they're linked into our program now and no longer necessary // Delete the shaders as they're linked into our program now and no longer necessary
@ -143,3 +146,14 @@ void Shader::SetMat4(const std::string &name, const glm::mat4 &mat) const
glUseProgram(ID); // Ensure the shader program is active glUseProgram(ID); // Ensure the shader program is active
glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, glm::value_ptr(mat)); glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, glm::value_ptr(mat));
} }
void Shader::SetVec3(const std::string &name, const glm::vec3 &value) const
{
glUseProgram(ID); // Ensure the shader program is active
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void Shader::SetSampler2D(const std::string &name, int textureUnit) const
{
glUseProgram(ID); // Ensure the shader program is active
glUniform1i(glGetUniformLocation(ID, name.c_str()), textureUnit);
}

View File

@ -3,7 +3,7 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <GL/glew.h> // or appropriate OpenGL headers #include <GL/glew.h> // or appropriate OpenGL headers
#include <glm/glm.hpp> // For glm::mat4 #include <glm/glm.hpp> // For glm::mat4
class Shader class Shader
@ -13,7 +13,7 @@ public:
// Constructors // Constructors
Shader(); Shader();
Shader(const char* vertexPath, const char* fragmentPath); Shader(const char *vertexPath, const char *fragmentPath);
~Shader(); // Destructor to clean up shader program ~Shader(); // Destructor to clean up shader program
// Use/activate the shader // Use/activate the shader
@ -24,6 +24,8 @@ public:
void SetFloat(const std::string &name, float value) const; void SetFloat(const std::string &name, float value) const;
void SetBool(const std::string &name, bool value) const; void SetBool(const std::string &name, bool value) const;
void SetMat4(const std::string &name, const glm::mat4 &mat) const; // For setting 4x4 matrices void SetMat4(const std::string &name, const glm::mat4 &mat) const; // For setting 4x4 matrices
void SetSampler2D(const std::string &name, int textureUnit) const;
void SetVec3(const std::string &name, const glm::vec3 &value) const;
private: private:
// Caching uniform locations for performance // Caching uniform locations for performance