WaltN's picture

How are glGenBuffers and glDeleteBuffers intended to be used?

Both glGenBuffers and glDeleteBuffers are defined with a second argument that is an array. However, all of the examples I've seen (save one) employ a count of 1 and a buffer ID of a scalar integer. (The single exception is the example in the Redbook.) Yet, under some circumstances, it is difficult or awkware to find an OpenTK overload that will take a scalar integer. I've ended up using the overload with the ref'ed ID. This leads me to wonder if I'm using these functions properly, which leads to the question in the subject line.

For example, was the intent of the OpenGL architects to have applications employ a single pool (fixed length array) of buffer IDs?
Or, perhaps, a list of relatively small buffer ID arrays? Or, did they ever had in mind that people would want to use the scalar case?

My case: I have a class called StandardObject that encapsulates key elements of buffer objects. Among the elements are the VAO ID (an int) and list of VBO IDs (a List) that constitute a buffered object. The objective is to represent a wide variety of buffered object forms -- legitimate combinations of (index array, vertex array, normal array, texcoord array, color array, etc.

For example, here is my code for the member function that deletes buffers:

        /// <summary>
        /// Deletes VAOs and VBOs associated with this standard object.
        /// </summary>
        public void Delete()
        {
            // Delete VAO
            int vaoid = VAOID;
            GL.DeleteVertexArrays(1, ref vaoid);
 
            foreach (int id in VBOIDs)
            {
                int refID = id;
                GL.DeleteBuffers(1, ref refID);
            }
        } // end delete

It strikes me that it is a bit awkward to use scalar buffer IDs.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Robmaister's picture

The "ref" is needed because the standard method call is a pointer to an int. (Arrays can also be used because they decay into pointers) Length is there to specify the length of the array, which can't be determined from the pointer itself. It's just something that translates a bit awkwardly to C#, I think OpenTK did a pretty good job and easing the transition though. IIRC the original method also sets the variables to 0 (might only be for textures, not sure), so passing a regular int would provide different functionality than the regular OpenGL API. In C/C++ the difference between 1 int and an array is trivial: glDeleteBuffers(1, &refID); or glDeleteBuffers(3, refArray);

OpenGL doesn't really care how your buffers are stored and they didn't have anything in mind when creating this part of the API. Do whatever is best for you. In my code I'll create a new array and accumulate all my buffers into the array and make a single GL.DeleteBuffers call.

WaltN's picture

Read and understood. Thanks for the perspective.