Light | Dark

# docs.GL

OpenGL 4
OpenGL 3
OpenGL 2
OpenGL ES 3
OpenGL ES 2

# glGenBuffers

## Name

glGenBuffers — generate buffer object names

## C Specification

`void glGenBuffers(` | GLsizei n,
| GLuint * buffers`)`;

## Parameters

`n`

  Specifies the number of buffer object names to be generated.

`buffers`

  Specifies an array in which the generated buffer object names are stored.

## Description

`glGenBuffers` returns `n` buffer object names in `buffers`. There is no guarantee that the names form a contiguous set of integers; however, it is guaranteed that none of the returned names was in use immediately before the call to `glGenBuffers`.

Buffer object names returned by a call to `glGenBuffers` are not returned by subsequent calls, unless they are first deleted with glDeleteBuffers.

The names returned in `buffers` are marked as used, for the purposes of `glGenBuffers` only, but they acquire state and type only when they are first bound by calling glBindBuffer.

## Errors

`GL_INVALID_VALUE` is generated if `n` is negative.

## Associated Gets

glIsBuffer

## Examples

Load a vertex buffer into OpenGL for later rendering.

    // data_size_in_bytes is the size in bytes of your vertex data.
    // data_vertices is your actual vertex data, probably a huge array of floats

    GLuint vertex_buffer; // Save this for later rendering
    glGenBuffers(1, &vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, data_size_in_bytes, data_vertices);

    GLint size = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
    if(data_size_in_bytes != size)
    {
        glDeleteBuffers(1, &vertex_buffer);
        // Log the error
        return;
    }

    // Success

Load an index buffer into OpenGL for later rendering.

    // data_size_in_bytes is the size in bytes of your vertex data.
    // data_indices is an array of integer offsets into your vertex data.

    GLuint index_buffer; // Save this for later rendering
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW);
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, data_size_in_bytes, data_indices);

    GLint size = 0;
    glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
    if(data_size_in_bytes != size)
    {
        glDeleteBuffers(1, &index_buffer);
        // Log the error
        return;
    }

    // Success

## Tutorials

Songho - OpenGL Pixel Buffer Object (PBO)
nehe.gamedev.net - iOS Lesson 02 - First Triangle
open.gl - The Graphics Pipeline
open.gl - Transform Feedback
opengl-tutorial.org - Particles / Instancing
opengl-tutorial.org - Tutorial 13 : Normal Mapping
opengl-tutorial.org - Tutorial 14 : Render To Texture
opengl-tutorial.org - Tutorial 2 : The first triangle
opengl-tutorial.org - Tutorial 4 : A Colored Cube
opengl-tutorial.org - Tutorial 8 : Basic shading

## API Version Support

| OpenGL ES API Version
--- | ---
Function Name | 2.0 | 3.0 | 3.1
glGenBuffers | ✔ | ✔ | ✔

## See Also

glBindBuffer, glDeleteBuffers, glGet

## Copyright

Copyright © 2005 Addison-Wesley. Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. https://opencontent.org/openpub/.

Think you can improve this page? Edit this page on GitHub.