Created shader class
This commit is contained in:
parent
48c2cd6386
commit
86468a9ce4
|
@ -1,7 +1,7 @@
|
|||
|
||||
add_executable(ComplexPlotting
|
||||
"main.cpp"
|
||||
"PlotWindow.hpp" "PlotWindow.cpp" "PlotManager.hpp" "PlotManager.cpp")
|
||||
"PlotWindow.hpp" "PlotWindow.cpp" "PlotManager.hpp" "PlotManager.cpp" "Shader.hpp" "Shader.cpp")
|
||||
|
||||
file(GLOB vendor_SRC
|
||||
${CMAKE_SOURCE_DIR}/vendor/src/*.c
|
||||
|
|
|
@ -18,7 +18,7 @@ PlotWindow::PlotWindow(int w, int h, int id, std::string title) :
|
|||
0, 1, 3,
|
||||
1, 2, 3
|
||||
},
|
||||
vert(0), frag(0), shader(0), VAO(0), VBO(0), EBO(0)
|
||||
shader(nullptr), VAO(0), VBO(0), EBO(0)
|
||||
{
|
||||
window = glfwCreateWindow(w, h, ("Plot " + std::to_string(id) + " | " + title).c_str(), NULL, NULL);
|
||||
if (window == nullptr)
|
||||
|
@ -42,61 +42,7 @@ PlotWindow::PlotWindow(int w, int h, int id, std::string title) :
|
|||
|
||||
glViewport(0, 0, w, h);
|
||||
|
||||
int success;
|
||||
char errorBuf[512];
|
||||
|
||||
// Load and compile Vertex Shader
|
||||
vert = glCreateShader(GL_VERTEX_SHADER);
|
||||
char* shaderSource = nullptr;
|
||||
LoadShaderSourceCode("shaders/basic.vert", &shaderSource);
|
||||
if (shaderSource == nullptr)
|
||||
shaderSource = "";
|
||||
|
||||
glShaderSource(vert, 1, &shaderSource, NULL);
|
||||
glCompileShader(vert);
|
||||
glGetShaderiv(vert, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vert, 512, NULL, errorBuf);
|
||||
std::cerr << "Vertex shader compilation error" << std::endl << errorBuf << std::endl;
|
||||
glfwSetWindowShouldClose(window, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
// load and compile Fragment Shader
|
||||
frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
shaderSource = nullptr;
|
||||
LoadShaderSourceCode("shaders/basic.frag", &shaderSource);
|
||||
if (shaderSource == nullptr)
|
||||
shaderSource = "";
|
||||
|
||||
glShaderSource(frag, 1, &shaderSource, NULL);
|
||||
glCompileShader(frag);
|
||||
glGetShaderiv(frag, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(frag, 512, NULL, errorBuf);
|
||||
std::cerr << "Fragment shader compilation error" << std::endl << errorBuf << std::endl;
|
||||
glfwSetWindowShouldClose(window, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Link shaders into shader program
|
||||
shader = glCreateProgram();
|
||||
glAttachShader(shader, vert);
|
||||
glAttachShader(shader, frag);
|
||||
glLinkProgram(shader);
|
||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(shader, 512, NULL, errorBuf);
|
||||
std::cerr << "Shader program linking error" << std::endl << errorBuf << std::endl;
|
||||
glfwSetWindowShouldClose(window, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
glDeleteShader(frag);
|
||||
glDeleteShader(vert);
|
||||
shader = new Shader("shaders/basic.vert", "shaders/basic.frag");
|
||||
|
||||
// Generate buffers
|
||||
glGenVertexArrays(1, &VAO);
|
||||
|
@ -130,7 +76,8 @@ void PlotWindow::Destroy()
|
|||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
|
||||
glDeleteProgram(shader);
|
||||
delete shader;
|
||||
shader = nullptr;
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
}
|
||||
|
@ -148,7 +95,7 @@ void PlotWindow::Clear()
|
|||
|
||||
void PlotWindow::Display()
|
||||
{
|
||||
glUseProgram(shader);
|
||||
shader->Use();
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
@ -161,23 +108,3 @@ void PlotWindow::FramebufferSizeCallback(GLFWwindow* window, int w, int h)
|
|||
glfwMakeContextCurrent(window);
|
||||
glViewport(0, 0, w, h);
|
||||
}
|
||||
|
||||
void PlotWindow::LoadShaderSourceCode(const char* path, char** buffer)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
if (!file.good())
|
||||
{
|
||||
std::cerr << "File not found: " << path << std::endl;
|
||||
*buffer = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string code((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
size_t len = code.size();
|
||||
*buffer = (char*)malloc(len + sizeof(char));
|
||||
if (*buffer == nullptr)
|
||||
return;
|
||||
|
||||
memcpy_s(*buffer, len, code.c_str(), len);
|
||||
(*buffer)[len] = '\0';
|
||||
}
|
||||
|
|
|
@ -5,11 +5,9 @@
|
|||
#include <glad/glad.h>
|
||||
#include <glfw/glfw3.h>
|
||||
|
||||
#pragma once
|
||||
#include "Shader.hpp"
|
||||
|
||||
typedef unsigned int BufferObject;
|
||||
typedef unsigned int Shader;
|
||||
typedef unsigned int ShaderProgram;
|
||||
|
||||
class PlotWindow
|
||||
{
|
||||
|
@ -28,10 +26,8 @@ private:
|
|||
GLFWwindow* window;
|
||||
|
||||
static void FramebufferSizeCallback(GLFWwindow* window, int w, int h);
|
||||
static void LoadShaderSourceCode(const char* path, char** buffer);
|
||||
|
||||
Shader vert, frag;
|
||||
ShaderProgram shader;
|
||||
Shader* shader;
|
||||
BufferObject VAO, VBO, EBO;
|
||||
|
||||
float vertices[4 * 3 * 4];
|
||||
|
|
99
src/Shader.cpp
Normal file
99
src/Shader.cpp
Normal file
|
@ -0,0 +1,99 @@
|
|||
#include "Shader.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
Shader::Shader(const char* vertShaderPath, const char* fragShaderPath) :
|
||||
program(0)
|
||||
{
|
||||
int success;
|
||||
char errorBuffer[512];
|
||||
|
||||
// Create Vertex shader
|
||||
unsigned int vert;
|
||||
vert = glCreateShader(GL_VERTEX_SHADER);
|
||||
char* sourceBuffer;
|
||||
LoadShaderSource(vertShaderPath, &sourceBuffer);
|
||||
if (sourceBuffer == nullptr)
|
||||
sourceBuffer = "";
|
||||
|
||||
glShaderSource(vert, 1, &sourceBuffer, NULL);
|
||||
glCompileShader(vert);
|
||||
glGetShaderiv(vert, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vert, 512, NULL, errorBuffer);
|
||||
std::cerr << vertShaderPath << std::endl << errorBuffer << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Create Fragment shader
|
||||
unsigned int frag;
|
||||
frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
LoadShaderSource(fragShaderPath, &sourceBuffer);
|
||||
if (sourceBuffer == nullptr)
|
||||
sourceBuffer = "";
|
||||
|
||||
glShaderSource(frag, 1, &sourceBuffer, NULL);
|
||||
glCompileShader(frag);
|
||||
glGetShaderiv(frag, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(frag, 512, NULL, errorBuffer);
|
||||
std::cerr << fragShaderPath << std::endl << errorBuffer << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Link shaders
|
||||
program = glCreateProgram();
|
||||
glAttachShader(program, vert);
|
||||
glAttachShader(program, frag);
|
||||
glLinkProgram(program);
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(program, 512, NULL, errorBuffer);
|
||||
std::cerr << errorBuffer << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
glDeleteShader(frag);
|
||||
glDeleteShader(vert);
|
||||
}
|
||||
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(program);
|
||||
}
|
||||
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(program);
|
||||
}
|
||||
|
||||
void Shader::LoadShaderSource(const char* filepath, char** buffer)
|
||||
{
|
||||
std::ifstream file(filepath);
|
||||
if (!file.good())
|
||||
{
|
||||
std::cerr << "Failed to open file: " << filepath << std::endl;
|
||||
*buffer = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string source(
|
||||
(std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
size_t len = source.size();
|
||||
|
||||
*buffer = (char*)malloc(len + sizeof(char));
|
||||
if (*buffer == nullptr)
|
||||
{
|
||||
std::cerr << "Failed to allocate memory for shader source code" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy_s(*buffer, len, source.c_str(), len);
|
||||
(*buffer)[len] = '\0';
|
||||
}
|
21
src/Shader.hpp
Normal file
21
src/Shader.hpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
typedef unsigned int ProgramID;
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
Shader(const char* vertShaderPath, const char* fragShaderPath);
|
||||
~Shader();
|
||||
|
||||
void Use();
|
||||
|
||||
private:
|
||||
|
||||
static void LoadShaderSource(const char* filepath, char** buffer);
|
||||
|
||||
private:
|
||||
ProgramID program;
|
||||
};
|
Loading…
Reference in a new issue