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

96 lines
2.9 KiB
C++
Raw Normal View History

2022-09-05 20:17:25 +02:00
#ifndef BEFORE_RENDER_CPP
#define BEFORE_RENDER_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "beforeRender.hpp"
#include "constants.hpp"
#include "misc.hpp"
2022-09-07 18:34:50 +02:00
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion)
{
2022-09-05 20:17:25 +02:00
// 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
// 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);
#endif
2022-09-05 20:17:25 +02:00
}
2022-09-07 18:34:50 +02:00
void instantiateGLFWwindow()
{
2022-09-05 20:17:25 +02:00
// Initialize GLFW
glfwInit();
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
}
2022-09-07 18:34:50 +02:00
GLFWwindow *createWindowObject()
{
// First two arguments are width and height
2022-09-05 20:17:25 +02:00
// Third is the name of the window
// We ignore last two
2022-09-07 18:34:50 +02:00
GLFWwindow *window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL);
2022-09-05 20:17:25 +02:00
return window;
}
2022-09-07 18:34:50 +02:00
int initializeGLAD()
2022-09-05 20:17:25 +02:00
{
// we load address of OGL OS-specific function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
print("Failed to initialize GLAD");
return -1;
2022-09-07 18:34:50 +02:00
}
2022-09-05 20:17:25 +02:00
return 0;
}
// resizes viewport when user resizes window
2022-09-07 18:54:59 +02:00
void framebuffer_size_callback(GLFWwindow *window, const int width, const int height)
2022-09-05 20:17:25 +02:00
{
glViewport(0, 0, width, height);
2022-09-07 18:34:50 +02:00
}
2022-09-05 20:17:25 +02:00
2022-09-07 18:34:50 +02:00
void viewPort(GLFWwindow *window)
2022-09-05 20:17:25 +02:00
{
// 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)
2022-09-07 18:34:50 +02:00
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
2022-09-05 20:17:25 +02:00
// we call framebuffer_size_callback on every window resize
}
2022-09-07 18:34:50 +02:00
GLFWwindow *prepareForRender()
2022-09-05 20:17:25 +02:00
{
instantiateGLFWwindow();
2022-09-07 18:34:50 +02:00
GLFWwindow *window = createWindowObject();
2022-09-05 20:17:25 +02:00
// function returns GLFWWindow object
2022-09-07 18:34:50 +02:00
2022-09-05 20:17:25 +02:00
if (window == NULL)
{
print("Failed to create GLFW window");
glfwTerminate();
return window;
}
// we make context of this window main context of current thread
glfwMakeContextCurrent(window);
2022-09-07 18:34:50 +02:00
if (initializeGLAD() == -1)
return NULL;
2022-09-05 20:17:25 +02:00
viewPort(window);
return window;
}
#endif