c++ - 在 Windows 上测量 Opengl 的运行时间?

标签 c++ windows visual-studio opengl visual-studio-2015

<分区>

我想测量 opengl 中渲染函数的运行时间。
我用 nupengl Visual Studio 2015 上的 Nuget 插件。
在下面的代码中,我想测量绘制三角形的时间。
但是,它在 glGenQueries(2, queryID) 处崩溃 Crash
请在下方或 github 上查看我的代码

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>

void display(void)
{
    GLuint64 startTime, stopTime;
    unsigned int queryID[2];

    // generate two queries
    glGenQueries(2, queryID);
        // issue the first query
        // Records the time only after all previous 
        // commands have been completed
        glQueryCounter(queryID[0], GL_TIMESTAMP);

    // call a sequence of OpenGL commands
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0f, 1.0f, 1.0f); //Defining color (white)
        glBegin(GL_LINE_LOOP);
        glVertex3f(5.0f, 5.0f, 0.0f);
        glVertex3f(25.0f, 5.0f, 0.0f);
        glVertex3f(25.0f, 25.0f, 0.0f);
        glEnd();
        glFlush(); 

        // issue the second query
        // records the time when the sequence of OpenGL 
        // commands has been fully executed
        glQueryCounter(queryID[1], GL_TIMESTAMP);
        // wait until the results are available
        GLint stopTimerAvailable = 0;
        while (!stopTimerAvailable) {
            glGetQueryObjectiv(queryID[1],
            GL_QUERY_RESULT_AVAILABLE,
            &stopTimerAvailable);
    }

    // get query results
    glGetQueryObjectui64v(queryID[0], GL_QUERY_RESULT, &startTime);
    glGetQueryObjectui64v(queryID[1], GL_QUERY_RESULT, &stopTime);

    printf("Time spent on the GPU: %f ms\n", (stopTime - startTime) / 1000000.0);

}
void init(void)
{
    /* select clearing color */
    glClearColor(0.5, 0.5, 0.5, 0.0);
    /* initialize viewing values */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 30.0, 0.0, 35.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("TRIANGLE");
    init();
    const char* version = (const char*)glGetString(GL_VERSION);
    std::cout << version;

    glutDisplayFunc(display);
    glutMainLoop();
    return 0; /* ANSI C requires main to return an int. */
}

最佳答案

这个错误告诉我们,函数指针 glGenQueries 是一个空指针。当无法加载特定扩展,或者请求/支持的 OpenGL 版本不够高时,这通常会在 Windows 下发生。

您要使用的函数从 OpenGL 1.5 开始可用,因此您必须确保上下文至少具有此版本。使用 freeglut,可以使用 glutInitContextVersionglutInitContextFlags 请求特定版本和配置文件。另请注意,在 Windows 下,OpenGL > 1.1 的所有功能都必须手动加载或由上下文管理库(如 glew)加载。 .

编辑:直接在glutCreateWindow之前添加如下代码:

glutInitContextVersion (1, 5);

这是在 glutCreateWindow 之后。

glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}

glew 的构建和链接的详细信息在他们的网站上给出。

关于c++ - 在 Windows 上测量 Opengl 的运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971785/

相关文章:

c++ - 在 C++ 中存储指向 vector 的指针的类

c++ - 初始化 COM 对象/将 VB 代码转换为 C++

带浮点的 Windows ASM printf

java - Windows 和 Linux 的应用程序开发

javascript - 是否可以在 Visual Studio 中突出显示某些代码行?

android - 在 Visual Studio 2015 上更新 Android SDK 工具

c++ - 仅在 Visual Studio 外部运行时才能找到文本文件

c++ - Makefile:不同目录下的Cpp文件,目标文件重定向到./obj,可执行文件重定向到./bin

linux - Windows 文件命名问题

c# - 未定义的预处理器变量 '$(var.WixInstall.TargetPath)' 。 WixInstaller D :work\Extractor\WixInstaller\Product. wxs