OpenGL Programming for Windows 95 and Windows NT - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

OpenGL Programming for Windows 95 and Windows NT

Description:

create a device context from the current window. CclientDC mydc(this) ... now create a pfd and fill it with what we'd like. PIXELFORMATDESCRIPTOR pfd ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 35
Provided by: tcl2
Category:

less

Transcript and Presenter's Notes

Title: OpenGL Programming for Windows 95 and Windows NT


1
OpenGL Programming forWindows 95 and Windows NT
CS580 Computer Graphics
  • 1998
  • Bomjun Kwon
  • Dept. of Computer Science
  • KAIST

2
Contents
  • 1. Overview
  • 2. Pixel Formats
  • 3. Rendering Contexts
  • 4. Fonts and OpenGL
  • 5. Double Buffering
  • 6. OpenGL and Libraries
  • 7. Creating a Simple OpenGL Program

3
1. Overview
  • Reference
  • Ron Fosner, OpenGL Programming for Windows 95 and
    Windows NT, Addison Wesley Developers Press,
    1997.
  • Basic steps
  • getting a device context
  • selecting and setting a pixel format
  • creating a rendering context
  • drawing using OpenGL commands
  • releasing the rendering context
  • releasing the device context

4
  • Device contexts (DC)
  • a rendering handle to the currently selected
    device
  • graphics device interface (GDI)
  • Windows original 2D graphics interface
  • draws to any device that can process GDI calls
  • all GDI calls pass through a DC
  • Rendering contexts (RC)
  • the OpenGL equivalent of the GDI DC
  • the portal through which OpenGL calls are
    rendered to the device
  • Pixel formats
  • you can modify them to optimize your OpenGL
    program

5
2. Pixel Formats
  • The translation layer
  • The functions that handle the pixel format
  • ChoosePixelFormat()
  • SetPixelFormat()
  • GetPixelFormat()
  • DescribePixelFormat()

OpenGL calls
The rendering operation that Windows Performs
how colors are displayed, the depth of field
resolution, etc.
6
  • Pixel format structure
  • single or double buffering
  • RGBA or color indexing
  • drawing to a window or bitmap
  • support of GDI or OpenGL calls
  • color depth
  • z-axis depth
  • stencil buffer
  • visibility masks

7
/ Pixel format descriptor / typedef struct
tagPIXELFORMATDESCRIPTOR WORD nSize
WORD nVersion DWORD dwFlags BYTE
iPixelType BYTE cColorBits BYTE
cRedBits BYTE cRedShift BYTE
cGreenBits BYTE cGreenShift BYTE
cBlueBits BYTE cBlueShift BYTE
cAlphaBits BYTE cAlphaShift BYTE
cAccumBits BYTE cAccumRedBits BYTE
cAccumGreenBits BYTE cAccumBlueBits
BYTE cAccumAlphaBits BYTE cDepthBits
BYTE cStencilBits BYTE cAuxBuffers
BYTE iLayerType BYTE bReserved DWORD
dwLayerMask DWORD dwVisibleMask DWORD
dwDamageMask PIXELFORMATDESCRIPTOR,
PPIXELFORMATDESCRIPTOR, FAR LPPIXELFORMATDESCRIP
TOR
8
  • nSize the size in bytes of the data structure,
    sizeof(PIXELFORMATDESCRIPTOR)
  • nVersion the version of the structure
  • dwFlags properties of the pixel buffer
  • PFD_DOUBLEBUFFER
  • PFD_STEREO
  • PFD_DRAW_TO_WINDOW
  • PFD_DRAW_TO_BITMAP
  • PFD_SUPPORT_GDI
  • PFD_SUPPORT_OPENGL
  • PFD_GENERIC_FORMAT
  • PFD_NEED_PALETTE
  • PFD_NEED_SYSTEM_PALETTE
  • PFD_SWAP_EXCHANGE
  • PFD_SWAP_COPY
  • PFD_SWAP_LAYER_BUFFERS
  • PFD_GENERIC_ACCELERATED
  • PFD_DEPTH_DONTCARE
  • PFD_DOUBLEBUFFER_DONTCARE
  • PFD_STEREO_DONTCARE

9
  • cGreenBits the number of green bit planes in each
    RGBA buffer
  • cGreenShift the shift count for green bit planes
    in each RGBA buffer
  • cBlueBits the number of blue bit planes in each
    RGBA buffer
  • cBlueShift the shift count for blue bit planes in
    each RGBA buffer
  • cAlphaBits the number of alpha bit planes in each
    RGBA buffer
  • cAlphaShift the shift count for alpha bit planes
    in each RGBA buffer
  • cAccumBits the total number of bit planes in the
    accumulation buffer
  • cAccumRedBits the number of red bit planes in the
    accumulation buffer
  • cAccumGreenBits the number of green bit planes in
    the accumulation buffer
  • cAccumBlueBits the number of blue bit planes in
    the accumulation buffer
  • cAccumAlphaBits the number of alpha bit planes in
    the accumulation buffer
  • cDepthBits the number of bits of the depth
    (z-axis) buffer (generally 0, 16, 24, 32)
  • cStencilBits the number of bits of the stencil
    buffer
  • cAuxBuffers the number of auxiliary buffers
  • iLayerType the type of layer
  • PFD_MAIN_PLANE, PFD_OVERLAY_PLANE,
    PFD_UNDERLAY_PLANE
  • bReserved not used, must be zero
  • dwLayerMask obsolete in OpenGL 1.1 or later
  • dwVisibleMask the visible mask

10
  • Examining a pixel format
  • // create a device context from the current
    window
  • CclientDC mydc(this)
  • // get a pixel format descriptor variable
  • PIXELFORMATDESCRIPTOR pfd
  • int MaxNumberPixelFormats
  • // if it succeeds, it returns the total number of
    formats
  • MaxNumberPixelFormats
  • DescribePixelFormat(
  • mydc.m_hdc, // pass in a DC
  • 1, // select the first pixel format
  • sizeof(pfd) // size of struct to fill
  • pfd) // ptr to struct
  • if (0 MaxNumberPixelFormats)
  • // Something went wrong...

11
  • Selecting a pixel format
  • CclientDC mydc(this)
  • int SelectedPixelFormat
  • BOOL retVal
  • // now create a pfd and fill it with what wed
    like
  • PIXELFORMATDESCRIPTOR pfd
  • sizeof(PIXELFORMATDESCRIPTOR), // size of this
    pfd
  • 1, // version number
  • PFD_DRAW_TO_WINDOW // support window
  • PFD_SUPPORT_OPENGL // OpenGL
  • PFD_DOUBLEBUFFER, // double buffering
  • PFD_TYPE_RGBA, // RGBA type
  • 24, // 24-bit color depth
  • 0, 0, 0, 0, 0, 0, // color bits ignored
  • 0, // no alpha buffer
  • 0, // shift bit ignored
  • 0, // no accumulation buffer

12
  • // get the device contexts best-available-match
    pixel format
  • SelectedPixelFormat ChoosePixelFormat(mydc.m_hdc
    , pfd)
  • ASSERT(0 ! SelectedPixelFormat)
  • // make that the device contexts current pixel
    format
  • retVal SetPixelFormat(mydc.m_hdc,
    SelectedPixelFormat, pfd)
  • // make sure it worked
  • Getting the current pixel format
  • CclientDC mydc(this)
  • int SelectedPixelFormat
  • // get the current pixel format for this DC
  • SelectedPixelFormat GetPixelFormat(mydc.m_hdc)
  • // make sure the call worked

13
3. Rendering Contexts
  • Rendering contexts
  • every OpenGL command is linked to an RC
  • an RC links OpenGL calls to a Windows window
  • The Windows GL (WGL) functions
  • OpenGL Win32
  • context-rendering functions
  • wglCreateContext()
  • wglMakeCurrent()
  • wglDeleteContext()
  • wglGetCurrentContext()
  • wglGetCurrentDC()

14
  • The usual sequence
  • 1. get a DC
  • 2. use it to create an RC
  • 3. make the RC current
  • 4. make OpenGL calls
  • 5. deselect the RC
  • 6. delete it
  • 7. release the DC

15
  • The traditional (and slow) method of DC/RC
    handling

Windows Message Loop
16
  • The improved (and faster) method of DC/RC handling

Windows Message Loop
WM_PAINT
Make OpenGL Calls
WM_DESTROY
Make RC Noncurrent Delete RC Release DC
17
4. Fonts and OpenGL
  • The steps for generating a display list of a font
  • 1. Select the desired font characteristics.
  • 2. Select and then generate that font, using the
    current DC.
  • 3. Call wgl functions to automatically generate
    the glyphs and assign them to display list Ids
    sequentially.
  • wglUseFontBitmaps()
  • wglUseFontOutlines()
  • 4. Deselect that font from the DC.
  • (We have the display lists we need.)

18
5. Double Buffering
  • The Windows API call to swap the front and back
    buffers
  • SwapBuffers()
  • You cant use double buffering
  • when a double-buffered pixel format isnt
    available
  • when you want to use GDI calls in your OpenGL
    window

19
6. OpenGL and Libraries
  • OpenGLs Main Library
  • gl.h
  • opengl32.lib
  • opengl32.dll
  • OpenGLs Utility Library
  • glu.h
  • glu32.lib
  • glu32.dll
  • OpenGLs Auxiliary Library
  • glaux.h
  • glaux.lib

20
7. Creating a Simple OpenGL Program
  • With Windows C API
  • /
  • A Simple OpenGL program using a C interface
  • designed to be a quick introduction into the
    minimal
  • settings needed to run OpenGL under
    Microsoft Windows.
  • Ron Fosner - Dec. 1995
  • /
  • include ltwindows.hgt // standard Windows
    headers
  • include ltGL/gl.hgt // OpenGL interface
  • include ltGL/glu.hgt // OpenGL utility Library
    interface
  • LONG WINAPI WndProc( HWND, UINT, WPARAM, LPARAM
    )
  • void DrawOpenGLScene( void )
  • HGLRC SetUpOpenGL( HWND hWnd )
  • //////////////////////////////////////////////////
    ////////

21
  • int WINAPI WinMain (HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
  • LPSTR lpszCmdLine, int
    nCmdShow)
  • static char szAppName "OpenGL"
  • static char szTitle "A Simple C OpenGL
    Program"
  • WNDCLASS wc // windows class sruct
  • MSG msg // message struct
  • HWND hWnd // Main window handle.
  • // Fill in window class structure with
    parameters that
  • // describe the main window.
  • wc.style CS_HREDRAW CS_VREDRAW
    // Class style(s).
  • wc.lpfnWndProc (WNDPROC)WndProc
    // Window Procedure
  • wc.cbClsExtra 0
    // No per-class extra data.
  • wc.cbWndExtra 0
    // No per-window extra data.
  • wc.hInstance hInstance
    // Owner of this class
  • wc.hIcon NULL
    // Icon name
  • wc.hCursor LoadCursor(NULL,
    IDC_ARROW) // Cursor

22
  • // Create a main window for this application
    instance.
  • hWnd CreateWindow(szAppName, //
    app name
  • szTitle, //
    Text for window title bar
  • WS_OVERLAPPEDWINDOW //
    Window style
  • WS_CLIPCHILDREN WS_CLIPSIBLINGS, //
    NEED THESE for OpenGL calls
  • CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, //
    to work!
  • NULL, //
    no parent window
  • NULL, //
    Use the window class menu.
  • hInstance, //
    This instance owns this window
  • NULL) //
    We don't use any extra data
  • // If window could not be created, return
    zero
  • if ( !hWnd )
  • return( 0 )
  • // Make the window visible update its
    client area
  • ShowWindow( hWnd, nCmdShow ) // Show the
    window
  • UpdateWindow( hWnd ) // Sends
    WM_PAINT message

23
  • return( msg.wParam )
  • //////////////////////////////////////////////////
    ////////
  • // WndProc processes messages to our program.
  • // It's called WndProc because Windows's expects
    it
  • // to be called that!
  • //////////////////////////////////////////////////
    ////////
  • LONG WINAPI WndProc( HWND hWnd, UINT msg, WPARAM
    wParam, LPARAM lParam )
  • HDC hDC
  • static HGLRC hRC // Note this is STATIC!
  • PAINTSTRUCT ps
  • GLdouble gldAspect
  • GLsizei glnWidth, glnHeight
  • switch (msg)

24
  • // Make the RC current since we're
    going to make an OpenGL call here..
  • hDC GetDC (hWnd)
  • wglMakeCurrent (hDC, hRC)
  • // get the new size of the client
    window
  • // note that we size according to the
    height,
  • // not the smaller of the height or
    width.
  • glnWidth (GLsizei) LOWORD (lParam)
  • glnHeight (GLsizei) HIWORD
    (lParam)
  • gldAspect (GLdouble)glnWidth/(GLdoub
    le)glnHeight
  • // set up a projection matrix to fill
    the client window
  • glMatrixMode( GL_PROJECTION )
  • glLoadIdentity()
  • // a perspective-view matrix...
  • gluPerspective(30.0, //
    Field-of-view angle
  • gldAspect, // Aspect
    ratio of view volume
  • 1.0, // Distance
    to near clipping plane
  • 10.0 ) // Distance
    to far clipping plane

25
  • // Get a DC, then make the RC current
    and associated with this DC
  • hDC BeginPaint( hWnd, ps )
  • wglMakeCurrent( hDC, hRC )
  • DrawOpenGLScene()
  • // we're done with the RC, so
    deselect it
  • // (note This technique is not
    recommended!)
  • wglMakeCurrent( NULL, NULL )
  • EndPaint( hWnd, ps )
  • return 0
  • case WM_DESTROY
  • // Clean up and terminate.
  • wglDeleteContext( hRC )
  • PostQuitMessage( 0 )
  • return 0

26
  • //////////////////////////////////////////////////
    ///////
  • // SetUpOpenGL sets the pixel format and a
    rendering
  • // context then returns the RC
  • //////////////////////////////////////////////////
    ///////
  • HGLRC SetUpOpenGL( HWND hWnd )
  • static PIXELFORMATDESCRIPTOR pfd
  • sizeof (PIXELFORMATDESCRIPTOR), // strcut
    size
  • 1, //
    Version number
  • PFD_DRAW_TO_WINDOW // Flags,
    draw to a window,
  • PFD_SUPPORT_OPENGL, // use
    OpenGL
  • PFD_TYPE_RGBA, // RGBA
    pixel values
  • 24, // 24-bit
    color
  • 0, 0, 0, // RGB
    bits shift sizes.
  • 0, 0, 0, // Don't
    care about them
  • 0, 0, // No
    alpha buffer info
  • 0, 0, 0, 0, 0, // No
    accumulation buffer
  • 32, // 32-bit
    depth buffer

27
  • hDC GetDC( hWnd )
  • nMyPixelFormatID ChoosePixelFormat( hDC,
    pfd )
  • // catch errors here.
  • // If nMyPixelFormat is zero, then there's
    something wrong
  • // most likely the window's style bits are
    incorrect (in CreateWindow() )
  • // or OpenGl isn't installed on this machine
  • SetPixelFormat( hDC, nMyPixelFormatID, pfd
    )
  • hRC wglCreateContext( hDC )
  • ReleaseDC( hWnd, hDC )
  • return hRC
  • //////////////////////////////////////////////////
    ////////
  • // DrawScene uses OpenGL commands to draw a
    cube.
  • // This is where the OpenGL drawing commands
    live

28
  • // Define the modelview transformation.
  • glMatrixMode( GL_MODELVIEW )
  • glLoadIdentity()
  • // move the viewpoint out to where we can see
    everything
  • glTranslatef( 0.0f, 0.0f, -5.0f )
  • // Draw a large triangle out of three smaller
    triangles
  • // sharing common vertex colors
  • // upper left triangle
  • glBegin( GL_POLYGON )
  • glColor3f( 0.0f, 0.0f, 0.0f ) // black
    center
  • glVertex3f( 0.0f, 0.0f, 0.0f)
  • glColor3f( 0.0f, 1.0f, 0.0f ) // left
    vertex green
  • glVertex3f(-1.0f, -1.0f, 0.0f)
  • glColor3f( 1.0f, 0.0f, 0.0f ) // upper
    vertex red
  • glVertex3f( 0.0f, 1.0f, 0.0f)
  • glEnd()

29
  • // upper right triangle
  • glBegin( GL_POLYGON )
  • glColor3f( 0.0f, 0.0f, 0.0f ) // black
    center
  • glVertex3f( 0.0f, 0.0f, 0.0f)
  • glColor3f( 1.0f, 0.0f, 0.0f ) // upper
    vertex red
  • glVertex3f( 0.0f, 1.0f, 0.0f)
  • glColor3f( 0.0f, 0.0f, 1.0f ) // bottom
    right blue
  • glVertex3f( 1.0f, -1.0f, 0.0f)
  • glEnd()
  • // flush the drawing pipeline since it's
    single buffered
  • glFlush ()

30
  • With OpenGLs auxiliary library
  • // mbounce.c
  • // Uses mouse to start and stop animation
  • include ltwindows.hgt // Standard windows include
  • include ltgl\gl.hgt // OpenGL library
  • include ltgl\glaux.hgt // AUX library
  • // Initial square position and size
  • GLfloat x1 100.0f
  • GLfloat y1 150.0f
  • GLsizei rsize 50
  • // Step size in x and y directions
  • // (number of pixels to move each time)
  • GLfloat xstep 1.0f
  • GLfloat ystep 1.0f
  • // Keep track of windows changing width and
    height

31
  • if(h 0)
  • h 1
  • // Set the viewport to be the entire window
  • glViewport(0, 0, w, h)
  • // Reset the coordinate system before modifying
  • glLoadIdentity()
  • // Keep the square square, this time, save
    calculated
  • // width and height for later use
  • if (w lt h)
  • windowHeight 250.0fh/w
  • windowWidth 250.0f
  • else
  • windowWidth 250.0fw/h

32
  • // Set background clearing color to blue
  • glClearColor(0.0f, 0.0f, 1.0f, 1.0f)
  • // Clear the window with current clearing color
  • glClear(GL_COLOR_BUFFER_BIT)
  • // Set drawing color to Red, and draw rectangle
    at current position.
  • glColor3f(1.0f, 0.0f, 0.0f)
  • glRectf(x1, y1, x1rsize, y1rsize)
  • glFlush()
  • // Quickly blast image to window
  • auxSwapBuffers()
  • // Called by AUX library when idle (window not
    being resized or moved)
  • void CALLBACK IdleFunction(void)

33
  • // Check bounds. This is incase the window is
    made
  • // smaller and the rectangle is outside the new
  • // clipping volume
  • if(x1 gt windowWidth-rsize)
  • x1 windowWidth-rsize-1
  • if(y1 gt windowHeight-rsize)
  • y1 windowHeight-rsize-1
  • // Actually move the square
  • x1 xstep
  • y1 ystep
  • // Redraw the scene with new coordinates
  • RenderScene()
  • // Starts animation, called by AUX library when
    left mouse button is clicked.
  • void CALLBACK MouseLeft(AUX_EVENTREC event)

34
  • // Main body of program
  • void main(void)
  • // AUX window setup and initialization,
  • // now with double buffering
  • auxInitDisplayMode(AUX_DOUBLE AUX_RGBA)
  • auxInitPosition(100,100,250,250)
  • auxInitWindow("Mouse LeftStart, RightStop")
  • // Set function to call when window is resized
  • auxReshapeFunc(ChangeSize)
  • // Set function to call when program is idle
  • auxIdleFunc(IdleFunction)
  • // Set function to call when left mouse button
    clicked
  • auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEDOWN,
    MouseLeft)
  • // Set function to call when right mouse button
    clicked
Write a Comment
User Comments (0)
About PowerShow.com