1
votes

I'm learning OpenGL ES to render for native android development. I'm able to get a triangle to draw. But I cannot get more than one triangle to draw. I just want to draw a rectangle, but if I tell the glVertexPointer function more than 3 vertices then it does not draw. I tried using GL_TRIANGLES and GL_TRIANGLE_STRIP.

What am I doing wrong?

struct Quad
{
    GLfloat         Vertices[18];
    const GLbyte    nNumVerts;

    Quad(GLfloat i_fWidth, GLfloat i_fHeight) : nNumVerts(6)
    {
        GLfloat wide = i_fWidth / 2;
        GLfloat high = i_fHeight / 2;

        Vertices[0] = -wide;    Vertices[1] = high;     Vertices[2] = 0.0f;
        Vertices[3] = -wide;    Vertices[4] = -high;    Vertices[5] = 0.0f;
        Vertices[6] = wide;     Vertices[7] = -high;    Vertices[8] = 0.0f;

        Vertices[9] = -wide;    Vertices[10] = high;    Vertices[11] = 0.0f;
        Vertices[12] = wide;    Vertices[13] = -high;   Vertices[14] = 0.0f;
        Vertices[15] = wide;    Vertices[16] = high;    Vertices[17] = 0.0f;
    }
};

void Renderer::Render()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableClientState(GL_VERTEX_ARRAY);

    // If I change "m_Quad.nNumVerts" to '3' instead of '6' it will draw a triangle
    // Anything higher than '3' and it doesn't draw anything
    glVertexPointer(m_Quad.nNumVerts, GL_FLOAT, 0, m_Quad.Vertices);

    glDrawArrays(GL_TRIANGLES, 0, m_Quad.nNumVerts);

    eglSwapBuffers(m_pEngine->pDisplay, m_pEngine->pSurface);
}
1
just quick glance, but I think it should be glVertexPointer(3, GL_FLOAT, 0, m_Quad.Vertices); glDrawArrays(GL_TRIANGLES, 0, 6);apple apple
I've tried that. I still only get the first triangle.szMuzzyA
Wait, that worked. I was trying before with GL_TRIANGLE_STRIP instead of GL_TRIANGLES. Thanks. Write that as an answer and I will accept it.szMuzzyA
Why would I put '3' instead of '6' in glVertexPointer()? Is 3 just telling how many values in each vertex? Then glDrawArrays() will use that with the vertex count to iterate the array?szMuzzyA
Just answered my own question. The answer is yes.szMuzzyA

1 Answers

1
votes

The first argument of glVertexPointer is the number of values per vertex, it should stays in 3 in this case.

glVertexPointer(3, GL_FLOAT, 0, m_Quad.Vertices);