在OpenGL中设置窗口的大小 [英] Setting the size of a window in OpenGL

查看:1571
本文介绍了在OpenGL中设置窗口的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修正:我试图在 C ++ 中使用 OpenGL 。我可以制作一个带有正方形的窗口,并使方块移动。

Fixed: I am trying to create a basic game in C++ using OpenGL. I can make a window with a square in it, and make the square move around. However, I am having trouble getting the window to be the correct size.

如果我尝试将窗口设置为800乘600,那么窗口和边框将是这样的大小,而不仅仅是边框内的位。有没有一种方法,我可以使边框内的位是我定义的大小?

If I try and make the window to be 800 by 600 then the window and borders will be this size, not just the bit inside the border. Is there a way I can make it so that the bit inside the border is the size I define?

我粘贴代码在 http://pastebin.com/jxd5YhHa

推荐答案

跳过你的代码后,我看到了很多典型的新手错误 - 这次是低级别的Win32 API风格。

After skimming over your code I saw a lot of the typical newbie mistakes – this time low level Win32 API style.

首先和最重要的:不使用裸Win32 API。有这么多好的OpenGL框架,这有额外的好处,使您的程序免费交叉plattform。在您的情况下,我建议您从 http://www.libsdl.org 下载SDL。

First and foremost: Do yourself a favor and please don't use the naked Win32 API. There are so many nice OpenGL frameworks, which have the added benefit of making your program cross plattform for free. In your case I'd recommend SDL, available from http://www.libsdl.org

现在你的程序的主要问题是,它分散了一些重要的OpenGL操作遍布整个地方。最重要的是设置视口和矩阵。您的绘图函数应始终遵循以下方案

Now the main problem with your program is, that it scattered some vital OpenGL operations all over the place. Most importantly setting up viewport and matrices. Your drawing functions should always follow the following scheme

void draw(…)
{
    glDisable(GL_SCISSOR_TEST);

    glClearColor(…);
    glClearDepth(…);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    for(subview in views) {
        glViewport(…);
    #ifdef USE_FIXED_FUNCTION_PIPELINE
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        projection_setup();

        for(model in scene) {        
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            modelview_setup();
        #else
            glUseProgram(…);
            setup_uniforms();
        #endif
            draw_stuff();
        }
    }

    SwapBuffers();
}

问题的关键在于设置视口,投影和模型视图<

The key here to your problem is, to setup viewport, projection and modelview in the drawing code right when you need it to what you need it to be.

这篇关于在OpenGL中设置窗口的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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