C++ openGL中的全屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15891856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Full screen in openGL
提问by Matt
I am trying to render an openGL window in fullscreen and am using the NeHe tutorials to learn how to do this. however I have reached a point where I am using the exact same code in both the example code given and my own code, but when it reaches this line:
我正在尝试以全屏方式渲染 openGL 窗口,并且正在使用 NeHe 教程来学习如何执行此操作。但是我已经达到了在给定的示例代码和我自己的代码中使用完全相同的代码的地步,但是当它到达这一行时:
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
this doesn't evaluate to true in my code, even though it does in the example code given. this is even more confusing as while de-bugging everything was exactly the same up to this point.
这在我的代码中不会评估为真,即使它在给出的示例代码中也是如此。这更令人困惑,因为到目前为止调试一切都完全相同。
Is there something simple I'm missing such as something in the project properties, or if not, could someone advise me on any other ways of creating a full screen window.
是否有一些简单的东西我遗漏了,例如项目属性中的某些东西,或者如果没有,有人可以就创建全屏窗口的任何其他方法向我提出建议。
NeHe tutorial I am using: http://nehe.gamedev.net/tutorial/creating_an_opengl_window_%28win32%29/13001/
我正在使用的 NeHe 教程:http: //nehe.gamedev.net/tutorial/creating_an_opengl_window_%28win32%29/13001/
回答by Alex
If you're just learning, you could try using GLUT. You can create a window with it in a few lines, and you can just mess with your OpenGL code, until you're comfortable with it enough to actually try out platform specific APIs for doing so such as WinAPI.
如果你只是在学习,你可以尝试使用 GLUT。您可以用它在几行中创建一个窗口,并且您可以弄乱您的 OpenGL 代码,直到您对它感到满意为止,可以实际尝试使用特定于平台的 API 来执行此操作,例如 WinAPI。
You'll need to install Freeglut (implementation of the outdated GLUT), and GLEW (for the ease of using OpenGL 1.1+ functions because Microsoft's gl.h
hasn't been updated since then)
您需要安装 Freeglut(过时的 GLUT 的实现)和 GLEW(为了便于使用 OpenGL 1.1+ 功能,因为gl.h
自那时起Microsoft尚未更新)
Bare minimum code:
最低限度的代码:
#define FREEGLUT_STATIC // defined so you can link to freeglut_static.lib when compiling
#define GLEW_STATIC // defined so you can link to glew's static .lib when compiling
#include <GL/glew.h> // has to be included before gl.h, or any header that includes gl.h
#include <GL/freeglut.h>
void draw()
{
// code for rendering here
glutSwapBuffers(); // swapping image buffer for double buffering
glutPostRedisplay(); // redrawing. Omit this line if you don't want constant redraw
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // enabling double buffering and RGBA
glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL"); // creating the window
glutFullScreen(); // making the window full screen
glutDisplayFunc(draw); // draw is your function for redrawing the screen
glutMainLoop();
return 0;
}
回答by datenwolf
Windows tends to be very picky in what you put into structures like DEVMODE
. Take a look at DEVMODE
, this structure is hugeand has a lot of entries completely irrelevant for monitors, for example paper dimensions (it turns out the same structure can be used for printers as well).
Windows 在您放入DEVMODE
. 看看DEVMODE
,这个结构很大,有很多与显示器完全无关的条目,例如纸张尺寸(事实证明,相同的结构也可用于打印机)。
Now if you build a DEVMODE yourself, the odds are, you're building something Windows won't like. This also goes for a lot of other structures of this kind, for example serial port settings are quite as picky.
现在,如果您自己构建 DEVMODE,则很有可能您正在构建 Windows 不喜欢的东西。这也适用于许多其他此类结构,例如串行端口设置非常挑剔。
Here's what I suggest and do in my own code: First retrieve from Windows a working structure, then modify it and pass it back. In case there is a enumeration function, first look, of Windows already knows a mode which comes close to what you want. In the case of Display Settingsyou mustuse one of the enumerated modesas any mode different will be disallowed by later versions of Windows so as not to leave the user with a blank screen, should the monitor fail to sync to the new settings.
这是我在我自己的代码中建议和执行的操作:首先从 Windows 检索一个工作结构,然后对其进行修改并将其传回。如果有一个枚举函数,首先看看,Windows 已经知道一个接近你想要的模式。在显示设置的情况下,您必须使用枚举模式之一,因为任何不同的模式都将被更高版本的 Windows 禁止,以免在显示器无法同步到新设置时让用户看到空白屏幕。
To enumerate use the function EnumDisplaySettings. Then look for a setting that's closest to your needs; or better, show the user the list of modes available and let him chose from there. Then use the structure handed to you by Windows to set the display mode by ChangeDisplaySettings
枚举使用函数EnumDisplaySettings。然后寻找最接近您需求的设置;或者更好的是,向用户显示可用模式列表,让他从中选择。然后使用Windows交给你的结构来设置显示模式ChangeDisplaySettings