Author Topic: OpenGL: Primitive Types  (Read 4537 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2480
OpenGL: Primitive Types
« on: July 25, 2008, 09:27:09 PM »
 
This sample demonstrates how to use the 10 primitive types available under OpenGL: points, lines, line strip, line loop, triangles triangle strips, triangle fan, quads, quads strips, and polygon.

It is an adaptation of ogl_primitive_types.cpp, by Kevin Harris, 02/01/05, downloadable at http://www.codesampler.com/oglsrc/oglsrc_1.htm#ogl_primitive_types

Code: [Select]
' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "GLU.INC"

$WindowCaption = "OpenGL - Primitve Types"

%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 g_currentPrimitive AS LONG

TYPE Vertex
   r AS BYTE
   g AS BYTE
   b AS BYTE
   a AS BYTE
   x AS SINGLE
   y AS SINGLE
   z AS SINGLE
END TYPE

GLOBAL g_points () AS Vertex
GLOBAL g_lines () AS Vertex
GLOBAL g_lineStrip_and_lineLoop () AS Vertex
GLOBAL g_triangles () AS Vertex
GLOBAL g_triangleStrip () AS Vertex
GLOBAL g_triangleFan() AS Vertex
GLOBAL g_quads () AS Vertex
GLOBAL g_quadStrip () AS Vertex
GLOBAL g_polygon () AS Vertex

' ========================================================================================
' Fills a Vertex structure
' ========================================================================================
MACRO FillVertex (v, r_, g_, b_, a_, x_, y_, z_)
   v.r = r_ : v.g = g_ : v.b = b_ : v.a = a_
   v.x = x_ : v.y = y_ : v.z = z_
END MACRO

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

   g_currentPrimitive = %GL_POLYGON

   DIM g_points(4)

   FillVertex(g_points(0), 255, 0, 0, 255, 0.0!, 0.0!, 0.0!)
   FillVertex(g_points(1), 0, 255, 0, 255, 0.5!, 0.0!, 0.0!)
   FillVertex(g_points(2), 0, 0, 255, 255, -0.5!, 0.0!, 0.0!)
   FillVertex(g_points(3), 255, 255, 0, 255, 0.0!, -0.5!, 0.0!)
   FillVertex(g_points(4), 255, 0, 255, 255, 0.0!, 0.5!, 0.0!)

   DIM g_lines(5)

   FillVertex(g_lines(0), 255, 0, 0, 255, -1.0!, 0.0!, 0.0!)
   FillVertex(g_lines(1), 255, 0, 0, 255, 0.0!, 1.0!, 0.0!)
   FillVertex(g_lines(2), 0, 255, 0, 255, 0.5!, 1.0!, 0.0!)
   FillVertex(g_lines(3), 0, 255, 0, 255, 0.5!, -1.0!, 0.0!)
   FillVertex(g_lines(4), 0, 0, 255, 255, 1.0!, -0.5!, 0.0!)
   FillVertex(g_lines(5), 0, 0, 255, 255, -1.0!, -0.5!, 0.0!)

   '//
   '// Note:
   '//
   '// We'll use the same vertices for both the line-strip and line-loop
   '// demonstrations. The difference between the two primitive types will not be
   '// noticeable until the vertices are rendered. Line-loop works exactly like
   '// line-strip except it creates a closed loop by automatically connecting
   '// the last vertex to the first with a line segment.
   '//

   DIM g_lineStrip_and_lineLoop(5)

   FillVertex(g_lineStrip_and_lineLoop(0), 255, 0, 0, 255, 0.5!, 0.5!, 0.0!)
   FillVertex(g_lineStrip_and_lineLoop(1), 0, 255, 0, 255, 1.0!, 0.0!, 0.0!)
   FillVertex(g_lineStrip_and_lineLoop(2), 0, 0, 255, 255, 0.0!, -0.0!, 0.0!)
   FillVertex(g_lineStrip_and_lineLoop(3), 255, 255, 0, 255, -1.0!, 0.0!, 0.0!)
   FillVertex(g_lineStrip_and_lineLoop(4), 255, 0, 0, 255, 0.0!, 0.0!, 0.0!)
   FillVertex(g_lineStrip_and_lineLoop(5), 255, 0, 0, 255, 0.0!, 1.0!, 0.0!)

   DIM g_triangles(5)

   FillVertex(g_triangles(0), 255, 0, 0, 255, -1.0!, 0.0!, 0.0!)
   FillVertex(g_triangles(1), 0, 0, 255, 255, 1.0!, 0.0!, 0.0!)
   FillVertex(g_triangles(2), 0, 255, 0, 255, 0.0!, 1.0!, 0.0!)
   FillVertex(g_triangles(3), 255, 255, 0, 255, -0.5!, 1.0!, 0.0!)
   FillVertex(g_triangles(4), 255, 0, 0, 255, 0.5!, -1.0!, 0.0!)
   FillVertex(g_triangles(5), 0, 255, 255, 255, 0.0!, -0.5!, 0.0!)

   DIM g_triangleStrip(7)

   FillVertex(g_triangleStrip(0), 255, 0, 0, 255, -2.0!, 0.0!, 0.0!)
   FillVertex(g_triangleStrip(1), 0, 0, 255, 255, -1.0!, 0.0!, 0.0!)
   FillVertex(g_triangleStrip(2), 0, 255, 0, 255, -1.0!, 1.0!, 0.0!)
   FillVertex(g_triangleStrip(3), 255, 0, 255, 255, 0.0!, 0.0!, 0.0!)
   FillVertex(g_triangleStrip(4), 255, 255, 0, 255, 0.0!, 1.0!, 0.0!)
   FillVertex(g_triangleStrip(5), 255, 0, 0, 255, 1.0!, 0.0!, 0.0!)
   FillVertex(g_triangleStrip(6), 0, 255, 255, 255, 1.0!, 1.0!, 0.0!)
   FillVertex(g_triangleStrip(7), 0, 255, 0, 255, 2.0!, 1.0!, 0.0!)

   DIM g_triangleFan(5)

   FillVertex(g_triangleFan(0), 255, 0, 0, 255, 0.0!, -1.0!, 0.0!)
   FillVertex(g_triangleFan(1), 0, 255, 255, 255, 1.0!, 0.0!, 0.0!)
   FillVertex(g_triangleFan(2), 255, 0, 255, 255, 0.5!, 0.5!, 0.0!)
   FillVertex(g_triangleFan(3), 255, 255, 0, 255, 0.0!, 1.0!, 0.0!)
   FillVertex(g_triangleFan(4), 0, 0, 255, 255, -0.5!, 0.5!, 0.0!)
   FillVertex(g_triangleFan(5), 0, 255, 0, 255, -1.0!, 0.5!, 0.0!)

   DIM g_quads(11)

   FillVertex(g_quads(0), 255, 0, 0, 255, -0.5!, -0.5!, 0.0!)
   FillVertex(g_quads(1), 0, 255, 0, 255, 0.5!, -0.5!, 0.0!)
   FillVertex(g_quads(2), 0, 0, 255, 255, 0.5!, 0.5!, 0.0!)
   FillVertex(g_quads(3), 255, 255, 0, 255, -0.5!, 0.5!, 0.0!)
   FillVertex(g_quads(4), 255, 0, 255, 255, -1.5!, -1.5!, 0.0!)
   FillVertex(g_quads(5), 0, 255, 255, 255, -1.0!, -1.0!, 0.0!)
   FillVertex(g_quads(6), 255, 0, 0, 255, -1.0!, 1.5!, 0.0!)
   FillVertex(g_quads(7), 0, 255, 0, 255, -1.5!, 1.5!, 0.0!)
   FillVertex(g_quads(8), 0, 0, 255, 255, 1.0!, -0.2!, 0.0!)
   FillVertex(g_quads(9), 255, 255, 0, 255, 2.0!, -0.2!, 0.0!)
   FillVertex(g_quads(10), 0, 255, 255, 255, 2.0!, 0.2!, 0.0!)
   FillVertex(g_quads(11), 255, 0, 255, 255, 1.0!, 0.2!, 0.0!)

   DIM g_quadStrip(7)

   FillVertex(g_quadStrip(0), 255, 0, 0, 255, -0.5!, -1.5!, 0.0!)
   FillVertex(g_quadStrip(1), 0, 255, 0, 255, 0.5!, -1.5!, 0.0!)
   FillVertex(g_quadStrip(2), 0, 0, 255, 255, -0.2!, -0.5!, 0.0!)
   FillVertex(g_quadStrip(3), 255, 255, 0, 255, 0.2!, -0.5!, 0.0!)
   FillVertex(g_quadStrip(4), 255, 0, 255, 255, -0.5!, 0.5!, 0.0!)
   FillVertex(g_quadStrip(5), 0, 255, 255, 255, 0.5!, 0.5!, 0.0!)
   FillVertex(g_quadStrip(6), 255, 0, 0, 255, -0.4!, 1.5!, 0.0!)
   FillVertex(g_quadStrip(7), 0, 255, 0, 255, 0.4!, 1.5!, 0.0!)

   DIM g_polygon(4)

   FillVertex(g_polygon(0), 255, 0, 0, 255, -0.3!, -1.5!, 0.0!)
   FillVertex(g_polygon(1), 0, 255, 0, 255, 0.3!, -1.5!, 0.0!)
   FillVertex(g_polygon(2), 0, 0, 255, 255, 0.5!, 0.5!, 0.0!)
   FillVertex(g_polygon(3), 255, 255, 0, 255, 0.0!, 1.5!, 0.0!)
   FillVertex(g_polygon(4), 255, 0, 255, 255, -0.5!, 0.5!, 0.0!)

   glClearColor 0.0!, 0.0!, 0.0!, 0.0!
glCullFace %GL_BACK
glEnable %GL_CULL_FACE

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!, -5.0!       ' Move into the screen 5.0

   SELECT CASE g_currentPrimitive
      CASE %GL_POINTS
         glInterleavedArrays %GL_C4UB_V3F, 0, g_points(0)
         glDrawArrays %GL_POINTS, 0, 5
      CASE %GL_LINES
         glInterleavedArrays %GL_C4UB_V3F, 0, g_lines(0)
         glDrawArrays %GL_LINES, 0, 6
      CASE %GL_LINE_STRIP
         glInterleavedArrays %GL_C4UB_V3F, 0, g_lineStrip_and_lineLoop(0)
         glDrawArrays %GL_LINE_STRIP, 0, 6
      CASE %GL_LINE_LOOP
         glInterleavedArrays %GL_C4UB_V3F, 0, g_lineStrip_and_lineLoop(0)
         glDrawArrays %GL_LINE_LOOP, 0, 6
      CASE %GL_TRIANGLES
         glInterleavedArrays %GL_C4UB_V3F, 0, g_triangles(0)
         glDrawArrays %GL_TRIANGLES, 0, 6
      CASE %GL_TRIANGLE_STRIP
         glInterleavedArrays %GL_C4UB_V3F, 0, g_triangleStrip(0)
         glDrawArrays %GL_TRIANGLE_STRIP, 0, 8
      CASE %GL_TRIANGLE_FAN
         glInterleavedArrays %GL_C4UB_V3F, 0, g_triangleFan(0)
         glDrawArrays %GL_TRIANGLE_FAN, 0, 6
      CASE %GL_QUADS
         glInterleavedArrays %GL_C4UB_V3F, 0, g_quads(0)
         glDrawArrays %GL_QUADS, 0, 12
      CASE %GL_QUAD_STRIP
         glInterleavedArrays %GL_C4UB_V3F, 0, g_quadStrip(0)
         glDrawArrays %GL_QUAD_STRIP, 0, 8
      CASE %GL_POLYGON
         glInterleavedArrays %GL_C4UB_V3F, 0, g_polygon(0)
         glDrawArrays %GL_POLYGON, 0, 5
   END SELECT

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 bRenderInWireFrame AS LONG

   SELECT CASE AS LONG vKeyCode

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

      CASE %VK_F1
         IF ISTRUE bKeyDown THEN
            IF g_currentPrimitive = %GL_POINTS THEN
               g_currentPrimitive = %GL_LINES
            ELSEIF g_currentPrimitive = %GL_LINES THEN
               g_currentPrimitive = %GL_LINE_STRIP
            ELSEIF g_currentPrimitive = %GL_LINE_STRIP THEN
               g_currentPrimitive = %GL_LINE_LOOP
            ELSEIF g_currentPrimitive = %GL_LINE_LOOP THEN
               g_currentPrimitive = %GL_TRIANGLES
            ELSEIF g_currentPrimitive = %GL_TRIANGLES THEN
               g_currentPrimitive = %GL_TRIANGLE_STRIP
            ELSEIF g_currentPrimitive = %GL_TRIANGLE_STRIP THEN
               g_currentPrimitive = %GL_TRIANGLE_FAN
            ELSEIF g_currentPrimitive = %GL_TRIANGLE_FAN THEN
               g_currentPrimitive = %GL_QUADS
            ELSEIF g_currentPrimitive = %GL_QUADS THEN
               g_currentPrimitive = %GL_QUAD_STRIP
            ELSEIF g_currentPrimitive = %GL_QUAD_STRIP THEN
               g_currentPrimitive = %GL_POLYGON
            ELSEIF g_currentPrimitive = %GL_POLYGON THEN
               g_currentPrimitive = %GL_POINTS
            END IF
         END IF

      CASE %VK_F2
         IF ISTRUE bKeyDown THEN
            bRenderInWireFrame = NOT bRenderInWireFrame
            IF ISTRUE bRenderInWireFrame THEN
               glPolygonMode %GL_FRONT, %GL_LINE
            ELSE
               glPolygonMode %GL_FRONT, %GL_FILL
            END IF
         END IF

   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
         ' 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 a 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
' =======================================================================================


« Last Edit: August 07, 2011, 07:59:24 PM by José Roca »

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2480
Re: OpenGL: Primitive Types [GLFW Version]
« Reply #1 on: September 16, 2008, 02:10:09 AM »
This sample demonstrates how to use the 10 primitive types available under OpenGL: points, lines, line strip, line loop, triangles triangle strips, triangle fan, quads, quads strips, and polygon.

It is an adaptation of ogl_primitive_types.cpp, by Kevin Harris, 02/01/05, downloadable at http://www.codesampler.com/oglsrc/oglsrc_1.htm#ogl_primitive_types

Code: [Select]
' SED_PBWIN - Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "GLFW.INC"

TYPE Vertex
   r AS BYTE
   g AS BYTE
   b AS BYTE
   a AS BYTE
   x AS SINGLE
   y AS SINGLE
   z AS SINGLE
END TYPE

FUNCTION PBMAIN () AS LONG

   LOCAL nWidth, nHeight, running, frames, x, y AS LONG
   LOCAL t, t0, fps AS DOUBLE
   LOCAL szTitlestr AS ASCIIZ * 200
   LOCAL kf1, kf2, bRenderInWireFrame, currentPrimitive AS LONG

   currentPrimitive = %GL_POLYGON

   DIM g_points (4) AS Vertex
   DIM g_lines (5) AS Vertex
   DIM g_lineStrip_and_lineLoop (5) AS Vertex
   DIM g_triangles (5) AS Vertex
   DIM g_triangleStrip (7) AS Vertex
   DIM g_triangleFan(5) AS Vertex
   DIM g_quads (11) AS Vertex
   DIM g_quadStrip (7) AS Vertex
   DIM g_polygon (4) AS Vertex

   g_points(0).r = 255
   g_points(0).g = 0
   g_points(0).b = 0
   g_points(0).a = 255
   g_points(0).x = 0.0!
   g_points(0).y = 0.0!
   g_points(0).z = 0.0!

   g_points(1).r = 0
   g_points(1).g = 255
   g_points(1).b = 0
   g_points(1).a = 255
   g_points(1).x = 0.5!
   g_points(1).y = 0.0!
   g_points(1).z = 0.0!

   g_points(2).r = 0
   g_points(2).g = 0
   g_points(2).b = 255
   g_points(2).a = 255
   g_points(2).x = -0.5!
   g_points(2).y = 0.0!
   g_points(2).z = 0.0!

   g_points(3).r = 255
   g_points(3).g = 255
   g_points(3).b = 0
   g_points(3).a = 255
   g_points(3).x = 0.0!
   g_points(3).y = -0.5!
   g_points(3).z = 0.0!

   g_points(4).r = 255
   g_points(4).g = 0
   g_points(4).b = 255
   g_points(4).a = 255
   g_points(4).x = 0.0!
   g_points(4).y = 0.5!
   g_points(4).z = 0.0!

   g_lines(0).r = 255
   g_lines(0).g = 0
   g_lines(0).b = 0
   g_lines(0).a = 255
   g_lines(0).x = -1.0!
   g_lines(0).y = 0.0!
   g_lines(0).z = 0.0!

   g_lines(1).r = 255
   g_lines(1).g = 0
   g_lines(1).b = 0
   g_lines(1).a = 255
   g_lines(1).x = 0.0!
   g_lines(1).y = 1.0!
   g_lines(1).z = 0.0!

   g_lines(2).r = 0
   g_lines(2).g = 255
   g_lines(2).b = 0
   g_lines(2).a = 255
   g_lines(2).x = 0.5!
   g_lines(2).y = 1.0!
   g_lines(2).z = 0.0!

   g_lines(3).r = 0
   g_lines(3).g = 255
   g_lines(3).b = 0
   g_lines(3).a = 255
   g_lines(3).x = 0.5!
   g_lines(3).y = -1.0!
   g_lines(3).z = 0.0!

   g_lines(4).r = 0
   g_lines(4).g = 0
   g_lines(4).b = 255
   g_lines(4).a = 255
   g_lines(4).x = 1.0!
   g_lines(4).y = -0.5!
   g_lines(4).z = 0.0!

   g_lines(5).r = 0
   g_lines(5).g = 0
   g_lines(5).b = 255
   g_lines(5).a = 255
   g_lines(5).x = -1.0!
   g_lines(5).y = -0.5!
   g_lines(5).z = 0.0!

   '//
   '// Note:
   '//
   '// We'll use the same vertices for both the line-strip and line-loop
   '// demonstrations. The difference between the two primitive types will not be
   '// noticeable until the vertices are rendered. Line-loop works exactly like
   '// line-strip except it creates a closed loop by automatically connecting
   '// the last vertex to the first with a line segment.
   '//

   g_lineStrip_and_lineLoop(0).r = 255
   g_lineStrip_and_lineLoop(0).g = 0
   g_lineStrip_and_lineLoop(0).b = 0
   g_lineStrip_and_lineLoop(0).a = 255
   g_lineStrip_and_lineLoop(0).x = 0.5!
   g_lineStrip_and_lineLoop(0).y = 0.5!
   g_lineStrip_and_lineLoop(0).z = 0.0!

   g_lineStrip_and_lineLoop(1).r = 0
   g_lineStrip_and_lineLoop(1).g = 255
   g_lineStrip_and_lineLoop(1).b = 0
   g_lineStrip_and_lineLoop(1).a = 255
   g_lineStrip_and_lineLoop(1).x = 1.0!
   g_lineStrip_and_lineLoop(1).y = 0.0!
   g_lineStrip_and_lineLoop(1).z = 0.0!

   g_lineStrip_and_lineLoop(2).r = 0
   g_lineStrip_and_lineLoop(2).g = 0
   g_lineStrip_and_lineLoop(2).b = 255
   g_lineStrip_and_lineLoop(2).a = 255
   g_lineStrip_and_lineLoop(2).x = 0.0!
   g_lineStrip_and_lineLoop(2).y = -1.0!
   g_lineStrip_and_lineLoop(2).z = 0.0!

   g_lineStrip_and_lineLoop(3).r = 255
   g_lineStrip_and_lineLoop(3).g = 255
   g_lineStrip_and_lineLoop(3).b = 0
   g_lineStrip_and_lineLoop(3).a = 255
   g_lineStrip_and_lineLoop(3).x = -1.0!
   g_lineStrip_and_lineLoop(3).y = 0.0!
   g_lineStrip_and_lineLoop(3).z = 0.0!

   g_lineStrip_and_lineLoop(4).r = 255
   g_lineStrip_and_lineLoop(4).g = 0
   g_lineStrip_and_lineLoop(4).b = 0
   g_lineStrip_and_lineLoop(4).a = 255
   g_lineStrip_and_lineLoop(4).x = 0.0!
   g_lineStrip_and_lineLoop(4).y = 0.0!
   g_lineStrip_and_lineLoop(4).z = 0.0!

   g_lineStrip_and_lineLoop(5).r = 255
   g_lineStrip_and_lineLoop(5).g = 0
   g_lineStrip_and_lineLoop(5).b = 255
   g_lineStrip_and_lineLoop(5).a = 255
   g_lineStrip_and_lineLoop(5).x = 0.0!
   g_lineStrip_and_lineLoop(5).y = 1.0!
   g_lineStrip_and_lineLoop(5).z = 0.0!

   g_triangles(0).r = 255
   g_triangles(0).g = 0
   g_triangles(0).b = 0
   g_triangles(0).a = 255
   g_triangles(0).x = -1.0!
   g_triangles(0).y = 0.0!
   g_triangles(0).z = 0.0!

   g_triangles(1).r = 0
   g_triangles(1).g = 0
   g_triangles(1).b = 255
   g_triangles(1).a = 255
   g_triangles(1).x = 1.0!
   g_triangles(1).y = 0.0!
   g_triangles(1).z = 0.0!

   g_triangles(2).r = 0
   g_triangles(2).g = 255
   g_triangles(2).b = 0
   g_triangles(2).a = 255
   g_triangles(2).x = 0.0!
   g_triangles(2).y = 1.0!
   g_triangles(2).z = 0.0!

   g_triangles(3).r = 255
   g_triangles(3).g = 255
   g_triangles(3).b = 0
   g_triangles(3).a = 255
   g_triangles(3).x = -0.5!
   g_triangles(3).y = 1.0!
   g_triangles(3).z = 0.0!

   g_triangles(4).r = 255
   g_triangles(4).g = 0
   g_triangles(4).b = 0
   g_triangles(4).a = 255
   g_triangles(4).x = 0.5!
   g_triangles(4).y = -1.0!
   g_triangles(4).z = 0.0!

   g_triangles(5).r = 0
   g_triangles(5).g = 255
   g_triangles(5).b = 255
   g_triangles(5).a = 255
   g_triangles(5).x = 0.0!
   g_triangles(5).y = -0.5!
   g_triangles(5).z = 0.0!

   g_triangleStrip(0).r = 255
   g_triangleStrip(0).g = 0
   g_triangleStrip(0).b = 0
   g_triangleStrip(0).a = 255
   g_triangleStrip(0).x = -2.0!
   g_triangleStrip(0).y = 0.0!
   g_triangleStrip(0).z = 0.0!

   g_triangleStrip(1).r = 0
   g_triangleStrip(1).g = 0
   g_triangleStrip(1).b = 255
   g_triangleStrip(1).a = 255
   g_triangleStrip(1).x = -1.0!
   g_triangleStrip(1).y = 0.0!
   g_triangleStrip(1).z = 0.0!

   g_triangleStrip(2).r = 0
   g_triangleStrip(2).g = 255
   g_triangleStrip(2).b = 0
   g_triangleStrip(2).a = 255
   g_triangleStrip(2).x = -1.0!
   g_triangleStrip(2).y = 1.0!
   g_triangleStrip(2).z = 0.0!

   g_triangleStrip(3).r = 255
   g_triangleStrip(3).g = 0
   g_triangleStrip(3).b = 255
   g_triangleStrip(3).a = 255
   g_triangleStrip(3).x = 0.0!
   g_triangleStrip(3).y = 0.0!
   g_triangleStrip(3).z = 0.0!

   g_triangleStrip(4).r = 255
   g_triangleStrip(4).g = 255
   g_triangleStrip(4).b = 0
   g_triangleStrip(4).a = 255
   g_triangleStrip(4).x = 0.0!
   g_triangleStrip(4).y = 1.0!
   g_triangleStrip(4).z = 0.0!

   g_triangleStrip(5).r = 255
   g_triangleStrip(5).g = 0
   g_triangleStrip(5).b = 0
   g_triangleStrip(5).a = 255
   g_triangleStrip(5).x = 1.0!
   g_triangleStrip(5).y = 0.0!
   g_triangleStrip(5).z = 0.0!

   g_triangleStrip(6).r = 0
   g_triangleStrip(6).g = 255
   g_triangleStrip(6).b = 255
   g_triangleStrip(6).a = 255
   g_triangleStrip(6).x = 1.0!
   g_triangleStrip(6).y = 1.0!
   g_triangleStrip(6).z = 0.0!

   g_triangleStrip(7).r = 0
   g_triangleStrip(7).g = 255
   g_triangleStrip(7).b = 0
   g_triangleStrip(7).a = 255
   g_triangleStrip(7).x = 2.0!
   g_triangleStrip(7).y = 1.0!
   g_triangleStrip(7).z = 0.0!

   g_triangleFan(0).r = 255
   g_triangleFan(0).g = 0
   g_triangleFan(0).b = 0
   g_triangleFan(0).a = 255
   g_triangleFan(0).x = 0.0!
   g_triangleFan(0).y = -1.0!
   g_triangleFan(0).z = 0.0!

   g_triangleFan(1).r = 0
   g_triangleFan(1).g = 255
   g_triangleFan(1).b = 255
   g_triangleFan(1).a = 255
   g_triangleFan(1).x = 1.0!
   g_triangleFan(1).y = 0.0!
   g_triangleFan(1).z = 0.0!

   g_triangleFan(2).r = 255
   g_triangleFan(2).g = 0
   g_triangleFan(2).b = 255
   g_triangleFan(2).a = 255
   g_triangleFan(2).x = 0.5!
   g_triangleFan(2).y = 0.5!
   g_triangleFan(2).z = 0.0!

   g_triangleFan(3).r = 255
   g_triangleFan(3).g = 255
   g_triangleFan(3).b = 0
   g_triangleFan(3).a = 255
   g_triangleFan(3).x = 0.0!
   g_triangleFan(3).y = 1.0!
   g_triangleFan(3).z = 0.0!

   g_triangleFan(4).r = 0
   g_triangleFan(4).g = 0
   g_triangleFan(4).b = 255
   g_triangleFan(4).a = 255
   g_triangleFan(4).x = -0.5!
   g_triangleFan(4).y = 0.5!
   g_triangleFan(4).z = 0.0!

   g_triangleFan(5).r = 0
   g_triangleFan(5).g = 255
   g_triangleFan(5).b = 0
   g_triangleFan(5).a = 255
   g_triangleFan(5).x = -1.0!
   g_triangleFan(5).y = 0.5!
   g_triangleFan(5).z = 0.0!

   g_quads(0).r = 255
   g_quads(0).g = 0
   g_quads(0).b = 0
   g_quads(0).a = 255
   g_quads(0).x = -0.5!
   g_quads(0).y = -0.5!
   g_quads(0).z = 0.0!

   g_quads(1).r = 0
   g_quads(1).g = 255
   g_quads(1).b = 0
   g_quads(1).a = 255
   g_quads(1).x = 0.5!
   g_quads(1).y = -0.5!
   g_quads(1).z = 0.0!

   g_quads(2).r = 0
   g_quads(2).g = 0
   g_quads(2).b = 255
   g_quads(2).a = 255
   g_quads(2).x = 0.5!
   g_quads(2).y = 0.5!
   g_quads(2).z = 0.0!

   g_quads(3).r = 255
   g_quads(3).g = 255
   g_quads(3).b = 0
   g_quads(3).a = 255
   g_quads(3).x = -0.5!
   g_quads(3).y = 0.5!
   g_quads(3).z = 0.0!

   g_quads(4).r = 255
   g_quads(4).g = 0
   g_quads(4).b = 255
   g_quads(4).a = 255
   g_quads(4).x = -1.5!
   g_quads(4).y = -1.5!
   g_quads(4).z = 0.0!

   g_quads(5).r = 0
   g_quads(5).g = 255
   g_quads(5).b = 255
   g_quads(5).a = 255
   g_quads(5).x = -1.0!
   g_quads(5).y = -1.0!
   g_quads(5).z = 0.0!

   g_quads(6).r = 255
   g_quads(6).g = 0
   g_quads(6).b = 0
   g_quads(6).a = 255
   g_quads(6).x = -1.0!
   g_quads(6).y = 1.5!
   g_quads(6).z = 0.0!

   g_quads(7).r = 0
   g_quads(7).g = 255
   g_quads(7).b = 0
   g_quads(7).a = 255
   g_quads(7).x = -1.5!
   g_quads(7).y = 1.5!
   g_quads(7).z = 0.0!

   g_quads(8).r = 0
   g_quads(8).g = 0
   g_quads(8).b = 255
   g_quads(8).a = 255
   g_quads(8).x = 1.0!
   g_quads(8).y = -0.2!
   g_quads(8).z = 0.0!

   g_quads(9).r = 255
   g_quads(9).g = 255
   g_quads(9).b = 0
   g_quads(9).a = 255
   g_quads(9).x = 2.0!
   g_quads(9).y = -0.2!
   g_quads(9).z = 0.0!

   g_quads(10).r = 0
   g_quads(10).g = 255
   g_quads(10).b = 255
   g_quads(10).a = 255
   g_quads(10).x = 2.0!
   g_quads(10).y = 0.2!
   g_quads(10).z = 0.0!

   g_quads(11).r = 255
   g_quads(11).g = 0
   g_quads(11).b = 255
   g_quads(11).a = 255
   g_quads(11).x = 1.0!
   g_quads(11).y = 0.2!
   g_quads(11).z = 0.0!

   g_quadStrip(0).r = 255
   g_quadStrip(0).g = 0
   g_quadStrip(0).b = 0
   g_quadStrip(0).a = 255
   g_quadStrip(0).x = -0.5!
   g_quadStrip(0).y = -1.5!
   g_quadStrip(0).z = 0.0!

   g_quadStrip(1).r = 0
   g_quadStrip(1).g = 255
   g_quadStrip(1).b = 0
   g_quadStrip(1).a = 255
   g_quadStrip(1).x = 0.5!
   g_quadStrip(1).y = -1.5!
   g_quadStrip(1).z = 0.0!

   g_quadStrip(2).r = 0
   g_quadStrip(2).g = 0
   g_quadStrip(2).b = 255
   g_quadStrip(2).a = 255
   g_quadStrip(2).x = -0.2!
   g_quadStrip(2).y = -0.5!
   g_quadStrip(2).z = 0.0!

   g_quadStrip(3).r = 255
   g_quadStrip(3).g = 255
   g_quadStrip(3).b = 0
   g_quadStrip(3).a = 255
   g_quadStrip(3).x = 0.2!
   g_quadStrip(3).y = -0.5!
   g_quadStrip(3).z = 0.0!

   g_quadStrip(4).r = 255
   g_quadStrip(4).g = 0
   g_quadStrip(4).b = 255
   g_quadStrip(4).a = 255
   g_quadStrip(4).x = -0.5!
   g_quadStrip(4).y = 0.5!
   g_quadStrip(4).z = 0.0!

   g_quadStrip(5).r = 0
   g_quadStrip(5).g = 255
   g_quadStrip(5).b = 255
   g_quadStrip(5).a = 255
   g_quadStrip(5).x = 0.5!
   g_quadStrip(5).y = 0.5!
   g_quadStrip(5).z = 0.0!

   g_quadStrip(6).r = 255
   g_quadStrip(6).g = 0
   g_quadStrip(6).b = 0
   g_quadStrip(6).a = 255
   g_quadStrip(6).x = -0.4!
   g_quadStrip(6).y = 1.5!
   g_quadStrip(6).z = 0.0!

   g_quadStrip(7).r = 0
   g_quadStrip(7).g = 255
   g_quadStrip(7).b = 0
   g_quadStrip(7).a = 255
   g_quadStrip(7).x = 0.4!
   g_quadStrip(7).y = 1.5!
   g_quadStrip(7).z = 0.0!

   g_polygon(0).r = 255
   g_polygon(0).g = 0
   g_polygon(0).b = 0
   g_polygon(0).a = 255
   g_polygon(0).x = -0.3!
   g_polygon(0).y = -1.5!
   g_polygon(0).z = 0.0!

   g_polygon(1).r = 0
   g_polygon(1).g = 255
   g_polygon(1).b = 0
   g_polygon(1).a = 255
   g_polygon(1).x = 0.3!
   g_polygon(1).y = -1.5!
   g_polygon(1).z = 0.0!

   g_polygon(2).r = 0
   g_polygon(2).g = 0
   g_polygon(2).b = 255
   g_polygon(2).a = 255
   g_polygon(2).x = 0.5!
   g_polygon(2).y = 0.5!
   g_polygon(2).z = 0.0!

   g_polygon(3).r = 255
   g_polygon(3).g = 255
   g_polygon(3).b = 0
   g_polygon(3).a = 255
   g_polygon(3).x = 0.0!
   g_polygon(3).y = 1.5!
   g_polygon(3).z = 0.0!

   g_polygon(4).r = 255
   g_polygon(4).g = 0
   g_polygon(4).b = 255
   g_polygon(4).a = 255
   g_polygon(4).x = -0.5!
   g_polygon(4).y = 0.5!
   g_polygon(4).z = 0.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

   ' Specify clear values for the color buffers
   glClearColor 0.0!, 0.0!, 0.0!, 0.0!
   ' Specify that back-facing facets can be culled
glCullFace %GL_BACK
glEnable %GL_CULL_FACE

   ' 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, "OpenGL - Primitve Types (%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 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, 1.0!, 100.0!

      ' Select and setup the modelview matrix
      glMatrixMode %GL_MODELVIEW
      glLoadIdentity

      glTranslatef 0.0!, 0.0!, -5.0!        ' Move into the screen 5.0

      SELECT CASE currentPrimitive
         CASE %GL_POINTS
            glInterleavedArrays %GL_C4UB_V3F, 0, g_points(0)
            glDrawArrays %GL_POINTS, 0, 5
         CASE %GL_LINES
            glInterleavedArrays %GL_C4UB_V3F, 0, g_lines(0)
            glDrawArrays %GL_LINES, 0, 6
         CASE %GL_LINE_STRIP
            glInterleavedArrays %GL_C4UB_V3F, 0, g_lineStrip_and_lineLoop(0)
            glDrawArrays %GL_LINE_STRIP, 0, 6
         CASE %GL_LINE_LOOP
            glInterleavedArrays %GL_C4UB_V3F, 0, g_lineStrip_and_lineLoop(0)
            glDrawArrays %GL_LINE_LOOP, 0, 6
         CASE %GL_TRIANGLES
            glInterleavedArrays %GL_C4UB_V3F, 0, g_triangles(0)
            glDrawArrays %GL_TRIANGLES, 0, 6
         CASE %GL_TRIANGLE_STRIP
            glInterleavedArrays %GL_C4UB_V3F, 0, g_triangleStrip(0)
            glDrawArrays %GL_TRIANGLE_STRIP, 0, 8
         CASE %GL_TRIANGLE_FAN
            glInterleavedArrays %GL_C4UB_V3F, 0, g_triangleFan(0)
            glDrawArrays %GL_TRIANGLE_FAN, 0, 6
         CASE %GL_QUADS
            glInterleavedArrays %GL_C4UB_V3F, 0, g_quads(0)
            glDrawArrays %GL_QUADS, 0, 12
         CASE %GL_QUAD_STRIP
            glInterleavedArrays %GL_C4UB_V3F, 0, g_quadStrip(0)
            glDrawArrays %GL_QUAD_STRIP, 0, 8
         CASE %GL_POLYGON
            glInterleavedArrays %GL_C4UB_V3F, 0, g_polygon(0)
            glDrawArrays %GL_POLYGON, 0, 5
      END SELECT

      ' Swap buffers
      glfwSwapBuffers

      ' F1
      IF glfwGetKey(%GLFW_KEY_F1) = %GLFW_PRESS AND ISFALSE kf1 THEN
         kf1 = %TRUE
         IF currentPrimitive = %GL_POINTS THEN
            currentPrimitive = %GL_LINES
         ELSEIF currentPrimitive = %GL_LINES THEN
            currentPrimitive = %GL_LINE_STRIP
         ELSEIF currentPrimitive = %GL_LINE_STRIP THEN
            currentPrimitive = %GL_LINE_LOOP
         ELSEIF currentPrimitive = %GL_LINE_LOOP THEN
            currentPrimitive = %GL_TRIANGLES
         ELSEIF currentPrimitive = %GL_TRIANGLES THEN
            currentPrimitive = %GL_TRIANGLE_STRIP
         ELSEIF currentPrimitive = %GL_TRIANGLE_STRIP THEN
            currentPrimitive = %GL_TRIANGLE_FAN
         ELSEIF currentPrimitive = %GL_TRIANGLE_FAN THEN
            currentPrimitive = %GL_QUADS
         ELSEIF currentPrimitive = %GL_QUADS THEN
            currentPrimitive = %GL_QUAD_STRIP
         ELSEIF currentPrimitive = %GL_QUAD_STRIP THEN
            currentPrimitive = %GL_POLYGON
         ELSEIF currentPrimitive = %GL_POLYGON THEN
            currentPrimitive = %GL_POINTS
         END IF
      END IF
      IF glfwGetKey(%GLFW_KEY_F1) <> %GLFW_PRESS THEN kf1 = %FALSE

      ' F2
      IF glfwGetKey(%GLFW_KEY_F2) = %GLFW_PRESS AND ISFALSE kf2 THEN
         kf2 = %TRUE
         bRenderInWireFrame = NOT bRenderInWireFrame
         IF ISTRUE bRenderInWireFrame THEN
            glPolygonMode %GL_FRONT, %GL_LINE
         ELSE
            glPolygonMode %GL_FRONT, %GL_FILL
         END IF
      END IF
      IF glfwGetKey(%GLFW_KEY_F2) <> %GLFW_PRESS THEN kf2 = %FALSE

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

   LOOP

   ' Close OpenGL window and terminate GLFW
   glfwTerminate

END FUNCTION
« Last Edit: August 07, 2011, 07:59:45 PM by José Roca »