home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opengl.zip / SAMPOGL.C
Text File  |  1994-11-21  |  7KB  |  205 lines

  1. /* THE TEST CASE PROVIDED UNDER THIS AGREEMENT IS PROVIDED      */
  2. /* ON AN "AS IS" BASIS WITHOUT ANY WARENTIES EXPRESS OR         */
  3. /* IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARENTIES*/
  4. /* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE      */
  5.  
  6. /* sample code for using OpenGL and PGL                         */
  7. /* Draws a gouraud shaded octahedron                            */
  8. #include <stdio.h>
  9. #include "pgl.h" /* PGL calls                             */
  10. #include "gl.h"  /* OpenGL calls  (os2.h included in gl.h)*/
  11.  
  12. #define PM_ESCAPE 0x0f
  13. #define MSGBOXID 22
  14. #define SQRT2  1.414
  15.  
  16. /* attributes passed into pglChooseConfig */
  17. int attriblist[] = {
  18.   PGL_DOUBLEBUFFER,  /* request doublebuffered visual config */
  19.   PGL_RGBA,          /* request rgb (true color) visual config */
  20.   None             /* always end list with this */
  21. };
  22.  
  23. HAB hab;
  24.  
  25. void DispError(PSZ errstr)
  26. {
  27.   char buffer[256];
  28.   sprintf(buffer, "Error (0x%x) in SAMPOGL.EXE:", WinGetLastError(hab));
  29.   WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,errstr,buffer,
  30.                 MSGBOXID,MB_MOVEABLE|MB_CUACRITICAL|MB_CANCEL);
  31.   exit(0);
  32. }
  33.  
  34. void Setup()
  35. {
  36.   glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
  37.   glDepthMask(GL_FALSE);
  38.   glEnable(GL_CULL_FACE);
  39. }
  40.  
  41. float verts[][3] = {
  42.   { 0.0, 0.0, (1.0/SQRT2)},
  43.   { 0.5, 0.5, 0.0},
  44.   {-0.5, 0.5, 0.0},
  45.   {-0.5,-0.5, 0.0},
  46.   { 0.5,-0.5, 0.0},
  47.   { 0.0, 0.0, -(1.0/SQRT2)}
  48. };
  49.  
  50. float colors[][3] = {
  51.   {1.0, 1.0, 1.0},
  52.   {1.0, 0.0, 0.0},
  53.   {0.0, 1.0, 0.0},
  54.   {0.0, 0.0, 1.0},
  55.   {1.0, 0.0, 1.0},
  56.   {0.0, 1.0, 1.0},
  57. };
  58.  
  59. MRESULT EXPENTRY WindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  60. {
  61.   static float t = 0.0;
  62.   static SWP clientsize;
  63.   static USHORT mycode;
  64.   static UCHAR key;
  65.  
  66.   switch(msg) {
  67.   case WM_SIZE:
  68.     /* Upon a resize, query new window size and set OpenGL viewport */
  69.     WinQueryWindowPos(hwnd,&clientsize);
  70.     glViewport(0, 0, clientsize.cx, clientsize.cy);
  71.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  72.   case WM_TIMER:
  73.     /* Upon getting a timer message, the invalidate rectangle call  */
  74.     /* will cause a WM_PAINT message to be sent, enabling animation */
  75.     WinInvalidateRect(hwnd, NULLHANDLE, NULL);
  76.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  77.   case WM_PAINT:
  78.     /* This is what is done for every frame of the animation        */
  79.     t += 1.0;
  80.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.     glPushMatrix();
  82.     glRotatef(t, 1.0, 1.0, 1.0);
  83.     glBegin(GL_TRIANGLE_FAN);
  84.       glColor3fv(colors[0]);
  85.       glVertex3fv(verts[0]);
  86.       glColor3fv(colors[1]);
  87.       glVertex3fv(verts[1]);
  88.       glColor3fv(colors[2]);
  89.       glVertex3fv(verts[2]);
  90.       glColor3fv(colors[3]);
  91.       glVertex3fv(verts[3]);
  92.       glColor3fv(colors[4]);
  93.       glVertex3fv(verts[4]);
  94.       glColor3fv(colors[1]);
  95.       glVertex3fv(verts[1]);
  96.     glEnd();
  97.     glBegin(GL_TRIANGLE_FAN);
  98.       glColor3fv(colors[5]);
  99.       glVertex3fv(verts[5]);
  100.       glColor3fv(colors[1]);
  101.       glVertex3fv(verts[1]);
  102.       glColor3fv(colors[4]);
  103.       glVertex3fv(verts[4]);
  104.       glColor3fv(colors[3]);
  105.       glVertex3fv(verts[3]);
  106.       glColor3fv(colors[2]);
  107.       glVertex3fv(verts[2]);
  108.       glColor3fv(colors[1]);
  109.       glVertex3fv(verts[1]);
  110.     glEnd();
  111.     glPopMatrix();
  112.     pglSwapBuffers(hab, hwnd);
  113.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  114.   case WM_CHAR: 
  115.     mycode = (USHORT)SHORT1FROMMP(mp1);
  116.     if ((mycode & KC_CHAR) && !(mycode & KC_KEYUP))
  117.       key = CHAR1FROMMP(mp2);
  118.     else if ((mycode & KC_VIRTUALKEY) && !(mycode & KC_KEYUP))
  119.       key = CHAR3FROMMP(mp2);
  120.     if (key == PM_ESCAPE)
  121.       WinPostMsg(hwnd, WM_CLOSE, (MPARAM)0, (MPARAM)0);
  122.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  123.   default:
  124.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  125.   }
  126. }
  127. main(int argc, char **argv)
  128. {
  129.   PVISUALCONFIG vishead; /* visual configuration             */
  130.   HMQ hmq;               /* message queue                    */
  131.   HWND hwnd;
  132.   HWND hwndFrame;
  133.   ULONG createflags = FCF_TITLEBAR |
  134.                       FCF_SYSMENU  |
  135.                       FCF_MINMAX   |
  136.                       FCF_SIZEBORDER;
  137.   QMSG qmsg;             /* message                          */
  138.   HGC hgc;               /* OpenGL context                   */
  139.   int  major, minor;     /* OpenGL version                   */
  140.   int err;
  141.  
  142.   hab = WinInitialize(0);
  143.  
  144.   /* Check to see if OpenGL exists */
  145.   if (pglQueryCapability(hab)) {
  146.     pglQueryVersion(hab, &major, &minor);
  147.     /* Version 1.0                */
  148.     if ((major == 1) && (minor == 0)) {
  149.       /* Choose a visual configuration that matches desired  */
  150.       /* attributes in attriblist                            */
  151.       vishead = pglChooseConfig(hab, attriblist);
  152.       if (!vishead)
  153.         DispError("Couldn't find a visual!\n");
  154.       hmq = WinCreateMsgQueue(hab, 0);
  155.       if (!hmq)
  156.         DispError("Couldn't create a message queue!\n");
  157.       if (WinRegisterClass(
  158.             hab,
  159.             (PSZ)"PGLtest",
  160.             WindowProc,
  161.             CS_SIZEREDRAW | CS_MOVENOTIFY, /* Need at least this! */
  162.             0))
  163.       {
  164.         hwndFrame = WinCreateStdWindow (
  165.                       HWND_DESKTOP,   /* Child of the desktop    */
  166.                       WS_VISIBLE,     /* Frame style             */
  167.                       &createflags,   /* min FCF_MENU|FCF_MINMAX */
  168.                       (PSZ)"PGLtest", /* class name              */
  169.                       "OpenGL Sample",/* window title            */
  170.                       WS_VISIBLE,     /* client style            */
  171.                       0,              /* resource handle         */
  172.                       1,              /* Resource ID             */
  173.                       &hwnd);         /* Window handle           */
  174.         if (!hwndFrame)
  175.           DispError("Couldn't create a window!\n");
  176.     /* you must set window size before you call pglMakeCurrent */
  177.         if (!WinSetWindowPos(
  178.                hwndFrame,
  179.                HWND_TOP,
  180.                0,
  181.                0,
  182.                300,
  183.                300,
  184.                SWP_ACTIVATE | SWP_SIZE | SWP_MOVE | SWP_SHOW))
  185.           DispError("Couldn't position window!\n");
  186.         hgc = pglCreateContext(hab,  /* anchor block handle    */
  187.                  vishead,            /* visual configuration   */
  188.                  (HGC)NULL,          /* (no) shared contexts   */
  189.                  (BOOL)TRUE);        /* direct (fast) context  */
  190.         if (!hgc)
  191.           DispError("Couldn't create an OpenGL context!\n");
  192.         if(!pglMakeCurrent(hab, hgc, hwnd)) 
  193.           DispError("Could not bind OpenGL context to window!\n");
  194.         /* Don't subclass your window past here! */
  195.         Setup();
  196.         /* Start timer to cause WM_TIMER messages to be sent   */
  197.         /* periodically.  This is used to animate.             */
  198.         WinStartTimer(hab, hwnd, 0L, 0L);
  199.         while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
  200.           WinDispatchMsg(hab, &qmsg);
  201.       }
  202.     }
  203.   }
  204. }
  205.