使用索引在 open gles c++ 中无法正常工作

Using indices not working properly in open gles c++

我正在使用 glDrawElement() 来渲染平面,但索引没有按预期工作,所以我得到了一个不相关的对象。我使用 AAssetmanager 从波前对象文件加载坐标。

这是我的渲染代码:

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);

    }

}

我已经读取了顶点然后渲染了它。

我的 .obj 文件:

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

我的输出:

这是你的顶点坐标

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

使用以下索引对四边形进行三角测量:

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