Код: Выделить всё
#ifndef __SHADER_HPP__
#define __SHADER_HPP__
#include
#include
namespace customlib
{
class Shader
{
public:
Shader(const char* vertexPath, const char* fragmentPath);
void Use();
void SetUniformBool(const char* name, bool value) const;
void SetUniformInt(const char* name, int value) const;
void SetUniformFloat(const char* name, float value) const;
uint32_t GetID();
private:
uint32_t ID;
void m_ReadShaderFiles(const char** outVertShaderSource, const char** outFragShaderSource, const char* vertexPath, const char* fragmentPath);
void m_CompileShader(uint32_t& shaderID, GLenum shaderType, const char* shaderSource);
void m_GenerateShaderProgram(uint32_t& shaderProgramID, uint32_t vertex, uint32_t fragment);
};
}
#endif
Код: Выделить всё
#include "./shader.hpp"
#include
#include
#include
#include
customlib::Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
const char* vertexShaderSource;
const char* fragmentShaderSource;
customlib::Shader::m_ReadShaderFiles(&vertexShaderSource, &fragmentShaderSource, vertexPath, fragmentPath);
uint32_t vertexID;
uint32_t fragmentID;
customlib::Shader::m_CompileShader(vertexID, GL_VERTEX_SHADER, vertexShaderSource);
customlib::Shader::m_CompileShader(fragmentID, GL_FRAGMENT_SHADER, fragmentShaderSource);
customlib::Shader::m_GenerateShaderProgram(ID, vertexID, fragmentID);
glDeleteShader(vertexID);
glDeleteShader(fragmentID);
}
void customlib::Shader::Use() { glUseProgram(ID); }
void customlib::Shader::SetUniformBool(const char* name, bool value) const
{
int uniformReference = glGetUniformLocation(ID, name);
glUniform1i(uniformReference, (int)value);
}
void customlib::Shader::SetUniformInt(const char* name, int value) const
{
int uniformReference = glGetUniformLocation(ID, name);
glUniform1i(uniformReference, value);
}
void customlib::Shader::SetUniformFloat(const char* name, float value) const
{
int uniformReference = glGetUniformLocation(ID, name);
glUniform1f(uniformReference, value);
}
uint32_t customlib::Shader::GetID() { return ID; }
void customlib::Shader::m_ReadShaderFiles(const char** outVertShaderSource, const char** outFragShaderSource, const char* vertexPath, const char* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
// Vertex Shader // Fragment Shader
vShaderFile.open(vertexPath); fShaderFile.open(fragmentPath);
std::stringstream vShaderStream; std::stringstream fShaderStream;
vShaderStream
Подробнее здесь: [url]https://stackoverflow.com/questions/79737141/visual-studio-2022-cant-solve-weird-linking-errors-imp-free-dbg-and-imp[/url]