简体   繁体   English

如何在OpenGL中绘制多个对象?

[英]How do I draw multiple objects in OpenGL?

I want to draw two separate objects so that I can perform a query while drawing the second object. 我想绘制两个单独的对象,以便可以在绘制第二个对象时执行查询。 The drawing code will look something like this: 绘图代码如下所示:

glDrawElements(GL_TRIANGLES,...);  // draw first object

glBeginQuery(GL_SAMPLES_PASSED, queries[0]);

glDrawElements(GL_TRIANGLES,...);  // draw second object

glEndQuery(GL_SAMPLES_PASSED);

glGetQueryObjectiv(queries[0], GL_QUERY_RESULT, &result);

return restult;

Most OpenGL tutorials don't go beyond a single glDraw*() command. 大多数OpenGL教程都不会超出单个glDraw*()命令。 As I understand it from this site I need two Vertex Array Objects, but the site doesn't explain how to set the Buffer Data for the separate objects. 该站点了解到,我需要两个顶点数组对象,但是该站点没有说明如何为单独的对象设置缓冲区数据。 For the sake of simplicity, let's just say I want the objects to be a single triangle each: 为了简单起见,我们只想让每个对象成为单个三角形:

Triangle1:
vertex1: -0.5, 0.0, 0.0
vertex2: -0.5, 0.5, 0.0
vertex3:  0.0, 0.0, 0.0

Triangle2:
vertex1: 0.0, 0.0, 0.0
vertex2: 0.5, 0.5, 0.0
vertex3: 0.5, 0.0, 0.0

Can someone show me how to setup the Vertex Array Objects, Vertex Buffer Objects, and Element Array Buffers to perform this query in C++ and OpenGL 3.2? 有人可以告诉我如何设置顶点数组对象,顶点缓冲区对象和元素数组缓冲区以在C ++和OpenGL 3.2中执行此查询吗?

Your code for drawing geometry misses two essential steps: 用于绘制几何图形的代码缺少两个基本步骤:

  1. creation of the GL_ARRAY_BUFFER (glGenBuffers, glBindBuffer, glBufferData) 创建GL_ARRAY_BUFFER(glGenBuffers,glBindBuffer,glBufferData)
  2. association of drawing state machine with the array buffer (calls to gl…Pointer functions) 绘图状态机与数组缓冲区的关联(调用gl…Pointer函数)

It is those which allow drawing multiple meshes. 那些允许绘制多个网格。

A couple of suggestions: 一些建议:

You can draw one collection of triangles that aren't connected to each other and appear to be two objects visually. 您可以绘制一组不相连的三角形,这些三角形在视觉上看起来像是两个对象。

You can also create two separate OpenGL contexts. 您还可以创建两个单独的OpenGL上下文。 One context for each of the objects you want to draw. 您要绘制的每个对象都有一个上下文。 When drawing each object make the associated context the 'current' context and make your draw calls. 绘制每个对象时,使关联的上下文成为“当前”上下文,并进行绘制调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM