home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / vopengl / bounce / bnccnv.cpp < prev    next >
C/C++ Source or Header  |  1998-06-22  |  6KB  |  269 lines

  1. //=======================================================================
  2. //@V@:Note: This file generated by vgen V1.04 (10:59:00 22 Jun 1998).
  3. //    bnccnv.cpp:    Source for bncOGLCanvasPane class
  4. //=======================================================================
  5. /*
  6.  * Bouncing ball demo.
  7.  *
  8.  * This program is in the public domain
  9.  *
  10.  * Brian Paul
  11.  */
  12. // Conversion to V by Bruce E. Wampler
  13.  
  14. #include "bnccnv.h"
  15. #include <math.h>
  16. #include <stdlib.h>
  17. #define COS(X)   cos( (X) * 3.14159/180.0 )
  18. #define SIN(X)   sin( (X) * 3.14159/180.0 )
  19.  
  20. #define RED 1
  21. #define WHITE 2
  22. #define CYAN 3
  23.  
  24. GLuint Ball;
  25. GLenum Mode;
  26. GLfloat Zrot = 0.0, Zstep = 6.0;
  27. GLfloat Xpos = 0.0, Ypos = 1.0;
  28. GLfloat Xvel = 0.2, Yvel = 0.0;
  29. GLfloat Xmin = -4.0, Xmax = 4.0;
  30. GLfloat Ymin = -3.8, Ymax = 4.0;
  31. GLfloat G = -0.1;
  32.  
  33. static GLuint 
  34. make_ball(void)
  35. {
  36.   GLuint list;
  37.   GLfloat a, b;
  38.   GLfloat da = 18.0, db = 18.0;
  39.   GLfloat radius = 1.0;
  40.   GLuint color;
  41.   GLfloat x, y, z;
  42.  
  43.   list = glGenLists(1);
  44.  
  45.   glNewList(list, GL_COMPILE);
  46.  
  47.   color = 0;
  48.   for (a = -90.0; a + da <= 90.0; a += da) {
  49.  
  50.     glBegin(GL_QUAD_STRIP);
  51.     for (b = 0.0; b <= 360.0; b += db) {
  52.  
  53.       if (color) {
  54.     glIndexi(RED);
  55.       } else {
  56.     glIndexi(WHITE);
  57.       }
  58.  
  59.       x = COS(b) * COS(a);
  60.       y = SIN(b) * COS(a);
  61.       z = SIN(a);
  62.       glVertex3f(x, y, z);
  63.  
  64.       x = radius * COS(b) * COS(a + da);
  65.       y = radius * SIN(b) * COS(a + da);
  66.       z = radius * SIN(a + da);
  67.       glVertex3f(x, y, z);
  68.  
  69.       color = 1 - color;
  70.     }
  71.     glEnd();
  72.  
  73.   }
  74.  
  75.   glEndList();
  76.  
  77.   return list;
  78. }
  79.  
  80. static void 
  81. reshape(int width, int height)
  82. {
  83.   glViewport(0, 0, (GLint) width, (GLint) height);
  84.   glMatrixMode(GL_PROJECTION);
  85.   glLoadIdentity();
  86.   glOrtho(-6.0, 6.0, -6.0, 6.0, -6.0, 6.0);
  87.   glMatrixMode(GL_MODELVIEW);
  88. }
  89.  
  90. static void 
  91. draw(void)
  92. {
  93.   GLint i;
  94.  
  95.   glClear(GL_COLOR_BUFFER_BIT);
  96.  
  97.   glIndexi(CYAN);
  98.   glBegin(GL_LINES);
  99.   for (i = -5; i <= 5; i++) {
  100.     glVertex2i(i, -5);
  101.     glVertex2i(i, 5);
  102.   }
  103.   for (i = -5; i <= 5; i++) {
  104.     glVertex2i(-5, i);
  105.     glVertex2i(5, i);
  106.   }
  107.   for (i = -5; i <= 5; i++) {
  108.     glVertex2i(i, -5);
  109.     glVertex2f(i * 1.15, -5.9);
  110.   }
  111.   glVertex2f(-5.3, -5.35);
  112.   glVertex2f(5.3, -5.35);
  113.   glVertex2f(-5.75, -5.9);
  114.   glVertex2f(5.75, -5.9);
  115.   glEnd();
  116.  
  117.   glPushMatrix();
  118.   glTranslatef(Xpos, Ypos, 0.0);
  119.   glScalef(2.0, 2.0, 2.0);
  120.   glRotatef(8.0, 0.0, 0.0, 1.0);
  121.   glRotatef(90.0, 1.0, 0.0, 0.0);
  122.   glRotatef(Zrot, 0.0, 0.0, 1.0);
  123.  
  124.   glCallList(Ball);
  125.  
  126.   glPopMatrix();
  127.  
  128.   glFlush();
  129. }
  130.  
  131. static void 
  132. idle(void)
  133. {
  134.   static float vel0 = -100.0;
  135.  
  136.   Zrot += Zstep;
  137.  
  138.   Xpos += Xvel;
  139.   if (Xpos >= Xmax) {
  140.     Xpos = Xmax;
  141.     Xvel = -Xvel;
  142.     Zstep = -Zstep;
  143.   }
  144.   if (Xpos <= Xmin) {
  145.     Xpos = Xmin;
  146.     Xvel = -Xvel;
  147.     Zstep = -Zstep;
  148.   }
  149.   Ypos += Yvel;
  150.   Yvel += G;
  151.   if (Ypos < Ymin) {
  152.     Ypos = Ymin;
  153.     if (vel0 == -100.0)
  154.       vel0 = fabs(Yvel);
  155.     Yvel = vel0;
  156.   }
  157. }
  158.  
  159.  
  160. //===================>>> bncOGLCanvasPane::bncOGLCanvasPane <<<====================
  161.   bncOGLCanvasPane::bncOGLCanvasPane(unsigned int vGLmode, PaneType pt)
  162.   {
  163.     initDone = 0;
  164.   }
  165.  
  166. //===================>>> bncOGLCanvasPane::~bncOGLCanvasPane <<<====================
  167.   bncOGLCanvasPane::~bncOGLCanvasPane()
  168.   {
  169.   }
  170.  
  171. //======================>>> bncOGLCanvasPane::TimerAnimate <<<========================
  172.   void bncOGLCanvasPane::TimerAnimate(void)
  173.   {
  174.     // **** Called by CmdWindow AuxTimer for animation.
  175.     idle();
  176.     vglMakeCurrent();  // Typically done here
  177.     draw();
  178.     vglFlush();  // After you draw, typically flush
  179.     
  180.  
  181.   }
  182. //======================>>> bncOGLCanvasPane::graphicsInit <<<========================
  183.   void bncOGLCanvasPane::graphicsInit(void)
  184.   {
  185.     vBaseGLCanvasPane::graphicsInit();    // Always call the superclass first!
  186.  
  187.     // **** Your OpenGL initialization code goes here!
  188.   Ball = make_ball();
  189.   glCullFace(GL_BACK);
  190.   glEnable(GL_CULL_FACE);
  191.   glDisable(GL_DITHER);
  192.   glShadeModel(GL_FLAT);
  193.   reshape(300,300);
  194.  
  195.  
  196.     initDone = 1;
  197.   }
  198. //======================>>> bncOGLCanvasPane::HPage <<<========================
  199.   void bncOGLCanvasPane::HPage(int shown, int top)
  200.   {
  201.     vBaseGLCanvasPane::HPage(shown, top);
  202.   }
  203.  
  204. //======================>>> bncOGLCanvasPane::VPage <<<========================
  205.   void bncOGLCanvasPane::VPage(int shown, int top)
  206.   {
  207.     vBaseGLCanvasPane::VPage(shown, top);
  208.   }
  209.  
  210. //=======================>>> bncOGLCanvasPane::HScroll <<<======================
  211.   void bncOGLCanvasPane::HScroll(int step)
  212.   {
  213.     vBaseGLCanvasPane::HScroll(step);
  214.   }
  215.  
  216. //======================>>> bncOGLCanvasPane::VScroll <<<======================
  217.   void bncOGLCanvasPane::VScroll(int step)
  218.   {
  219.     vBaseGLCanvasPane::VScroll(step);
  220.   }
  221.  
  222. //======================>>> bncOGLCanvasPane::MouseDown <<<======================
  223.   void bncOGLCanvasPane::MouseDown(int X, int Y, int button)
  224.   {
  225.     vBaseGLCanvasPane::MouseDown(X,Y,button);
  226.   }
  227.  
  228. //========================>>> bncOGLCanvasPane::MouseUp <<<======================
  229.   void bncOGLCanvasPane::MouseUp(int X, int Y, int button)
  230.   {
  231.     vBaseGLCanvasPane::MouseUp(X,Y,button);
  232.   }
  233.  
  234. //======================>>> bncCanvasPane::MouseMove <<<======================
  235.   void bncOGLCanvasPane::MouseMove(int x, int y, int button)
  236.   {
  237.     vBaseGLCanvasPane::MouseMove(x,y,button);
  238.   }
  239.  
  240. //=========================>>> bncOGLCanvasPane::Redraw <<<======================
  241.   void bncOGLCanvasPane::Redraw(int x, int y, int w, int h)
  242.   {
  243.     static int inRedraw = 0;
  244.  
  245.     if (inRedraw || !initDone)  // Don't draw until initialized
  246.         return;
  247.  
  248.     inRedraw = 1;  // Don't allow recursive redraws.
  249.  
  250.     vglMakeCurrent();  // Typically done here
  251.  
  252.     // *** Your drawing code typically goes here. You may
  253.     // insert it here, or just call a drawing routine.
  254.     draw();
  255.  
  256.     vglFlush();  // After you draw, typically flush
  257.  
  258.     inRedraw = 0;  // Out of Redraw
  259.  
  260.   }
  261.  
  262. //======================>>> bncOGLCanvasPane::Resize <<<======================
  263.   void bncOGLCanvasPane::Resize(int w, int h)
  264.   {
  265.     vBaseGLCanvasPane::Resize(w,h);
  266.     reshape(w,h);
  267.   }
  268.  
  269.