You are on page 1of 19

OpenGL

3D Graphics & View


3D Graphic Coordinate System
The 3D graphics coordinate system used by OpenGL is as illustrated:

Notes:

The y-axis is NOT inverted. !-axis is inverted in most o" the #D graphics coordinate system $ith the origin at the top-le"t corner% such as &indo$s and 'ava #D Graphics (ystems.) The xy* axes "ollo$ the right-hand rule% as in the +artesian coordinate system.

OpenGL Geometric Primitives


Geometric primitives are the basic entities that $e can use in our graphics applications. OpenGL supports three types o" geometric primitives: points% line segments% and closed polygons. They are speci"ied via vertices. ,ach vertex is associated $ith its properties or attributes% such as the color% si*e% and thic-ness. To create a geometric ob.ect or model% $e use a pair o" glBegin(PrimitiveType) and
glEnd() to enclose the vertices that "orm the model. /or primitiveType that ends $ith 0 S0 e.g.% GL_TRIANGLES)% $e can de"ine multiple shapes o" the same type% $here each set o" re1uired

vertices de"ine a shape. OpenGL provides 23 primitive types as illustrated as "ollo$s:

Points
GL_POINTS: ,ach vertex de"ines a point. glPointSize can be used to set the si*e. void glPointSize(GLflo t !izeInPi"el!) ## $%ere !ize i! me !&red in pi"el!' $it% def &lt of ()*)

Line Segments
Three types:
GL_LINES: ,ach successive pair o" vertices speci"ies a line segment. GL_LINE_STRIP: /irst pair o" vertices speci"ies the "irst line segment. The end point o"

the previous line segment and the next vertex "orm the next line segment% and so on.
GL_LINE_LOOP: (imilar to GL_LINE_STRIP% except that the last vertex is .oined to the

"irst vertex to "orm a closed loop. The "ollo$ing "unctions can be used to set the line0s attributes:

glLine+idt%: (et the $idth o" the line segment in pixels de"ault is 2.3). void glLine+idt%(GLflo t $idt%InPi"el!)

Closed Polygons
4 polygon is associated $ith a surface normal. !ou can use the right-hand rule to determine the direction o" the surface normal% based on the order o" vertices. /or example% i" the polygon "alls on the x-y plane% and the vertices are listed in counter-cloc-$ise% the normal is pointing out o" the screen i.e.% 5* axis). There are 6 "illed polygonal primitives:
GL_POL,GON: The vertices "orm one closed polygon. GL_TRIANGLES: ,ach group o" 3 vertices de"ines a triangle. GL_TRIANGLE_STRIP: The "irst three vertices de"ine the "irst triangle. The last t$o

vertices o" the previous triangle plus the next vertex "orm the next triangle% and so on. The order o" vertices are -*-(-.% -.-(-/ ta-e note o" the order)% -.-/-0% and so on% to ensure consistent sur"ace normal.
GL_TRIANGLE_1AN: The "irst three vertices de"ine the "irst triangle. The "irst vertex% the

last vertex o" the previous triangle% and the next vertex in the list "orm the next triangle% and so on. The order o" vertices are -*-(-.% -*-.-/% -*-/-0% and so on% to ensure consistent sur"ace normal.
GL_23A4S: ,ach group o" 7 vertices de"ine a 1uad. GL_23A4_STRIP: The "irst 7 vertices "orm the "irst 1uad. The last # vertices o" the

previous 1uad plus the next # vertices "rom the list "orm the next 1uad% and so on. The order o" vertices are -*-(-/-.% -.-/-5-0% -0-5-6-7% and so on% to ensure consistent sur"ace normal. Ta-e note that circle and sphere are not a primitive in OpenGL. !ou could create a circle via triangles% or use high-level utilities in GL8 or GL8T.

Model, View, Projection & Viewport


The process used to produce a scene in OpenGL is li-e ta-ing a photograph $ith a camera. 9t involves "our trans"ormations:

1. 2. 3. 4.

4rrange the ob.ects or models in the $orld Model trans"ormation). :osition and orientate the camera View trans"ormation). (elect a camera lens $ide angle% normal or telescopic)% ad.ust the "ocus length and *oom "actor to choose the camera0s "ield o" vie$ Projection trans"ormation). (elect the portion o" negative trans"ormation). or photograph) to be printed or sho$n Viewport

9n +omputer Graphics% moving the ob.ects relative to a "ixed camera Model trans"orm)% and moving the camera relative to a "ixed ob.ect View trans"orm) produce the same image% and there"ore are e1uivalent. OpenGL% there"ore% combines the ;odel trans"orm and <ie$ trans"orm into Model-View trans"orm% managed by a so-called Model-View matrix. :ro.ection trans"ormation is carry out via a Projection matrix. <ie$port trans"orm is speci"ied by the gl-ie$port command.

Position and Orientation of the Camera


(ix parameters are needed to speci"y the camera0s position x% y% *) and orientation angle-x% angle-y% angle-*). =o$ever% instead o" using 6 parameters% OpenGL uses a >-parameter
gl&Loo8At command% $hich speci"ies:

1. 2.
3.

an ?eye? point or camera0s x% y% *) position% an ?at? point $here the camera is aiming at usually the center o" the model)% and an ?up? vector to orientate the up$ard direction o" the camera.

void gl&Loo8At(GLdo&9le "Eye' GLdo&9le yEye' GLdo&9le zEye' GLdo&9le "At' GLdo&9le yAt' GLdo&9le zAt' GLdo&9le "3p' GLdo&9le y3p' GLdo&9le z3p)

The camera is positioned at the ?eye? point xEye% yEye% Eye)% aiming at the ?at? point x!t% y!t% !t). The projection plane can be determined by these t$o points% $hich acts as its sur"ace normal. =o$ever% ?eye? and ?at? alone does not speci"y the camera0s orientation% as the camera is still "ree to rotate $ithin the pro.ection plane. 4 third parameter - an ?up? vector is needed. The pro.ection o" the ?up? vector onto the pro.ection plane% speci"ies the up$ard orientation o" the camera. Notice that the > parameters actually produce 6 degrees o" "reedom to position and orientate the camera.

The de"ault setting o" gl&Loo8At is:


gl&Loo8At(*)*' *)*' *)*' *)*' *)*' :(**)*' *)*' ()*' *)*)

The camera is position at the origin 3% 3% 3)% aimed into the screen negative *-axis)% and "aced up$ards positive y-axis).

Projection
Once the camera is positioned and oriented% $e need to decide $hat it can see analogous to choosing the camera0s "ield o" vie$ by ad.usting the "ocus length and *oom "actor)% and ho$ the ob.ects are projected onto the screen. This is done by selecting a pro.ection orthographic or perspective) and speci"ying a vie$ing volume or clipping volume. Ob.ects outside the clipping volume cannot be seen clipped out o" the scene). 9n real $orld% $e can see everything in "ront o" the camera till in"initely "ar a$ay. 9n +omputer Graphics% $e need to set a "ront clipping plane as $ell as a bac- clipping plane.

Orthographic Projection vs Perspective Projection


OpenGL support t$o types o" pro.ections: orthographic pro.ection and perspective pro.ection. The "ollo$ing diagram illustrates the orthographic projection or parallel projection)% $hich is a special case $here the camera is placed very "ar a$ay "rom the model or telescopic lens is used. The clipping volume "or orthographic pro.ection is a parallelepiped.

The de"ault 3D pro.ection in OpenGL is the orthographic $ith parameters :()*% ()*% :()*% ()*%
:()*% ()*)% i.e.% a cube $ith sides o" .)*% centered at origin.

9n the perspective projection as illustrated in the "ollo$ing diagram)% a "ar ob.ect appears smaller% $hich corresponds better to the real-$orld camera imaging and =uman <isual (ystem =<(). The clipping volume is a "rustum truncated pyramid).

Orthographic Projection
!ou can use glOrt%o command to choose the orthographic pro.ection and speci"y its clipping volume:
void glOrt%o(GLdo&9le "Left' GLdo&9le "Rig%t' GLdo&9le yBottom' GLdo&9le

yTop' GLdo&9le zNe r' GLdo&9le z1 r)

The coordinates o" the clipping volume are relative to the camera0s ?eye? position. 9n orthographic vie$% both zNe r and z1 r could be negative% i.e% the camera is positioned in "ront o" the "ront and possibly bac-) clipping plane. 9n this case% ho$ever% ob.ects placed behind the camera $ill not be sho$n. /or #D graphics% you can use gl&Ort%o.4 GL8 command instead o" GL) to choose #D orthographic pro.ection and set its clipping area:
void gl&Ort%o.4(GLdo&9le "Left' GLdo&9le "Rig%t' GLdo&9le yBottom' GLdo&9le

yTop)

Perspective Projection
There are t$o commands "or choosing the perspective pro.ection and setting its clipping volume:

1.
.) /) 0) 5)

;ore commonly-used GL8 command gl&Per!pe;tive:


void gl&Per!pe;tive(GLdo&9le fovy' GLdo&9le

!pe;tR tio' GLdo&9le


nd top of t%e pro<e;tor!=

zNe r' GLdo&9le z1 r)


## fovy i! t%e ## ngle 9et$een t%e 9ottom

!pe;tR tio i! t%e r tio of $idt% l!o 9 ;8) ;lipping pl ne=


## zNe r nd z1 r !pe;ify t%e front

nd %eig%t of t%e front ( nd nd 9 ;8 ;lipping pl ne!)

6.
6) >) ?)

+ore GL command gl1r&!t&m:


void glOrt%o(GLdo&9le "Left' GLdo&9le "Rig%t' GLdo&9le yBottom' GLdo&9le yTop' GLdo&9le zNe r' GLdo&9le z1 r) ## "Left' "Rig%t' yBottom pl ne) ## zNe r nd z1 r !pe;ify t%e po!ition! of t%e front nd 9 ;8 ;lipping pl ne!) nd yTop !pe;ifie! t%e front ;lipping

The coordinates o" the clipping volume are relative to the camera0s ?eye? position. 9n perspective vie$% both zNe r and z1 r must be in "ront o" the camera% i.e.% positive values% and z1 r @ zNe r. The clipping volume set by gl1r&!t&m could be non-symmetrical $hich is unusual). Nate @obin0s OpenGL tutor has an excellent animated program called ?pro.ection? to illustrate the parameters o" gl&Loo8At and gl&Per!pe;tive.

Viewport
<ie$port is the display area on the OpenGL0s #D application $indo$% $hich is measured in pixels in &indo$0s coordinates $ith origin at the top-le"t corner). The vie$port de"ines the si*e and shape o" the display area to map the pro.ected scene captured by the camera onto the application $indo$% as illustrated:

Ay de"ault% the vie$port is set to cover the entire OpenGL0s application $indo$. !ou can use
gl-ie$port command to choose a smaller area

e.g.% "or split-screen or multi-screen

application).

void gl-ie$port(GLint "TopLeft' GLint yTopLeft' GL!izei $idt%' GL!izei

%eig%t)

(uppose the the the vie$port is 3% 3% 673% 7B3)% then the top-le"t corner o" the pro.ection plane maps to 3% 3) in the vie$port% bottom-right to 63>% 7C>)% and so on. 9t is obvious that i" the aspect ratio "or the pro.ection plane set via glOrt%o% gl&Per!pe;tive) and the vie$port set via gl-ie$port) are not the same% the shapes $ill be distorted. The gl-ie$port command should be included in re!% ped() handler% so as to re-si*e the vie$port $henever the OpenGL0s $indo$ is re-si*ed. 9t is important that the aspect ratio o" the pro.ection clipping volume is re-computed to match the vie$port0s aspect ratio% in order not to distort the shapes.
## A ll9 ;8 $%en t%e OpenGLB! $indo$ i! re:!ized) void re!% pe(GL!izei $idt%' GL!izei %eig%t) C integer if (%eig%t DD *) %eig%t D (= 9y * GLflo t r tio ## Set t%e vie$port (di!pl y ppli; tion $indo$ gl-ie$port(*' *' $idt%' %eig%t)= ## Ad<&!t t%e vie$port glE tri"Eode(GL_PROFEATION)= ## Sele;t Pro<e;tion m tri" !pe;t r tio of pro<e;tionB! ;lipping vol&me to m t;% t%e re on t%e $indo$) to ;over t%e $%ole !pe;t D (GLflo t)$idt% # (GLflo t)%eig%t= ## Aomp&te !pe;t ## To prevent divide ## GL!izei for non:neg tive

glLo dIdentity()=

## Re!et t%e Pro<e;tion m tri"

## Eit%er Gper!pe;tive pro<e;tionG or Gort%ogr p%i; pro<e;tionG' NOT 9ot% ## /4 Per!pe;tive Pro<e;tion (fovy' ; mer B! eye po!ition gl&Per!pe;tive(05)*' !pe;t' *)(' (**)*)= !pe;t' zNe r' z1 r)' rel tive to

## /4 Ort%ogr p%i; Pro<e;tion ("Left' "Rig%t' yBottom' yTop' zNe r' z1 r)' ## rel tive to ; mer B! eye po!ition) if ($idt% HD %eig%t) C glOrt%o(:()*' ()*' :()* # !pe;t HD ( I el!e C glOrt%o(:()* J !pe;t @ ( I ## Re!et t%e Eodel:-ie$ m tri" glE tri"Eode(GL_EO4EL-IE+)= glLo dIdentity()= I !pe;t' ()* J !pe;t' :()*' ()*' :()*' ()*)= ## !pe;t' ()* # !pe;t' :()*' ()*)= ##

Projection Matrix and Model-View Matrix


9n OpenGL% pro.ection e.g.% perspective or orthographic) is carried out via a arojection matrix. ;odel trans"orm e.g.% rotation% translation) and camera vie$ trans"orm e.g.% gl&Loo8At) are combined and carried out via a model-view matrix. &e need to "irst select the matrix "or manipulation via:
void glE tri"Eode(GLen&m m tri") ## Sele;t m tri" for m nip&l ting' e)g)' GL_PROFEATION' GL_EO4EL-IE+)

&e can reset the currently selected matrix via:


void glLo dIdentity(void)

&e can save the value o" the currently selected matrix and restore it bac- via:
void glP&!%E tri"(void) void glPopE tri"(void)

:ush and pop use a stac- and operate in a last-in-"irst-out manner% and can be nested.

!"#$$er and %idden!S#r$ace &emoval


9

OpenGL uses a -buffer or depth-buffer) to remove hidden sur"aces sur"aces bloc-ed by other sur"aces and cannot be seen "rom the camera). *-bu""er is an extra storage that stores depth in"ormation about an ob.ect. To use *-bu""er% $e need to:

1.
.) /)

@e1uest "or *-bu""er via gl&tInit4i!pl yEode:


gl&tInit4i!pl yEode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4EPTL)= ## GL3T_4EPTL to reM&e!t for dept%:9&ffer

7.
5)

,nable *-bu""er "or depth testing:


glEn 9le(GL_4EPTL_TEST)=

6.
6)

+lear the *-bu""er $hen $e clear the color bu""er:


glAle r(GL_AOLOR_B311ER_BIT K GL_4EPTL_B311ER_BIT)= nd dept% 9&ffer! ## Ale r ;olor

State Mana'ement
OpenGL operates as a state machine. 9t maintains many states and state variables% such as hidden sur"ace removal% lighting% texture% etc. Ay de"ault% many o" these states are disabled and initial inactive. !ou could use glEn 9le and
gl4i! 9le commands to set a particular state active or inactive. void glEn 9le(GLen&m !t te) void gl4i! 9le(GLen&m !t te)

!ou can chec- $hether a state is enabled or disabled% via:


GL9oole n glI!En 9led(GLen&m !t te) ## Ret&rn! GL_TR3E or GL_1ALSE)

/or example%
glEn 9le(GL_4EPTL_TEST)= ## En 9le dept%:9&ffer for %idden !&rf ;e remov l

()ample* + rotatin' color c#,e and pyramid -.ehe/s Lesson 01

10

#J J Ne%e Le!!on N5 (GL3T)O /4 S% pe! : Rot ting ;olor ;&9e J# Nin;l&de Hgl#gl&t)%@ ## Glo9 l - ri 9le! ;% r titlePQ D GNeLe Le!!on N5O /4 S% pe!G= int $indo$+idt% int $indo$Po!R int $indo$Po!, D 70*= D 5*= D 5*= int $indo$Leig%t D 0>*= ## +indo$ed modeB! title ## +indo$ed modeB! $idt% ## +indo$ed modeB! %eig%t ## +indo$ed modeB! top:left ;orner " ## +indo$ed modeB! top:left ;orner y ## Al!o in;l&ded gl)% nd gl&)% nd pyr mid)

9ool f&llS;reenEode D tr&e= ## 1&ll:!;reen or $indo$ed modeS ## Anim tion GLflo t GLflo t nglePyr mid D *)*f= ## Rot tion l ngleA&9e D *)*f= ## Rot tion l ngle for t%e pyr mid ngle for t%e ;olor ;&9e

## Initi lize OpenGL void initGL(GLvoid) C glS% deEodel(GL_SEOOTL)= GL_1LAT) ##glS% deEodel(GL_1LAT)= to 9l ;8 glAle r4ept%(()*f)= glEn 9le(GL_4EPTL_TEST)= remov l gl4ept%1&n;(GL_LE23AL)= ## T%e type of dept% te!ting to do ## Set ;le r dept% v l&e to f rt%e!t ## En 9le! dept%:9&ffer for %idden !&rf ;e ## Aomp re GL_1LAT nd GL_SEOOTL glAle rAolor(*)*f' *)*f' *)*f' ()*f)= ## Set 9 ;8gro&nd (;le r) ;olor ## En 9le !moot% !% ding of ;olor (try

11

glLint(GL_PERSPEATI-E_AORREATION_LINT' GL_NIAEST)= vie$ I ## L ndler for $indo$ p int void di!pl y(void) C nd re:p int event

## ni;e per!pe;tive

glAle r(GL_AOLOR_B311ER_BIT K GL_4EPTL_B311ER_BIT)= ## Ale r !;reen dept% 9&ffer!

nd

## 4r $ pyr mid glLo dIdentity()= glTr n!l tef(:()5f' *)*f' :7)*f)= !;reen 7)* glRot tef( nglePyr mid' *)*f' ()*f' *)*f)= ## Rot te t%e tri ngle t%e y: "i! glBegin(GL_TRIANGLES)= glAolor/f(()*f' *)*f' *)*f)= gl-erte"/f( *)*f' ()*f' *)*f)= glAolor/f(*)*f' ()*f' *)*f)= glAolor/f(*)*f' *)*f' ()*f)= gl-erte"/f(()*f' :()*f' ()*f)= glAolor/f(()*f' *)*f' *)*f)= gl-erte"/f(*)*f' ()*f' *)*f)= glAolor/f(*)*f' *)*f' ()*f)= gl-erte"/f(()*f' :()*f' ()*f)= glAolor/f(*)*f' ()*f' *)*f)= ## 4r $ &!ing tri ngle! ## Red ## Top of tri ngle (1ront) ## Green ## Bl&e ## Rig%t of tri ngle (1ront) ## Red ## Top of tri ngle (Rig%t) ## Bl&e ## Left of tri ngle (Rig%t) ## Green 9o&t ## Re!et t%e vie$ ## Eove left ()5 &nit! nd into t%e

gl-erte"/f(:()*f' :()*f' ()*f)= ## Left of tri ngle (1ront)

gl-erte"/f(()*f' :()*f' :()*f)= ## Rig%t of tri ngle (Rig%t) glAolor/f(()*f' *)*f' *)*f)= gl-erte"/f(*)*f' ()*f' *)*f)= glAolor/f(*)*f' ()*f' *)*f)= gl-erte"/f(()*f' :()*f' :()*f)= glAolor/f(*)*f' *)*f' ()*f)= ## Red ## Top of tri ngle (B ;8) ## Green ## Left of tri ngle (B ;8) ## Bl&e

gl-erte"/f(:()*f' :()*f' :()*f)= ## Rig%t of tri ngle (B ;8) glAolor/f(()*f' *)*f' *)*f)= gl-erte"/f(*)*f' ()*f' *)*f)= glAolor/f(*)*f' *)*f' ()*f)= glAolor/f(*)*f' ()*f' *)*f)= ## Red ## Top of tri ngle (Left) ## Bl&e ## Green

gl-erte"/f(:()*f' :()*f' :()*f)= ## Left of tri ngle (Left)

12

gl-erte"/f(:()*f' :()*f' ()*f)= glEnd()=

## Rig%t of tri ngle (Left)

## 4one dr $ing t%e pyr mid

## 4r $ ;olor ;&9e glLo dIdentity()= glTr n!l tef(()5f' *)*f' :7)*f)= !;reen 7)* glRot tef( ngleA&9e' ()*f' ()*f' ()*f)= (' () glBegin(GL_23A4S)= glAolor/f(*)*f' ()*f' *)*f)= ## 4r $ &!ing M& d! ## Green ## Rot te t%e ;&9e 9o&t ((' ## Re!et t%e vie$ ## Eove rig%t ()5 &nit! nd into t%e

gl-erte"/f( ()*f' ()*f' :()*f)= ## Top:rig%t of t%e M& d (Top) gl-erte"/f(:()*f' ()*f' :()*f)= ## Top:left of t%e M& d (Top) gl-erte"/f(:()*f' ()*f' gl-erte"/f( ()*f' ()*f' ()*f)= ## Bottom:left of t%e M& d (Top) ()*f)= ## Bottom:rig%t of t%e M& d (Top) ## Or nge

glAolor/f(()*f' *)5f' *)*f)= gl-erte"/f( ()*f' :()*f' gl-erte"/f(:()*f' :()*f'

()*f)= ## Top:rig%t of t%e M& d (Bottom) ()*f)= ## Top:left of t%e M& d (Bottom)

gl-erte"/f(:()*f' :()*f' :()*f)= ## Bottom:left of t%e M& d (Bottom) gl-erte"/f( ()*f' :()*f' :()*f)= ## Bottom:rig%t of t%e M& d (Bottom) glAolor/f(()*f' *)*f' *)*f)= gl-erte"/f( ()*f' gl-erte"/f(:()*f' ()*f' ()*f)= ()*f' ()*f)= ## Red ## Top:rig%t of t%e M& d (1ront) ## Top:left of t%e M& d (1ront) ## Bottom:left of t%e M& d (1ront) ## Bottom:rig%t of t%e M& d (1ront) ## ,ello$

gl-erte"/f(:()*f' :()*f' ()*f)= gl-erte"/f( ()*f' :()*f' ()*f)= glAolor/f(()*f' ()*f' *)*f)=

gl-erte"/f( ()*f' :()*f' :()*f)= ## Bottom:left of t%e M& d (B ;8) gl-erte"/f(:()*f' :()*f' :()*f)= ## Bottom:rig%t of t%e M& d (B ;8) gl-erte"/f(:()*f' gl-erte"/f( ()*f' ()*f' :()*f)= ## Top:rig%t of t%e M& d (B ;8) ()*f' :()*f)= ## Top:left of t%e M& d (B ;8) ## Bl&e

glAolor/f(*)*f' *)*f' ()*f)= gl-erte"/f(:()*f' gl-erte"/f(:()*f' ()*f'

()*f)= ## Top:rig%t of t%e M& d (Left)

()*f' :()*f)= ## Top:left of t%e M& d (Left) ()*f)= ## Bottom:rig%t of t%e M& d (Left) ## -iolet

gl-erte"/f(:()*f' :()*f' :()*f)= ## Bottom:left of t%e M& d (Left) gl-erte"/f(:()*f' :()*f'

glAolor/f(()*f' *)*f' ()*f)= gl-erte"/f( ()*f'

()*f' :()*f)= ## Top:rig%t of t%e M& d (Rig%t)

13

gl-erte"/f( ()*f'

()*f'

()*f)= ## Top:left of t%e M& d (Rig%t) ()*f)= ## Bottom:left of t%e M& d (Rig%t)

gl-erte"/f( ()*f' :()*f' glEnd()=

gl-erte"/f( ()*f' :()*f' :()*f)= ## Bottom:rig%t of t%e M& d (Rig%t) ## 4one dr $ing t%e ;olor ;&9e nd 9 ;8 9&ffer! (do&9le 9&ffered mode) ngle! for t%e ne"t refre!% ngle for t%e pyr mid ngle for t%e ;&9e

gl&tS$ pB&ffer!()= ## S$ p front

## Anim tion Aontrol : Aomp&te rot tion l ngleA&9e :D *)(5f= I ## L ndler for $indo$B! re:!ize event void re!% pe(GL!izei $idt%' GL!izei %eig%t) C integer if (%eig%t DD *) %eig%t D (= ## Set t%e vie$port (di!pl y

nglePyr mid TD *).f= ## In;re !e t%e rot tion l ## 4e;re !e t%e rot tion l

## GL!izeiO non:neg tive

## prevent divide 9y * re ) to ;over entire ppli; tion $indo$

gl-ie$port(*' *' $idt%' %eig%t)= ## Sele;t t%e !pe;t r tio of t%e ;lipping re to m t;% t%e vie$port

glE tri"Eode(GL_PROFEATION)= glLo dIdentity()= ## fovy'

## Sele;t t%e Pro<e;tion m tri" ## Re!et t%e Pro<e;tion m tri"

gl&Per!pe;tive(05)*' (flo t)$idt% # (flo t)%eig%t' *)(' (**)*)= !pe;t r tio' zNe r' z1 r

## Re!et t%e Eodel:-ie$ m tri" glE tri"Eode(GL_EO4EL-IE+)= glLo dIdentity()= I ## L ndler for 8ey event void 8ey9o rd(&n!igned ;% r 8ey' int "' int y) C !$it;% (8ey) C ; !e .6O def &ltO 9re 8= I I ## L ndler for !pe;i l:8ey event void !pe;i lUey(int 8ey' int "' int y) C ## ESAO e"it t%e progr m e"it(*)= 9re 8= ## Sele;t t%e Eodel:-ie$ m tri" ## Re!et t%e Eodel:-ie$ m tri"

14

!$it;% (8ey) C ; !e GL3T_UE,_1(O mode f&llS;reenEode D Vf&llS;reenEode= if (f&llS;reenEode) C $indo$Po!R $indo$Po!, $indo$+idt% D gl&tGet(GL3T_+IN4O+_,)= D gl&tGet(GL3T_+IN4O+_+I4TL)= ## S$it;% into f&ll ## +indo$ed mode ## Toggle !t te ## 1&ll:!;reen mode ## 1(O Toggle 9et$een f&ll:!;reen nd $indo$ed

D gl&tGet(GL3T_+IN4O+_R)= ## S ve p r meter!

$indo$Leig%t D gl&tGet(GL3T_+IN4O+_LEIGLT)= gl&t1&llS;reen()= !;reen I el!e C $indo$ed mode gl&tPo!ition+indo$($indo$Po!R' $indo$Po!R)= left ;orner I 9re 8= def &ltO 9re 8= I I ## m in f&n;tionO GL3T r&n! int m in(int rg;' ;% rJJ rgv)= gl&tInit(W rg;' ! rgv) C ## Initi lize GL3T ## Initi l $indo$ $idt% ;on!ole ppli; tion ## Po!tion top: gl&tRe!% pe+indo$($indo$+idt%' $indo$Leig%t)= ## S$it;% into

gl&tInit4i!pl yEode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4EPTL)= ## Set mode! gl&tInit+indo$Size($indo$+idt%' $indo$Leig%t)= nd %eig%t gl&tInit+indo$Po!ition($indo$Po!R' $indo$Po!,)= ## Initi l $indo$ top: left ;orner ("' y) gl&tAre te+indo$(title)= gl&t1&llS;reen()= gl&t4i!pl y1&n;(di!pl y)= gl&tRe!% pe1&n;(re!% pe)= gl&tUey9o rd1&n;(8ey9o rd)= gl&tIdle1&n;(di!pl y)= initGL()= gl&tE inLoop()= ret&rn *= I ## Are te $indo$ $it% t%e given title ## P&ll into f&ll !;reen ## Regi!ter % ndler for $indo$ re:p int ## Regi!ter % ndler for $indo$ re:!ize ## Regi!ter % ndler for 8ey event ## Regi!ter % ndler if no ot%er event

gl&tSpe;i l1&n;(!pe;i lUey)= ## Regi!ter % ndler for !pe;i l:8ey event ## O&r o$n OpenGL initi liz tion ## Enter t%e infinitely event:% ndling loop

Explanations:

15

/or the triangles that made up the pyramid)% each o" their three vertices is set to a di""erent color. Observe "rom the output display that the color blends smoothly "rom one vertex into another vertex. 9t is made possible via setting the shading mode to GL_SEOOTL. Try changing it to GL_1LAT and observe the di""erence the color o" the triangle is set by the color o" the last vertex).

## initGL() glS% deEodel(GL_SEOOTL)= ## try GL_1LAT

&e use *-bu""er or depth-bu""er) "or hidden sur"ace removal. The "ollo$ing codes are related:

## In m in()O initi lize GL3T $it% dept% 9&ffer gl&tInit4i!pl yEode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4EPTL)= ## In initGL()O En 9le dept%:9&ffer glEn 9le(GL_4EPTL_TEST)= remov l gl4ept%1&n;(GL_LE23AL)= ## A%oo!e t%e dept% te!ting lgorit%m ## En 9le! dept%:9&ffer for %idden !&rf ;e

&e choose the :erspective :ro.ection via:


## In re!% pe() glE tri"Eode(GL_PROFEATION)= glLo dIdentity()= ## Sele;t t%e Pro<e;tion m tri" ## Re!et t%e Pro<e;tion m tri" nd top pro<e;tion pl ne!) i! 05 ! t%e vie$port' to t zD*)( void di!tortion) nd zD(**)*'

gl&Per!pe;tive(05)*' (flo t)$idt% # (flo t)%eig%t' *)(' (**)*)= ## 1ovy ( ngle 9et$een 9ottom degree! (or ..)5 ## T%e front 9ove ## A!pe;t r tio i! t%e ! me nd 9elo$ t%e %orizon))

nd 9 ;8 ;lipping pl ne! i!

rel tive to ; mer B! eye po!ition)

&here is the cameraD The de"ault gl&Loo8At:


gl&Loo8At(*)*' *)*' *)*' *)*' *)*' :(**)*' *)*' ()*' *)*) ## A mer B! GEyeG ## Aiming ## A mer t t%e origin (*' *' *) long po!itive y: "i! (*' (' *) long neg tive z: "i! (*' *' :(**) orient te! $it% &p$ rd!

Ae"ore dra$ing the pyramid and color cube% $e use glLo dIdentity to reset the ;odel-<ie$ matrix. Other$ise% the a""ine trans"ormations translation and rotation) are accumulative. 4lternatively% $e can use glP&!%E tri"#glPopE tri" to save and restore the state o" ;odel-<ie$ matrix. 4nimation is driven by registering di!pl y() as the ?idle-event? handler. 9n other $ords% OpenGL graphics system callbac-s di!pl y() $henever it has no other event to process.

16

The rotational angles "or pyramid and cube global variables) are changed a"ter each call to
di!pl y(). ## In di!pl y() ## 3!e gl&tS$ pB&ffer! in!te d of gl1l&!% gl&tS$ pB&ffer!()= mode) ## S$ p front nd 9 ;8 9&ffer! (do&9le 9&ffered ## In di!pl y() glRot tef( nglePyr mid' *)*f' ()*f' *)*f)= 9o&t t%e y: "i! )))))) glRot tef( ngleA&9e' ()*f' ()*f' ()*f)= ((' (' () )))))) nglePyr mid TD *).f= ngleA&9e ## In;re !e t%e rot tion ngle for t%e pyr mid ngle for t%e ;&9e :D *)(5f= ## 4e;re !e t%e rot tion ## Rot te t%e ;&9e 9o&t ## Rot te t%e tri ngle ## Glo9 l v ri 9le! GLflo t GLflo t nglePyr mid D *)*f= ## Rot tion l ngleA&9e D *)*f= ## Rot tion l ngle for t%e pyr mid ngle for t%e ;olor ;&9e ## In m in() gl&tIdle1&n;(di!pl y)= ## Regi!ter % ndler if no ot%er event

Double bu""ering is used "or better and smoother animation.


## In m in() ## En 9le do&9le 9&ffering gl&tInit4i!pl yEode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4EPTL)=

special

-ey

handler

called

!pe;i lUey()

registered

via

gl&tSpe;i l1&n;(!pe;i lUey)) is provided $here /2 can be used to toggle bet$een

"ullscreen and $indo$ed modes.


## Glo9 l - ri 9le! 9ool f&llS;reenEode D tr&e= ## 1&ll:!;reen or $indo$ed modeS )))))) ## L ndler for !pe;i l:8ey event void !pe;i lUey(int 8ey' int "' int y) C !$it;% (8ey) C ; !e GL3T_UE,_1(O $indo$ed mode f&llS;reenEode D Vf&llS;reenEode= ## Toggle !t te ## 1(O Toggle 9et$een f&ll:!;reen nd

17

mode f&ll !;reen mode I I

if (f&llS;reenEode) C $indo$Po!R $indo$Po!, $indo$+idt%

## 1&ll:!;reen

D gl&tGet(GL3T_+IN4O+_R)= ## S ve p r meter! D gl&tGet(GL3T_+IN4O+_,)= D gl&tGet(GL3T_+IN4O+_+I4TL)= ## S$it;% into ## +indo$ed

$indo$Leig%t D gl&tGet(GL3T_+IN4O+_LEIGLT)= gl&t1&llS;reen()= I el!e C

gl&tRe!% pe+indo$($indo$+idt%' $indo$Leig%t)= ## S$it;% into $indo$ed mode gl&tPo!ition+indo$($indo$Po!R' $indo$Po!R)= top:left ;orner I 9re 8= def &ltO 9re 8= ## Po!tion

4 program.

-eyboard

handler

called

8ey9o rd()

registered

via

gl&t8ey9o rd1&n;(8ey9o rd)) is provided $here ,(+ -ey can be used to 1uit the

## L ndler for 8ey event void 8ey9o rd(&n!igned ;% r 8ey' int "' int y) C !$it;% (8ey) C ; !e .6O def &ltO 9re 8= I I ## ESAO e"it t%e progr m e"it(*)= 9re 8=

More Models
GL O!jects

GL8 supports these 1uadrics: spheres% cylinders% dis-s% and partial dis-s.
void gl&Sp%ere(GL3M& dri;O9< Jo9<' GLdo&9le r di&!' GLint !li;e!' GLint !t ;8!)

18

void gl&Aylinder(GL3M& dri;O9< Jo9<' GLdo&9le 9 !e' GLdo&9le top' GLdo&9le %eig%t' GLint !li;e!' GLint !t ;8!) void gl&4i!8(GL3M& dri;O9< Jo9<' GLdo&9le inner' GLdo&9le o&ter' GLint !li;e!' GLint ring!) void gl&P rti l4i!8(GL3M& dri;O9< Jo9<' GLdo&9le inner' GLdo&9le o&ter' GLint !li;e!' GLint ring!' GLdo&9le !t rt' GLdo&9le ngle)

GL " o!jects
GL8T added these ob.ects: cube% sphere% cone% torus% some regular polyhedra% and the 8tah teapot - in either $ire"rame or "illed polygon.
void gl&t+ireA&9e(GLdo&9le !ide!) void gl&tSolidA&9e(GLdo&9le !ide!) void gl&t+ireSp%ere(GLdo&9le r di&!' GLint !li;e!' GLint !t ;8!) void gl&tSolidSp%ere(GLdo&9le r di&!' GLint !li;e!' GLint !t ;8!) void gl&t+ireAone(GLdo&9le 9 !e' GLdo&9le %eig%t' GLint !li;e!' GLint !t ;8!) void gl&tSolidAone(GLdo&9le 9 !e' GLdo&9le %eig%t' GLint !li;e!' GLint !t ;8!) void gl&t+ireTor&!(GLdo&9le inner' GLdo&9le o&ter' GLint !ide!' GLint !li;e!) void gl&tSolidTor&!(GLdo&9le inner' GLdo&9le o&ter' GLint !ide!' GLint !li;e!) void gl&t+ireTetr %edron() void gl&tSolidTetr %edron() void gl&t+ireO;t %edron() void gl&tSolidO;t %edron() void gl&t+ire4ode; %edron() void gl&tSolid4ode; %edron() void gl&t+ireI;o! %edron() void gl&tSolidI;o! %edron() void gl&t+ireTe pot() void gl&tSolidTe pot()

19

You might also like