From fcadf735c97df0d10e3387becf29c2aacbc44866 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 31 Aug 2020 17:55:45 +0200 Subject: [PATCH] Initialize GLFW --- src/CMakeLists.txt | 8 +-- src/PlotWindow.hpp | 0 src/ShaderProgram.hpp | 95 ----------------------------------- src/main.cpp | 96 +++--------------------------------- src/primitives/IDrawable.hpp | 18 ------- src/primitives/Quad.hpp | 49 ------------------ src/primitives/Triangle.hpp | 35 ------------- src/shaders/triangle.frag | 11 ----- src/shaders/triangle.vert | 10 ---- src/util.hpp | 6 --- 10 files changed, 10 insertions(+), 318 deletions(-) create mode 100644 src/PlotWindow.hpp delete mode 100644 src/ShaderProgram.hpp delete mode 100644 src/primitives/IDrawable.hpp delete mode 100644 src/primitives/Quad.hpp delete mode 100644 src/primitives/Triangle.hpp delete mode 100644 src/shaders/triangle.frag delete mode 100644 src/shaders/triangle.vert delete mode 100644 src/util.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index feca1ad..a4330f4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable(ComplexPlotting "main.cpp" - "ShaderProgram.hpp") + "PlotWindow.hpp") file(GLOB vendor_SRC ${CMAKE_SOURCE_DIR}/vendor/src/*.c @@ -21,6 +21,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/shaders $/shaders +#) \ No newline at end of file diff --git a/src/PlotWindow.hpp b/src/PlotWindow.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/ShaderProgram.hpp b/src/ShaderProgram.hpp deleted file mode 100644 index 99418e4..0000000 --- a/src/ShaderProgram.hpp +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include - -#include -#include - -class ShaderProgram -{ -public: - ShaderProgram() = default; - - ~ShaderProgram() - { - glDeleteShader(fragmentShader_id); - glDeleteShader(vertexShader_id); - } - - std::string VertexShader, FragmentShader; - - int Build(char** error) - { - int success; - - // Vertex Shader initialization - if (VertexShader != "") - { - vertexShader_id = glCreateShader(GL_VERTEX_SHADER); - char* source = new char; - LoadFile(VertexShader, &source); - glShaderSource(vertexShader_id, 1, &source, NULL); - glCompileShader(vertexShader_id); - - glGetShaderiv(vertexShader_id, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(vertexShader_id, 512, NULL, *error); - return success; - } - } - - // Fragment Shader initialization - if (FragmentShader != "") - { - fragmentShader_id = glCreateShader(GL_FRAGMENT_SHADER); - char* source = new char; - LoadFile(FragmentShader, &source); - glShaderSource(fragmentShader_id, 1, &source, NULL); - glCompileShader(fragmentShader_id); - - glGetShaderiv(fragmentShader_id, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(fragmentShader_id, 512, NULL, *error); - return success; - } - } - - // Link shaders - program_id = glCreateProgram(); - glAttachShader(program_id, vertexShader_id); - glAttachShader(program_id, fragmentShader_id); - glLinkProgram(program_id); - - glGetProgramiv(program_id, GL_LINK_STATUS, &success); - if (!success) - { - glGetProgramInfoLog(program_id, 512, NULL, *error); - return success; - } - - return 1; - } - - void Use() - { - glUseProgram(program_id); - } - - unsigned int vertexShader_id, fragmentShader_id, program_id; - -private: - void LoadFile(std::string path, char** source) - { - std::ifstream file(path); - std::string shader( - (std::istreambuf_iterator(file)), - std::istreambuf_iterator() - ); - size_t s = shader.size(); - *source = (char*)realloc(*source, s + sizeof(char)); - memcpy_s(*source, s, shader.c_str(), s); - (*source)[s] = '\0'; - } -}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8345dc7..ae5fa9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,97 +1,13 @@ -#include -#include - #include - -#include "ShaderProgram.hpp" -#include "Triangle.hpp" -#include "Quad.hpp" - -void LogGLFWerror() -{ - const char* error = new const char; - glfwGetError(&error); - std::cerr << error << std::endl; -} - -void FramebufferSizeCallback(GLFWwindow* window, int w, int h) -{ - glViewport(0, 0, w, h); -} +#include int main(int argc, char** argv) { - glfwInit(); - - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - GLFWwindow* window; - window = glfwCreateWindow(800, 800, "Complex Plot", NULL, NULL); - if (window == nullptr) + int result = glfwInit(); + if (result != GL_TRUE) { - LogGLFWerror(); - return -1; + const char* buffer = new char[512]; + glfwGetError(&buffer); + std::cout << "Failed to initialize GLFW: " << std::endl << buffer << std::endl; } - - glfwMakeContextCurrent(window); - - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) - { - std::cerr << "GLAD failed to initialize" << std::endl; - return -1; - } - - glViewport(0, 0, 800, 800); - glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback); - - // Create Shader Program - ShaderProgram program; - program.VertexShader = "shaders/triangle.vert"; - program.FragmentShader = "shaders/triangle.frag"; - - char* error = new char; - if (!program.Build(&error)) - { - std::cout << error << std::endl; - return -1; - } - - // Make triangle - Triangle t( - { -0.5f, -0.5f, 0.f }, - { 0.5f, -0.5f, 0.f }, - { 0.f, 0.5, 0.f } - ); - - Quad q( - { -0.5f, 0.5f, 0.0f }, - { 0.5f, 0.5f, 0.0f }, - { 0.5f, -0.5f, 0.0f }, - { -0.5f, -0.5f, 0.0f } - ); - - while (!glfwWindowShouldClose(window)) - { - glfwPollEvents(); - - float time = glfwGetTime(); - int timeUniformLocation = glGetUniformLocation(program.program_id, "t"); - - glClearColor(0.4f, 0.1f, 0.5f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - program.Use(); - glUniform1f(timeUniformLocation, time); - q.Draw(); - - glfwSwapBuffers(window); - } - - glfwDestroyWindow(window); - - glfwTerminate(); - - return 0; } \ No newline at end of file diff --git a/src/primitives/IDrawable.hpp b/src/primitives/IDrawable.hpp deleted file mode 100644 index 4bdd78b..0000000 --- a/src/primitives/IDrawable.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include - -class IDrawable -{ -public: - virtual void Draw() = 0; - -protected: - IDrawable() - { - glCreateVertexArrays(1, &VAO); - glCreateBuffers(1, &VBO); - } - - unsigned int VBO, VAO; -}; \ No newline at end of file diff --git a/src/primitives/Quad.hpp b/src/primitives/Quad.hpp deleted file mode 100644 index bb96f1e..0000000 --- a/src/primitives/Quad.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include - -#include "IDrawable.hpp" -#include "util.hpp" - -class Quad : - public IDrawable -{ -public: - Quad(Vec3 topLeft, Vec3 topRight, Vec3 botRight, Vec3 botLeft) - { - memcpy_s(vertices + 0, sizeof(vertices), &topLeft, sizeof(topLeft)); - memcpy_s(vertices + 3, sizeof(vertices), &topRight, sizeof(topRight)); - memcpy_s(vertices + 6, sizeof(vertices), &botRight, sizeof(botRight)); - memcpy_s(vertices + 9, sizeof(vertices), &botLeft, sizeof(botLeft)); - - glGenBuffers(1, &EBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - 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_ELEMENT_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - - void Draw() override - { - glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices); - } - -private: - unsigned int EBO; - float vertices[3 * 4]; - inline static unsigned int indices[6] = { - 0, 1, 3, - 1, 2, 3 - }; -}; \ No newline at end of file diff --git a/src/primitives/Triangle.hpp b/src/primitives/Triangle.hpp deleted file mode 100644 index c2d9aea..0000000 --- a/src/primitives/Triangle.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -#include "IDrawable.hpp" -#include "util.hpp" - -class Triangle : - public IDrawable -{ -public: - Triangle(Vec3 v1, Vec3 v2, Vec3 v3) - { - memcpy_s(vertices + 0, 9 * sizeof(float), &v1, 3 * sizeof(float)); - memcpy_s(vertices + 3, 9 * sizeof(float), &v2, 3 * sizeof(float)); - memcpy_s(vertices + 6, 9 * sizeof(float), &v3, 3 * sizeof(float)); - - glBindVertexArray(VAO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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 Draw() override - { - glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); - } - -private: - float vertices[9]; -}; \ No newline at end of file diff --git a/src/shaders/triangle.frag b/src/shaders/triangle.frag deleted file mode 100644 index f685067..0000000 --- a/src/shaders/triangle.frag +++ /dev/null @@ -1,11 +0,0 @@ -#version 460 core -out vec4 FragColor; - -in vec4 vertexColor; - -uniform float t; - -void main() -{ - FragColor = vertexColor * vec4((sin(t) + 1.f) / 2, (cos(t) + 1.f) / 2, (-sin(t) + 1.f), 1.f); -} \ No newline at end of file diff --git a/src/shaders/triangle.vert b/src/shaders/triangle.vert deleted file mode 100644 index c3295e0..0000000 --- a/src/shaders/triangle.vert +++ /dev/null @@ -1,10 +0,0 @@ -#version 460 core -layout (location = 0) in vec3 aPos; - -out vec4 vertexColor; - -void main() -{ - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); - vertexColor = vec4(aPos.x + 0.5, aPos.y + 0.5, 0.5, 1.0); -} \ No newline at end of file diff --git a/src/util.hpp b/src/util.hpp deleted file mode 100644 index 5264dfa..0000000 --- a/src/util.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -typedef struct -{ - float x, y, z; -} Vec3; \ No newline at end of file