Course list http://www.c-jump.com/bcc/

OpenGL Application Structure Overview


  1. Pseudocode legend
  2. main ( )
  3. Model constructor ( )
  4. Model::load_OBJ_file ( )
  5. Model::upload_bitmap_texture ( )
  6. Model::upload_2_server ( )
  7. render ( )
  8. Model::render ( )
  9. Example populating buffer objects

1. Pseudocode legend



2. main ( )



3. Model constructor ( )



4. Model::load_OBJ_file ( )



5. Model::upload_bitmap_texture ( )



6. Model::upload_2_server ( )



7. render ( )



8. Model::render ( )



9. Example populating buffer objects


  • glGenVertexArrays : number of arrays, location of array object names

    • generates vertex array object names (integer identifiers)

    • Vertex Array Object (or VAO) is a descriptor of vertex data

  • glBindBuffer : attaches named buffer object to a target, e.g. GL_ARRAY_BUFFER

    • GL_ARRAY_BUFFER suggests that buffer is used for vertex data, but may be re-bound later for other uses.

  • glVertexAttribPointer ( 0, 4, GL_FLOAT, GL_FALSE, 0, 0 );

    • 0 is vertex attribute index, such as zero in layout(location=0) in shader code. The number is programmer's choice, e.g. we could use 18 instead of zero.

    • 4 means four float components per vertex

    • GL_FLOAT component type

    • GL_FALSE means "use the float values directly." Fixed-point data does not need to be normalized (converted to float) in a certain range ( -1, 1 signed or 0, 1 unsigned.)

    • 0 is stride - byte offset (gap) between consecutive vertex attributes

    • 0 is pointer - byte offset of the first component

  • Vertex attributes are input parameters to the vertex shader

  • Vertex array object stores OpenGL state needed to draw an object

  • Vertex buffer object is an array of GPU memory.

  • 
    GLuint VertexShaderId;
    GLuint FragmentShaderId;
    GLuint ProgramId;
    GLuint VaoId;         // vertex array object name
    GLuint VboId;         // vertex buffer object name
    GLuint ColorBufferId;
    
    void CreateVBO()
    {
        // Each vertex has four dimensions
        GLfloat Vertices[] = {
            -0.8f, -0.8f, 0.0f, 1.0f,
             0.0f,  0.8f, 0.0f, 1.0f,
             0.8f, -0.8f, 0.0f, 1.0f
        };
     
        // Each vertex color has four components
        GLfloat Colors[] = {
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 1.0f
        };
     
        GLenum ErrorCheckValue = glGetError();
         
        glGenVertexArrays(1, &VaoId);
        glBindVertexArray(VaoId);
     
        glGenBuffers(1, &VboId);
        glBindBuffer(GL_ARRAY_BUFFER, VboId);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(0);
         
        glGenBuffers(1, &ColorBufferId);
        glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(1);
     
        ErrorCheckValue = glGetError();
        if (ErrorCheckValue != GL_NO_ERROR)
        {
            fprintf(
                stderr,
                "ERROR: Could not create a VBO: %s \n",
                gluErrorString(ErrorCheckValue)
            );
     
            exit(-1);
        }
     }