refactor: extract gfx functions
This commit is contained in:
parent
37025fbb09
commit
4795d88b91
88
src/gfx.cpp
88
src/gfx.cpp
|
@ -2,8 +2,52 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#include "glm/glm.hpp"
|
||||
#include "glm/gtc/matrix_transform.hpp"
|
||||
|
||||
#include "io.hpp"
|
||||
|
||||
const int WIDTH = 640;
|
||||
const int HEIGHT = 480;
|
||||
const float ASPECT = (float)WIDTH / (float)HEIGHT;
|
||||
|
||||
// Initialize GLFW, OpenGL and GLEW, open a window and make it the current context.
|
||||
// Returns 0 for success, and -1 for any failure.
|
||||
// Failures are printed to STDERR.
|
||||
int initGraphics(GLFWwindow** window, const std::string& title)
|
||||
{
|
||||
// Set up GLFW, OpenGL and GLEW.
|
||||
if (!glfwInit())
|
||||
{
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
*window = glfwCreateWindow(WIDTH, HEIGHT, title.c_str(), NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
glfwTerminate();
|
||||
std::cerr << "Failed to open window with GLFW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(*window);
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK)
|
||||
{
|
||||
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
|
||||
{
|
||||
GLuint shader;
|
||||
|
@ -56,3 +100,47 @@ GLuint compileShaderProgram(const std::string& fragShaderPath)
|
|||
return shaderProgram;
|
||||
}
|
||||
|
||||
void updateProjectionMatrix(GLuint shaderProgram)
|
||||
{
|
||||
float left = -ASPECT, right = ASPECT, bottom = -1.0, top = 1.0, near = -1.0, far = 1.0;
|
||||
glm::mat4 projection = glm::ortho(left, right, bottom, top, near, far);
|
||||
GLint projectionLocation = getShaderUniformLocation(shaderProgram, "_Projection");
|
||||
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]);
|
||||
}
|
||||
|
||||
void updateModelMatrix(GLuint shaderProgram, float time)
|
||||
{
|
||||
constexpr float angle = glm::radians(10.0);
|
||||
glm::vec3 axis = glm::vec3(0.0, 1.0, 0.0);
|
||||
glm::mat4 model = glm::rotate(glm::mat4(1.0), angle * time, axis);
|
||||
GLint modelLocation = getShaderUniformLocation(shaderProgram, "_Model");
|
||||
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, &model[0][0]);
|
||||
}
|
||||
|
||||
void updateViewMatrix(GLuint shaderProgram)
|
||||
{
|
||||
glm::mat4 view = glm::mat4(1.0);
|
||||
GLint viewLocation = getShaderUniformLocation(shaderProgram, "_View");
|
||||
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, &view[0][0]);
|
||||
}
|
||||
|
||||
void updateModelViewProjectionMatrix(GLuint shaderProgram, float time)
|
||||
{
|
||||
// Calculate matrices
|
||||
updateProjectionMatrix(shaderProgram);
|
||||
updateModelMatrix(shaderProgram, time);
|
||||
updateViewMatrix(shaderProgram);
|
||||
}
|
||||
|
||||
GLint getShaderUniformLocation(GLuint shaderProgram, const std::string& uniformName)
|
||||
{
|
||||
GLint location = glGetUniformLocation(shaderProgram, uniformName.c_str());
|
||||
if (location == -1)
|
||||
{
|
||||
std::cerr << "Could not find uniform: " << uniformName << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
|
|
13
src/gfx.hpp
13
src/gfx.hpp
|
@ -3,6 +3,19 @@
|
|||
#include <string>
|
||||
|
||||
#include "GL/glew.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
extern const int WIDTH;
|
||||
extern const int HEIGHT;
|
||||
extern const float ASPECT;
|
||||
|
||||
int initGraphics(GLFWwindow** window, const std::string& title);
|
||||
|
||||
GLuint compileShaderProgram(const std::string& fragShaderPath);
|
||||
GLuint compileShader(const std::string& shaderPath, GLenum shaderType);
|
||||
GLint getShaderUniformLocation(GLuint shaderProgram, const std::string& uniformName);
|
||||
|
||||
void updateProjectionMatrix(GLuint shaderProgram);
|
||||
void updateModelMatrix(GLuint shaderProgram, float time);
|
||||
void updateViewMatrix(GLuint shaderProgram);
|
||||
void updateModelViewProjectionMatrix(GLuint shaderProgram, float time);
|
105
src/main.cpp
105
src/main.cpp
|
@ -35,120 +35,17 @@
|
|||
// cmake ..
|
||||
// cmake --build .
|
||||
|
||||
#include "glm/glm.hpp"
|
||||
#include "glm/gtc/matrix_transform.hpp"
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "gfx.hpp"
|
||||
#include "icosphere.hpp"
|
||||
#include "orbit.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include "astro/twoBodyMethods.hpp"
|
||||
|
||||
const int WIDTH = 640;
|
||||
const int HEIGHT = 480;
|
||||
const float ASPECT = (float)WIDTH / (float)HEIGHT;
|
||||
|
||||
// Initialize GLFW, OpenGL and GLEW, open a window and make it the current context.
|
||||
// Returns 0 for success, and -1 for any failure.
|
||||
// Failures are printed to STDERR.
|
||||
int initGraphics(GLFWwindow** window)
|
||||
{
|
||||
// Set up GLFW, OpenGL and GLEW.
|
||||
if (!glfwInit())
|
||||
{
|
||||
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
*window = glfwCreateWindow(WIDTH, HEIGHT, "Hello Astro", NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
glfwTerminate();
|
||||
std::cerr << "Failed to open window with GLFW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(*window);
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK)
|
||||
{
|
||||
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
GLint getShaderUniformLocation(GLuint shaderProgram, const std::string& uniformName)
|
||||
{
|
||||
GLint location = glGetUniformLocation(shaderProgram, uniformName.c_str());
|
||||
if (location == -1)
|
||||
{
|
||||
std::cerr << "Could not find uniform: " << uniformName << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
void updateProjectionMatrix(GLuint shaderProgram)
|
||||
{
|
||||
float left = -ASPECT, right = ASPECT, bottom = -1.0, top = 1.0, near = -1.0, far = 1.0;
|
||||
glm::mat4 projection = glm::ortho(left, right, bottom, top, near, far);
|
||||
GLint projectionLocation = getShaderUniformLocation(shaderProgram, "_Projection");
|
||||
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]);
|
||||
}
|
||||
|
||||
void updateModelMatrix(GLuint shaderProgram, float time)
|
||||
{
|
||||
constexpr float angle = glm::radians(10.0);
|
||||
glm::vec3 axis = glm::vec3(0.0, 1.0, 0.0);
|
||||
glm::mat4 model = glm::rotate(glm::mat4(1.0), angle * time, axis);
|
||||
GLint modelLocation = getShaderUniformLocation(shaderProgram, "_Model");
|
||||
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, &model[0][0]);
|
||||
}
|
||||
|
||||
void updateViewMatrix(GLuint shaderProgram)
|
||||
{
|
||||
glm::mat4 view = glm::mat4(1.0);
|
||||
GLint viewLocation = getShaderUniformLocation(shaderProgram, "_View");
|
||||
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, &view[0][0]);
|
||||
}
|
||||
|
||||
void updateModelViewProjectionMatrix(GLuint shaderProgram, float time)
|
||||
{
|
||||
// Calculate matrices
|
||||
updateProjectionMatrix(shaderProgram);
|
||||
updateModelMatrix(shaderProgram, time);
|
||||
updateViewMatrix(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)
|
||||
if (initGraphics(&window, "Hello Astro") != 0)
|
||||
return -1;
|
||||
|
||||
GLuint litProgram = compileShaderProgram("./frag_lit.glsl");
|
||||
|
|
Loading…
Reference in New Issue