1

I would like to measure running time of render function in opengl.
I use nupengl Nuget plugin on Visual studio 2015.
In the code below, I would like to measure time to draw a triangle.
However, it crash at glGenQueries(2, queryID) Crash
Please check my code below or on 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. */
}
Haha TTpro
  • 5,137
  • 6
  • 45
  • 71
  • 1
    Start by running in the debugger to catch the crash "in action". Then you will be able to locate where in your code it happens, and you can tell us that. – Some programmer dude Mar 13 '16 at 15:04
  • Dear @JoachimPileborg, thank for comment. In the screenshot above, I run the debugger and it glGenQueries(2, queryID). That is all I know. – Haha TTpro Mar 13 '16 at 15:06
  • It seems like glGenQueries isn't loaded. You need OpenGL 1.5 context for it. Check the address of the function and use GLEW (or other loaders) and try to load it. – pearcoding Mar 13 '16 at 15:06

1 Answers1

2

This error tells, that the functionpointer glGenQueries is a null-pointer. This normally happens under windows when the specific extension could not be loaded, or when the OpenGL Version requested/supported is not high enough.

The function you want to use is available starting from OpenGL 1.5, so you have to ensure that the context has at least this version. With freeglut, one can request a specific version and profile with glutInitContextVersion and glutInitContextFlags. Also note, that under windows all function for OpenGL > 1.1 have to be loaded manually or by a context-managing library like glew.

Edit: Add the following code directly before glutCreateWindow:

glutInitContextVersion (1, 5);

And this after 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));
}

Details on building and linking of glew is given on their website.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • Dear @BDL, thanks for answering. Could you please add more detail ? (code example). I am new to opengl. – Haha TTpro Mar 13 '16 at 15:11
  • `The function you want to use is available starting from OpenGL 1.5` note that this essentially means, that given any reasonable drivers you'll never end up with a OpenGL context that does not support this function. core/compatibility profiles have been introduced only with OpenGL-3.3 and asking for a <=1.4 context specifically would have spit out an 2.1 context if it were core context capable. The only reason this can happen, if OpenGL is limited to 1.4. *And that* is the case with the default Windows drivers.* Solution: Download and install drivers from the GPU vendor website. – datenwolf Mar 13 '16 at 15:45