[FIXED] Using indices not working properly in open gles c++

Issue

I am using glDrawElement() for rendering a plain but the indexing is not working as intended so i am getting an irrelevant object .I have used AAssetmanager for loading the coordinates from a wavefront object file .

here is my code for rendering :

GLushort squareindices[] = {

        0 , 1 ,2,
        0, 3, 2

};

GLfloat vertexColor[] = {
        1.0f, 1.0f , 0.0f,
        0.0f , 0.0f , 1.0f,
        1.0f, 1.0f , 0.0f,
        0.0f , 0.0f , 1.0f,

};

void flatPlain::render(GLuint program) {
    if (renderSelf) {
//        flatPlainProgram = program;

        auto *gens = new programGenerator;
        auto *mats = new matrixOperation;

        flatPlainProgram = gens->createProgram(vertexplain, fragmentplain);
//    LOGE2("%x" , flatPlainProgram);

        vertexLocation = glGetAttribLocation(flatPlainProgram, "vertexPosition");
//    textureLocation = glGetAttribLocation(flatPlainProgram, "texturecord");
        vertexColorlocation = glGetAttribLocation(flatPlainProgram, "vertexColour");


        projection = glGetUniformLocation(flatPlainProgram, "projection");
        model = glGetUniformLocation(flatPlainProgram, "modelView");
//    sampleLocation = glGetUniformLocation(flatPlainProgram, "texture");

        mats->perspective(projectionMatrix, (float) fov, (float) w / (float) h, 0.1f, 100.0f);


//    if ( image != nullptr)
//    loadTexture();

        mats->createVolume(modelMatrix);
        mats->rotateX(modelMatrix, angle);
        mats->translateMatrix(x, y, z, modelMatrix);

        glUseProgram(flatPlainProgram);

        glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, vertexCord);
        glEnableVertexAttribArray(vertexLocation);

        glVertexAttribPointer(vertexColorlocation, 3, GL_FLOAT, GL_FALSE, 0, vertexColor);
        glEnableVertexAttribArray(vertexColorlocation);

        glUniformMatrix4fv(projection, 1, GL_FALSE, projectionMatrix);
        glUniformMatrix4fv(model, 1, GL_FALSE, modelMatrix);

//    glUniform1i(sampleLocation, 0);

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, squareindices);
    glDeleteProgram(flatPlainProgram);

    }

}

I have read the vertices and then rendered it.

my .obj file:

v 0.000000 0.000000 0.000000
v 10.000000 0.000000 0.000000
v 0.000000 0.000000 -10.000000
v 10.000000 0.000000 -10.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 0.0000 1.0000 0.0000
usemtl None
s off
f 1/1/1 2/2/1 4/3/1 3/4/1

my output:
plain

Solution

This are your vertex coordinates

0: ( 0.0, 0.0,   0.0)
1: (10.0, 0.0,   0.0)
2: ( 0.0, 0.0, -10.0)
3: (10.0, 0.0, -10.0)
0        1
  +-----+
  |     |
  |     |
  +-----+
2        3

Triangulate the quad with the following indices:

0, 1, 2,    1, 3, 2 
0         1
  +-----+  +
  |   /  / |
  | /  /   |
  +  +-----+
   2        3

Answered By – Rabbid76

Answer Checked By – Candace Johnson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published