0% found this document useful (0 votes)
89 views53 pages

Open GL

OpenGL is a software interface for rendering 2D and 3D graphics. It consists of over 150 commands to specify graphics objects and operations. The commands are organized into groups that define primitives, attributes, viewing, transformations, inputs, controls and querying. Color in OpenGL can be specified using RGB or indexed color models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views53 pages

Open GL

OpenGL is a software interface for rendering 2D and 3D graphics. It consists of over 150 commands to specify graphics objects and operations. The commands are organized into groups that define primitives, attributes, viewing, transformations, inputs, controls and querying. Color in OpenGL can be specified using RGB or indexed color models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 53

OpenGL

What is OpenGL
OpenGL is a software interface to graphics hardware.
Graphics rendering API
Rendering?–converting geometric or
mathematical object descriptions into frame
buffer values.
 high-quality color images composed of geometric
and image primitives
 window system independent
 operating system independent
This interface consists of 150 distinct commands they are
used to specify the object and operations needed to produce
interactive 2D & 3D graphics application.
Currently there are 3 major graphics APIs currently
being used right now.

• OpenGL. OpenGL was first released in 1992, by


Silicon Graphics.
• Direct3D. Microsoft DirectX is an advanced suite of
multimedia APIs built into Microsoft Windows® operating
systems. ...
• Metal. Metal provides near-direct access to the graphics
processing unit (GPU), enabling you to maximize ... Get
sample code to see how Metal APIs are implemented.
The OpenGL API
Graphics Functions
 The basic model of a graphics is a black box
 The model is used and described only its input and outputs
 OpenGL structure is similar other modern API

Function output
Application calls Graphics I/O
Programs system Devices
Data input
 OpenGL has seven group of functions

1. Primitive functions
2. Attribute functions
3. Viewing functions
4. Transformation functions
5. Input functions
6. Control functions
7. Query functions
1. Primitive functions- Define the low-level object entities such as
points, line segments ,polygons, pixels, text and various types of
curves and surface.Eg: : glBegin,glVertex
2. Attribute functions- Govern the way that a primitive appears on
the display (color, pattern, filling, typeface, text styles
etc.,)Eg:glColor3f()
3. Viewing functions- API allows us to clip out objects that are too
close or too far away (synthetic camera position and degree of
orientation)
4. Transformation functions- API should provides the user with a set
of transformations functions that allows to carry out
transformation of objects such as rotation, scaling and translation.
5. Input functions- An API must provide input functions to deal with
different input devices (key boards, mouse, light pen, tablets etc.,)
6. Control functions- These functions enable us to communicate with
the window system, to initialize our programs, and to deal with
any errors that take place during the execution of our programs.
7. Query functions- A good API must provide information through a
set of query functions to write device-independent programs , to
use various camera parameters and values in the frame buffer.
TRANSFORMATION FUNCTIONS
(The OpenGL Interface)
In OpenGL all graphic functions are stored in three Libraries
1. GL (OpenGL in windows)- The functions in this library have names
that begin with letters gl and are stored in library GL
2. GLU (OpenGL Utility Library)- This library uses GL functions and
contains code for generating objects and simplifying views.
Function GLU library begin with “glu". They are used for 1. Setting
up matrices for viewing transformation 2. Rendering surfaces 3.
performing polygon tessellation.
3. GLUT(OpenGL Utility Toolkit)- Used to interface with the window
system and to get input from external devices.
 GLX, Xlib and Xtk are used by x-windows
GLU
OpenGL Frame
GL
Application Buffer
Program Xlib, Xtk
GLUT
6

GLX
Abstractions
GLUT
Windowing toolkit (key board, mouse handler, window
events)

GLU
• Viewing –perspective/orthographic
• Image scaling, polygon tessellation
• Sphere, cylinders, quadratic surfaces
GL

• Primitives - points, line, polygons


• Shading and Colour
• Translation, rotation, scaling
• Viewing, Clipping, Texture
• Hidden surface removal
OpenGL Geometric Primitives
 All geometric primitives are specified by vertices

GL_LINES
GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP
GL_POINTS

GL_TRIANGLES

GL_QUADS
GL_QUAD_STRIP
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

8
GL_POINTS each vertex is displayed as one
pixel
GL_LINES  Takes successive pair of
vertices(lines are disconnected)
GL_LINE_STRIP Successive vertices are
connected
GL_LINE_LOOP polyline are closed.
POLYGON
Polygon is an object that has
1.Border that can be describe by line
loop.
2.Defined interiors.
3 properties of a polygon
Simple, Convex, Flat
 If no two edges of a polygon cross each other it’s a
simple polygon.

simple not simple


 An object is convex, a line segment between two
points on the boundary never goes outside the
polygon.

convex non-convex
Data types supported in OpenGL
Suffix Data Type Typical Corresponding C- OpenGL Type
Language Type Definition
b 8-bit integer signed char GLbyte

s 16-bit integer Short GLshort


i 32-bit integer int or long GLint
f 32-bit floating- Float GLfloat
point
d 64-bit floating- Double GLdouble
point
ub 8-bit unsigned unsigned char GLubyte,
integer GLboolean
us 16-bit unsigned unsigned short GLushort
integer
ui 32-bit unsigned unsigned int or unsigned long GLuint,
integer Glbitfield
OpenGL Command Formats

glVertex3fv( v )

Number of Data Type Vector


components b - byte omit “v” for
ub - unsigned byte
2 - (x,y) scalar form
s - short
3 - (x,y,z)
us - unsigned short
4 - (x,y,z,w) glVertex2f( x, y )
i - int
ui - unsigned int
f - float
d - double
BITS WASE Computer Graphics delivered by Dr. K Satyanarayan Reddy May 24, 2023 12
 Color attribute:
 Additive color- Primary colors (RGB)are
added to give the perceived color
 Subtractive color (CMY)- colored pigments
remove the color components from light
that is striking the white surface (printing
and painting).
 Color in Graphics system is handled
through the API.
 There are 2 approaches
1. RGB Color Model
2.Indexed Color Model  easier to support
in hardware because of its lower memory
requirement and limited colors available
on display.
15
 Eg: glColor3f(0.0,0.0,0.0)----black
glColor3f(1.0,1.0,1.0)-----white
glColor3f(1.0,0.0,0.0)------Red
glClearColor(1.0,1.0,1.0,1.0)- (to clear the window before
drawing a new frame)
glClear(GL_COLOR_BUFFER_BIT);
GL_COLOR_BUFFER_BIT Indicates the buffers currently enabled
for color writing.
Four Color system(RGBA ), A-Alpha [Opacity (Opaque-No light
passes through) or Transparency value]
 A=0 .0(Transparent), A=1.0 (Opaque)
 Alpha value will be considered only if Blending is enabled.
By default blending is disabled  overwrites any existing
color.
 Indexed color
 Each pixel has 8-bits
 Divide each pixel’s 8-bit into smaller groups and assign RGB to
each.
 Not flexible with color assignment
 Provides wide range of colors
 Depth pixel are used as index to color lookup table.
 Color is selected from look-up table
 For example : if Frame buffer has K bits/pixel, each pixel value or
index, is an integer between 0—2k -1
 If precession is m bits, can select 2m red, 2m blue, 2m green,
which produces 23m colors on the display, but frame buffer
can specify on 2K colors.
17
Eg: glIndexi(element)- assign present color to element, this
index value is stored in the frame buffer for subsequent
operation.
glIndexi(196);
191966 2081

00000000 00001000 00100001

red gun green gun blue gun


GLUT allows to set the entries in the color table for each
window by using
glutSetColor(int color, Glfloat red, Glfloat green,
Glfloat blue);
 glClearIndex(index) specified index is cleared.

 Advantages

The Color index mode requires less memory for the


frame buffer and hardware components.

 Disadvantages

Interaction with window system is more complex


than RGB color.
 Viewing
 Describes how objects appear on the
display
 Camera forms an image by exposing film,
but computer forms an image by carrying
out a sequence of operations in its
pipeline.
 OpenGL default view is the orthographic
projection (Image plane is fixed and
moving camera far from this plane)
ORTHOGRAPHIC Projection
OpenGL Camera
OpenGL places a camera at the origin in object space
pointing in the negative z direction.
The default viewing volume is a box centred at the
origin with sides of length 2
Void glOrtho(GLdouble left, Gldouble right,
Gldouble bottom, Gldouble top, Gldouble
near, Gldouble far)

•All the parameters are distance measured from the camera.


•Orthographic projection sees only those objects in the volume
specified by the view volume.
•OpenGL uses default view volume 2x2x2 cube with the origin in the
center. (left, bottom, near)=(-1,-1,-1), (right, top, far)=(+1,+1,+1).
gluOrtho2D(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top)

25
Orthographic Viewing
 In the default orthographic view,
points are projected forward
along the z axis onto the
plane z=0
Coordinate Systems
The units in points are determined by the application and
are called
– object (or model) coordinates model view transform
– world coordinates
Viewing specifications usually are also in object coordinates
transformed through
-- eye (or camera) coordinates
--clip coordinates projection transform
--normalized device coordinates
--window (or screen) coordinates
• OpenGL also uses some internal representations that usually are not visible
to the application but are important in the shades
Coordinate Systems and Transformations
 Steps in Forming an Image
 specify geometry (world coordinates)
 specify camera (camera coordinates)
 project (window coordinates)
 map to viewport (screen coordinates)
 Each step uses transformations
 Everytransformation is equivalent to a change
in coordinate systems (frames)
Camera Analogy and Transformations
 Projection transformations
 adjust the lens of the camera
 Viewing transformations
 tripod–define
position and orientation of the viewing
volume in the world
 Modeling transformations
 moving the model
 Viewport transformations
 enlarge or reduce the physical photograph
OpenGL Geometric Transformations
glMatrixMode(GL_MODELVIEW);
Matrix modes
 OpenGL pipeline architecture depend on multiplying or
concatenating a number of transformation matrices to
achieve the desired image or primitive.
 The values of these matrices are part of the state of
the system
 In OpenGL Model-view and Projection are two
important matrices used.
 Initially these matrices are identity matrices.
 The set commands used for two-dimensional viewing
are
32
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);

 This sequence defines 50.0x50.0 viewing


rectangle with the lower left corner of the
rectangle at the origin of the 2-D system
 Itthen switches the matrix mode back to model-
view mode
Control functions
 Interface between the graphics system and operating system (GLUT)
 Interaction with the window system
 Window displays the content of frame buffer
 Position of window are measured in pixels
1. glutInit(int *argc, char **argv) initializes GLUT and processes any command line arguments
glutInit() should be called before any other GLUT routine.
Eg: glutInit(&argc, argv)
2. glutInitDisplayMode(unsigned int mode)
 specifies whether to use an RGBA or color-index color model.
 specify whether we want a single- or double-buffered window.
 we can use this routine to indicate that we want the window to have an associated depth, stencil,
and/or accumulation buffer.
 Eg: If we want a window with double buffering, the RGBA color model, and a depth buffer, we might
call
3. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH).
 Eg: Single buffer with RGB glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB). 34
 glutInitWindowPosition(int x, int y) - Specifies the screen location for the upper-left corner of
window.
Eg: glutInitWindowPosition(0,0); // Place window top left on display
 glutInitWindowSize(int width, int size) - Specifies the size in pixels of the window.
Eg: glutInitWindowSize(500,500); //500x500 window
 int glutCreateWindow(char *string) - Creates a window with an OpenGL context. It returns a unique
identifier for the new window. Until glutMainLoop() is called , the window is not yet displayed.
Eg: glutCreateWindow(“Sierpinski Gasket”);
 glutDisplayFunc(void (*func)(void)) - It is the first and most important event callback function .
Whenever GLUT determines the contents of the window need to be redisplayed, the callback
function registered by glutDisplayFunc() is executed. Therefore, we should put all the routines you
need to redraw the scene in the display callback function.
 glutMainLoop(void)- All windows that have been created are now shown, and rendering to those
windows is now effective. Event processing begins, and the registered display callback is triggered.
Once this loop is entered, it is never exited.
Aspect Ratio and View ports: void glViewport(Glint x, Glint y, Glsizei w, Glsizei
35
h)
(x,y)-Lower left corner of view port, w:h is the aspect ratio
 glFlush forces all such buffers to be emptied and the OpenGL functions to be
processed
 This is simply a routine to force execution of our OpenGL functions, which
are stored by computer systems in buffers in different locations, depending on
how OpenGL is implemented. On a busy network, for example, there could
be delays in processing some buffers.
Window size and window position
OpenGL syntax

 Functions:
glFunction: glBegin, glClear, glVertex, …
 Constants:
GL_CONSTANT: GL_2D, GL_LINE
 Datatypes:
GLtype: GLbyte, GLint, GLfloat

H&B 3-5:64-72
Example
glClearColor(1.0,1.0,1.0,0.0);// Background color
glMatrixMode(GL_PROJECTION); // Set transformation
glLoadIdentity;
gluOrtho2D(0, 200, 0, 150);

glClear(GL_COLOR_BUFFER_BIT); // Clear background

glColor3f(1.0, 0.0, 0.0); // Set color to red


glBegin(GL_LINES); // Draw line
glVertex2i(180, 15); // - first point
glVertex2i(10, 145); // - second point
glEnd; // Ready with line
glFlush; // Send
Example
The following is a main program that works for most graphics applications
#include <GL/glut.h>
void main(int *argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow(“ Sample program”);
glutdisplayFunc(display);
myinit();
glutMainLoop();
}
41
Example 3D

Aim: Draw two rectangular boxes

1. Set up viewing transformation


2. Specify the colors
3. Draw the objects
Example 3D
// Set up viewing transformation

glViewport(0, 0, 500, 500); // Select part of window

glMatrixMode(GL_PROJECTION); // Set projection


glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 4.0, 20.0);

glMatrixMode(GL_MODELVIEW); // Set camera


glLoadIdentity();
gluLookAt(3.0, 6.0, 5.0, - eye point
1.0, 0.0, 0.0, - center point
0.0, 0.0, 1.0); - up axis
Example 3D
// Clear background

glClearColor(1.0,1.0,1.0,0.0);// Background color


glClear(GL_COLOR_BUFFER_BIT); // Clear background

// Set color

glColor3f(0.0, 0.0, 0.0); // Set color to black


Example 3D

// Draw two rectangular boxes

glutWireCube(1.0); // unit box around origin

glTranslatef(2.0, 0.0, 0.0); // move in x-direction


glRotatef(30, 0.0, 0.0, 1.0); // rotate 30 degrees
around z-axis
glScalef(1.0, 1.0, 2.0); // scale in z-direction

glutWireCube(1.0); // translated, rotated, scaled box


Example 3D

glutWireCube(1.0); // unit box around origin

glTranslatef(3.0, 0.0, 0.0); // move in x-direction


glRotatef(30, 0.0, 0.0, 1.0); // rotate 30 degrees
around z-axis
glScalef(1.0, 1.0, 2.0); // scale in z-direction

glutWireCube(1.0); // translated, rotated, scaled box

Note:
• Objects are drawn in the current local axis-frame;
• With transformations this frame can be changed.
View Ports
•Do not have use the entire window for the
image: glViewport(x,y,w,h)
•Values in pixels (screen coordinates)
void glViewport(Glint x, Glint y, Glsizei w, Glsizei h)
(x,y)-Lower left corner of view port, w:h is the aspect ratio

48
Camera Analogy

 3Dis just like taking a photograph (lots of


photographs!) viewing
volume

camera

tripod model
Transformations in OpenGL
Modeling
Viewing
orient camera
projection

Animation
Map to screen
Polygon and Recursion (Gasket Problem)
1. Draw a triangle
2. Draw line segments connecting the midpoints of the sides of the triangle
3. Divide the triangle into four triangles
4. Middle triangle contains no points
5. Repeat step 2 to 4 subdivided triangles
Eg: Program for Sierpinski gasket that uses polygons and not require the use of a random number
generator.
Three –Dimensional Gasket
 3D pictures are not restricted to a plane
 Basic: Triangle in 2D=Tetrahedron in 3D
 Tetrahedron is convex
 The midpoint of a line segment between a vertex and any point inside a tetrahedron is
also inside the tetrahedron
Eg: 3D-Program for Sierpinski gasket that uses points and a random number generator .
51
Use of polygons in three dimensions

 Uses both polygons and subdivisions of a tetrahedron into smaller


tetrahedrons.
1. Find the midpoints of its six edges and connect them
2. Form four tetrahedrons
3. For each triangle apply steps 1 and 2
4. Use recursive approach to repeat steps 1 to 3

Eg: 3D- Program for Sierpinski gasket that uses polygons (tetrahedron) and not
require the use of a random number generator.

52
3D Gasket

 We can subdivide each of the four faces

 Appears as if we remove a solid tetrahedron from the


center leaving four smaller tetrahedra

53

You might also like