feat: split working shader into 2 files

This commit is contained in:
Krzysztof Rudnicki 2022-12-11 19:52:49 +01:00
parent 725346d6f8
commit 2a393604c6
4 changed files with 67 additions and 76 deletions

View File

@ -9,7 +9,7 @@
#include "shaders.hpp"
#include "constants.hpp"
#include "misc.hpp"
#include "shader.h"
#include "shader.hpp"
int drawFigure(const int whatToDraw)
{

Binary file not shown.

View File

@ -1,5 +1,5 @@
#ifndef SHADER_H
#define SHADER_H
#ifndef SHADER_CPP
#define SHADER_CPP
#include <glad/glad.h>
@ -7,15 +7,12 @@
#include <fstream>
#include <sstream>
#include <iostream>
#include "shader.hpp"
class Shader
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
{
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const std::string vertexPath, const std::string fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
@ -67,55 +64,50 @@ public:
// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// activate the shader
// ------------------------------------------------------------------------
void use()
{
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
void setBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
}
// ------------------------------------------------------------------------
void setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
void setFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
}
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(unsigned int shader, std::string type)
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::checkCompileErrors(unsigned int shader, std::string type)
{
int success;
char infoLog[1024];
if (type != "PROGRAM")
{
int success;
char infoLog[1024];
if (type != "PROGRAM")
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << 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 << "\n -- --------------------------------------------------- -- " << std::endl;
}
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
};
#endif
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 << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
#endif // SHADER_CPP

View File

@ -1,36 +1,35 @@
#ifndef SHADER_HPP
#define SHADER_HPP
#include <glad/glad.h> // include glad to get all the required OpenGL headers
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
// the program ID
unsigned int ID;
// constructor reads and builds the shader
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const std::string vertexPath, const std::string fragmentPath);
// use/activate the shader
// activate the shader
// ------------------------------------------------------------------------
void use();
// utility uniform functions
void setBool(const std::string &name, bool value) const;
void setInt(const std::string &name, int value) const;
// ------------------------------------------------------------------------
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;
//
private:
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader) const;
const char* readFile(const std::string fileName) const;
std::pair<unsigned int, unsigned int> compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource) const;
unsigned int compileShader(const GLenum shaderType, const char *shaderSource) const;
int shaderSuccessful(const unsigned int shader, const bool compilation) const;
void shaderFailedMessage(const unsigned int shader, const bool compilation) const;
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(unsigned int shader, std::string type);
};
#endif
#endif