Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you render multiple VBO/IBOs?
    text
    copied!<p>Here is what I am currently trying...</p> <p>Variable initialization:</p> <pre><code>GLuint vertexbuffer; GLuint uvbuffer; GLuint normalbuffer; GLuint colorbuffer; GLuint elementbuffer; GLuint VertexArrayID; std::vector&lt;unsigned short&gt; indices; std::vector&lt;glm::vec3&gt; indexed_vertices; std::vector&lt;glm::vec2&gt; indexed_uvs; std::vector&lt;glm::vec3&gt; indexed_normals; std::vector&lt;glm::vec4&gt; indexed_colors; GLuint vertexbuffer2; GLuint uvbuffer2; GLuint normalbuffer2; GLuint colorbuffer2; GLuint elementbuffer2; GLuint VertexArrayID2; std::vector&lt;unsigned short&gt; indices2; std::vector&lt;glm::vec3&gt; indexed_vertices2; std::vector&lt;glm::vec2&gt; indexed_uvs2; std::vector&lt;glm::vec3&gt; indexed_normals2; std::vector&lt;glm::vec4&gt; indexed_colors2; struct PackedVertex{ glm::vec3 position; glm::vec2 uv; glm::vec3 normal; glm::vec4 color; bool operator&lt;(const PackedVertex that) const{ return memcmp((void*)this, (void*)&amp;that, sizeof(PackedVertex))&gt;0; }; }; std::vector&lt;glm::vec3&gt; in_vertices; std::vector&lt;glm::vec2&gt; in_uvs; std::vector&lt;glm::vec3&gt; in_normals; std::vector&lt;glm::vec4&gt; in_colors; in_vertices.push_back(glm::vec3(0,0,0)); in_vertices.push_back(glm::vec3(1,0,0)); in_uvs.push_back(glm::vec2(0,0)); in_uvs.push_back(glm::vec2(0,0)); in_normals.push_back(glm::vec3(0,1,0)); in_normals.push_back(glm::vec3(0,1,0)); in_colors.push_back(glm::vec4(1,1,1,1)); in_colors.push_back(glm::vec4(1,0,0,1)); std::vector&lt;glm::vec3&gt; in_vertices2; std::vector&lt;glm::vec2&gt; in_uvs2; std::vector&lt;glm::vec3&gt; in_normals2; std::vector&lt;glm::vec4&gt; in_colors2; in_vertices2.push_back(glm::vec3(0,1.3,0)); in_vertices2.push_back(glm::vec3(1,1.3,0)); in_uvs2.push_back(glm::vec2(0,0)); in_uvs2.push_back(glm::vec2(0,0)); in_normals2.push_back(glm::vec3(0,1,0)); in_normals.push_back(glm::vec3(0,1,0)); in_colors2.push_back(glm::vec4(0,0,1,1)); in_colors2.push_back(glm::vec4(0,0,1,1)); </code></pre> <p>Intit the buffers:</p> <pre><code>void InitializeVertexBuffer(GLuint &amp;theBuffer, GLenum target, GLenum usage, const void* data, int size) { glGenBuffers(1, &amp;theBuffer); glBindBuffer(target, theBuffer); glBufferData(target, size, data, usage); glBindBuffer(target, 0); } </code></pre> <p>Setting up IBO/VAO/VBO:</p> <pre><code>// For each input vertex for ( unsigned int i=0; i&lt;2; i++ ){ // Try to find a similar vertex in out_XXXX unsigned short index; bool found = getSimilarVertexIndex(in_vertices[i], in_uvs[i], in_normals[i], indexed_vertices, indexed_uvs, indexed_normals, index); if ( found ){ // A similar vertex is already in the VBO, use it instead ! indices.push_back( index ); }else{ // If not, it needs to be added in the output data. indexed_vertices.push_back( in_vertices[i]); indexed_uvs .push_back( in_uvs[i]); indexed_normals .push_back( in_normals[i]); indexed_colors .push_back( in_colors[i]); indices .push_back( (unsigned short)indexed_vertices.size() - 1 ); } } size_t colorDataOffset = 0; InitializeVertexBuffer(vertexbuffer, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_vertices[0], indexed_vertices.size() * sizeof(glm::vec3) ); InitializeVertexBuffer(uvbuffer, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_vertices[0], indexed_vertices.size() * sizeof(glm::vec2) ); InitializeVertexBuffer(normalbuffer, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_normals[0], indexed_normals.size() * sizeof(glm::vec3) ); InitializeVertexBuffer(colorbuffer, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_colors[0], indexed_colors.size() * sizeof(glm::vec4) ); InitializeVertexBuffer(elementbuffer, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indices[0], indices.size() * sizeof(unsigned short) ); //Generate VAO glGenVertexArrays(1, &amp;VertexArrayID); glBindVertexArray(VertexArrayID); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // 2nd attribute buffer : normals glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // 3nd attribute buffer : UVs glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // 4th attribute buffer : colors glEnableVertexAttribArray(3); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // Index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); //****Second one*****// // For each input vertex for ( unsigned int i=0; i&lt;2; i++ ){ // Try to find a similar vertex in out_XXXX unsigned short index; bool found = getSimilarVertexIndex(in_vertices2[i], in_uvs2[i], in_normals2[i], indexed_vertices2, indexed_uvs2, indexed_normals2, index); if ( found ){ // A similar vertex is already in the VBO, use it instead ! indices2.push_back( index ); }else{ // If not, it needs to be added in the output data. indexed_vertices2.push_back( in_vertices2[i]); indexed_uvs2 .push_back( in_uvs2[i]); indexed_normals2 .push_back( in_normals[i]); indexed_colors2 .push_back( in_colors2[i]); indices2.push_back( (unsigned short)indexed_vertices2.size() - 1 ); } } size_t colorDataOffset = 0; InitializeVertexBuffer(vertexbuffer2, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_vertices2[0], indexed_vertices2.size() * sizeof(glm::vec3) ); InitializeVertexBuffer(uvbuffer2, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_vertices2[0], indexed_vertices2.size() * sizeof(glm::vec2) ); InitializeVertexBuffer(normalbuffer2, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_normals2[0], indexed_normals2.size() * sizeof(glm::vec3) ); InitializeVertexBuffer(colorbuffer2, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indexed_colors2[0], indexed_colors2.size() * sizeof(glm::vec4) ); InitializeVertexBuffer(elementbuffer2, GL_ARRAY_BUFFER, GL_STATIC_DRAW, &amp;indices2[0], indices2.size() * sizeof(unsigned short) ); //Generate VAO glGenVertexArrays(1, &amp;VertexArrayID2); glBindVertexArray(VertexArrayID2); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // 2nd attribute buffer : normals glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, normalbuffer2); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // 3nd attribute buffer : UVs glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // 4th attribute buffer : colors glEnableVertexAttribArray(3); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer2); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, (void*)0 ); // Index buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer2); </code></pre> <p>Now Drawing</p> <pre><code>do{ glUseProgram(programID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); // Draw the primatives ! glDrawElements(GL_POINTS, 2, GL_UNSIGNED_SHORT, (void*)0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer2); glDrawElements(GL_LINES, 2, GL_UNSIGNED_SHORT, (void*)0); glUseProgram(0); glfwSwapBuffers(); ..} </code></pre> <p>So when I init the 2nd VBO it over and I only get 2 blue dots and not 2 dots and a line</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload