OpenGL Rendering Constricted to bottom left quarter of the screen

旧时模样 提交于 2020-01-24 06:28:09

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/31303291/opengl-rendering-constricted-to-bottom-left-quarter-of-the-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!