From 2fae908a559b4952c412687bdff5847117426238 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 2 Sep 2020 12:15:29 +0200 Subject: [PATCH] Added rendering --- src/CMakeLists.txt | 6 +- src/PlotWindow.cpp | 127 +++++++++++++++++++++++++++++++++++++++++- src/PlotWindow.hpp | 12 ++++ src/shader/basic.frag | 8 +++ src/shader/basic.vert | 8 +++ 5 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 src/shader/basic.frag create mode 100644 src/shader/basic.vert diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 755c266..ae94d27 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,6 +20,6 @@ target_link_libraries(ComplexPlotting glfw ) -#add_custom_command(TARGET ComplexPlotting POST_BUILD -# COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/shaders $/shaders -#) \ No newline at end of file +add_custom_command(TARGET ComplexPlotting POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/shader $/shaders +) \ No newline at end of file diff --git a/src/PlotWindow.cpp b/src/PlotWindow.cpp index 4834792..9799cc2 100644 --- a/src/PlotWindow.cpp +++ b/src/PlotWindow.cpp @@ -2,9 +2,21 @@ #include #include +#include PlotWindow::PlotWindow(int w, int h, int id, std::string title) : - window(nullptr), id(id) + window(nullptr), id(id), + vertices{ + -0.5f, 0.5f, 0.0f, + 0.5f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f + }, + indices{ + 0, 1, 3, + 1, 2, 3 + }, + vert(0), frag(0), shader(0), VAO(0), VBO(0), EBO(0) { window = glfwCreateWindow(w, h, ("Plot " + std::to_string(id) + " | " + title).c_str(), NULL, NULL); if (window == nullptr) @@ -27,10 +39,95 @@ 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); + + // Generate buffers + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glGenBuffers(1, &EBO); + + glBindVertexArray(VAO); + + // Create VBO + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + // Create EBO + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } void PlotWindow::Destroy() { + glfwMakeContextCurrent(window); + + glDeleteBuffers(1, &EBO); + glDeleteBuffers(1, &VBO); + glDeleteVertexArrays(1, &VAO); + + glDeleteProgram(shader); + glfwDestroyWindow(window); } @@ -41,16 +138,42 @@ void PlotWindow::GiveContext() void PlotWindow::Clear() { - glClearColor(0.6f, 0.1f, 0.4f, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); } void PlotWindow::Display() { + glUseProgram(shader); + + glBindVertexArray(VAO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glfwSwapBuffers(window); } 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(file)), std::istreambuf_iterator()); + 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'; +} diff --git a/src/PlotWindow.hpp b/src/PlotWindow.hpp index a8b4095..a837dbf 100644 --- a/src/PlotWindow.hpp +++ b/src/PlotWindow.hpp @@ -7,6 +7,10 @@ #pragma once +typedef unsigned int BufferObject; +typedef unsigned int Shader; +typedef unsigned int ShaderProgram; + class PlotWindow { public: @@ -24,4 +28,12 @@ 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; + BufferObject VAO, VBO, EBO; + + float vertices[4 * 3]; + unsigned int indices[2 * 3]; }; \ No newline at end of file diff --git a/src/shader/basic.frag b/src/shader/basic.frag new file mode 100644 index 0000000..ba5871e --- /dev/null +++ b/src/shader/basic.frag @@ -0,0 +1,8 @@ +#version 460 core + +out vec4 oColor; + +void main() +{ + oColor = vec4(0.6f, 0.2f, 0.9f, 1.0f); +} diff --git a/src/shader/basic.vert b/src/shader/basic.vert new file mode 100644 index 0000000..234598a --- /dev/null +++ b/src/shader/basic.vert @@ -0,0 +1,8 @@ +#version 460 core + +layout (location = 0) in vec3 aPos; + +void main() +{ + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); +}