• Welcome to Jose's Read Only Forum 2023.
 

NeHe Lesson 18: Quadrics

Started by José Roca, July 25, 2008, 08:10:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
This example illustrates the use of quadrics. With quadrics you can easily create complex objects such as spheres, discs, cylinders and cones. These object can be created with just one line of code. With some fancy math and planning it should be possible to morph these objects from one object into another.

It is an adaptation of NeHe Lesson 18: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18


' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "GLU.INC"
#INCLUDE "GDIPLUS.INC"
#INCLUDE "GDIPUTILS.INC"

$WindowCaption = "NeHe Lesson 18"

%GL_WINDOWWIDTH  = 640         ' Window width
%GL_WINDOWHEIGHT = 480         ' Window height
%GL_BITSPERPEL   = 16          ' Color resolution in bits per pixel
%GL_DEPTHBITS    = 16          ' Depth of the depth (z-axis) buffer

GLOBAL hDC AS LONG             ' Device context handle
GLOBAL TextureHandles() AS DWORD
GLOBAL LightAmbient() AS SINGLE
GLOBAL LightDiffuse() AS SINGLE
GLOBAL LightPosition() AS SINGLE
GLOBAL p1 AS LONG                   ' Increase 1
GLOBAL p2 AS LONG                   ' Increase 2
GLOBAL zoom AS SINGLE               ' Depth into the screen
GLOBAL nObject AS DWORD             ' Which object to draw
GLOBAL quadratic AS DWORD           ' Quadratic
GLOBAL xrot AS SINGLE               ' X rotation
GLOBAL yrot AS SINGLE               ' Y rotation
GLOBAL filter AS LONG               ' Which filter to use
GLOBAL part1 AS LONG                ' Start of disc
GLOBAL part2 AS LONG                ' End of disc
GLOBAL xspeed AS SINGLE             ' X rotation speed
GLOBAL yspeed AS SINGLE             ' Y rotation speed

' =======================================================================================
' All the setup goes here
' =======================================================================================
SUB SetupScene (BYVAL hwnd AS DWORD, BYVAL nWidth AS LONG, BYVAL nHeight AS LONG)

   LOCAL hr AS LONG
   LOCAL strTextureData AS STRING
   LOCAL TextureWidth, TextureHeight AS LONG
   DIM   TextureHandles(2) AS DWORD
   DIM   LightAmbient(3) AS SINGLE
   DIM   LightDiffuse(3) AS SINGLE
   DIM   LightPosition(3) AS SINGLE

   p1 = 0 : p2 = 1 : zoom = -5.0!

   ARRAY ASSIGN LightAmbient()  = 0.5!, 0.5!, 0.5!, 1.0!
   ARRAY ASSIGN LightDiffuse()  = 1.0!, 1.0!, 1.0!, 1.0!
   ARRAY ASSIGN LightPosition() = 0.0!, 0.0!, 2.0!, 1.0!

   ' Load bitmap texture from disk
   hr = GdiPlusLoadTexture("wall.bmp", TextureWidth, TextureHeight, strTextureData, %TRUE)

   ' Assign handles
   glGenTextures 3, TextureHandles(0)

   ' Create Nearest Filtered Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(0)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_NEAREST
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_NEAREST
   glTexImage2D %GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight, 0, _
                %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Create Linear Filtered Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(1)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR
   glTexImage2D %GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight, 0, _
                %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Create MipMapped Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(2)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR_MIPMAP_NEAREST
   gluBuild2DMipmaps %GL_TEXTURE_2D, 3, TextureWidth, TextureHeight, _
                     %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Enable texture mapping
   glEnable %GL_TEXTURE_2D
   ' Select smooth shading
   glShadeModel %GL_SMOOTH
   ' Specify clear values for the color buffers
   glClearColor 0.0!, 0.0!, 0.0!, 0.0!
   ' Specify the clear value for the depth buffer
   glClearDepth 1.0!
   ' The type of depth test to do
   glDepthFunc %GL_LEQUAL
   ' Enables depth testing
   glEnable %GL_DEPTH_TEST
' Really nice perspective calculations
   glHint %GL_PERSPECTIVE_CORRECTION_HINT, %GL_NICEST

   glLightfv %GL_LIGHT1, %GL_AMBIENT,  LightAmbient(0)  ' Setup the ambient light
   glLightfv %GL_LIGHT1, %GL_DIFFUSE,  LightDiffuse(0)  ' Setup the diffuse light
   glLightfv %GL_LIGHT1, %GL_POSITION, LightPosition(0) ' Position the light
   glEnable  %GL_LIGHT1                                 ' Enable light one

   quadratic = gluNewQuadric                            ' Create a pointer to the quadric object
   gluQuadricNormals quadratic, %GLU_SMOOTH             ' Create smooth normals
   gluQuadricTexture quadratic, %GL_TRUE                ' Create texture coords

END SUB
' =======================================================================================

' =======================================================================================
' Resize the scene
' =======================================================================================
SUB ResizeScene (BYVAL hwnd AS DWORD, BYVAL nWidth AS LONG, BYVAL nHeight AS LONG)

   ' Prevent divide by zero making height equal one
   IF nHeight = 0 THEN nHeight = 1
   ' Reset the current viewport
   glViewport 0, 0, nWidth, nHeight
   ' Select the projection matrix
   glMatrixMode %GL_PROJECTION
   ' Reset the projection matrix
   glLoadIdentity
   ' Calculate the aspect ratio of the window
   gluPerspective 45.0!, nWidth / nHeight, 0.1!, 100.0!
   ' Select the model view matrix
   glMatrixMode %GL_MODELVIEW
   ' Reset the model view matrix
   glLoadIdentity

END SUB
' =======================================================================================

' =======================================================================================
' Draw the scene
' =======================================================================================
SUB DrawScene (BYVAL hwnd AS DWORD, BYVAL nWidth AS LONG, BYVAL nHeight AS LONG)

   ' Clear the screen buffer
   glClear %GL_COLOR_BUFFER_BIT OR %GL_DEPTH_BUFFER_BIT
   ' Reset the view
   glLoadIdentity

   glTranslatef 0.0!, 0.0!, zoom
   glRotatef xrot, 1.0!, 0.0!, 0.0!
   glRotatef yrot, 0.0!, 1.0!, 0.0!
   glBindTexture %GL_TEXTURE_2D, TextureHandles(filter)

   SELECT CASE nObject

      CASE 0
         glBegin %GL_QUADS
         ' Front face
         glNormal3f 0.0!, 0.0!, 1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!,  1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!,  1.0!
         ' Back face
         glNormal3f 0.0!, 0.0!, -1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!, -1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!, -1.0!
         ' Top face
         glNormal3f 0.0!, 1.0!, 0.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!,  1.0!,  1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!,  1.0!,  1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
         ' Bottom face
         glNormal3f 0.0!, -1.0!, 0.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!, -1.0!, -1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!, -1.0!, -1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
         ' Right face
         glNormal3f 1.0!, 0.0!, 0.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!, -1.0!, -1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!,  1.0!,  1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
         ' Left face
         glNormal3f -1.0!, 0.0!, 0.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!, -1.0!, -1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!,  1.0!,  1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
         glEnd

      CASE 1
         ' Center the cylinder
         glTranslatef 0.0!, 0.0!, -1.5!
         ' A cylinder with a radius of 0.5 and a height of 2
         gluCylinder quadratic, 1.0!, 1.0!, 3.0!, 32, 32

      CASE 2
         ' Draw a disc (cd shape) with an inner radius of 0.5, and an outer radius of 2.  plus a lot of segments ;)
         gluDisk quadratic, 0.5!, 1.5!, 32, 32

      CASE 3
         '  Draw a sphere with a radius of 1 and 16 longitude and 16 latitude segments
         gluSphere quadratic, 1.3!, 32, 32

      CASE 4
         '  Center the cone
         glTranslatef 0.0!, 0.0!, -1.5!
         '  a cone with a bottom radius of .5 and a height of 2
         gluCylinder quadratic, 1.0!, 0.0!, 3.0!, 32, 32

      CASE 5
         part1 = part1 + p1
         part2 = part2 + p2
         IF part1 > 359 THEN               ' 360 Degrees
            p1 = 0
            part1 = 0
            p2 = 1
            part2 = 0
         END IF
         IF part2 > 359 THEN               ' 360 Degrees
            p1 = 1
            p2 = 0
         END IF
         '  A disk like the one before
         gluPartialDisk quadratic, 0.5!, 1.5!, 32, 32, part1, part2 - part1

   END SELECT

   xrot = xrot + xspeed
   yrot = yrot + yspeed

END SUB
' =======================================================================================

' =======================================================================================
' Cleanup
' =======================================================================================
SUB Cleanup (BYVAL hwnd AS DWORD)

   ' Delete the textures
   glDeleteTextures(3, TextureHandles(0))

END SUB
' =======================================================================================

' =======================================================================================
' Processes keystrokes
' Parameters:
' * hwnd = Window hande
' * vKeyCode = Virtual key code
' * bKeyDown = %TRUE if key is pressed; %FALSE if it is released
' =======================================================================================
SUB ProcessKeystrokes (BYVAL hwnd AS DWORD, BYVAL vKeyCode AS LONG, BYVAL bKeyDown AS LONG)

   STATIC lp, fp, sp, light AS LONG

   SELECT CASE AS LONG vKeyCode

      CASE %VK_ESCAPE
         ' Quit if Esc key pressed
         SendMessage hwnd, %WM_CLOSE, 0, 0

      CASE %VK_L
         IF ISTRUE bKeyDown AND ISFALSE lp THEN
            lp = %TRUE
            light = NOT light
            IF ISFALSE light THEN
               glDisable %GL_LIGHTING
            ELSE
               glEnable %GL_LIGHTING
            END IF
         END IF
         IF ISFALSE bKeyDown THEN lp = %FALSE

      CASE %VK_F
         IF ISTRUE bKeyDown AND ISFALSE fp THEN
            fp = %TRUE
            filter = filter + 1
            IF filter > 2 THEN filter = 0
         END IF
         IF ISFALSE bKeyDown THEN fp = %FALSE

      CASE %VK_SPACE
         IF ISTRUE bKeyDown AND ISFALSE sp THEN
            sp = %TRUE
         nObject = nObject + 1
         IF nObject > 5 THEN nObject = 0
         END IF
         IF ISFALSE bKeyDown THEN sp = %FALSE

      CASE %VK_PGUP
         IF ISTRUE bKeyDown THEN zoom = zoom - 0.02!

      CASE %VK_PGDN
         IF ISTRUE bKeyDown THEN zoom = zoom + 0.02!

      CASE %VK_LEFT
         IF ISTRUE bKeyDown THEN yspeed = yspeed - 0.01!

      CASE %VK_RIGHT
         IF ISTRUE bKeyDown THEN yspeed = yspeed + 0.01!

      CASE %VK_UP
         IF ISTRUE bKeyDown THEN xspeed = xspeed - 0.01!

      CASE %VK_DOWN
         IF ISTRUE bKeyDown THEN xspeed = xspeed + 0.01!

   END SELECT

END SUB
' =======================================================================================

' =======================================================================================
' Main
' =======================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   LOCAL  hwnd        AS DWORD
   LOCAL  wcex        AS WNDCLASSEX
   LOCAL  szClassName AS ASCIIZ * 256
   LOCAL  szCaption   AS ASCIIZ * 256
   LOCAL  msg         AS tagMSG
   LOCAL  rc          AS RECT
   LOCAL  bDone       AS LONG
   LOCAL  nLeft       AS LONG
   LOCAL  nTop        AS LONG
   LOCAL  nWidth      AS LONG
   LOCAL  nHeight     AS LONG
   LOCAL  dwStyle     AS DWORD
   LOCAL  dwStyleEx   AS DWORD
   STATIC vKeyCode    AS LONG
   STATIC bKeyDown    AS LONG
   LOCAL  t           AS DOUBLE
   LOCAL  t0          AS DOUBLE
   LOCAL  fps         AS DOUBLE
   LOCAL  nFrames     AS LONG
   LOCAL  dm          AS DEVMODE
   LOCAL  bFullScreen AS LONG
   LOCAL  lResult     AS LONG

   ' Register the window class
   szClassName        = "PBOPENGL"
   wcex.cbSize        = SIZEOF(wcex)
   wcex.style         = %CS_HREDRAW OR %CS_VREDRAW OR %CS_OWNDC
   wcex.lpfnWndProc   = CODEPTR(WndProc)
   wcex.cbClsExtra    = 0
   wcex.cbWndExtra    = 0
   wcex.hInstance     = hInstance
   wcex.hCursor       = LoadCursor (%NULL, BYVAL %IDC_ARROW)
   wcex.hbrBackground = %NULL
   wcex.lpszMenuName  = %NULL
   wcex.lpszClassName = VARPTR(szClassName)
   wcex.hIcon         = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Sample, if resource icon: LoadIcon(hInst, "APPICON")
   wcex.hIconSm       = LoadIcon (%NULL, BYVAL %IDI_APPLICATION) ' Remember to set small icon too..
   RegisterClassEx wcex

' Ask the user which screen mode he prefers
lResult = MessageBox(%NULL, "Would you like to run in fullscreen mode?", _
             "Start fullScreen?", %MB_YESNOCANCEL OR %MB_ICONQUESTION)
   SELECT CASE lResult
      CASE %IDCANCEL : EXIT FUNCTION
      CASE %IDYES    : bFullScreen = %TRUE
      CASE %IDNO     : bFullScreen = %FALSE
   END SELECT

   ' Window size
   nWidth  = %GL_WINDOWWIDTH
   nHeight = %GL_WINDOWHEIGHT

   IF bFullScreen THEN
      ' Change display settings
      dm.dmSize       = SIZEOF(dm)
      dm.dmPelsWidth  = nWidth
      dm.dmPelsHeight = nHeight
      dm.dmBitsPerPel = %GL_BITSPERPEL
      dm.dmFields     = %DM_BITSPERPEL OR %DM_PELSWIDTH OR %DM_PELSHEIGHT
      IF ChangeDisplaySettings(dm, %CDS_FULLSCREEN) = 0 THEN ShowCursor %FALSE
   END IF

   ' Window caption
   szCaption = $WindowCaption

   ' Window styles
   IF ISFALSE bFullScreen THEN
      dwStyle = %WS_OVERLAPPEDWINDOW
      dwStyleEx = %WS_EX_APPWINDOW OR %WS_EX_WINDOWEDGE
   ELSE
      dwStyle = %WS_POPUP
      dwStyleEx = %WS_EX_APPWINDOW
   END IF

   ' Create the window
   hwnd = CreateWindowEx( _
            dwStyleEx, _                      ' extended styles
            szClassName, _                    ' window class name
            szCaption, _                      ' window caption
            dwStyle, _                        ' window style
            nLeft, _                          ' initial x position
            nTop, _                           ' initial y position
            nWidth, _                         ' initial x size
            nHeight, _                        ' initial y size
            %NULL, _                          ' parent window handle
            0, _                              ' window menu handle
            hInstance, _                      ' program instance handle
            BYVAL %NULL)                      ' creation parameters

   ' Retrieve the coordinates of the window's client area
   GetClientRect hwnd, rc
   ' Initialize the new OpenGl window
   SetupScene hwnd, rc.nRight - rc.nLeft, rc.nBottom - rc.nTop

   ' Show the window
   ShowWindow hwnd, nCmdShow
   UpdateWindow hwnd

   DO UNTIL bDone

      ' Windows message pump
      DO WHILE PeekMessage(msg, %NULL, 0, 0, %PM_REMOVE)
         IF msg.message = %WM_QUIT THEN
            bDone = %TRUE
         ELSE
            IF msg.message = %WM_KEYDOWN THEN
               vKeyCode = msg.wParam
               bKeyDown = %TRUE
            ELSEIF msg.message = %WM_KEYUP THEN
               vKeyCode = msg.wParam
               bKeyDown = %FALSE
            END IF
            TranslateMessage msg
            DispatchMessage msg
         END IF
      LOOP

      IF ISFALSE bFullScreen THEN
         ' Get time and mouse position
         t = INT(TIMER)
         ' Calculate and display FPS (frames per second)
         IF t > t0 OR nFrames = 0 THEN
            fps = nFrames \ (t - t0)
            wsprintf szCaption, $WindowCaption & " (%i FPS)", BYVAL fps
            SetWindowText hwnd, szCaption
            t0 = t
            nFrames = 0
         END IF
         nFrames = nFrames + 1
      END IF

      ' Draw the scene
      DrawScene hwnd, nWidth, nHeight
      ' Exchange the front and back buffers
      SwapBuffers hDC

      ' Process the keystrokes
      IF vKeyCode THEN
         ProcessKeystrokes hwnd, vKeyCode, bKeyDown
         vKeyCode = 0
      END IF

   LOOP

   ' Retore defaults
   IF bFullScreen THEN
      ChangeDisplaySettings BYVAL %NULL, 0
      ShowCursor %TRUE
   END IF

   FUNCTION = msg.wParam

END FUNCTION
' =======================================================================================

' =======================================================================================
' Main window procedure
' =======================================================================================
FUNCTION WndProc (BYVAL hwnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL  pf           AS LONG
   LOCAL  pfd          AS PIXELFORMATDESCRIPTOR
   STATIC hRC          AS LONG

   SELECT CASE wMsg

      CASE %WM_SYSCOMMAND

         ' Disable the Windows screensaver
         IF (wParam AND &HFFF0) = %SC_SCREENSAVE THEN EXIT FUNCTION

         ' Close the window
         IF (wParam AND &HFFF0) = %SC_CLOSE THEN
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

      CASE %WM_CREATE

         ' Retrieve the device context handle
         hDC = GetDC(hwnd)

         ' Fill the PIXELFORMATDESCRIPTOR structure
         pfd.nSize           = SIZEOF(PIXELFORMATDESCRIPTOR)   ' Size of the structure
         pfd.nVersion        = 1                               ' Version number
         pfd.dwFlags         = %PFD_DRAW_TO_WINDOW _           ' Format must support window
                               OR %PFD_SUPPORT_OPENGL _        ' Format must support OpenGL
                               OR %PFD_DOUBLEBUFFER            ' Format must support double buffering
         pfd.iPixelType      = %PFD_TYPE_RGBA                  ' Request an RGBA format
         pfd.cColorBits      = %GL_BITSPERPEL                  ' Number of color bitplanes in each color buffer
         pfd.cRedBits        = 0                               ' Number of red bitplanes in each RGBA color buffer.
         pfd.cRedShift       = 0                               ' Shift count for red bitplanes in each RGBA color buffer.
         pfd.cGreenBits      = 0                               ' Number of green bitplanes in each RGBA color buffer.
         pfd.cGreenShift     = 0                               ' Shift count for green bitplanes in each RGBA color buffer.
         pfd.cBlueBits       = 0                               ' Number of blue bitplanes in each RGBA color buffer.
         pfd.cBlueShift      = 0                               ' Shift count for blue bitplanes in each RGBA color buffer.
         pfd.cAlphaBits      = 0                               ' Number of alpha bitplanes in each RGBA color buffer
         pfd.cAlphaShift     = 0                               ' Shift count for alpha bitplanes in each RGBA color buffer.
         pfd.cAccumBits      = 0                               ' Total number of bitplanes in the accumulation buffer.
         pfd.cAccumRedBits   = 0                               ' Number of red bitplanes in the accumulation buffer.
         pfd.cAccumGreenBits = 0                               ' Number of gree bitplanes in the accumulation buffer.
         pfd.cAccumBlueBits  = 0                               ' Number of blue bitplanes in the accumulation buffer.
         pfd.cAccumAlphaBits = 0                               ' Number of alpha bitplanes in the accumulation buffer.
         pfd.cDepthBits      = %GL_DEPTHBITS                   ' Depth of the depth (z-axis) buffer.
         pfd.cStencilBits    = 0                               ' Depth of the stencil buffer.
         pfd.cAuxBuffers     = 0                               ' Number of auxiliary buffers.
         pfd.iLayerType      = %PFD_MAIN_PLANE                 ' Ignored. Earlier implementations of OpenGL used this member, but it is no longer used.
         pfd.bReserved       = 0                               ' Number of overlay and underlay planes.
         pfd.dwLayerMask     = 0                               ' Ignored. Earlier implementations of OpenGL used this member, but it is no longer used.
         pfd.dwVisibleMask   = 0                               ' Transparent color or index of an underlay plane.
         pfd.dwDamageMask    = 0                               ' Ignored. Earlier implementations of OpenGL used this member, but it is no longer used.

         ' Find a matching pixel format
         pf = ChoosePixelFormat(hDC, pfd)
         IF ISFALSE pf THEN
            MessageBox hwnd, "Can't find a suitable pixel format", _
                       "Error", %MB_OK OR %MB_ICONEXCLAMATION
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

         ' Set the pixel format
         IF ISFALSE SetPixelFormat(hDC, pf, pfd) THEN
            MessageBox hwnd, "Can't set the pixel format", _
                       "Error", %MB_OK OR %MB_ICONEXCLAMATION
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

         ' Create a new OpenGL rendering context
         hRC = wglCreateContext(hDC)
         IF ISFALSE hRC THEN
            MessageBox hwnd, "Can't create an OpenGL rendering context", _
                       "Error", %MB_OK OR %MB_ICONEXCLAMATION
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

         ' Make it current
         IF ISFALSE wglMakeCurrent(hDC,hRC) THEN
            MessageBox hwnd, "Can't activate the OpenGL rendering context", _
                       "Error", %MB_OK OR %MB_ICONEXCLAMATION
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

         EXIT FUNCTION

      CASE %WM_DESTROY
         ' Clear resources
         Cleanup hwnd
         ' Release the device and rendering contexts
         wglMakeCurrent hDC, 0
         ' Make the rendering context no longer current
         wglDeleteContext hRC
         ' Release the device context
         ReleaseDC hwnd, hDC
         ' Post an WM_QUIT message
         PostQuitMessage 0
         EXIT FUNCTION

      CASE %WM_SIZE
         ResizeScene hwnd, LO(WORD, lParam), HI(WORD, lParam)
         EXIT FUNCTION

   END SELECT

   ' Call the default window procedure to process unhandled messages
   FUNCTION = DefWindowProc(hwnd, wMsg, wParam, lParam)

END FUNCTION
' =======================================================================================




José Roca

#1
 
This example illustrates the use of quadrics. With quadrics you can easily create complex objects such as spheres, discs, cylinders and cones. These object can be created with just one line of code. With some fancy math and planning it should be possible to morph these objects from one object into another.

It is an adaptation of NeHe Lesson 18: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18


' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "SDL.INC"
#INCLUDE "GLU.INC"
#INCLUDE "GDIPUTILS.INC"

$WindowCaption = "NeHe Lesson 18"

%MAX_PARTICLES = 1000   ' Number of particles to create

GLOBAL hDC AS LONG             ' Device context handle
GLOBAL TextureHandles() AS DWORD
GLOBAL LightAmbient() AS SINGLE
GLOBAL LightDiffuse() AS SINGLE
GLOBAL LightPosition() AS SINGLE
GLOBAL p1 AS LONG                   ' Increase 1
GLOBAL p2 AS LONG                   ' Increase 2
GLOBAL zoom AS SINGLE               ' Depth into the screen
GLOBAL nObject AS DWORD             ' Which object to draw
GLOBAL quadratic AS DWORD           ' Quadratic
GLOBAL xrot AS SINGLE               ' X rotation
GLOBAL yrot AS SINGLE               ' Y rotation
GLOBAL filter AS LONG               ' Which filter to use
GLOBAL part1 AS LONG                ' Start of disc
GLOBAL part2 AS LONG                ' End of disc
GLOBAL xspeed AS SINGLE             ' X rotation speed
GLOBAL yspeed AS SINGLE             ' Y rotation speed

' =======================================================================================
' All the setup goes here
' =======================================================================================
SUB SetupScene ()

   LOCAL hr AS LONG
   LOCAL strTextureData AS STRING
   LOCAL TextureWidth, TextureHeight AS LONG
   DIM   TextureHandles(2) AS DWORD
   DIM   LightAmbient(3) AS SINGLE
   DIM   LightDiffuse(3) AS SINGLE
   DIM   LightPosition(3) AS SINGLE

   p1 = 0 : p2 = 1 : zoom = -5.0!

   ARRAY ASSIGN LightAmbient()  = 0.5!, 0.5!, 0.5!, 1.0!
   ARRAY ASSIGN LightDiffuse()  = 1.0!, 1.0!, 1.0!, 1.0!
   ARRAY ASSIGN LightPosition() = 0.0!, 0.0!, 2.0!, 1.0!

   ' Load bitmap texture from disk
   hr = GdiPlusLoadTexture("wall.bmp", TextureWidth, TextureHeight, strTextureData, %TRUE)

   ' Assign handles
   glGenTextures 3, TextureHandles(0)

   ' Create Nearest Filtered Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(0)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_NEAREST
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_NEAREST
   glTexImage2D %GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight, 0, _
                %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Create Linear Filtered Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(1)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR
   glTexImage2D %GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight, 0, _
                %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Create MipMapped Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(2)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR_MIPMAP_NEAREST
   gluBuild2DMipmaps %GL_TEXTURE_2D, 3, TextureWidth, TextureHeight, _
                     %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Enable texture mapping
   glEnable %GL_TEXTURE_2D
   ' Select smooth shading
   glShadeModel %GL_SMOOTH
   ' Specify clear values for the color buffers
   glClearColor 0.0!, 0.0!, 0.0!, 0.0!
   ' Specify the clear value for the depth buffer
   glClearDepth 1.0!
   ' The type of depth test to do
   glDepthFunc %GL_LEQUAL
   ' Enables depth testing
   glEnable %GL_DEPTH_TEST
' Really nice perspective calculations
   glHint %GL_PERSPECTIVE_CORRECTION_HINT, %GL_NICEST

   glLightfv %GL_LIGHT1, %GL_AMBIENT,  LightAmbient(0)  ' Setup the ambient light
   glLightfv %GL_LIGHT1, %GL_DIFFUSE,  LightDiffuse(0)  ' Setup the diffuse light
   glLightfv %GL_LIGHT1, %GL_POSITION, LightPosition(0) ' Position the light
   glEnable  %GL_LIGHT1                                 ' Enable light one

   quadratic = gluNewQuadric                            ' Create a pointer to the quadric object
   gluQuadricNormals quadratic, %GLU_SMOOTH             ' Create smooth normals
   gluQuadricTexture quadratic, %GL_TRUE                ' Create texture coords

END SUB
' =======================================================================================

' =======================================================================================
' Draw the scene
' =======================================================================================
SUB DrawScene (BYVAL nWidth AS LONG, BYVAL nHeight AS LONG)

   LOCAL i AS LONG
   LOCAL sx, sy, sz AS SINGLE

   ' Clear the screen buffer
   glClear %GL_COLOR_BUFFER_BIT OR %GL_DEPTH_BUFFER_BIT

   ' Select and setup the projection matrix
   glMatrixMode %GL_PROJECTION
   glLoadIdentity

   ' Prevent divide by zero making height equal one
   IF nHeight = 0 THEN nHeight = 1

   ' Calculate the aspect ratio of the window
   gluPerspective 45.0!, nWidth/nHeight, 1.0!, 100.0!

   ' Select and setup the modelview matrix
   glMatrixMode %GL_MODELVIEW
   glLoadIdentity

   glTranslatef 0.0!, 0.0!, zoom
   glRotatef xrot, 1.0!, 0.0!, 0.0!
   glRotatef yrot, 0.0!, 1.0!, 0.0!
   glBindTexture %GL_TEXTURE_2D, TextureHandles(filter)

   SELECT CASE nObject

      CASE 0
         glBegin %GL_QUADS
         ' Front face
         glNormal3f 0.0!, 0.0!, 1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!,  1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!,  1.0!
         ' Back face
         glNormal3f 0.0!, 0.0!, -1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!, -1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!, -1.0!
         ' Top face
         glNormal3f 0.0!, 1.0!, 0.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!,  1.0!,  1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!,  1.0!,  1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
         ' Bottom face
         glNormal3f 0.0!, -1.0!, 0.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!, -1.0!, -1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!, -1.0!, -1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
         ' Right face
         glNormal3f 1.0!, 0.0!, 0.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!, -1.0!, -1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!,  1.0!,  1.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
         ' Left face
         glNormal3f -1.0!, 0.0!, 0.0!
         glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!, -1.0!, -1.0!
         glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
         glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!,  1.0!,  1.0!
         glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
         glEnd

      CASE 1
         ' Center the cylinder
         glTranslatef 0.0!, 0.0!, -1.5!
         ' A cylinder with a radius of 0.5 and a height of 2
         gluCylinder quadratic, 1.0!, 1.0!, 3.0!, 32, 32

      CASE 2
         ' Draw a disc (cd shape) with an inner radius of 0.5, and an outer radius of 2.  plus a lot of segments ;)
         gluDisk quadratic, 0.5!, 1.5!, 32, 32

      CASE 3
         '  Draw a sphere with a radius of 1 and 16 longitude and 16 latitude segments
         gluSphere quadratic, 1.3!, 32, 32

      CASE 4
         '  Center the cone
         glTranslatef 0.0!, 0.0!, -1.5!
         '  a cone with a bottom radius of .5 and a height of 2
         gluCylinder quadratic, 1.0!, 0.0!, 3.0!, 32, 32

      CASE 5
         part1 = part1 + p1
         part2 = part2 + p2
         IF part1 > 359 THEN               ' 360 Degrees
            p1 = 0
            part1 = 0
            p2 = 1
            part2 = 0
         END IF
         IF part2 > 359 THEN               ' 360 Degrees
            p1 = 1
            p2 = 0
         END IF
         '  A disk like the one before
         gluPartialDisk quadratic, 0.5!, 1.5!, 32, 32, part1, part2 - part1

   END SELECT

   xrot = xrot + xspeed
   yrot = yrot + yspeed

   ' // Swap buffers
   SDL_GL_SwapBuffers

END SUB
' =======================================================================================

' =======================================================================================
' Main
' =======================================================================================
FUNCTION PBMAIN () AS LONG

   LOCAL pscreen AS SDL_Surface PTR
   LOCAL nWidth, nHeight, bpp AS LONG
   LOCAL lResult, bFullScreen AS LONG
   LOCAL szCaption AS ASCIIZ * 256
   LOCAL dwFlags AS DWORD
   LOCAL t, t0, fps AS DOUBLE
   LOCAL nFrames AS LONG
   LOCAL lp, fp, sp, light AS LONG

   szCaption = $WindowCaption   ' Window caption

' Ask the user which screen mode he prefers
lResult = MessageBox(%NULL, "Would you like to run in fullscreen mode?", _
             "Start fullScreen?", %MB_YESNOCANCEL OR %MB_ICONQUESTION)
   SELECT CASE lResult
      CASE %IDCANCEL : EXIT FUNCTION
      CASE %IDYES    : bFullScreen = %TRUE
      CASE %IDNO     : bFullScreen = %FALSE
   END SELECT

   ' // Initialize SDL's subsystems - in this case, only video.
   IF SDL_Init(%SDL_INIT_VIDEO) < 0 THEN
      MSGBOX "Unable to init SDL: " & SDL_GetError()
      EXIT FUNCTION
   END IF

   ' // Attempt to create a 640x480 window
   nWidth = 640 : nHeight = 480 : bpp = 16
   IF bFullScreen THEN
      dwFlags = %SDL_OPENGL OR %SDL_FULLSCREEN
   ELSE
      dwFlags = %SDL_OPENGL
   END IF
   pscreen = SDL_SetVideoMode(nWidth, nHeight, bpp, dwFlags)
   SDL_WM_SetCaption szCaption, ""

   ' // If we fail, return error.
   IF pscreen = %NULL THEN
      SDL_Quit
      MSGBOX "Unable to set the video mode: " & SDL_GetError()
      EXIT FUNCTION
   END IF

   ' // Set up the scene
   SetupScene

   ' Enable keyboard repeat
   SDL_EnableKeyRepeat %SDL_DEFAULT_REPEAT_DELAY, %SDL_DEFAULT_REPEAT_INTERVAL

   ' // Main loop
   LOCAL done AS LONG
   LOCAL uevent AS SDL_Event
   WHILE done = %FALSE

      ' // Poll for events, and handle the ones we care about.
      WHILE SDL_PollEvent(VARPTR(uevent))
         SELECT CASE uevent.type
            CASE %SDL_KEYDOWN
               SELECT CASE uevent.key.keysym.sym
                  CASE %SDLK_L
                     IF ISFALSE lp THEN
                        lp = %TRUE
                        light = NOT light
                        IF ISFALSE light THEN
                           glDisable %GL_LIGHTING
                        ELSE
                           glEnable %GL_LIGHTING
                        END IF
                     END IF
                     CASE %SDLK_F
                        IF ISFALSE fp THEN
                           fp = %TRUE
                           filter = filter + 1
                           IF filter > 2 THEN filter = 0
                        END IF
                     CASE %SDLK_SPACE
                        IF ISFALSE sp THEN
                           sp = %TRUE
                           nObject = nObject + 1
                           IF nObject > 5 THEN nObject = 0
                        END IF
                  CASE %SDLK_PAGEUP
                     zoom = zoom - 0.02!
                  CASE %SDLK_PAGEDOWN
                     zoom = zoom + 0.02!
                  CASE %SDLK_LEFT
                     yspeed = yspeed - 0.01!
                  CASE %SDLK_RIGHT
                     yspeed = yspeed + 0.01!
                  CASE %SDLK_UP
                     xspeed = xspeed - 0.01!
                  CASE %SDLK_DOWN
                     xspeed = xspeed + 0.01!
               END SELECT
            CASE %SDL_KEYUP
               ' // Quit if escape is pressed
               IF uevent.key.keysym.sym = %SDLK_ESCAPE THEN
                  done = %TRUE
                  EXIT LOOP
               END IF
               ' Reset flags
               lp = %FALSE
               fp = %FALSE
               sp = %FALSE
            CASE %SDL_QUIT
               done = %TRUE
               EXIT LOOP
         END SELECT
      WEND

      IF ISFALSE bFullScreen THEN
         ' Get time and mouse position
         t = INT(TIMER)
         ' Calculate and display FPS (frames per second)
         IF t > t0 OR nFrames = 0 THEN
            fps = nFrames \ (t - t0)
            wsprintf szCaption, $WindowCaption & " (%i FPS)", BYVAL fps
            SDL_WM_SetCaption szCaption, szCaption
            t0 = t
            nFrames = 0
         END IF
         nFrames = nFrames + 1
      END IF

      ' // Draw the scene
      DrawScene nWidth, nHeight

   WEND

   ' Delete the texture
   glDeleteTextures(3, TextureHandles(0))

   ' Shut down SDL
   SDL_Quit

END FUNCTION
' =======================================================================================


José Roca

#2
 
This example illustrates the use of quadrics. With quadrics you can easily create complex objects such as spheres, discs, cylinders and cones. These object can be created with just one line of code. With some fancy math and planning it should be possible to morph these objects from one object into another.

It is an adaptation of NeHe Lesson 18: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18


' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "GLFW.INC"
#INCLUDE ONCE "GDIPUTILS.INC"

' =======================================================================================
' Main
' =======================================================================================
FUNCTION PBMAIN () AS LONG

   LOCAL hr, nWidth, nHeight, running, frames, x, y AS LONG
   LOCAL t, t0, fps AS DOUBLE
   LOCAL szTitlestr AS ASCIIZ * 200
   LOCAL strTextureData AS STRING
   LOCAL TextureWidth, TextureHeight AS LONG
   DIM   TextureHandles(2) AS DWORD   ' Storage for 3 textures
   LOCAL filter AS LONG               ' Which filter to use
   LOCAL light AS LONG                ' Lighting on/off
   LOCAL lp AS LONG                   ' L pressed?
   LOCAL fp AS LONG                   ' F pressed?
   LOCAL sp AS LONG                   ' Spacebar pressed?
   LOCAL part1 AS LONG                ' Start of disc
   LOCAL part2 AS LONG                ' End of disc
   LOCAL p1 AS LONG                   ' Increase 1
   LOCAL p2 AS LONG                   ' Increase 2
   LOCAL xrot AS SINGLE               ' X rotation
   LOCAL yrot AS SINGLE               ' Y rotation
   LOCAL xspeed AS SINGLE             ' X rotation speed
   LOCAL yspeed AS SINGLE             ' Y rotation speed
   LOCAL z AS SINGLE                  ' Depth into the screen
   LOCAL nObject AS DWORD             ' Which object to draw
   LOCAL quadratic AS DWORD           ' Quadratic

   DIM LightAmbient(3) AS SINGLE
   DIM LightDiffuse(3) AS SINGLE
   DIM LightPosition(3) AS SINGLE

   p1 = 0 : p2 = 1 : z = -5.0!

   ARRAY ASSIGN LightAmbient()  = 0.5!, 0.5!, 0.5!, 1.0!
   ARRAY ASSIGN LightDiffuse()  = 1.0!, 1.0!, 1.0!, 1.0!
   ARRAY ASSIGN LightPosition() = 0.0!, 0.0!, 2.0!, 1.0!

   ' Initialize GLFW
   glfwInit

   ' Open OpenGL window
   IF ISFALSE glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, %GLFW_WINDOW) THEN
      glfwTerminate
      EXIT FUNCTION
   END IF

   ' Load bitmap texture from disk
   hr = GdiPlusLoadTexture("wall.bmp", TextureWidth, TextureHeight, strTextureData, %TRUE)

   ' Assign handles
   glGenTextures 3, TextureHandles(0)

   ' Create Nearest Filtered Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(0)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_NEAREST
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_NEAREST
   glTexImage2D %GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight, 0, _
                %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Create Linear Filtered Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(1)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR
   glTexImage2D %GL_TEXTURE_2D, 0, 3, TextureWidth, TextureHeight, 0, _
                %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Create MipMapped Texture
   glBindTexture %GL_TEXTURE_2D, TextureHandles(2)
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR
   glTexParameteri %GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR_MIPMAP_NEAREST
   gluBuild2DMipmaps %GL_TEXTURE_2D, 3, TextureWidth, TextureHeight, _
                     %GL_RGBA, %GL_UNSIGNED_BYTE, BYVAL STRPTR(strTextureData)

   ' Enable texture mapping
   glEnable %GL_TEXTURE_2D
   ' Select smooth shading
   glShadeModel %GL_SMOOTH
   ' Specify clear values for the color buffers
   glClearColor 0.0!, 0.0!, 0.0!, 0.0!
   ' Specify the clear value for the depth buffer
   glClearDepth 1.0!
   ' The type of depth test to do
   glDepthFunc %GL_LEQUAL
   ' Enables depth testing
   glEnable %GL_DEPTH_TEST
' Really nice perspective calculations
   glHint %GL_PERSPECTIVE_CORRECTION_HINT, %GL_NICEST

   glLightfv %GL_LIGHT1, %GL_AMBIENT,  LightAmbient(0)  ' Setup the ambient light
   glLightfv %GL_LIGHT1, %GL_DIFFUSE,  LightDiffuse(0)  ' Setup the diffuse light
   glLightfv %GL_LIGHT1, %GL_POSITION, LightPosition(0) ' Position the light
   glEnable  %GL_LIGHT1                                 ' Enable light one

   quadratic = gluNewQuadric                            ' Create a pointer to the quadric object
   gluQuadricNormals quadratic, %GLU_SMOOTH             ' Create smooth normals
   gluQuadricTexture quadratic, %GL_TRUE                ' Create texture coords

   ' Enable sticky keys
   glfwEnable %GLFW_STICKY_KEYS

   ' Disable vertical sync (on cards that support it)
   glfwSwapInterval 0

   ' Main loop
   running = %TRUE
   frames = 0
   t0 = glfwGetTime

   DO WHILE running

      ' Get time and mouse position
      t = glfwGetTime
      glfwGetMousePos x, y
      ' Calculate and display FPS (frames per second)
      IF t - t0 > 1.0! OR frames = 0 THEN
         fps = frames / (t-t0)
         wsprintf szTitlestr, "NEHE Lesson 18 (%i FPS)", BYVAL fps
         glfwSetWindowTitle szTitlestr
         t0 = t
         frames = 0
      END IF
      frames = frames + 1

      '  Get window size (may be different than the requested size)
      glfwGetWindowSize nWidth, nHeight
      IF nHeight <= 0 THEN nHeight = 1

      ' Set viewport
      glViewport 0, 0, nWidth, nHeight

      ' Clear the screen and the depth buffers
      glClear %GL_COLOR_BUFFER_BIT OR %GL_DEPTH_BUFFER_BIT

      ' Select and setup the projection matrix
      glMatrixMode %GL_PROJECTION
      glLoadIdentity

      ' Calculate the aspect ratio of the window
      gluPerspective 45.0!, nWidth/nHeight, 0.1!, 100.0!

      ' Select the modelview matrix
      glMatrixMode %GL_MODELVIEW
      glLoadIdentity

      glTranslatef 0.0!, 0.0!, z
      glRotatef xrot, 1.0!, 0.0!, 0.0!
      glRotatef yrot, 0.0!, 1.0!, 0.0!
      glBindTexture %GL_TEXTURE_2D, TextureHandles(filter)

      SELECT CASE nObject

         CASE 0
            glBegin %GL_QUADS
            ' Front face
            glNormal3f 0.0!, 0.0!, 1.0!
            glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
            glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!,  1.0!
            glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!,  1.0!
            ' Back face
            glNormal3f 0.0!, 0.0!, -1.0!
            glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!, -1.0!
            glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
            glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
            glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!, -1.0!
            ' Top face
            glNormal3f 0.0!, 1.0!, 0.0!
            glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
            glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!,  1.0!,  1.0!
            glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!,  1.0!,  1.0!
            glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
            ' Bottom face
            glNormal3f 0.0!, -1.0!, 0.0!
            glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!, -1.0!, -1.0!
            glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!, -1.0!, -1.0!
            glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
            glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
            ' Right face
            glNormal3f 1.0!, 0.0!, 0.0!
            glTexCoord2f 1.0!, 0.0! : glVertex3f  1.0!, -1.0!, -1.0!
            glTexCoord2f 1.0!, 1.0! : glVertex3f  1.0!,  1.0!, -1.0!
            glTexCoord2f 0.0!, 1.0! : glVertex3f  1.0!,  1.0!,  1.0!
            glTexCoord2f 0.0!, 0.0! : glVertex3f  1.0!, -1.0!,  1.0!
            ' Left face
            glNormal3f -1.0!, 0.0!, 0.0!
            glTexCoord2f 0.0!, 0.0! : glVertex3f -1.0!, -1.0!, -1.0!
            glTexCoord2f 1.0!, 0.0! : glVertex3f -1.0!, -1.0!,  1.0!
            glTexCoord2f 1.0!, 1.0! : glVertex3f -1.0!,  1.0!,  1.0!
            glTexCoord2f 0.0!, 1.0! : glVertex3f -1.0!,  1.0!, -1.0!
            glEnd

         CASE 1
            ' Center the cylinder
            glTranslatef 0.0!, 0.0!, -1.5!
            ' A cylinder with a radius of 0.5 and a height of 2
            gluCylinder quadratic, 1.0!, 1.0!, 3.0!, 32, 32

         CASE 2
            ' Draw a disc (cd shape) with an inner radius of 0.5, and an outer radius of 2.  plus a lot of segments ;)
            gluDisk quadratic, 0.5!, 1.5!, 32, 32

         CASE 3
            '  Draw a sphere with a radius of 1 and 16 longitude and 16 latitude segments
            gluSphere quadratic, 1.3!, 32, 32

         CASE 4
            '  Center the cone
            glTranslatef 0.0!, 0.0!, -1.5!
            '  a cone with a bottom radius of .5 and a height of 2
            gluCylinder quadratic, 1.0!, 0.0!, 3.0!, 32, 32

         CASE 5
            part1 = part1 + p1
            part2 = part2 + p2
            IF part1 > 359 THEN               ' 360 Degrees
               p1 = 0
               part1 = 0
               p2 = 1
               part2 = 0
            END IF
            IF part2 > 359 THEN               ' 360 Degrees
               p1 = 1
               p2 = 0
            END IF
            '  A disk like the one before
            gluPartialDisk quadratic, 0.5!, 1.5!, 32, 32, part1, part2 - part1

      END SELECT

      xrot = xrot + xspeed
      yrot = yrot + yspeed

      ' Swap buffers
      glfwSwapBuffers

      ' L key
      IF glfwGetKey(76) = %GLFW_PRESS AND ISFALSE lp THEN
         lp = %TRUE
         light = NOT light
         IF ISFALSE light THEN
            glDisable %GL_LIGHTING
         ELSE
            glEnable %GL_LIGHTING
         END IF
      END IF
      IF glfwGetKey(76) <> %GLFW_PRESS THEN lp = %FALSE

      ' F key
      IF glfwGetKey(70) = %GLFW_PRESS AND ISFALSE fp THEN
         fp = %TRUE
         filter = filter + 1
         IF filter > 2 THEN filter = 0
      END IF
      IF glfwGetKey(70) <> %GLFW_PRESS THEN fp = %FALSE

      ' Space
      IF glfwGetKey(32) = %GLFW_PRESS AND ISFALSE sp THEN
         sp = %TRUE
         nObject = nObject + 1
         IF nObject > 5 THEN nObject = 0
      END IF
      IF glfwGetKey(32) <> %GLFW_PRESS THEN sp = %FALSE

      IF glfwGetKey(%GLFW_KEY_PAGEUP) = %GLFW_PRESS THEN z = z - 0.02!
      IF glfwGetKey(%GLFW_KEY_PAGEDOWN) = %GLFW_PRESS THEN z = z + 0.02!
      IF glfwGetKey(%GLFW_KEY_UP) = %GLFW_PRESS THEN xspeed = xspeed - 0.01!
      IF glfwGetKey(%GLFW_KEY_DOWN) = %GLFW_PRESS THEN xspeed = xspeed + 0.01!
      IF glfwGetKey(%GLFW_KEY_RIGHT) = %GLFW_PRESS THEN yspeed = yspeed + 0.01!
      IF glfwGetKey(%GLFW_KEY_LEFT) = %GLFW_PRESS THEN yspeed = yspeed - 0.01!

      ' Check if the ESC key was pressed or the window was closed
      running = NOT glfwGetKey(%GLFW_KEY_ESC) AND glfwGetWindowParam(%GLFW_OPENED)

   LOOP

   ' Delete the quadratic to free system resources
   gluDeleteQuadric quadratic

   ' Delete the textures
   glDeleteTextures(3, TextureHandles(0))

   ' Close OpenGL window and terminate GLFW
   glfwTerminate

END FUNCTION
' =======================================================================================