home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MiniGL / demos / bounce.c next >
Encoding:
C/C++ Source or Header  |  2000-04-07  |  6.8 KB  |  390 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <mgl/gl.h>
  6. #include <exec/exec.h>
  7. #include <dos/dos.h>
  8. #if defined(__PPC__) || defined(__VBCC__)
  9. #include <proto/dos.h>
  10. #else
  11. #include <inline/dos.h>
  12. #endif
  13.  
  14. #define COS(X)   cos( (X) * 3.14159/180.0 )
  15. #define SIN(X)   sin( (X) * 3.14159/180.0 )
  16.  
  17. #ifndef __PPC__
  18. extern struct IntuitionBase *IntuitionBase;
  19. extern struct GfxBase *GfxBase;
  20. extern struct DosLibrary *DOSBase;
  21. #endif
  22.  
  23. #define RED 1
  24. #define WHITE 2
  25. #define CYAN 3
  26.  
  27. #define NUMSLICE 6.0
  28.  
  29. GLboolean IndexMode = GL_FALSE;
  30. GLuint Ball;
  31. GLenum Mode;
  32. GLfloat Zrot = 0.0, Zstep = 3.0;
  33. GLfloat Xpos = 0.0, Ypos = 1.0;
  34. GLfloat Xvel = 0.2, Yvel = 0.0;
  35. GLfloat Xmin = -4.0, Xmax = 4.0;
  36. GLfloat Ymin = -2.8, Ymax = 4.0;
  37. GLfloat G = -0.05;
  38. GLfloat rot = 0.0;
  39. GLfloat step = 0.0;
  40. GLboolean do_cage = GL_FALSE;
  41.  
  42. typedef struct
  43. {
  44.     GLint command;
  45.     GLfloat p1, p2, p3;
  46. } GL_Command;
  47.  
  48. #define GLC_VERTEX 1
  49. #define GLC_COLOR  2
  50. #define GLC_BEGIN  3
  51. #define GLC_END    4
  52. #define GLC_FINISH 5
  53.  
  54. GL_Command cmd[150000];
  55. int cmdptr=0;
  56.  
  57. static GLuint make_ball(void)
  58. {
  59.     GLuint list;
  60.     GLfloat a, b;
  61.     GLfloat da = 180.0/NUMSLICE, db = 180.0/NUMSLICE;
  62.     GLfloat radius = 1.0;
  63.     GLuint color;
  64.     GLfloat x, y, z;
  65.  
  66.     cmdptr=0;
  67.  
  68.     color = 0;
  69.  
  70.     for (a = -90.0; a + da <= 90.0; a += da)
  71.     {
  72.  
  73.         cmd[cmdptr++].command = GLC_BEGIN;
  74.  
  75.         for (b = 0.0; b <= 360.0; b += db)
  76.         {
  77.  
  78.             if (color)
  79.             {
  80.                 cmd[cmdptr].command = GLC_COLOR;
  81.                 cmd[cmdptr].p1 = 1.0;
  82.                 cmd[cmdptr].p2 = 0.0;
  83.                 cmd[cmdptr].p3 = 0.0;
  84.                 cmdptr++;
  85.             }
  86.             else
  87.             {
  88.                 cmd[cmdptr].command = GLC_COLOR;
  89.                 cmd[cmdptr].p1 = 1.0;
  90.                 cmd[cmdptr].p2 = 1.0;
  91.                 cmd[cmdptr].p3 = 1.0;
  92.                 cmdptr++;
  93.             }
  94.  
  95.             x = COS(b) * COS(a);
  96.             y = SIN(b) * COS(a);
  97.             z = SIN(a);
  98.             cmd[cmdptr].command = GLC_VERTEX;
  99.             cmd[cmdptr].p1 = x;
  100.             cmd[cmdptr].p2 = y;
  101.             cmd[cmdptr].p3 = z;
  102.             cmdptr++;
  103.  
  104.             x = radius * COS(b) * COS(a + da);
  105.             y = radius * SIN(b) * COS(a + da);
  106.             z = radius * SIN(a + da);
  107.             cmd[cmdptr].command = GLC_VERTEX;
  108.             cmd[cmdptr].p1 = x;
  109.             cmd[cmdptr].p2 = y;
  110.             cmd[cmdptr].p3 = z;
  111.             cmdptr++;
  112.  
  113.             color = 1 - color;
  114.         }
  115.         cmd[cmdptr++].command = GLC_END;
  116.     }
  117.     cmd[cmdptr++].command = GLC_FINISH;
  118.  
  119.     return list;
  120. }
  121.  
  122. static void drawCube(void)
  123. {
  124.     glBegin(GL_QUADS);
  125.         glColor3f(1.0, 0.0, 0.0);
  126.         glVertex3f(-1,-1,-1); glColor3f(0.5, 0.5, 0.5);
  127.         glVertex3f(-1,1,-1); glVertex3f(1,1,-1); glVertex3f(1,-1,-1);
  128.  
  129.         glColor3f(1.0, 0.5, 0.0);
  130.         glVertex3f(-1,1,-1); glColor3f(0.5, 0.5, 0.5);
  131.         glVertex3f(-1,1,1); glVertex3f(1,1,1); glVertex3f(1,1,-1);
  132.  
  133.         glColor3f(0.0, 1.0, 0.0);
  134.         glVertex3f(1,1,-1); glColor3f(0.5, 0.5, 0.5);
  135.         glVertex3f(1,1,1); glVertex3f(1,-1,1); glVertex3f(1,-1,-1);
  136.  
  137.         glColor3f(0.0, 0.0, 1.0);
  138.         glVertex3f(1,1,1); glColor3f(0.5, 0.5, 0.5);
  139.         glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(1,-1,1);
  140.  
  141.         glColor3f(0.0, 0.0, 0.0);
  142.         glVertex3f(-1,1,1); glColor3f(0.5, 0.5, 0.5);
  143.         glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1);
  144.  
  145.         glColor3f(1.0, 1.0, 1.0);
  146.         glVertex3f(-1,-1,1); glColor3f(0.5, 0.5, 0.5);
  147.         glVertex3f(-1,-1,-1);
  148.         glVertex3f(1,-1,-1);
  149.         glVertex3f(1,-1,1);
  150.  
  151.     glEnd();
  152. }
  153.  
  154.  
  155. static void drawBall(void)
  156. {
  157.     int i = 0;
  158.  
  159.     while (cmd[i].command != GLC_FINISH)
  160.     {
  161.         switch(cmd[i].command)
  162.         {
  163.             case GLC_BEGIN:
  164.                 glBegin(GL_QUAD_STRIP);
  165.                 break;
  166.             case GLC_COLOR:
  167.                 glColor3f(cmd[i].p1, cmd[i].p2, cmd[i].p3);
  168.                 break;
  169.             case GLC_VERTEX:
  170.                 glVertex3f(cmd[i].p1, cmd[i].p2, cmd[i].p3);
  171.                 break;
  172.             case GLC_END:
  173.                 glEnd();
  174.                 break;
  175.         }
  176.         i++;
  177.     }
  178. }
  179.  
  180.  
  181. void reshape(int width, int height)
  182. {
  183.     float aspect = (float) width / (float) height;
  184.     glViewport(0, 0, (GLint) width, (GLint) height);
  185.     glMatrixMode(GL_PROJECTION);
  186.     glLoadIdentity();
  187.     glOrtho(-6.0 * aspect, 6.0 * aspect, -6.0, 6.0, -6.0, 6.0);
  188.     glMatrixMode(GL_MODELVIEW);
  189. }
  190.  
  191. static void key(char k)
  192. {
  193.     switch (k)
  194.     {
  195.         case 27:
  196.             mglExit();
  197.             break;
  198.         case 'b':
  199.             do_cage = !do_cage;
  200.             break;
  201.     }
  202. }
  203.  
  204. static void drawCage(void)
  205. {
  206.     GLfloat xmin = Xmin - 2.0,
  207.             xmax = Xmax + 2.0,
  208.             ymin = Ymin - 2.0,
  209.             ymax = Ymax + 2.0,
  210.             zmin = -2.0,
  211.             zmax = 2.0;
  212.     GLfloat color[] = { 1.0, 0.5, 0.0, 0.5 };
  213.  
  214.     glEnable(GL_BLEND);
  215.     glColor4f(0.0, 0.5, 0.5, 0.5);
  216.  
  217.     glBegin(GL_QUADS);
  218.         glVertex2f(xmax, ymax);
  219.         glVertex2f(xmax, ymin);
  220.         glVertex2f(xmin, ymin);
  221.         glVertex2f(xmin, ymax);
  222.     glEnd();
  223.     glDisable(GL_BLEND);
  224. }
  225.  
  226. static void draw(void)
  227. {
  228.     GLfloat xmin = Xmin - 1.0,
  229.             xmax = Xmax + 1.0,
  230.             ymin = Ymin - 1.0,
  231.             ymax = Ymax + 1.0,
  232.             zmin = -2.0,
  233.             zmax = 2.0;
  234.  
  235.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  236.     glRotatef(rot, 0.0, 1.0, 0.0);
  237.  
  238.     if (do_cage == GL_TRUE) drawCage();
  239.  
  240.     glPushMatrix();
  241.     glTranslatef(Xpos, Ypos, 0.0);
  242.     glScalef(2.f, 2.f, 2.f);
  243.     glRotatef(8.0, 0.0, 0.0, 1.0);
  244.     glRotatef(90.0, 1.0, 0.0, 0.0);
  245.     glRotatef(Zrot, 0.0, 0.0, 1.0);
  246.  
  247.     drawBall();
  248.  
  249.  
  250.     glPopMatrix();
  251.  
  252.     glFrontFace(GL_CCW);
  253.     if (do_cage == GL_TRUE) drawCage();
  254.     glFrontFace(GL_CW);
  255.  
  256.     glFlush();
  257.     mglUnlockDisplay();
  258.     mglSwitchDisplay();
  259. }
  260.  
  261. static void idle(void)
  262. {
  263.     static float vel0 = -100.0;
  264.  
  265.     step += rot;
  266.     step = fmod(step,180.0);
  267.     Zrot += fabs(Zstep);
  268.  
  269.     Xpos += Xvel;
  270.     if (Xpos >= Xmax)
  271.     {
  272.         Xpos = Xmax;
  273.         Xvel = -Xvel;
  274.         Zstep = -Zstep;
  275.     }
  276.     if (Xpos <= Xmin)
  277.     {
  278.         Xpos = Xmin;
  279.         Xvel = -Xvel;
  280.         Zstep = -Zstep;
  281.     }
  282.     Ypos += Yvel;
  283.     Yvel += G;
  284.     if (Ypos < Ymin)
  285.     {
  286.         Ypos = Ymin;
  287.         if (vel0 == -100.0)
  288.         {
  289.             vel0 = fabs(Yvel);
  290.         }
  291.         Yvel = vel0;
  292.     }
  293.     draw();
  294. }
  295.  
  296. int main(int argc, char *argv[])
  297. {
  298.     GLint width=320; GLint height=240;
  299.     int i;
  300.     GLboolean culling = GL_TRUE;
  301.     ULONG ModeID = INVALID_ID;
  302.  
  303.     for (i=1; i<argc; i++)
  304.     {
  305.         if (0 == stricmp(argv[i], "-modeid"))
  306.         {
  307.             i++;
  308.             ModeID = atoi(argv[i]);
  309.         }
  310.         if (0 == stricmp(argv[i], "-width"))
  311.         {
  312.             i++;
  313.             width = atoi(argv[i]);
  314.         }
  315.         if (0 == stricmp(argv[i], "-height"))
  316.         {
  317.             i++;
  318.             height = atoi(argv[i]);
  319.         }
  320.         if (0 == stricmp(argv[i],"-xmin"))
  321.         {
  322.             i++;
  323.             Xmin = atof(argv[i]);
  324.         }
  325.         if (0 == stricmp(argv[i],"-xmax"))
  326.         {
  327.             i++;
  328.             Xmax = atof(argv[i]);
  329.         }
  330.         if (0 == stricmp(argv[i],"-rot"))
  331.         {
  332.             i++;
  333.             rot = atof(argv[i]);
  334.         }
  335.         if (0 == stricmp(argv[i],"-nocull"))
  336.         {
  337.             culling = GL_FALSE;
  338.         }
  339.         if (0 == stricmp(argv[i],"-noback"))
  340.         {
  341.             do_cage = GL_FALSE;
  342.         }
  343.         if (0 == stricmp(argv[i],"-window"))
  344.         {
  345.             mglChooseWindowMode(GL_TRUE);
  346.         }
  347.     }
  348.  
  349.     mglChooseVertexBufferSize(1000);
  350.     mglChooseNumberOfBuffers(3);
  351.     MGLInit();
  352.     if (ModeID == INVALID_ID)
  353.     {
  354.         mglCreateContext(0,0,width,height);
  355.     }
  356.     else
  357.     {
  358.         mglCreateContextFromID(ModeID, &width, &height);
  359.         printf("Screen mode is %d×%d\n", width, height);
  360.     }
  361.  
  362.  
  363.     mglEnableSync(GL_FALSE);
  364.  
  365.     glClearColor(0.0, 0.0, 0.0, 1.0);
  366.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  367.     Ball = make_ball();
  368.     glCullFace(GL_BACK);
  369.     if (culling == GL_TRUE)
  370.     {
  371.         glEnable(GL_CULL_FACE);
  372.     }
  373.     else
  374.     {
  375.         glDisable(GL_CULL_FACE);
  376.     }
  377.  
  378.     glDisable(GL_DEPTH_TEST);
  379.     glShadeModel(GL_FLAT);
  380.  
  381.     reshape(width,height);
  382.     mglLockMode(MGL_LOCK_SMART);
  383.     mglIdleFunc(idle);
  384.     mglKeyFunc(key);
  385.     mglMainLoop();
  386.     mglDeleteContext();
  387.     MGLTerm();
  388.     return 0;             /* ANSI C requires main to return int. */
  389. }
  390.