2023-07-23 16:46:30 +02:00
|
|
|
// To compile on Windows
|
|
|
|
|
2023-07-24 23:15:49 +02:00
|
|
|
// Install GLFW 3.3.8
|
|
|
|
// https://www.glfw.org/download.html
|
2023-07-26 23:29:51 +02:00
|
|
|
// Install GLEW 2.2.0
|
|
|
|
// https://github.com/nigels-com/glew/releases/tag/glew-2.2.0
|
|
|
|
//
|
|
|
|
// extract the downloaded .zip files to "C:/libs"; this is currently expected
|
2023-07-24 23:15:49 +02:00
|
|
|
// by our CMakeLists.txt.
|
|
|
|
|
2023-07-23 16:46:30 +02:00
|
|
|
// Install CMake
|
|
|
|
// https://cmake.org/download
|
|
|
|
// Add to PATH for all users
|
|
|
|
// from project root:
|
|
|
|
// mkdir build
|
|
|
|
// cd build
|
|
|
|
// cmake ..
|
|
|
|
// cmake --build .
|
2023-07-23 17:46:53 +02:00
|
|
|
// The last step compiles the executable - this can also be done from Visual
|
|
|
|
// Studio
|
2023-07-23 16:46:30 +02:00
|
|
|
|
2023-07-23 17:46:53 +02:00
|
|
|
// To run in VS
|
|
|
|
// Set startup project in Solution Explorer
|
2023-07-24 23:15:49 +02:00
|
|
|
// Press F5 to run
|
2023-08-02 00:29:52 +02:00
|
|
|
|
|
|
|
// To run in VSCode
|
|
|
|
// https://code.visualstudio.com/docs/cpp/config-mingw
|
|
|
|
|
2023-07-24 23:15:49 +02:00
|
|
|
// To compile on Arch Linux
|
|
|
|
//
|
|
|
|
// Install dependencies
|
2023-07-24 23:31:58 +02:00
|
|
|
// sudo pacman -S glfw mesa glew
|
2023-07-24 23:15:49 +02:00
|
|
|
//
|
|
|
|
// Build
|
|
|
|
// cmake ..
|
|
|
|
// cmake --build .
|
2023-07-23 17:46:53 +02:00
|
|
|
|
2023-08-02 23:35:52 +02:00
|
|
|
#include "glm/glm.hpp"
|
|
|
|
#include "glm/gtc/matrix_transform.hpp"
|
2023-07-24 23:31:58 +02:00
|
|
|
#include <GL/glew.h>
|
2023-07-23 17:46:53 +02:00
|
|
|
#include <GLFW/glfw3.h>
|
2023-07-23 16:45:15 +02:00
|
|
|
|
2023-07-24 23:31:58 +02:00
|
|
|
#include <iostream>
|
2023-07-24 23:54:37 +02:00
|
|
|
#include <string>
|
|
|
|
|
2023-07-31 00:08:17 +02:00
|
|
|
#include "gfx.hpp"
|
2023-07-30 23:02:20 +02:00
|
|
|
#include "triangle.hpp"
|
2023-08-02 01:52:04 +02:00
|
|
|
#include "icosphere.hpp"
|
2023-07-24 23:54:37 +02:00
|
|
|
|
2023-07-27 01:04:23 +02:00
|
|
|
#include <cmath>
|
|
|
|
#include "astro/twoBodyMethods.hpp"
|
|
|
|
|
2023-07-28 01:39:37 +02:00
|
|
|
// 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)
|
2023-07-23 16:45:15 +02:00
|
|
|
{
|
2023-07-24 23:54:37 +02:00
|
|
|
// Set up GLFW, OpenGL and GLEW.
|
2023-07-23 17:46:53 +02:00
|
|
|
if (!glfwInit())
|
2023-07-28 01:39:37 +02:00
|
|
|
{
|
|
|
|
std::cerr << "Failed to initialize GLFW" << std::endl;
|
2023-07-23 17:46:53 +02:00
|
|
|
return -1;
|
2023-07-28 01:39:37 +02:00
|
|
|
}
|
2023-07-23 17:46:53 +02:00
|
|
|
|
2023-07-28 01:39:37 +02:00
|
|
|
*window = glfwCreateWindow(640, 480, "Hello Astro", NULL, NULL);
|
2023-07-23 17:46:53 +02:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
2023-07-28 01:39:37 +02:00
|
|
|
std::cerr << "Failed to open window with GLFW" << std::endl;
|
2023-07-23 17:46:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:39:37 +02:00
|
|
|
glfwMakeContextCurrent(*window);
|
2023-07-23 17:46:53 +02:00
|
|
|
|
2023-07-24 23:31:58 +02:00
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
if (glewInit() != GLEW_OK)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to initialize GLEW" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-07-28 01:39:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:35:52 +02:00
|
|
|
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 updateModelViewProjectionMatrix(GLuint shaderProgram, float time)
|
|
|
|
{
|
|
|
|
// Calculate matrices
|
|
|
|
float left = -1.0, right = 1.0, bottom = -1.0, top = 1.0, near = -1.0, far = 1.0;
|
|
|
|
glm::mat4 projection = glm::ortho(left, right, bottom, top, near, far);
|
|
|
|
|
|
|
|
constexpr float angle = glm::radians(50.0);
|
|
|
|
glm::vec3 axis = glm::vec3(0.0, 1.0, 0.0);
|
|
|
|
glm::mat4 model = glm::rotate(glm::mat4(1.0), angle * time, axis);
|
|
|
|
|
|
|
|
glm::mat4 view = glm::mat4(1.0);
|
|
|
|
|
|
|
|
glm::mat4 mvp = projection * model * view;
|
|
|
|
|
|
|
|
GLint mvpLocation = getShaderUniformLocation(shaderProgram, "_ModelViewProjectionMatrix");
|
|
|
|
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &mvp[0][0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateTime(GLuint shaderProgram, float time)
|
|
|
|
{
|
|
|
|
GLint timeLocation = getShaderUniformLocation(shaderProgram, "_Time");
|
|
|
|
float timeValue = time;
|
|
|
|
glUniform1f(timeLocation, timeValue);
|
|
|
|
}
|
|
|
|
|
2023-07-30 23:02:20 +02:00
|
|
|
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;
|
|
|
|
|
2023-08-02 01:52:04 +02:00
|
|
|
GLuint shaderProgram = compileShaderProgram();
|
2023-08-02 23:35:52 +02:00
|
|
|
Triangle triangle;
|
|
|
|
Icosphere sphere(0);
|
2023-07-30 23:02:20 +02:00
|
|
|
|
2023-08-02 01:52:04 +02:00
|
|
|
// Wireframe
|
2023-08-02 00:56:45 +02:00
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
|
2023-07-27 21:54:06 +02:00
|
|
|
// Main loop
|
2023-07-23 17:46:53 +02:00
|
|
|
while (!glfwWindowShouldClose(window))
|
|
|
|
{
|
2023-07-24 23:54:37 +02:00
|
|
|
glClearColor(0.2, 0.3, 0.3, 1.0);
|
2023-07-23 18:21:44 +02:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2023-08-02 23:35:52 +02:00
|
|
|
// Render everything with the same shaders
|
|
|
|
glUseProgram(shaderProgram);
|
|
|
|
|
|
|
|
// Update uniforms
|
2023-08-02 01:52:04 +02:00
|
|
|
float time = glfwGetTime();
|
2023-08-02 23:35:52 +02:00
|
|
|
updateTime(shaderProgram, time);
|
|
|
|
updateModelViewProjectionMatrix(shaderProgram, time);
|
|
|
|
|
|
|
|
// Render objects
|
2023-08-02 01:52:04 +02:00
|
|
|
triangle.render(time);
|
|
|
|
sphere.render(time);
|
2023-07-23 18:21:44 +02:00
|
|
|
|
2023-07-24 23:54:37 +02:00
|
|
|
glfwSwapBuffers(window);
|
2023-07-23 17:46:53 +02:00
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
2023-07-23 16:45:15 +02:00
|
|
|
return 0;
|
2023-07-24 23:15:49 +02:00
|
|
|
}
|