Implement LearnOpenGL::Shader
This commit is contained in:
@@ -21,25 +21,43 @@ include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME J3ML
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-7.zip
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-17.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME ReWindow
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.12.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME GLAD
|
||||
URL https://git.redacted.cc/Redacted/glad/archive/v2.1.zip
|
||||
)
|
||||
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
|
||||
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp" )
|
||||
file(GLOB_RECURSE ASSETS "assets/*")
|
||||
file(GLOB_RECURSE ASSETS "content/*")
|
||||
|
||||
add_library(JGL SHARED ${SOURCES}
|
||||
include/JGL/JGL.h
|
||||
src/JGL/JGL.cpp)
|
||||
src/JGL/JGL.cpp
|
||||
include/LearnOpenGL/Shader.h
|
||||
src/LearnOpenGL/Shader.cpp)
|
||||
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLUT REQUIRED)
|
||||
find_package(Freetype REQUIRED)
|
||||
|
||||
add_executable(JGL_Demo main.cpp)
|
||||
set_target_properties(JGL_Demo PROPERTIES LINK_FLAGS "-Wl,-rpath,./lib")
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include
|
||||
${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS}
|
||||
${J3ML_SOURCE_DIR}/include)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
include_directories(${OPENGL_INCLUDE_DIRS})
|
||||
include_directories(${GLUT_INCLUDE_DIRS})
|
||||
include_directories(${J3ML_SOURCE_DIR}/include)
|
||||
include_directories(${ReWindow_SOURCE_DIR}/include)
|
||||
include_directories(${glad_SOURCE_DIR}/include)
|
||||
|
||||
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} J3ML)
|
||||
target_link_libraries(JGL_Demo PRIVATE ${FREETYPE_LIBRARIES})
|
||||
target_include_directories(JGL_Demo PRIVATE ${FREETYPE_INCLUDE_DIRS})
|
||||
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} J3ML ReWindowLibrary GLEW glad)
|
||||
|
56
include/LearnOpenGL/Shader.h
Normal file
56
include/LearnOpenGL/Shader.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
// LearnOpenGL::Shader
|
||||
// OpenGL Shader Class Wrapper
|
||||
// Updated by dawsh
|
||||
// https://learnopengl.com/code_viewer_gh.php?code=includes/learnopengl/shader.h
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <glad/glad.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <J3ML/LinearAlgebra/Vector4.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix2x2.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix3x3.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix4x4.h>
|
||||
|
||||
namespace LearnOpenGL {
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using J3ML::LinearAlgebra::Vector3;
|
||||
using J3ML::LinearAlgebra::Vector4;
|
||||
using J3ML::LinearAlgebra::Matrix2x2;
|
||||
using J3ML::LinearAlgebra::Matrix3x3;
|
||||
using J3ML::LinearAlgebra::Matrix4x4;
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
unsigned int ID{};
|
||||
|
||||
Shader();
|
||||
Shader(std::filesystem::path vertexProgramPath, std::filesystem::path fragmentProgramPath);
|
||||
Shader(std::string vertexProgramSrc, std::string fragmentProgramSrc);
|
||||
|
||||
void use();
|
||||
// Utility uniform functions
|
||||
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 setVec2(const std::string& name, const Vector2& value) const;
|
||||
void setVec2(const std::string& name, float x, float y) const;
|
||||
void setVec3(const std::string& name, const Vector3& value) const;
|
||||
void setVec3(const std::string& name, float x, float y, float z) const;
|
||||
void setVec4(const std::string& name, const Vector4& value) const;
|
||||
void setVec4(const std::string& name, float x, float y, float z, float w) const;
|
||||
void setMat2(const std::string& name, const Matrix2x2 &mat) const;
|
||||
void setMat3(const std::string& name, const Matrix3x3 &mat) const;
|
||||
void setMat4(const std::string& name, const Matrix4x4 &mat) const;
|
||||
|
||||
|
||||
private:
|
||||
void checkCompileErrors(GLuint shader, std::string type);
|
||||
};
|
||||
}
|
200
src/LearnOpenGL/Shader.cpp
Normal file
200
src/LearnOpenGL/Shader.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#include <LearnOpenGL/Shader.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace LearnOpenGL
|
||||
{
|
||||
|
||||
Shader::Shader(std::filesystem::path vertexProgramPath, std::filesystem::path fragmentProgramPath) {
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::string geometryCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream fShaderFile;
|
||||
std::ifstream gShaderFile;
|
||||
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try { // Open Files
|
||||
vShaderFile.open(vertexProgramPath);
|
||||
fShaderFile.open(fragmentProgramPath);
|
||||
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();
|
||||
if (false)
|
||||
{
|
||||
gShaderFile.open("");
|
||||
std::stringstream gShaderStream;
|
||||
gShaderStream << gShaderFile.rdbuf();
|
||||
gShaderFile.close();
|
||||
geometryCode = gShaderStream.str();
|
||||
}
|
||||
} catch (std::ifstream::failure& e) {
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
|
||||
}
|
||||
const char* vShaderCode = vertexCode.c_str();
|
||||
const char* fShaderCode = fragmentCode.c_str();
|
||||
// 2. Compile shaders
|
||||
unsigned int vertex, fragment;
|
||||
// vertex shader
|
||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||
glCompileShader(vertex);
|
||||
checkCompileErrors(vertex, "VERTEX");
|
||||
// fragment shader
|
||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||
glCompileShader(fragment);
|
||||
checkCompileErrors(fragment, "FRAGMENT");
|
||||
// if geometry shader is given, compile geometry shader
|
||||
unsigned int geometry;
|
||||
if (false)
|
||||
{
|
||||
//const char * gShaderCode = geometryCode.c_str();
|
||||
//geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
//glShaderSource(geometry, 1, &gShaderCode, NULL);
|
||||
//glCompileShader(geometry);
|
||||
//checkCompileErrors(geometry, "GEOMETRY");
|
||||
}
|
||||
// shader Program
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
if (false) // geometryPath != nullptr)
|
||||
glAttachShader(ID, geometry);
|
||||
glLinkProgram(ID);
|
||||
checkCompileErrors(ID, "PROGRAM");
|
||||
// delete the shaders as they're linked into our program now and are no longer necessary
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
if (false) //geometryPath != nullptr)
|
||||
glDeleteShader(geometry);
|
||||
}
|
||||
|
||||
Shader::Shader(std::string vertexProgramSrc, std::string fragmentProgramSrc) {
|
||||
|
||||
std::string geometryCode;
|
||||
|
||||
const char* vShaderCode = vertexProgramSrc.c_str();
|
||||
const char* fShaderCode = fragmentProgramSrc.c_str();
|
||||
// 2. Compile shaders
|
||||
unsigned int vertex, fragment;
|
||||
// vertex shader
|
||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||
glCompileShader(vertex);
|
||||
checkCompileErrors(vertex, "VERTEX");
|
||||
// fragment shader
|
||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||
glCompileShader(fragment);
|
||||
checkCompileErrors(fragment, "FRAGMENT");
|
||||
// if geometry shader is given, compile geometry shader
|
||||
unsigned int geometry;
|
||||
if (false)
|
||||
{
|
||||
//const char * gShaderCode = geometryCode.c_str();
|
||||
//geometry = glCreateShader(GL_GEOMETRY_SHADER);
|
||||
//glShaderSource(geometry, 1, &gShaderCode, NULL);
|
||||
//glCompileShader(geometry);
|
||||
//checkCompileErrors(geometry, "GEOMETRY");
|
||||
}
|
||||
// shader Program
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
if (false) // geometryPath != nullptr)
|
||||
glAttachShader(ID, geometry);
|
||||
glLinkProgram(ID);
|
||||
checkCompileErrors(ID, "PROGRAM");
|
||||
// delete the shaders as they're linked into our program now and are no longer necessary
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
if (false) //geometryPath != nullptr)
|
||||
glDeleteShader(geometry);
|
||||
}
|
||||
|
||||
void Shader::use() {
|
||||
glUseProgram(ID);
|
||||
}
|
||||
|
||||
void Shader::setBool(const std::string &name, bool value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
|
||||
void Shader::setInt(const std::string &name, int value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void Shader::setFloat(const std::string &name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
|
||||
void Shader::setVec2(const std::string &name, const Vector2 &value) const {
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), value.x, value.y);
|
||||
}
|
||||
|
||||
void Shader::setVec2(const std::string &name, float x, float y) const {
|
||||
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, const Vector3 &value) const {
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), value.x, value.y, value.z);
|
||||
}
|
||||
|
||||
void Shader::setVec3(const std::string &name, float x, float y, float z) const {
|
||||
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, const Vector4 &value) const {
|
||||
glUniform4f(glGetUniformLocation(ID, name.c_str()), value.x, value.y, value.z, value.w);
|
||||
}
|
||||
|
||||
void Shader::setVec4(const std::string &name, float x, float y, float z, float w) const {
|
||||
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
|
||||
}
|
||||
|
||||
void Shader::setMat2(const std::string &name, const Matrix2x2 &mat) const {
|
||||
//glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat.At(0, 0));
|
||||
}
|
||||
|
||||
void Shader::setMat3(const std::string &name, const Matrix3x3 &mat) const {
|
||||
//glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat.At(0, 0));
|
||||
}
|
||||
|
||||
void Shader::setMat4(const std::string &name, const Matrix4x4 &mat) const {
|
||||
//glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat.At(0, 0));
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(GLuint shader, std::string type) {
|
||||
GLint success;
|
||||
GLchar infoLog[1024];
|
||||
if (type != "PROGRAM") {
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << std::endl;
|
||||
}
|
||||
} else {
|
||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
||||
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Shader::Shader() {}
|
||||
}
|
Reference in New Issue
Block a user