chore: extract Triangle class

This commit is contained in:
Cat Flynn 2023-07-30 23:02:20 +02:00
parent 37dbe741fa
commit e9cdefe28a
4 changed files with 96 additions and 56 deletions

View File

@ -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

View File

@ -45,6 +45,7 @@
#include <chrono>
#include "io.hpp"
#include "triangle.hpp"
GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
{
@ -78,7 +79,6 @@ float getTime()
return timeSpan.count();
}
#include <vector>
#include <cmath>
#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;
}

45
src/triangle.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "triangle.hpp"
#include <iostream>
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);
}

26
src/triangle.hpp Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <GL/glew.h>
#include <vector>
class Triangle
{
public:
Triangle(GLuint shaderProgram);
void render(float time);
~Triangle();
private:
GLuint _vbo;
GLuint _vao;
GLuint _shaderProgram;
std::vector<float> _vertices =
{
-0.5, -0.5, 0.0, // left
0.5, -0.5, 0.0, // right
0.0, 0.5, 0.0 // top
};
};