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: nature of VBOs

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

    Question nature of VBOs

    Hello everyone.

    I am fairly new to opengl and I've used it so with far with C for rendering 2D sprites in immediate mode. I have looked over VBOs
    a bit and it appears that everything in the VBO is drawn at once (possibly in parallel by all available graphics cores). It seems
    to me that this is exponentially more efficient than anything I'm doing.

    My questions are:
    1. Can individual elements in a VBO (like the nth 'rotation' float in the VBO) be modified in my 'animate' and 'detect_collisions' functions or are all of these changed at once. (see below).
    2. How should I set up my structs to easily fill in the VBO in say a create_VBO_from_sprites(sprite_Array) function.


    Here's the basic setup I'm using which works well so far on even weaker laptops with integrated GPUs:

    a sprite is defined:

    Code :
    typdef struct
    {
       Texture* image;
     
       GLfloat width;
       GLfloat height;
       GLfloat xcoords[4];  // x vertices
       GLfloat ycoords[4];  // y vertices
       GLfloat rotation;      // in degrees
       GLfloat xdir;            // direction of travel x
       GLfloat ydir;            // direction of travel y
    }
    Sprite;

    In my idle function, I go through all sprites and do
    Code :
    //get start time 't0'
     
    animate(sprite_Array);
    detect_collisions(sprite_Array);
    glutPostRedisplay();
     
    // get time 't1' = time since 't0'
    // get difference 'gap' between 't1' and wanted refresh of 16.667 mlsec (60 frames per second)
     
    Sleep(gap);

    This basically just updates the vertices 'xcoords', 'ycoords' based on new values of 'rotation', 'xdir' and 'ydir'. If a
    collision is detected, the values of 'rotation', 'xdir', and 'ydir' are all changed based on some physics I will not write
    here.

    My display function simply draws the textures based on the 'xcoords' and 'ycoords' settings

    Code :
    for(GLuint i=0; i<sizeof(sprite_Array); i++)
    {
       Sprite* sp = sprite_Array[i];
     
       glEnable(GL_TEXTURE_2D);
       glBindTexture(GL_TEXTURE_2D, sp->image->ID);
       glBegin(GL_POLYGON);
       glColor4f(1, 1, 1, sp->alpha);
       glTexCoord2i(0, 0); glVertex2f(sp->xcoords[0], sp->ycoords[0]);
       glTexCoord2i(1, 0); glVertex2f(sp->xcoords[1], sp->ycoords[1]);
       glTexCoord2i(1, 1); glVertex2f(sp->xcoords[2], sp->ycoords[2]);
       glTexCoord2i(0, 1); glVertex2f(sp->xcoords[3], sp->ycoords[3]);
       glEnd();
       glDisable(GL_TEXTURE_2D);
    }

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    Yes, you can edit individual elements in a VBO. See glBufferSubData. There is also glMapBuffer and glUnmapBuffer.
    Also glMapBufferRange.
    There is an extensive page in the Wiki http://www.opengl.org/wiki/Buffer_Object

    You can draw from any part of a VBO. It is your choice.

    Why store rotation in a VBO? It looks like you aren't using shaders. If you write a shader and decide to rotate your vertex in your vertex shader, then it makes sense to have a rotate value in your VBO.

    Normally, people declare a vertex structure. See http://www.opengl.org/wiki/Vertex_Specification

    the part that starts with
    struct Vertex { GLfloat position[3];
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posting Permissions

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