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

时间:2016-03-13 15:00:03

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

我想在opengl中测量渲染函数的运行时间。
我在Visual Studio 2015上使用nupengl 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. */
}

1 个答案:

答案 0 :(得分:2)

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

您要使用的功能从OpenGL 1.5开始可用,因此您必须确保上下文至少包含此版本。使用freeglut,可以使用glutInitContextVersionglutInitContextFlags请求特定版本和个人资料。另请注意,在Windows下,所有功能都适用于OpenGL&gt; 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的详细信息,请访问他们的网站。