home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / Intros / Intro / aglIntro.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  2.1 KB  |  120 lines  |  [TEXT/CWIE]

  1. /*
  2. **    aglIntro.c
  3. */
  4.  
  5. #include "agl.h"
  6.  
  7. #include <Fonts.h>
  8. #include <MacWindows.h>
  9.  
  10. #define WIDTH  300
  11. #define HEIGHT 250
  12.  
  13. /*
  14. ** OpenGL Setup
  15. */
  16. static AGLContext setupAGL(AGLDrawable win)
  17. {
  18.     GLint          attrib[] = { AGL_RGBA, AGL_NONE };
  19.     AGLPixelFormat fmt;
  20.     AGLContext     ctx;
  21.     GLboolean      ok;
  22.  
  23.     /* Choose an rgb pixel format */
  24.     fmt = aglChoosePixelFormat(NULL, 0, attrib);
  25.     if(fmt == NULL) return NULL;
  26.  
  27.     /* Create an AGL context */
  28.     ctx = aglCreateContext(fmt, NULL);
  29.     if(ctx == NULL) return NULL;
  30.  
  31.     /* Attach the window to the context */
  32.     ok = aglSetDrawable(ctx, win);
  33.     if(!ok) return NULL;
  34.     
  35.     /* Make the context the current context */
  36.     ok = aglSetCurrentContext(ctx);
  37.     if(!ok) return NULL;
  38.  
  39.     /* Pixel format is no longer needed */
  40.     aglDestroyPixelFormat(fmt);
  41.  
  42.     return ctx;
  43. }
  44.  
  45. /*
  46. ** OpenGL Cleanup
  47. */
  48. static void cleanupAGL(AGLContext ctx)
  49. {
  50.     aglSetCurrentContext(NULL);
  51.     aglSetDrawable(ctx, NULL);
  52.     aglDestroyContext(ctx);
  53. }
  54.  
  55. /*
  56. ** OpenGL Drawing
  57. */
  58. static void drawGL(void)
  59. {
  60.     /* Clear color buffer to yellow */
  61.     glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
  62.     glClear(GL_COLOR_BUFFER_BIT);
  63.     
  64.     /* Draw a smooth shaded polygon */
  65.     glBegin(GL_POLYGON);
  66.     glColor3d(1.0, 0.0, 0.0);
  67.     glVertex3d( 0.8,  0.8, 0.0);
  68.     glColor3d(0.0, 1.0, 0.0);
  69.     glVertex3d( 0.8, -0.8, 0.0);
  70.     glColor3d(0.0, 0.0, 1.0);
  71.     glVertex3d(-0.8, -0.8, 0.0);
  72.     glColor3d(1.0, 0.0, 1.0);
  73.     glVertex3d(-0.8,  0.8, 0.0);
  74.     glEnd();
  75.  
  76.     /* Ensure completion */
  77.     glFinish();
  78. }
  79.  
  80. /*
  81. ** Macintosh main routine
  82. */
  83. int main(int argc, char *argv[])
  84. {
  85.     Rect       rect;
  86.     WindowPtr  win;
  87.     AGLContext ctx;
  88.     
  89.     /* Initialize Macintosh system */
  90.     InitGraf(&qd.thePort);
  91.     InitFonts();
  92.     InitWindows();
  93.  
  94.     /* Create a window */
  95.     SetRect(&rect, 50, 50, 50 + WIDTH, 50 + HEIGHT);
  96.     win = NewCWindow(NULL, &rect, "\pAGL Intro", false,
  97.                                 plainDBox, (WindowPtr) -1L, true, 0L);
  98.     if(win == NULL) return 1;
  99.     
  100.     /* Display the window */
  101.     ShowWindow(win);
  102.  
  103.     /* Setup the OpenGL context */
  104.     ctx = setupAGL((AGLDrawable) win);
  105.     if(!ctx) return 1;
  106.  
  107.     /* Do the OpenGL drawing */
  108.     drawGL();
  109.     
  110.     /* Wait until the mouse button is pressed */
  111.     while(!Button()) {}
  112.     
  113.     /* Cleanup the OpenGL context */
  114.     cleanupAGL(ctx);
  115.  
  116.     /* Cleanup */
  117.     DisposeWindow(win);
  118.  
  119.     return 0;
  120. }