Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: VAO/VBO with multiple non-static objects

  1. #1
    Newbie Newbie
    Join Date
    Oct 2012
    Posts
    1

    VAO/VBO with multiple non-static objects

    Hello everyone! This marks my first post here, and ideally not my last.

    I have been teaching myself OpenGL in my spare time as a hobby and I decided it was time to learn about using VAO and VBO.

    In a current test program, I am currently only using a VBO to render a static quad with a texture on it. (This is not the complete source, only the relevant source)
    Code :
    #include <GL/glew.h>
    #include <GL/glext.h>
    #include <GL/glfw.h>
    #include <GL/SOIL.h>
     
    #define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
     
    //...
    GLfloat myVertices[] =
    {
    	0, 200,
    	200, 200,
    	200, 0,
    	0, 0
    };
     
    GLfloat myTexVertices[] =
    {
    	0.0, 0.0,
    	1.0, 0.0,
    	1.0, 1.0,
    	0.0, 1.0
    };
     
    GLuint buf[2];
     
    // ...
     
    // Assign data from arrays to buffers
    glGenBuffers(2, &buf);
    glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(myVertices), myVertices, GL_STREAM_DRAW_ARB);
     
    glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(myTexVertices), myTexVertices, GL_STATIC_DRAW);
     
    // Bind vertex buffer
    glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
    glVertexPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
     
    // Texture buffer
    glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
    glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
     
    // ...
     
    // Enable vertex arrays
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     
    // Draw
    glBindTexture(GL_TEXTURE_2D, m_texture);
    glDrawArrays(GL_QUADS, 0, 4);
     
    // Disable arrays
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    I ran across this tutorial on the OpenGL wiki: Tutorial: OpenGL 3.1 The First Triangle (C++/Win)

    I understand that to get set up for 3.1, I need to add the usage of VAO and change some of my function calls. But I read something that got me thinking:

    VAO saves all states for all vertex attributes. The maximum number supported by your video card can be obtained by calling glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &MaxVertexAttribs).
    On my machine, that returned 29. I am able to use 29 VAOs on my machine. From what I understand, you need a different VAO for each object that you are rendering. This cannot be right, but I'm not sure what else would be valid.

    I shall illustrate an image of what it is I'm trying to do:


    The grey outlines are imaginary borders, representing the boundaries of the object's texture. Every other object on the screen, with the exception of the brown squares, represent a different object with different logic handling it's type of movement. The brown squares are static, they do not move. I plan on using the viewport and moving it around when necessary. I am using a 2d orthogonal view.

    I'll summarize my questions/concerns in a list:
    1. Do I need to create a new VAO/VBO for each of those shapes?
    2. I assume a simple glBindTexture() call before drawing each item using a different texture would be sufficient. Am I correct?
    3. How do I set up each vertex buffer such that if the position of an object changes, it is updated? DO I need to recreate and rebind the buffer in place of the old one or is there a way to update it?


    I am all for learning. If you would do something differently, please let me know. If I am wrong in my assumptions, also please let me know. If you require any additional information, I will be gladly to provide it.

    Thank you for your time.

  2. #2
    Newbie Newbie
    Join Date
    Nov 2012
    Posts
    3
    GL_MAX_VERTEX_ATTRIBS provides the number of distinct vertex attributes that can be read by your shader. It is not a limit on the number of of VAOs. In your case your only vertex attributes appear to be the locations and one set of texture coordinates, so the limit is of no concern.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •