Languages

Menu
Sites
Language
How to draw multiple objects by OpenGL ES 2.0 codes

We can find a lot of samples to introduce painting object by OpenGL ES 2.0 codes. Typically, the main content for these articles is to introduce the shader sample codes for OpenGL ES 2.0, they draw mostly only one object in the sample. It makes the developer puzzeled how to draw multiple objects through OpenGL ES 2.0 codes.

Infact, Shader is the codes to do the rendering. Shaders calculate rendering effects on graphics hardware with a high degree of flexibility. Most shaders are coded for a graphics processing unit (GPU), though this is not a strict requirement. So shader codes infact is written for the effects of the painting, it is not directly related how to draw your objects.

You still uses basic opengl painting APIs like glDrawArrays() or glDrawElements() to paint your objects.  After loaded your shader, you just need repeatly do below things:

1) binding texture (optional)

2) prepare vertexes for one object

3) painting the object

Let's look at the sample codes for drawing multiple objects after shader already loaded.

static void draw_gl(Evas_Object *obj) {
    appdata_s *ad = evas_object_data_get(obj, "ad");

    glViewport(0, 0, w, h);

    glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   // Paint the first object 

   glVertexAttribPointer(ad->idx_position, 3, GL_FLOAT, GL_FALSE,
            3 * sizeof(float), cube_vertices);
    glVertexAttribPointer(ad->idx_color, 4, GL_FLOAT, GL_FALSE,
            4 * sizeof(float), cube_colors);

    glEnableVertexAttribArray(ad->idx_position);
    glEnableVertexAttribArray(ad->idx_color);

    glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);

    glDrawElements(GL_TRIANGLES, cube_indices_count, GL_UNSIGNED_SHORT,
            cube_indices);

   // Paint the second object 

    glVertexAttribPointer(ad->idx_position, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), axis_vertices);

    glEnableVertexAttribArray(ad->idx_position);

    glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);
    glDrawArrays(GL_LINES,0,2);
    glDrawArrays(GL_LINES,1,2);
    glDrawArrays(GL_LINES,3,2);
    glDrawArrays(GL_LINES,4,2);
    glDrawArrays(GL_LINES,6,2);
    glDrawArrays(GL_LINES,7,2);

    glFlush();

}

the definition for the vertexes are:

const float cube_vertices[] =
{
    -0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f,  0.5f,
     0.5f, -0.5f,  0.5f,
     0.5f, -0.5f, -0.5f,
    -0.5f,  0.5f, -0.5f,
    -0.5f,  0.5f,  0.5f,
     0.5f,  0.5f,  0.5f,
     0.5f,  0.5f, -0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f,  0.5f, -0.5f,
     0.5f,  0.5f, -0.5f,
     0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f,  0.5f,
    -0.5f,  0.5f,  0.5f,
     0.5f,  0.5f,  0.5f,
     0.5f, -0.5f,  0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f,  0.5f,
    -0.5f,  0.5f,  0.5f,
    -0.5f,  0.5f, -0.5f,
     0.5f, -0.5f, -0.5f,
     0.5f, -0.5f,  0.5f,
     0.5f,  0.5f,  0.5f,
     0.5f,  0.5f, -0.5f
};

const int cube_indices_count = 36;
const unsigned short cube_indices[] =
{
     0,  2,  1,
     0,  3,  2,
     4,  5,  6,
     4,  6,  7,
     8,  9, 10,
     8, 10, 11,
    12, 15, 14,
    12, 14, 13,
    16, 17, 18,
    16, 18, 19,
    20, 23, 22,
    20, 22, 21
};

const float cube_colors[] =
{
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0,
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0
};

const float axis_vertices[] =
{
    -300.0f, 0.0f, 0.0f,     // -x
    0.0f, 0.0f, 0.0f,           // 0
    300.0f, 0.0f, 0.0f,      // +x
    0.0f, -300.0f, 0.0f,     // -y
    0.0f, 0.0f, 0.0f,           // 0
    0.0f, 300.0f, 0.0f,      // +y
    0.0f, 0.0f, -300.0f,     // -z
    0.0f, 0.0f, 0.0f,           // 0
    0.0f, 0.0f, 300.0f,      // +z
};