Joe Cabezas
Joe Cabezas

Reputation: 1527

Using C++ std::Vector in glVertexPointer OpenGL function

I have a vector of floats, this vector describes a set of triangles each triangle is described using 18 floats, first 3 are the first vertex, next 3 decribes the normal of the last vertex, and so on 3 times until describe each triangle.

i am using

std::vector< GLfloat >

to store all numbers, and this is my code to render the triangles

void Visualizer::draw3dModel()
{
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    glNormalPointer(GL_FLOAT, 6 * sizeof(GLfloat), this->vertexes->data() + 3);
    glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), this->vertexes->data());

    glPushMatrix();

        glScalef(0.05f, 0.05f, 0.05f);
        glColor3f(1,1,1);

        glDrawArrays(GL_TRIANGLES, 0, this->vertexes->size());

    glPopMatrix();

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
}

this code actually works, but in certain moments I recreate the vector of floats with new triangles, it can be more or less triangles

before creating new triangles I clear the vector of vertexes, using this functions

std::vector< GLfloat >* MarchingCubesThread::getTriangles(float minvalue_scale)
{
    this->generateTriangles(minvalue_scale);

    for(unsigned int i=0; i < this->num_triangles; i++)
    {
        for(int j=0; j < 3; j++)
        {   
            this->vertexes.push_back(this->triangles[i].p[j].x);
            this->vertexes.push_back(this->triangles[i].p[j].y);
            this->vertexes.push_back(this->triangles[i].p[j].z);

            this->vertexes.push_back(this->triangles[i].norm.x);
            this->vertexes.push_back(this->triangles[i].norm.y);
            this->vertexes.push_back(this->triangles[i].norm.z);
        }
    }

    return &(this->vertexes);
}

void MarchingCubesThread::generateTriangles(float minvalue_scale)
{
    this->vertexes.clear();
    this->triangles.clear();

    this->triangles = MarchingCubesDataset(this->dataset->getMaxVal() * minvalue_scale, *(this->dataset), LinearInterp, this->num_triangles);   
}

After playing around creating a new set of new triangles, the OpenGL render updates the mesh well, but in certain moments, I get some garbage triangles, and/or triangles that are from last iteration, and they should be cleared by calling this:

this->vertexes.clear();
this->triangles.clear();

here some screenshots taken in consecutive times:

original surface after creating new triangles, shows some triangles from past surface the same effect here

any clues about what is happening here?, thank you

PD: for a complete source code, this is the public git repository on github: https://github.com/joecabezas/MemoriaJoeCabezasCode/tree/visualizer

Upvotes: 2

Views: 5663

Answers (1)

Mārtiņš Možeiko
Mārtiņš Možeiko

Reputation: 12917

glDrawArrays expects not count of floats in your buffer, but count of vertices. So correct call should be:

glDrawArrays(GL_TRIANGLES, 0, this->vertexes->size() / 6);

By letting glDrawArrays to draw 6 times more vertices you were displaying garbage after end of valid vector data, and sometimes it was data from previous iteration.

Upvotes: 5

Related Questions