OpenGL 窗口未打开

发布于 2024-12-19 08:09:01 字数 2265 浏览 4 评论 0原文

我有来自 OpenGLBook (openglbook.com) 的代码,它可以编译,但无法加载。我完全不知道为什么它没有加载。代码如下:

ma​​in.cpp

#include "main.h"

/// Methods
/// -----------------------------

int main(int argc, char* argv[])
{
    Initialize(argc, argv);

    glutMainLoop();

    exit(EXIT_SUCCESS);
}

ma​​in.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();
}

当我编译并尝试使用 Visual Studio 2010 运行我的 .exe 时,什么也没有发生。 OpenGL 窗口甚至打不开。 Visual Studio 的行为就像正在运行某些内容大约 2 秒,然后返回到正常状态。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2024-12-26 08:09:01
glutInitContextVersion(4, 2);

您当前的图形驱动程序实际上支持 OpenGL 4.2 吗?如果没有,那么您的窗口创建将会失败。 4.2 还比较新;尝试 4.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.

夜无邪 2024-12-26 08:09:01

尝试调试!在主函数上放置一个断点并单步执行,直到出现某种情况导致程序退出。

正如 Nicol Bolas 提到的,您可能没有支持 OpenGL 4.2 的显卡。访问 AMD 或 nVidia 的网站,了解您的显卡是否支持 OpenGL 4.2。如果没有,请将以下行更改为您的卡支持的任何版本。

glutInitContextVersion(4, 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文