简体   繁体   中英

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. 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?

Your code for drawing geometry misses two essential steps:

  1. creation of the GL_ARRAY_BUFFER (glGenBuffers, glBindBuffer, glBufferData)
  2. association of drawing state machine with the array buffer (calls to gl…Pointer functions)

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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