Harold Serrano

View Original

Visualizing OpenGL Objects

Introduction

Visualizing how OpenGL objects are created, bound and initialized is difficult. Comparing OpenGL objects to C++ objects can be helpful in visualizing the similarities between creating objects in OpenGL and creating objects in C++.

NOTE: The explanation in this post should not be taken literally. It is only for pedagogy reasons to help you visualize OpenGL. The functions and structures mentioned in this section may not be part of OpenGL.

The OpenGL Context

A good way to visualize the openGL context is to see it as a structure with many data members;

For example:

struct OpenGLContext{
    DataType1 *dataMember1;
    DataType2 *dataMember2;
    //...
};

Object in OpenGL

Objects in OpenGL can be visualized as structure as well. For example, this struct can represent an OpenGL object of Buffer type.

struct Buffer{
    int value1;
    int value2;
}

Comparing OpenGL and C++ object creation and binding

This section will show how OpenGL creates and binds objects and compare it to how it would be done in C++.

Refer to the following C++ structures as you read the following sections:

//Structure representing an OpenGL Buffer object.
struct Buffer{
    float opacity;
    float depth;
}

//Structure representing an OpenGL context.
struct OpenGLContext{
    Buffer *GL_ARRAY_BUFFER;
    Buffer *GL_TEXTURE_BUFFER;
    ...
};

//creation of an OpenGL context instance.
OpenGLContext context;

Creating an object

To create an object of Buffer type, you do the following in C++:

Buffer bufferName;

However, in OpenGL this is equivalent to doing this:

//create the storage for the object
GLuint bufferName;
glGenBuffer(1,&bufferName);

Binding an object

To bind an object you do the following in C++:

context.GL_ARRAY_BUFFER=bufferName;

which is equivalent to doing this in OpenGL

glBindBuffer(GL_ARRAY_BUFFER,bufferName);

Assigning data to an object

In C++, to assign data to an object:

context.GL_ARRAY_BUFFER.opacity=0.5;

whereas in OpenGL is done like this:

glBufferData(GL_ARRAY_BUFFER,...,0.5,...);

I hope this helps you understand how OpenGL creates, bind and load data into OpenGL buffers.

NOTE: The explanation in the post should not be taken literally. It is only for pedagogy reasons to help you visualize OpenGL. The functions and structures mentioned in the previous section may not be part of OpenGL.