Search code examples
c++openglvertex-shader

openGL displaying multiple objects


I'm using openGL 3.3 to draw 4 polygons in a window. My program calculates the vertices of 2 polygons (m and n sides) and applies some transformation to draw and animate 2 polygons of m sides and 2 of n sides.

My problem is that I need to store the vertices of the 2 polygons in different memory locations, since I want to apply different transformations, but I'm not able to retrieve and display both the objects from my vertex shader.

#version 330

in vec3 a_vertex;
uniform mat4 transform;

void main(void)
{
    gl_Position = transform * vec4(a_vertex, 1.0);
}

This shader shows only the first 2 objects (whose vertices are stored in location 0). The same happens if I write layout (location = 0) in vec3 a_vertex;.

Otherwise writing layout (location = 1) in vec3 a_vertex; I can only see the other two polygons (whose vertices are stored in location 1).

This seems reasonable. Now my question is: how can I show all the 4 polygons?

P.S.: I think Multiple objects drawing (OpenGL) previous question contains the answer but the explanation is so high level that I cannot translate it into code.


Solution

  • You misunderstood what shader locations are and what they're used for. Locations are designator for separate attributes within the same vertex passed to a shader. Color is an attribute and has a location. Surface normal is an attribute and has a location. Texture coordinate is an attribute and… well you get the idea.

    What you have there however are different vertices. All you have to do is simply make draw calls for your different vertices. Just call glDraw… several times or put all your different vertices into a single buffer and draw them all at once.