+ All Categories
Home > Documents > Computer Graphics 3D Graphic Coordinate System

Computer Graphics 3D Graphic Coordinate System

Date post: 04-Jun-2018
Category:
Upload: jkkp
View: 219 times
Download: 0 times
Share this document with a friend

of 19

Transcript
  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    1/19

    OpenGL3D 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 coordinatesystem $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 2 primitive types as illustrated as "ollo$s:

    1

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    2/19

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    3/19

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    4/19

    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

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    5/19

    The camera is position at the origin % % )% 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 tochoosing 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

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    6/19

    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. ;ore commonly-used GL8 command gl&Per!pe;tive :

    . void gl&Per!pe;tive(GLdo&9le fovy ' GLdo&9le !pe;tR tio ' GLdo&9lezNe r ' GLdo&9le z1 r )

    / ## fovy i! t%e ngle 9et$een t%e 9ottom nd top of t%e pro

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    7/19

    6. +ore GL command gl1r&!t&m :

    6 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 nd yTop !pe;ifie! t%e front ;lipping

    pl ne? ## zNe r nd z1 r !pe;ify t%e po!ition! of t%e front nd 9 ;8

    ;lipping pl ne!

    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 theparameters o" gl&Loo8At and gl&Per!pe;tive .

    Viewport

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    8/19

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

    (uppose the the the vie$port is % % 67 % 7B )% then the top-le"t corner o" the pro ection plane

    maps to % ) 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.

    ## ll9 ;8 $%en t%e OpenGL ! $indo$ i! re:!izedvoid re!% pe(GL!izei $idt%' GL!izei %eig%t) C ## GL!izei for non:neg tiveinteger if (%eig%t DD *) %eig%t D = ## To prevent divide9y * GLflo t !pe;t D (GLflo t)$idt% # (GLflo t)%eig%t= ## omp&te !pe;tr tio

    ## Set t%e vie$port (di!pl y re on t%e $indo$) to ;over t%e $%oleppli; tion $indo$ gl-ie$port(*' *' $idt%' %eig%t)=

    ## Ad

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    9/19

    glLo dIdentity()= ## Re!et t%e Pro

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    10/19

    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 y ode :

    . gl&tInit4i!pl y ode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4E T )=/ ## GL3T_4EPT to reM&e!t for dept%:9&ffer

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

    5 glEn 9le(GL_4EPT _TEST)=

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

    6 gl le r(GL_ OLOR_B311ER_BIT KGL_4E T _ 311ER_ IT

    )= ## le r ;olornd dept% 9&ffer!

    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_4EPT _TEST)= ## En 9le dept%:9&ffer for %idden !&rf ;e remov l

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

    10

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    11/19

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    12/19

    gl int(GL_PERSPE TI-E_ ORRE TION_ INT' GL_NI EST)= ## ni;e per!pe;tivevie$

    ## ndler for $indo$ p int nd re:p int eventvoid di!pl y(void) C gl le r(GL_ OLOR_B311ER_BIT K GL_4EPT _B311ER_BIT)= ## le r !;reen nddept% 9&ffer!

    ## 4r $ pyr mid

    glLo dIdentity()= ## Re!et t%e vie$ glTr n!l tef(: 5f' * *f' :7 *f)= ## ove left 5 &nit! nd into t%e!;reen 7 * glRot tef( nglePyr mid' * *f' *f' * *f)= ## Rot te t%e tri ngle 9o&tt%e y: "i!

    glBegin(GL_TRIANGLES)= ## 4r $ &!ing tri ngle! gl olor/f( *f' * *f' * *f)= ## Red gl-erte"/f( * *f' *f' * *f)= ## Top of tri ngle (1ront) gl olor/f(* *f' *f' * *f)= ## Green gl-erte"/f(: *f' : *f' *f)= ## Left of tri ngle (1ront) gl olor/f(* *f' * *f' *f)= ## Bl&e

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

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

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

    gl olor/f( *f' * *f' * *f)= ## Red gl-erte"/f(* *f' *f' * *f)= ## Top of tri ngle (Left) gl olor/f(* *f' * *f' *f)= ## Bl&e

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

    12

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    13/19

    gl-erte"/f(: *f' : *f' *f)= ## Rig%t of tri ngle (Left) glEnd()= ## 4one dr $ing t%e pyr mid

    ## 4r $ ;olor ;&9e

    glLo dIdentity()= ## Re!et t%e vie$ glTr n!l tef( 5f' * *f' :7 *f)= ## ove rig%t 5 &nit! nd into t%e!;reen 7 * glRot tef( ngle &9e' *f' *f' *f)= ## Rot te t%e ;&9e 9o&t ( '' )

    glBegin(GL_23A4S)= ## 4r $ &!ing M& d! gl olor/f(* *f' *f' * *f)= ## Green 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' *f)= ## Bottom:left of t%e M& d (Top) gl-erte"/f( *f' *f' *f)= ## Bottom:rig%t of t%e M& d (Top)

    gl olor/f( *f' * 5f' * *f)= ## Or nge gl-erte"/f( *f' : *f' *f)= ## Top:rig%t of t%e M& d (Bottom) gl-erte"/f(: *f' : *f' *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)

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

    gl olor/f( *f' *f' * *f)= ## ,ello$ 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' *f' : *f)= ## Top:rig%t of t%e M& d (B ;8) gl-erte"/f( *f' *f' : *f)= ## Top:left of t%e M& d (B ;8)

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

    gl olor/f( *f' * *f' *f)= ## -iolet

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

    13

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    14/19

    gl-erte"/f( *f' *f' *f)= ## Top:left of t%e M& d (Rig%t) gl-erte"/f( *f' : *f' *f)= ## Bottom:left of t%e M& d (Rig%t) gl-erte"/f( *f' : *f' : *f)= ## Bottom:rig%t of t%e M& d (Rig%t) glEnd()= ## 4one dr $ing t%e ;olor ;&9e

    gl&tS$ pB&ffer!()= ## S$ p front nd 9 ;8 9&ffer! (do&9le 9&ffered mode)

    ## Anim tion ontrol : omp&te rot tion l ngle! for t%e ne"t refre!%

    nglePyr mid D * .f= ## In;re !e t%e rot tion l ngle for t%e pyr mid ngle &9e :D * 5f= ## 4e;re !e t%e rot tion l ngle for t%e ;&9e

    ## ndler for $indo$ ! re:!ize eventvoid re!% pe(GL!izei $idt%' GL!izei %eig%t) C ## GL!izei non:neg tiveinteger if (%eig%t DD *) %eig%t D = ## prevent divide 9y *

    ## Set t%e vie$port (di!pl y 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 gl tri" ode(GL_PROFE TION)= ## Sele;t t%e Pro

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    15/19

    !$it;% (8ey) C ; !e GL3T_UE,_1 ## 1 Toggle 9et$een f&ll:!;reen nd $indo$edmode f&llS;reen ode D Vf&llS;reen ode= ## Toggle !t te

    if (f&llS;reen ode) C ## 1&ll:!;reen mode $indo$Po! D gl&tGet(GL3T_+IN4O+_ )= ## S ve p r meter! $indo$Po!, D gl&tGet(GL3T_+IN4O+_,)= $indo$+idt% D gl&tGet(GL3T_+IN4O+_+I4T )= $indo$ eig%t D gl&tGet(GL3T_+IN4O+_ EIG T)= gl&t1&llS;reen()= ## S$it;% into f&ll!;reen el!e C ## +indo$ed mode gl&tRe!% pe+indo$($indo$+idt%' $indo$ eig%t)= ## S$it;% into

    $indo$ed mode gl&tPo!ition+indo$($indo$Po! ' $indo$Po! )= ## Po!tion top:left ;orner 9re 8= def &lt 9re 8=

    ## m in f&n;tion GL3T r&n! ! ;on!ole ppli; tionint m in(int rg;' ;% rJJ rgv) C gl&tInit(W rg;' rgv)= ## Initi lize GL3T gl&tInit4i!pl y ode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4EPT )= ## Set mode! gl&tInit+indo$Size($indo$+idt%' $indo$ eig%t)= ## Initi l $indo$ $idt%nd %eig%t gl&tInit+indo$Po!ition($indo$Po! ' $indo$Po!,)= ## Initi l $indo$ top:left ;orner ("' y) gl&t re te+indo$(title)= ## re te $indo$ $it% t%e given title gl&t1&llS;reen()= ## P&ll into f&ll !;reen gl&t4i!pl y1&n;(di!pl y)= ## Regi!ter % ndler for $indo$ re:p int gl&tRe!% pe1&n;(re!% pe)= ## Regi!ter % ndler for $indo$ re:!ize gl&tUey9o rd1&n;(8ey9o rd)= ## Regi!ter % ndler for 8ey event gl&tSpe;i l1&n;(!pe;i lUey)= ## Regi!ter % ndler for !pe;i l:8ey event gl&tIdle1&n;(di!pl y)= ## Regi!ter % ndler if no ot%er event initGL()= ## O&r o$n OpenGL initi liz tion gl&t inLoop()= ## Enter t%e infinitely event:% ndling loop ret&rn *=

    Explanations:

    15

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    16/19

    /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_S OOT .

    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% de odel( GL_S OOT )= ## 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() initi lize GL3T $it% dept% 9&ffer

    gl&tInit4i!pl y ode(GL3T_RGBA K GL3T_4O3BLE K GL3T_4E T )=

    ## In initGL() En 9le dept%:9&ffer

    glEn 9le( GL_4E T _TEST )= ## En 9le! dept%:9&ffer for %idden !&rf ;eremov l

    gl4ept%1&n;(GL_LE23AL)= ## %oo!e t%e dept% te!ting lgorit%m

    &e choose the erspective ro ection via:

    ## In re!% pe()

    gl tri" ode(GL_PROFE TION)= ## Sele;t t%e Pro

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    17/19

    The rotational angles "or pyramid and cube global variables) are changed a"ter each call to

    di!pl y() .

    ## In m in()

    gl&tIdle1&n;(di!pl y)= ## Regi!ter % ndler if no ot%er event

    ## Glo9 l v ri 9le!

    GLflo t nglePyr mid D * *f= ## Rot tion l ngle for t%e pyr mid

    GLflo t ngle &9e D * *f= ## Rot tion l ngle for t%e ;olor ;&9e

    ## In di!pl y()

    glRot tef( nglePyr mid' * *f' *f' * *f)= ## Rot te t%e tri ngle9o&t t%e y: "i!

    glRot tef( ngle &9e' *f' *f' *f)= ## Rot te t%e ;&9e 9o&t( ' ' )

    nglePyr mid D * .f= ## In;re !e t%e rot tion ngle for t%e pyr mid

    ngle &9e :D * 5f= ## 4e;re !e t%e rot tion ngle for t%e ;&9e

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

    ## In m in()

    ## En 9le do&9le 9&ffering

    gl&tInit4i!pl y ode(GL3T_RGBA K GL3T_4O3 LE K GL3T_4EPT )=

    ## In di!pl y()

    ## 3!e gl&tS$ pB&ffer! in!te d of gl1l&!%

    gl&tS$ pB&ffer!()= ## S$ p front nd 9 ;8 9&ffer! (do&9le 9&fferedmode)

    4 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;reen ode D tr&e= ## 1&ll:!;reen or $indo$ed mode

    ## ndler for !pe;i l:8ey event

    void !pe;i lUey(int 8ey' int "' int y) C

    !$it;% (8ey) C

    ; !e GL3T_UE,_1 ## 1 Toggle 9et$een f&ll:!;reen nd

    $indo$ed mode f&llS;reen ode D Vf&llS;reen ode= ## Toggle !t te

    17

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    18/19

    if (f&llS;reen ode) C ## 1&ll:!;reenmode

    $indo$Po! D gl&tGet(GL3T_+IN4O+_ )= ## S ve p r meter!

    $indo$Po!, D gl&tGet(GL3T_+IN4O+_,)=

    $indo$+idt% D gl&tGet(GL3T_+IN4O+_+I4T )= $indo$ eig%t D gl&tGet(GL3T_+IN4O+_ EIG T)=

    gl&t1&llS;reen()= ## S$it;% intof&ll !;reen

    el!e C ## +indo$edmode

    gl&tRe!% pe+indo$($indo$+idt%' $indo$ eig%t)= ## S$it;%into $indo$ed mode

    gl&tPo!ition+indo$($indo$Po! ' $indo$Po! )= ## Po!tion

    top:left ;orner

    9re 8=

    def &lt

    9re 8=

    4 eyboard handler called 8ey9o rd() registered via

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

    program.

    ## ndler for 8ey event

    void 8ey9o rd(&n!igned ;% r 8ey' int "' int y) C

    !$it;% (8ey) C

    ; !e .6 ## ES e"it t%e progr m

    e"it(*)= 9re 8=

    def &lt

    9re 8=

    More ModelsGL O!jects

    GL8 supports these 1uadrics: spheres% cylinders% dis s% and partial dis s.

    void gl&Sp%ere(GL3M& dri;O9< Jo9

  • 8/13/2019 Computer Graphics 3D Graphic Coordinate System

    19/19

    void gl& ylinder(GL3M& dri;O9< Jo9


Recommended