Getting Started With OpenGL - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Getting Started With OpenGL

Description:

The letter after the digit (an b, s, i, f, or d normally) indicates the type of the coordinates ... { double angle; glBegin( GL_LINES); for ( angle = 0; angle ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 42
Provided by: eecsUt
Category:

less

Transcript and Presenter's Notes

Title: Getting Started With OpenGL


1
Getting Started With OpenGL
  • August 24, 2005

2
Primitives
  • OpenGL supports a number of primitives that
    include
  • Points (GL_POINTS)
  • Lines (GL_LINES), Line Strips or Polylines
    (GL_LINE_STRIP), and Line Loops (GL_LINELOOP)
  • Triangles (GL_TRIANGLES), Triangle Strips
    (GL_TRIANGLE_STRIP), and Triangle Fans
    (GL_TRIANGLE_FAN)
  • Quadrangles (GL_QUADS) and Quadrangle Strips
    (GL_QUAD_STRIP)
  • Polygons (GL_POLYGON )

3
Drawing Primitives
  • All primitives are drawn in a similar fashion
  • glBegin (GL_????) // starts the primitive
  • glVertex3f ( xcoord, ycoord, zcoord) // defines
    the next vertex
  • glEnd( ) // ends the primitive

4
glVertex
  • There are many glVertex calls in OpenGL. The
    digit after the glVertex can be a 2, 3, or a 4
    which indicates the number of values for the
    coordinate (2 2 D, 3 3 D, 4 Homogeneous
    Coordinates)
  • The letter after the digit (an b, s, i, f, or d
    normally) indicates the type of the coordinates
  • b indicates bytes
  • s indicates shorts
  • i indicates ints
  • f indicates floats
  • d indicates doubles

5
Other Vertex Modifiers
  • Can set the color on a per vertex basis
  • glColor34bsifd
  • glColor3bsifd gives color in terms of red,
    green, and blue components.
  • glColor4bsifd gives color in terms of red,
    green, blue, and alpha components. You could
    consider alpha to be a "transparency" factor.

6
Color Cubes
  • red, green, and blue are between 0.0 and 1.0 for
    each component with floats and doubles.
  • 0 to 255 for bytes
  • 0 to 32,767 for ints

7
Normals for the Cornerscan also be specified
  • glNormal3bsifd
  • For several reasons OpenGL would prefer unit
    normals but doesn't require them.

8
Vector Calls
  • Much easier to pass an array (or vector) of
    values for some of these. Some OpenGL calls
    support Vector versions as well that take a 1 D
    array as the parameter.
  • Examples
  • glVertex234sifdv
  • glColor34bsifdv
  • glNormal3bsifdv

9
Different Ways to Draw an Object.
  • Let us assume we want to draw a cone where the
    base and the point are aligned on the Y axis.
  • We can draw approximations of it using a number
    of methods and primitives.

10
One Version -- Lines
  • void drawCone ( double baseRadius, double height,
    int numberOfSections) double
    angle glBegin( GL_LINES) for ( angle 0
    angle lt 2 3.14159 angle 2.0
    3.14159 / numberOfSections) glVertex3d (
    sin(angle)baseRadius, 0.0,
    cos(angle)baseRadius, 0.0) glVertex3d ( 0.0,
    0.0, height)

11
  • glEnd( )glBegin(GL_LINE_LOOP)for (angle
    0.0 angle lt 3.14159 angle 3.14159 /
    numberOfSections) glVertex3d(sin(angle)baseRad
    ius, 0.0, cos(angle)baseRadius)glEnd()
    return

12
Version 2 Solids (Triangles)
  • void drawCone (double baseRadius, double
    height, int nbrOfSections) double
    angleglBegin(GL_TRIANGLE_FAN)glVertex3d(0.0,
    height, 0.0)for (angle 0.0 angle lt 2.0
    3.14159 angle 3.14159 2.0 /
    nbrOfSections) glVertex3d(sin(angle)baseRadius
    , 0.0, cos(angle) baseRadius)glEnd(
    )

13
  • glBegin(GL_TRIANGLE_FAN)glVertex3d(0.0, 0.0,
    0.0)for (angle 0.0 angle lt 2.0 3.14159
    angle 2.0 3.14159 / nbrOfSections)
    glVertex3d(sin(angle) baseRadius, 0.0,
    cos(angle) baseRadius)glEnd(
    )return

14
How would we integrate this into a GLUT Program?
  • int main(int argCount, char argValues)
    glutInit( argCount, argValues) glutInitDi
    splayMode(GLUT_RGBA) glutInitWindowSize(600,
    600) glutCreateWindow("Main Window") glutDis
    playFunction(display) init( ) glutMainLoop()
    return EXIT_SUCCESS

15
display Routine
  • void display( ) glMatrixMode(GL_MODELVIEW)
    glClear(GL_COLOR_BUFFER_BIT) drawCone(1.0,
    3.0, 36) glFlush()

16
init routine
  • void init( ) glClearColor(0.0, 0.0, 0.0,
    0.0)glMatrixMode(GL_PROJECTION)glLoadIdentity(
    )glOrtho(-3.0, 3.0, -1.0, 5.0, -3.0,
    3.0)glMatrixMode (GL_MODELVIEW)

17
Other Features
  • People also like to have handlers to capture
    keystrokes (to quit a program for example) in
    their code. This can be added as
  • glutKeyboardFunc( processKeys )

18
processKeys Code
  • void processKeys ( unsigned char keyCode, int x,
    int y)
  • if (keyCode 'q') exit(0) else if
    (keyCode '?')
  • // whatever.

19
Attributes
  • We mentioned Attributes as an item to change the
    appearance of primitives. The easiest one is the
    one to set the color.
  • glColor3usifd( red, green, blue)

20
Attributes Lines
  • For lines and edges we can set the width and
    style of the line.
  • glLineWidth( width )
  • glLineStipple ( scale, pattern)
  • glEnable (GL_LINE_STIPPLE) // need to enable
    the system to use stippilng
  • The argument for width is a scale factor. The
    bigger the number the wider the line.
  • The pattern argument for the line stipple is a 16
    bit pattern where the line is drawn for 1's and
    blank areas for 0's.
  • The scale argument for the line stipple is an
    integer to enlarge the pattern by. (2 times, 3
    times, 10 times, etc)

21
Attributes Of Polygons, Triangles, and
Quadrilaterals
  • What gets Drawn?
  • glPolygonMode ( GLenum face, GLenum mode)
  • face is one of GL_FRONT, GL_BACK, and
    GL_FRONT_AND_BACK.
  • mode is one of GL_POINT, GL_LINE, GL_FILL.

22
Stippling Polygons
  • glEnable (GL_POLYGON_STIPPLE)
  • glPolygonStipple (stipplePattern)
  • The stipple pattern must be an array of unsigned
    bytes that holds a 32 x 32 bitmap of the stipple.
    1's are drawn, 0's are not.

23
Where are we at?
  • We should now be able to draw what we need using
    OpenGL calls.
  • We don't have all the viewing options we would
    like.
  • There is no shading/lighting specified yet so
    things look a little flat.

24
Complex Objects
  • We have a few options if we wish to draw a
    complex object.
  • Write a series of glBegin's, glEnd's, and
    glVertex's
  • Write a procedure to generate the shape given
    some parameters.
  • Both of these store the information in the
    program could we read it in from a file.
  • We could.
  • Some advantages in that it may be easier to
    change.

25
How big can Objects Be?
  • They could be thousands of polygons in size.
    (that gets rid of coding into the program!)
  • Even reading it in that's a lot of calls!
  • Vector Calls (Vertex Arrays)
  • OpenGL has some calls that work on groups of
    points stored in arrays. These are the vertex
    calls.

26
This is a model that contains 69451 triangles!
27
How Does a Vertex Array Work?
  • double points 3 0.0, 0.0, 0.0,
    1.0, 0.0, 0.0,
  • 1.0, 1.0, 0.0, 0.0, 1.0,
    0.0
  • glEnableClientState(GL_VERTEX_ARRAY)
  • glVertexPointer(3, GL_DOUBLE, 0, points)
  • glDrawArrays(GL_QUAD, 0, 4)

28
Why Do That?
  • Fewer calls
  • May be faster due to call overhead.

29
Saving and Restoring Attributes
  • glPopAttrib()
  • glPushAttrib(mask)
  • The mask may be or'd between a whole slew of
    values but the ones that would be most useful at
    the moment are
  • GL_LINE_BIT
  • GL_COLOR_BUFFER_BIT
  • GL_POINT_BIT
  • GL_POLYGON_BIT
  • GL_POLYGON_STIPPLE_BIT
  • GL_ALL_ATTRIB_BITS

30
What Primitives Are We Missing?
  • One primitive that OpenGL does not directly
    support is text!
  • Glut has a facility for generating text if
    needed.
  • glutStrokeCharacter( font, character)
  • font is one of GLUT_STROKE_ROMAN and
    GLUT_STROKE_MONO_ROMAN
  • character is a single character value (not a
    string!)
  • glutBitmapCharacter(font, character)
  • Same idea, different fonts.

31
Sources of Information
  • www.opengl.org

32
Windows Example
  • include ltwindows.hgtinclude ltGL/gl.hgtinclude
    ltGL/glu.hgtinclude ltwingdi.hgt
  • char applicationName "OpenGL With Windows"
  • / set up the pixel format for a given device
    context. Need to have a format that is
    appropriate for an OpenGL Rendering
    Context./

33
  • void SetDrawingContextPixelFormat( HDC
    handleDeviceContext )
  • int pixelFormat static PIXELFORMATDESCRIPTOR
    pfdesc sizeof(PIXELFORMATDESCRIPTOR), //
    Size of the structure 1, // version
    PFD_DRAW_TO_WINDOW PFD_SUPPORT_OPENGL
    PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, // set up as
    Red-green-blue-alpha 32, // 32 bit
    color 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 16, // Depth buffer depth in bits 0,
    0, 0, 0, 0, 0, 0

34
  • // Choose a pixel format supported by the
    machine// that matches the given description as
    closely as// possible.
  • pixelFormat ChoosePixelFormat(handleDeviceConte
    xt, pfdesc )
  • SetPixelFormat ( handleDeviceContext,
    pixelFormat, pfdesc)
  • return

35
  • LRESULT CALLBACK WndProc(HWND window, UINT
    message, WPARAM wordParameter, LPARAM
    longParameter)
  • static HGLRC renderingContext NULL static
    HDC deviceContext NULL static float angle
    0.0 switch (message) case WM_CREATE //
    Called on window creation deviceContext
    GetDC(window) // create the device
    and rendering SetDrawingContextPixelFormat(devic
    eContext) // contexts renderingContext
    wglCreateContext(deviceContext) wglMakeCurren
    t(deviceContext, renderingContext)
  • SetTimer(window, 30000, 1, NULL) //
    set up the timer to expire periodically glClearC
    olor(0.0, 0.0, 0.0, 0.0) glMatrixMode(GL_PROJEC
    TION) glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0,
    2.0) glMatrixMode(GL_MODELVIEW) break

36
  • case WM_DESTROY // Called when the
    application ends
  • KillTimer(window, 101) // Kill the time
    and release
  • wglMakeCurrent(deviceContext, NULL) // the
    contexts
  • wglDeleteContext(renderingContext)
  • PostQuitMessage(0) // Tell the
    application to exit
  • break
  • case WM_SIZE // Called on resize
  • break

37
  • case WM_TIMER // Timer callback InvalidateRec
    t(window, NULL, FALSE) // Force a redraw
    -- the timer has rung break
  • case WM_PAINT // Called on redisplay / redraw
  • // Do the OpenGL drawing.... glMatrixMode(GL
    _MODELVIEW) glClear(GL_COLOR_BUFFER_BIT) g
    lLineWidth(8.0) glPolygonMode(GL_FRONT_AND_BAC
    K, GL_LINE) glColor3f(1.0, 1.0,
    1.0) glBegin(GL_TRIANGLES) glVertex3f(0.0
    , 0.0, 0.0) glVertex3f(0.0, 1.0,
    0.0) glVertex3f(1.0, 0.0, 0.0) glEnd()
    SwapBuffers(deviceContext) // Swap
    front and back buffers ValidateRect(window,
    NULL) break

38
  • default
  • return DefWindowProc(window, message,
    wordParameter, longParameter)
  • return 0L

39
  • int APIENTRY WinMain ( HINSTANCE instance,
    HINSTANCE previousInstance, LPSTR cmdLine, int
    cmdShow)
  • MSG message WNDCLASS windowClass HWND
    window
  • const unsigned int xPosition 50, yPosition
    50, initialWidth 600, initialHeight 600
  • windowClass.style CS_HREDRAW
    CS_VREDRAW CS_OWNDC
  • windowClass.lpfnWndProc WndProc windowClass.c
    bClsExtra 0 windowClass.cbWndExtra
    0 windowClass.hInstance instance windowCla
    ss.hIcon NULL windowClass.hCursor
    LoadCursor(NULL, IDC_ARROW) windowClass.hbrBackg
    round NULL windowClass.lpszMenuName
    NULL windowClass.lpszClassName
    applicationName

40
  • if (RegisterClass(windowClass) 0) return
    FALSE
  • window CreateWindow ( applicationName, applic
    ationName, WS_OVERLAPPEDWINDOW
    WS_CLIPCHILDREN WS_CLIPSIBLINGS, xPosition,
    yPosition, initialWidth, initialHeight, NULL,
    NULL, instance, NULL)
  • if (window NULL) return FALSE

41
  • ShowWindow(window, cmdShow) UpdateWindow(window
    )
  • while (GetMessage(message, NULL, 0, 0))
    TranslateMessage(message) DispatchMessage(
    message)
  • return message.wParam
Write a Comment
User Comments (0)
About PowerShow.com