Shahab
Shahab

Reputation: 81

GL_TRIANGLE_STRIP drawing back to the origin at the end of verticies

I'm a biggener in open gl. To start with I just trying to draw a simple triangle with three vertices using GL_TRIANGLE_STRIP.Why the open gl adds extra point at the origin at the end? Here is the code snippet:

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

}

Here is the output:

Output

Upvotes: 0

Views: 185

Answers (1)

Columbo
Columbo

Reputation: 6766

The third parameter in the call to glDrawArrays wants the number of vertices, not the number of bytes. Try changing:

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

to

glDrawArrays(GL_TRIANGLE_STRIP, 0, sphereTriangleStripVertexCount);

Upvotes: 1

Related Questions