CG Chapter 3 2024
CG Chapter 3 2024
Applicaton
17
OpenGL Libraries
• OpenGL core library
• OpenGL32 on Windows
• GL on most unix/linux systems
• OpenGL Utility Library (GLU)
• Provides functionality in OpenGL core but avoids having
to rewrite code
• It uses only GL functions to create common objects.
• It is available in all OpenGL implementations.
• OpenGL Utility Toolkit (GLUT)
• It provides the minimum functionalities expected for
interacting with modern windowing systems.
• Links with window system
• GLX for X window systems
• WGL for Windows
• AGL for Macintosh
• OpenGL utility library (GLU)
– It consists of a number of functions that use the base
OpenGL library to provide higher-level drawing routines
from the more primitive routines that OpenGL provides.
– It is usually distributed with the base OpenGL package
– Functions in GLU are basically built up by routines from
GL
• Features of GLU includes:
• Setting up viewing and projection
• Describing complex objects with line and polygon
approximation
• Display quadrics and B-splines using approximation
• It also provides additional primitives for use in
OpenGL applications, including spheres, cylinders
and disks
• Generally in more human-friendly terms than the
routines presented by OpenGL.
• OpenGL Utility Toolkit (GLUT):
• A library of utilities for OpenGL programs, which primarily perform system-
level I/O with the host operating system.
• Functions performed include window definition, window control, and
monitoring of keyboard and mouse input: including setting up windows,
window size, position, keyboard, mouse, etc.
• Functions with prefix: glut, see glut reference
• Provides functionality common to all window systems
• Open a window, Initialize OpenGL State
• Get input from mouse and keyboard
• Menus and Event-driven
• Code is portable but GLUT lacks the functionality of a good toolkit for a
specific platform
• Features:
• Routines for drawing a number of geometric primitives (both in solid and
wireframe mode), including cubes, spheres, and the teapot
• GLUT has some limited support for creating pop-up menus
• Note that there special extension for each platform
• GLX for X window system
• Apple GL (AGL)
• WGL (Windows-to-OpenGL)
• GLUI is a GLUT-based C++ user interface library
(was written by Paul Rademacher)
• It provides controls such as buttons, checkboxes, radio
buttons, and spinners to OpenGL applications
• It is window- and operating system independent, relying
on GLUT to handle all system-dependent issues, such as
window and mouse management
• GLUI can be installed by updating from Dev-C++
• 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 (e.g., in gfx.c)
OpenGL API
OpenGL API Library Organization
23
OpenGL API
❖Program Framework: Window Management
▪ glutInit():initializes GLUT and should be
called before any other GLUT routine.
▪ glutInitDisplayMode():specifies the
color model (RGB or color-index color model)
▪ glutInitWindowSize(): specifies the size,
in pixels, of your window.
▪ glutInitWindowPosition():specifies the
screen location for the upper-left corner
▪ glutCreateWindow():creates a window
with an OpenGL context.
24
OpenGL API
❖Program Framework: Color Manipulation
▪ glClearColor():establishes what color the
window will be cleared to.
▪ glClear():actually clears the window.
▪ glColor3f():establishes what color to use
for drawing objects.
▪ glFlush():ensures that the drawing
command are actually executed.
Remark: OpenGL is a state machine. You put it into various
states or modes that remain in effect until you change them
25
GLUT functions:
glutInit( ) :
• It allows application to get command line arguments
and initializes system
• The arguments allows application to get command
line arguments (argc and argv) and initializes system
• This procedure must be called before any others.
• It processes (and removes) command-line arguments
that may be of interest to GLUT and the window
system and does general initialization of GLUT and
OpenGL.
glutInitDisplayMode( ):
• This function performs initializations informing
OpenGL how to set up its frame buffer.
• It requests properties of the window (the rendering
context)
• RGB color
• Single buffering
• Properties logically ordered together
• The argument to glutInitDisplayMode() is a logical-OR (using
the operator “|”) of a number of possible options, which are
given in Table
• glutInitWindowSize( ):
• This command specifies the desired width and height of
the graphics window. The general form is:
glutInitWindowSize(int width, int height)
• The values are given in numbers of pixels.
• glutInitPosition( ):
• This command specifies the location of the upper left
corner of the graphics window. The form is
glutInitWindowPosition(int x, int y)
• where the (x, y) coordinates are given relative to the
upper left corner of the display. Thus, the arguments
(0, 0) places the window in the upper left corner of the
display.
• glutCreateWindow( ):
• This command actually creates the graphics
window.
• The general form of the command is
glutCreateWindowchar(*title)
• where title is a character string. Each window
has a title, and the argument is a string which
specifies the window’s title.
• glutDisplayFunc( ) display callback
• glutMainLoop( ) enter infinite event loop
OpenGL Program Structure
Basic program structure of C/C++ with function call to OpenGL
functions includes:
• Header files
• Global variables and data structures for controls and modeling
• Self-defined functions (C/C++, gl, glu, glui, function calls are
involved)
• Main function (glut functions calls are involved)
• Header files:
• Header files include necessary libraries and declarations needed for an
OpenGL program:
• OpenGL Libraries: Include standard OpenGL libraries such as
GL/gl.h or GL/glut.h for basic OpenGL functionality.
• GLU (OpenGL Utility Library): Include GL/glu.h for utility
functions that simplify tasks, such as setting up projections.
• GLUT (OpenGL Utility Toolkit): Include GL/glut.h for window
management and handling user input.
• Additional Libraries: You might include other libraries depending
on your needs, such as #include <math> for mathematical functions
and stdio.h,stdlib.h,etc.
Header files can be used in the OpenGL program as
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glui.h> // if GLUI is installed
glutDisplayFunc(myDisplay);
display callback
myInit(); set OpenGL state
void myInit(){
/* set colors */
glClearColor(1.0, 1.0, 1.0, 0.0);
}/* End of myInit */
void myDisplay(){
/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}/* End of GasketDisplay */
43
Example: init.c
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
viewing volume
44
#include <GL/glut.h>
glutMainLoop();
} enter event loop
• A Simple Program: Generate a square on a solid
background
#include <glut.h>
void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
int main(int argc, char** argv){
glutCreateWindow("simple");
glutDisplayFunc(mydisplay);
glutMainLoop();
OpenGL Functions:
▪ OpenGL Primitives and attributes
▪ Points
▪ Line Segments
▪ Polygons
▪ Transformations
▪ Viewing
▪ Modeling
▪ Control
▪ Input (GLUT)
• OpenGL Primitives:
• the basic building blocks in OpenGL for rendering 2D and 3D shapes.
• The types of primitives includes: points , line segments and polygons.
• Points:
Syntax:
glBegin(GL_POINTS);
glVertex3f(x, y, z);
glEnd();
Example: glBegin(GL_POINTS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glEnd();
• Line Segments:
Syntax:
glBegin(GL_LINES);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glEnd();
Example:glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glEnd();
• Attributes:
• It is any property that determines how a geometric primitive is to be
rendered.
• Each geometric primitive has a set of attributes.
• Point: Color
• Line Segments: Color, Thickness, and Pattern
• Polygon: Pattern
• It controls the appearance and behavior of the primitives, such as
color, lighting, texture, and material properties.
• Syntax :
• glColor3f(r, g, b); sets the current color.
• glNormal3f(nx, ny, nz); sets the current normal vector.
• glTexCoord2f(s, t); sets the current texture coordinates.
• Example:
• glColor3f(1.0f, 0.0f, 0.0f); // set color to red
• glBegin(GL_TRIANGLES);
• glVertex2f(-10.0f, -10.0f);
• glVertex2f(10.0f, -10.0f);
• glVertex2f(0.0f, 10.0f);
• glEnd();
OpenGL Transformations:
• The process of manipulating the position, orientation, and scale of
the objects in the 3D world.
• It includes viewing and modeling transformation
• Viewing Transformations:
• used for setting up the camera and view-related
transformations in the 3D world.
• Syntax:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ,
upX, upY, upZ); sets the camera position and orientation.
gluLookAt(0.0f, 0.0f, 5.0f, // Eye position
0.0f, 0.0f, 0.0f, // Look at point
0.0f, 1.0f, 0.0f); // Up vector
• Modeling Transformations :
• applying transformations (translation, rotation, scaling) to the
objects in the scene.
• manipulating the position, orientation, and scale of the objects
in the 3D world.
• Syntax:
• glMatrixMode(GL_MODELVIEW);
• glLoadIdentity();
• glTranslatef(x, y, z); // applies a translation transformation
• glRotatef(angle, x, y, z); // applies a rotation transformation
• glScalef(x, y, z); // applies a scaling transformation
Example: glTranslatef(1.0f, 0.0f, 0.0f); // Move right by 1 unit
glRotatef(45.0f, 0.0f, 1.0f, 0.0f); // Rotate 45 degrees
around y-axis
OpenGL Control functions :
• OpenGL provides various control functions to manage the
rendering process, such as clearing the color and depth buffers,
enabling and disabling features, and more.
• Syntax:
• glEnable(GL_DEPTH_TEST); enables depth testing for hidden
surface removal.
• glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT); clears the color and depth buffers.
• glDisable(GL_CULL_FACE); // disable face culling
• glFlush(); forces all issued commands to be executed as soon as
possible.
OpenG LInput (GLUT):
• It provides a simple way to handle user input and create
OpenGL windows.
• Syntax:
• glutInit(&argc, argv); initializes GLUT.
• glutCreateWindow("Window Title"); creates a window.
• glutDisplayFunc(display_callback); sets the display callback
function.
• glutMainLoop(); enters the main event processing loop.
3.2. Coordinate Systems
• Coordinate systems are a fundamental concept in computer
graphics and are used to define the position and orientation of
objects in 3D space.
• In OpenGL, coordinate systems are used to specify the
position, orientation, and scale of objects, lights, and cameras.
• Understanding coordinate systems is essential for creating 3D
graphics with OpenGL.
• To define positions of points in space one requires a
coordinate system. It is way of determining the position of a
point by defining a set of numbers called as coordinates.
• The most common coordinate systems used in OpenGL are:
• Model Coordinate System
• World Coordinate System
• View Coordinate System
• Clip Coordinate System
• Screen Coordinate System
• Model Coordinates:
• These are the coordinates defined by the application to
describe the geometry of an object.
• It is also known as Object Coordinates or local Coordinates
• The origin and orientation of object coordinates are defined
by the application.
• There are no built-in OpenGL functions to manipulate object
coordinates.
• World Coordinates:
• the coordinates used to define the position and orientation of
objects in the scene.
• It is also known as the user’s coordinate system
• A world-coordinate area selected for display is called a
window (defines what is to be viewed).
• It can be transformed into camera coordinates using the
modelview matrix.
• The function glLoadIdentity() sets the modelview matrix to
the identity matrix, and glMultMatrixd() multiplies the
current matrix by a given matrix.
• View Coordinate /Camera Coordinates:
• the coordinates used to define the position and orientation
of the camera.
• It can be transformed into screen coordinates using the
projection matrix.
• The function gluPerspective() sets up a perspective
projection matrix, and glFrustum() sets up a frustum
projection matrix.
• Clip Coordinate System:
• Defines the coordinate after the camera's viewing frustum
is applied.
• The viewing frustum is the 3D volume that defines what is
visible to the camera.
• Objects outside the viewing frustum are clipped and not
rendered.
• Coordinates are transformed from the view coordinate
system to the clip coordinate system.
• Screen Coordinates:
• Defines the 2D coordinate system of the final rendered image
on the screen.
• It can be transformed from the clip coordinate system to the
screen coordinate system for rendering.
• An area on a display device to which a window is mapped is
called a viewport (defines where it is to be displayed).
• Often, windows and viewports are rectangles in standard
position, with the rectangle edges parallel to the coordinate
axes.
• The actual coordinate system on the output device.
• The graphics system is responsible to map the user’s coordinate
to the screen coordinate.
• It ranges from -1 to 1 in both the x and y directions.
• The function glViewport() sets up the screen coordinates.
• Homogeneous Coordinates:
• OpenGL uses homogeneous coordinates for transformations,
which allows for easy representation of translation with matrix
multiplication.
• A 3D point (𝑥,𝑦,𝑧) is represented as (𝑥,𝑦,𝑧,𝑤) where 𝑤 typically
equals 1.
OpenGL Functions for Coordinate System Management
• glTranslatef(): Applies a translation transformation.
• glRotatef(): Applies a rotation transformation.
• glScalef(): Applies a scaling transformation.
• glLoadIdentity(): Resets the current matrix to the
identity matrix.
• glMatrixMode(): Selects which matrix stack to
manipulate (model, view, projection).
• gluPerspective(): Defines a perspective projection
matrix.
• glViewport(): Sets the viewport dimensions.
• Example:
• glMatrixMode(GL_MODELVIEW);
• glLoadIdentity();
• glTranslated(0, 0, -10); //move the camera 10 units away from the
origin
• glMatrixMode(GL_PROJECTION);
• glLoadIdentity();
• glFrustum(-1, 1, -1, 1, 1, 100); // set up a frustum projection
matrix
• glViewport(0, 0, 500, 500); // set up the screen coordinates
• // Set camera position and orientation
gluLookAt(0.0, 0.0, 5.0, // Camera position
0.0, 0.0, 0.0, // Look-at point
0.0, 1.0, 0.0); // Up vector
• glTranslatef(1.0f, 0.0f, 0.0f); moves the object 1 unit along the x-axis.
• glRotatef(90.0f, 0.0f, 1.0f, 0.0f); rotates the object 90 degrees around
the y-axis.
• glScalef(2.0f, 1.0f, 1.0f); doubles the object's size along the x-axis
3.3. Viewing Using a Synthetic Camera
• In OpenGL, we can use a synthetic camera to control the
viewing of a scene.
• A synthetic camera is a virtual camera that allows us to specify
the position, orientation, and other parameters of a camera in a
3D scene.
• The Viewing Pipeline:
• It is the process of transforming 3D objects in a scene into 2D
images on the screen.
• The pipeline consists of several stages:
• Modeling: Defining the 3D objects in the scene.
• Viewing: Specifying the camera's position, orientation, and
other parameters.
• Projection: Transforming the 3D objects into 2D images
using a projection matrix.
• Clipping: Discarding objects outside the view volume.
• Rendering: Drawing the 2D images on the screen.
Viewing: Camera Analogy
64
• By changing the position of the window, we can view
objects at different positions on the display area of an
output device.
• By varying the size of viewports, we can change the
size and proportions of the displayed objects. We
achieve zooming effects by successively mapping
different-sized windows on a fixed-size viewport.
• As the windows are made smaller, we zoom in on
some part of a scene to view details that are not
shown with larger windows.
• Similarly, more overview is obtained by zooming out
from a section of a scene with successively larger
windows. 65
• Viewports are typically defined within the unit square
(normalized coordinates).
• This provides a means for separating the viewing and
other transformations from specific output-device
requirements, so that the graphics package is largely
device-independent.
• Once the scene has been transferred to normalized
coordinates, the unit square is simply mapped to the
display area for the particular output device in use at
that time.
• Different output devices can be used by providing the
appreciate device derivers.
66
• In OpenGL, the camera is modeled as a synthetic camera with the
following parameters:
• Eye position (ex, ey, ez): The location of the camera in 3D space.
• Look-at point (lx, ly, lz): The point the camera is looking at.
• Up vector (ux, uy, uz): The direction of the camera's up vector.
• These parameters define the camera's orientation and position in 3D
space.
• OpenGL Functions for Viewing:
• glMatrixMode(GL_PROJECTION): Specifies the projection matrix.
• glMatrixMode(GL_MODELVIEW): Specifies the modelview matrix.
• glLoadIdentity(): Loads the identity matrix into the current matrix.
• gluLookAt(): Sets up a viewing matrix based on the camera's
parameters.
• gluPerspective(): Sets up a perspective projection matrix.
• Example Code: to set up a synthetic camera using OpenGL
• glMatrixMode(GL_PROJECTION);
• glLoadIdentity();
• gluPerspective(45, aspectRatio, 1, 100);
• glMatrixMode(GL_MODELVIEW);
• glLoadIdentity();
• gluLookAt(ex, ey, ez, lx, ly, lz, ux, uy, uz);
• In OpenGL, the viewer or "camera" is simulated using a
transformation matrix called the Modelview matrix.
• This matrix defines the position and orientation of the camera in the
WCS.
• In OpenGL, the viewing transformation is used to simulate a camera's
viewpoint.
• The camera's position, orientation, and field of view are specified
using the following functions:
• glLoadIdentity( ); Resets the Modelview matrix to the identity matrix.
• glTranslatef(x, y, z); Translates the Modelview matrix by (x, y, z).
• glRotatef(angle, x, y, z); Rotates the Modelview matrix by angle degrees
around the vector (x, y, z).
• glMultMatrixf(matrix); Multiplies the current Modelview matrix by the
4x4 matrix specified by matrix.
• gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY,
upZ); gluLookAt( ) is commonly used to set up the camera.
• Example: gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
• This sets up a viewing transformation simulating a camera located at (0,
0, 10) looking towards the origin (0, 0, 0) with the up direction (0, 1, 0).
• In OpenGL, we can define a synthetic camera to control
the viewpoint and orientation of the scene.
• The function gluLookAt() is commonly used to set up
the camera.
• It takes three arguments for each of the eye, center, and
up vectors.
• The eye vector specifies the position of the camera, the
center vector specifies the point the camera is looking
at, and the up vector specifies the orientation of the
camera.
• Example:
• glMatrixMode(GL_MODELVIEW);
• glLoadIdentity();
• gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
• set up the camera at (0, 0, 10), looking at (0, 0, 0), with up
vector (0, 1, 0)
• This camera has several properties that determine what is visible
in the scene:
• Position: The camera's location in world coordinates.
• Orientation: The camera's direction of view and its "up" direction.
• Projection: The way the 3D world is projected onto the 2D screen.
• Functions:
• gluLookAt(): Sets the camera position, orientation, and look-at point.
• gluPerspective(): Creates a perspective projection, simulating a real
camera lens.
• glOrtho(): Creates an orthographic projection, where objects are projected
without perspective.
• Examples:
• Setting the camera: gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0); places the camera at (0, 0, 5) and points it towards the
origin.
• Perspective projection: gluPerspective(45.0f, 1.0f, 0.1f, 100.0f);
creates a perspective projection with a 45-degree field of view.
• Orthographic projection: glOrtho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f,
100.0f); creates an orthographic projection with a view volume of
10x10 units.
Two- and three-dimensional viewing
• In glOrtho(left, right, bottom, top, near, far) the near and far
distances are measured from the camera
• Two-dimensional vertex commands place all vertices in the plane z=0
• If the application is in 2D or 3D, we can use the function
gluOrtho2D(left, right,bottom,top)
gluOrtho3D(left, right,bottom,top)
Example:
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glBegin(GL_LINE_STRIP)
glVertex3f(0.0f, 0.0f, 0.0f); // v0
glVertex3f(50.0f, 50.0f, 0.0f); // v1
glVertex3f(50.0f, 100.0f, 0.0f); // v2
glEnd(); 71
3.4. Output Primitives and Attributes in OpenGL
• Output primitives are the basic shapes rendered in OpenGL
geometry.
• OpenGL supports several types of output primitives, such as
points, lines, and triangles.
• These primitives can be specified using various functions and
attributes.
• These primitives are :
• Points: The simplest primitive, represented by a single
vertex.
• Lines: A sequence of connected vertices, forming a straight
line.
• Triangles: A three-vertex polygon, the most common
primitive for 3D models.
• Polygons: A closed shape defined by multiple vertices.
• Circles, character strings and so on.
• Functions and its syntax:
• glBegin(mode); Begins the specification of a set of vertices that
define a primitive of the specified mode.
• glVertex*(): Specifies the coordinates of a vertex.
• glVertex3f(x, y, z); Specifies a 3D coordinate (x, y, z) for the
current vertex.
• glEnd(); Ends the specification of a set of vertices for a primitive.
Examples: Drawing a triangle: draws a triangle with vertices at (0,
0, 0), (1, 0, 0), and (0, 1, 0).
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
Setting color: glColor3f(1.0f, 0.0f, 0.0f); sets the color of the next
primitive to red.
• Applying texture: glTexCoord2f(0.0f, 0.0f); sets the texture
coordinates for the current vertex to (0, 0).
• Attributes:
• It defines the features and appearances of primitives
• These includes:.
• Color: specified using the function glColor3f(red, green, blue).
• Normal: specified using the function glNormal3f(x, y, z).
• Texture Coordinate: specified using the function glTexCoord2f(s, t).
• Syntax: glColor3f(red, green, blue)
glNormal3f(x, y, z);
glTexCoord2f(s, t);
• Example:
• glBegin(GL_TRIANGLES);
• glColor3f(1, 0, 0); // set the color to red
• glVertex3f(0, 0, 0); // define the first vertex of the triangle
• glColor3f(0, 1, 0); // set the color to green
• glVertex3f(1, 0, 0); // define the second vertex of the triangle
• glColor3f(0, 0, 1); // set the color to blue
• glVertex3f(0, 1, 0); // define the third vertex of the triangle
• glEnd();
• This specifies a triangle with vertices at (1, 0, 0), (0, 1, 0), and (0, 0, 1) with
the color red, green, and blue respectively, normal vector (0, 0, 1) and
texture coordinates (0, 0), (0, 1) and (1, 1) respectively.
THE END!
QUESTIONS
?????
75