Results 1 to 2 of 2

Thread: Loading VBOs

  1. #1
    Junior Member Newbie LorgonJortle's Avatar
    Join Date
    Jan 2011
    Location
    Winter Park, Florida
    Posts
    2

    Loading VBOs

    Hey everyone,

    It's my first time posting here, so 'ello and nice to meet you all. I've been working on loading a 3D model (*.obj) into a VBO and displaying it. The data loading works just as it should (debugging allows me this knowledge), but when I try to draw an outline of the model with GL_POINTS it only draws one single point. The VBO holds far more data than a single vertex... so I don't know what's going on. Moreover, if I try to draw with GL_TRIANGLES (which is what the buffer holds), I get nothing.

    I've come to the conclusion that it must be some improper setup of my VBO, but everything I've read about them says that I'm approaching this properly.

    Here are the two key methods:
    Code :
    GLboolean MaterialGroup::UploadData()
    {
       /* Copy all data into one array. */      
       data.clear();
       data.insert(data.end(), Vertices.begin(), Vertices.end());
       data.insert(data.end(), TexCoords.begin(), TexCoords.end());
       data.insert(data.end(), Normals.begin(), Normals.end());
     
       /* Calculate some statistics. */
       faces = (Vertices.size() / 3) / 3;
       vertexCount = Vertices.size() / 3;
       texCoordCount = TexCoords.size() / 2;
       normalCount = Normals.size() / 3;
       hasTexture = Mat.Texture.IsAllocated && texCoordCount > 0;
       hasNormals = normalCount > 0;
     
       /* Only bother uploading if we have geometric data. */
       if(Vertices.size() > 0)
       {
          /* Delete any existing buffer. */
          if(glIsBuffer(VBO))
             glDeleteBuffers(1, &VBO);
     
          /* Generate and bind the vertex array. */
          if(!glIsVertexArray(VAO))
             glGenVertexArrays(1, &VAO);
          glBindVertexArray(VAO);
     
          /* Generate a new buffer. */
          glGenBuffers(1, &VBO);
          glBindBuffer(GL_ARRAY_BUFFER, VBO);
          glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(GLfloat), &data.at(0), GL_STATIC_DRAW);
          glVertexAttribPointer(Attribute_Position, 3, GL_FLOAT, GL_FALSE, 0, &data.at(0));
     
          /* If we have texcoords, set the pointer to them. */
          if(texCoordCount)
             glVertexAttribPointer(Attribute_Texture0, 2, GL_FLOAT, GL_FALSE, 0, &data.at(Vertices.size()));
     
          /* if we have normals, set the pointer to them. */
          if(normalCount)
             glVertexAttribPointer(Attribute_Normal, 3, GL_FLOAT, GL_FALSE, 0, &data.at(Vertices.size() + TexCoords.size()));
       }
       else 
       {
          /* We don't have any geometric data. Let's leave. */
          data.clear();
          return GL_FALSE;
       }
     
       /* Temp data is no longer needed. */
       TempVertices.clear();
       TempTexCoords.clear();
       TempNormals.clear();
     
       /* Success! */
       return GL_TRUE;
    }
     
    GLvoid MaterialGroup::Draw()
    {
       /* Bind any textures and set any colors. */
       Mat.Bind();
     
       /* Only draw if we've uploaded. */
       if(data.size() > 0)
       {
          /* Bind the vertex array before anything. */
          glBindVertexArray(VAO);
     
          /* Bind our VBO and setup vertex drawing. */
          glBindBuffer(GL_ARRAY_BUFFER, VBO);
          glEnableClientState(GL_VERTEX_ARRAY);  
          glVertexPointer(3, GL_FLOAT, 0, 0);
          glEnableVertexAttribArray(Attribute_Position);
     
          /* If we have texture coords, set them up. */
          if(hasTexture)
          {
             glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
             glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)(texCoordCount * 2));
             glEnableVertexAttribArray(Attribute_Texture0);
          }
     
          /* If we have normals, set them up. */
          if(hasNormals)
          {
             glEnableClientState(GL_NORMAL_ARRAY);
             glNormalPointer(3, GL_FLOAT, (GLvoid*)((texCoordCount * 2) + (normalCount * 3)));
             glEnableVertexAttribArray(Attribute_Normal);
          }
     
          /* Draw a point outline of out model. */
          glDrawArrays(GL_POINTS, 0, vertexCount);
     
          /* Disable states and attributes. */
          glDisableClientState(GL_VERTEX_ARRAY);
          glDisableClientState(GL_TEXTURE_COORD_ARRAY);
          glDisableClientState(GL_NORMAL_ARRAY);
          glDisableVertexAttribArray(Attribute_Position);
          glDisableVertexAttribArray(Attribute_Texture0);
          glDisableVertexAttribArray(Attribute_Normal);
       }
     
       /* Unbind our texture. */
       Mat.UnBind();
    }

    If anyone can shed some light on what might be going on, I'd very much appreciate it.

    Thanks,
    LorgonJortle

  2. #2
    Junior Member Newbie LorgonJortle's Avatar
    Join Date
    Jan 2011
    Location
    Winter Park, Florida
    Posts
    2

    Re: Loading VBOs

    SOLVED by Candy on Freenode.

    Code :
    glVertexAttribPointer(Attribute_Position, 3, GL_FLOAT, GL_FALSE, 0, &data.at(0));

    Should be:
    Code :
    glVertexAttribPointer(Attribute_Position, 3, GL_FLOAT, GL_FALSE, 0, 0);

    Thanks!

Similar Threads

  1. Loading VBOs at runtime
    By killermud in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-30-2013, 06:29 PM
  2. ES2 and VBOs
    By birdcage in forum OpenGL ES
    Replies: 2
    Last Post: 01-04-2011, 11:25 AM
  3. Replies: 8
    Last Post: 12-01-2003, 08:35 AM
  4. Replies: 2
    Last Post: 07-28-2003, 07:30 PM
  5. loading RAW files / general model loading question
    By tcs in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 03-23-2000, 12:41 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