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,14 +7,11 @@
#include <fstream>
#include <sstream>
#include <iostream>
#include "shader.hpp"
class Shader
{
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const std::string vertexPath, const std::string fragmentPath)
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
@ -68,33 +65,28 @@ public:
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// activate the shader
// ------------------------------------------------------------------------
void use()
void Shader::use()
{
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
void setBool(const std::string &name, bool value) const
void Shader::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
void Shader::setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
void setFloat(const std::string &name, float value) const
void Shader::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::checkCompileErrors(unsigned int shader, std::string type)
{
int success;
char infoLog[1024];
@ -117,5 +109,5 @@ private:
}
}
}
};
#endif
#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 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