简体   繁体   中英

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];
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM