engineer-thesis-WUT/Engine/engine/beforeRender.cpp

96 lines
2.9 KiB
C++
Raw Normal View History

2023-03-12 16:24:48 +01:00
// "Copyright [2023] <Krzysztof Rudnicki>"
2022-09-05 20:17:25 +02:00
#ifndef BEFORE_RENDER_CPP
#define BEFORE_RENDER_CPP
#include "./beforeRender.hpp"
#include <glad/glad.h>
2023-03-14 02:17:42 +01:00
#include <GLFW/glfw3.h>
2022-09-05 20:17:25 +02:00
#include <iostream>
2023-03-12 16:24:48 +01:00
#include "./constants.hpp"
#include "./misc.hpp"
2022-09-05 20:17:25 +02:00
2023-03-12 16:24:48 +01:00
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion) {
// first argument tells us what option to configure
// second is to what we set the value of this option
// see: https://www.glfw.org/docs/latest/window.html#window_hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
2022-09-07 18:34:50 +02:00
// we set GLFW to 3.3 CORE
2023-03-12 16:24:48 +01:00
// core profile gives us access to smaller subset of
// OGL without backwards compatible features
2022-09-05 20:17:25 +02:00
2022-09-07 18:34:50 +02:00
// if we are on Mac OS X we need this for our code to work
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
2022-09-07 18:34:50 +02:00
#endif
2022-09-05 20:17:25 +02:00
}
2023-03-12 16:24:48 +01:00
void instantiateGLFWwindow() {
// Initialize GLFW
glfwInit();
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
2022-09-05 20:17:25 +02:00
}
2023-03-12 16:24:48 +01:00
GLFWwindow *createWindowObject() {
// First two arguments are width and height
// Third is the name of the window
// We ignore last two
GLFWwindow *window = glfwCreateWindow(
constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT,
constants::MAIN_WINDOW_NAME, nullptr, nullptr);
return window;
2022-09-05 20:17:25 +02:00
}
2023-03-12 16:24:48 +01:00
int initializeGLAD() {
// we load address of OGL OS-specific function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
print("Failed to initialize GLAD");
return -1;
}
return 0;
2022-09-05 20:17:25 +02:00
}
// resizes viewport when user resizes window
void framebuffer_size_callback(GLFWwindow *window, const int width,
const int height) {
glViewport(0, 0, width, height);
2022-09-07 18:34:50 +02:00
}
2022-09-05 20:17:25 +02:00
2023-03-12 16:24:48 +01:00
void viewPort(GLFWwindow *window) {
// We tell OGL size of rendering window
// First two define left corner of window
// 3th and 4th width and height of rendering window
// we could set them to be smaller than window dimension,
// ogl rendering will be then displayed in smaller window
glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT);
// processed coordinates are between -1 and 1 so here we map:
// (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH)
// and (0, constants::MAIN_WINDOW_HEIGHT)
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// we call framebuffer_size_callback on every window resize
2022-09-05 20:17:25 +02:00
}
2023-03-12 16:24:48 +01:00
GLFWwindow *prepareForRender() {
instantiateGLFWwindow();
2022-09-05 20:17:25 +02:00
GLFWwindow *window = createWindowObject();
// function returns GLFWWindow object
2022-09-07 18:34:50 +02:00
if (window == nullptr) {
print("Failed to create GLFW window");
glfwTerminate();
2022-09-05 20:17:25 +02:00
return window;
}
// we make context of this window main context of current thread
glfwMakeContextCurrent(window);
if (initializeGLAD() == -1) return nullptr;
viewPort(window);
return window;
2022-09-05 20:17:25 +02:00
}
2023-03-12 16:24:48 +01:00
#endif