home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / SAMPOGL.C < prev    next >
Text File  |  1994-08-16  |  7KB  |  200 lines

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