OpenGL rendering Limited to the lower left quarter of the screen
So, I am working on an OpenGL project in C ++ and I am confused with a weird problem where after creating a GLFWwindow and drawing on it, the area I am drawing only includes the bottom left quarter of the screen, For example, if the screen dimensions are 640x480 and I drew a 40x40 (600, 440) square which is displayed here and not in the top right corner as I expected:
If I move the square to an area not within 640x480, it is cropped as shown below:
I'll post my code from main.cpp below:
#define FRAME_CAP 5000.0;
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "InputHandler.h"
#include "Game.h"
using namespace std;
void gameLoop(GLFWwindow *window){
InputHandler input = InputHandler(window);
Game game = Game(input);
//double frameTime = 1.0 / FRAME_CAP;
while(!glfwWindowShouldClose(window)){
GLint windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowWidth, 0.0, windowHeight, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, windowWidth, windowHeight);
game.render();
game.handleInput();
game.update();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
int main(int argc, const char * argv[]){
GLFWwindow *window;
if(!glfwInit()){
return -1;
}
window = glfwCreateWindow(640.0, 480.0, "OpenGL Base Project", NULL, NULL);
if(!window){
glfwTerminate();
exit(EXIT_FAILURE);
}
glewInit();
glfwMakeContextCurrent(window);
gameLoop(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
I'm not sure why this will happen, but if you have any ideas, let me know, thanks!
source to share
For those of you like me who run into this problem, you need to pass the framebuffer size to your glViewport call, for example:
GLFWwindow * window = Application::getInstance().currentWindow;
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
On some devices (mainly Apple Retina displays), pixel sizes don't necessarily match your 1: 1 view sizes. Check here for GLFW documentation.
source to share