1 91.427 Computer Graphics I, Fall 2010 OpenGL Transformations.
Review 1 ====opengl I
-
Upload
tarekhemdan -
Category
Documents
-
view
213 -
download
0
Transcript of Review 1 ====opengl I
-
8/20/2019 Review 1 ====opengl I
1/46
OpenGL
Comp 575 - Fall 2008
-
8/20/2019 Review 1 ====opengl I
2/46
2
Input in OpenGL
• You tell OpenGL which user-defined function to callwhen certain events occur.
• OpenGL monitors the input devices. When
something happens (mouse down, e! press, etc. "
OpenGL calls !our code.• #an provide functions for
• window resi$ing
• mouse %utton presses
• mouse %utton releases
• mouse motion
• e!%oard presses
-
8/20/2019 Review 1 ====opengl I
3/46
&
Window Resizing
void glutReshapeFunc(void (*func)(int w, int h))• func is a pointer to a function that takes two arguments,
the new width w and new height h of the window.OpenGL calls this function whenever the window is
resized.• func typically makes a call to glViewport() so that
the display is clipped to the new size, and redefines the
projection matrix so that the aspect ratio of the projected
image matches the viewport.
-
8/20/2019 Review 1 ====opengl I
4/46
'
Mouse !ents
void glutMouseFunc(void (*func)(int button, int state,
int x, int y));
•
func is a pointer to a function that takes 4 integerparameters. func is called by OpenGL whenever a mousebutton is pressed or released.
• button specifies which mouse button was pressed, and iseither:
GLUT_LEFT_BUTTON,
GLUT_RG!T_BUTTON, or
GLUT_#$$LE_BUTTON
-
8/20/2019 Review 1 ====opengl I
5/46
Mouse !ents
• %t&te specifies the state of the mouse button, andis either:
GLUT_U', or
GLUT_$ON
• and* specify the location (in window-
relative coordinates) of the mouse when theevent occurred.
-
8/20/2019 Review 1 ====opengl I
6/46
)
Mouse Motion
glutMotionFunc( void (*func)(int x, int y));
• func is called by OpenGL when the mouse pointer moves
within the window while one or more mouse buttons are
pressed.• and* specify the location of the mouse when theevent occurred.
-
8/20/2019 Review 1 ====opengl I
7/46
*
"e#$oa%d !ents
void glutKeyboardFunc(void (*func)(unsigned char ey,
int x, int y));
• func is a pointer to a function that takes 3 parameters.func is called by OpenGL whenever a key is pressed.
• +e* is the ASCII value of the key that was pressed
• and* specify the location of the mouse when thekey was pressed.
-
8/20/2019 Review 1 ====opengl I
8/46
+
Idle Fun&tion
glut!dleFunc( void (*func)());
• func is called by OpenGL when no other events are
pending, when it would otherwise be idle.
• Pass in Null (0) to disable the function.
-
8/20/2019 Review 1 ====opengl I
9/46
Example Code using OpenGL Input
int &in( int &rgc, ch&r-- &rg.)/
glutnit(0&rgc, &rg.)1
glutnit$i%pl&*#o2e( GLUT_RGB 33 GLUT_4NGLE )1
glutnitin2ow4i5e ( 678, 678 )1
glutnitin2ow'o%ition ( 988, 988 )1
glut:re&tein2ow (&rg.;8
-
8/20/2019 Review 1 ====opengl I
10/46
'ample Code (sing OpenGLInput.oi2 2i%pl&* ( .oi2 )/
gl:le&r ( GL_:OLOR_BUFFER_BT )1
gl:olor?f (9@8, 9@8, 9@8)1
glBegin (GL_'ONT4)1glVerte?f (8@7, 8@67, 8@8)1
glVerte?f (8@A7, 8@7, 8@8)1
glVerte?f (8@A7, 8@A7, 8@8)1
glVerte?f (8@67, 8@A7, 8@8)1glEn2()1
glFlu%h()1
>
-
8/20/2019 Review 1 ====opengl I
11/46
'ample Code (sing OpenGLInput.oi2 re%h&pe ()/
c&lle2 when win2ow i% re%i5e2@
t*pic&ll* %et .iewport,
%et proCection &tri, etc@>
-
8/20/2019 Review 1 ====opengl I
12/46
2
'ample Code (sing OpenGLInput.oi2 ou%e (int button, int %t&te, int , int *)/
%witch (button)
c&%e GLUT_LEFT_BUTTOND
if (%t&te GLUT_$ON) 2o %oething
c&%e GLUT_RG!T_BUTTOND
etc@, etc@
>
-
8/20/2019 Review 1 ====opengl I
13/46
&
'ample Code (sing OpenGLInput.oi2 otion (int , int *)/
2o wh&te.er i% &ppropri&te,
e@g@, o.e obCect to point (, *)
>
-
8/20/2019 Review 1 ====opengl I
14/46
'
'ample Code (sing OpenGLInput.oi2 +e*bo&r2(un%igne2 ch&r +e*, int , int *)
/
%witch (+e*) /
c&%e %D
2o %oething
bre&+1
c&%e 4D
2o %oething here too
bre&+1c&%e tD
c&%e TD f *ou 2ont w&nt to be c&%e %en%iti.e@
2o %oething here too
bre&+1
@
@
@
>>
-
8/20/2019 Review 1 ====opengl I
15/46
Matrix Operations
•OpenGL has 4 matrices it uses:•GL_MODELVIEW
•contains the composite modeling and viewing matrices•GL_PROJECTION
•contains a matrix for the projection transformation
•GL_TEXTURE•used for performing operations on the texture (stretching,moving, rotating, etc.)
•GL_COLOR•used for color space conversions
• /ae sure the appropriate matri0 is current when !ou domodeling, viewing, or pro1ections
-
8/20/2019 Review 1 ====opengl I
16/46
)
Setting the Current Matrix
.oi2 gl#&tri#o2e(Glenu o2e)1
• sets the current matri0 to %e o2e
•
o2e is GL_#O$ELVE, GL_'ROHE:TON,GL_TEITURE, or GL_:OLOR
-
8/20/2019 Review 1 ====opengl I
17/46
*
Loading the Current Matrix
.oi2 glLo&2#&tri/f2>( TJ'E -)
• loads the arra! of TJ'E GLflo&t or GL2ouble to
the current matri0
-
8/20/2019 Review 1 ====opengl I
18/46
+
Multiplying the Current Matrix
.oi2 gl#ult#&tri/f2>(TJ'E -)
• ostmultiplies the current matri0 %! , which is of
TJ'E GLflo&t or GL2ouble
-
8/20/2019 Review 1 ====opengl I
19/46
Transformations
•Translation:
.oi2 glTr&n%l&te/f2>( TJ'E 2, TJ'E 2*,TJ'E 25)
• TJ'E is GLflo&t or GL2ouble
• 3he translation is applied to the current matri0 4 maesure the appropriate matri0 is current, e.g.,
gl#&tri#o2e(GL_#O$ELVE)1 glTr&n%l&te(8@?, 8@6, 9@7)1
• 5istances are in world coordinates
-
8/20/2019 Review 1 ====opengl I
20/46
2
Transformations
•Rotation:
.oi2 glRot&te/f2>(TJ'E &ngle, TJ'E 2,
TJ'E 2*, TJ'E 25)
• rotates &ngle a%out the a0is given %! (2, 2*,
25) and the origin
• TJ'E is GLflo&t or GL2ouble
-
8/20/2019 Review 1 ====opengl I
21/46
2
Transformations
•Scale:
.oi2 gl4c&le/f2>( TJ'E %, TJ'E %*, TJ'E
%5)
• scales a%out the origin %! (%, %*, %5)
• TJ'E is GLflo&t or GL2ouble
-
8/20/2019 Review 1 ====opengl I
22/46
22
Viewing - Parallel
•
Setting an orthographic view gl"rtho(#$double left, #$double right,
#$double botto%, #$double top,
#$double &'ear, #$double &Far) • produces a parallel projection with the clipping volume being
• ;left, right
-
8/20/2019 Review 1 ====opengl I
23/46
2&
Viewing - Perspective
• Setting a perspective view gluerspective (#$double fovy, #$double aspect
#$double near, #$double far)
• fo.* is the angle of the field of view in thexz plane,and must bebetween 0 and 180.
• &%pect is the aspect ratio (width / height)• ne&r is the distance from the viewpoint to the nearclipping plane (always positive).
• f&r is the distance from the viewpoint to the farclipping plane (always positive).
-
8/20/2019 Review 1 ====opengl I
24/46
2'
Camera Set-Up
•Used to specify camera position and parametersvoid glu$oo.t( #$double eyex, #$double eyey,#$double eye&, #$double atx, #$double aty, #$doubleat&, #$double upx, #$double upy, #$double up&)
3he e*e point (e*e, e*e*, e*e5) is the loo6rom point3he &t point (&t, &t*, &t5) is the loo7t point3he up point (up, up*, up5) is the 8iew 9p 8ector3his call creates the appropriate matri0 and applies it to the current
matri0. :ence, we need to set the current matri0 mode;
glMatrixMode(#$M"/+$0!+1); gl$oad!dentity(); glu$oo.t(232, 232, 432, 232, 232,
232, 232, 432, 232);
-
8/20/2019 Review 1 ====opengl I
25/46
2
Creating a Normal Vector
gl'or%al56dfi7(-8+ nx, -8+ ny, -8+ n& )
• TJ'E i% GL2ouble, GLflo&t, or GLint• (n0, n!, n$" are the coordinates of the normal vector
• ?@#7L?=AO>/7L is ena%led, normals do nothave to %e unit si$e. 3he s!stem will normali$e them for!ou.
• Aormali$ation is disa%led %! default• #an ena%le %! calling either glEn&ble(GL_NOR#KLE)
or glEn&ble(GL_RE4:KLE_NOR#KL)
-
8/20/2019 Review 1 ====opengl I
26/46
2)
Setting the Normals
glBegin (GL_'OLJGON)1glNor&l?f.(n8)1
glVerte?f.(.8)1
glNor&l?f.(n9)1glVerte?f.(.9)1
glNor&l?f.(n6)1
glVerte?f.(.6)1glEn2()1
-
8/20/2019 Review 1 ====opengl I
27/46
-
8/20/2019 Review 1 ====opengl I
28/46
2+
Lighting
3o set the value of a light source parameter;
gl$ight6if7v(#$enu% light, #$enu% pna%e,-8+ para%s )
• light is the light num%er of the form #$$!#9-i,
where B i C GL=/7D=LGE7"GL_$FFU4E (>GE7"GL_4'E:ULKR (>GE7" GL_'O4TON (0, !, $, w" 4 if w F , it is a directional light
GL_4'OT_$RE:TON (0, !, $"
• para%s is a pointer to an arra! that contains the data
-
8/20/2019 Review 1 ====opengl I
29/46
2
Lighting
3o set the am%ient lighting in the scene;
gl$ightModel6if76v7(
#$enu% #$$!#9-M"/+$.M:!+'-,
-8+ para%s )
• para%s contains a pointer to ' int or float values that
specif! the am%ient >GE7 intensit! of the entire scene.
-
8/20/2019 Review 1 ====opengl I
30/46
&
Shading
3o set the shading t!pe for the scene;glhadeModel (#$enu% %ode )
• o2e specifies the t!pe of shading to %edone, and is either
GL_FLKT
GL_4#OOT! (Gouraud shading"
3he default is GL_4#OOT!
-
8/20/2019 Review 1 ====opengl I
31/46
&
Setting Material Properties
3o set the material properties for the current material;
gl#&teri&l/fi>/.>(GLenu f&ce, GLenu
pn&e, TJ'E p&r&% "
•
f&cespecifies which face the properties should %e
applied to and is;
GL_FRONT
GL_BK:=
GL_FRONT_KN$_BK:=
GL_BK:= is used for shading %ac-facing
pol!gons when two-sided lighting is ena%led
-
8/20/2019 Review 1 ====opengl I
32/46
&2
Setting Material Properties• pna%e specifies which parameter is to %e set and is;
GL_K#BENT• 3he am%ient reflectance of the material. (>GE7"
GL_$FFU4E• 3he diffuse reflectance of the material. (>GE7"
GL_4'E:ULKR• 3he specular reflectance of the material. (>GE7"
GL_E#44ON• 3he emitted light intensit! of the material. (>GE7"
GL_4!NNE44• 3he specular e0ponent of the material. (int or float"
-
8/20/2019 Review 1 ====opengl I
33/46
&&
Texture Mapping
• Steps in using textures in OpenGL;
1. Create a texture.
2. Indicate how the texture is to be applied to eachpixel@
3. Enable texture mapping@
4. Draw the scene, specifying both texture and
geometric coordinates.
-
8/20/2019 Review 1 ====opengl I
34/46
&'
Creating a Texture
gl-ex!%age
-
8/20/2019 Review 1 ====opengl I
35/46
&
Naming a Texture Object
-
8/20/2019 Review 1 ====opengl I
36/46
&)
)etting t*e +e'tu%en!i%onment.oi2 glTeEn.i (GLenu t&rget, GLenupn&e, GLint p&r&)1
•This call sets the current texturing environment
parameters • t&rget should beGL_TEITURE_ENV • pn&e should beGL_TEITURE_ENV_#O$E • p&r& should beGL_$E:KL • Other texturing functions are available - look at an
OpenGL book for details
• These are probably the parameters you want.
-
8/20/2019 Review 1 ====opengl I
37/46
&*
)etting t*e +e'tu%e,a%amete%s.oi2 glTe'&r&eteri (GLenu t&rget,GLenu pn&e, GLint p&r&)1
•This call sets the current texturing parameters
• t&rget should beGL_TEITURE_6$ • pn&e should beGL_TEITURE_RK'_4 orGL_TEITURE_RK'_T
•
p&r& should be GL_RE'EKT • Other texturing functions are available, but these are
probably the parameters you want.
-
8/20/2019 Review 1 ====opengl I
38/46
&+
+e'tu%e Coo%dinates
When !oudraw an object, you need to specify bothgeometriccoordinates and texture coordinates for eachvertex.
Textures are interpolated between vertices.
void gl-exoord
-
8/20/2019 Review 1 ====opengl I
39/46
&
'ample o (sing +e'tu%es
.oi2 init (.oi2)/
glGenTeture%(9, 0teN&e)1
glBin2Teture(GL_TEITURE_6$, teN&e)1
glTe'&r&eteri(GL_TEITURE_6$,GL_TEITURE_RK'_4, GL_RE'EKT)1
glTe'&r&eteri(GL_TEITURE_6$,
GL_TEITURE_RK'_T, GL_RE'EKT)1
glTe&ge6$(GL_TEITURE_6$, GL_RGB,wi2th,height, 8, GL_RGB, GL_NT,te&ge)1
>
-
8/20/2019 Review 1 ====opengl I
40/46
'
'ample o (sing +e'tu%es
.oi2 2i%pl&* (.oi2)/
glEn&ble(GL_TEITURE_6$)1
glTeEn.i(GL_TEITURE_ENV,
GL_TEITURE_ENV_#O$E, GL_$E:KL)1glBin2Teture(GL_TEITURE_6$, teN&e)1
$r&wMu&2()1
glFlu%h()1
gl$i%&ble (GL_TEITURE_6$)1
>
-
8/20/2019 Review 1 ====opengl I
41/46
'
'ample o (sing +e'tu%es
.oi2 $r&wMu&2(.oi2) /
glBegin(GL_MUK$4)1glTe:oor26f (8@8, 8@8)1glVerte?f (6@8, 9@8, 8@8)1glTe:oor26f (8@8, 9@8)1glVerte?f (6@8, 9@8, 8@8)1glTe:oor26f (9@8, 9@8)1glVerte?f (8@8, 9@8, 8@8)1glTe:oor26f (9@8, 8@8)1glVerte?f (8@8, 9@8, 8@8)1glTe:oor26f (8@8, 8@8)1glVerte?f (9@8, 9@8, 8@8)1glTe:oor26f (8@8, 9@8)1glVerte?f (9@8, 9@8, 8@8)1glTe:oor26f (9@8, 9@8)1glVerte?f (6@9, 9@8, 9@@9)1glTe:oor26f (9@8, 8@8)1glVerte?f (6@9, 9@8, 9@9)1glEn2()1
>
-
8/20/2019 Review 1 ====opengl I
42/46
'2
.e%te' /%%a#s
• /ost vertices appear in multiple pol!gons.• @i0 t!pes of arra!s !ou can activate;
• GL_VERTEI_KRRKJ• GL_:OLOR_KRRKJ• GL_N$EI_KRRKJ• GL_NOR#KL_KRRKJ• GL_TEITURE_:OOR$_KRRKJ•
GL_E$GE_:OOR$_KRRKJ• 7ctivate %! calling
• glEn&ble:lient4t&te(GL_VERTEI_KRRKJ)
-
8/20/2019 Review 1 ====opengl I
43/46
'&
)pe&i#ing t*e ata
%t&tic Glint .ertice%;1
4t&tic GLflo&t color%;1
gl:olor'ointer(?, GL_FLOKT, 8, color%)1
glVerte'ointer(6, GL_NT, 8, .ertice%)1
.oi2 glVerte'ointer(Glint size, GLenu type, GL%i5ei stride,
con%t GL.oi2 -pointer)1
size is t*e num$e% o &oo%dinates1 t#pe spe&ies t*e data t#pe1
st%ide is t*e $#te o3set $etween !e%ti&es1 pointe% is t*e a%%a# wit* t*edata4
-
8/20/2019 Review 1 ====opengl I
44/46
''
(sing t*e /%%a#
glBegin(GL_TRKNGLE4)1
glKrr&*Eleent(8)1
glKrr&*Eleent(9)1
glKrr&*Eleent(?)1
glEn2()1
+*is does t*e same t*ing as
glBegin(GL_TRKNGLE4)1
gl:olor?f. (color% (8-?-%i5eof(GLflo&t)))1
glVerte?f.(.ertice%(8-?-%i5eof(GLint)))1
gl:olor?f. (color% (9-?-%i5eof(GLflo&t)))1
glVerte?f.(.ertice%(9-?-%i5eof(GLint)))1
gl:olor?f. (color% (?-?-%i5eof(GLflo&t)))1
glVerte?f.(.ertice%(?-?-%i5eof(GLint)))1
glEn2()1
-
8/20/2019 Review 1 ====opengl I
45/46
'
(sing t*e /%%a#
%t&tic GLub*te in2ice% /8,9,?,8,9,6>1
gl$r&wEleent%(GL_TRKNGLE4,S,GL_UN4GNE$_BJTE,in2ice%)1
.oi2 gl$r&wEleent%(GLenu o2e, GL%i5ei count,GLenu t*pe, .oi2 - in2ice%)1
mode is the wa! !ou are drawing the elements.
count is the num%er of elements in the arra! of indices.
t!pe is the data t!pe of the indices arra!.indices is the arra! of indices.
5o not put gl5raw?lements in %etween a glEegin gl?nd pair.
-
8/20/2019 Review 1 ====opengl I
46/46
/nimation
•
Use double-buffer mode.•Render to one buffer, then to the other, swapping each time.
.oi2 2i%pl&* ( .oi2 )/gl:le&r (GL_:OLOR_BUFFER_BT)1
2r&w obCect% here
glut4w&pBuffer%()1>
int &in ()
/glutnit$i%pl&*#o2e (GLUT_$OUBLE 3 GLUT_RGB)1
>