OpenGL Basics: Resources & Sample Code
OpenGL Basics: Resources & Sample Code
1 Resources
4.1 Resources “OpenGL Programming Guide: The Official Guide to
4.2 Background Learning OpenGL”, Woo, Neider, and Davis, Addison-
Wesley (aka “The Red Book”).
4.3 GL/GLU/GLUT etc.
1st edition online: [Link]
4.4 A Simple OpenGL Program
What the Program Does OpenGL/GLUT manuals
See COMP 372 Resources page
The Code
Aspects of the Code
4.5 Geometric Primitives in OpenGL
© 2008 Burkhard Wuensche [Link] Slide 1 © 2008 Burkhard Wuensche [Link] Slide 2
© 2008 Burkhard Wuensche [Link] Slide 19 © 2008 Burkhard Wuensche [Link] Slide 20
Initialisation window & view Initialisation window & view (cont’d)
gluOrtho2D(Gldouble left, Gldouble right, Gldouble
bottom, Gldouble top);
glClearColor(Glclampf red, Glclampf green, Glclampf blue, – Defines a 2D orthographic projection matrix.
Glclampf alpha);
– Maps a section of the 2D world coordinates (the coordinates in which the scene
– Set colour used for clearing (glClear()) the drawing window. is defined) onto the drawing window.
– Colour is an RGBA tuple (red, green, blue, alpha) where alpha is transparency. – Ratio (right-left):(top-bottom) should be the same as width:height of the window
– Arguments are floats (values are clamped to the range [0,1]) →otherwise scene is distorted
y
glMatrixMode(Glenum mode); 200
glLoadIdentity(); 150
x
-150
(a) Using ‘gluOrtho2D(-150, 150, -200, 200)’ (b) Using ‘gluOrtho2D(-150, 150, -50, 200)’
© 2008 Burkhard Wuensche [Link] Slide 21 © 2008 Burkhard Wuensche [Link] Slide 22
y v
w.t
⎛ u ⎞ ⎛ A 0 ⎞ ⎛ x ⎞ ⎛ C ⎞ ⎛ Ax + C ⎞
200
v.t
⎜ ⎟=⎜ ⎟⎜ ⎟ + ⎜ ⎟ = ⎜ ⎟
⎝ v ⎠ ⎝ 0 B ⎠ ⎝ y ⎠ ⎝ D ⎠ ⎝ By + D ⎠
150
x
-150
Drawing the Scene (cont’d) Summary An OpenGL program has 3 important parts:
glBegin(Glenum mode); Like all C programs it has a main function,
… which is used to initialise the drawing window
void display(void){
and set up the scene and event handling.
glEnd(); // draw scene objects
} Before drawing a scene the scene and the
– glBegin and glEnd delimit the vertices that define a primitive or a group of like virtual camera (view of the scene) must be
primitives. defined
– The type of primitive is specified by the argument mode. void init(void) { Can be done inside the display function
– GL_LINES treats each pair of vertices as an independent line segment. Vertices 2n-1 // set up background colour, window etc. but more efficient to define a separate
// set up view projection (e.g. glOrtho2D) method init.
and 2n define line n. N/2 lines are drawn. // set up scene Only called once, except if the view is
} changed (e.g. zoom in).
glVertex2fv(const GLfloat *v ) Scene is draw by the display function.
– v specifies a pointer to an array of two float numbers representing a vertex. Window must be redrawn if it is created,
int main(int argc, char** argv){
– the glVertex command is used within glBegin/glEnd pairs to specify point, line, moved or uncovered.
// create a single buffered colour window
and polygon vertices. The current color, normal, and texture coordinates are associated // initialise view and scene Performed automatically by GLUT by
with the vertex when glVertex is called. // set up event handling calling glutDisplayFunc(display).
glutDisplayFunc(display); // draw scene Two parts missing in our example:
glFlush() glutMainLoop(); could define reshape function in case the
return 0; drawing window is resized.
– empties all buffers, causing all issued commands to be executed as quickly as they are } event handling (mouse and keyboard input).
accepted by the actual rendering engine.
© 2008 Burkhard Wuensche [Link] Slide 27 © 2008 Burkhard Wuensche [Link] Slide 28
Remarks Remarks (cont’d)
Note that many methods (e.g. glColor2f, glVertex2fv) are of the form:
glXXX<n><t>[v], where The GL data types (Glfloat, Gldouble, …) are not C types.
For example, GLint is not necessarily equivalent with the C type int.
n is dimension of quantity being defined (3 ⇒ 3-space)
t is type of parameter (f = float) The reason for this is that OpenGL needs for each data type a certain
minimum number of bits in order to get the necessary precision for graphics
optional v denotes “vector” parameter, e.g. glVertex3fv takes a 3-element
operation.
array of floats as a parameter, whereas glVertex3f takes three separate
The corresponding C data types are specified in the file gl.h, e.g.
floats, (x,y,z)
typedef float GLfloat;
OpenGL often defines several different forms of each call, e.g.
glVertex{234}{sifd}[v] typedef float GLclampf;
24 forms in total! typedef double GLdouble;
In 372 we are using the f form (float) everywhere …
Simplest to stick with a single type when learning Hence if you use float instead of Glfloat you won’t get a warning
Although double is often a little more convenient, it costs too much in message. However, it’s a good style to use the GL data types in case you
memory and performance. port you program to another machine.
© 2008 Burkhard Wuensche [Link] Slide 29 © 2008 Burkhard Wuensche [Link] Slide 30
© 2008 Burkhard Wuensche [Link] Slide 31 © 2008 Burkhard Wuensche [Link] Slide 32
Geometric Primitives (cont’d) Geometric Primitives (cont’d)
GL_POINTS GL_LINE_STRIP
treats each vertex as a single point. draws a connected group of line segments from the first vertex to the
vertex n defines point n (n=0,...,N-1). last.
vertices n and n+1 define line n (n=0,...,N-2).
GL_LINES GL_LINE_LOOP
treats each pair of vertices as an independent line segment. as above, but additionally a line segment is drawn from the last vertex to
Vertices 2n and 2n+1 defines line n (n=0,...,N/2-1). the first.
© 2008 Burkhard Wuensche [Link] Slide 33 © 2008 Burkhard Wuensche [Link] Slide 34
© 2008 Burkhard Wuensche [Link] Slide 35 © 2008 Burkhard Wuensche [Link] Slide 36
Geometric Primitives (cont’d) Remarks
GL_QUADS
Geometric primitives are defined identical in 2D and 3D with the dimension of
treats each group of four vertices as an independent quadrilateral.
the vertices being the only difference.
vertices 4n , 4n+1 , 4n+2 and 4n+3 define quadrilateral n (n=0,…,N/4-1).
The vertices of a triangle strip or quad strip should be all either in clockwise or
GL_QUAD_STRIP
in anticlockwise order (necessary for a consistent surface orientation in 3D).
draws a connected group of quadrilaterals with the first four vertices
defining one quadrilateral and each subsequent pair of vertices defining a Point size is modified with glPointSize(Glfloat size).
quadrilateral with the last two vertices of the previous one. Line width is modified with glLineWidth(Glfloat width).
vertices 2n , 2n+1 , 2n+3 and 2n+2 define quadrilateral n (n=0,…,N/2-2). Can apply a colour either to all vertices or to each vertex individually. In the
latter case the vertex colours are interpolated.
© 2008 Burkhard Wuensche [Link] Slide 37 © 2008 Burkhard Wuensche [Link] Slide 38