mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-26 12:47:21 +01:00
feat: working shader class
This commit is contained in:
parent
4205679da3
commit
725346d6f8
11
.vscode/settings.json
vendored
11
.vscode/settings.json
vendored
@ -79,10 +79,19 @@
|
|||||||
"ratio": "cpp",
|
"ratio": "cpp",
|
||||||
"compare": "cpp",
|
"compare": "cpp",
|
||||||
"concepts": "cpp",
|
"concepts": "cpp",
|
||||||
"numbers": "cpp"
|
"numbers": "cpp",
|
||||||
|
"hash_map": "cpp",
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"mutex": "cpp",
|
||||||
|
"semaphore": "cpp",
|
||||||
|
"stop_token": "cpp",
|
||||||
|
"thread": "cpp"
|
||||||
},
|
},
|
||||||
"C_Cpp.errorSquiggles": "Disabled",
|
"C_Cpp.errorSquiggles": "Disabled",
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
|
"kuchy",
|
||||||
"VERTICE"
|
"VERTICE"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -1,5 +0,0 @@
|
|||||||
out vec4 FragColor;
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
|
||||||
}
|
|
||||||
6
Engine/engine/Shaders/fragmentShaderSource.fs
Normal file
6
Engine/engine/Shaders/fragmentShaderSource.fs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||||
|
}
|
||||||
@ -1,5 +0,0 @@
|
|||||||
layout (location = 0) in vec3 aPos;
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
|
||||||
}
|
|
||||||
6
Engine/engine/Shaders/vertexShaderSource.vs
Normal file
6
Engine/engine/Shaders/vertexShaderSource.vs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
|
}
|
||||||
@ -37,7 +37,7 @@ GLFWwindow *createWindowObject()
|
|||||||
// First two arguments are width and height
|
// First two arguments are width and height
|
||||||
// Third is the name of the window
|
// Third is the name of the window
|
||||||
// We ignore last two
|
// We ignore last two
|
||||||
GLFWwindow *window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL);
|
GLFWwindow *window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, nullptr, nullptr);
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ GLFWwindow *prepareForRender()
|
|||||||
GLFWwindow *window = createWindowObject();
|
GLFWwindow *window = createWindowObject();
|
||||||
// function returns GLFWWindow object
|
// function returns GLFWWindow object
|
||||||
|
|
||||||
if (window == NULL)
|
if (window == nullptr)
|
||||||
{
|
{
|
||||||
print("Failed to create GLFW window");
|
print("Failed to create GLFW window");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
@ -88,7 +88,7 @@ GLFWwindow *prepareForRender()
|
|||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
if (initializeGLAD() == -1)
|
if (initializeGLAD() == -1)
|
||||||
return NULL;
|
return nullptr;
|
||||||
viewPort(window);
|
viewPort(window);
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,8 +53,8 @@ namespace constants
|
|||||||
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||||
"}\0" } ;
|
"}\0" } ;
|
||||||
|
|
||||||
inline const std::string VERTEX_SHADER_SOURCE_FILENAME {
|
inline const char* VERTEX_SHADER_SOURCE_FILENAME {
|
||||||
"Engine/engine/Shaders/vertexShaderSource.vert"
|
"./Shaders/vertexShaderSource.vs"
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char *VERTEX_SHADER_VERTICE_COLOR { "#version 330 core\n"
|
inline const char *VERTEX_SHADER_VERTICE_COLOR { "#version 330 core\n"
|
||||||
@ -100,8 +100,8 @@ namespace constants
|
|||||||
"}\0"
|
"}\0"
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const std::string FRAGMENT_SHADER_SOURCE_FILENAME {
|
inline const char* FRAGMENT_SHADER_SOURCE_FILENAME {
|
||||||
"Engine/engine/Shaders/fragmentShaderSource.frag"
|
"./Shaders/fragmentShaderSource.fs"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -9,14 +9,14 @@
|
|||||||
#include "shaders.hpp"
|
#include "shaders.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "misc.hpp"
|
#include "misc.hpp"
|
||||||
#include "shader.hpp"
|
#include "shader.h"
|
||||||
|
|
||||||
int drawFigure(const int whatToDraw)
|
int drawFigure(const int whatToDraw)
|
||||||
{
|
{
|
||||||
switch (whatToDraw)
|
switch (whatToDraw)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE, false);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false);
|
||||||
case 1:
|
case 1:
|
||||||
return drawSquare(constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
|
return drawSquare(constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
|
||||||
case 2:
|
case 2:
|
||||||
@ -45,7 +45,19 @@ int drawFigure(const int whatToDraw)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const std::string vertexPath, const std::string fragmentPath, const bool colorIncluded)
|
unsigned int getShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||||
|
{
|
||||||
|
const std::pair<unsigned int, unsigned int> shaders = compileShaders(vertexShaderSource, fragmentShaderSource);
|
||||||
|
if (shaders.first == 0 || shaders.second == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
|
||||||
|
if (shaderProgram == 0)
|
||||||
|
return 0;
|
||||||
|
return shaderProgram;
|
||||||
|
}
|
||||||
|
|
||||||
|
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded)
|
||||||
{
|
{
|
||||||
Shader ourShader(vertexPath, fragmentPath);
|
Shader ourShader(vertexPath, fragmentPath);
|
||||||
ourShader.use();
|
ourShader.use();
|
||||||
@ -59,16 +71,21 @@ int drawTriangleClass(const float triangleVertices[], const size_t triangleVerti
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int getShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
|
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded)
|
||||||
{
|
{
|
||||||
const std::pair<unsigned int, unsigned int> shaders = compileShaders(vertexShaderSource, fragmentShaderSource);
|
|
||||||
if (shaders.first == 0 || shaders.second == 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
|
const unsigned int shaderProgram = getShaderProgram(vertexShaderSource, fragmentShaderSource);
|
||||||
if (shaderProgram == 0)
|
if (shaderProgram == 0)
|
||||||
return 0;
|
return -1;
|
||||||
return shaderProgram;
|
|
||||||
|
const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
||||||
|
const unsigned int vertexArrayObject = generateBindVAO();
|
||||||
|
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
||||||
|
|
||||||
|
// set vertex attribute pointers
|
||||||
|
configureVertexAttribute(colorIncluded);
|
||||||
|
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource)
|
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||||
@ -95,22 +112,7 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA
|
|||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded)
|
|
||||||
{
|
|
||||||
|
|
||||||
const unsigned int shaderProgram = getShaderProgram(vertexShaderSource, fragmentShaderSource);
|
|
||||||
if (shaderProgram == 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
|
||||||
const unsigned int vertexArrayObject = generateBindVAO();
|
|
||||||
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
|
||||||
|
|
||||||
// set vertex attribute pointers
|
|
||||||
configureVertexAttribute(colorIncluded);
|
|
||||||
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName)
|
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -8,7 +8,7 @@ int drawFigure(const int whatToDraw);
|
|||||||
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource);
|
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||||
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw);
|
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw);
|
||||||
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded);
|
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded);
|
||||||
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const std::string vertexPath, const std::string fragmentPath, const bool colorIncluded);
|
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded);
|
||||||
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName);
|
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName);
|
||||||
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
|
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
|
||||||
unsigned int drawSquareShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource);
|
unsigned int drawSquareShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||||
|
|||||||
Binary file not shown.
@ -17,7 +17,7 @@ int main()
|
|||||||
{
|
{
|
||||||
// changed in glfwWindowShouldClose
|
// changed in glfwWindowShouldClose
|
||||||
GLFWwindow *window = prepareForRender();
|
GLFWwindow *window = prepareForRender();
|
||||||
if (window == NULL)
|
if (window == nullptr)
|
||||||
return -1;
|
return -1;
|
||||||
renderLoop(window);
|
renderLoop(window);
|
||||||
|
|
||||||
|
|||||||
@ -1,158 +0,0 @@
|
|||||||
#ifndef SHADER_CPP
|
|
||||||
#define SHADER_CPP
|
|
||||||
#include "shader.hpp"
|
|
||||||
#include "misc.hpp"
|
|
||||||
#include <glad/glad.h> // include glad to get all the required OpenGL headers
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
|
|
||||||
{
|
|
||||||
const char* vertexSource = this->readFile(vertexPath);
|
|
||||||
const char* shaderSource = this->readFile(fragmentPath);
|
|
||||||
const std::pair<unsigned int, unsigned int> shaders = this->compileShaders(vertexSource, shaderSource);
|
|
||||||
|
|
||||||
this->ID = this->linkShaderObjectsShaderProgram(shaders.first, shaders.second);
|
|
||||||
}
|
|
||||||
// use/activate the shader
|
|
||||||
void Shader::use()
|
|
||||||
{
|
|
||||||
glUseProgram(ID);
|
|
||||||
}
|
|
||||||
// utility uniform functions
|
|
||||||
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::shaderFailedMessage(const unsigned int shader, const bool compilation) const
|
|
||||||
{
|
|
||||||
char infoLog[512];
|
|
||||||
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
|
||||||
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
|
|
||||||
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
|
|
||||||
std::cout << "ERROR vertex shader compilation failed \n"
|
|
||||||
<< infoLog << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Shader::shaderSuccessful(const unsigned int shader, const bool compilation) const
|
|
||||||
{
|
|
||||||
// check if compilation was successful
|
|
||||||
// int because glGetShaderiv requires int
|
|
||||||
int success;
|
|
||||||
// here we store info about compilation
|
|
||||||
// check if compilation was successful
|
|
||||||
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
|
|
||||||
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
|
||||||
// if not display compilation log
|
|
||||||
if (!success)
|
|
||||||
{
|
|
||||||
shaderFailedMessage(shader, compilation);
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int Shader::compileShader(const GLenum shaderType, const char *shaderSource) const
|
|
||||||
{
|
|
||||||
// we create vertex shader and assign its id to shader variable
|
|
||||||
const unsigned int shaderID = glCreateShader(shaderType);
|
|
||||||
|
|
||||||
// attach shader source code to shader object
|
|
||||||
// from left: shader object to compile, how many strings as source code, actual source code (we leave the 4th as NULL)
|
|
||||||
glShaderSource(shaderID, 1, &shaderSource, NULL);
|
|
||||||
// compile shader
|
|
||||||
glCompileShader(shaderID);
|
|
||||||
if (!shaderSuccessful(shaderID, true))
|
|
||||||
return 0;
|
|
||||||
return shaderID;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* Shader::readFile(const std::string fileName) const
|
|
||||||
{
|
|
||||||
// 1. retrieve the vertex/fragment source code from filePath
|
|
||||||
std::string shaderCode;
|
|
||||||
std::ifstream shaderFile;
|
|
||||||
// ensure ifstream objects can throw exceptions:
|
|
||||||
shaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// open file
|
|
||||||
shaderFile.open(fileName);
|
|
||||||
std::stringstream shaderStream;
|
|
||||||
// read file buffer contents into streams
|
|
||||||
shaderStream << shaderFile.rdbuf();
|
|
||||||
// close file handlers
|
|
||||||
shaderFile.close();
|
|
||||||
// convert stream into string
|
|
||||||
shaderCode = shaderStream.str();
|
|
||||||
}
|
|
||||||
catch(std::ifstream::failure const& e)
|
|
||||||
{
|
|
||||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
|
|
||||||
}
|
|
||||||
const char* vShaderCode = shaderCode.c_str();
|
|
||||||
return vShaderCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int Shader::linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader) const
|
|
||||||
{
|
|
||||||
// link shader objects into shader program
|
|
||||||
// will store shader program id
|
|
||||||
// creates program
|
|
||||||
const unsigned int programID = glCreateProgram();
|
|
||||||
|
|
||||||
// attachShaders
|
|
||||||
glAttachShader(programID, vertexShaders);
|
|
||||||
glAttachShader(programID, fragmentShader);
|
|
||||||
|
|
||||||
// link shaders
|
|
||||||
glLinkProgram(programID);
|
|
||||||
if (!shaderSuccessful(programID, false))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// activate program
|
|
||||||
// after that every shader and rendering call will use this program object
|
|
||||||
glUseProgram(programID);
|
|
||||||
|
|
||||||
// delete shaders (they are linked into shaderProgram and we do not need them anymore)
|
|
||||||
glDeleteShader(vertexShaders);
|
|
||||||
glDeleteShader(fragmentShader);
|
|
||||||
if (programID == 0)
|
|
||||||
print("Shader Program Linking Failed");
|
|
||||||
|
|
||||||
return programID;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<unsigned int, unsigned int> Shader::compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource) const
|
|
||||||
{
|
|
||||||
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
|
||||||
if (vertexShader == 0)
|
|
||||||
{
|
|
||||||
print("Vertex Shader Compilation Failed");
|
|
||||||
return std::make_pair(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
|
||||||
if (fragmentShader == 0)
|
|
||||||
{
|
|
||||||
print("Fragment Shader Compilation Failed");
|
|
||||||
return std::make_pair(0, 0);
|
|
||||||
}
|
|
||||||
return std::make_pair(vertexShader, fragmentShader);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
121
Engine/engine/shader.h
Normal file
121
Engine/engine/shader.h
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Shader
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
std::ifstream vShaderFile;
|
||||||
|
std::ifstream fShaderFile;
|
||||||
|
// ensure ifstream objects can throw exceptions:
|
||||||
|
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// open files
|
||||||
|
vShaderFile.open(vertexPath);
|
||||||
|
fShaderFile.open(fragmentPath);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
catch (std::ifstream::failure& e)
|
||||||
|
{
|
||||||
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_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");
|
||||||
|
// shader Program
|
||||||
|
ID = glCreateProgram();
|
||||||
|
glAttachShader(ID, vertex);
|
||||||
|
glAttachShader(ID, fragment);
|
||||||
|
glLinkProgram(ID);
|
||||||
|
checkCompileErrors(ID, "PROGRAM");
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
int success;
|
||||||
|
char 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 << "\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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
@ -40,8 +40,8 @@ unsigned int compileShader(const GLenum shaderType, const char *shaderSource)
|
|||||||
const unsigned int shaderID = glCreateShader(shaderType);
|
const unsigned int shaderID = glCreateShader(shaderType);
|
||||||
|
|
||||||
// attach shader source code to shader object
|
// attach shader source code to shader object
|
||||||
// from left: shader object to compile, how many strings as source code, actual source code (we leave the 4th as NULL)
|
// from left: shader object to compile, how many strings as source code, actual source code (we leave the 4th as nullptr)
|
||||||
glShaderSource(shaderID, 1, &shaderSource, NULL);
|
glShaderSource(shaderID, 1, &shaderSource, nullptr);
|
||||||
// compile shader
|
// compile shader
|
||||||
glCompileShader(shaderID);
|
glCompileShader(shaderID);
|
||||||
if (!shaderSuccessful(shaderID, true))
|
if (!shaderSuccessful(shaderID, true))
|
||||||
@ -71,8 +71,8 @@ void shaderFailedMessage(const unsigned int shader, const bool compilation)
|
|||||||
{
|
{
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
||||||
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
|
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
|
||||||
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
|
: glGetProgramInfoLog(shader, sizeOfInfoLog, nullptr, infoLog);
|
||||||
std::cout << "ERROR vertex shader compilation failed \n"
|
std::cout << "ERROR vertex shader compilation failed \n"
|
||||||
<< infoLog << std::endl;
|
<< infoLog << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user