OpenGL渲染限制在屏幕的左下角 [英] OpenGL Rendering Constricted to bottom left quarter of the screen

查看:272
本文介绍了OpenGL渲染限制在屏幕的左下角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用C ++进行OpenGL项目,遇到一个奇怪的问题:在创建GLFWwindow并绘制到它之后,我正在绘制的区域仅包含屏幕的左下角.例如,如果屏幕尺寸为640x480,并且我在(600,440)处绘制了40x40正方形,则它显示在此处,而不是我期望的那样显示在右上角:

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:

如果我将正方形移到640x480参数之外的区域,它将被切除,如下所示:

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

我将从下面的main.cpp中发布我的代码:

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!

推荐答案

对于像我这样遇到此问题的人,您需要将帧缓冲区大小传递给glViewport调用,如下所示:

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);

在某些设备(主要是Apple Retina显示器)中,像素尺寸不一定与您的视口尺寸1:1匹配.在此处中查看GLFW文档.

In some devices (mainly Apple Retina displays), the pixel dimensions don't necessarily match your viewport dimensions 1:1. Check here for GLFW documentation.

这篇关于OpenGL渲染限制在屏幕的左下角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆