Skip to content
Snippets Groups Projects
Commit d9b107c9 authored by Sindre Eiklid's avatar Sindre Eiklid
Browse files

Added window resize callback

parent 104513fd
No related branches found
No related tags found
No related merge requests found
......@@ -278,6 +278,38 @@ void destroyVAO(GLuint &VAO) {
//delete VAO
glDeleteVertexArrays(1, &VAO);
}
/**
* @brief Resize frame to keep aspect ratio.
*
* @param window
* @param width
* @param height
*/
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
//set difference values
int widthDifference = width - 1024;
int heightDifference = height - 1024;
//set scene size and position according to width and height difference
if(widthDifference > 0 && heightDifference > 0) {
glViewport((widthDifference / 2) - (heightDifference / 2), 0, (width - widthDifference) + heightDifference, height);
} else if(widthDifference < 0 && heightDifference < 0) {
glViewport(-(heightDifference / 2), -(widthDifference / 2), (width + heightDifference), (height + widthDifference)); //wrong
} else if(widthDifference > 0 && heightDifference < 0) {
glViewport((widthDifference / 2) - (heightDifference / 2), 0, (width - widthDifference) + heightDifference, height);
} else if(widthDifference < 0 && heightDifference > 0) {
glViewport(0, -(widthDifference / 2) + (heightDifference / 2), width, (height + widthDifference) - heightDifference);
} else {
if(widthDifference > 0) {
glViewport((widthDifference / 2), 0, (width - widthDifference), height);
} else if(widthDifference < 0) {
glViewport(0, -(widthDifference / 2), width, (height + widthDifference));
} else if(heightDifference > 0) {
glViewport(0, (heightDifference / 2), width, (height - heightDifference));
} else if(heightDifference < 0) {
glViewport(-(heightDifference / 2), 0, (width + heightDifference), height);
}
}
}
/**
* @brief Eanable capture of debug output.
*
......
......@@ -15,6 +15,7 @@ GLuint loadModel(const std::string path, const std::string file, int &size);
GLuint loadTexture(const std::string &filepath, const GLuint slot);
GLuint createVAO(const std::vector<GLfloat> &arr, const std::vector<GLuint> &arr_indices);
void destroyVAO(GLuint &VAO);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void enableDebug();
void GLAPIENTRY messageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);
#endif
......@@ -37,6 +37,11 @@ int main() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//create window
GLFWwindow* window = glfwCreateWindow(1024, 1024, "Landscape", nullptr, nullptr);
//set framebuffer size data
int framebufferWidth = 0, framebufferHeight = 0;
glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
//declare framebuffer size callback
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//setting the OpenGL context to the window
glfwMakeContextCurrent(window);
//enable capture of cursor and focus it on the middle while hiding the icon
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment