Chapter 9 - OPEN GL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Hi t

History off OpenGL


O GL What is OpenGL
 OpenGL
p is a software API to ggraphics
p hardware
• Silicon Graphics (SGI) revolutionized the graphics  designed as a streamlined, hardware-independent interface
workstation by implementing the pipeline in hardware (1982) to be implemented on many different hardware platforms
• To access the system
system, application programmers used a library  Intuitive, procedural interface with c binding
called GL
 No windowing commands!
• With GL, it was relativelyy simple
p to pprogram
g three
 No
N hihigh-level
h l l commands d for
f describing
d ibi models d l off three-
th
dimensional interactive applications
dimensional objects
 The OpenGL
p Utilityy Library
y ((GLU)) pprovides many y of the
modeling features, such as quadric surfaces and NURBS
curves and surfaces

Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
1 2
Hillside College of Engineering Hillside College of Engineering

What is OpenGL What is OpenGL


 Graphics
p renderingg API ((Low Level))  Easy
y to use
 High-quality color images composed of geometric and  Close enough to the hardware to get excellent performance
image primitives  Focus on rendering
 Window system independent  O itt d windowing
Omitted i d i andd input
i t to
t avoidid window
i d system t
dependencies
 Operating system independent
 Display
Di l device
d i independent
i d d t
 Generate high-quality color images composed of geometric
and image
g pprimitives

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

GLUT (GL Utility Toolkit) (GL Utility Toolkit)


 Window-system
y independent
p toolkit with numerous utilityy  Code is p
portable but GLUT lacks the functionality
y of a
functions, mostly dealing with user interface good toolkit for a specific platform
 GLUT provide a portable API for creating window and No slide bars, buttons, …
i t
interacting
ti with
ith I/O devices
d i  Handles:
 Provides functionality common to all window systems Window creation,
 Open a window OS system
t calls ll
 Get input from mouse and keyboard Mouse buttons, movement, keyboard, etc…
 Menus
 Event-driven

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

Program Structure Callback functions


 Most OpenGL programs have the following structure Callbacks are user-defined functions designed
g to react on
– main(): specific events:
defines the callback functions Whenever OpenGL decided it needs to redraw window
opens
opens one or more windows with the required properties contents
t t
enters event loop (last executable statement)
– init(): sets the state variables What to do when a user resizes a window.
Vi i
Viewing Handle mouse motions
motions…
Attributes React on keyboard,
– callbacks What to do duringg idle pperiod ((no input
p from user),
),
Display function
Input and window functions

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

Callback Functions Callback Functions


• gglutMouseFunc(myMouse);
( y ); • g
glutPassiveMotionFunc(myPassiveMotionFunc)
( y )
– Handles mouse button presses. Knows mouse location and – Handles case where mouse enters the window with no
nature of button (up or down and which button). buttons pressed.
• glutMotionFunc(myMotionFunc); • glutKeyboardFunc(myKeyboardFunc);
– Handles case when the mouse is moved with one or more – Handles key presses and releases. Knows which key was
mouse buttons pressed.
pressed pressed and mouse location
location.
• glutMainLoop()
– Runs forever waitingg for an event. When one occurs,, it is
handled by the appropriate callback function.

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

Rendering Callback Idle Callback


• Callback function where all our drawing is done • Use for animation and continuous update
• Every GLUT program must have a display callback – Can use glutTimerFunc or timed callbacks for animations
• glutIdleFunc( idle );
• glutDisplayFunc( my_display_func ); /* this part is in main.c
*/
void my_display_func (void ) void idle( void )
{ {
glClear( GL_COLOR_BUFFER_BIT
GL COLOR BUFFER BIT ); //* change something *//
glBegin( GL_TRIANGLE ); t += dt;
glVertex3fv( v[0] ); glutPostRedisplay();
glVertex3fv(( v[1]
g [ ] );
glVertex3fv( v[2] ); }
glEnd();
glFlush();
}
Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
19 20
Hillside College of Engineering Hillside College of Engineering
User Input Callbacks Mouse Callback
 Process user input  Captures mouse press and release events
 gglutKeyboardFunc(
y ( myy_keyy_events );
 glutMouseFunc( my_mouse );
void my_key_events (char key, int x, int y )
{ void
id myMouse(int
M (i t button,
b tt int
i t state,
t t int
i t x, int
i t y))
switch ( key ) { {
case ‘q’ : case ‘Q’ :
exit
i ( EXIT_SUCCESS);
S CC SS) if (button == GLUT_LEFT_BUTTON
GLUT LEFT BUTTON && state ==
break; GLUT_DOWN)
case ‘r’ : case ‘R’ : {
rotate = GL_TRUE; …
break; }
} }
}
Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
21 22
Hillside College of Engineering Hillside College of Engineering

Events in OpenGL Color Models: RGB


Event Example OpenGL Callback Function

 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:

glColor3f(0.5,1.0,0.6); // RGB mode, fully opaque glColor3f(0.5,0.1,0.9);

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.

Note: For grayscale,


grayscale rr=g=b
g b.

Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
25 26
Hillside College of Engineering Hillside College of Engineering

OpenGL RGB Colors OpenGL Color Blending


 To enable blending: glEnable(GL_BLEND);
 To disable blending: glDisable(GL_BLEND);
Color Component Color Common Name  Must set blending function, e.g.
R G B
glEnable(GL_BLEND);
0 0 0 Black glBlendFunc (GL_SRC_ALPHA,
(GL SRC ALPHA GL_ONE_MINUS_SRC_ALPHA);
GL ONE MINUS SRC ALPHA);
0 0 1 Blue // source_factor, destination_factor
0 1 0 Green glColor4f(1.0,0.0,0.0,0.9); // almost opaque
0 1 1 Cyan glLineWidth(5);
1 0 0 Red glBegin(GL_LINES);
1 0 1 Magenta glVertex3f(0.0,0.0,0.0);
1 1 0 Yellow glVertex3f(1.0,1.0,0.0);
1 1 1 White glEnd();

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

Color Values 2D Geometric Primitives

GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP

GL_POLYGON GL_QUADS GL_TRIANGLES GL_TRIANGLE_FAN

All geometric primitives are specified by vertices


Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
31 32
Hillside College of Engineering Hillside College of Engineering
Types
GL_POINTS
Geometry Commands
GL_LINES : each successive pair for a ling segment g
glBegin(GLenum
g ( type)
yp )
GL LINE STRIP vertices
GL_LINE_STRIP: i defining
d fi i a sequence off line
li
segments marks the beginning of a vertex-data list that describes a geometric
GL_LINE_LOOP: GL_LINE_STRIP + the last vertex primitives
connects t to
t the
th first
fi t
GL_POLYGON : sequence of vertices of polygon, filled  glEnd (void)
GL_QUADS: each successive group of four vertices for a
quadrilaterals
d il l marks
k the
h end
d off a vertex-data
d li list
GL_TRIANGLES: each successive group of three vertices
for a triangle  glVertex*(…)
GL_TRIANGLE_FAN: first three vertices for the first specifies vertex for describing a geometric object
triangle and each subsequent vertex with the first vertex
and the previous vertex for the next triangle

Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
33 34
Hillside College of Engineering Hillside College of Engineering

Specif ing Geometric Primitives


Specifying Primiti es Example
void drawSquare ()
{
glBegin( type ); glClear(GL_COLOR_BUFFER_BIT);
glVertex (…);
glVertex*(…); glBegin(GL POLYGON);
glBegin(GL_POLYGON);
…… glVertex2f ( 0.0, 0.0 );
g
glVertex*(…);
( ); glVertex2f ( 1.0, 0.0 );
glEnd(); glVertex2f ( 1.1, 1.1 );
glVertex2f ( 0.0, 1.0 );
glEnd();
lE d()
type determines how vertices are combined glFlush(); // force the renderer to output the results
}

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

Geometric Primitives: Points, Lines Geometric Primitives: Points, Lines


andd P
Polygons
l E
Examplel andd P
Polygons
l E
Examplel
#ifdef __FLAT__ // The display callback function
#include <windows.h>
<windows h> void display(void)
#endif {
#include <gl/glut.h> static float v[] = { 0.1, 0.1 };
glClear(GL_COLOR_BUFFER_BIT) ;
glColor3f(0 0 0.0,
glColor3f(0.0, 0 0 0.0
0 0 ); // Set the point color to black
// The initialization function glPointSize( 3.5 );
void init(void) glBegin( GL_POINTS);
{ glVertex2fv( v );
glutInitWindowSize( glutGet( GLUT
GLUT_SCREEN_WIDTH)/3,
SCREEN WIDTH)/3 glVertex2f( 0.05,
0 05 00.2);
2);
glutGet( GLUT_SCREEN_HEIGHT)/3 ); glVertex2f( 0.05, 0.3);
glutInitWindowPosition( 0, 0 ); glVertex2f( 0.05, 0.4);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glVertex2f( 0.05, 0.5);
glutCreateWindow("Rendering
glutCreateWindow( Rendering Primitives
Primitives")) ; glVertex2f( 0.1,
0 1 0.2);
0 2);
glClearColor(1.0, 1.0, 1.0, 0.0) ; glVertex2f( 0.1, 0.3);
glShadeModel(GL_FLAT) ; glVertex2f( 0.1, 0.4);
} glVertex2f( 0.1, 0.5);
glVertex2i( 0,
0 0 );
glVertex2f( -0.1, -0.1);
Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
39 40
Hillside College of Engineering Hillside College of Engineering
Geometric Primitives: Points, Lines Geometric Primitives: Points, Lines
andd P
Polygons
l E
Examplel andd P
Polygons
l E
Examplel
glBegin( GL_QUADS );
glVertex2f( -0.05,-0.2); glColor3f( 1.0, 0.0, 0.0); // Draw the quad in red
glVertex2f( -0.05,-0.3); glVertex2f( -0.8, -0.8);
glVertex2f( -0.05,-0.4); glVertex2f( -0.5, -0.3);
glVertex2f( -0.2, -0.9);
glVertex2f( -0.05,-0.5);
glVertex2f( -0.4, -1.0);
glVertex2f( -0.1,-0.2);
0 1 0 2); glEnd();
glVertex2f( -0.1,-0.3); glBegin( GL_POLYGON );
glVertex2f( -0.1,-0.4); glColor3f( 0.0, 0.0, 0.0); // Set point color to magenta
gglVertex2f(( -0.1,-0.5);
, ); glVertex2f( -0.9, 0.6);
glVertex2f( -0.8, 0.55);
glEnd();
glVertex2f( -0.7, 0.6);
glVertex2f( -0.6,0.8);
glBegin( GL_LINES ); glVertex2f( -0.85, 0.9);
glColor3f(0.0, 0.0, 1.0); // Set the point color to blue glEnd();
glVertex2f( 0.5, 0.5 ); glColor3f(0.2, 0.9, 0.1);
glRectf(-.1, .6, .3, .9);
glVertex2f( 0.1, 0.1);
glutSwapBuffers();
glEnd(); }

Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
41 42
Hillside College of Engineering Hillside College of Engineering

Geometric Primitives: Points, Lines Geometric Primitives: Points, Lines


andd P
Polygons
l E
Examplel andd P
Polygons
l E
Examplel
// The main function
int main(int argc, char** argv)
{
glutInit(&argc argv) ;
glutInit(&argc,

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

Directional and Positional Lights Creating & Positioning Lights


 when GL_POSITION is passed as an argument to glLight*()
 Directional light source is positioned at infinity four values (x,y,z,w)
(x y z w) are passed as parameters
parameters.
(like the sun). • W determines the type of light we are defining:
0  directional - (x,y,z) is the direction.
 Positional light
g source is positioned
p near the scene •
• 1  positional
i i l - (x,y,z)
( ) is
i the
h position.
ii
and its exact position determines its effect.
 GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION,
GL_QUADRATIC_ATTENUATION.
• These define the attenuation of the light. Usually disabled for directional lights.
 GL
GL_SPOT_DIRECTION,
SPOT DIRECTION GL
GL_SPOT_EXPONENT,
SPOT EXPONENT
GL_SPOT_CUTOFF.
• These define spotlights and spotlight properties.

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

OpenGL viewing General Transformation Commands


• glMatrixMode()
 Modelview transformation – Specifies
S ifi whether
h h the h modelview,
d l i projection,
j i or texture
• Modeling transformation: matrix will be modified, using the argument
model local coordinates  world coordinates GL_MODELVIEW,
_ GL_PROJECTION,
_ or GL_TEXTURE
_
• Viewing transformation: for mode. Subsequent transformation commands affect the
specified matrix. Note that only one matrix can be modified
world coordinates  eye
y coordinates at a time.
time By default,
default the modelview matrix is the one that's
that s
modifiable, and all three matrices contain the identity
matrix.
• void glLoadIdentity(void);
– Sets the currently modifiable matrix to the 4 × 4 identity
matrix.
matrix
Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
51 52
Hillside College of Engineering Hillside College of Engineering
Load and Mult Matrices Viewing transformation
 void glLoadMatrix{fd}(const TYPE *m); m);  Viewingg transformation
• gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z,
• Sets the sixteen values of the current matrix to up.x, up.y, up.z)
those specified
p f byy m. • Viewing direction: center – eye
 void glMultMatrix{fd}(const TYPE *m); • Up is the upward direction
• Viewing direction and up vector  eye coordinate system
• Multiplies the matrix specified by the sixteen  X axis
i points
i t to
t the
th right
i ht off viewer
i
values pointed to by m by the current matrix and  Y axis points upward
stores the result as the current matrix.  Z axis points to the back of viewer
• Generate a matrix, which is postmultiplied to the top-of-
the-stack matrix on the Modelview stack
• Thus, must be called before anyy modeling g transformations

Prepared By: Bal Krishna Nyaupane             Prepared By: Bal Krishna Nyaupane            
53 54
Hillside College of Engineering Hillside College of Engineering

gluPerspective Viewport Transformation


void glViewport(GLint x, GLint y, GLsizei width,
voidd gluPerspective(GLdouble
( d bl fovy, f GLdouble
d bl aspect,
GLdouble near, GLdouble far); GLsizei height);
Creates a matrix ffor a symmetric
y perspective-view
p p ffrustum Defines a pixel rectangle in the window into which
and multiplies the current matrix by it. fovy is the angle of the final image is mapped. The (x, y) parameter
the field of view in the x-z plane; its value must be in the specifies the lower-left corner of the viewport, and
range [0.0,180.0].
[0 0 180 0] aspect is the aspect ratio of the frustum,
frustum width
idth andd height
h i ht are the
th size
i off the
th viewport
i t
its width divided by its height. near and far values the
rectangle. By default, the initial viewport values
distances between the viewpoint and the clipping planes,
along
l the
h negative z-axis. They
Th should
h ld always
l be
b positive are (0,
(0 0,
0 winWidth,
winWidth winHeight),
winHeight) where winWidth
and winHeight are the size of the window.

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

You might also like