Franky Rivera
Franky Rivera

Reputation: 553

in openGL how do i edit a specific vertex buffer attribute from memory

if my vertex data was layed out

example:

struct Vertex
{
   float position[4];
   float normal[3];
   float texCoord[2];
}

i know we use

    glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_uiVertBufferHandle );

    //get the pointer position where we can add verts
    void* pPositionBuffer = glMapBufferARB( GL_ARRAY_BUFFER_ARB, GL_READ_WRITE );

    //now copy into our memory spot
    //which we need to move to the right position
    memcpy( ((char*)pPositionBuffer) + ( uiVertLocation*sizeof(VertexFormat) ), pVerts, iNumVerts*sizeof(VertexFormat));

    //now stop mapping
    glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);

for a full copy location this is how i been doing it but i just need to edit the position data of the vertices and not change any of the other attributes

i am just updating the positional data on the cpu side for some testing

struct Vertex
{
   float position[4]; <----
   float normal[3];
   float texCoord[2];
}

Upvotes: 1

Views: 768

Answers (2)

smani
smani

Reputation: 1973

You could re-arrange the data in your buffer by first storing all the vertices, then all the normals and then all the texture coordinates. Then just use glMapBufferRange and map only the portion containing the vertices, and update only that portion.

Upvotes: 1

Alex Telishev
Alex Telishev

Reputation: 2274

After mapping buffer you can use its memory just like any other memory in your program. For example, you can just write this code:

Vertex *cur_vertex = (Vertex *)pPositionBuffer + uiVertLocation;
for (int i = 0; i < iNumVerts; i++)
  cur_vertex[i]->position = pVerts[i]->position;

instead of memcpy

Upvotes: 1

Related Questions