Full screen in openGL

2019-07-17 03:53发布

问题:

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:

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/

回答1:

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.

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)

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


回答2:

Windows tends to be very picky in what you put into structures like DEVMODE. Take a look at DEVMODE, this structure is huge and 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).

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.

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 Settings you must use one of the enumerated modes as 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.

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