presentation govardhan

Upload: harika-kilari

Post on 06-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Presentation Govardhan

    1/22

    OpenGL

    The success of GL lead to OpenGL (1992), a platform-independent API that was

    Easy to use Close enough to the hardware to get excellent performance Focus on rendering Omitted windowing and input to avoid window system dependencies

    OpenGL Evolution

    Controlled by an Architectural Review Board (ARB) Members include SGI, Microsoft, NVIDIA, HP, 3DLabs, IBM Relatively stable (present version 2.0)

    Evolution reflects new hardware capabilities

    3D texture mapping and texture objects

    Vertex programs Allows for platform specific features through extensions

    12/16/2010 1I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    2/22

    OpenGL Libraries

    OpenGL core libraryOpenGL32 on Windows, GL on most unix/linux systems (libGL.a)

    OpenGL Utility Library (GLU)Provides functionality in OpenGL core but avoids having to rewrite code

    Links with window system

    GLX for X window systems, WGL for Windows, AGL for Macintosh

    GLUT (OpenGL Utility Toolkit)

    Provides functionality common to all window systems

    Open a window, Get input from mouse and keyboard, Menus,

    Event-driven

    Code is portable but GLUT lacks the functionality of a good toolkit for a

    specific platform: No slide bars

    12/16/2010 2I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    3/22

    Software Organization

    GLUT

    GLU

    GL

    GLX, AGL

    or WGL

    X, Win32, Mac O/S

    software and/or hardware

    application program

    OpenGL Motifwidget or similar

    12/16/2010 3I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    4/22

    OpenGL Architecture

    Immediate Mode

    Display

    List

    Polynomial

    Evaluator

    Per Vertex

    Operations &

    PrimitiveAssembly

    RasterizationPer Fragment

    Operations

    Texture

    Memory

    CPU

    Pixel

    Operations

    Frame

    Buffer

    geometry

    pipeline

    12/16/2010 4I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    5/22

    OpenGL Functions Primitives

    Points Line Segments

    Polygons

    Attributes

    Transformations

    Viewing

    Modeling Control (GLUT)

    Input (GLUT)

    Query

    OpenGL is a state machine

    OpenGL functions are of two types

    Primitive generating Can cause output if primitive is visible

    How vertices are processed and appearance of primitive are controlled by the state

    State changing

    Transformation functions

    Attribute functions12/16/2010 5I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    6/22

    Object Orientation

    OpenGL is not object oriented so that there are multiple functions for a given logicalfunction

    glVertex3fglVertex2iglVertex3dv

    Underlying storage mode is the same

    Easy to create overloaded functions in C++ but issue is efficiency

    OpenGL #defines

    Most constants are defined in the include files gl.h, glu.h and glut.h

    Note #include should automatically include the others

    Examples: glBegin(GL_POLYGON)

    glClear(GL_COLOR_BUFFER_BIT)include files also define OpenGL data types: GLfloat, GLdouble

    12/16/2010 6I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    7/22

    OpenGL function format

    glVertex3f(x,y,z)

    belongs to GL library

    function name

    x,y,z are floats

    glVertex3fv(p)

    p is a pointer to an array

    dimensions

    12/16/2010 7I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    8/22

    #include

    void mydisplay(){

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POLYGON);

    glVertex2f(-0.5, -0.5);

    glVertex2f(-0.5, 0.5);

    glVertex2f(0.5, 0.5);

    glVertex2f(0.5, -0.5);

    glEnd();

    glFlush();

    }

    int main(int argc, char** argv){glutCreateWindow("simple");

    glutDisplayFunc(mydisplay);

    glutMainLoop();

    }

    Sample Program

    12/16/2010 8I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    9/22

    #include

    int main(int argc, char** argv)

    {

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);

    glutInitWindowPosition(0,0);

    glutCreateWindow("simple");

    glutDisplayFunc(mydisplay);

    init();glutMainLoop();

    }

    Sample Program

    12/16/2010 9I.Govardhana Rao

  • 8/2/2019 Presentation Govardhan

    10/22

    12/16/2010 I.Govardhana Rao 10

    OpenGL Primitives

    GL_QUAD_STRIPGL_QUAD_STRIP

    GL_POLYGONGL_POLYGON

    GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN

    GL_POINTSGL_POINTS

    GL_LINESGL_LINES

    GL_LINE_LOOPGL_LINE_LOOP

    GL_LINE_STRIPGL_LINE_STRIP

    GL_TRIANGLESGL_TRIANGLES

  • 8/2/2019 Presentation Govardhan

    11/22

    12/16/2010 I.Govardhana Rao 11

    Attributes

    Attributes are part of the OpenGL state and determine the appearance

    of objects

    Color (points, lines, polygons)

    Size and width (points, lines)

    Stipple pattern (lines, polygons)

    Polygon mode

    Display as filled: solid color or stipple patternDisplay edges

    Display vertices

  • 8/2/2019 Presentation Govardhan

    12/22

    12/16/2010 I.Govardhana Rao 12

    RGB color

    Each color component is stored separately in the frame buffer

    Usually 8 bits per component in buffer

    Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), whereas

    in glColor3ub the values range from 0 to 255

  • 8/2/2019 Presentation Govardhan

    13/22

    12/16/2010 I.Govardhana Rao 13

    Indexed Color

    Colors are indices into tables of RGB valuesRequires less memory

    indices usually 8 bits

    not as important now

    Memory inexpensive

    Need more colors for shading

    Color and State

    The color as set by glColor becomes part of thestate and will be used until changed

    Colors and other attributes are not part of the object but are assigned whenthe object is rendered

  • 8/2/2019 Presentation Govardhan

    14/22

    12/16/2010 I.Govardhana Rao 14

    Smooth Color

    Default is smooth shading

    OpenGL interpolates vertex colors across

    visible polygons

    Alternative is flat shadingColor of first vertex

    determines fill color

    glShadeModel

    (GL_SMOOTH)

    orGL_FLAT

  • 8/2/2019 Presentation Govardhan

    15/22

    12/16/2010 I.Govardhana Rao 15

    Viewports

    Do not have use the entire window for the image:

    glViewport(x,y,w,h)

    Values in pixels (screen coordinates)

  • 8/2/2019 Presentation Govardhan

    16/22

    12/16/2010 I.Govardhana Rao 16

    Draw one triangle

    void triangle( GLfloat *a,

    GLfloat *b, GLfloat *c)

    /* display one triangle */

    {

    glVertex2fv(a);

    glVertex2fv(b);

    glVertex2fv(c);

    }

    Connect bisectors of sides and remove central triangle

    Repeat

  • 8/2/2019 Presentation Govardhan

    17/22

    12/16/2010 I.Govardhana Rao 17

    void divide_triangle(GLfloat *a, GLfloat *b, GLfloat*c, int m){/* triangle subdivision using vertex numbers */

    point2 v0, v1, v2;int j;if(m>0){

    for(j=0; j

  • 8/2/2019 Presentation Govardhan

    18/22

  • 8/2/2019 Presentation Govardhan

    19/22

    12/16/2010 19I.Govardhana Rao

    Pixel Maps

    OpenGL works with rectangular arrays of pixels called

    pixel maps or images

    Pixels are in one byte ( 8 bit) chunks

    Luminance (gray scale) images 1 byte/pixel

    RGB 3 bytes/pixel

    Three functions

    Draw pixels: processor memory to frame buffer

    Read pixels: frame buffer to processor memory Copy pixels: frame buffer to frame buffer

  • 8/2/2019 Presentation Govardhan

    20/22

    12/16/2010 20I.Govardhana Rao

    OpenGL Pixel Functions

    glReadPixels(x,y,width,height,format,type,myimage)

    start pixel in frame buffer size

    type of image

    type of pixels

    pointer to processor

    memory

    GLubyte myimage[512][512][3];

    glReadPixels(0,0, 512, 512, GL_RGB,

    GL_UNSIGNED_BYTE, myimage);

    glDrawPixels(width,height,format,type,myimage)

    starts at raster position

  • 8/2/2019 Presentation Govardhan

    21/22

    12/16/2010 21I.Govardhana Rao

    We often work with images in a standard

    format (JPEG, TIFF, GIF)

    How do we read/write such images with

    OpenGL?

    No support in OpenGL

    OpenGL knows nothing of image formats

    Some code available on Web

    Can write readers/writers for some simple

    formats in OpenGL

    Image Formats

  • 8/2/2019 Presentation Govardhan

    22/22

    12/16/2010 22I.Govardhana Rao

    Thank you