CS123 02 GL 2D 9.10.19
CS123 02 GL 2D 9.10.19
Introduction to 2D Graphics
Using OpenGL
Older (OpenGL 1.0) API provided features for rapid prototyping; newer API
(OpenGL 2.0 and newer) provides more flexibility and control
Many old features exist in new API as “deprecated” functionality, supported only
for backwards-compatibility with legacy apps
We will use the new API exclusively
Specification of hierarchy
support building scenes as hierarchy of objects, using transforms (scale,
rotate, translate) to place children into parents' coordinate systems
support manipulating composites as coherent objects
Retained
• JavaFX (Oracle)
Smart Controls
• Qt (Qt Group)
In class Question 1
OpenGL (1/3)
Immediate-mode graphics API
No display model, application must direct
OpenGL to draw primitives
Cross-platform
Also available on mobile (OpenGL ES) and in the browser (WebGL)
Different platforms provide ‘glue’ code for initializing OpenGL within the desktop
manager (e.g. GLX, WGL)
Labs and projects for CS123 use Qt library to abstract this glue code away
OpenGL (3/3)
Now supports programmable hardware – the common industry practice today
Modern graphics cards are miniature, highly parallel computers with many-core GPUs, on-board
RAM, etc.
GPUs are a large collection of highly parallel high speed arithmetic units; several thousand cores!
GPUs run simple programs called “shaders”: take in vertices and other data and output a color
value for an individual pixel.
GLSL, (O)GL Shader Language, is C-like language, controls arithmetic pipelines
Other shader languages: (DirectX) High-Level Shader Language, RenderMan Shading Language for offline
rendering
Your final project (typically a team project) will involve writing your choice of shaders (learned in
labs)
Only true for desktop; must use shaders exclusively to program with OpenGL ES 2.0+ or WebGL
We will use GLM (OpenGL Mathematics) to do our linear algebra instead of using the old
Fixed-function API
Shaders
In future labs and your final project you will write your own shaders, but for now we
will provide shaders for you
Various types of input to shaders
Attributes are the properties of a single vertex
Position, normal vector are examples of these
Uniforms are properties with a single value for multiple vertices (or an entire object)
Scale factor, rotation are examples of these
OpenGL has many built in types including vectors and matrices
Inputs are provided to a particular “location” within the shader
“Location” just an identifier used by shader to determine where value is stored
glGetAttribLocation returns location of particular attribute
glGetUniformLocation returns location of particular uniform
Labs will cover details on how to use these functions
Representing Shapes
3D shapes are usually represented as a collection of vertices that make
up triangles or quads
OpenGL uses triangles
Other methods include 3D voxels, polynomial spline curves and surfaces, etc.
We can use triangles to build arbitrary polygons, and approximate
smooth shapes.
Application
Display
Coordinates
OGL Coordinates
Andries van Dam© 2D Graphics using OpenGL – 9/10/2019 23/34
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
In class Question 2
N✓ NX
Andries van Dam© 2D Graphics using OpenGL – 9/10/2019 25/34
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Transformations (1/3)
Standard object transformations are scaling, rotation, and translation
These transformations can be represented by multiplying matrices
Don’t worry about specifics for now – the math is explained in viewing lectures
Use GLM to do linear algebra necessary for these transformations
Builds (hierarchical) models that constitute “the scene” (aka “the world”)
For now, we will only use the model matrix which is used to position objects in
terms of the OpenGL coordinate system
For examples below assume our model matrix starts as an identity matrix
This means each object’s position will only be changed by the transformations we apply to
it; our coordinate system is the OGL coordinate system
To create this identity matrix in code, initialize as:
glm::mat4 model = glm::mat4(1.0);
Transformations (2/3)
Geometric Transformations in 2D (note the z-coordinate is 0)
Original Translate
model = glm::translate(.1, .1, 0) * model;
Original Rotate
model = glm::rotate(-45, glm::vec3(0, 0, 1)) * model;
Original Scale
Positive angles rotate counter-
clockwise, here about the origin (i.e.,
model = glm::scale(2, 2, 1) * model;
Z-axis as vector)
Andries van Dam© 2D Graphics using OpenGL – 9/10/2019 27/34
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Transformations (3/3)
Transformations can be composed (combined by multiplying
matrices) but are NOT commutative, so proper order is vital
Original Original
Animation (1/3)
Rapidly displaying sequence of images to create an illusion of
movement
Flipbook (https://www.youtube.com/watch?v=8k2h4c7uHWs)
Keyframe animation: specify keyframes, computer interpolates (e.g., ball
bouncing)
Image Credit:
https://alexshawanimation.
Flipbook Keyframe Animation wordpress.com/2014/09/2
4/the-bouncing-ball/
Animation (2/3)
Idea: Move objects incrementally every time we render
Example: Animating the hands of a clock
Given the number of seconds elapsed, how many degrees should we
rotate the seconds hand?
need to convert from seconds to degrees
Idea: Use rotations around the clock as a common conversion factor
Seconds per revolution: 60
Degrees per revolution: 360
Every time we render, we need to recalculate the position of the hands
Animation (3/3)
//Example in code
float secondsElapsed = ...; // Time since last render
float secondHandAngle = 0;
Point pSecondHand;
pSecondHand.y = center.y + (radius * sin(secondHandAngle));
pSecondHand.x = center.x + (radius * cos(secondHandAngle));
drawLine(center, pSecondHand);
secondHandAngle += -1 // Turn clockwise
* secondsElapsed // Δt
* DEGREES_PER_REVOLUTION // Turn 360 degrees ...
/ SECONDS_PER_REVOLUTION; // ... every 60 seconds
github.com/sprintr/opengl-examples/blob/master/OpenGL-Clock-Animated.cpp
Andries van Dam© 2D Graphics using OpenGL – 9/10/2019 31/34
CS123 | INTRODUCTION TO COMPUTER GRAPHICS
Book Sections
Preface, Intro as useful background
Chapter 2 – while written in terms of Microsoft’s WPF, a retained-mode
library, the concepts carry over to OGL. Useful to know about
HTML/XML style syntax, given its prominence, but don’t worry about
the syntactic details
Fragment: a pixel-independent sample within a triangle with all its associated attributes,
e.g., color, depth, texture coordinates, … the fragment shader provides a many-to-one mapping
between fragments and pixels (e.g. for supersampling, see Image Processing 2)