Why the color vertieces not work when I try to eliminate those redundant points from the vertexes array?

Why the color vertieces not work when I try to eliminate those redundant points from the vertexes array?

BY 03 Aug 2015 Native Application Development

I can draw x, y, z axises with colors as below picture.

The position and color vertex arrayies are defined as below.

const float full_coords_vertices[] =
{
 0.0f, 0.0f, 0.0f, // [0] Coordinate system origin
 300.0f, 0.0f, 0.0f, // [1] +x
 0.0f, 0.0f, 0.0f, // [2] origin
 -300.0f, 0.0f, 0.0f,// [3] -x
 0.0f, 0.0f, 0.0f, // [4] origin
 0.0f, 300.0f, 0.0f, // [5] +y
 0.0f, 0.0f, 0.0f, // [6] origin
 0.0f, -300.0f, 0.0f,// [7] -y
 0.0f, 0.0f, 0.0f, // [8] origin
 0.0f, 0.0f, 300.0f, // [9] +z
 0.0f, 0.0f, 0.0f, // [10] origin
 0.0f, 0.0f, -300.0f,// [11] -z
};

const int full_coords_indices_count = 12;
const unsigned short full_coords_indices[] =
{
 0, 1,
 2, 3,
 4, 5,
 6, 7,
 8, 9,
 10, 11,
};

const float full_coords_colors[] =
{
 1.0, 0.0, 0.0, 1.0, // [0] red
 1.0, 0.0, 0.0, 1.0, // [1] red
 1.0, 0.8, 0.8, 1.0, // [2] light red
 1.0, 0.8, 0.8, 1.0, // [3] light red
 0.0, 1.0, 0.0, 1.0, // [4] green
 0.0, 1.0, 0.0, 1.0, // [5] green
 0.8, 1.0, 0.8, 1.0, // [6] pale green
 0.8, 1.0, 0.8, 1.0, // [7] pale green
 0.0, 0.0, 1.0, 1.0, // [8] blue
 0.0, 0.0, 1.0, 1.0, // [9] blue
 0.9, 0.9, 1.0, 1.0, // [10] light basket
 0.9, 0.9, 1.0, 1.0, // [11] light basket
};

The codes to draw the axises is:

static void draw_gl(Evas_Object *obj) {
 appdata_s *ad = evas_object_data_get(obj, “ad”);
 float model[16], view[16];
 float aspect;
 int w, h;

 if (!ad)
  return;

 init_matrix(model);
 init_matrix(view);

 elm_glview_size_get(obj, &w, &h);
 if (!h)
  return;

 aspect = (float) w / (float) h;
 view_set_perspective(view, 60.0f, aspect, 1.0f, 20.0f);

 translate_xyz(model, 0.0f, 0.0f, -2.5f);
 rotate_xyz(model, ad->xangle, ad->yangle, 0.0f);

 multiply_matrix(ad->mvp, view, model);

 glViewport(0, 0, w, h);

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

 glVertexAttribPointer(ad->idx_position, 3, GL_FLOAT, GL_FALSE,
   3 * sizeof(float), full_coords_vertices);
 glVertexAttribPointer(ad->idx_color, 4, GL_FLOAT, GL_FALSE,
   4 * sizeof(float), full_coords_colors);
 glEnableVertexAttribArray(ad->idx_position);
 glEnableVertexAttribArray(ad->idx_color);
 glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);
 glDrawElements(GL_LINES, full_coords_indices_count, GL_UNSIGNED_SHORT,
   full_coords_indices);

 glFlush();

 display_fps();
}

Above codes works as expectation.

I tried to remove those reduntant vertices in the position vertext array, I changed the color vertext array corrospondly.

Position vertext array is changed to:

const float coords_vertices[] =
{
 0.0f, 0.0f, 0.0f,     // [0] Coordinate system origin
 300.0f, 0.0f, 0.0f,  // [1] +x
 -300.0f, 0.0f, 0.0f,   // [2] -x
 0.0f, 300.0f, 0.0f,  // [3] +y
 0.0f, -300.0f, 0.0f,   // [4] -y
 0.0f, 0.0f, 300.0f,  // [5] +z
 0.0f, 0.0f, -300.0f,   // [6] -z
};

The indices array is changed to:

const int coords_indices_count = 12;
const unsigned short coords_indices[] =
{
 0, 1,
 0, 2,
 0, 3,
 0, 4,
 0, 5,
 0, 6,
};

And correspondly, the color indices array is changed to:

const float coords_colors[] =
{
 1.0, 1.0, 1.0, 1.0, // [0] is white
 1.0, 0.0, 0.0, 1.0, // [1] is red
 1.0, 0.8, 0.8, 1.0, // [2] is light red
 0.0, 1.0, 0.0, 1.0, // [3] is green
 0.8, 1.0, 0.8, 1.0, // [4] is pale green
 0.0, 0.0, 1.0, 1.0, // [5] is blue
 0.8, 0.8, 1.0, 1.0, // [6] is light basket
};

The codes to draw the changed vertices are:

 glVertexAttribPointer(ad->idx_position, 3, GL_FLOAT, GL_FALSE,
   3 * sizeof(float), coords_vertices);
 glVertexAttribPointer(ad->idx_color, 4, GL_FLOAT, GL_FALSE,
   4 * sizeof(float), coords_colors);
 glEnableVertexAttribArray(ad->idx_position);
 glEnableVertexAttribArray(ad->idx_color);
 glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);
 glDrawElements(GL_LINES, coords_indices_count, GL_UNSIGNED_SHORT,
   coords_indices);

But it seems the new coords_colors array now works, the vertices was drawn as below picture. Only the first group of white color are used in the shadering.

Written by