Chapter 9 - OPEN GL
Chapter 9 - OPEN GL
Chapter 9 - OPEN GL
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
1 2
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
3 4
Hillside College of Engineering Hillside College of Engineering
OpenGL as a state machine OpenGL Libraries
GL State Variables- can be set and queried by OpenGL. Remains GL ((Graphics
p Library):
y) Libraryy of 2-D,, 3-D drawingg
unchanged
nchanged until
ntil the next
ne t change.
change
Projection and viewing matrix primitives and operations
Color and material properties API for 3-D hardware acceleration
Lights and shading
Line and polygon drawing modes GLU (GL Utilities): Miscellaneous functions dealing with
OpenGL functions are of two types camera set-up and higher-level shape descriptions
Primitive generating
g g GLUT (GL Utility Toolkit): Window-system independent
Can cause output if primitive is visible
toolkit with numerous utility functions, mostly dealing with
How vertices are processed and appearance of primitive are
controlled by the state user interface
State
S changing
h i
Transformation functions
Attribute functions
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
5 6
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
7 8
Hillside College of Engineering Hillside College of Engineering
OpenGL Command Formats
GLUT Basics
Program Structure
1. Configure and open window (GLUT)
2. Initialize OpenGL (Optional)
3. Register input callback functions (GLUT)
– R d
Render
– Resize
– Input: keyboard,
keyboard mouse
mouse, etc
4. Enter event processing loop (GLUT)
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
9 10
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
11 12
Hillside College of Engineering Hillside College of Engineering
Callback functions GLUT Callback Functions
For OpenGL
p to become aware of yyour callbacks,, yyou need • Contents of window need to be refreshed
to register them within it before you start drawing things. glutDisplayFunc()
• Window is resized or moved
Some of the callbacks are mandatory, such as display, so
glutReshapeFunc()
th t OpenGL
that O GL know k how
h to t render
d your graphics.
hi
• Key action
Programming interface for event-driven input glutKeyboardFunc()
Define a callback function for each type of event the • Mouse button action
graphics system recognizes glutMouseFunc()
• Mouse moves while a button is pressed
This user-supplied function is executed when the event
glutMotionFunc()
occurs • Mouse moves regardless of mouse button state
GLUT example: glutMouseFunc(mymouse) glutPassiveMouseFunc()
• Called when nothing else is going on
glutIdleFunc()
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
13 14
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
15 16
Hillside College of Engineering Hillside College of Engineering
Window Resize Callback
Register Callback Functions
Set up any callback function you
you’re
re going to use It’ called
It’s ll d when
h the
th window
i d isi resized
i d or movedd
void main (int argc, char **argv)
void resize(int
( w,, int h))
{
{
……
……
glutDisplayFunc ( display ); // display callback
display();
glutReshapeFunc ( resize ); // window resize callback
}
glutKeyboardFunc ( key ); // keyboard callback
……
}
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
17 18
Hillside College of Engineering Hillside College of Engineering
Additive color
Keypress KeyDown glutKeyboardFunc
Used in display screen. Pixels emit three kinds of light: Red,
KeyUp
Green and Blue
Mouse leftButtonDown glutMouseFunc We choose Red,, Green and Blue to be our pprimary y colors.
leftButtonUp No set of 3 primary colors can generate all possible colors.
Motion With mouse press glutMotionFunc But, Red, Green and Blue are close enough.
Without g
glutPassiveMotionFunc
Window Moving glutReshapeFunc
Resizing
System Idle
dl glutIdleFunc
l dl
Timer glutTimerFunc
Software What to draw g
glutDisplayFunc
p y
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
23 24
Hillside College of Engineering Hillside College of Engineering
OpenGL RGB and RGBA modes Color and grayscale
Grayscale means from black to white (only vary in shade)
“A” stands for alpha,
p , refers to transparency.
p y Color means deviation from gray scale.
scale
Alpha = 1.0 : Fully opaque In OpenGL, color is specified in RGB.
Alpha = 0.0 : Fully transparent glColor3f(r,g,b);
In RGB mode, alpha is assumed to be 1.0.
where r, g and b are floating point numbers between 0.0 and 1.0, for
Example: example:
glColor4f(0.5,1.0,0.6,0.3);
lC l 4f(0 5 1 0 0 6 0 3) // RGBA mode,
d alpha
l h sett to
t 0.3
03 This tells
Thi t ll display
di l to t emitit 0.5
0 5 intensity
i t it redd light
li ht together
t th with ith 0.1
01
intensity green light together with 0.9 intensity blue light.
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
25 26
Hillside College of Engineering Hillside College of Engineering
glDisable(GL_BLEND);
Fi l l = s_factor
Final_color f t * object_color
bj t l + d_factor
d f t * frame_buffer_color
f b ff l
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
27 28
Hillside College of Engineering Hillside College of Engineering
Drawing Attributes: Color Color Functions
glColor3f(GLfloat r, GLfloat g, GLfloat b) sets the drawing glColor3f( red value, green value, blue value) ;
color
• Used to specify the wanted color
• glColor3d(), glColor3ui() can also be used
• Has three float parameter;
• Remember OpenGL is a state machine
• Once set, the attribute applies to all subsequent defined glClearColor( red value, green value, blue value, alpha
objects until it is set to some other value value) ;
• glColor3fv() takes a flat array as input • Used to specify the initial background color.
There are more drawing attributes than color • Has
as four
ou float
oat parameters
pa a ete s
• Point size: glPointSize() • Alpha value is used to determine the color of two overlapped objects
• Line width: glLinewidth() glClear ( GL_COLOR_BUFFER_BIT);
• Dash
D h or ddotted
tt d line:
li glLineStipple()
lLi Sti l () • Used to set the bit value in the color buffer (refresh buffer) to the color
• Polygon pattern: glPolygonStipple() indicated in the glClearColor function.
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
29 30
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
33 34
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
35 36
Hillside College of Engineering Hillside College of Engineering
myDisplay() Drawing: Miscellaneous
void myDisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glColor(): Range is [0, 1] for each color channel
glColor3f(1.0, 0.0, 0.0); // set color to red glBegin(GL_POLYGON);
// list the vertices to draw a diamond
glRect(x1, y1, x2, y2) specifying opposite corners of
glVertex2f(0.90, 0.50);
g is equivalent
rectangle q to GL_POLYGON
_ with four
glVertex2f(0.50,
lV 2f(0 50 00.90);
90)
glVertex2f(0.10, 0.50); vertices listed (i.e., filled)
glVertex2f(0.50, 0.10);
glEnd();
lE d() Can set persistent attributes outside of glBegin()/
glColor3f(0.0, 0.0, 1.0); // set color to blue
glRectf(0.25, 0.25, 0.75, 0.75); // draw a rectangle glEnd()
// glFlush();
l l h() // force
f OpenGL to render
d
glutSwapBuffers(); // swap buffers
• glPointSize(GLfloat size)
}
• glLineWidth(GLfloat width)
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
37 38
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
41 42
Hillside College of Engineering Hillside College of Engineering
init();
glutDisplayFunc(display)
g p y ( p y) ;
glutMainLoop() ;
return 0 ;
}
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
43 44
Hillside College of Engineering Hillside College of Engineering
Lights Creating & Positioning Lights
Create and select a lighting model.
model void glLight
glLight*(light
(light, pname,
pname param);
• Creates the light specified by light, which can be GL_LIGHT0, ... , or
Define material properties for the objects in GL_LIGHT7
the scene.
scene • The
h characteristic
h i i off the
h light
li h being
b i set isi defined
d fi d by
b pname, which
hi h
specifies a named parameter
And most important … Enable the lights: • param indicates the values to which the pname characteristic is set.
• glEnable(GL_LIGHTING); • pname can get one of several values:
• glEnable(GL_LIGHT#); GL_AMBIENT, GL_DIFFUSE & GL_SPECULAR define the
light RGBA values for each of the light components.
components
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
45 46
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
47 48
Hillside College of Engineering Hillside College of Engineering
Multiple Light Sources Lighting Model
void glLightModel*(pname, param);
You can define several light
g sources by y callingg • GL_LIGHT_MODEL_AMBIENT defines the global
ambient
bi t RGBA values.
l
glLight*() several times with different light names:
• GL_LIGHT_MODEL_LOCAL_VIEWER determines
• glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); whether we are using a local or infinite viewpoint.
• glLightfv(GL_LIGHT1,
lLi htf (GL LIGHT1 GL_AMBIENT,
GL AMBIENT light1_ambient);
li ht1 bi t) • GL_LIGHT_MODEL_TWO_SIDE determines whether we
are using two sided lighting.
void glMaterial
glMaterial*(face
(face, pname,
pname param);
• Specifies a current material property for use in lighting
calculations.
• Face can be
b GGL_FRONT,
O G
GL_BACK,
AC or
GL_FRONT_AND_BACK to indicate which face of the
object the material should be applied to.
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
49 50
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
53 54
Hillside College of Engineering Hillside College of Engineering
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
55 56
Hillside College of Engineering Hillside College of Engineering
Modeling Transformations OpenGL Perspective Projection example using
gluPerspective
l P i
• Consider the following code sequence:
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL MODELVIEW); gluPerspective(theta,aspect,dnear,dfar);
glLoadIdentity();
glMultMatrixf(N); /* apply transformation N */ glMatrixMode(GL_MODELVIEW);
glMultMatrixf(M); /* apply transformation M */
glMultMatrixf(L); //* apply transformation L *// gluLookAt(x0,y0,z0,xref,yref,zref,Vx,Vy,Vz);
glBegin(GL_POINTS);
glVertex3f(v);/* draw transformed vertex v */ // geometry
glEnd();
glBegin(GL_QUADS);
Th vertex
The t ttransformation
f ti iis N(M(L
N(M(Lv));
)) (A
(As we k
know, th
the matrices
t i are …
multiplied together first)
w
• Special matrix multiplications: h
void glScale{fd}(TYPE x, TYPE y, TYPEz); (xref,yref,zref) (Vx,Vy,Vz)
void glRotate{fd}(TYPE angle,TYPE x,TYPE y,TYPE z);
q
void glTranslate{fd}(TYPEx,
glTranslate{fd}(TYPEx TYPE y,
y TYPEz);
dnear
(x0,y0,z0)
Prepared By: Bal Krishna Nyaupane Prepared By: Bal Krishna Nyaupane
Hillside College of Engineering
57
Hillside College of Engineering
w/h = aspect58