So I'm working on an OpenGL project in C++ and I'm running into a weird issue where after creating the GLFWwindow and drawing to it, the area that I'm drawing in only encompasses the bottom left quarter of the screen. For example if the screen dimensions are 640x480, and I drew a 40x40 square at (600, 440) it shows up here, instead of in the top right corner like I would expect:
If I move the square into an area that's not within the 640x480 parameter it gets cut off, like 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 would be happening, but if you have any ideas let me know, thank you!
For those of you like me who bump into this problem, you need to pass the frame buffer size to your glViewport call, like this:
GLFWwindow * window = Application::getInstance().currentWindow;
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
In some devices (mainly Apple Retina displays), the pixel dimensions don't necessarily match your viewport dimensions 1:1. Check here for GLFW documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With