OpenGL Code Snippets

Can't remember how to load data into buffers? or how to create texture objects? Below are OpenGL code snippets which you can use as reference.

Don't forget to subscribe, since new code snippets are added on a regular basis.


How to load data into OpenGL Buffers

Steps:

  1. Create buffer object
  2. Bind buffer object
  3. Load data into buffer objects
//Steps to using OpenGL Buffers

//1. Create a buffer object
GLuint myBuffer;
glGenBuffers(1,&myBuffer);

//2. Bind the buffer object
glBindBuffer(GL_ARRAY_BUFFER, myBuffer);

//3. Load data into the buffer object
glBufferData(GL_ARRAY_BUFFER,sizeof(data),data,GL_STATIC_DRAW);

How to load data into OpenGL Textures

Steps:

  1. Activate a texture unit
  2. Create a texture object
  3. Bind the texture object
  4. Load data into the texture object
//Steps to using OpenGL Textures

//1. Activate a texture unit. We are activating texture unit 0.
glActiveTexture(GL_TEXTURE0);

//2. Create Texture Object
GLuint texture;
glGenTextures(1,&texture);

//3. Bind texture Object
glBindTexture(GL_TEXTURE_2D, texture);

//4. Load data into texture object
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);

How to compile/link Shaders in OpenGL

Steps:

  1. Create shader objects
  2. Compile shader objects
  3. Create Program object
  4. Attach Shader objects
  5. Link Shader Objects
//Steps to compile, attach and link shader objects

// Temporary Shader objects representing the vertex and fragment shader, respectively.
GLuint VertexShader;
GLuint FragmentShader;

//1. Create shader objects
VertexShader = glCreateShader(GL_VERTEX_SHADER);
FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

//2. Compile both shader objects
glCompileShader(VertexShader);
glCompileShader(FragmentShader);

//3. Create a shader program object
GLuint programObject = glCreateProgram();

//4. Attach the shader objects to the shader program object
glAttachShader(programObject, VertexShader);
glAttachShader(programObject, FragmentShader);

//5. Link both shader objects to the program object
glLinkProgram(programObject);

//Use the program object
glUseProgram(programObject);

How to use Vertex Array Objects (VAO) in OpenGL

Steps:

  1. Create VAO object
  2. Bind VAO object
  3. Set up buffer object
  4. Unbind VAO object
//Steps to using Vertex Array Objects

//1. Generate a Vertex Array Object.
GLuint myVAO;
glGenVertexArrays(1,&myVAO);

//2. Bind the Vertex Array Object
glBindVertexArray(myVAO);

//3. Set up Buffer. 
//...See code snippet "Using Buffers" shown above 

//4.Unbind the VAO
glBindVertexArray(0);

How to set OpenGL textures sampling parameters

Steps:

  1. Set Wrapping parameters
  2. Set Filtering parameters
//Steps to setting texture sampling parameters

//1. set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

//2. set the texture magnification/minification filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

How to load attribute data into OpenGL buffers

Steps:

  1. Create buffer object
  2. Bind buffer object
  3. Load data into buffer
  4. Get attribute locations
  5. Enable attribute locations
  6. Link buffer data to the attribute location
//Steps to load vertex attributes into buffer objects

//Vertex data of character
float vertexData[250]={1.0,0.4,0.9,1.0,....};

//1a. Create a buffer object
GLuint myBuffer;
glGenBuffers(1,&myBuffer);

//1b. Bind a buffer
glBindBuffer(GL_ARRAY_BUFFER,myBuffer);

//1c. Load data in the buffer
glBufferData(GL_ARRAY_BUFFER,sizeof(vertexData),vertexData,GL_STATIC_DRAW);

//2. Get the location of the shader attribute called "position"    
GLuint positionLocation=glGetAttribLocation(programObject, "position");

//3. Enable the attribute location
glEnableVertexAttribArray(positionLocation);

//4. Link the buffer data to the shader attribute locations.
glVertexAttribPointer(positionLocation,3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)0);

How to load attributes separately in OpenGL

Steps:

  1. Create buffer object
  2. Bind buffer object
  3. Load data into the buffer by using glBufferSubData
  4. Get each attribute location
  5. Enable each attribute location
  6. Link buffer data to the attribute locations
//Steps to load multiple separate vertex attributes into buffer objects

//Arrays representing data: vertices positions, Normals and UV coordinates. 

float position[250]={1.0,0.0,1.0,
                   0.0,1.0,0.0,
                   0.5,0.5,1.0,
                   0.5,0.0,0.4,....};

float normal[250]={1.0,0.0,1.0,
                   0.0,1.0,0.0,
                   0.5,0.5,1.0,
                   0.5,0.0,0.4,....};

float uv[250]={1.0,0.0,
               1.0,0.0,
               1.0,0.0,
               0.5,0.5,....};     

//1a. Create a buffer
GLuint myBuffer;
glGenBuffers(1, &myBuffer);

//1b. Bind the Vertex Buffer Object    
glBindBuffer(GL_ARRAY_BUFFER, myBuffer);

//1c. Dump the data into the Buffer. Notice the NULL set as a pointer to the data.    
glBufferData(GL_ARRAY_BUFFER, sizeof(position)+sizeof(normal)+sizeof(uv), NULL, GL_STATIC_DRAW);

//1d. Load position data using glBufferSubData
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(position), position);

//1e. Load normal data using glBufferSubData
glBufferSubData(GL_ARRAY_BUFFER, sizeof(position), sizeof(normal), normal);

//1f. Load UV data using glBufferSubData
glBufferSubData(GL_ARRAY_BUFFER, sizeof(position)+sizeof(normal), sizeof(uv), uv);

//2. Get the location of the attributes from the shader
GLuint positionLocation=glGetAttribLocation(shaderProgram,"position");
GLuint normalLocation=glGetAttribLocation(shaderProgram,"normal");
GLuint uvLocation=glGetAttribLocation(shaderProgram, "texCoord");

//3. enable the attribute locations
glEnableVertexAttribArray(positionLocation);
glEnableVertexAttribArray(normalLocation);
glEnableVertexAttribArray(uvLocation);

//4. Link the buffer data to the shader attribute locations
//4a. Link buffer data to position attribute location   
glVertexAttribPointer(positionLocation,3,GL_FLOAT,GL_FALSE,0,(const GLvoid*)(0));

//4b. Link buffer data to normal attribute location  
glVertexAttribPointer(normalLocation,3,GL_FLOAT,GL_FALSE,0,(const GLvoid*)sizeof(position));

//4c. Link buffer data to texture attribute location
glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)sizeof(position)+sizeof(normal));

How to load interleaved attribute data

Steps:

  1. Create buffer object
  2. Bind buffer object
  3. Load data into the buffer by using glBufferData
  4. Get each attribute location
  5. Enable each attribute location
  6. Link the buffer data to the attribute locations
//Steps to load interleaved vertex attributes into buffer objects

//Array representing interleaved data: UV coordinates, Normals and vertices positions
GLfloat characterData[192] =
    {
        // Data layout for each line below is:
        // UV1,UV2, Normal1, Normal2, Normal3, positionX, positionY, positionZ
        0.00000, 1.00000, 0.00000, 0.00000, 1.00000, -0.50000, -0.50000, 0.50000,
        0.00000, 0.00000, 0.00000, 0.00000, 1.00000, -0.50000, 0.50000, 0.50000...};

//1a. Create a Vertex Buffer Object
GLuint myBuffer;
glGenBuffers(1, &myBuffer);

//1b. Bind the Vertex Buffer Object    
glBindBuffer(GL_ARRAY_BUFFER, myBuffer);

//1c. Dump the data into the Buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(characterData), characterData, GL_STATIC_DRAW);

//2. Get the location of the shader attribute called "position"
GLuint positionLocation=glGetAttribLocation(shaderProgram, "position");

//Get the location of the shader attribute called "normal"    
GLuint normalLocation=glGetAttribLocation(shaderProgram, "normal");

//Get the location of the shader attribute called "texCoord"    
GLuint textureLocation=glGetAttribLocation(shaderProgram, "texCoord");

//3. Enable attribute locations    
glEnableVertexAttribArray(positionLocation);
glEnableVertexAttribArray(normalLocation);
glEnableVertexAttribArray(textureLocation);

//4. Link the buffer data to the shader attribute locations
glVertexAttribPointer(textureLocation, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(8));
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(20));