mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-24 14:46:27 +01:00
feat: split working shader into 2 files
This commit is contained in:
parent
725346d6f8
commit
2a393604c6
@ -9,7 +9,7 @@
|
|||||||
#include "shaders.hpp"
|
#include "shaders.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "misc.hpp"
|
#include "misc.hpp"
|
||||||
#include "shader.h"
|
#include "shader.hpp"
|
||||||
|
|
||||||
int drawFigure(const int whatToDraw)
|
int drawFigure(const int whatToDraw)
|
||||||
{
|
{
|
||||||
|
|||||||
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
#ifndef SHADER_H
|
#ifndef SHADER_CPP
|
||||||
#define SHADER_H
|
#define SHADER_CPP
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
@ -7,15 +7,12 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#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
|
// 1. retrieve the vertex/fragment source code from filePath
|
||||||
std::string vertexCode;
|
std::string vertexCode;
|
||||||
std::string fragmentCode;
|
std::string fragmentCode;
|
||||||
@ -67,55 +64,50 @@ public:
|
|||||||
// 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
|
||||||
glDeleteShader(vertex);
|
glDeleteShader(vertex);
|
||||||
glDeleteShader(fragment);
|
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:
|
void Shader::use()
|
||||||
// utility function for checking shader compilation/linking errors.
|
{
|
||||||
// ------------------------------------------------------------------------
|
glUseProgram(ID);
|
||||||
void checkCompileErrors(unsigned int shader, std::string type)
|
}
|
||||||
|
|
||||||
|
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;
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
char infoLog[1024];
|
if (!success)
|
||||||
if (type != "PROGRAM")
|
|
||||||
{
|
{
|
||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||||
if (!success)
|
std::cout << "ERROR::SHADER_COMPILATION_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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
else
|
||||||
#endif
|
{
|
||||||
|
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
|
||||||
@ -1,36 +1,35 @@
|
|||||||
#ifndef SHADER_HPP
|
#ifndef SHADER_HPP
|
||||||
#define 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 <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
class Shader
|
class Shader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// the program ID
|
|
||||||
unsigned int ID;
|
unsigned int ID;
|
||||||
|
// constructor generates the shader on the fly
|
||||||
// constructor reads and builds the shader
|
// ------------------------------------------------------------------------
|
||||||
Shader(const std::string vertexPath, const std::string fragmentPath);
|
Shader(const std::string vertexPath, const std::string fragmentPath);
|
||||||
// use/activate the shader
|
|
||||||
|
// activate the shader
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
void use();
|
void use();
|
||||||
// utility uniform functions
|
// utility uniform functions
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
void setBool(const std::string &name, bool value) const;
|
void setBool(const std::string &name, bool value) const;
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
void setInt(const std::string &name, int value) const;
|
void setInt(const std::string &name, int value) const;
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
void setFloat(const std::string &name, float 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
|
||||||
Loading…
Reference in New Issue
Block a user