Introduction to OpenGL

Introduction

OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. It is a standard that has been adopted by many different hardware vendors, making it a reliable and widely-used API for graphics programming.

OpenGL ES (OpenGL for Embedded Systems) is a subset of OpenGL that is designed for embedded systems, such as mobile devices and game consoles. It is a smaller and more lightweight API than OpenGL, but it still provides a powerful set of features for rendering 2D and 3D graphics.

Both OpenGL and OpenGL ES are extensible, meaning that they can be extended with new features and functionality through the use of extensions. Extensions are add-on specifications that are not part of the core OpenGL or OpenGL ES specification. They are typically developed by hardware vendors or third-party developers to add new features or improve performance.

GLSL (OpenGL Shading Language) is a high-level programming language that is used to write shaders for OpenGL and OpenGL ES. Shaders are small programs that are executed on the GPU (graphics processing unit) to control the rendering of graphics primitives. They can be used to perform a variety of tasks, such as calculating lighting, texturing, and transformations.

Here are some examples of OpenGL extensions:

  • GL_ARB_vertex_shader: This extension adds support for vertex shaders, which are programs that are executed on the GPU to transform and render vertices.
  • GL_ARB_fragment_shader: This extension adds support for fragment shaders, which are programs that are executed on the GPU to calculate the color of each pixel.
  • GL_EXT_texture_compression_s3tc: This extension adds support for S3TC texture compression, which is a method of reducing the size of texture images without sacrificing too much quality.

Here are some examples of GLSL shaders:

  • A vertex shader that transforms a set of vertices into clip space.
  • A fragment shader that calculates the color of each pixel based on its position and the texture coordinates of the fragment.
  • A shader that performs lighting calculations on a mesh.

OpenGL and OpenGL ES are powerful APIs that can be used to create a wide variety of 2D and 3D graphics applications. They are supported by a wide range of hardware platforms, making them a reliable and versatile choice for graphics programming.

Example

Screen framebuffer

// Declare some variables
GLuint framebuffer;
GLuint texture;

// Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

// Create the framebuffer
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

// Create the texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

// Check the status of the framebuffer
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    std::cout << "Framebuffer is not complete!" << std::endl;
}

// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);

// Swap the buffers
glfwSwapBuffers(window);

This code first declares some variables that will be used to store the framebuffer and texture objects. Then, it initializes OpenGL and clears the screen to black. Next, it creates the framebuffer and texture objects. Finally, it binds the framebuffer and texture objects, draws the triangle, and swaps the buffers.

The glFramebufferTexture2D() function attaches the texture object to the framebuffer object. The first parameter specifies the target framebuffer, the second parameter specifies the attachment point, the third parameter specifies the texture target, the fourth parameter specifies the texture object, and the fifth parameter specifies the mipmap level.

The glCheckFramebufferStatus() function checks the status of the framebuffer object. If the framebuffer object is not complete, the function will return an error code.

Off-screen framebuffer

// Declare some variables
GLuint framebuffer;
GLuint texture;
GLuint renderbuffer;

// Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

// Create the framebuffer
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

// Create the texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

// Create the renderbuffer
glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);

// Check the status of the framebuffer
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    std::cout << "Framebuffer is not complete!" << std::endl;
}

// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);

// Swap the buffers
glfwSwapBuffers(window);

This code is very similar to the screen framebuffer example, but it also creates a renderbuffer object. The renderbuffer object is used to store the depth and stencil data for the framebuffer.

The glFramebufferRenderbuffer() function attaches the renderbuffer object to the framebuffer object. The first parameter specifies the target framebuffer, the second parameter specifies the attachment point, the third parameter specifies the renderbuffer target, and the fourth parameter specifies the renderbuffer object.

The glCheckFramebufferStatus() function checks the status of the framebuffer object. If the framebuffer object is not complete, the function will return an error code.