Results 1 to 5 of 5

Thread: Creating a VBO with std::vector

  1. #1

    Creating a VBO with std::vector

    Hallo,

    eventhough I found plenty of posts online asking the same question neither of those answers could actually help me.
    I am trying to create a VBO using data stored in std::vectors which I am loading from a .md5mesh file.
    Even though it should work in theory and my code compiles and runs fine, it just won`t render on screen..

    An small example..

    Using arrays works just fine:
    Code :
    		static const GLfloat vertex_data[] =
    		{
    			//front
    			0.5f,  0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
    			0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
    		       -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
    		       -0.5f,  0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
     
                           //the same for the other 5 sides of the cube...
    		};
     
    		static const GLuint indices[] =
    		{
    			0, 1, 3,
    			1, 2, 3,
                           //the same for the other 5 sides of the cube...
    		};
     
    		//create the vertex array object
    		GLuint vao;
    		glGenVertexArrays(1, &vao);
     
    		//bind the vao
    		glBindVertexArray(vao);
     
    			//create the vertex buffer object
    			GLuint vbo;
    			glGenBuffers(1, &vbo);
     
    			//copy data into the vbo
    			glBindBuffer(GL_ARRAY_BUFFER, vbo);
    			glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
     
    			//creat the element buffer object
    			GLuint ebo;
    			glGenBuffers(1, &ebo);
     
    			//copy data into6 the ebo
    			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    			glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
     
    			//set the vertex attribute
    			glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
    			glEnableVertexAttribArray(0);
    			//set the color attribute
    			glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
    			glEnableVertexAttribArray(1);
    			//set the texture attribute
    			glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    			glEnableVertexAttribArray(2);
     
     
    		//unbind the vao
    		glBindVertexArray(0);
     
                    (........)
     
                   glBindVertexArray(vao);
     
    		//draw the cube
    		glDrawElements(GL_TRIANGLES, sizeof(indices), GL_UNSIGNED_INT, 0);
                     (........)

    Using vectors doesn`t:
    Code :
                    //converting the arrays from above to vectors
                    std::vector<GLfloat> v_vertex_data(vertex_data, vertex_data + sizeof vertex_data / sizeof vertex_data[0]);
                    std::vector<GLfloat> v_indices(indices, indices + sizeof indices / sizeof indices[0]);
     
    		//create the vertex array object
    		GLuint vao;
    		glGenVertexArrays(1, &vao);
     
    		//bind the vao
    		glBindVertexArray(vao);
     
    			//create the vertex buffer object
    			GLuint vbo;
    			glGenBuffers(1, &vbo);
     
    			//copy data into the vbo
    			glBindBuffer(GL_ARRAY_BUFFER, vbo);
    			glBufferData(GL_ARRAY_BUFFER, v_vertex_data.size() * sizeof(GLfloat), v_vertex_data.data(), GL_STATIC_DRAW);
     
    			//creat the element buffer object
    			GLuint ebo;
    			glGenBuffers(1, &ebo);
     
    			//copy data into6 the ebo
    			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    			glBufferData(GL_ELEMENT_ARRAY_BUFFER, v_indices.size() * sizeof(GLfloat), v_indices.data(), GL_STATIC_DRAW);
     
    			//set the vertex attribute
    			glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
    			glEnableVertexAttribArray(0);
    			//set the color attribute
    			glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
    			glEnableVertexAttribArray(1);
    			//set the texture attribute
    			glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    			glEnableVertexAttribArray(2);
     
     
    		//unbind the vao
    		glBindVertexArray(0);
     
                    (........)
     
                   glBindVertexArray(vao);
     
    		//draw the cube
    		glDrawElements(GL_TRIANGLES, v_indices.size(), GL_UNSIGNED_INT, 0);
     
                    (........)

    Maybe I am missing something completely obvious but I just don`t get what it might be?!?


    Looking forward to your answers,

    BobbyLeChimp

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by BobbyLeChimp View Post
    Code :
    		glDrawElements(GL_TRIANGLES, sizeof(indices), GL_UNSIGNED_INT, 0);
    sizeof(indices) is incorrect; it should be the number of indices, not the number of bytes.

    Quote Originally Posted by BobbyLeChimp View Post
    Code :
                    std::vector<GLfloat> v_indices(indices, indices + sizeof indices / sizeof indices[0]);
    This should be GLuint, not GLfloat.

  3. #3
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    std::vector::size:
    Returns the number of elements in the vector.

    This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.
    When loading data to a buffer object, you need to specify the total size in bytes, not the number of elements.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    Quote Originally Posted by mhagain View Post
    std::vector::size:
    When loading data to a buffer object, you need to specify the total size in bytes, not the number of elements.
    That part he got correct; the result of .size() is multiplied by sizeof() the element type.

    Quote Originally Posted by BobbyLeChimp View Post
    Code :
    glBufferData(GL_ARRAY_BUFFER, v_vertex_data.size() * sizeof(GLfloat), v_vertex_data.data(), GL_STATIC_DRAW);
    ...
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, v_indices.size() * sizeof(GLfloat), v_indices.data(), GL_STATIC_DRAW);
    The problem is that the element array is a std::vector<GLfloat> but glDrawElements() is expecting integers.

  5. #5
    Aaaaaaaahhhh.. dammit! Thanks for the help guys! I can`t believe I missed that!
    Well once I pass the right input everything works like a charm!

    Thanks again!

    ~BobbyLeChimp

Similar Threads

  1. Replies: 1
    Last Post: 07-20-2017, 05:44 PM
  2. Replies: 0
    Last Post: 01-31-2012, 10:29 AM
  3. Replies: 1
    Last Post: 10-01-2009, 06:04 PM
  4. Creating a ChaseCamera in OGL & Creating multiple cameras
    By Nuno Miguel Silva in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-03-2004, 12:45 AM
  5. Replies: 2
    Last Post: 01-07-2004, 11:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean