geometric modellin

Upload: sarvesh-gantala

Post on 06-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Geometric Modellin

    1/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 1/54

    Geometric Modeling;The Graphics Framework;Introduction to OpenGL

  • 8/2/2019 Geometric Modellin

    2/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 2/54

    Recap

    CG markets: entertainment and science same tools (hw & sw), different apps come talk to me about a Directed Study

    CG paradigms: sample-based graphics (pixels, image

    processing)

    geometry-based graphics Geometric modeling

    primitives decomposition of a model

  • 8/2/2019 Geometric Modellin

    3/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 3/54

    Decomposition of a GeometricModel

    Divide and Conquer Hierarchy of geometrical components Reduction to primitives (e.g., spheres,

    cubes, etc.) Simple vs. not-so-simple elements (nail

    vs. screw)

    Head

    Shaft

    Point

    composition decomposition

  • 8/2/2019 Geometric Modellin

    4/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 4/54

    Hierarchical (Tree) Diagram ofNail

    Object to be modeled is (visually) analyzed, andthen decomposed into collections of primitiveshapes.

    Tree diagram provides visual method ofexpressing composed of relationships of model

    Such diagrams are part of 3D program interfaces(e.g., 3D Studio MAX, Maya)

    As data structure to be rendered, it is called ascenegraph

    Nail

    Head

    (cylinder)

    Body

    root node

    leaf nodes

    Shaft

    (cylinder)

    Point

    (cone)

    tree diagram

  • 8/2/2019 Geometric Modellin

    5/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 5/54

    Composition of a GeometricModel

    Primitives created in decomposition processmust be assembled to create final object.Done with affine transformations, T, R, S(as in above example).

    Other composition operators exist (e.g.,Constructive Solid Geometry CSG -- usesBoolean operators).

    Primitives

    in their ownmodeling

    coordinate system

    Composition

    in world (root)coordinate

    system

    Translate

    Translate and Scale

    Translate and Rotate

  • 8/2/2019 Geometric Modellin

    6/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 6/54

    2D Primitives

    Line

    X

    Y

    Polyline

    X

    Y

    Polygon

    X

    Y

    Circle

    X

    Y

  • 8/2/2019 Geometric Modellin

    7/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 7/54

    Curves

    Piecewise linear approximation

    Splines: higher-order polynomials piecewise curvilinear approximation

    French Curves Draftmans Spline

    Mathematical Splines

    Natural Cubic Spline:

    (duck)

  • 8/2/2019 Geometric Modellin

    8/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 8/54

    Example 3D PrimitivesPolyhedron

    Sphere

    Polyline

    Patch

    (spline boundary curve)

  • 8/2/2019 Geometric Modellin

    9/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 9/54

    What Kind of Math do We Need?Cartesian Coordinates

    Typically modeling space is floating point,screen space is integer

    NB:Often, screen coordinates are measured top tobottom, based on raster scan

    (0,0)

    x, y Cartesian gridInteger Grid

  • 8/2/2019 Geometric Modellin

    10/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 10/54

    Conceptual Framework forInteractive Graphics

    Graphics library/package (e.g., OpenGL) isintermediary between application and displayhardware (Graphics System)

    Application program maps application objects toviews (images) of those objects by calling ongraphics library

    This hardware and software framework is morethan 4 decades old but is still useful, indeeddominant

    Applicationprogram

    GraphicsLibrary(GL)

    GraphicsSystem

    applicationmodel

    (objects)

  • 8/2/2019 Geometric Modellin

    11/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 11/54

    2D Hw/Sw Topics

    a) Display Hardware: Raster scan vs. Vector

    b) Color Tables

    indirect specification of (pseudo) color color correction, simple types of animation

    c) BitBlt/RasterOp for operating on blocks ofpixels

  • 8/2/2019 Geometric Modellin

    12/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 12/54

    a) Graphics Display Hardware Vector (calligraphic, stroke, random-scan)

    still used in some plotters

    Raster (TV, bitmap, pixmap), used in displays andlaser printers

    Ideal Drawing Vector Drawing

    Raster

    Outline primitives Filled primitives

  • 8/2/2019 Geometric Modellin

    13/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 13/54

    2D Raster Architecture

    Raster displays store images (pixmaps, orbitmaps) in a frame buffer, also known asbitmapbuffer, or refresh buffer

    The frame bufferis a chunk of memory locatedeither in separate hardware (VRAM) or in CPUsmain memory (DRAM)

    The frame buffercan be accessed directly,through memory operations

    Video controller draws all scan-lines at consistent> 60 Hz; separates update rate of the frame buffer and

    refresh rate of the display contains Color-Table

    DisplayFrame buffer

  • 8/2/2019 Geometric Modellin

    14/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 14/54

    b) Color Tables: 1-bit vs. n-bit

    Display

    2n intensities or colors

  • 8/2/2019 Geometric Modellin

    15/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 15/54

    Image Display SystemLook-up Table

    Any specific 2n colors may be inadequate (n may be as lowas 16 in low-end systems)

    Look-up table allows 2n colors out of 224 colors to be usedin one image, some other 2n in another image

    224 = approx. 16.7 million, exceeds eyes ability todiscriminate (somewhere between 7-10 million)

    Color table is resource managed (usually) by windowmanager

  • 8/2/2019 Geometric Modellin

    16/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 16/54

    Color LookUp Table Operation

    Pixel value is indexed to color look up table (CLUT)where color is stored. Here we use only 12 bits(4bits per color) for clarity typically, 24 bits areused

    CLUT look-up done at video rates In 24-bit true color systems, 3 x 8 bits for R, G,

    B; each color has its own 8-bit CLUT (0-255)

    CLUT allows variety of effects fast image changes: change table rather than

    stored image multiple images: select or composite/blend

    animation hack

  • 8/2/2019 Geometric Modellin

    17/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 17/54

    c) BitBlt/RasterOp (1/3) Logically operate on each pixel in

    rectangular source and destinationregions in same or different pixmaps to

    achieve dynamics, e.g., to move/scrollwindows on screen

    RasterOp (Source, Destination) Destination

    In some implementations S and D neednot be same size

    pixmap stored inframe buffer

  • 8/2/2019 Geometric Modellin

    18/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 18/54

    BitBlt/RasterOp (2/3) AND (S,D): S can mask out pixels in D OR (S,D): S is non-destructively added to D;

    used for painting, transparent and kernedcharacters (where characters extend beyond their

    boxes)

    Heres how you can use them:Lets say you want to add some game sprites to background

    0 (black) AND anything is 0, 1 (white) AND anything is anything

    AND =

    1. Mask out with black

    0 OR anything is anything

    OR =

    2. Add sprites in

  • 8/2/2019 Geometric Modellin

    19/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 19/54

    More BitBlt/RasterOp (3/3) Replace (S,D): S destructively replaces D, i.e., is

    deleted and copied on top of D (also called Move); usedfor making opaque characters, icons, scroll

    Copy (S,D) as above, but S is not deleted XOR (S,D) S selectively inverts D; used in 1-bit systems

    for cheap cursors:

    Note: effects in color systems for all but replace may beweird

    S XOR (S XOR D) = D 0 = W, 1 = B

    S D D

    S D D

    XOR

    XOR

  • 8/2/2019 Geometric Modellin

    20/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 20/54

    Recap

    Geometry-based paradigm (modeldecomposition, scenegraphs)

    The Graphics Framework 2D hw/sw select topics

    display hw: vector vs. raster; the frame-buffer, the video controller, the display

    color-tables BitBlt operation

    Applicationprogram

    GraphicsLibrary(GL)

    GraphicsSystem

  • 8/2/2019 Geometric Modellin

    21/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 21/54

    Next: the Graphics Library

    Application

    model

    Application

    program

    GraphicsLibrary

    (GL)

    Graphics

    System

  • 8/2/2019 Geometric Modellin

    22/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 22/54

    Graphics Library examples: OpenGL, DirectX, X3D provides representations and support for:

    primitives attributes

    color line style

    material properties for 3D

    lights transformations

    immediate mode vs. retained mode immediate mode: no stored representation,

    package holds only attribute state, andapplication must completely draw each frame;simple to use but slow; requires at least one

    function call for every vertex to be drawn. retained mode: library compiles and displays

    from scenegraph; give the library a pointer to abunch of Vertex data and tell it to render fromthere

  • 8/2/2019 Geometric Modellin

    23/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

    credits: van Dam, R. Hwa September 6, 2011 OpenGL 23/54

    What is OpenGL?

    The OpenGraphics Library 3-D graphics API specificationa software interface to graphics hardware1 raster graphics library

    pass in vertices, normals, and otherscene data

    get pixels out industry standard

    supported across many platforms Mac OS, Windows, Linux, iPhone,

    PSP specification publicly available

    1 The OpenGL Graphics System: A Specification Version 3.0

  • 8/2/2019 Geometric Modellin

    24/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

    credits: van Dam, R. Hwa September 6, 2011 OpenGL 24/54

    OpenGL Architecture (1/2)

    OpenGL uses a client-server model client sends commands to the server server interprets, processes commands note: client and server usually on the same

    computer, but need not be your program = client OpenGL = server

    example interaction:program OpenGL

    begin trianglenormal (0, 0, -1)

    vertex (-1, 1, -1, 1)vertex (1, -1, -1, 1)

    vertex (-1, -1, -1, 1)

    end triangle

  • 8/2/2019 Geometric Modellin

    25/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

    credits: van Dam, R. Hwa September 6, 2011 OpenGL 25/54

    OpenGL Architecture (2/2)

    OpenGL is state-full and procedural the current OpenGL state (collectively called a

    context) contains data describing how rendering

    should proceed

    ex: current color, lights, textures, etc state does not change until explicitly set

    once some state is set, it remains in effect until changed considering we are working with hardware, this makes

    some sense want to have precise control

    procedural model usually accessed through a plain C API NOT object-oriented at all (though this changes gradually

    in OpenGL 3.1 and beyond)

    can be cumbersome if you are used to workingwith object-oriented systems

    one line can affect how all subsequent code executes sometimes difficult to encapsulate functionality have to be mindful of default state as well

  • 8/2/2019 Geometric Modellin

    26/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

    credits: van Dam, R. Hwa September 6, 2011 OpenGL 26/54

    OpenGL vs. Direct3D

    Direct3D is Microsofts low-level 3D graphics API and plays a similar role toOpenGL in the graphics pipeline.

    Though the APIs feel somewhat different, at the end of the day, they providebasically the same functionality.

    Differences OpenGL is a specification with several free implementations on different

    platforms, while Direct3D is a proprietary API written by Microsoft.

    Direct3D exposes the programmer to the graphics hardware much morethan OpenGL does. While OpenGL certainly supports hardware

    acceleration, it generally tries to abstract away details of dealing with the

    hardware. Direct3D on the other hand, allows much more fine-grainedcontrol but sacrifices some ease of use to do so.

    For Windows users, D3Ds tight coupling with windows has itsadvantages

    Development environment is standardized and robust. D3D development istightly integrated with Visual Studio and other powerful tools Direct3D provides a much more object-oriented API than GL. Still, many would argue Direct3D is clunkier and harder to use than

    OpenGL.

    Overall, OpenGL is more widely used than Direct3D due to its simplicityand availability on a wider range of platforms. (both used in the games

    industry, however OpenGL used also w/out fail in scivis.)

  • 8/2/2019 Geometric Modellin

    27/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 27/54

    OpenGL, GLU, and GLUT

    OpenGL (Graphics Library) offers a platform-independent software

    interface with graphics hardware

    supports basic geometric primitives (points,lines, polygons)

    sets up an environment for graphicalprogramming

    May be useful to think of it as a statemachine

    GLU (OpenGL Utility Library) higher-level features (e.g. curved surfaces)

    built out of basic OpenGL functions

    GLUT (OpenGL Utility Toolkit) separate library by Mark Kilgard, platform

    independent

    hides details about basic window operationsand context creation

    a simple framework for writing OpenGLapplications independent of any particularplatform

  • 8/2/2019 Geometric Modellin

    28/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 28/54

    Anatomy of an OpenGL program

    #include #include int main(int argc, char **argv) {

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400, 300);glutInitWindowPosition(0,0);glutCreateWindow("GLUT Skeleton");

    //initialize callbacks here//

    my_setup();

    glutMainLoop();

    return(0);}

  • 8/2/2019 Geometric Modellin

    29/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 29/54

    Single vs. Double Buffering

    Use two buffers (two chunks of memory) Draw always on the back buffer When done drawing, swap buffers

  • 8/2/2019 Geometric Modellin

    30/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 30/54

    glutInitDisplayMode()

    #include #include int main(int argc, char **argv) {

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400, 300);glutInitWindowPosition(0,0);glutCreateWindow("GLUT Skeleton");

    //initialize callbacks here//

    my_setup();

    glutMainLoop();return(0);

    } GLUT_DOUBLE, GLUT_SINGLE GLUT_RGB, GLUT_RGBA, GLUT_INDEX GLUT_DEPTH

  • 8/2/2019 Geometric Modellin

    31/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 31/54

    glutInitWindow*()

    #include #include int main(int argc, char **argv) {

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400, 300);glutInitWindowPosition(0,0);glutCreateWindow("GLUT Skeleton");

    //initialize callbacks here//

    my_setup();

    glutMainLoop();return(0);

    }

  • 8/2/2019 Geometric Modellin

    32/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 32/54

    glutCreateWindow()

    #include #include int main(int argc, char **argv) {

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400, 300);glutInitWindowPosition(0,0);glutCreateWindow("GLUT Skeleton");

    //initialize callbacks here//

    my_setup();

    glutMainLoop();return(0);

    } glutCreateWindow merely requests a window

  • 8/2/2019 Geometric Modellin

    33/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 33/54

    Anatomy of an OpenGL program

    #include #include int main(int argc, char **argv) {

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400, 300);glutInitWindowPosition(0,0);glutCreateWindow("GLUT Skeleton");

    //initialize callbacks here//

    my_setup();

    glutMainLoop();

    return(0);}

  • 8/2/2019 Geometric Modellin

    34/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 34/54

    glutMainLoop()

    Event-Driven Programming Interactive graphics program are typically

    event-driven

    Main program goes into a loop, waitingfor things to happen e.g., keyboard, mouse inputs, etc. when an interesting event occurs, callback

    function returns control to program

    The events can be user-input generated (mouse, keyboard etc) system-generated (window creation,

    overlapping-window removed etc)

  • 8/2/2019 Geometric Modellin

    35/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 35/54

    Callbacks

    OpenGL handles events through themechanism of callbacks:

    associate events with procedures procedure gets called-backwhenever its associated event takesplace

    Every OpenGL program must handlethe display event

  • 8/2/2019 Geometric Modellin

    36/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 36/54

    Common Callbacks

    Input events: glutMouseFunc glutKeyboardFunc

    System events: glutDisplayFunc (!!!) glutReshapeFunc glutTimerFunc glutIdleFunc

    Example: setting up callbacksglutDisplayFunc( my_display);

    glutIdleFunc( my_idle );

  • 8/2/2019 Geometric Modellin

    37/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 37/54

    Example my_display()

    void my_display(void) {

    //insert your drawing code always here

    glClear(GL_COLOR_BUFFER_BIT) ;

    glColor3f(0,0,1) ;

    glBegin(GL_POLYGON);glVertex2f(-0.25, 0.75);glVertex2f(0.95, 0.75);glVertex2f(0.95, -0.55);glVertex2f(-0.25, -0.55);

    glEnd();

    glutSwapBuffers();

    return ;}

  • 8/2/2019 Geometric Modellin

    38/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 38/54

    The Callback for Displaying

    my_displaygets called: at initial creation of the window when window becomes unobstructed when the program requests it

    glutPostRedisplay()

  • 8/2/2019 Geometric Modellin

    39/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 39/54

    A Simple Display Function

    void my_display(void) {

    glClear(GL_COLOR_BUFFER_BIT) ;glColor3f(0,0,1) ;

    glBegin(GL_POLYGON);glVertex2f(-0.1, 0.7);glVertex2f(0.9, 0.7);glVertex2f(0.9, -0.3);glVertex2f(-0.1, -0.3);

    glEnd();

    glutSwapBuffers();return ;

    }

  • 8/2/2019 Geometric Modellin

    40/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 40/54

    What Goes Between glBeginand glEnd Specification of mode

    GL_POINTS GL_LINES, GL_LINE_STRIP,

    GL_LINE_LOOP

    GL_POLYGON GL_TRIANGLES, GL_TRIANGLE_STRIP,

    GL_TRIANGLE_FAN

    GL_QUADS, GL_QUADS_STRIP Declaration of vertices

    glVertex{2|3|4}{i|f|d}[v] integer, float, or double

    Attributes Color Point size

    Line width, style Patterns for filled polygons

  • 8/2/2019 Geometric Modellin

    41/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 41/54

    How does the output change

    with respect to different modes?

    void my_display(void) {

    glClear(GL_COLOR_BUFFER_BIT) ;

    glColor3f(0,0,1) ;

    glBegin(??);glVertex2f( 0.0, 0.9);glVertex2f(-0.5,-0.9);glVertex2f( 0.9, 0.2);

    glVertex2f(-0.9, 0.2);glVertex2f( 0.5,-0.9);

    glEnd();

    glutSwapBuffers();return ;

    }

  • 8/2/2019 Geometric Modellin

    42/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 42/54

    Mouse Inputs

    Associate mouse events with my_mouseprocedure:glutMouseFunc(my_mouse)

    Write your own code inside my-mouse:void my_mouse(int button, int state, int

    x, int y)

    where button can take these values:

    GLUT_LEFT_BUTTON,GLUT_MIDDLE_BUTTON,GLUT_RIGHT_BUTTON

    state:GLUT_UP, GLUT_DOWN

    x, y: position in window (in pixels) with origin at

    upper left

  • 8/2/2019 Geometric Modellin

    43/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 43/54

    Drawing Space

    void my_display(void) {

    glBegin(GL_POLYGON);glVertex2f(-0.1, 0.7);

    glVertex2f(0.9, 0.7);glVertex2f(0.9, -0.3);glVertex2f(-0.1, -0.3);

    glEnd();

    glutSwapBuffers();return ;

    }

    void glut_setup(void) {

    glutInitWindowSize(400,100);

    glutInitWindowPosition(0,0);

    glutDisplayFunc(my_display);

    return ;

    }

    What is the expected image output?

  • 8/2/2019 Geometric Modellin

    44/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 44/54

    Viewport

    Defines where in the window todisplay the image

    Uses within window coordinatesystem (pixel unit; origin at lower

    left corner)

    OpenGL default: set to be theentire window

    default window reshape: setviewport to whole window

  • 8/2/2019 Geometric Modellin

    45/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 45/54

    Adding-in a Viewport Function

    Define callback: my_reshape(w,h) Otherwise, viewport will be set to whole window

    again

    In the function my_reshape call:glViewport(origin_x,origin_y,

    width,height); note that origin is at the lower left corner

  • 8/2/2019 Geometric Modellin

    46/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 46/54

    Define User-Friendly

    Coordinate System More intuitive instead of square in the interval

    of [-1,1]

    Graphics system does the mapping in OpenGL, this is done with theprojection

    matrix

    glMatrixMode (GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(left,right,bottom,top);

  • 8/2/2019 Geometric Modellin

    47/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    credits: van Dam,R. Hwa September 6-8, 2011 OpenGL 47/54

    Changes of Co-OrdinatesglBegin(GL_POLYGON);

    glVertex2i(1,2);glVertex2i(7,2);

    glVertex2i(4,10);glEnd();

    gluOrtho2D(2,8,5,12);

    glViewPort(0,0,500,300);

    glutInitWindowSize(800,500);

    glutInitWindowPosition(0,0);

  • 8/2/2019 Geometric Modellin

    48/48

    I N T R O D U C T I O N T O C O M P U T E R G R A P H I CS

    Bonus: Operating on the FrameBuffer

    Steps:1.Allocate space for pixel array

    1 x width x height x sizeof(pixel) array of pixels(grayscale image) or

    3 x width x height sizeof(pixel) array (RGBimage) or

    4 x width x height array sizeof(pixel) (RGBAimage) etc

    2.Initialize array with original image values

    3.Perform desired operations on pixel array

    4.Write array to Frame Buffer:

    glRasterPos( x, y ) specify x, y position wherewe want to write some pixels

    glDrawPixels( , *pixel_array) write a blockof pixels to the frame buffer