/* Compile with : g++ query.cpp -g -lglfw -lGL -lGLEW -lGLU -o query */ #include #include #include #include #include #include #include #include GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */ {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} }; GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */ {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4}, {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} }; GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */ void drawBox(void) { for (int i = 0; i < 6; i++) { glBegin(GL_QUADS); glNormal3fv(&n[i][0]); glVertex3fv(&v[faces[i][0]][0]); glVertex3fv(&v[faces[i][1]][0]); glVertex3fv(&v[faces[i][2]][0]); glVertex3fv(&v[faces[i][3]][0]); glEnd(); } } #define CHECK_GL_ERROR {GLenum err = glGetError(); if (err != GL_NO_ERROR) { std::cout << "Error : " << gluErrorString(err) << std::endl; assert(false); }} float randf() { return rand() / (float)INT_MAX; } int main(int argc, char** argv) { glfwInit(); int num_queries = 50; if (argc > 1) num_queries = atoi(argv[1]); int w = 640, h =400 ; glfwOpenWindow(w, h, 8, 8, 8, 8, 24, 8, GLFW_WINDOW); glewInit(); // setup camera glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70, w / (float)h, 0.1, 200); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ glClearColor(1,1,1,1); glColor3f(1,0,0); /* Setup cube vertex data. */ v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1; v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1; v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1; v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1; v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1; v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1; GLuint queryIds[num_queries]; glGenQueries(num_queries, queryIds); while(true) { CHECK_GL_ERROR glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ /* Change cube position */ glTranslatef(randf(), randf(), randf()); glRotatef(60, 1.0, 0.0, 0.0); glRotatef(-20, 0.0, 0.0, 1.0); int prev = -1; for(int i=0; i