Languages

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

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.

Responses

7 Replies
Jean Yang

This is a Tizen OpenGL ES 2.0 bug. Please develop team notice this.

I've tried almost the same codes on Andriod, no problem to draw these lines with colors defined for each vertex.

Jean Yang

The vertex shader source and fragment shader source are:

/* Vertex Shader Source */
static const char vertex_shader[] =
        "uniform mat4 u_mvpMatrix;\n"
        "attribute vec4 a_position;\n"
        "attribute vec4 a_color;\n"
        "varying vec4 v_color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    v_color = a_color;\n"
        "    gl_Position = u_mvpMatrix * a_position;\n"
        "}";

/* Fragment Shader Source */
static const char fragment_shader[] =
        "#ifdef GL_ES\n"
        "precision mediump float;\n"
        "#endif\n"
        "varying vec4 v_color;\n"
        "\n"
        "void main (void)\n"
        "{\n"
        "    gl_FragColor = v_color;\n"
        "}";

Jean Yang

The codes to init the shader:

static void init_shaders(Evas_Object *obj) {
    appdata_s *ad = evas_object_data_get(obj, "ad");
    const char *p;

    p = vertex_shader;
    ad->vtx_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(ad->vtx_shader, 1, &p, NULL);
    glCompileShader(ad->vtx_shader);

    p = fragment_shader;
    ad->fgmt_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(ad->fgmt_shader, 1, &p, NULL);
    glCompileShader(ad->fgmt_shader);

    ad->program = glCreateProgram();
    glAttachShader(ad->program, ad->vtx_shader);
    glAttachShader(ad->program, ad->fgmt_shader);

    glLinkProgram(ad->program);

    ad->idx_position = glGetAttribLocation(ad->program, "a_position");
    ad->idx_color = glGetAttribLocation(ad->program, "a_color");
    ad->idx_mvp = glGetUniformLocation(ad->program, "u_mvpMatrix");

    glUseProgram(ad->program);
}

pius lee

This is not bug.

Your vertex has too large to show the gradient of each point colors.

Test with this vertex.

const float full_coords_vertices[] =
{
    	 0.0f, 0.0f, 0.0f,     // [1] 0,0
	 1.0f, 0.0f, 0.0f,     // [1] +x
	-1.0f, 0.0f, 0.0f,     // [2] -x
	 0.0f, 1.0f, 0.0f,     // [3] +y
	 0.0f,-1.0f, 0.0f,     // [4] -y
	 0.0f, 0.0f, 1.0f,     // [5] +z
	 0.0f, 0.0f,-1.0f,     // [6] -z
};

Your every line is start with white color, but end point too far from start position.

So color gradient start with white but can't reach next color of end point.

Jean Yang

no, It's really a bug, you can try the example in another link:

https://developer.tizen.org/forums/native-application-development/how-pass-color-each-vertex-opengl-es-2.0?tab=my

Only three vertices has the same problem. You can have a try.

pius lee

I put some comment to your link.

That's not bug too, your vertex is too large.

Just try thin about relative position with perspective.

Jean Yang

Ok, I confirmed with the shrinked vertices, the color works well now. Thanks Pius!