05_Intro to OpenGL
05_Intro to OpenGL
to OpenGL
6.53
Computer Graphics
CPIT285
PREFACE
o In computer games we do not design fancy objects for the user to watch, they
need to interact.
o The games need to receive user inputs, analyze them and make all changes
according to the orders given...
o This process involves math calculation and physics simulation…
o In order for the game to be seen, there must be images representing the objects
to be executed.
o These images need to be created and have a very quick appearance based on
the game's data (user inputs) to show the game as a video clip or animated
objects rather than a series of images displayed in a sequence.
o An experienced user may code algorithms to perform all physical logic, math
calculations, manage the memory…
o however, how the drawing process can be handled?
o Early ages of desktop computers, drawing was very simple and the CPU managed
the screen contents.
o E.g. drawing a line segment → CPU would run a loop to set the
color of each pixel that lies along the line.
o Drawing process could take up a lot of the CPU’s time.
o Graphics performance was very slow, compared to what
we expect today.
o Computers are much faster in general, of course, but the big change is that in modern
computers, graphics processing is done by a specialized component called a GPU, or
Graphics Processing Unit.
o To draw graphical operation:
▪ CPU simply has to send commands, along with any necessary data, to the GPU,
▪ GPU is responsible for actually carrying out those commands.
▪ The set of commands that the GPU understands make up the API of the GPU.
o OpenGL is an example of a graphics API, and most GPUs support OpenGL in the sense
that they can understand OpenGL commands, or at least that OpenGL commands can
efficiently be translated into commands that the GPU can understand.
o Back to the game, giving the CPU the role of drawings processes takes a long
time because it has several tasks to manage at the same time in order to run the
entire computer, thus the games will be very slow!
o To overcome this problem, Open Graphical Library (OpenGL) facilitates and
accelerates these processes by direct access to the computer Graphics Card
o The current version, in early 2018, is 4.6, and it is very different from the 1.0
version from 1992.
o In this course, we will begin our study of OpenGL with version 1.1. Most of the
concepts and many of the details from that version are still relevant and
functioning, and it offers an easier entry point for someone new to 3D graphics
programming.
▪ Furthermore, there is a specialized version called for “embedded systems”
such as mobile phones and tablets.
▪ And there is also , for use in Web browsers.
4.6
2018
2016
2017
6 March 2025 CPIT285 8
OpenGL
Getting Started…
o To start taking advantage of the OpenGL API for game development or software
uses OpenGL, you need to install the appropriate graphics card driver which
enables usage of the functionality provided.
o To program using the OpenGL API, you need the driver and the development
package (depends on platform and programming language).
o In all three major desktop platforms (Linux, macOS, and Windows), OpenGL
more or less comes with the system. However, you will need to ensure that you
have downloaded and installed a recent driver for your graphics hardware.
GLUI (Graphics Library User Interface) based on GLUT to create objects on the window
such as a button, a text box, and so on.
GLEW / GLX / AUX .......
folders of the
OS System
programming
folder
language
o Functions starts with a prefix that expresses the library from which it’s taken:
▪ e.g. the function glBindVertexArray is taken from the GL library.
o When a function consists of more than one word, each word begins with a
capital letter:
▪ glClearColor
▪ glUseProgram
o Constants start with the “GL”, followed by underscore ‘_’ to split words, and all
the letters in upper cases:
▪ GL_COLOR_BUFFER_BIT
▪ GL_TRIANGLES
14
OpenGL
Primitives
6.53
OpenGL Primitives
point. Edges
gl_Position =
GL_LINE_LOOP same as above, with a segment added between last and first vertices
▪ What if I provide a position value that exceeds the default values? (0,0,0)
GL.glPointSize(2) 0.5
GL.glDrawArrays(GL_POINTS, 0, 1)
o Line attributes: y
-1 (0,0) +1 x
-1
▪ GL_QUAD_STRIP
- The latest 2 vertices for a quad represent the first 2 vertices for the followed quad…
o The following primitives have been removed from modern OpenGL (3.1 and above):
o In order for a quad to be rendered correctly in OpenGL, all vertices of the quad must lie
in the same plane. The same is true for polygon primitives.
o Since OpenGL doesn't check whether these conditions are satisfied, the use of quads
and polygons is error-prone.
▪ GL_TRIANGLE_STRIP
- Every group of 3 adjacent vertices forms a triangle.
- The face direction of the strip is determined by the winding of the first triangle.
- A vertex stream of n length will generate n-2 triangles.
▪ GL_TRIANGLE_FAN
- The first vertex is held fixed.
- From there on, every group of 2 adjacent vertices forms a triangle with the first.
- A vertex stream of n length will generate n-2 triangles.
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease();
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
27
PROGRAM STRUCTURE
#include <whateverYouNeed.h> The structure starts with
header files. Any function
main() { used in the code needs to
InitializeAWindowPlease(); include its library header
glClearColor (0.0, 0.0, 0.0, 0.0); file.
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0); In this example, we used
functions from GL, thus we
glBegin(GL_POLYGON); need to include:
glVertex3f (0.25, 0.25, 0.0); #include <GL/gl.h>
glVertex3f (0.75, 0.25, 0.0); Also functions such as
glVertex3f (0.75, 0.75, 0.0); “InitializeAWindowPlease()”
glVertex3f (0.25, 0.75, 0.0); from GLUT, Therefore GLUT
glEnd(); header file is needed:
glFlush(); #include <GL/glut.h>
}
28
PROGRAM STRUCTURE
#include <whateverYouNeed.h>
main() {
In this example, the
InitializeAWindowPlease();
functions used in the main
glClearColor (0.0, 0.0, 0.0, 0.0);
routine. This is not a
glClear (GL_COLOR_BUFFER_BIT);
professional way to code in
glColor3f (1.0, 1.0, 1.0);
OpenGL. Later, it will be
learned how to use call
glBegin(GL_POLYGON);
back functions.
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
29
PROGRAM STRUCTURE
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease(); Defining background color.
glClearColor (0.0, 0.0, 0.0, 0.0); We have four parameters
glClear (GL_COLOR_BUFFER_BIT); here
glColor3f (1.0, 1.0, 1.0); (red, green, blue, alpha),
the color is black.
glBegin(GL_POLYGON); The Alpha parameter works
glVertex3f (0.25, 0.25, 0.0); like transparency. It takes
glVertex3f (0.75, 0.25, 0.0); values 0.0 - 1.0
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
30
PROGRAM STRUCTURE
#include <whateverYouNeed.h>
Perform the color clearing,
main() { and whenever this function
InitializeAWindowPlease(); is called, the window is
glClearColor (0.0, 0.0, 0.0, 0.0); cleared with the specified
glClear (GL_COLOR_BUFFER_BIT); color.
glColor3f (1.0, 1.0, 1.0);
Defining the color for the
glBegin(GL_POLYGON); object/ rectangle = white
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
31
PROGRAM STRUCTURE
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease();
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
In between glEnd() /
glColor3f (1.0, 1.0, 1.0);
glBegin() functions of
drawing a rectangle is used
glBegin(GL_POLYGON);
specifying 4 vertices
glVertex3f (0.25, 0.25, 0.0);
The constant
glVertex3f (0.75, 0.25, 0.0);
“GL_POLYGON” is used for
glVertex3f (0.75, 0.75, 0.0);
this purpose.
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
32
PROGRAM STRUCTURE
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease();
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
“glVertex” is a function to
glBegin(GL_POLYGON);
specify a vertex. It takes 3
glVertex3f (0.25, 0.25, 0.0);
parameters which define
glVertex3f (0.75, 0.25, 0.0);
x,y,z coordinates.
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
}
33
PROGRAM STRUCTURE
#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease();
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
All the previous orders are
glVertex3f (0.75, 0.25, 0.0);
placed in a buffer. “glFlush”
glVertex3f (0.75, 0.75, 0.0);
role is to show at once on
glVertex3f (0.25, 0.75, 0.0);
the window.
glEnd();
glFlush();
}
34
HEADER FILES
As the previous example demonstrates, the structure has to begin with declaring
header files.
Any function needs to be used it must declare its library header file:
▪#include <GL/gl.h>
▪#include <GL/glu.h>
▪#include <GL/glut.h> *
There are also header files required for C ++
▪#include <stdio.h>
▪#include <stdlib.h>
▪#include <math.h>
35
CLEAR COLOR FUNCTION
Drawing on a computer screen is different from drawing on paper in that the paper starts
out white.
On a computer, the memory holding the picture is usually filled with the last picture you
drew, so you need to clear it to some background color before you start to draw the new
scene.
glClear (GL_COLOR_BUFFER_BIT);
36
MANAGING DISPLAY-WINDOW
37
MANAGING DISPLAY-WINDOW
The following functions from GLUT will be used repeatedly in our applications:
Function Role
38
MANAGING DISPLAY-WINDOW
glutInitWindowSize (400, 300);
(0,0)
glutCreateWindow (“An
monitor 100 Example”);
coordinates
An Example
50
glutDisplayFunc
(LineSegment);
glutMainLoop ( );
300
Window coordinates
(0,0)
400
glutDisplayFunc(functions to be called)
40
DISPLAY CALLBACK FUNCTION
In the previous example, drawing a white rectangle on a black background
▪ Drawing instructions were put directly in the main() routine
▪ Applications that we will code is not only a simple drawing
▪ Think about complex drawing (e.g. lots of objects in a game), how instructions will be
organized in one block?
That’s why display callback function is used
glutDisplayFunc(functions to be called)
41
#include <glut.h>
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white
When the window is viewed, a user may need to change the window's position or
resize it.
There should be a way to re-format the graphics according to the new position and
sizing of the window.
43
COORDINATE REPRESENTATIONS
Coordinate systems describe the location of points in a space.
The five coordinates' stages:
▪modeling/local/object coordinates
▪world coordinates
▪camera/view coordinates
▪normalized/clip coordinates
▪device/screen coordinates
44
COORDINATE REPRESENTATIONS
(0,0,0
)
45
COORDINATE REPRESENTATIONS
modeling/local/object coordinates
▪the place where the object begins in. The origin of the object is at (0,0,0), the
initial position.
world coordinates
▪moving an object from its initial position to a new position to organize with
other objects.
46
COORDINATE REPRESENTATIONS
camera/view coordinates
▪the result of transforming world-space coordinates to coordinates that are in front of the
user's view.
normalized/clip coordinates
▪specifying a range and any coordinate that falls outside this range is clipped.
47
THANK YOU