diff --git a/CMakeLists.txt b/CMakeLists.txt index 687063d..2cc85e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ elseif(APPLE) message(FATAL_ERROR "macOS is not supported yet.") endif() -add_executable(${PROJECT_NAME} src/hello.cpp src/io.cpp) +add_executable(${PROJECT_NAME} src/hello.cpp src/io.cpp src/triangle.cpp) if(WIN32) add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD diff --git a/src/hello.cpp b/src/hello.cpp index 4f624ef..82ba3ed 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -45,6 +45,7 @@ #include #include "io.hpp" +#include "triangle.hpp" GLuint compileShader(const std::string& shaderPath, GLenum shaderType) { @@ -78,7 +79,6 @@ float getTime() return timeSpan.count(); } -#include #include #include "astro/twoBodyMethods.hpp" @@ -114,46 +114,8 @@ int initGraphics(GLFWwindow** window) return 0; } -int main() +GLuint getShaderProgram() { - // Calculate period of ISS orbit around the Earth - const float semiMajorAxis = 6738000; - const float gravitationalParameter = 3.986e14; - float period = astro::computeKeplerOrbitalPeriod(semiMajorAxis, gravitationalParameter); - period /= 60.0; - std::cout << period << std::endl; - - glm::vec3 v(0.0, 1.0, 2.0); - std::cout << "(" << v.x << ", " << v.y << ", " << v.z << ")" << std::endl; - - GLFWwindow* window = nullptr; - if (initGraphics(&window) != 0) - return -1; - - // VAO, VBO - float vertices[] = - { - -0.5, -0.5, 0.0, // left - 0.5, -0.5, 0.0, // right - 0.0, 0.5, 0.0 // top - }; - - GLuint VBO, VAO; - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - - 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); - - // Shaders GLuint vertShader = compileShader("./vert.glsl", GL_VERTEX_SHADER); GLuint fragShader = compileShader("./frag.glsl", GL_FRAGMENT_SHADER); @@ -176,32 +138,39 @@ int main() glDeleteShader(vertShader); glDeleteShader(fragShader); + return shaderProgram; +} + +int main() +{ + // Calculate period of ISS orbit around the Earth + const float semiMajorAxis = 6738000; + const float gravitationalParameter = 3.986e14; + float period = astro::computeKeplerOrbitalPeriod(semiMajorAxis, gravitationalParameter); + period /= 60.0; + std::cout << period << std::endl; + + glm::vec3 v(0.0, 1.0, 2.0); + std::cout << "(" << v.x << ", " << v.y << ", " << v.z << ")" << std::endl; + + GLFWwindow* window = nullptr; + if (initGraphics(&window) != 0) + return -1; + + Triangle triangle(getShaderProgram()); + // Main loop while (!glfwWindowShouldClose(window)) { glClearColor(0.2, 0.3, 0.3, 1.0); glClear(GL_COLOR_BUFFER_BIT); - glUseProgram(shaderProgram); - - GLint timeLocation = glGetUniformLocation(shaderProgram, "_Time"); - if (timeLocation == -1) - { - std::cerr << "Could not find uniform: _Time" << std::endl; - } - float timeValue = getTime(); - glUniform1f(timeLocation, timeValue); - - glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); + triangle.render(getTime()); glfwSwapBuffers(window); glfwPollEvents(); } - glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &VBO); - glfwTerminate(); return 0; } diff --git a/src/triangle.cpp b/src/triangle.cpp new file mode 100644 index 0000000..41c56f9 --- /dev/null +++ b/src/triangle.cpp @@ -0,0 +1,45 @@ +#include "triangle.hpp" + +#include + +Triangle::Triangle(GLuint shaderProgram) + : _shaderProgram(shaderProgram) +{ + glGenVertexArrays(1, &_vao); + glGenBuffers(1, &_vbo); + + glBindVertexArray(_vao); + + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), &_vertices[0], 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 Triangle::render(float time) +{ + glUseProgram(_shaderProgram); + + GLint timeLocation = glGetUniformLocation(_shaderProgram, "_Time"); + if (timeLocation == -1) + { + std::cerr << "Could not find uniform: _Time" << std::endl; + return; + } + float timeValue = time; + glUniform1f(timeLocation, timeValue); + + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBindVertexArray(_vao); + glDrawArrays(GL_TRIANGLES, 0, 3); +} + +Triangle::~Triangle() +{ + glDeleteVertexArrays(1, &_vao); + glDeleteBuffers(1, &_vbo); +} \ No newline at end of file diff --git a/src/triangle.hpp b/src/triangle.hpp new file mode 100644 index 0000000..4e02e4f --- /dev/null +++ b/src/triangle.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include + +class Triangle +{ +public: + Triangle(GLuint shaderProgram); + void render(float time); + + ~Triangle(); + +private: + GLuint _vbo; + GLuint _vao; + GLuint _shaderProgram; + + std::vector _vertices = + { + -0.5, -0.5, 0.0, // left + 0.5, -0.5, 0.0, // right + 0.0, 0.5, 0.0 // top + }; +}; \ No newline at end of file