Reputation: 8414
I wrote a simple program using OpenGL 4.3 which displays a triangle, quadrilateral, and pentagon. I defined my vertices in the following array:
vec2 vertices[NumPoints] = {
vec2(-1, -0.75), vec2(-0.75, -0.25), vec2(-0.5, -0.75), //Triangle
vec2(0, -0.25), vec2(0.5, -0.25), vec2(0.5, -0.75), vec2(0, -0.75), //Quad
vec2(0.25, 0.25), vec2(0.5, 0.5), vec2(0.75, 0.25), vec2(0.65, 0), vec2(0.35, 0) // pentagon
};
For the sake of brevity I'll omit most of the boilerplate code. In my display function I have the following code:
glDrawArrays(GL_TRIANGLES, 0, 3); // draw the points
glDrawArrays(GL_TRIANGLE_FAN, 3, 4); //quad
glDrawArrays(GL_TRIANGLE_FAN, 7, 5); //polygon
Everything works fine and there isn't any problems. However, it seems rather tedious and almost impossible to create complex scenes if you need know exactly how many vertices you need upfront. Am I missing something here? Also, if needed to create a circle, how would I do that using just GL_TRIANGLES?
Upvotes: 2
Views: 863
Reputation: 49319
In a real world application you will have scene management, with multiple objects in the scene and multiple sub-objects for each object. Objects are responsible for generating their vertex data and corresponding drawing calls and schedule them appropriately.
For example, you can have a cube object that has a single property - edge length. From that single property you generate the full set of vertices required to render a cube.
You can also chose to convert the cube primitive to another compatible object, for example a box primitive where you have 3 properties - height, width and depth, or even an arbitrary polygonal mesh that is made of faces, which are made of edges which are made of vertices.
It is a good idea to sort the different scene objects in such an order to allow minimizing the number of draw calls, which is the typical bottleneck 3D graphics struggles with. Combined with instancing and adaptive LOD you can get significant performance improvements.
For the circle - in primitive mode the most efficient way to draw it is using a triangle fan. But if you convert the circle primitive to a polygonal mesh, you could render regular triangles. The number of vertices, needed to draw the circle will grow. With triangle fan, you need 3 vertices for the first triangle and then only 1 additional vertex for every additional segment, with regular triangles you will need the full 3 vertices for every segment of the circle.
Upvotes: 1
Reputation: 162297
Am I missing something here?
Yes. You can allocate memory dynamically and read data from files. That's how any real world program deals with this kind of things. You'll have some scene management structure which allows to load a scene and objects from files. The file itself will contain some metadata, as number of faces, vertices, etc. which can be used to prepare the data structures at runtime.
Upvotes: 1