OpenGL Rendering Constricted to bottom left quarter of the screen
Asked Answered
A

2

6

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: enter image description here

If I move the square into an area that's not within the 640x480 parameter it gets cut off, like below:

enter image description here

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!

Amalee answered 8/7, 2015 at 20:41 Comment(2)
Make sure you are not running into issues where window size is not equal to the framebuffer size.Temuco
I have the same issue on my mac, opengl 4.1, it randomly happened, you found a solution?Signorelli
S
6

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.

Signorelli answered 8/11, 2017 at 4:12 Comment(0)
Z
1

While Rafael's answer works, another approach you can take is simply to turn off the full resolution framebuffers on Retina displays:

glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);

(reference: the same doc Rafael linked)

Zaneta answered 9/1 at 15:11 Comment(2)
But this is worse, right? Since you get lower framebuffer resolution.Pediform
You get lower framebuffer resolution. Yes, that's my understanding. That may be worse, or could be better if you don't want to decrease performance on rendering to more pixels.Zaneta

© 2022 - 2024 — McMap. All rights reserved.