GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


VBO drawing by baldurk
1
 
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    if(!vboenabled)
    {
        vboenabled = true;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);

        // NumTriangles triangles, each with 3 normals and 3 vertices, each with 3 components
        glBufferData(GL_ARRAY_BUFFER,
             NumTriangles * 3 * 3 * 2 * sizeof(float),
             NULL, GL_STATIC_DRAW);

        float *verts = new float[NumTriangles * 3 * 3 * sizeof(float)];
        float *norms = new float[NumTriangles * 3 * 3 * sizeof(float)];

        for(int i=0; i < NumTriangles; i++)
        {
        int ref = i*3*3;
        verts[ref+0] = Vertices[Triangles[i].vertices[0]].pos.x;
        verts[ref+1] = Vertices[Triangles[i].vertices[0]].pos.y;
        verts[ref+2] = Vertices[Triangles[i].vertices[0]].pos.z;

        verts[ref+3] = Vertices[Triangles[i].vertices[1]].pos.x;
        verts[ref+4] = Vertices[Triangles[i].vertices[1]].pos.y;
        verts[ref+5] = Vertices[Triangles[i].vertices[1]].pos.z;

        verts[ref+6] = Vertices[Triangles[i].vertices[2]].pos.x;
        verts[ref+7] = Vertices[Triangles[i].vertices[2]].pos.y;
        verts[ref+8] = Vertices[Triangles[i].vertices[2]].pos.z;

        norms[ref+0] = Vertices[Triangles[i].vertices[0]].normal.x;
        norms[ref+1] = Vertices[Triangles[i].vertices[0]].normal.y;
        norms[ref+2] = Vertices[Triangles[i].vertices[0]].normal.z;

        norms[ref+3] = Vertices[Triangles[i].vertices[1]].normal.x;
        norms[ref+4] = Vertices[Triangles[i].vertices[1]].normal.y;
        norms[ref+5] = Vertices[Triangles[i].vertices[1]].normal.z;

        norms[ref+6] = Vertices[Triangles[i].vertices[2]].normal.x;
        norms[ref+7] = Vertices[Triangles[i].vertices[2]].normal.y;
        norms[ref+8] = Vertices[Triangles[i].vertices[2]].normal.z;
        }

        glBufferSubData(GL_ARRAY_BUFFER,
                0,
                NumTriangles * 3 * 3 * sizeof(float),
                verts);

        glBufferSubData(GL_ARRAY_BUFFER,
                NumTriangles * 3 * 3 * sizeof(float),
                NumTriangles * 3 * 3 * sizeof(float),
                norms);

        delete[] verts;
        delete[] norms;

        glVertexPointer(3, GL_FLOAT, 0, (void*)0);
        glNormalPointer(GL_FLOAT, 0, (void*)(NumTriangles * 3 * 3 * sizeof(float)));
    }

    glDrawArrays(GL_TRIANGLES, 0, NumTriangles*3);