c++ - OpenGL 跳过 visual studio 上的主要功能

标签 c++ visual-studio opengl glut

我正在尝试在 Visual Studio 2010 中运行以下 c++ opengl 代码。我正在使用 openglut 作为 opengl 实用工具包。

#include <iostream>
#include <gl\openglut.h>

using std::cout;
using std::endl;

void RenderScene()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

void SetupRC()
{
    glClearColor(0, 0, 1.0f, 1.0f);
}

int main(int argc, char* argv[])
{
    cout << "Test1" << endl;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutCreateWindow("Simple");
    glutDisplayFunc(RenderScene);

    SetupRC();

    glutMainLoop();

    cout << "Test2" << endl;

    return 0;
}

代码编译正确,但是当我在没有调试的情况下运行时,控制台上没有显示任何内容。当我在第一个 cout 语句处创建断点并使用调试运行时,它不会在断点处停止。它似乎跳过了主要功能。在我注释掉 2 个 cout 语句和干净的解决方案之间的 glut、gl 语句之后,它的行为正确(在断点处停止)。如果我不清理解决方案,它不会正常运行,但我看到 visual studio 在我进行更改后编译代码。这些奇怪的行为只有在我包含 opengl 代码时才会发生。正常进行更改后,我不需要清理解决方案。这是什么原因?

最佳答案

通常,glutMainLoop 函数不会返回。当您关闭窗口时,该函数将完全停止应用程序。因此,该函数调用之后的任何内容都不会被执行。

即使后面的代码被执行,它也只会在您关闭窗口时发生。 glutMainLoop 是等待您对窗口执行操作的工具。当窗口中发生事件(鼠标移动、鼠标单击、显示需要更新、计时器等)时,它会调用您注册的回调函数。

将其视为一个 while(true) 循环。

关于c++ - OpenGL 跳过 visual studio 上的主要功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8966128/

相关文章:

opengl - 在不裁剪的情况下缩放 SDL 表面的正确方法?

c++ - 隐藏光标

c++ - 将 QtSoap 引用添加到 Qt Creator

java - C++ web 框架,如 spring for Java

visual-studio - Windows 8 Developer Preview 捆绑了哪个版本的 Visual Studio 11?

visual-studio-2010 - 如何设置我的 git 存储库以便能够轻松地在 Visual Studio 中重用项目

c++ - 在 C++/CLI 包装器中访问私有(private) C++ 方法

c++ - 为什么在 C++ 中不允许调用 main()

VB.NET 应用程序中的 C# 代码?

.net - OpenGL 是否可以使用 .NET 形式?