3 OpenGL
3 OpenGL
1
Writing OpenGL Applications: Tutorials
http://www.opengl-tutorial.org
http://alfonse.bitbucket.org/oldtut/
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/
https://en.wikibooks.org/wiki/OpenGL_Programming
http://ogldev.atspace.co.uk/
http://www.swiftless.com/
http://antongerdelan.net/opengl/index.html
http://www.songho.ca/opengl/index.html
http://www.lighthouse3d.com/
http://nehe.gamedev.net/
http://www.g-truc.net/project-0026.html
https://developer.nvidia.com/gameworks-vulkan-and-opengl-samples
The Programmer’s Interface
3
OpenGL
What is OpenGL?
Alternative API’s
Direct3D:
◼ Proprietary, Microsoft-only software API for graphics
◼ Very well-supported by graphics card vendors
Metal
◼ Graphics API developed by Apple
◼ https://en.wikipedia.org/wiki/Metal_(API)
5
OpenGL
⚫ www.opengl.org
⚫ Wikipedia OpenGL
– http://en.wikipedia.org/wiki/OpenGL
⚫ http://www.khronos.org/opengl
OpenGL Evolution
• OpenGL 4.6
OpenGL Evolution
OpenGL Evolution
OpenGL ES and WebGL
▪ OpenGL ES
▪ Designed for embedded and hand-held devices such as cell phones
▪ Based on OpenGL 3.1
▪ Shader based
▪ WebGL
▪ JavaScript implementation of OpenGL ES
▪ Runs on most recent browsers
▪ http://madebyevan.com/webgl-water/
WebGL
https://threejs.org/!
One unified API framework for desktop, mobile, console, and embedded
No “Vulkan ES” or “Vulkan Desktop”
GLFW (http://www.glfw.org/)
free, Open Source, multi-platform library for OpenGL, OpenGL ES and
Vulkan development on the desktop
provides a simple API for creating windows, contexts and surfaces,
receiving input and events.
written in C, native support for Windows, OS X and many Unix-like systems
such as Linux and FreeBSD
Related API’s
GLU (OpenGL Utility Library)
Part of OpenGL
20
Example GLUT Window
Better GLUT Alternatives: freeGlut
freeGlut (freeglut.sourceforge.net) ***
http://seevisionc.blogspot.ca/2013/08/setting-up-freeglut-and-
gltools-with.html
GLFW (http://www.glfw.org/)
WGL
set of windows API’s used to set up OpenGL
www.opengl.org/resources/libraries/windowtoolkits/
GLU – GL Utility Library
23
OpenGL Extension Wrangler Library (GLEW)
OpenGL extensions:
Card manufacturers may implement new proprietary features after
latest OpenGL version released
These features are published, made available as extensions to latest
OpenGL
GLEW: library to access OpenGL extensions on a
graphics card
What’s in OpenGL?
Light Source(s)
Materials
Other information
Input from devices such as mouse and keyboard
Capabilities of system
25
Aside: Scene Description
26
Object (Model) Specification
27
Camera Specification
28
Lights and Materials Specification
Types of lights
Point light sources vs distributed light sources
Spot lights
Near and far light sources (e.g. sun)
Color of light
30
OpenGL State
State changing
◼ Transformation functions (modeling, viewing)
◼ Attribute functions (set color etc.)
31
OpenGL: Conventions
Functions in OpenGL start with gl
Most functions just gl (e.g., glColor())
Functions
starting with glu are utility functions (e.g.,
gluLookAt())
Functions
starting with glx are for interfacing with
the X Windows system
32
OpenGL: Conventions
Examples
– glColor3f() takes 3 floats
– glColor4fv() takes an array of 4 floats
33
OpenGL: Conventions
34
Lack of Object Orientation
35
OpenGL function format
function name
dimensions
glVertex3f(x,y,z)
glVertex3fv(p)
p is a pointer to an array
36
OpenGL Geometric Primitives
Points
Linesegments
Polygons
Bitmaps/Images
37
Primitive Types
GL_POINTS
GL_LINE
{S | _STRIP | _LOOP}
GL_TRIANGLE
{S | _STRIP | _FAN}
GL_QUAD
{S | _STRIP}
GL_POLYGON
38
Polygon types
Polygon types
GL_POLYGON
41
Polygon Issues
43
Vertex Attributes
Texture coordinates
44
Other Attributes
Polygon mode
◼ Display as filled: solid color or stipple pattern
◼ Display edges
◼ Display vertices
45
Attributes for (a) lines and (b) polygons
Filled objects
Methods of displaying a polygon
PreBuilt OpenGL 3D Shape Primitives
Parametric:
Cylinder
Sphere
Cone
Disk
Torus
glutWireCube(size);
glutSolidCube(size);
glutWireSphere (r, nLongitudes, nLatitudes);
glutSolidSphere (r, nLongitudes, nLatitudes);
glutWireCone(rBase, height, nLong, nLat);
glutSolidCone(rBase, height, nLong, nLat);
glutWireTorus(rCrossSection, rAxial, nConcentric, nRadial);
glutWireTorus(rCrossSection, rAxial, nConcentric, nRadial);
glutWireTeapot(size);
glutSolidTeapot(size);
Stroke text (Post-Script font)
Raster text
Stroke-text Attributes
Color
RGB color
55
Smooth Color
56
Drawing in OpenGL
1 - Immediate Mode Rendering (Old Style)
type of object
location of vertex
glBegin(GL_POLYGON)
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 1.0);
glEnd( );
59
2 - Drawing Mesh using Vertex Arrays**
glDrawArrays( … )
◼ Draws a sequence of vertices
glDrawElements( … )
◼ Draws a sequence of vertices based on an indexed array.
◼ Generally you want to use this one.
2 - Drawing Mesh using Vertex Arrays and Index Buffer
3
Also specifies rendering
order
0
1
2 - Drawing using Vertex Arrays: Example
void display_square()
{
unsigned int indices[] = { 0, 1, 2 };
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices );
}
3 - Drawing Mesh using VBOs (GPU based)
vec3 points[3];
points[0] = vec3(0.0, 0.0, 0.0);
points[1] = vec3(0.0, 1.0, 0.0);
points[2] = vec3(0.0, 0.0, 1.0);
Texture coordinates
74
Reminder: The Camera Analogy (3D)
75
Modelling Transformation
A:
◼ Hard code the coordinates of each vertex (i.e. set their x,y,z coordinates
to where you want them)
B:
◼ Construct a model in its own local (“Model-based”) coordinate system,
◼ make a copy of it (an instance),
◼ then translate/scale/rotate into scene (using glTranslate(),glScale(),
glRotate() etc.) into the world coordinate system
76
Modelling Complex Multi-part Objects
⚫ We can also compose a more complex object (e.g. a nail) by
putting its parts together with transformations.
Translate , T1
World coord
Primitives
Rotate& translate , T3R
Composition
77
OpenGL Matrix Modes
T2Sp
Suppose we want to model some
objects (e.g. red triangles)
Sp T1Sp
p
We start with simple primitive (blue triangle), defined in its own local (i.e.
model) coordinate system, then copy it, scale it and move it to desired location
in the scene (i.e. world coordinate system) (modelling transformation)
An image of these objects is created by rendering process. Part of this process
is the viewing and projection operations.
In OpenGL, modeling + viewing + projection transformations are each
implemented with a transformation matrix (Next week’s lecture).
78
Transformation via a Matrix
glTranslate(), glRotate(), glScale() and other geometric
transformations are implemented using matrices
OpenGL Matrix Modes
A sequence of transformations (modeling+viewing+projection) can be
multiplied together (concatenated) to form a single matrix.
This matrix (the current transformation matrix – CTM) forms a part of the
state of OpenGL
There are two matrix modes in OpenGL which are used to setup two
matrices:
MODELVIEW matrix – Modeling Matrix * Viewing Matrix
PROJECTION matrix
glMatrixMode() is used to set the mode:
glMatrixMode(GL_PROJECTION)
glMatrixMode(GL_MODELVIEW)
Default setting is MODELVIEW matrix
80
Modelling Transformation Example
void display_object()
{
…
// Some transformations
glTranslatef( 0.0, 0.0, -2.0 );
glRotatef( 45.0, 0.0, 0.0, 1.0 );
glScalef( 2.0, 2.0, 2.0 );
draw_object();
}
Code for draw object() function would use immediate mode or VAO or VBO to draw object
OpenGL Camera: Viewing Transformation
82
3D viewing: use gluLookAt(...) to position the camera
yw
xw
zw
Projection Transformation
Projection matrix defines the type of projection (orthographic or
perspective) and the view volume . Think of it as setting the type of
lens on the camera (i.e. whether you want to zoom in or out)
Projection Plane
Perspective View Volume
Projection Plane
2D Viewing/Projection Transformation
If the application is 2D, we can use the function gluOrtho2D(left, right, bottom, top)
(NOTE: not glOrtho() but gluOrtho2D())
gluOrtho2D() calls gluLookAt() and sets the camera on the z axis looking toward the
origin
gluOrtho2D() also calls glOrtho() and creates a very thin orthographic view volume so
all 2D objects essentially have their vertex z values = 0
You can use 2D vertex commands place all vertices in the plane z=0 (e.g.
glVertex2f(x,y) )
The view volume (also called the clipping volume) essentially becomes a clipping
rectangle (also called a clip window or view window)
89
• “2D” orthographic view: gluOrtho2D() calls gluLookAt() and
glOrtho()
Basic OpenGL Program
Basic OpenGL Program Sequence
1. Use glutCreateWindow() to create window on screen
2. You decide on your world coordinate ranges (store in global variables)
(xmin, xmax, ymin, ymax, zmin, zmax)
you never actually specify to openGL what your world range is!!!
3. Use gluLookAt() to position the camera in the world and where it is aimed
4. Use gluOrtho()/gluPerspective() to define what part of this world to look at
i.e. zoom in or out
5. Use glViewport() to define part of screen window you will use
6. Create your objects (models) and move them (translate, rotate, scale) into
the world
7. Draw the scene (i.e. call glFlush() or glutSwapBuffers())
glutswapBuffers() for moving objects – uses double buffering
Viewports (Screen Mapping)
93
Basic OpenGL Program Structure
94
main.c
96
GLUT Windows
Create a top-level window with:
int id = glutCreateWindow(char *name);
glFlush();
} 101
Compiling OpenGL Programs
Compilation on Windows
Visual Studio
Get glut.h, glut32.lib and glut32.dll from web
◼ Use freeglut!!
Create a win32 console application
Click next then check Empty Project
Add opengl32.lib, glu32.lib, glut32.lib to project settings (under link
tab – see posted instructions on D2L
104