computer graphics(unit2)

Upload: jack

Post on 07-Jul-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 Computer Graphics(unit2)

    1/107

    UNIT II

  • 8/19/2019 Computer Graphics(unit2)

    2/107

    OPENGL API

    • OpenGL’s structure is similar to that of

    most modern APIs including Jaa!" and

    "irect#$

    • OpenGL is eas% to learn&compared 'ith

    other API$

    • It supports the simple (" and !"

    programs$

  • 8/19/2019 Computer Graphics(unit2)

    3/107

    Graphics functions

    Black box•our )asic model of a graphics pac*age is a black box$•This term is used to denote a s%stem 'hose properties aredescri)ed onl% )% its inputs and outputs$

    •Graphics s%stem is a )o+ 'hose inputs are function callsfrom an application program, measurements from inputdeices&such as the mouse and *e%)oard and messagesfrom os.•Outputs are primaril% the graphics sent to our output

    deices•E+ample•  'e can ta*e simplified ie' of inputs as function callsand outputs as primities$

  • 8/19/2019 Computer Graphics(unit2)

    4/107

    Graphics s%stem as a )lac* )o+

     function calls output

     

    •   "ata Input

     Application

    program Graphics s%stemInput-output

    deices

  • 8/19/2019 Computer Graphics(unit2)

    5/107

    •  API contain hundreds of functions&so it is helpfulto diide them into seen ma.or groups$

    /$Primitie functions

    ($Attri)ute functions!$0ie'ing functions

    1$Transformation functions

    2$Input functions3$4ontrol functions

    5$6uer% functions$

  • 8/19/2019 Computer Graphics(unit2)

    6/107

    Primitive function

    •  "efine the lo' leel o).ects or atomic entities that our s%stem can

    displa%$• "epending on the API&the primitives can include points, line,

    segments, polygons, pixels, text , and arious t%pe of cures andsurfaces$

    • E+ample  gl7egin8GL9POINT:;,

      gl0erte+(f8/

  • 8/19/2019 Computer Graphics(unit2)

    7/107

     Attri)ute function

    • To perform operations ranging from choosingthe color  'ith 'hich 'e displa% a line segment$

     = Eg gl4olor!f8/$

  • 8/19/2019 Computer Graphics(unit2)

    8/107

    0ie'ing and transformation

    • The ie'ing functions allo's us to specif% ariousie's&although APIs differ in the degree of fle+i)ilit% the%proide in choosing a ie'$

     = gl0ie'port 8+min& %min& p>idth& p?eight;,

     = gluOrtho(" 8+'min& +'ma+& %'min& %'ma+;, = gluLoo*At8@;,

    • Transformation function that allo's to carr% outtransformations of o).ects&such as rotation& translation and

    scaling$ = glTranslatef 8t+& t%& t;,

     = glBotatef 8theta& +& %& ;,

     =  gl:calef 8s+& s%& s;,

  • 8/19/2019 Computer Graphics(unit2)

    9/107

    Input function•  Allo's us to deal 'ith the dierse forms of input

    that characterie modern graphics s%stems$

     = oid glutCe%)oardDunc8oid 8func; 8unsignedchar *e%& int +& int %;;,

    Control function•  Ena)le us to communicate 'ith the 'indo'

    s%stem&to initialie our programs&and to deal'ith an% errors that ta*e place during thee+ecution of our programs$

    •   glutInit>indo':ie82

  • 8/19/2019 Computer Graphics(unit2)

    10/107

    Query function

    •   ho' man% color are supported or the sie

    of the displa%$

    • 4amera parameters or alues in the frame

    )uffer$

  • 8/19/2019 Computer Graphics(unit2)

    11/107

    The graphics pipeline and state

    machine• Entire graphics s%stem as a state machine$

    •  A )lac* )o+ that contains a finite state machine$

    • This state machine has inputs that come from the

    application program$

    • These inputs ma% change the state of the machine

    or can cause the machine to produce a isi)le

    output$

  • 8/19/2019 Computer Graphics(unit2)

    12/107

     • T'o t%pes of graphics functionsF

     = Those that define primities that flo' through a pipeline inside

    the state machine

     =  those that either changes the state inside the machine or

    return state information$

      glerte+ first t%pe

      Other t%pes =second t%pe

    • In openGL most parameters are persistent$

    • Their alues remain unchanged until 'e e+plicitl% change themthrough functions that alter the state$

  • 8/19/2019 Computer Graphics(unit2)

    13/107

    • Dor e+ample& once 'e set a color&that

    color remains the current color until it is

    changed through a coloraltering function

  • 8/19/2019 Computer Graphics(unit2)

    14/107

    OpenGL interface

    • Three li)raries

     = GL li)rar%

     = GLU8OpenGL Utilit% li)rar%;

     = GLUT8OpenGL Utilit% Tool*it;

    G

    functions in the GL li)rar% hae names that

    )egin 'ith the letters gl and are stored in ali)rar% usuall% referred to as GL

  • 8/19/2019 Computer Graphics(unit2)

    15/107

    • GLU = This li)rar% uses onl% GL functions )ut

    contains code for creating common o).ects

    and simplif%ing ie'ing$ = Dunctions in the GLU li)rar% )egin 'ith the

    letter glu$

    G!"

    To interface 'ith the 'indo' s%stem and to getinput from e+ternal deices into ourprograms&'e need at least one more li)rar%$

  • 8/19/2019 Computer Graphics(unit2)

    16/107

    • G# $ %penG &xtension for the # 'indo( )ystem* = Dor the # (indo( system& this li)rar% is called G#

    'G

    for (indo(s, it is (gl = 'G or 'iggle is the 'indo'ing s%stem interface to the

    Hicrosoft >indo's implementation of the OpenGL specification $

    • +G = for the macintosh it is agl or+pple graphics library-.

    • Bather than using a different li)rar% for each s%stem&'euse a readil% aaila)le li)rar% called the openGL utilit%Tool*it8GLUT;$

    • GLUT proides minimum functionalit% that should )ee+pected in an% modern 'indo'ing s%stem$

    http://www710.univ-lyon1.fr/~jciehl/Public/OpenGL_PG/apc.htmlhttp://en.wikipedia.org/wiki/Microsoft_Windowshttp://en.wikipedia.org/wiki/OpenGLhttp://en.wikipedia.org/wiki/OpenGLhttp://en.wikipedia.org/wiki/Microsoft_Windowshttp://www710.univ-lyon1.fr/~jciehl/Public/OpenGL_PG/apc.html

  • 8/19/2019 Computer Graphics(unit2)

    17/107

    Li)rar% organiation

    OpenGL

     Application

    program

    GLU

    GL

    GLUT

    GL#

    #li)t*

    Drame

    )uffer 

  • 8/19/2019 Computer Graphics(unit2)

    18/107

    PBIHITI0E: AN"

     ATTBI7UTE:

  • 8/19/2019 Computer Graphics(unit2)

    19/107

    • OpenGL supports t(o classes ofprimitiesF

     = Geometric primities = Image 8or; raster primities

    Geometric primitives

    • Include points&line segments&pol%gons&curesand surfaces$

    • These primities pass through a geometricpipeline$ 'here the% are su).ect to a series of

    geometric operations that determine 'hethera primitie is isi)le$

    • >here on the displa% it appears if it is isi)le$

  • 8/19/2019 Computer Graphics(unit2)

    20/107

    :implified OpenGL pipeline

    OpenGL

    application

    program

    Transform Pro.ect 4lip

    Drame

    )uffer Pi+el operations

  • 8/19/2019 Computer Graphics(unit2)

    21/107

    • Geometric Primities can )e used as )uilding )loc*s for othergeometric o).ects$

    • Baster primities such as arra%s of pi+els pass through a separate

    parallel pipeline$• The )asic OpenGL geometric primities are specified )% sets ofertices$  gl7egin8t%pe;,

      gl0erte+8@;,

      $

      $  gl0erte+8@;,

    glEnd8;,

    • The alue of type specifies ho' OpenGL assem)les the ertices todefine geometric o).ects$

    • Other code and OpenGL function calls can occur )et'een glBegin

    and gl&nd$• Ha.or difference )et'een the geometric t%pes = >hether or not the% hae interiors$

     All the )asic t%pes are defined )% sets of ertices$

  • 8/19/2019 Computer Graphics(unit2)

    22/107

    I& )&G/&")• Dinite sections of lines )et'een t'o

    ertices called line segments.

    !)&)• Use line segments to define appro+imations to

    cures$•  %ou can use a se6uence of line segments to

    connect data alues for a graph$• ou can also use line segments for the edges of

    closed o).ects& such as pol%gon&that haeinteriors$

  • 8/19/2019 Computer Graphics(unit2)

    23/107

    • The primitie and their type specification includethe follo'ingF

    • PointsG0P%I")-$ =  Each erte+ is displa%ed at a sie of at least onepi+el$

    • ine segmentsG0I&)-$ = :uccessie pairs of ertices to )e interpreted as the

    endpoints of indiidual segments$• PolylinesG0I&0)"1IP,G0I&0%%P-$

    G0I&0)"1IP = If successie ertices are to )e connected&'e can use

    the line strip&or pol%line form$G0I&0%%P 

     = 'ill dra' a line segment from the final erte+ to thefirst&thus creating a closed path$

  • 8/19/2019 Computer Graphics(unit2)

    24/107

  • 8/19/2019 Computer Graphics(unit2)

    25/107

    Pol%gon 7asics

    • Polygon =has a )order and has a 'ell definedinterior$It can )e descri)ed )% line loop$

    • Pol%gons pla% a special role in 4G$•

    The performance of graphics s%stems ischaracteried )% the num)er of pol%gons persecond that can )e rendered$

    Polygon rendering•   Bender onl% its edges$•   Bender its interior 'ith a solid color or a

    pattern$•   'e can render or not render the edges

  • 8/19/2019 Computer Graphics(unit2)

    26/107

    • "hree properties 'ill ensure that a

    pol%gon 'ill )e displa%ed correctl%$

    • It must )e simple,convex and flat$

    )imple

     in (" &as long as no t'o edges of a pol%gon

    cross each other&'e hae a simple pol%gon$

    >ell defined interiors$

  • 8/19/2019 Computer Graphics(unit2)

    27/107

    • a; :imple pol%gon

    • 7; Nonsimple pol%gon

  • 8/19/2019 Computer Graphics(unit2)

    28/107

    Convex•  An o).ect is cone+ if all points on the line

    segment )et'een an% t'o points inside the

    o).ect&or on its )oundar%&are inside the o).ect$• p/ and p( are ar)itrar% points inside a pol%gon

    and the entire line segment connecting them isinside the pol%gon$

    • 4one+ o).ects include triangle,tetrahedra,rectangles,circles,spheres,

      parallepipeds.• )imple$ edges cannot cross•  Convex$ All points on line segment )et'een

    t'o points in a pol%gon are also in the pol%gon• 2lat$ all ertices are in the same plane

  • 8/19/2019 Computer Graphics(unit2)

    29/107

    • 4one+it%

  • 8/19/2019 Computer Graphics(unit2)

    30/107

    4one+ o).ects

  • 8/19/2019 Computer Graphics(unit2)

    31/107

    Pol%gon t%pes in OpenGL

    • PolygonsG0P%3G%-$ = successie ertices define line segments$and a line

    segment connects the final erte+ to the first$

     = use the function glpolygon/ode  to tell the rendererto generate onl% the edges or .ust points for theertices&instead of fill$

    • "riangle and 4uadrilateralsG0"1I+G&),G0Q!+5)-$

     = :uccessie groups of three and four ertices areinterpreted as triangles and 6uadrilaterals$

  • 8/19/2019 Computer Graphics(unit2)

    32/107

    • )trips and 2ansG0"1I+G&0)"1IP,G0Q!+50)"1IP, G0"1I+G&02+- =  groups of triangles or 6uadrilaterals that share ertices and

    edges$

    "riangle strip = Each additional erte+ is com)ined 'ith the preious t'oertices to define a ne' triangle$

    Quadstrip

     = 4om)ine t'o ne' ertices 'ith the preious t'o ertices todefine a ne' 6uadrilateral$

    "riangle fan

     = Triangle fan is )ased on the one fi+ed point$The ne+t t'o pointsdetermine the first triangle& and su)se6uent triangles are formedfrom one ne' point&the preious point and the first point$

  • 8/19/2019 Computer Graphics(unit2)

    33/107

    Pol%gon t%pes

  • 8/19/2019 Computer Graphics(unit2)

    34/107

  • 8/19/2019 Computer Graphics(unit2)

    35/107

     Appro+imating a :phere

    #8&K;sin cos K&8&K;cos cos K&

    M8&K;sin K

    Unit sphere$

    Ceep theta fi+ and change phi&

    'e get circles of constant

    latitude

    Ceep phi fi+ and change theta&

    'e get circles of constantlongitude$

    Poles are on the a+is of the

    sphere$

  • 8/19/2019 Computer Graphics(unit2)

    36/107

    :phere

    C hi fi d h th t D

  • 8/19/2019 Computer Graphics(unit2)

    37/107

    Ceep phi fi+ and change theta@Dor

    Latitudesfor8float phi

  • 8/19/2019 Computer Graphics(unit2)

    38/107

     At Polesgl7egin8GL9TBIANGLE9DAN;,

    gl0erte+!d8

  • 8/19/2019 Computer Graphics(unit2)

    39/107

  • 8/19/2019 Computer Graphics(unit2)

    40/107

    :tro*e te+t

    • "efining a full /( or(23 character stro*e

    font can )e comple+$

    • It ta*es significantmemor% and

    processing time$

    • standard

    postscript fonts

  • 8/19/2019 Computer Graphics(unit2)

    41/107

    Baster Te+t

    1aster text 

    glut7itmap8GLUT97ITHAP9979/!& c;,

    •  simple and fast$

    • 4haracters are defined as rectangles of )itcalled bit blocks$

    • Each )loc* defines a single character )% thepattern of < and / )its in the )loc*$

    • Bit6block6transfer  = A raster character can )e placed in the frame )uffer

    rapidl% )% a bit6block6transferbitblt- operation&'hich moes the )loc* of )its using a single call$

  • 8/19/2019 Computer Graphics(unit2)

    42/107

    • a;Baster te+t ); Baster character replication

  • 8/19/2019 Computer Graphics(unit2)

    43/107

    1eplicating

    •   %ou can increase the sie of raster

    characters )% replicating or duplicating

    pi+els&a process that gies larger

    characters a )loc*% appearance$

    • GLUT li)rar% proides a fe' predefined

    )itmap and stro*e character sets $

  • 8/19/2019 Computer Graphics(unit2)

    44/107

    4ured o).ects

    • T'o approaches

     = >e can use the primities that 'e hae to

    appro+imate cures and surfaces$

    • a circle 'ith a regular pol%gon 'ith n sides

     = mathematical definitions of cured o).ects

    • supported )% GLU li)rar%

    • 6uadric surfaces& parametric pol%nomial curesand surfaces

  • 8/19/2019 Computer Graphics(unit2)

    45/107

     Attri)utes

     An Attri)utes is an% propert% that determines ho' a geometricprimitie is to )e rendered

     = point 8color& sie;& line 8color&thic*ness& t%pe;& pol%gon 8patternt%pes& etc;$

     = te+t 8height& 'idth& font& st%le 8)old& italic& underlined;;

    immediate mode

      primities are not stored in the displa% s%stem )ut ratherpassed through the s%stem for possi)le rendering as soon asthe% are defined$

      no memor%

      once erased from the displa%&it is lost$ display list

      *eep o).ects in memor% so that these o).ects can )eredispla%ed$

  • 8/19/2019 Computer Graphics(unit2)

    46/107

    4OLOB

    •  A isi)le color can )e characteried )% afunction 48λ; that occupies 'aelengthsfrom a)out !2< to 5

  • 8/19/2019 Computer Graphics(unit2)

    47/107

    •  Additie color model = Primar% colors add together to gie the

    perceied color$

     = The primaries are red&green&)lue8BG7; = E+ample of additie color include

    4BT&pro.ectors

    )ubractive color model

    Primaries are complementar% colorsF 4%an&magenta& %ello'84H;

    E+ampleF commercial printing and painting

  • 8/19/2019 Computer Graphics(unit2)

    48/107

    4olor formation

    •   Additie color :u)tractie color 

  • 8/19/2019 Computer Graphics(unit2)

    49/107

    4olor solid

  • 8/19/2019 Computer Graphics(unit2)

    50/107

    4olor solid

    • 4olor as a point in a color solid$

    • "ra' :olid using a coordinate s%stem

    corresponding to the three primaries$

    • Principal diagonal of the cu)e connects

    the origin8)lac*; 'ith 'hite$All colors along

    this line hae shades of gra%$

  • 8/19/2019 Computer Graphics(unit2)

    51/107

    BG7 4olor 

  • 8/19/2019 Computer Graphics(unit2)

    52/107

    BG7 4olor 

    • There are separate buffers for red,green, andblue images.

    • Each pi+el has separate red&green and )luecomponents that correspond to locations in memor%$

    • In a t%pical s%stem there might )e a /(< #/

  • 8/19/2019 Computer Graphics(unit2)

    53/107

    • 4olor components )et'een

  • 8/19/2019 Computer Graphics(unit2)

    54/107

    • Hust clear the 'indo' 'heneer 'e 'ant

    to dra' a ne' frame$

    • gl4lear4olor8/$

  • 8/19/2019 Computer Graphics(unit2)

    55/107

    Inde+ed 4olor 

    • Earl% graphics s%stems had frame )uffers that 'ere limitedin depth$

    • Each pi+el 'as )its deep$• :maller group of pi+elassign to B&G&7$

    • It gies less no of color$• Inde+ed color proide solution that allo'ed applications todispla% a 'ide range of color$

    • Images are called indexed color  )ecause the actual

    image color data for each pi+el is the inde+ into thispalette $

    •  Analog% 'ith an artist 'ho paints in oil$

  • 8/19/2019 Computer Graphics(unit2)

    56/107

    • (23 entries in the

    ta)le constitute the

    user’s color palette$

    •  figFcolor loo*up ta)le

  • 8/19/2019 Computer Graphics(unit2)

    57/107

    Inde+ed color 

  • 8/19/2019 Computer Graphics(unit2)

    58/107

    • :uppose that our frame has * )its per pi+el$

    • Each pi+el alue or inde+ is an integer )et'een < and

      (*/$

    • >e can displa% colors 'ith precision of m )its$$'e canchoose from (m reds&(m greens&and (m )lues$

    • >e can produce an% of (!m colors on the displa%&)ut theframe )uffer can specif% onl% (*of them$

    • >e handle the specification through a user defined colorloo*up ta)le that is of sie (* !m$

    • The user program fills the (*entries of the ta)le 'ith thedesired colors &using m )its for each of red &green and)lue$

    • The first BG7 color in the ta)le is inde+

  • 8/19/2019 Computer Graphics(unit2)

    59/107

    • If 'e are in color inde+ mode the present

    color is selected )% a function such as

     = glIndexielement-:

    That selects a particular color out of the ta)le$

    GLUT allo's us to set the entries in a color

    ta)le for each 'indo' through the follo'ing

    functionF glut)etColorint color,Gfloat red,Gfloat

    green,Gfloat blue-

  • 8/19/2019 Computer Graphics(unit2)

    60/107

    •  Adantage

     = Less memor%

     = De'er other hard'are components$

    • )it inde+ed images are still 'idel% used

    to sae )and'idth and storage space$

    • >hen earl% computer screens 'ere

    commonl% limited to (23 colors& inde+ed

    color methods 'ere essential$

  • 8/19/2019 Computer Graphics(unit2)

    61/107

    :etting of color attri)utes

    • clear color  

    gl4lear4olor8/$

  • 8/19/2019 Computer Graphics(unit2)

    62/107

    0IE>ING

  • 8/19/2019 Computer Graphics(unit2)

    63/107

    0ie'ing

    • The specification of the o).ects is completel%

    independent of specification of the camera$

    • 4amera forms an image )% e+posing a film$

    •  The computer s%stem forms an image )%carr%ing out a se6uence of operations in its

    pipeline$

    • The application program specifies parameters

    for o).ect and the camera$

  • 8/19/2019 Computer Graphics(unit2)

    64/107

    0IE>INGOrthographic ie'

    • OpenGL default ie' orthographic pro.ection

    • Place the camera infinitel% far from o).ects$

    • Image plane is fi+ed&moe camera far from this

    plane$•  All pro.ectors )ecome parallel and the center of

    pro.ection is replaced )% direction of

    pro

  • 8/19/2019 Computer Graphics(unit2)

    65/107

    • Pro.ectors that are parallel to the positie

    a+is and the pro.ection plane at

  • 8/19/2019 Computer Graphics(unit2)

    66/107

    Orthographic Pro.ection

    Pro.ectors are orthogonal to pro.ection surface

  • 8/19/2019 Computer Graphics(unit2)

    67/107

  • 8/19/2019 Computer Graphics(unit2)

    68/107

    • Dor orthographic ie'ing&specialorthographic camera resides in thepro.ection plane$

    • There is reference point in the pro.ectionplane from 'hich 'e can ma*emeasurements of a ie' olume and adirection of pro.ection$

    • 1eference point origin$ camera points inthe negatie direction$

    The default camera and an

  • 8/19/2019 Computer Graphics(unit2)

    69/107

    The default camera and an

    orthographic ie' olume

  • 8/19/2019 Computer Graphics(unit2)

    70/107

    0ie'ing olume

  • 8/19/2019 Computer Graphics(unit2)

    71/107

    • It ta*es a point8+&%&; and pro.ects it into the point8+&%&

  • 8/19/2019 Computer Graphics(unit2)

    72/107

    • The orthographic pro.ection sees onl%those o).ects in the olume specified )%ie'ing olume$

    • If 'e do not specif% a ie'ingolume&openGL uses its default& ( + ( + (cu)e&'ith the origin in the center$

    • In (" t'o dimensional plane&the )ottomleft corner is at 8/$

  • 8/19/2019 Computer Graphics(unit2)

    73/107

    (" ie'ing

    • =oid glu%rtho>5Gdouble left,Gdouble

    right,Gdouble bottom,Gdouble top-

    • Near and far set to /$< and /$< respectiel%$

    • Ta*e a rectangular area of our t'o dimensional'orld and transfering its contents to the displa%$

    • The area of the 'orld that 'e image is *no'n as

    vie(ing rectangle or clipping rectangle$

    • O).ects inside the rectangle are in the

    image$o).ects outside are clipped out$

  • 8/19/2019 Computer Graphics(unit2)

    74/107

    (" ie'ing

    • a; o).ects )eforeclipping$

    • ); o).ects after

    clipping

  • 8/19/2019 Computer Graphics(unit2)

    75/107

    HATBI# HO"E

    • Using gl/atrix/ode function %ou chose 'hich matri+ %ou 'ant tochange$

    • OpenGL 'or*s 'ith ! matricesF G0/%5&=I&'F this one is usedto moe erte+ to model space$

    • 7% default& the matri+ mode is GL9HO"EL0IE>& 'hich assumeseer%thing %ou dra' 'ill )e in one !d space$

    • G0P1%?&C"I%F this one is used to conert !d coordinate to (dcoordinate for final pi+el position$

    •  A computer monitor is a (" surface$ >e need to transform !" sceneinto (" image in order to displa% it$ GL9PBOJE4TION matri+ is forthis pro.ection transformation

    • G0""!1&F this one is used to alter te+ture coordinates$

    http://www.songho.ca/opengl/gl_transform.htmlhttp://www.songho.ca/opengl/gl_transform.html

  • 8/19/2019 Computer Graphics(unit2)

    76/107

    Hatri+ mode

    • Pipeline graphics s%stem depends on multipl%ingtogether or Concatenating  a num)er of transformationmatrices to achiee the desired image of a primitie

    • T'o important matricesF

     = /odel6vie( = Prohich are initiall% set to identit% matri+$

     = The usual se6uence is to modif% the initial identit% matri+ )%appl%ing se6uence of transformations$

     = :elect the matri+ to 'hich the operations appl% )% first settingthe matri+ mode&a aria)le that is set to one t%pe of matri+ that isalso part of the state$

  • 8/19/2019 Computer Graphics(unit2)

    77/107

    • Once %ou set the matri+ mode& each su)se6uentoperation is applied to that particular matri+ mode and)elo' it $

      glHatri+Hode8GL9PBOJE4TION;,

     glLoadIdentit%8;,

     glOrtho8/$

  • 8/19/2019 Computer Graphics(unit2)

    78/107

    4ONTBOL DUN4TION

  • 8/19/2019 Computer Graphics(unit2)

    79/107

    Interaction 'ith the 'indo' s%stem

    • GLUT is a li)rar% of function that proidesa simple interface )et'een s%stems$

    • 'indo( or screen (indo(  denote a

    rectangular area of our displa%$• 'indo(

     = has height and 'idth

     = it displa%s the contents of the frame )uffer$ = Positions in the 'indo' measured in terms

    pixels.

    • >e can displa% man% t%pe of 'indo's on

  • 8/19/2019 Computer Graphics(unit2)

    80/107

    >e can displa% man% t%pe of 'indo's on

    monitor$

    • The 'indo' in 'hich the graphics output

    appears is one of the 'indo's managed )% the

    'indo' s%stem$

    • In science and engglo'er left corner is the

    origin$•  All raster s%stem top left corner is origin$

    • openGL )ottom left corner is origin$

    • :creen resolution/(< +/

  • 8/19/2019 Computer Graphics(unit2)

    81/107

    • Interaction )et'een 'indo'ing s%stem and OpenGL isinitialied )% the follo'ing function$• glutInitint @argc,char @@argv-

    •  Argument is same as main$• Open OpenGL 'indo' using GLUT function

    glutCreate'indo(char @title-

    • >here the Title at the top of the 'indo' is gien )% string

    title$• Use follo'ing GLUT functions )efore 'indo' creation to

    specif% these parameters$• glutInit5isplay/odeG!"01GB A G!"05&P" A

    G!"05%!B&-:

    • glutInit'indo()i;eD8,DE8-:• glutInit'indo(Position8,8 ;,

    • :pecifies a 1< + 31< 'indo' in the topleft corner of thedispla%$

    A t ti d i t

  • 8/19/2019 Computer Graphics(unit2)

    82/107

     Aspect ratio and ie'ports

    • The aspect ratio of a rectangle is the ratio of therectangle’s 'idth to its height$

    • If alues in gl%rtho and glutInit'indo()i;e

    differ  = undesira)le side effects• If the% differ& o).ects are distorted on the

    screen$

    • To aoid this distortion if 'e ensure that the

    clipping rectangle and displa% 'indo' hae the

    same aspect ratio$

    A t ti i t h

  • 8/19/2019 Computer Graphics(unit2)

    83/107

     Aspect ratio mismatch

    •   a; ie'ing

    •   rectangle

    •   );displa%

    •   'indo'

    A i t th i t

  • 8/19/2019 Computer Graphics(unit2)

    84/107

     A mapping to the ie'port

  • 8/19/2019 Computer Graphics(unit2)

    85/107

    •  Another method = =ie(port

    •  A ie'port is a rectangular area of the displa%'indo'$)% default it is entire 'indo'$

    • It can )e set to an% smaller sie in pi+els ia thefunction$

    •  void gl=ie(portGint x,Glint y,Gsi;ei

    (,Gsi;ei h-• #&%lo'er left corner of the 'indo'$

    •  '&h gie height and 'idth$

    The main&displa% and m%init

  • 8/19/2019 Computer Graphics(unit2)

    86/107

    The main&displa% and m%init

    Dunctions = =oid glut/ainoop-:

    • >hose e+ecution 'ill cause the program to )egin aneentprocessing loop$If there are no eents toprocess&the program 'ill sit in a 'ait state&until 'eterminate the program$

    • The graphics are sent to the screen through a functioncalled the display callback$ this function is namedthrough the GLUT function$

    • void glut5isplay2uncdisplay-•  And registered 'ith the 'indo' s%stem$ function named

    display 'ill )e called 'heneer the 'indo'ing s%stemdetermines that the OpenGL 'indo' needs to )eredispla%ed$

    •  A displa% call)ac* also occurs &'hen the 'indo' ismoed from one location on the screen to another$

    0oid main8int argc&char arg;

  • 8/19/2019 Computer Graphics(unit2)

    87/107

    Q

      glutInit8Vargc& arg;,

      glutInit"ispla%Hode8GLUT9:INGLEWGLUT9BG7;,  glutInit>indo':ie82

  • 8/19/2019 Computer Graphics(unit2)

    88/107

    Program structure

    • Eer% program hae same structure$

    •  Al'a%s use the GLUT tool*it$

    • The main function 'ill consists of calls to GLUT

    functions to set up our 'indo'$• Eer% program must hae displa% call)ac*$

    • H%init function set up user operations$

    • In ma.orit% of programs&the graphics output 'ill)e generated in the displa% call)ac*

    :i i *i G * t 8(";

  • 8/19/2019 Computer Graphics(unit2)

    89/107

    :ierpins*i Gas*et 8(";

    • :tart 'ith a triangle

    • 4onnect )isectors of sides and remoe centraltriangle

    • Bepeat

  • 8/19/2019 Computer Graphics(unit2)

    90/107

    • If 'e dra' line segments connecting themidpoints of the sides of the original triangle&then'e diide the original triangle into fourtriangles&the middle one containing no points$

    • >e can appl% the same o)eration to each ofthem$• It uses polygons instead of points and does

    not re6uire the use of a random num)ergenerator$

    • The adantages of using pol%gon is that 'e getsolid areas on our display.

    G * t P

  • 8/19/2019 Computer Graphics(unit2)

    91/107

    Gas*et Program

    #include  

    /* initial triangle */

    GLfloat v[3][2]={{-.!" -!.$%"{.!" -!.$%" {!.!" .%%&

    int n& /* nu'(er of recur)ive )te) */

    " t i l

  • 8/19/2019 Computer Graphics(unit2)

    92/107

    "ra' one triangle

    void triangle+ GLfloat *a" GLfloat *("GLfloat *c,

    /* di)la one triangle */

    {  gl7egin8GL9TBIANGLE:;,

      gl0erte+(f8a;,

      gl0erte+(f8);,

      gl0erte+(f8c;,

      glEnd8;,

    S

  • 8/19/2019 Computer Graphics(unit2)

    93/107

    displa% and init Dunctions

  • 8/19/2019 Computer Graphics(unit2)

    94/107

    displa% and init Dunctions

    void di)la+,{  gllear+GL4L456788956:;,&dividetriangle+v[!]" v[]" v[2]" n,&

      gl8lu)h+,&%

    void 'init+,{

      glu4rtho2+-2.!" 2.!" -2.!" 2.!,&  gllearolor +.!" .!" .!".!,  glolor3f+!.!"!.!"!.!,&%

    main Dunction

  • 8/19/2019 Computer Graphics(unit2)

    95/107

    main Dunction

    int 'ain+int argc" char **argv,{  n=&  glut:nit+argc" argv,&

      glut:niti)la?ode+GL7;@:AGL9BGL7;5G6,&  glut:nitCindo@iDe+!!" !!,&  glutreateCindo+E2 Ga)Fet,&

      gluti)la8unc+di)la,&  'init+,&  glut?ainLoo+,&%

    E+ample

  • 8/19/2019 Computer Graphics(unit2)

    96/107

    E+ample

    •Die su)diisions

  • 8/19/2019 Computer Graphics(unit2)

    97/107

    !" GA:CET

    • A tetrahedron 8pluralF tetrahedra; is a pol%hedron 

    composed of four triangular faces three of 'hich meet at

    http://en.wikipedia.org/wiki/Polyhedronhttp://en.wikipedia.org/wiki/Trianglehttp://en.wikipedia.org/wiki/Trianglehttp://en.wikipedia.org/wiki/Polyhedron

  • 8/19/2019 Computer Graphics(unit2)

    98/107

    composed of four triangular  faces& three of 'hich meet at

    each erte+$

    • The tetrahedron is the onl% cone+ pol%hedron that hasfour faces$

    •>e can su)diide each of the four faces$

    !" Gas*et Use of !" points

    http://en.wikipedia.org/wiki/Trianglehttp://en.wikipedia.org/wiki/Vertex_(geometry)http://en.wikipedia.org/wiki/Vertex_(geometry)http://en.wikipedia.org/wiki/Triangle

  • 8/19/2019 Computer Graphics(unit2)

    99/107

    !" Gas*et Use of !" points

    • Eer% tetrahedron is cone+& the midpoint of a linesegment )et'een a erte+ and an% point inside atetrahedron is also inside the tetrahedron$

    • >e need initial four ertices to define a tetrahedron$

    • >e define and initialie an arra% to hold the ertices asfollo's$

    • GLfloat erticesZ1[Z![ QQ

  • 8/19/2019 Computer Graphics(unit2)

    100/107

    Q

     

    Int i,

    fork78:kF888:kHH-Q

    Jrand8;\1,

    pZ

  • 8/19/2019 Computer Graphics(unit2)

    101/107

    dimensions

    • :u)diision of a tetrahedron into smaller tetrahedron$• :tart 'ith a tetrahedron and find 'ith midpoints of its six

    edges and connect these midpoints.• There are four smaller tetrahedrons& one for each of the

    original ertices& and another area in the middle that 'e 'ill

    discard$• "riangle function

    0oid triangle8GLfloat a&GLfloat )&GLfloat c;

    Q

      gl0erte+!f8a;,

      gl0erte+!f8);,

      gl0erte+!f8c;

    S

  • 8/19/2019 Computer Graphics(unit2)

    102/107

    • >e dra' each tetrahedron&coloring each face 'ith a different color)% using the follo'ing function$

    0oid tetra8GLfloat a& &GLfloat )&GLfloat c&GLfloat d;

    Q

      gl4olor!f8colorsZ

  • 8/19/2019 Computer Graphics(unit2)

    103/107

    Q

      GLfloat midZ3[Z![,

      int .,

      if8mY

  • 8/19/2019 Computer Graphics(unit2)

    104/107

    p %8;

    Q

    gl4lear8GL94OLOB97UDDEB97IT;,

      gl7egin8GL9TBIANGLE:;,

      diide9tetra8Z

  • 8/19/2019 Computer Graphics(unit2)

    105/107

    ?idden surface remoal• Drom the ie'er’s position&6uadrilateral A

    is seen clearl%&)ut triangle is )loc*ed fromie'&and triangle 4 is onl% partiall% isi)le$

    •  Algorithms for ordering o).ects so that the%are dra'n correctl% are called visiblesurface algorithms or hidden surface

    removal algorithms.

    • ;6buffer algorithm is used for hiddensurface remoal&that is supported )%openGL$

    • "his algorithm can be turnedonenabled- and offdisabled- easily.

    • >e re6uest the au+iliar% storage&a ; depth- buffer & )%

  • 8/19/2019 Computer Graphics(unit2)

    106/107

    6 % g & p - & %modif%ing the initialiation of the displa% mode to thefollo'ing$

    glutInit5isplay/odeG!"0)IG& A G!"01GB AG!"05&P"-:

    • >e ena)le the algorithm )% the function call gl&nableG05&P"0"&)"-

    either in main or in an initialiation function such asm%init$

    • >e modif% the clear procedure in the displa% functionFgl4lear8GL94OLOB97UDDEB97IT W

    GL9"EPT?97UDDEB97IT;,

  • 8/19/2019 Computer Graphics(unit2)

    107/107

    0oid displa%8;Q

      gl4lear8GL94OLOB97UDDEB97ITWGL9"EPT?97IT;,

      gl7egin8GL9TBIANGLE:;,  diide9tetra8Z