One further query type that you can use to judge how long rendering is taking is the timer query. Timer queries are made by passing the GL_TIME_ELAPSED query type as the target parameter of glBeginQuery() and glEndQuery(). When you call glGetQueryObjectuiv() to get the result from the query object, the value is the number of nanoseconds that elapsed between when OpenGL executes your calls to glBeginQuery() and glEndQuery(). This is actually the amount of time it took OpenGL to process all the commands between the glBeginQuery() and glEndQuery() commands. You can use this, for example, to identify the most expensive part of your scene. Consider the code shown in Listing 12.8. 另一个查询类型就是timer query,你可以用这个技术来查询渲染花费了多少时间。当我们使用timer query的时候,传给glBeginQuery和glEndQuery的参数要相应的变成GL_TIME_ELAPSED。当你调用glGetQueryObjectuiv获取 query object的结果的时候,返回值是从你调用glBeginQuery开始到你调用glEndQuery之间所有的OpenGL指令耗费的时间,单位是纳秒。你可以通过这个数据来评估你场景中渲染最耗时的部分。我们来看看清单12.8的代码:
时间: 2024-03-16 14:47:05 浏览: 314
1021_judge_TheRing_
对于定时查询,你可以使用glBeginQuery和glEndQuery函数,并将GL_TIME_ELAPSED作为目标参数传递。当你调用glGetQueryObjectuiv函数来获取查询对象的结果时,返回值是从调用glBeginQuery到glEndQuery之间所有OpenGL指令执行所花费的纳秒数。这实际上是OpenGL处理glBeginQuery和glEndQuery之间所有命令所需的时间。你可以使用它来识别场景中最昂贵的部分。以下是一个示例:
```c++
GLuint queryID;
glGenQueries(1, &queryID);
glBeginQuery(GL_TIME_ELAPSED, queryID);
// Your OpenGL code here
glEndQuery(GL_TIME_ELAPSED);
GLuint64 timeElapsed;
glGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &timeElapsed);
timeElapsed = (timeElapsed / 1000000.0); // Convert nanoseconds to milliseconds
std::cout << "OpenGL rendering took " << timeElapsed << " ms to execute" << std::endl;
```
在这里,我们首先使用glGenQueries创建计时查询对象。然后,我们使用glBeginQuery开始计时查询,并编写OpenGL代码。最后,我们使用glEndQuery结束计时查询,并使用glGetQueryObjectui64v检索查询结果。最后,我们将时间间隔从纳秒转换为毫秒,并输出结果。你可以将此代码段放置在你想要测量OpenGL渲染的前后,以测量OpenGL渲染的运行时间。
阅读全文