c++ - OpenGL + Can't update buffer data with buffersubdata -


i attempting use either glnamedbuffersubdata or glbuffersubdata overwrite vertices values in buffer doesn't seem anything.

i have function updategeometry want call rendergeometry function modify 1 of variable of springmass object. want data on written using - offset intended.

const int num_vertices_per_line = 2; const int numfloatspervertices = 6; const int vertex_byte_size = numfloatspervertices * sizeof(float);  glint numspringindices, nummassindices; #define num_array_elements(a) sizeof(a) / sizeof(*a); spring::spring() : anchorposition{ 0.0f, 1.0f, 0.0f }, mposition{ 0.0f, 0.0f, 0.0f }, displacement{ 0.0f, -0.3f, 0.0f } {     using_atlas_gl_ns; //short atlas gl namespace     using_atlas_math_ns;     glgenvertexarrays(1, &mvertexarrayobject);     glbindvertexarray(mvertexarrayobject);      //mspringmass = objectgenerator::makespring(anchorposition, stretch, d);     //mmass = objectgenerator::makemass(calculateconnectionpoint(anchorposition), masswidth, massheight);     mspringmass = objectgenerator::makespringmass(anchorposition, stretch, d, masswidth, massheight);     numspringindices = mspringmass.numindices;      glgenbuffers(1, &mvertexbufferobject);     glbindbuffer(gl_array_buffer, mvertexbufferobject);     glbufferdata(gl_array_buffer, mspringmass.vertexbuffersize(), mspringmass.vertices, gl_static_draw);     glenablevertexattribarray(0);     glvertexattribpointer(0, 3, gl_float, gl_false, vertex_byte_size, 0);     glenablevertexattribarray(1);     glvertexattribpointer(1, 3, gl_float, gl_false, vertex_byte_size, (char*)(sizeof(float) * 3));      glgenbuffers(1, &springindexbufferid);     glbindbuffer(gl_element_array_buffer, springindexbufferid);     glbufferdata(gl_element_array_buffer, mspringmass.indexbuffersize(), mspringmass.indices, gl_static_draw);     // path our shaders stored.     std::string shaderdir = generated::shaderpaths::getshaderdirectory();      // set information our shaders.     std::vector<shaderinfo> shaders     {         shaderinfo{ gl_vertex_shader, shaderdir + "spring.vs.glsl" },         shaderinfo{ gl_fragment_shader, shaderdir + "spring.fs.glsl" }     };     // create new shader , add our list.     mshaders.push_back(shaderpointer(new shader));     mshaders[0]->compileshaders(shaders);     mshaders[0]->linkshaders();     mshaders[0]->disableshaders(); }  spring::~spring() {     gldeletevertexarrays(1, &mvertexarrayobject);     gldeletebuffers(1, &mspringbuffer); }  void spring::rendergeometry(atlas::math::matrix4 projection, atlas::math::matrix4 view) {     // avoid warnings unused variables, can use      //unused macro.     unused(projection);     unused(view);     mshaders[0]->enableshaders();     updategeometry();     glint displocation = mshaders[0]->getuniformvariable("displacement");     glint anchorlocation = mshaders[0]->getuniformvariable("anchorposition");     gluniform3fv(anchorlocation, 1, &anchorposition[0]);     gluniform3fv(displocation, 1, &displacement[0]);     glbindvertexarray(mvertexarrayobject);      gldrawelements(gl_lines, numspringindices, gl_unsigned_short, 0);      mshaders[0]->disableshaders(); }  void spring::updategeometry() {     //glbindbuffer(gl_array_buffer, mvertexbufferobject);     glfloat newd = d * 0;     mspringmass = objectgenerator::makespringmass(anchorposition, stretch, newd, masswidth, massheight);     //glbuffersubdata(gl_array_buffer, 0, sizeof(mspringmass.vertices), mspringmass.vertices);     glnamedbuffersubdata(mvertexbufferobject, 0, sizeof(mspringmass.vertices), mspringmass.vertices); } 

edit

for clarification here objectgenerator code.

#include "objectgenerator.h" #define num_array_elements(a) sizeof(a) / sizeof(*a); shapedata objectgenerator::makespringmass(glm::vec3 anchorposition, glfloat stretch, glfloat d, glfloat width, glfloat height) {     shapedata ret;     glm::vec3 springcolor{ 1.0f, 0.0f, 0.0f }, masscolor{ 0.0f, 1.0f, 0.0f }, connectionpoint{ anchorposition.x, anchorposition.y - (18 * d), 0.0f };     static const vertex vertices[] = {         glm::vec3(anchorposition.x, anchorposition.y, 0.0f), // 0         springcolor,         glm::vec3(anchorposition.x, anchorposition.y - d,0.0f), // 1         springcolor,         glm::vec3(anchorposition.x + stretch, anchorposition.y - (2 * d), 0.0f), // 2         springcolor,         glm::vec3(anchorposition.x - stretch, anchorposition.y - (4 * d), 0.0f), // 3         springcolor,         glm::vec3(anchorposition.x + stretch, anchorposition.y - (6 * d), 0.0f), // 4         springcolor,         glm::vec3(anchorposition.x - stretch, anchorposition.y - (8 * d), 0.0f), // 5         springcolor,         glm::vec3(anchorposition.x + stretch, anchorposition.y - (10 * d), 0.0f), // 6         springcolor,         glm::vec3(anchorposition.x - stretch, anchorposition.y - (12 * d), 0.0f), // 7         springcolor,         glm::vec3(anchorposition.x + stretch, anchorposition.y - (14 * d), 0.0f), // 8         springcolor,         glm::vec3(anchorposition.x - stretch, anchorposition.y - (16 * d), 0.0f), // 9         springcolor,         glm::vec3(anchorposition.x, anchorposition.y - (17 * d), 0.0f), // 10         springcolor,         connectionpoint, // 11         springcolor,         //=================mass==============//         glm::vec3(connectionpoint.x - width, connectionpoint.y, 0.0f), //top left 12         masscolor,         glm::vec3(connectionpoint.x + width, connectionpoint.y, 0.0f), //top right 13         masscolor,         glm::vec3(connectionpoint.x + width, connectionpoint.y - height, 0.0f), // bottom right 14         masscolor,         glm::vec3(connectionpoint.x - width, connectionpoint.y - height, 0.0f), // bottom left 15         masscolor,     };     ret.numvertices = num_array_elements(vertices);     ret.vertices = new vertex[ret.numvertices];     memcpy(ret.vertices, vertices, sizeof(vertices)); //memcpy(dest, source, size);      glushort indices[] = { 0,1 ,1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,10, 10,11, 11,12, 12,13, 13,14, 14,15, 15,12};     //glushort indices[] = { 0,1 ,1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,10, 10,11 };     ret.numindices = num_array_elements(indices);     ret.indices = new glushort[ret.numindices];     memcpy(ret.indices, indices, sizeof(indices));      return ret; }  

i have figured out.

in object generator class vertices data never updating because left static constqualifiers. glbuffersubdata working overwriting exact same data......


Comments