Mesh generation

Hi foum,

I am going through a opengl code snippet that generates cube from array of vertices. The vertex array also contain texture coordinates as follows

    static const GLfloat vertex_data[] =
    {
         // Position                 Tex Coord
        -0.25f, -0.25f,  0.25f,      0.0f, 1.0f,
        -0.25f, -0.25f, -0.25f,      0.0f, 0.0f,
         0.25f, -0.25f, -0.25f,      1.0f, 0.0f,

         0.25f, -0.25f, -0.25f,      1.0f, 0.0f,
         0.25f, -0.25f,  0.25f,      1.0f, 1.0f,
        -0.25f, -0.25f,  0.25f,      0.0f, 1.0f,

         0.25f, -0.25f, -0.25f,      0.0f, 0.0f,
         0.25f,  0.25f, -0.25f,      1.0f, 0.0f,
         0.25f, -0.25f,  0.25f,      0.0f, 1.0f,

         0.25f,  0.25f, -0.25f,      1.0f, 0.0f,
         0.25f,  0.25f,  0.25f,      1.0f, 1.0f,
         0.25f, -0.25f,  0.25f,      0.0f, 1.0f,

         0.25f,  0.25f, -0.25f,      1.0f, 0.0f,
        -0.25f,  0.25f, -0.25f,      0.0f, 0.0f,
         0.25f,  0.25f,  0.25f,      1.0f, 1.0f,

        -0.25f,  0.25f, -0.25f,      0.0f, 0.0f,
        -0.25f,  0.25f,  0.25f,      0.0f, 1.0f,
         0.25f,  0.25f,  0.25f,      1.0f, 1.0f,

        -0.25f,  0.25f, -0.25f,      1.0f, 0.0f,
        -0.25f, -0.25f, -0.25f,      0.0f, 0.0f,
        -0.25f,  0.25f,  0.25f,      1.0f, 1.0f,

        -0.25f, -0.25f, -0.25f,      0.0f, 0.0f,
        -0.25f, -0.25f,  0.25f,      0.0f, 1.0f,
        -0.25f,  0.25f,  0.25f,      1.0f, 1.0f,

        -0.25f,  0.25f, -0.25f,      0.0f, 1.0f,
         0.25f,  0.25f, -0.25f,      1.0f, 1.0f,
         0.25f, -0.25f, -0.25f,      1.0f, 0.0f,

         0.25f, -0.25f, -0.25f,      1.0f, 0.0f,
        -0.25f, -0.25f, -0.25f,      0.0f, 0.0f,
        -0.25f,  0.25f, -0.25f,      0.0f, 1.0f,

        -0.25f, -0.25f,  0.25f,      0.0f, 0.0f,
         0.25f, -0.25f,  0.25f,      1.0f, 0.0f,
         0.25f,  0.25f,  0.25f,      1.0f, 1.0f,

         0.25f,  0.25f,  0.25f,      1.0f, 1.0f,
        -0.25f,  0.25f,  0.25f,      0.0f, 1.0f,
        -0.25f, -0.25f,  0.25f,      0.0f, 0.0f,
    };

How to add the vertices and texture coordinates above inside the ofMesh/ofVboMesh.

I am not sure in which order i have to call the addVertices()/addIndices()/addTexCoords()

Thanks

the order in which you add them doesn’t matter as long as you add a texture coordinate, normal… per vertex, you can add all vertices first and then all tex coords or one vertex, one tex coord…

Thanks @arturo

As you can see that i have a raw array that contains both the vertex data and texture coordinate corresponding the vertex.

Now what will be the parameters for the following functions

void ofVbo::setVertexData(const float *vert0x, int numCoords, int total, int usage, int stride=0)

I think the parameter will be as follows, please correct me if i am wrong

  1. vertex_data.
  2. 36,
  3. GL_STATIC_DRAW.
  4. 5 * sizeof(GLfloat)

How to extract the texture data from the very same array and assign the texture data with the following function

void ofVbo::setTexCoordData(const float *texCoord0x, int total, int usage, int stride=0)

I guessed the parameter as well

  1. (3 * sizeof(GLfloat)) - I M NOT SURE ABOUT THIS
  2. GL_STATIC_DRAW.
  3. 5 * sizeof(GLfloat)

Please let me know

Thanks

Ok . I think it will be better to ask how to convert the following snippet

glGenBuffers(1, &position_buffer);
        glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
        glBufferData(GL_ARRAY_BUFFER,
                     sizeof(vertex_data),
                     vertex_data,
                     GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), NULL);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid *)(3 * sizeof(GLfloat)));
        glEnableVertexAttribArray(1); 

I already created the ofVbo object for it. Would that be better to call the setAttributeData() instead for both the vertex and texture data. The function has the following definition

void ofVbo::setAttributeData(int location, const float *vert0x, int numCoords, int total, int usage, int stride=0)

for the vertex attribute i specify the following parameter for the above function

  1. 0
  2. vertex_data - (as mentioned in the very first post)
  3. 3
  4. 36
  5. GL_STATIC_DRAW
  6. 5 * sizeof(GLfloat)

for the texture attribute i specify the following parameter

  1. 1
  2. (GLvoid *)(3 * sizeof(GLfloat))
  3. 2
  4. 36
    5 GL_STATIC_DRAW
  5. 5 * sizeof(GLfloat)

should be something like:

vbo.setVertexData(vertex_data, 3, num_vertices, GL_STATIC_DRAW, sizeof(float)*5);
vbo.setTexCoordData(vertex_data + 3, num_vertices, GL_STATIC_DRAW, sizeof(float)*5);
1 Like