PowerPoint Presentation
Introduction to OpenGL
Computer Graphics
Instructor: Sungkil Lee
2
OpenGL
• IRIS GL (Graphics Library):
• Silicon Graphics (SGI) revolutionized the graphics workstation by
implementing the pipeline approach in hardware (1982).
• OpenGL (Open Graphics Library):
• The success of IRIS GL led to OpenGL (1992).
• A platform-independent rendering API
• Close enough to the hardware to get excellent performance
• Extensible for platform-specific features through extension mechanics
• Still, it is an industry standard for 3D graphics.
3
OpenGL Management Consortium
• Originally controlled by Architectural Review Board (ARB)
• Members includes: SGI, MS, NVIDIA, HP, Apple, 3DLabs, IBM, …
• Now managed by Khronos group (www.khronos.org)
• Current promoter members (board of directors)
• Active standards
4
Old-Style OpenGL
• Through version 2.5 (more strictly up to version 3.1)
• was relatively stable and backward-compatible
• The features were fixed, and impossible to modify
• So, the pipeline is called the “fixed-pipeline,” which simulates the basic
transformation/projection (in vertex shader) and Blinn-Phong shading (in
fragment shader).
• Now, many of the architecture are deprecated in modern-style OpenGL
(since version 3.2).
5
Graphics Hardware
• GPU (Graphics Processing Unit)
• Modern computer has dedicated Graphics Processing Unit (GPU) to
produce images for the display.
• GPU is a special complete parallel computing system which has thousands
of cores with its own graphics memory hierarchy (or Video RAM or VRAM).
6
Graphics Hardware
• Example: NVIDIA GTX 1080
• 2560 CUDA Cores (1.6 GHz)
• 160 Texture units, 8GB Memory
7
Modern-Style OpenGL
• Modern-style OpenGL:
• OpenGL since version 3.2 is called “modern-style OpenGL”.
• Using Powerful GPUs
• Intensively using GPU rather than CPU for high performance
• Application’s job is only to send data to GPU.
• GPU does all rendering (as well as some computing).
8
Modern-Style OpenGL
• User-programmable pipeline
• Now, user can modify the vertex and fragment processing stages.
• This is possible by GPU programs called shaders.
• Totally shader-based
• No default shaders (as fixed pipeline) available
• Each application must provide both a vertex and a fragment shader
• Most 2.5 and previous functions deprecated.
9
Modern-Style OpenGL
• OpenGL for each Hardware generation
10
Modern-Style OpenGL
• This course will focus only on modern-style OpenGL.
• Basically, old-style OpenGL stuffs (many available on the web) are not
allowed here.
• Even, I will not let you know what the old-style was for less confusion.
• Why?
• Recently, all the major industries only use modern style.
• e.g., OpenGL ES 2.0/3.x, WebGL
• Largely extensible via shader programming.
• Your own stuffs can be implemented only with the modern style.
• The latest GPU features are available only in modern style.
11
Other APIs
• WebGL
• Javascript implementation of OpenGL ES 2.0
• Supported on most modern browsers (e.g., Chrome, FireFox, Safari, Opera)
• Vulkan
• The next-generation graphics API: the successor of OpenGL 4
• Designed for high-performance graphics and computing with less driver
overhead
• Useful for low-level graphics developers
Introduction to OpenGL ES
13
OpenGL ES
• OpenGL ES (Embedded Systems)
• Well-defined subsets of desktop OpenGL, essential in mobile applications.
• Includes profiles for floating-point and fixed-point systems and EGL.
• Roadmap for OpenGL ES
14
OpenGL ES 1.x
• OpenGL ES 1.x
• Defined for fixed-function hardware, but now deprecated in later versions.
• OpenGL ES 1.0 is derived from OpenGL 1.3 (old style GL)
• OpenGL ES 1.1 is derived from OpenGL 1.5 (old style GL)
15
OpenGL ES 2.0
• OpenGL ES 2
• Defined relative to OpenGL 2.0 (old style but near to modern style)
• Programmable pipeline with vertex/fragment shaders.
• Does not support fixed-function transformation and fragment pipeline of
OpenGL ES 1.x
16
OpenGL ES 2.0
• OpenGL ES Shading Language 1.0 (ESSL 1.0, 2009)
• Adds the same shading language used in OpenGL 2.0 but adapted for
embedded platforms.
• Precision qualifier should be specified in the shader program.
• Minimum precisions required in any platforms
• Vertex shader: 16-bit precision (in range [-262,+262] for fp)
• Fragment shader: 10-bit precision (in range [-214,+214] for fp)
17
OpenGL ES 2.0
• Precision Qualifiers
• highp, mediump, lowp
• range/precision:
18
OpenGL ES 3.x
• OpenGL ES 3.x is fully compatible with OpenGL 4.3
• OpenGL ES 3.0
• Multiple render targets
• Occlusion queries, transform feedback, instanced rendering
• ETC2/EA texture compression
• A new ESSL with 32-bit integers and floats
• Textures: NPOT, floating-point, 3D, 2D array
• Swizzle, LOD, mip level clamps, seamless cube maps, and sampler objects
19
OpenGL ES 3.x
• OpenGL ES 3.1
• Computer shaders
• Indirect draw commands
• OpenGL ES 3.2
• Geometry and tessellation shaders
• Floating-point render targets
• ASTC (adaptive scalable texture compression)
• Enhanced blending and handling of multiple color attachments
• Advanced texture targets: texture buffers, multisample 2D array, cubemap
arrays
• Debug and robustness for security
20
• EGL (Khronos Native Platform Graphics Interface)
• Interface specification between Khronos rendering APIs (e.g., OpenGL and
OpenGL ES) and the underlying native windowing platform.
• Handles graphics context management, surface/buffer binding, rendering
synchronization, and mixed-mode 2D and 3D rendering.
• Prior to EGL 1.2, it was OpenGL ES Native Platform Graphics Interface.
• Similar interfaces
• WGL: for windows
• CGL (Core OpenGL): for OS X
• GLX: for X11
• WSI (Window System Interface) : for Vulkan
Introduction to OpenGL API
22
API?
• Application Programming Interface (API)
• A protocol intended to be used as an interface by software/hardware
components to communicate with each other.
• A library that may include specification for routines, data structures, object
classes, and variables.
• Examples:
• POSIX (for Unix-like programming)
• Microsoft Windows API (for windows programming)
• C++ Standard Template Library (STL)
23
OpenGL API
• OpenGL API:
• Allows us to interact with graphics hardware and other software platform
via abstract forms of function calls.
• Cross-language, cross-platform API
• Languages: C/C++, Java, C#, Fortran 90, Perl, Python, Delphi, Ada, …
• Platforms: Windows, Linux, Apple, …
• Windowing support not exists for cross-platform API
• 3rd-party libraries necessary (e.g., GLUT, freeGLUT, GLFW)
24
OpenGL API: Lack of Object Orientation
• It’s a pure “C” API.
• There are multiple functions for a given logical function.
• e.g., glUniform3f(), glUniform2i(), glUniform3dv()
• Underlying storage mode is the same
• Easy to create overloaded functions in C++ but an issue is efficiency.
• However, in practice, many of third-party libraries use OpenGL API with
their C++ APIs.
• OpenGL is a state machine
• Since it does not use OOP, many of the API states are stored internally.
• You can access it by query functions (e.g., glGetSomething())
25
OpenGL API: Function Format
• It uses a Hungarian-like notation with variable types.
glUniform3f(x,y,z)
belongs to GL library
function name
x,y,z are floats
dimensions
glUniform3fv(p)
p is a pointer to an array
26
OpenGL #defines
• Most constants are defined in
• glEnable(GL_DEPTH_TEST)
• glClear(GL_COLOR_BUFFER_BIT)
• include files also define OpenGL data types:
• e.g., GLint, GLfloat, GLdouble,….
• basically, GLint is equivalent to int.
OpenGL Shading Language (GLSL)
28
OpenGL Shading Language (GLSL)
• Shader-based OpenGL:
• is based less on a state machine model than a data flow model.
• Major action happens in shaders
• API’s Job is for application to get data to GPU
• Shader uses a C-like scripting language (GLSL)
• For writing OpenGL shader sources we should use GLSL.
• Direct3D uses HLSL (High-Level Shading Language).
• Usually, the shader program codes are compiled and launched at run-time.
• Part of OpenGL 2.0 and higher
• As of OpenGL 3.1, application must provide shaders.
29
Code Snippets in Vertex Shader
• Vertex shader
in vec3 position; // vertex input attribute (you may use vec3/vec4 too)
in vec3 normal; // vertex input attribute
out vec3 vertex_color; // output of vertex shader
void main()
{
// gl_Position: builtin output variable that must be written as a screen position of vertex
gl_Position = vec4( position, 1 );
…
// another output passed via input variable
vertex_color = normal;
}
30
Code Snippets in Vertex Shader
• Fragment shader
// the second input from vertex shader
in vec3 vertex_color;
// define output variable to be shown in the display
out vec4 fragColor;
// shader’s global variables, called the uniform variables
// Uniform variables will be shared among all the fragments (like a global variable).
uniform bool bUseSolidColor;
uniform vec4 solid_color;
void main()
{
// fragColor: you must specify it to produce color
fragColor = bUseSolidColor ? solid_color :vec4(vertex_color,1);
}
31
OpenGL Shading Language (GLSL)
• GLSL features
• All shaders have a single main() function
• New data types: vectors, matrices, texture samplers
• Overloaded operators, C++ like constructors
• Preprocessors like C supported
• If you know C,
• But, there are missing C features
• No pointers or dynamic memory allocation: All data in stack
• No call stack (meaning no recursion): All functions are inlined
• No strings, characters, double, short, long
• No file I/O, console I/O (e.g., printf)
32
GLSL Data Types
• Basic data types
• void, float, int, bool
• Vector data types
• vec2, vec3, vec4: float vector types
• ivec2/uvec2, ivec3/uvec3, ivec4/uvec3: (unsigned) int vector types
• bvec2, bvec3, bvec4: bool vector types
• Matrix data types
• mat2, mat3, mat4: 2×2, 3×3, 4×4 matrix
• Sampler data types (for textures)
• sampler1D, sampler2D, sampler3D, samplerCube, …
• Opaque data types (as opposed to the other transparent data types),
because their implementation is hidden to the programmer.
33
More on GLSL Vector Types
• Accessing components in four ways
• position/direction: .x, .y, .z, .w
• color: .r, .g, .b, .a
• texture coordinates: .s, .t, .p, .q
• array indexing: [0], [1], [2], [3]
• Constructors supported
• vec3 v = vec3(1.0, 0.0, 0.0)
• Swizzle operators: select or rearrange components
• vec4 v = vec4(1.0, 0.0, 0.0, 1.0)
• vec3 v3 = v.rgb;
• vec4 v4 = v.zzyx;
34
GLSL Built-in Functions
• Math
• radians, degrees, sin, cos, tan, asin, acos, atan
• pow, exp, exp2, log2, sqrt, inversesqrt
• abs, sign, floor, ceil, fract, mod, min, max, clamp
• Interpolations
• mix (similar to lerp; linear interpolation), step, smoothstep
• Geometric
• length, distance, cross, dot, normalize, faceForward, reflect
• Vector relational
• lessThan, lessThanEqual, greaterThan, greaterThanEqual, equal, notEqual,
any, all
35
GLSL Variable Qualifiers
• Common for all shader types
• uniform: read-only global variables (well cached)
• e.g., uniform vec4 solid_color;
• Vertex shader
• Modern style (GL ES 3): in/out
• Old-style (GL ES2): attribute/varying
• Fragment shader
• Modern style (GL ES 3): in/out
• Old-style (GL ES2): varying/gl_FragColor
• gl_FragColor is built-in default output variable
36
Specific to OpenGL ESSL
• #version should accompany es
• e.g., #version 300 es
• Precision qualifiers
• Unless default precision is given, per-value precision or default precision
needs to be provided. Default precisions on vertex and fragment shaders
are:
• The default precisions applies also to vector types (e.g., float for vec4).
• highp support can be detected using the macro
37
OpenGL/GLES/GLSL References
• Official Wiki/Tutorials (very helpful)
• https://www.khronos.org/opengl/wiki/
• http://open.gl/
• Specification
• https://www.khronos.org/registry/OpenGL/specs/gl/
• https://www.khronos.org/registry/OpenGL/specs/es/3.2/
• Reference pages
• https://www.khronos.org/registry/OpenGL-Refpages/gl4/
• https://www.khronos.org/registry/OpenGL-Refpages/es3/
• Quick reference cards
• https://www.khronos.org/developers/reference-cards/