The following sample code draws a gouraud shaded octahedron.

/* sample code for using OpenGL and PGL  */
/* Draws a gouraud shaded octahedron     */

#include <stdio.h>
#include "pgl.h"     /* PGL calls    */
#include "gl.h"      /* OpenGL calls */
#define PM_ESCAPE 0x0f
#define MSGBOXID 22
#define SQRT2  1.414

/* attributes passed into pglChooseConfig */
int attriblistíù = {
  PGL_DOUBLEBUFFER,  /* request doublebuffered visual config   */
  PGL_RGBA,          /* request rgb (true color) visual config */
  None               /* always end list with this              */
};

HAB hab;

void DispError(PSZ errstr)
{
  char bufferí256ù;
  sprintf(buffer, "Error (0x%x) in SAMPOGL.EXE:", WinGetLastError(hab));
  WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,errstr,buffer,
                MSGBOXID,MB_MOVEABLE!MB_CUACRITICAL!MB_CANCEL);
  exit(0);
}

void Setup()
{
  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
  glDepthMask(GL_FALSE);
  glEnable(GL_CULL_FACE);
}

float vertsíùí3ù = {
  { 0.0, 0.0, (1.0/SQRT2)},
  { 0.5, 0.5, 0.0},
  {-0.5, 0.5, 0.0},
  {-0.5,-0.5, 0.0},
  { 0.5,-0.5, 0.0},
  { 0.0, 0.0, -(1.0/SQRT2)}
};

float colorsíùí3ù = {
  {1.0, 1.0, 1.0},
  {1.0, 0.0, 0.0},
  {0.0, 1.0, 0.0},
  {0.0, 0.0, 1.0},
  {1.0, 0.0, 1.0},
  {0.0, 1.0, 1.0},
};

MRESULT EXPENTRY WindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
  static float t = 0.0;
  static SWP clientsize;
  static USHORT mycode;
  static UCHAR key;

  switch(msg) {
  case WM_SIZE:
    /* Upon a resize, query new window size and set OpenGL viewport */
    WinQueryWindowPos(hwnd,&clientsize);
    glViewport(0, 0, clientsize.cx, clientsize.cy);
    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  case WM_TIMER:
    /* Upon getting a timer message, the invalidate rectangle call  */
    /* will cause a WM_PAINT message to be sent, enabling animation */
    WinInvalidateRect(hwnd, NULLHANDLE, NULL);
    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  case WM_PAINT:
    /* This is what is done for every frame of the animation        */
    t += 1.0;
    glClear(GL_COLOR_BUFFER_BIT ! GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(t, 1.0, 1.0, 1.0);
    glBegin(GL_TRIANGLE_FAN);
      glColor3fv(colorsí0ù);
      glVertex3fv(vertsí0ù);
      glColor3fv(colorsí1ù);
      glVertex3fv(vertsí1ù);
      glColor3fv(colorsí2ù);
      glVertex3fv(vertsí2ù);
      glColor3fv(colorsí3ù);
      glVertex3fv(vertsí3ù);
      glColor3fv(colorsí4ù);
      glVertex3fv(vertsí4ù);
      glColor3fv(colorsí1ù);
      glVertex3fv(vertsí1ù);
    glEnd();
    glBegin(GL_TRIANGLE_FAN);
      glColor3fv(colorsí5ù);
      glVertex3fv(vertsí5ù);
      glColor3fv(colorsí1ù);
      glVertex3fv(vertsí1ù);
      glColor3fv(colorsí4ù);
      glVertex3fv(vertsí4ù);
      glColor3fv(colorsí3ù);
      glVertex3fv(vertsí3ù);
      glColor3fv(colorsí2ù);
      glVertex3fv(vertsí2ù);
      glColor3fv(colorsí1ù);
      glVertex3fv(vertsí1ù);
    glEnd();
    glPopMatrix();
    pglSwapBuffers(hab, hwnd);
    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  case WM_CHAR:
    mycode = (USHORT)SHORT1FROMMP(mp1);
    if ((mycode & KC_CHAR) && !(mycode & KC_KEYUP))
      key = CHAR1FROMMP(mp2);
    else if ((mycode & KC_VIRTUALKEY) && !(mycode & KC_KEYUP))
      key = CHAR3FROMMP(mp2);
    if (key == PM_ESCAPE)
      WinPostMsg(hwnd, WM_CLOSE, (MPARAM)0, (MPARAM)0);
    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  default:
    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  }
}
main(int argc, char **argv)
{
  PVISUALCONFIG vishead; /* visual configuration             */
  HMQ hmq;               /* message queue                    */
  HWND hwnd;
  HWND hwndFrame;
  ULONG createflags = FCF_TITLEBAR !
                      FCF_SYSMENU  !
                      FCF_MINMAX   !
                      FCF_SIZEBORDER;
  QMSG qmsg;             /* message                          */
  HGC hgc;               /* OpenGL context                   */
  int  major, minor;     /* OpenGL version                   */
  int err;

  hab = WinInitialize(0);
  hmq = WinCreateMsgQueue(hab, 0);
  if (!hmq)
    DispError("Couldn't create a message queue!\n");

  /* Check to see if OpenGL exists */
  if (pglQueryCapability(hab)) {
    pglQueryVersion(hab, &major, &minor);
    /* Version 1.0                */
    if ((major == 1) && (minor == 0)) {
      /* Choose a visual configuration that matches desired  */
      /* attributes in attriblist                            */
      vishead = pglChooseConfig(hab, attriblist);
      if (!vishead)
        DispError("Couldn't find a visual!\n");
      if (WinRegisterClass(
            hab,
            (PSZ)"PGLtest",
            WindowProc,
            CS_SIZEREDRAW ! CS_MOVENOTIFY, /* Need at least this! */
            0))
      {
        hwndFrame = WinCreateStdWindow (
                      HWND_DESKTOP,            /* Child of the desktop    */
                      WS_VISIBLE,              /* Frame style             */
                      &createflags,        /* min FCF_MENU!FCF_MINMAX */
                      (PSZ)"PGLtest",  /* class name              */
                      "OpenGL Sample", /* window title            */
                      WS_VISIBLE,              /* client style            */
                      0,                       /* resource handle         */
                      1,                       /* Resource ID             */
                      &hwnd);              /* Window handle           */
        if (!hwndFrame)
          DispError("Couldn't create a window!\n");
        /* you must set window size before you call pglMakeCurrent */
        if (!WinSetWindowPos(
               hwndFrame,
               HWND_TOP,
               0,
               0,
               300,
               300,
               SWP_ACTIVATE ! SWP_SIZE ! SWP_MOVE ! SWP_SHOW))
          DispError("Couldn't position window!\n");
        hgc = pglCreateContext(hab,  /* anchor block handle    */
                 vishead,            /* visual configuration   */
                 (HGC)NULL,          /* (no) shared contexts   */
                 (BOOL)TRUE);        /* direct (fast) context  */
        if (!hgc)
          DispError("Couldn't create an OpenGL context!\n");
        if(!pglMakeCurrent(hab, hgc, hwnd))
          DispError("Could not bind OpenGL context to window!\n");
        /* Don't subclass your window past here! */
        Setup();
        /* Start timer to cause WM_TIMER messages to be sent   */
        /* periodically.  This is used to animate.             */
        WinStartTimer(hab, hwnd, 0L, 0L);
        while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
          WinDispatchMsg(hab, &qmsg);
      }
    }
  }
}


[Back: VISUALCONFIG Field - next]
[Next: OS/2 Character Mode Output]