GL_TRIANGLE_STRIP回退到verticies末尾的原点

时间:2016-02-20 07:53:13

标签: ios iphone opengl-es

我是开放式的大人物。首先,我尝试使用GL_TRIANGLE_STRIP绘制一个带有三个顶点的简单三角形。为什么open gl在末尾的原点处添加了额外的点?以下是代码段:

- (void)setUpGL {
    glEnable(GL_DEPTH_TEST);
    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(*sphereTriangleStripVertices)*sphereTriangleStripVertexCount, sphereTriangleStripVertices, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), BUFFER_OFFSET(0));
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), BUFFER_OFFSET(3*sizeof(float)));
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(_program);

    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
    glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(*sphereTriangleStripVertices)*sphereTriangleStripVertexCount);

}

- (void)getSimpleTriangle:(VertexData **)triangleStripVertexHandle
                   :(GLuint *)triangleStripVertexCount {

    VertexData *triangleStripVertices;
    *triangleStripVertexCount = 3;

    triangleStripVertices = calloc(*triangleStripVertexCount, sizeof(VertexData));

    float y = 1.0f;
    float x = 0;
    float z = 0;

    Vertex3D position = Vertex3DMake(x, y, z);
    Vertex3D normal = Vertex3DMake(x, y, z);
    Vector3DNormalize(&normal);
    triangleStripVertices[0] = VertexDataMake(position, normal);

    y = 0.5f;
    x = 1;
    z = 0;

    position = Vertex3DMake(x, y, z);
    normal = Vertex3DMake(x, y, z);
    Vector3DNormalize(&normal);
    triangleStripVertices[1] = VertexDataMake(position, normal);

    y = 0.5f;
    x = 0.5;
    z = 0;

    position = Vertex3DMake(x, y, z);
    normal = Vertex3DMake(x, y, z);
    Vector3DNormalize(&normal);
    triangleStripVertices[2] = VertexDataMake(position, normal);

    *triangleStripVertexHandle = triangleStripVertices;

}

这是输出:

A.__dict__

1 个答案:

答案 0 :(得分:1)

调用glDrawArrays的第三个参数需要顶点数,而不是字节数。尝试更改:

glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(*sphereTriangleStripVertices)*sphereTriangleStripVertexCount);

glDrawArrays(GL_TRIANGLE_STRIP, 0, sphereTriangleStripVertexCount);