问题
I have code from the OpenGLBook (openglbook.com) which compiles, but does not load. I have absolutely no idea why it's not loading. The code is as follows:
main.cpp
#include "main.h"
/// Methods
/// -----------------------------
int main(int argc, char* argv[])
{
Initialize(argc, argv);
glutMainLoop();
exit(EXIT_SUCCESS);
}
main.h
#ifndef main_h
#define main_h
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "CHAPTER 1"
void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
#endif
functions.cpp
#include "main.h"
int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;
void Initialize(int argc, char* argv[])
{
InitWindow(argc, argv);
fprintf(
stdout,
"INFO: OpenGL Version: %s\n",
glGetString(GL_VERSION)
);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void InitWindow(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion(4, 2);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);</pre>
glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);
glutInitWindowSize(CurrentWidth, CurrentHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
if(WindowHandle < 1) {
fprintf(
stderr,
"ERROR: Could not create a new rendering window.\n"
);
exit(EXIT_FAILURE);
}
glutReshapeFunc(ResizeFunction);
glutDisplayFunc(RenderFunction);
}
void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport(0, 0, CurrentWidth, CurrentHeight);
}
void RenderFunction(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glutPostRedisplay();
}
When I compile and try to run my .exe with Visual Studio 2010, nothing happens at all. The OpenGL window doesn't even open. Visual Studio acts as if it is running something for about 2 seconds, then returns to its normal state.
回答1:
glutInitContextVersion(4, 2);
Does your current graphics driver actually support OpenGL 4.2? If not, then your window creation will fail. 4.2 is still rather new; try 4.1 instead.
回答2:
Try debugging! Place a breakpoint on your main function and step through until something causes the program to exit.
As Nicol Bolas mentioned, you might not have a graphics card that supports OpenGL 4.2. Go on to AMD or nVidia's website and find out if your graphics card supports OpenGL 4.2. If not, then change the following line to whatever version your card supports.
glutInitContextVersion(4, 2);
来源:https://stackoverflow.com/questions/8356058/opengl-window-isnt-opening