OpenGL

Graphique 3D - ENSIMAG 2A

OpenGL Main Concepts

OpenGL defines an API of a rendering system.

Context
Contains all of the information that will be used by the OpenGL system to render
State
OpenGL context information used by the rendering system
Shader
Programmable stage of the rendering pipeline
Object
Group of state values that could be bind to the context
Khronos Group wiki on OpenGL Concepts

Before Rendering

Create data on CPU

OpenGL Data














	  

Before Rendering

Generate buffer object name for vertex positions and bind it to the GL_ARRAY_BUFFER target

OpenGL Data

GLuint posVBO, colVBO, indicesVBO;

glGenBuffer(1, &posVBO);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);









	  

Before Rendering

Create and initialize buffer object's data store of the currently bound buffer object

OpenGL Data

GLuint posVBO, colVBO, indicesVBO;

glGenBuffer(1, &posVBO);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), vPos, GL_STATIC_DRAW);








	  

Before Rendering

Create and initialize buffer object for vertex colors

OpenGL Data

GLuint posVBO, colVBO, indicesVBO;

glGenBuffer(1, &posVBO);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), vPos, GL_STATIC_DRAW);

glGenBuffer(1, &colVBO);
glBindBuffer(GL_ARRAY_BUFFER, colVBO);
glBufferData(GL_ARRAY_BUFFER, 16*sizeof(float), vCol, GL_STATIC_DRAW);




	  

Before Rendering

Create and initialize buffer object for triangle indices

OpenGL Data

GLuint posVBO, colVBO, indicesVBO;

glGenBuffer(1, &posVBO);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), vPos, GL_STATIC_DRAW);

glGenBuffer(1, &colVBO);
glBindBuffer(GL_ARRAY_BUFFER, colVBO);
glBufferData(GL_ARRAY_BUFFER, 16*sizeof(float), vCol, GL_STATIC_DRAW);

glGenBuffer(1, &indicesVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*sizeof(unsigned int), vIndices, GL_STATIC_DRAW);
	  

Rendering Pipeline Overview

OpenGL Rendering Pipeline

Vertex Specification

Specify the value of a uniform variables for the program object

OpenGL Data

int projLoc = glGetUniformLocation(program, "projMat");
int viewlLoc = glGetUniformLocation(program, "viewMat");	      
int modelLoc = glGetUniformLocation(program, "modelMat");

glUniformMatrix4fv(projLoc, 1, GL_FALSE, projMat);
glUniformMatrix4fv(viewlLoc, 1, GL_FALSE, viewMat);
glUniformMatrix4fv(modellLoc, 1, GL_FALSE, modelMat);








	  

Vertex Specification

Enable a generic vertex attribute array for vertex position

OpenGL Data

int posLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(posLoc);













	  

Vertex Specification

Specify location and data layout for a generic vertex array

OpenGL Data

int posLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(posLoc);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);











	  

Vertex Specification

Enable and specify data of a generic vertex attribute array for vertex color

OpenGL Data

int posLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(posLoc);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

int colLoc = glGetAttribLocation(program, "vCol");
glEnableVertexAttribArray(colLoc);
glBindBuffer(GL_ARRAY_BUFFER, colVBO);
glVertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);






	  

Vertex Specification

Bind the IBO and render

OpenGL Data

int posLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(posLoc);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

int colLoc = glGetAttribLocation(program, "vCol");
glEnableVertexAttribArray(colLoc);
glBindBuffer(GL_ARRAY_BUFFER, colVBO);
glVertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vIndices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);



	  

Vertex Specification

Release the vertex array attrbutes

OpenGL Data

int posLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(posLoc);
glBindBuffer(GL_ARRAY_BUFFER, posVBO);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

int colLoc = glGetAttribLocation(program, "vCol");
glEnableVertexAttribArray(colLoc);
glBindBuffer(GL_ARRAY_BUFFER, colVBO);
glVertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vIndices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);

glDisableVertexAttribArray(posLoc);
glDisableVertexAttribArray(colLoc);
	  

Rendering Pipeline : Vertex Processing

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline

Vertices are processed via a series of Shaders

Vertex Shader
Perform computations on the attributes of each vertex, such as model-view transformation and projection
Tesselation Shader
Evaluate if new vertices should be created (optional)
Geometry Shader
Performs computations on primitive (optional), can discard a primitive or creates more primitives

Rendering Pipeline : Vertex Shader

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline

Data layout for vertex array was specified by

glVertexAttribPointer​(GLuint index​, GLint size​,
		      GLenum type​, GLboolean normalized​,
                      GLsizei stride​, const void *offset​)
	      
  • size : the number of components (4 max)
  • type : data type
  • stride : offset between consecutive attributes (0 for packed data)
  • offset : offset of the first component of the first attribute in the VBO

Rendering Pipeline : Vertex Post-Processing

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline
  • Clipping
  • Viewport Tranformation to the window space

Rendering Pipeline : Primitive Assembly

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline
  • Convert vertex stream into a sequance of base primitive
  • Primitives are points, lines and traingles

Rendering Pipeline : Rasterization

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline
  • Discretizes primitives into fragments
  • Linear interpolation of vertex attributes

Rendering Pipeline : Fragment Shader

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline
  • Perform computations on fragments
  • e.g. local illumination, texture mapping

Rendering Pipeline : Per-samples operations

OpenGL Rendering Pipeline
OpenGL Rendering Pipeline
  • Blending
  • Depth test (depth buffer)
  • Stencil test (stencil buffer)