SlideShare a Scribd company logo
Chapter 2:
Graphics Programming
www.themegallery.com
Instructor: Shih-Shinh Huang
1
Outlines
Introduction
OpenGL API
Primitives and Attributes
OpenGL Viewing
Sierpinski Gasket Example
Implicit Functions Plotting
2
Introduction
Graphics System
 The basic model of a graphics package is a
black box described by its input and output.
 Input Interface
• Function Calls from User Program
• Measurements from Input Devices
 Output Interface
• Graphics to Output Device.
3
Introduction
Graphics System
 API Categories
• Primitive Functions
• Attribute Functions
• Viewing Functions
• Transformation Functions
• Input Functions
• Control Functions
• Query Functions
4
Introduction
Coordinate System
 It is difficult to specify the vertices in units of
the physical device.
 Device-independent graphics makes users
easy to define their own coordinate system
• World Coordinate System
• Application Coordinate System.
Rendering Process
5
Introduction
Running OpenGL on Windows VC++
 Step 1: Download the GLUT for windows from website.
 Step 2: Put the following files in the locations
• glut32.dll -> C:windowssystem32
• glut32.lib -> <VC Install Dir>lib
• glut.h -> <VC Install Dir>include
 Step 3: Create a VC++ Windows Console Project
 Step 4: Add a C++ File to the created project
 Step 5: Add opengl32.lib glu32.lib glut32.lib to
• Project->Properties->Configuration Properties->Linker->Input-
>Additional Dependencies
6
OpenGL API
What is OpenGL (Open Graphics Library)
 It is a layer between programmer and
graphics hardware.
 It is designed as hardware-independent
interface to be implemented on many different
hardware platforms
 This interface consists of over 700 distinct
commands.
• Software library
• Several hundred procedures and functions
7
OpenGL API
What is OpenGL
Applicaton
Graphics Package
OpenGL Application Programming Interface
Hardware and software
Output Device Input Device
Applicaton
Input Device
8
OpenGL API
Library Organization
 OpenGL (GL)
• Core Library
• OpenGL on Windows.
 OpenGL Utility Library (GLU)
• 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.
9
OpenGL API
Library Organization
GLX for X window systems
WGL for Windows
AGL for Macintosh
10
OpenGL API
Program Structure
 Step 1: Initialize the interaction between windows
and OpenGL.
 Step 2: Specify the window properties and further
create window.
 Step 3: Set the callback functions
 Step 4: Initialize the program attributes
 Step 5: Start to run the program
11
OpenGL API
Program Framework
#include <GL/glut.h>
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
includes gl.h
define window properties
set OpenGL state
enter event loop
display callback
interaction initialization
12
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.
13
OpenGL API
Program Framework
void myDisplay(){
/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}/* End of GasketDisplay */
void myInit(){
/* set colors */
glClearColor(1.0, 1.0, 1.0, 0.0);
}/* End of myInit */
14
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.
15
Remark: OpenGL is a state machine. You put it into various
states or modes that remain in effect until you change them
Primitives and Attributes
Primitive Description
 An API should contain a small set of primitives
that the hardware can be expected to support.
 The primitives should be orthogonal.
OpenGL Primitive
 Basic Library: has a small set of primitives
 GLU Library: contains a rich set of objects
derived from basic library.
16
Primitives and Attributes
Primitive Classes
 Geometric Primitives
• They are subject to series of geometric operations.
• They include points, line segments, curves, etc.
 Raster Primitives
• They are lack of geometric properties
• They may be array of pixels.
17
Primitives and Attributes
Program Form of Primitives
 The basic ones are specified by a set of vertices.
 The type specifies how OpenGL assembles the
vertices.
glBegin(type);
glVertex*(…);
glVertex*(…);
.
.
glVertex*(…);
glEnd();
18
Primitives and Attributes
Program Form of Primitives
 Vertex Function: glVertex*()
• * : can be as the form [nt | ntv]
• n : the number of dimensions (2, 3, 4)
• t : data type (i: integer, f: float, and d: double)
• v : the variables is a pointer.
glVertex2i (GLint x, GLint y);
glVertex3f(GLfloat x, GLfloat y, GLfloat z);
glVertex2fv(p); // int p[2] = {1.0, 1.0}
19
Primitives and Attributes
Points and Line Segment
 Point: GL_POINTS
 Line Segments: GL_LINES
 Polygons:
• GL_LINE_STRIP
• GL_LINE_LOOP
20
Primitives and Attributes
Polygon Definition
 It is described by a line loop
 It has a well-defined interior.
Polygon in Computer Graphics
 The polygon can be displayed rapidly.
 It can be used to approximate arbitrary surfaces.
21
Primitives and Attributes
Polygon Properties
 Simple: no two edges cross each other
 Convex: all points on the line segment
between two points inside the object.
 Flat: any three no-collinear determines a
plane where that triangle lies.
Simple Non-Simple Convexity
22
Primitives and Attributes
Polygon Primitives
 Polygons: GL_POLYGON
 Triangles: GL_TRIANGLES
 Quadrilaterals: GL_QUADS
 Stripes: GL_TRIANGLE_STRIP
 Fans: GL_TRIANGLE_FAN
23
Primitives and Attributes
Attributes
 An attribute 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
24
Primitives and Attributes
Example: Sphere Approximation
 A set of polygons are used to construct an
approximation to a sphere.
• Longitude
• Latitude
25
Primitives and Attributes
Example: Sphere Approximation
 We use quad strips primitive to approximate
the sphere.



sin),(
coscos),(
cossin),(



z
y
x
26
Primitives and Attributes
void myDisplay(){
/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);
for(phi=-80; phi <= 80; phi+=20.0){
glBegin(GL_QUAD_STRIP);
phi20 = phi+20;
phir = (phi * 3.14159 / 180);
phir20 = (phi20 * 3.14159 / 180);
for(theta=-180; theta <= 180; theta += 20){
}/* End of for-loop */
glEnd();
}/* End for-loop */
glFlush();
}/* End of Sphere */
27
Primitives and Attributes
thetar = (theta * 3.14159 / 180);
/* compute the point coordinate */
x = sin(thetar)*cos(phir);
y = cos(thetar)*cos(phir);
z = sin(phir);
glVertex3d(x,y,z);
x = sin(thetar)*cos(phir20);
y = cos(thetar)*cos(phir20);
z = sin(phir20);
glVertex3d(x,y,z);
28
Primitives and Attributes
Color
 From the programmer’s view, the color is
handled through the APIs.
 There are two different approaches
• RGB-Color Model
• Index-Color Model
 Index-Color model is easier to support in
hardware implementation
• Low Memory Requirement
• Limited Available Color
29
Primitives and Attributes
RGB-Model
 Each color component is stored separately in
the frame buffer
 For hardware independence consideration,
color values range from 0.0 (none) to 1.0 (all),
30
Primitives and Attributes
RGB-Model
 Setting Operations
 Clear Color Setting
• Transparency: alpha = 0.0
• Opacity: alpha = 1.0;
glColor3f(r value, g value, b value);
glClearColor(r value, g value, b value, alpha);
31
Primitives and Attributes
Indexed Color
 Colors are indexed into tables of RGB values
 Example
• For k=m=8, we can choose 256 out of 16M colors.
glIndex(element);
glutSetColor(color, r value, g value, b value);
32
OpenGL Viewing
Description
 The viewing is to describe how we would like
these objects to appear.
 The concept is just as what we record in a
photograph
• Camera Position
• Focal Lens
 View Models
• Orthographic Viewing
• Two-Dimensional Viewing
33
OpenGL Viewing
Orthographic View
 It is the simple and OpenGL’s default view
 It is what we would get if the camera has an
infinitely long lens.
 All projections become parallel
34
OpenGL Viewing
Orthographic View
 There is a reference point in the projection
plane where we can make measurements.
• View Volume
• Projection Direction
35
OpenGL Viewing
Orthographic View
 The parameters are distances measured from
the camera
 It sees only the objects in the viewing volume.
 OpenGL Default
• Cube Volume: 2x2x2
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble near, GLdouble far)
36
OpenGL Viewing
Two-Dimensional Viewing
 It is a special case of three-dimensional graphics
 Viewing rectangle is in the plane z=0.
void gluOrtho2D(GLdouble left, GLdouble bottom,
GLdouble right, GLdouble top);
37
OpenGL Viewing
Two-Dimensional Viewing
 It directly takes a viewing rectangle (clipping
rectangle) of our 2D world.
 The contents of viewing rectangle is
transferred to the display.
38
OpenGL Viewing
Aspect Ratio
 It is the ratio of rectangle’s width to its height.
 The independence of the object and viewing
can cause distortion effect.
void gluOrtho2D(left, bottom, right, top);
void glutInitWIndowSize(width, height)
39
OpenGL Viewing
Aspect Ratio
 The distortion effect can be avoided if clipping
rectangle and display have the same ratio.
void glViewport(Glint x, Glint y, Glsizei w, Glsizei h);
40
(x,y): lower-left corner
Sierpinski Gasket Example
Description
 It is shape of interest in areas such as fractal
geometry.
 It is an object that can be defined recursively
and randomly.
 The input is the three points that are
not collinear.
},,{ 210 vvv
41
Sierpinski Gasket Example
Construction Process
 Step 1: Pick an initial point inside the triangle.
 Step 2: Select one of the three vertices
randomly.
 Step 3: Display a marker at the middle point.
 Step 4: Replace with the middle point
 Step 5: Return to Step 2.
p
p
},,{ 210 vvv
42
Sierpinski Gasket Example
43
void myInit(){
/* set colors */
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
/* set the view */
gluOrtho2D(0.0, 50.0, 0.0, 50.0);
}/* End of myInit */
int main(int argc, char** argv){
/* initialize the interaction */
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple");
/* set the callback function */
glutDisplayFunc(myDisplay);
myInit();
/* start to run the program */
glutMainLoop();
}/* End of main */
Sierpinski Gasket Example: 2D
void myDisplay(){
/* declare three points */
GLfloat vertices[3][2]={{0.0,0.0},{25.0, 50.0},{50.0, 0.0}};
GLfloat p[2] = {25.0, 25.0};
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for(int k=0; k < 5000; k++){
/* generate the random index */
int j = rand() % 3;
p[0] = (p[0] + vertices[j][0]) / 2;
p[1] = (p[1] + vertices[j][1]) / 2;
glVertex2fv(p);
}/* End of for-loop */
glEnd();
glFlush();
}/* End of GasketDisplay */
44
Sierpinski Gasket Example: 2D
45
Implicit Function Plotting
What is Implicit Function
 A function equals to a specific value
 It is a contour curves that correspond to a set
of fixed values
zyxf ),( 1),( 22
 yxyxf
z
Cutoff Value
z
),( yxf
46
Implicit Function Plotting
Marching Squares
 An algorithm finds an approximation of the
desired function from a set of samples.
 It starts from a set of samples )},({ jiij yxff 
xixxi  0
yjyyj  0
1,...2,1,0  Ni
1,...2,1,0  Mj
47
Implicit Function Plotting
Marching Squares
 The contour curves passes through the edge
• One Vertex:
• Adjacent Vertex:
0),(  zyxf
0),(  zyxf
Solution 1 Solution 2
Principle of Occam’s Razor: if there are multiple possible
explanation of phenomenon, choose the simplest one.
48
Implicit Function Plotting
Marching Squares
 Intersection Points
• Midpoint
• Interpolation
ba
xca
xx i



)(
Halfway Interpolation
49
Implicit Function Plotting
Marching Squares
16 possibilities
ambiguity
Translation
Inversion
50
Implicit Function Plotting
Marching Squares
 Ambiguity Effect
• We have no idea to prefer one over the other.
 Ambiguity Resolving
• Subdivide the cell into four smaller cells.
• Analyze these four cells until no ambiguity occurs.
51
Implicit Function Plotting
Example
04)(),( 4222222
 bxaayxyxf
Midpoint Interpolation
49.0a
5.0b
52
Implicit Function Plotting
53
void myDisplay(){
/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);
double data[N_X][N_Y];
double sx, sy;
glBegin(GL_POINTS);
for(int i=0; i < N_X; i++)
for(int j=0; j < N_Y; j++){
sx = MIN_X + (i * (MAX_X - MIN_X) / N_X);
sy = MIN_Y + (j * (MAX_Y - MIN_Y) / N_Y);
data[i][j] = myFunction(sx, sy);
glVertex2d(sx,sy);
}/* End of for-loop */
glEnd();
……………
}
(sx,sy)
N_X=4
N_Y=4
(MIN_X,MIN_Y)
(MAX_X,MAX_Y)
Implicit Function Plotting
54
void myDisplay(){
…………
/* process each cell */
for(int i=0; i < N_X; i++)
for(int j=0; j < N_Y; j++){
int c;
/* check the cell case */
c = cell(data[i][j], data[i+1][j], data[i+1][j+1], data[i][j+1]);
/* drawing lines depending on the cell case */
drawLine(c, i, j, data[i][j], data[i+1][j], data[i+1][j+1], data[i][j+1]);
}/* End of for-loop */
glFlush();
}/* End of GasketDisplay */
Implicit Function Plotting
Example: Implementation
55
double myFunction(double x, double y){
double a=0.49, b=0.5;
/* compute ovals of cassini */
return (x*x + y*y + a*a)*(x*x + y*y + a*a) - 4*a*a*x*x - b*b*b*b;
}/* End of myFunction */
Implicit Function Plotting
Example: Implementation
56
int cell(double a, double b, double c, double d){
int n=0;
if(a > 0) n = n+1;
if(b > 0) n = n+2;
if(c > 0) n = n+4;
if(d > 0) n = n+8;
return n;
}/* End of cell */
a b
cd
0
15
1
14
2
13
3
12
Implicit Function Plotting
57
void drawLine(int state, int i, int j, double a, double b, double c, double d){
x = MIN_X + (double) i * (MAX_X - MIN_X) / N_X;
y = MIN_Y + (double) j * (MAX_Y - MIN_Y) / N_Y;
halfx = (MAX_X - MIN_X) / (2 * N_X);
halfy = (MAX_Y - MIN_Y) / (2 * N_Y);
x1 = x2 = x;
y1 = y2 = y;
; determine (x1, y1) and (x2, y2)
……………….
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
}/* End of drawLines */
(x,y)
2*halfy
2*halfx
Implicit Function Plotting
58
void drawLine(int state, int i, int j, double a, double b, double c, double d){
……….
switch(state){
/* draw nothing */
case 0: case 15:
break;
case 1: case 14:
x1 = x; y1 = y + halfy;
x2 = x + halfx; y2 = y;
break;
case 2: case 13:
x1 = x + halfx; y1 = y;
x2 = x + halfx * 2; y2 = y + halfy;
break;
}/* End of switch */
…………
}/* End of drawLines */
(x1,y1)
(x2,y2)
59
Ad

More Related Content

What's hot (20)

Compare between pop and oop
Compare between pop and oopCompare between pop and oop
Compare between pop and oop
Md Ibrahim Khalil
 
Football Player Detection and Tracking
Football Player Detection and TrackingFootball Player Detection and Tracking
Football Player Detection and Tracking
Rittwik Adhikari
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
Ankit Garg
 
computer vision
computer vision computer vision
computer vision
RITAFARIARICHI221155
 
CONVOLUTIONAL NEURAL NETWORK
CONVOLUTIONAL NEURAL NETWORKCONVOLUTIONAL NEURAL NETWORK
CONVOLUTIONAL NEURAL NETWORK
Md Rajib Bhuiyan
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
Gary Yeh
 
Introduction to 2D/3D Graphics
Introduction to 2D/3D GraphicsIntroduction to 2D/3D Graphics
Introduction to 2D/3D Graphics
Prabindh Sundareson
 
Introduction to OOP(in java) BY Govind Singh
Introduction to OOP(in java)  BY Govind SinghIntroduction to OOP(in java)  BY Govind Singh
Introduction to OOP(in java) BY Govind Singh
prabhat engineering college
 
Artificial intelligence and knowledge representation
Artificial intelligence and knowledge representationArtificial intelligence and knowledge representation
Artificial intelligence and knowledge representation
Sajan Sahu
 
Introduction to Graphics & Multimedia.ppt
Introduction to Graphics & Multimedia.pptIntroduction to Graphics & Multimedia.ppt
Introduction to Graphics & Multimedia.ppt
Abbas Hyder , Assistant Professor
 
Difference between association, aggregation and composition
Difference between association, aggregation and compositionDifference between association, aggregation and composition
Difference between association, aggregation and composition
baabtra.com - No. 1 supplier of quality freshers
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
2D graphics
2D graphics2D graphics
2D graphics
Muhammad Rashid
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 
1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf
SantoshKumar326148
 
Lecture 6 introduction to open gl and glut
Lecture 6   introduction to open gl and glutLecture 6   introduction to open gl and glut
Lecture 6 introduction to open gl and glut
simpleok
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Image to image translation with Pix2Pix GAN
Image to image translation with Pix2Pix GANImage to image translation with Pix2Pix GAN
Image to image translation with Pix2Pix GAN
S.Shayan Daneshvar
 
An Introduction to Image Processing and Artificial Intelligence
An Introduction to Image Processing and Artificial IntelligenceAn Introduction to Image Processing and Artificial Intelligence
An Introduction to Image Processing and Artificial Intelligence
Wasif Altaf
 
Football Player Detection and Tracking
Football Player Detection and TrackingFootball Player Detection and Tracking
Football Player Detection and Tracking
Rittwik Adhikari
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
Ankit Garg
 
CONVOLUTIONAL NEURAL NETWORK
CONVOLUTIONAL NEURAL NETWORKCONVOLUTIONAL NEURAL NETWORK
CONVOLUTIONAL NEURAL NETWORK
Md Rajib Bhuiyan
 
Introduction of openGL
Introduction  of openGLIntroduction  of openGL
Introduction of openGL
Gary Yeh
 
Artificial intelligence and knowledge representation
Artificial intelligence and knowledge representationArtificial intelligence and knowledge representation
Artificial intelligence and knowledge representation
Sajan Sahu
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 
1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf1.2 Views, Layouts, and Resources.pptx.pdf
1.2 Views, Layouts, and Resources.pptx.pdf
SantoshKumar326148
 
Lecture 6 introduction to open gl and glut
Lecture 6   introduction to open gl and glutLecture 6   introduction to open gl and glut
Lecture 6 introduction to open gl and glut
simpleok
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Image to image translation with Pix2Pix GAN
Image to image translation with Pix2Pix GANImage to image translation with Pix2Pix GAN
Image to image translation with Pix2Pix GAN
S.Shayan Daneshvar
 
An Introduction to Image Processing and Artificial Intelligence
An Introduction to Image Processing and Artificial IntelligenceAn Introduction to Image Processing and Artificial Intelligence
An Introduction to Image Processing and Artificial Intelligence
Wasif Altaf
 

Viewers also liked (20)

CG OpenGL 3D object representations-course 8
CG OpenGL 3D object representations-course 8CG OpenGL 3D object representations-course 8
CG OpenGL 3D object representations-course 8
fungfung Chen
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3
elnaqah
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2
Sardar Alam
 
CG OpenGL polar curves & input display color-course 4
CG OpenGL polar curves & input display color-course 4CG OpenGL polar curves & input display color-course 4
CG OpenGL polar curves & input display color-course 4
fungfung Chen
 
Angel6 e05
Angel6 e05Angel6 e05
Angel6 e05
Mohammed Romi
 
Ch8
Ch8Ch8
Ch8
Mohammed Romi
 
OpenGL Starter L01
OpenGL Starter L01OpenGL Starter L01
OpenGL Starter L01
Mohammad Shaker
 
Ch19 network layer-logical add
Ch19 network layer-logical addCh19 network layer-logical add
Ch19 network layer-logical add
Mohammed Romi
 
Ir 09
Ir   09Ir   09
Ir 09
Mohammed Romi
 
Ir 03
Ir   03Ir   03
Ir 03
Mohammed Romi
 
Swe notes
Swe notesSwe notes
Swe notes
Mohammed Romi
 
OpenGL Starter L02
OpenGL Starter L02OpenGL Starter L02
OpenGL Starter L02
Mohammad Shaker
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
ICS
 
Ch12
Ch12Ch12
Ch12
Mohammed Romi
 
Open gl
Open glOpen gl
Open gl
ch samaram
 
Web Introduction
Web IntroductionWeb Introduction
Web Introduction
Jayant Mukherjee
 
Ir 08
Ir   08Ir   08
Ir 08
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch 23
Ian Sommerville,  Software Engineering, 9th Edition Ch 23Ian Sommerville,  Software Engineering, 9th Edition Ch 23
Ian Sommerville, Software Engineering, 9th Edition Ch 23
Mohammed Romi
 
Ch2020
Ch2020Ch2020
Ch2020
Mohammed Romi
 
Ch 4 software engineering
Ch 4 software engineeringCh 4 software engineering
Ch 4 software engineering
Mohammed Romi
 
CG OpenGL 3D object representations-course 8
CG OpenGL 3D object representations-course 8CG OpenGL 3D object representations-course 8
CG OpenGL 3D object representations-course 8
fungfung Chen
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3
elnaqah
 
3 d graphics with opengl part 2
3 d graphics with opengl  part 23 d graphics with opengl  part 2
3 d graphics with opengl part 2
Sardar Alam
 
CG OpenGL polar curves & input display color-course 4
CG OpenGL polar curves & input display color-course 4CG OpenGL polar curves & input display color-course 4
CG OpenGL polar curves & input display color-course 4
fungfung Chen
 
Ch19 network layer-logical add
Ch19 network layer-logical addCh19 network layer-logical add
Ch19 network layer-logical add
Mohammed Romi
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
ICS
 
Ian Sommerville, Software Engineering, 9th Edition Ch 23
Ian Sommerville,  Software Engineering, 9th Edition Ch 23Ian Sommerville,  Software Engineering, 9th Edition Ch 23
Ian Sommerville, Software Engineering, 9th Edition Ch 23
Mohammed Romi
 
Ch 4 software engineering
Ch 4 software engineeringCh 4 software engineering
Ch 4 software engineering
Mohammed Romi
 
Ad

Similar to Chapter02 graphics-programming (20)

openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).ppt
HIMANKMISHRA2
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.ppt
HIMANKMISHRA2
 
3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx
ssuser255bf1
 
CG3_ch3+ch4computergraphicsbreesenhan.pdf
CG3_ch3+ch4computergraphicsbreesenhan.pdfCG3_ch3+ch4computergraphicsbreesenhan.pdf
CG3_ch3+ch4computergraphicsbreesenhan.pdf
VikramBhathal
 
september11.ppt
september11.pptseptember11.ppt
september11.ppt
CharlesMatu2
 
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
Syed Zaid Irshad
 
Computer Graphics involves technology to access. The Process transforms and p...
Computer Graphics involves technology to access. The Process transforms and p...Computer Graphics involves technology to access. The Process transforms and p...
Computer Graphics involves technology to access. The Process transforms and p...
ErNandiniDharne
 
OpenGL Introduction.
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.
Girish Ghate
 
OpenGL Introduction
OpenGL IntroductionOpenGL Introduction
OpenGL Introduction
Jayant Mukherjee
 
Opengl (1)
Opengl (1)Opengl (1)
Opengl (1)
ch samaram
 
Introduction to OpenGL.ppt
Introduction to OpenGL.pptIntroduction to OpenGL.ppt
Introduction to OpenGL.ppt
16118MdFirozAhmed
 
Programming with OpenGL
Programming with OpenGLProgramming with OpenGL
Programming with OpenGL
Syed Zaid Irshad
 
Open gl basics
Open gl basicsOpen gl basics
Open gl basics
saad siddiqui
 
UNIT 1 OPENGL_UPDATED .pptx
UNIT 1 OPENGL_UPDATED               .pptxUNIT 1 OPENGL_UPDATED               .pptx
UNIT 1 OPENGL_UPDATED .pptx
miteshchaudhari4466
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
Adri Jovin
 
18csl67 vtu lab manual
18csl67 vtu lab manual18csl67 vtu lab manual
18csl67 vtu lab manual
NatsuDragoneel5
 
CGChapter 3.pptx
CGChapter 3.pptxCGChapter 3.pptx
CGChapter 3.pptx
YazanAlbilleh
 
Graphics Libraries
Graphics LibrariesGraphics Libraries
Graphics Libraries
Prachi Mishra
 
Opengl basics
Opengl basicsOpengl basics
Opengl basics
pushpa latha
 
3D Programming Basics: WebGL
3D Programming Basics: WebGL3D Programming Basics: WebGL
3D Programming Basics: WebGL
Globant
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).ppt
HIMANKMISHRA2
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.ppt
HIMANKMISHRA2
 
3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx3 CG_U1_P2_PPT_3 OpenGL.pptx
3 CG_U1_P2_PPT_3 OpenGL.pptx
ssuser255bf1
 
CG3_ch3+ch4computergraphicsbreesenhan.pdf
CG3_ch3+ch4computergraphicsbreesenhan.pdfCG3_ch3+ch4computergraphicsbreesenhan.pdf
CG3_ch3+ch4computergraphicsbreesenhan.pdf
VikramBhathal
 
Computer Graphics involves technology to access. The Process transforms and p...
Computer Graphics involves technology to access. The Process transforms and p...Computer Graphics involves technology to access. The Process transforms and p...
Computer Graphics involves technology to access. The Process transforms and p...
ErNandiniDharne
 
OpenGL Introduction.
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.
Girish Ghate
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
Adri Jovin
 
3D Programming Basics: WebGL
3D Programming Basics: WebGL3D Programming Basics: WebGL
3D Programming Basics: WebGL
Globant
 
Ad

More from Mohammed Romi (12)

Ai 02 intelligent_agents(1)
Ai 02 intelligent_agents(1)Ai 02 intelligent_agents(1)
Ai 02 intelligent_agents(1)
Mohammed Romi
 
Ai 01 introduction
Ai 01 introductionAi 01 introduction
Ai 01 introduction
Mohammed Romi
 
Ai 03 solving_problems_by_searching
Ai 03 solving_problems_by_searchingAi 03 solving_problems_by_searching
Ai 03 solving_problems_by_searching
Mohammed Romi
 
Swiching
SwichingSwiching
Swiching
Mohammed Romi
 
Ir 02
Ir   02Ir   02
Ir 02
Mohammed Romi
 
Ir 01
Ir   01Ir   01
Ir 01
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch 4
Ian Sommerville,  Software Engineering, 9th Edition Ch 4Ian Sommerville,  Software Engineering, 9th Edition Ch 4
Ian Sommerville, Software Engineering, 9th Edition Ch 4
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch2
Ian Sommerville,  Software Engineering, 9th Edition Ch2Ian Sommerville,  Software Engineering, 9th Edition Ch2
Ian Sommerville, Software Engineering, 9th Edition Ch2
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch1
Ian Sommerville,  Software Engineering, 9th Edition Ch1Ian Sommerville,  Software Engineering, 9th Edition Ch1
Ian Sommerville, Software Engineering, 9th Edition Ch1
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th EditionCh 8
Ian Sommerville,  Software Engineering, 9th EditionCh 8Ian Sommerville,  Software Engineering, 9th EditionCh 8
Ian Sommerville, Software Engineering, 9th EditionCh 8
Mohammed Romi
 
Ch7
Ch7Ch7
Ch7
Mohammed Romi
 
Ch 6
Ch 6Ch 6
Ch 6
Mohammed Romi
 
Ai 02 intelligent_agents(1)
Ai 02 intelligent_agents(1)Ai 02 intelligent_agents(1)
Ai 02 intelligent_agents(1)
Mohammed Romi
 
Ai 03 solving_problems_by_searching
Ai 03 solving_problems_by_searchingAi 03 solving_problems_by_searching
Ai 03 solving_problems_by_searching
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch 4
Ian Sommerville,  Software Engineering, 9th Edition Ch 4Ian Sommerville,  Software Engineering, 9th Edition Ch 4
Ian Sommerville, Software Engineering, 9th Edition Ch 4
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch2
Ian Sommerville,  Software Engineering, 9th Edition Ch2Ian Sommerville,  Software Engineering, 9th Edition Ch2
Ian Sommerville, Software Engineering, 9th Edition Ch2
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th Edition Ch1
Ian Sommerville,  Software Engineering, 9th Edition Ch1Ian Sommerville,  Software Engineering, 9th Edition Ch1
Ian Sommerville, Software Engineering, 9th Edition Ch1
Mohammed Romi
 
Ian Sommerville, Software Engineering, 9th EditionCh 8
Ian Sommerville,  Software Engineering, 9th EditionCh 8Ian Sommerville,  Software Engineering, 9th EditionCh 8
Ian Sommerville, Software Engineering, 9th EditionCh 8
Mohammed Romi
 

Recently uploaded (20)

The History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.pptThe History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.ppt
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
INQUISITORS School Quiz Prelims 2025.pptx
INQUISITORS School Quiz Prelims 2025.pptxINQUISITORS School Quiz Prelims 2025.pptx
INQUISITORS School Quiz Prelims 2025.pptx
SujatyaRoy
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & ConfigurationsBipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
GS Virdi
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
INQUISITORS School Quiz Prelims 2025.pptx
INQUISITORS School Quiz Prelims 2025.pptxINQUISITORS School Quiz Prelims 2025.pptx
INQUISITORS School Quiz Prelims 2025.pptx
SujatyaRoy
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & ConfigurationsBipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
GS Virdi
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 

Chapter02 graphics-programming

  • 2. Outlines Introduction OpenGL API Primitives and Attributes OpenGL Viewing Sierpinski Gasket Example Implicit Functions Plotting 2
  • 3. Introduction Graphics System  The basic model of a graphics package is a black box described by its input and output.  Input Interface • Function Calls from User Program • Measurements from Input Devices  Output Interface • Graphics to Output Device. 3
  • 4. Introduction Graphics System  API Categories • Primitive Functions • Attribute Functions • Viewing Functions • Transformation Functions • Input Functions • Control Functions • Query Functions 4
  • 5. Introduction Coordinate System  It is difficult to specify the vertices in units of the physical device.  Device-independent graphics makes users easy to define their own coordinate system • World Coordinate System • Application Coordinate System. Rendering Process 5
  • 6. Introduction Running OpenGL on Windows VC++  Step 1: Download the GLUT for windows from website.  Step 2: Put the following files in the locations • glut32.dll -> C:windowssystem32 • glut32.lib -> <VC Install Dir>lib • glut.h -> <VC Install Dir>include  Step 3: Create a VC++ Windows Console Project  Step 4: Add a C++ File to the created project  Step 5: Add opengl32.lib glu32.lib glut32.lib to • Project->Properties->Configuration Properties->Linker->Input- >Additional Dependencies 6
  • 7. OpenGL API What is OpenGL (Open Graphics Library)  It is a layer between programmer and graphics hardware.  It is designed as hardware-independent interface to be implemented on many different hardware platforms  This interface consists of over 700 distinct commands. • Software library • Several hundred procedures and functions 7
  • 8. OpenGL API What is OpenGL Applicaton Graphics Package OpenGL Application Programming Interface Hardware and software Output Device Input Device Applicaton Input Device 8
  • 9. OpenGL API Library Organization  OpenGL (GL) • Core Library • OpenGL on Windows.  OpenGL Utility Library (GLU) • 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. 9
  • 10. OpenGL API Library Organization GLX for X window systems WGL for Windows AGL for Macintosh 10
  • 11. OpenGL API Program Structure  Step 1: Initialize the interaction between windows and OpenGL.  Step 2: Specify the window properties and further create window.  Step 3: Set the callback functions  Step 4: Initialize the program attributes  Step 5: Start to run the program 11
  • 12. OpenGL API Program Framework #include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(myDisplay); myInit(); glutMainLoop(); } includes gl.h define window properties set OpenGL state enter event loop display callback interaction initialization 12
  • 13. 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. 13
  • 14. OpenGL API Program Framework void myDisplay(){ /* clear the display */ glClear(GL_COLOR_BUFFER_BIT); glFlush(); }/* End of GasketDisplay */ void myInit(){ /* set colors */ glClearColor(1.0, 1.0, 1.0, 0.0); }/* End of myInit */ 14
  • 15. 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. 15 Remark: OpenGL is a state machine. You put it into various states or modes that remain in effect until you change them
  • 16. Primitives and Attributes Primitive Description  An API should contain a small set of primitives that the hardware can be expected to support.  The primitives should be orthogonal. OpenGL Primitive  Basic Library: has a small set of primitives  GLU Library: contains a rich set of objects derived from basic library. 16
  • 17. Primitives and Attributes Primitive Classes  Geometric Primitives • They are subject to series of geometric operations. • They include points, line segments, curves, etc.  Raster Primitives • They are lack of geometric properties • They may be array of pixels. 17
  • 18. Primitives and Attributes Program Form of Primitives  The basic ones are specified by a set of vertices.  The type specifies how OpenGL assembles the vertices. glBegin(type); glVertex*(…); glVertex*(…); . . glVertex*(…); glEnd(); 18
  • 19. Primitives and Attributes Program Form of Primitives  Vertex Function: glVertex*() • * : can be as the form [nt | ntv] • n : the number of dimensions (2, 3, 4) • t : data type (i: integer, f: float, and d: double) • v : the variables is a pointer. glVertex2i (GLint x, GLint y); glVertex3f(GLfloat x, GLfloat y, GLfloat z); glVertex2fv(p); // int p[2] = {1.0, 1.0} 19
  • 20. Primitives and Attributes Points and Line Segment  Point: GL_POINTS  Line Segments: GL_LINES  Polygons: • GL_LINE_STRIP • GL_LINE_LOOP 20
  • 21. Primitives and Attributes Polygon Definition  It is described by a line loop  It has a well-defined interior. Polygon in Computer Graphics  The polygon can be displayed rapidly.  It can be used to approximate arbitrary surfaces. 21
  • 22. Primitives and Attributes Polygon Properties  Simple: no two edges cross each other  Convex: all points on the line segment between two points inside the object.  Flat: any three no-collinear determines a plane where that triangle lies. Simple Non-Simple Convexity 22
  • 23. Primitives and Attributes Polygon Primitives  Polygons: GL_POLYGON  Triangles: GL_TRIANGLES  Quadrilaterals: GL_QUADS  Stripes: GL_TRIANGLE_STRIP  Fans: GL_TRIANGLE_FAN 23
  • 24. Primitives and Attributes Attributes  An attribute 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 24
  • 25. Primitives and Attributes Example: Sphere Approximation  A set of polygons are used to construct an approximation to a sphere. • Longitude • Latitude 25
  • 26. Primitives and Attributes Example: Sphere Approximation  We use quad strips primitive to approximate the sphere.    sin),( coscos),( cossin),(    z y x 26
  • 27. Primitives and Attributes void myDisplay(){ /* clear the display */ glClear(GL_COLOR_BUFFER_BIT); for(phi=-80; phi <= 80; phi+=20.0){ glBegin(GL_QUAD_STRIP); phi20 = phi+20; phir = (phi * 3.14159 / 180); phir20 = (phi20 * 3.14159 / 180); for(theta=-180; theta <= 180; theta += 20){ }/* End of for-loop */ glEnd(); }/* End for-loop */ glFlush(); }/* End of Sphere */ 27
  • 28. Primitives and Attributes thetar = (theta * 3.14159 / 180); /* compute the point coordinate */ x = sin(thetar)*cos(phir); y = cos(thetar)*cos(phir); z = sin(phir); glVertex3d(x,y,z); x = sin(thetar)*cos(phir20); y = cos(thetar)*cos(phir20); z = sin(phir20); glVertex3d(x,y,z); 28
  • 29. Primitives and Attributes Color  From the programmer’s view, the color is handled through the APIs.  There are two different approaches • RGB-Color Model • Index-Color Model  Index-Color model is easier to support in hardware implementation • Low Memory Requirement • Limited Available Color 29
  • 30. Primitives and Attributes RGB-Model  Each color component is stored separately in the frame buffer  For hardware independence consideration, color values range from 0.0 (none) to 1.0 (all), 30
  • 31. Primitives and Attributes RGB-Model  Setting Operations  Clear Color Setting • Transparency: alpha = 0.0 • Opacity: alpha = 1.0; glColor3f(r value, g value, b value); glClearColor(r value, g value, b value, alpha); 31
  • 32. Primitives and Attributes Indexed Color  Colors are indexed into tables of RGB values  Example • For k=m=8, we can choose 256 out of 16M colors. glIndex(element); glutSetColor(color, r value, g value, b value); 32
  • 33. OpenGL Viewing Description  The viewing is to describe how we would like these objects to appear.  The concept is just as what we record in a photograph • Camera Position • Focal Lens  View Models • Orthographic Viewing • Two-Dimensional Viewing 33
  • 34. OpenGL Viewing Orthographic View  It is the simple and OpenGL’s default view  It is what we would get if the camera has an infinitely long lens.  All projections become parallel 34
  • 35. OpenGL Viewing Orthographic View  There is a reference point in the projection plane where we can make measurements. • View Volume • Projection Direction 35
  • 36. OpenGL Viewing Orthographic View  The parameters are distances measured from the camera  It sees only the objects in the viewing volume.  OpenGL Default • Cube Volume: 2x2x2 void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) 36
  • 37. OpenGL Viewing Two-Dimensional Viewing  It is a special case of three-dimensional graphics  Viewing rectangle is in the plane z=0. void gluOrtho2D(GLdouble left, GLdouble bottom, GLdouble right, GLdouble top); 37
  • 38. OpenGL Viewing Two-Dimensional Viewing  It directly takes a viewing rectangle (clipping rectangle) of our 2D world.  The contents of viewing rectangle is transferred to the display. 38
  • 39. OpenGL Viewing Aspect Ratio  It is the ratio of rectangle’s width to its height.  The independence of the object and viewing can cause distortion effect. void gluOrtho2D(left, bottom, right, top); void glutInitWIndowSize(width, height) 39
  • 40. OpenGL Viewing Aspect Ratio  The distortion effect can be avoided if clipping rectangle and display have the same ratio. void glViewport(Glint x, Glint y, Glsizei w, Glsizei h); 40 (x,y): lower-left corner
  • 41. Sierpinski Gasket Example Description  It is shape of interest in areas such as fractal geometry.  It is an object that can be defined recursively and randomly.  The input is the three points that are not collinear. },,{ 210 vvv 41
  • 42. Sierpinski Gasket Example Construction Process  Step 1: Pick an initial point inside the triangle.  Step 2: Select one of the three vertices randomly.  Step 3: Display a marker at the middle point.  Step 4: Replace with the middle point  Step 5: Return to Step 2. p p },,{ 210 vvv 42
  • 43. Sierpinski Gasket Example 43 void myInit(){ /* set colors */ glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(1.0, 0.0, 0.0); /* set the view */ gluOrtho2D(0.0, 50.0, 0.0, 50.0); }/* End of myInit */ int main(int argc, char** argv){ /* initialize the interaction */ glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("simple"); /* set the callback function */ glutDisplayFunc(myDisplay); myInit(); /* start to run the program */ glutMainLoop(); }/* End of main */
  • 44. Sierpinski Gasket Example: 2D void myDisplay(){ /* declare three points */ GLfloat vertices[3][2]={{0.0,0.0},{25.0, 50.0},{50.0, 0.0}}; GLfloat p[2] = {25.0, 25.0}; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); for(int k=0; k < 5000; k++){ /* generate the random index */ int j = rand() % 3; p[0] = (p[0] + vertices[j][0]) / 2; p[1] = (p[1] + vertices[j][1]) / 2; glVertex2fv(p); }/* End of for-loop */ glEnd(); glFlush(); }/* End of GasketDisplay */ 44
  • 46. Implicit Function Plotting What is Implicit Function  A function equals to a specific value  It is a contour curves that correspond to a set of fixed values zyxf ),( 1),( 22  yxyxf z Cutoff Value z ),( yxf 46
  • 47. Implicit Function Plotting Marching Squares  An algorithm finds an approximation of the desired function from a set of samples.  It starts from a set of samples )},({ jiij yxff  xixxi  0 yjyyj  0 1,...2,1,0  Ni 1,...2,1,0  Mj 47
  • 48. Implicit Function Plotting Marching Squares  The contour curves passes through the edge • One Vertex: • Adjacent Vertex: 0),(  zyxf 0),(  zyxf Solution 1 Solution 2 Principle of Occam’s Razor: if there are multiple possible explanation of phenomenon, choose the simplest one. 48
  • 49. Implicit Function Plotting Marching Squares  Intersection Points • Midpoint • Interpolation ba xca xx i    )( Halfway Interpolation 49
  • 50. Implicit Function Plotting Marching Squares 16 possibilities ambiguity Translation Inversion 50
  • 51. Implicit Function Plotting Marching Squares  Ambiguity Effect • We have no idea to prefer one over the other.  Ambiguity Resolving • Subdivide the cell into four smaller cells. • Analyze these four cells until no ambiguity occurs. 51
  • 52. Implicit Function Plotting Example 04)(),( 4222222  bxaayxyxf Midpoint Interpolation 49.0a 5.0b 52
  • 53. Implicit Function Plotting 53 void myDisplay(){ /* clear the display */ glClear(GL_COLOR_BUFFER_BIT); double data[N_X][N_Y]; double sx, sy; glBegin(GL_POINTS); for(int i=0; i < N_X; i++) for(int j=0; j < N_Y; j++){ sx = MIN_X + (i * (MAX_X - MIN_X) / N_X); sy = MIN_Y + (j * (MAX_Y - MIN_Y) / N_Y); data[i][j] = myFunction(sx, sy); glVertex2d(sx,sy); }/* End of for-loop */ glEnd(); …………… } (sx,sy) N_X=4 N_Y=4 (MIN_X,MIN_Y) (MAX_X,MAX_Y)
  • 54. Implicit Function Plotting 54 void myDisplay(){ ………… /* process each cell */ for(int i=0; i < N_X; i++) for(int j=0; j < N_Y; j++){ int c; /* check the cell case */ c = cell(data[i][j], data[i+1][j], data[i+1][j+1], data[i][j+1]); /* drawing lines depending on the cell case */ drawLine(c, i, j, data[i][j], data[i+1][j], data[i+1][j+1], data[i][j+1]); }/* End of for-loop */ glFlush(); }/* End of GasketDisplay */
  • 55. Implicit Function Plotting Example: Implementation 55 double myFunction(double x, double y){ double a=0.49, b=0.5; /* compute ovals of cassini */ return (x*x + y*y + a*a)*(x*x + y*y + a*a) - 4*a*a*x*x - b*b*b*b; }/* End of myFunction */
  • 56. Implicit Function Plotting Example: Implementation 56 int cell(double a, double b, double c, double d){ int n=0; if(a > 0) n = n+1; if(b > 0) n = n+2; if(c > 0) n = n+4; if(d > 0) n = n+8; return n; }/* End of cell */ a b cd 0 15 1 14 2 13 3 12
  • 57. Implicit Function Plotting 57 void drawLine(int state, int i, int j, double a, double b, double c, double d){ x = MIN_X + (double) i * (MAX_X - MIN_X) / N_X; y = MIN_Y + (double) j * (MAX_Y - MIN_Y) / N_Y; halfx = (MAX_X - MIN_X) / (2 * N_X); halfy = (MAX_Y - MIN_Y) / (2 * N_Y); x1 = x2 = x; y1 = y2 = y; ; determine (x1, y1) and (x2, y2) ………………. glBegin(GL_LINES); glVertex2d(x1, y1); glVertex2d(x2, y2); glEnd(); }/* End of drawLines */ (x,y) 2*halfy 2*halfx
  • 58. Implicit Function Plotting 58 void drawLine(int state, int i, int j, double a, double b, double c, double d){ ………. switch(state){ /* draw nothing */ case 0: case 15: break; case 1: case 14: x1 = x; y1 = y + halfy; x2 = x + halfx; y2 = y; break; case 2: case 13: x1 = x + halfx; y1 = y; x2 = x + halfx * 2; y2 = y + halfy; break; }/* End of switch */ ………… }/* End of drawLines */ (x1,y1) (x2,y2)
  • 59. 59
  翻译: