home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MiniGL / src / GLTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-11  |  23.1 KB  |  909 lines

  1. /*
  2.  * $Id: GLTest.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $
  3.  *
  4.  * $Date: 2000/04/07 19:44:51 $
  5.  * $Revision: 1.1.1.1 $
  6.  *
  7.  * (C) 1999 by Hyperion
  8.  * All rights reserved
  9.  *
  10.  * This file is part of the MiniGL library project
  11.  * See the file Licence.txt for more details
  12.  *
  13.  */
  14.  
  15. #include "mgl/gl.h"
  16. #include <math.h>
  17. #include <exec/exec.h>
  18. #include <intuition/intuition.h>
  19. #include <graphics/gfx.h>
  20. #include <libraries/lowlevel.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <dos/dos.h>
  24. #ifdef __GNUC__
  25. #ifdef __PPC__
  26. #include <proto/exec.h>
  27. #include <proto/intuition.h>
  28. #include <proto/lowlevel.h>
  29. #include <proto/dos.h>
  30. #include <proto/graphics.h>
  31. #else
  32. #include <inline/exec.h>
  33. #include <inline/intuition.h>
  34. #include <inline/lowlevel.h>
  35. #include <inline/dos.h>
  36. #include <inline/graphics.h>
  37. #endif
  38. #endif
  39.  
  40. #ifdef __STORMC__
  41. #include <string.h>
  42. #include <clib/powerpc_protos.h>
  43. #ifndef __PPC__
  44. #include <proto/exec.h>
  45. #include <proto/intuition.h>
  46. #include <proto/lowlevel.h>
  47. #include <proto/dos.h>
  48. #include <proto/graphics.h>
  49. #else
  50. #include <clib/exec_protos.h>
  51. #include <clib/intuition_protos.h>
  52. #include <clib/lowlevel_protos.h>
  53. #include <clib/dos_protos.h>
  54. #include <clib/graphics_protos.h>
  55. #endif
  56. #define inline __inline
  57. #define M_PI 3.1415927
  58. int kprintf(char *format, ...)
  59. {
  60.     return 0;
  61. }
  62. #endif
  63.  
  64. #ifdef __VBCC__
  65. #include <proto/exec.h>
  66. include <proto/intuition.h>
  67. #include <proto/lowlevel.h>
  68. #include <proto/dos.h>
  69. #include <proto/graphics.h>
  70. #define inline
  71. #define   M_PI 3.14159265358979323846
  72. #endif
  73.  
  74. static char rcsid[] = "$Id: GLTest.c,v 1.1.1.1 2000/04/07 19:44:51 hfrieden Exp $";
  75. struct Library *LowLevelBase;
  76.  
  77. #ifndef __PPC__
  78. extern struct IntuitionBase *IntuitionBase;
  79. extern struct GfxBase *GfxBase;
  80. extern struct Library *UtilityBase;
  81. extern struct DosLibrary *DOSBase;
  82. extern struct ExecBase *SysBase;
  83. #endif
  84.  
  85. extern int kprintf(char *format, ...);
  86. #define DEBUG(x) kprintf x
  87.  
  88. extern void GLPrintMatrix(int);
  89.  
  90. static struct EClockVal eval;
  91. static float fps;
  92. static GLfloat fog_start, fog_end;
  93. static GLboolean fogon = GL_FALSE;
  94. static GLboolean sync = GL_TRUE;
  95.  
  96. typedef struct
  97. {
  98.     GLfloat x,y,z,u,v;
  99. } MyVertex;
  100.  
  101. static MyVertex vertices1 [] =
  102. {
  103.     {-1, -1, -1, 0.5, 0.5}, // 0
  104.     {-1, -1,  1, 0.5, 0.0}, // 1
  105.     {-1,  1,  1, 0.0, 0.0}, // 2
  106.     {-1,  1, -1, 0.0, 0.5}, // 3
  107.     { 1,  1, -1, 0.0, 1.0}, // 4
  108.     { 1, -1, -1, 0.5, 1.0}, // 5
  109.     { 1, -1,  1, 1.0, 1.0}, // 6
  110.     {-1, -1,  1, 1.0, 0.5}  // 7
  111. };
  112.  
  113. static MyVertex vertices2 [] =
  114. {
  115.     { 1,  1,  1, 0.5, 0.5}, // 0
  116.     { 1, -1,  1, 0.5, 0.0}, // 1
  117.     { 1, -1, -1, 0.0, 0.0}, // 2
  118.     { 1,  1, -1, 0.0, 0.5}, // 3
  119.     {-1,  1, -1, 0.0, 1.0}, // 4
  120.     {-1,  1,  1, 0.5, 1.0}, // 5
  121.     {-1, -1,  1, 1.0, 1.0}, // 6
  122.     { 1, -1,  1, 1.0, 0.5}  // 7
  123. };
  124.  
  125. struct ResInfo
  126. {
  127.     int width, height;
  128.     char *name;
  129. };
  130.  
  131. struct ResInfo Resolutions [] =
  132. {
  133.     {320, 240, "320 x 240"},
  134.     {400, 300, "400 x 300"},
  135.     {640, 480, "640 x 480"},
  136.     {800, 600, "800 x 600"}, // Out of memory...
  137.     {1024, 768, "1024 x 768"},
  138.     {-1, -1, NULL}
  139. };
  140.  
  141. GLint ResPtr;
  142. char *CurrentRes;
  143. GLboolean ShowRes = GL_TRUE;
  144. GLboolean zbuffer = GL_TRUE;
  145.  
  146. GLfloat mouse_x = 0.0, mouse_y = 0.0, mouse_z = 0.0;
  147. GLint offset = 0;
  148. GLfloat fov = 70.0;
  149. GLfloat inf_w = 0.1;
  150. GLfloat zback = 1000.0;
  151. GLfloat alpha = 1.0;
  152.  
  153. GLubyte index_texture[] =
  154. {
  155.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  156.     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  157.     1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,
  158.     1,0,1,2,2,2,2,2,2,2,2,2,2,1,0,1,
  159.     1,0,1,2,1,1,1,1,1,1,1,1,2,1,0,1,
  160.     1,0,1,2,1,3,3,3,3,3,3,1,2,1,0,1,
  161.     1,0,1,2,1,3,1,1,1,1,3,1,2,1,0,1,
  162.     1,0,1,2,1,3,1,4,4,1,3,1,2,1,0,1,
  163.     1,0,1,2,1,3,1,4,4,1,3,1,2,1,0,1,
  164.     1,0,1,2,1,3,1,1,1,1,3,1,2,1,0,1,
  165.     1,0,1,2,1,3,3,3,3,3,3,1,2,1,0,1,
  166.     1,0,1,2,1,1,1,1,1,1,1,1,2,1,0,1,
  167.     1,0,1,2,2,2,2,2,2,2,2,2,2,1,0,1,
  168.     1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,
  169.     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  170.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  171. };
  172.  
  173. GLubyte palette[] =
  174. {
  175.     0x00, 0x00, 0x00,
  176.     0xFF, 0x00, 0x00,
  177.     0x00, 0xFF, 0x00,
  178.     0x00, 0x00, 0xff,
  179.     0xFF, 0xFF, 0xFF,
  180. };
  181.  
  182. static void ReplaceTexture(void)
  183. {
  184.     GLenum error;
  185.     int i=1;
  186.  
  187.     glDeleteTextures(1, (const unsigned int *)&i);
  188.     glBindTexture(GL_TEXTURE_2D, 1);
  189.     glColorTable(GL_COLOR_TABLE, GL_RGB, 5, GL_RGB, GL_UNSIGNED_BYTE, palette);
  190.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, index_texture);
  191.     error = glGetError();
  192.     kprintf("Error = %d\n", error);
  193. }
  194.  
  195. static void MakeRot(GLfloat angle1, GLfloat angle2)
  196. {
  197.     GLfloat sinel = (GLfloat)sin((double)angle1/180*M_PI);
  198.     GLfloat cosel = (GLfloat)cos((double)angle1/180*M_PI);
  199.     GLfloat sinaz = (GLfloat)sin((double)angle2/180*M_PI);
  200.     GLfloat cosaz = (GLfloat)cos((double)angle2/180*M_PI);
  201.     GLfloat mat[16];
  202.  
  203.     mat[0] = cosaz;        mat[4] = 0.0;    mat[8] = -sinaz;       mat[12] = 0;
  204.     mat[1] = -sinel*sinaz; mat[5] = cosel;  mat[9] = -sinel*cosaz; mat[13] = 0.0;
  205.     mat[2] = cosel*sinaz;  mat[6] = sinel;  mat[10] = cosel*cosaz; mat[14] = 0.0;
  206.     mat[3] =               mat[7] =         mat[11] = 0.0;         mat[15] = 1.0;
  207.     glMultMatrixf(mat);
  208. }
  209.  
  210. static void UpRes(void)
  211. {
  212.     ResPtr++; if (Resolutions[ResPtr].width == -1) ResPtr = 0;
  213.     mglResizeContext(Resolutions[ResPtr].width, Resolutions[ResPtr].height);
  214.     CurrentRes = Resolutions[ResPtr].name;
  215.     ShowRes = GL_TRUE;
  216. }
  217.  
  218. void PrExit(void)
  219. {
  220.     if (LowLevelBase)   CloseLibrary(LowLevelBase);
  221.     exit(0L);
  222. }
  223.  
  224. void PrInit(void)
  225. {
  226.     LowLevelBase  = OpenLibrary("lowlevel.library", 40L);
  227.     if (!LowLevelBase) PrExit();
  228. }
  229.  
  230. GLdouble angle = 0.0;
  231. GLfloat mouse_angle_x = 0.0;
  232. GLfloat mouse_angle_y = 0.0;
  233. GLfloat tlow = 0.0;
  234. GLfloat offx = 0.f, offy = 0.f;
  235. GLdouble zclear = 1.0;
  236. GLenum primitive = GL_POLYGON;
  237.  
  238. /*
  239. ** Load a PPM file into memory.
  240. ** The resulting pointer can be free()'d
  241. */
  242. GLubyte *LoadPPM(char *name, GLint *w, GLint *h)
  243. {
  244.     int i;
  245.     unsigned long x,y;
  246.     FILE *f;
  247.     GLubyte *where;
  248.  
  249.     f = fopen(name, "r");
  250.  
  251.     if (!f)
  252.     {
  253.         *w = 0; *h=0;
  254.         return NULL;
  255.     }
  256.     #ifndef __STORM__
  257.     i = fscanf(f, "P6\n%ld %ld\n255\n",&x, &y);
  258.     #else
  259.     i = fscanf(f, "P6\n%ld\n%ld\n255\n", &x, &y);
  260.     #endif
  261.  
  262.     if (i!= 2)
  263.     {
  264.         printf("Error scanning PPM header\n");
  265.         fclose(f);
  266.         *w = 0; *h = 0;
  267.         return NULL;
  268.     }
  269.  
  270.     *w = x;
  271.     *h = y;
  272.  
  273.     where = malloc(x*y*3);
  274.     if (!where)
  275.     {
  276.         printf("Error out of Memory\n");
  277.         fclose(f);
  278.         *w = 0; *h = 0;
  279.         return NULL;
  280.     }
  281.  
  282.     i = fread(where, 1, x*y*3, f);
  283.     fclose(f);
  284.  
  285.     if (i != x*y*3)
  286.     {
  287.         printf("Error while reading file\n");
  288.         free(where);
  289.         *w = 0; *h = 0;
  290.         return NULL;
  291.     }
  292.  
  293.     return where;
  294. }
  295.  
  296. void TexInit(void)
  297. {
  298.     GLubyte *tmap;
  299.     GLint x,y;
  300.  
  301.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  302.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  303.  
  304.     tmap = LoadPPM("data/t1.ppm",&x, &y);
  305.     glBindTexture(GL_TEXTURE_2D, 1);
  306.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  307.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  308.     free(tmap);
  309.  
  310.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  311.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  312.  
  313.     tmap = LoadPPM("data/t2.ppm",&x, &y);
  314.     glBindTexture(GL_TEXTURE_2D, 2);
  315.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  316.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  317.     free(tmap);
  318.  
  319.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  320.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  321.  
  322.     tmap = LoadPPM("data/stars.ppm",&x, &y);
  323.     glBindTexture(GL_TEXTURE_2D, 3);
  324.     glTexImage2D(GL_TEXTURE_2D, 0, 3,
  325.         x,y, 0, GL_RGB, GL_UNSIGNED_BYTE, tmap);
  326.     free(tmap);
  327.  
  328.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  329.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  330.  
  331. }
  332.  
  333. static void drawCubeFan(GLint tex1, GLint tex2)
  334. {
  335.     GLfloat w = 1.0;
  336.  
  337.     if (tex1 == tex2) w = inf_w;
  338.  
  339.     glBindTexture(GL_TEXTURE_2D, tex1);
  340.     glBegin(GL_TRIANGLE_FAN);
  341.         //glColor3f(1.0, 0.0, 0.0);
  342.         glTexCoord2f(vertices1[0].u, vertices1[0].v);
  343.         glVertex4f(vertices1[0].x, vertices1[0].y, vertices1[0].z,w);
  344.         if (tex1 != tex2) glColor4f(0.7, 0.7, 0.7, alpha);
  345.         glTexCoord2f(vertices1[1].u, vertices1[1].v);
  346.         glVertex4f(vertices1[1].x, vertices1[1].y, vertices1[1].z,w);
  347.         glTexCoord2f(vertices1[2].u, vertices1[2].v);
  348.         glVertex4f(vertices1[2].x, vertices1[2].y, vertices1[2].z,w);
  349.         glTexCoord2f(vertices1[3].u, vertices1[3].v);
  350.         glVertex4f(vertices1[3].x, vertices1[3].y, vertices1[3].z,w);
  351.         glTexCoord2f(vertices1[4].u, vertices1[4].v);
  352.         glVertex4f(vertices1[4].x, vertices1[4].y, vertices1[4].z,w);
  353.         glTexCoord2f(vertices1[5].u, vertices1[5].v);
  354.         glVertex4f(vertices1[5].x, vertices1[5].y, vertices1[5].z,w);
  355.         glTexCoord2f(vertices1[6].u, vertices1[6].v);
  356.         glVertex4f(vertices1[6].x, vertices1[6].y, vertices1[6].z,w);
  357.         glTexCoord2f(vertices1[7].u, vertices1[7].v);
  358.         glVertex4f(vertices1[7].x, vertices1[7].y, vertices1[7].z,w);
  359.     glEnd();
  360.  
  361.     glBindTexture(GL_TEXTURE_2D, tex2);
  362.     glBegin(GL_TRIANGLE_FAN);
  363.         if (tex1 != tex2) glColor4f(0.0, 1.0, 0.0, alpha);
  364.         glTexCoord2f(vertices2[0].u, vertices2[0].v);
  365.         glVertex4f(vertices2[0].x, vertices2[0].y, vertices2[0].z,w);
  366.         if (tex1 != tex2) glColor4f(0.7, 0.7, 0.7, alpha);
  367.         glTexCoord2f(vertices2[1].u, vertices2[1].v);
  368.         glVertex4f(vertices2[1].x, vertices2[1].y, vertices2[1].z,w);
  369.         glTexCoord2f(vertices2[2].u, vertices2[2].v);
  370.         glVertex4f(vertices2[2].x, vertices2[2].y, vertices2[2].z,w);
  371.         glTexCoord2f(vertices2[3].u, vertices2[3].v);
  372.         glVertex4f(vertices2[3].x, vertices2[3].y, vertices2[3].z,w);
  373.         glTexCoord2f(vertices2[4].u, vertices2[4].v);
  374.         glVertex4f(vertices2[4].x, vertices2[4].y, vertices2[4].z,w);
  375.         glTexCoord2f(vertices2[5].u, vertices2[5].v);
  376.         glVertex4f(vertices2[5].x, vertices2[5].y, vertices2[5].z,w);
  377.         glTexCoord2f(vertices2[6].u, vertices2[6].v);
  378.         glVertex4f(vertices2[6].x, vertices2[6].y, vertices2[6].z,w);
  379.         glTexCoord2f(vertices2[7].u, vertices2[7].v);
  380.         glVertex4f(vertices2[7].x, vertices2[7].y, vertices2[7].z,w);
  381.     glEnd();
  382. }
  383.  
  384. #ifndef __STORM__
  385. static
  386. #endif
  387. inline void myVertex(int i)
  388. {
  389.     glTexCoord2f(vertices1[i].u, vertices1[i].v);
  390.     glVertex3f(vertices1[i].x, vertices1[i].y, vertices1[i].z);
  391. }
  392.  
  393. #ifndef __STORM__
  394. static
  395. #endif
  396. inline void myVertex2(int i)
  397. {
  398.     glTexCoord2f(vertices2[i].u, vertices2[i].v);
  399.     glVertex3f(vertices2[i].x, vertices1[i].y, vertices2[i].z);
  400. }
  401.  
  402. static void drawCubeQuad(GLint tex1, GLint tex2)
  403. {
  404.     glDisable(GL_TEXTURE_2D);
  405.     glBegin(GL_QUADS);
  406.         glColor3f(1.0, 0.0, 0.0);
  407.         glVertex3f(-1,-1,-1); glColor3f(0.5, 0.5, 0.5);
  408.         glVertex3f(-1,1,-1); glVertex3f(1,1,-1); glVertex3f(1,-1,-1);
  409.  
  410.         glColor3f(1.0, 0.5, 0.0);
  411.         glVertex3f(-1,1,-1); glColor3f(0.5, 0.5, 0.5);
  412.         glVertex3f(-1,1,1); glVertex3f(1,1,1); glVertex3f(1,1,-1);
  413.  
  414.         glColor3f(0.0, 1.0, 0.0);
  415.         glVertex3f(1,1,-1); glColor3f(0.5, 0.5, 0.5);
  416.         glVertex3f(1,1,1); glVertex3f(1,-1,1); glVertex3f(1,-1,-1);
  417.  
  418.         glColor3f(0.0, 0.0, 1.0);
  419.         glVertex3f(1,1,1); glColor3f(0.5, 0.5, 0.5);
  420.         glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(1,-1,1);
  421.  
  422.         glColor3f(0.0, 0.0, 0.0);
  423.         glVertex3f(-1,1,1); glColor3f(0.5, 0.5, 0.5);
  424.         glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1);
  425.  
  426.         glColor3f(1.0, 1.0, 1.0);
  427.         glVertex3f(-1,-1,1); glColor3f(0.5, 0.5, 0.5);
  428.         glVertex3f(-1,-1,-1);
  429.         glVertex3f(1,-1,-1);
  430.         glVertex3f(1,-1,1);
  431.  
  432.     glEnd();
  433.     glEnable(GL_TEXTURE_2D);
  434. }
  435.  
  436. static void drawCubePoly(GLint tex1, GLint tex2)
  437. {
  438.     glDisable(GL_TEXTURE_2D);
  439.     glBegin(GL_POLYGON);
  440.         glColor3f(1.0, 0.0, 0.0);
  441.         glVertex3f(-1,-1,-1); glColor3f(0.5, 0.5, 0.5);
  442.         glVertex3f(-1,1,-1); glVertex3f(1,1,-1); glVertex3f(1,-1,-1);
  443.     glEnd();
  444.     glBegin(GL_POLYGON);
  445.         glColor3f(1.0, 0.5, 0.0);
  446.         glVertex3f(-1,1,-1); glColor3f(0.5, 0.5, 0.5);
  447.         glVertex3f(-1,1,1); glVertex3f(1,1,1); glVertex3f(1,1,-1);
  448.     glEnd();
  449.     glBegin(GL_POLYGON);
  450.         glColor3f(0.0, 1.0, 0.0);
  451.         glVertex3f(1,1,-1); glColor3f(0.5, 0.5, 0.5);
  452.         glVertex3f(1,1,1); glVertex3f(1,-1,1); glVertex3f(1,-1,-1);
  453.     glEnd();
  454.     glBegin(GL_POLYGON);
  455.         glColor3f(0.0, 0.0, 1.0);
  456.         glVertex3f(1,1,1); glColor3f(0.5, 0.5, 0.5);
  457.         glVertex3f(-1,1,1); glVertex3f(-1,-1,1); glVertex3f(1,-1,1);
  458.     glEnd();
  459.     glBegin(GL_POLYGON);
  460.         glColor3f(0.0, 0.0, 0.0);
  461.         glVertex3f(-1,1,1); glColor3f(0.5, 0.5, 0.5);
  462.         glVertex3f(-1,1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,1);
  463.     glEnd();
  464.     glBegin(GL_POLYGON);
  465.         glColor3f(1.0, 1.0, 1.0);
  466.         glVertex3f(-1,-1,1); glColor3f(0.5, 0.5, 0.5);
  467.         glVertex3f(-1,-1,-1);
  468.         glVertex3f(1,-1,-1);
  469.         glVertex3f(1,-1,1);
  470.     glEnd();
  471.     glEnable(GL_TEXTURE_2D);
  472. }
  473.  
  474.  
  475. typedef void (*drawFunc)(GLint, GLint);
  476.  
  477. drawFunc funcs[] =
  478. {
  479.     drawCubeFan,
  480.     drawCubeQuad,
  481.     drawCubePoly,
  482.     NULL,
  483. };
  484.  
  485. static void (*drawCube)(GLint, GLint) = drawCubeFan;
  486.  
  487. void Rot1(void)
  488. {
  489.     glLoadIdentity();
  490.     glTranslatef(0.f, 0.f, -8.f);
  491.     glRotatef(angle, 0.f, 1.f, 1.f);
  492. }
  493.  
  494. void Rot2(void)
  495. {
  496.     glLoadIdentity();
  497.     glTranslatef(1.0, 3.0, -8.f);
  498.     glRotatef(-angle, 0.f, 1.f, 1.f);
  499.     glScalef(0.8, 2.0, 1.2);
  500. }
  501.  
  502. void Rot3(void)
  503. {
  504.     glLoadIdentity();
  505.     glTranslatef(-3.0, 2.0, -7.0);
  506.     glRotatef(-90, 1,0,0);
  507.     glRotatef(90, 0,0,1);
  508.     glRotatef(angle, 1.0, 0.0, 0.0);
  509.     glRotatef(-angle, 0.0, 1.0, 0.0);
  510.     glRotatef(angle/2.0, 0.0, 0.0, 1.0);
  511.     glRotatef(-angle, 1.0, 1.0, 1.0);
  512. }
  513.  
  514. void Rot4(void)
  515. {
  516.     glLoadIdentity();
  517.     glTranslatef(3.0, 3.0, -10.0);
  518.     glRotatef(2.0*angle, 0.0, 0.0, 1.0);
  519. }
  520.  
  521. void Rot5(void)
  522. {
  523.     glScalef(20.0, 20.0, 20.0);
  524. }
  525.  
  526.  
  527. static
  528. GLboolean draw(void)
  529. {
  530.     static int framecount = 0;
  531.     ULONG fracsecs;
  532.     static  char buffer[256];
  533.     struct Window *win = mglGetWindowHandle();
  534.  
  535.  
  536.     #ifndef NODRAW
  537.     if (GL_FALSE == mglLockDisplay())
  538.     {
  539.         printf("Unable to lock drawing area\n");
  540.         return GL_FALSE;
  541.     }
  542.  
  543.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  544.     #endif
  545.  
  546.  
  547.     if (alpha != 1.0)
  548.     {
  549.         glEnable(GL_BLEND);
  550.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  551.     }
  552.  
  553.     glEnable(GL_TEXTURE_2D);
  554.  
  555.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  556.     glLoadIdentity();
  557.     //MakeRot(mouse_angle_x, mouse_angle_y);
  558.     MakeRot(angle, angle);
  559.     Rot5();
  560.     glFrontFace(GL_CW);
  561.     glColor4f(1.f, 1.f, 1.f, 1.f);
  562.     drawCube(3,3);
  563.     glFrontFace(GL_CCW);
  564.  
  565.     if (alpha != 1.0) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  566.  
  567.     Rot1();
  568.     glColor4f(1.0, 1.0, 0.0, alpha);
  569.     drawCube(1,2);
  570.  
  571.     Rot2();
  572.     glColor4f(1.0, 0.0, 0.0, alpha);
  573.     drawCube(1,2);
  574.  
  575.     Rot3();
  576.     glColor4f(0.0, 0.0, 1.0, alpha);
  577.     drawCube(1,2);
  578.  
  579.     Rot4();
  580.     glColor4f(0.0, 1.0, 1.0, alpha);
  581.     drawCube(1,2);
  582.  
  583.     #ifndef NODRAW
  584.     mglUnlockDisplay();
  585.     #endif
  586.  
  587.     mglSwitchDisplay();
  588.     framecount++;
  589.  
  590.     if (fogon == GL_TRUE)
  591.     {
  592.         Move(win->RPort, 10, win->Height-5);
  593.         sprintf(buffer, "S: %6.3f E: %6.3f", fog_start, fog_end);
  594.         Text(win->RPort, buffer, strlen(buffer));
  595.     }
  596.  
  597.     fracsecs  = ElapsedTime(&eval);
  598.     fracsecs &= 0xFFFF;
  599.  
  600.     fps = 65536.0/(float)(fracsecs+1);
  601.     
  602.     Move(win->RPort, win->Width - 50, 14);
  603.     sprintf(buffer, "%4.2f", fps);
  604.     Text(win->RPort, buffer, strlen(buffer));
  605.     
  606.     return GL_TRUE;
  607. //    return GL_FALSE;
  608. }
  609.  
  610. static GLboolean idle (void)
  611. {
  612.   return draw ();
  613. }
  614.  
  615. static void reshape(int width, int height, int offset, float fov)
  616. {
  617.     GLfloat fog_color[4] = {0.6, 0.3, 0.1, 1.0};
  618.     glFogfv(GL_FOG_COLOR, fog_color);
  619.     glFogf(GL_FOG_MODE, GL_LINEAR);
  620.     glFogf(GL_FOG_START, 1.5);
  621.     glFogf(GL_FOG_END,   fog_end);
  622.  
  623.  
  624.     glMatrixMode(GL_PROJECTION);
  625.     glLoadIdentity();
  626.     gluPerspective(fov, 1.3333333, 1.0, (GLdouble)zback);
  627.  
  628.     glMatrixMode(GL_MODELVIEW);
  629.     glViewport(offset,offset, (GLint)width-2*offset, (GLint)height-2*offset);
  630.     glClearColor(0.0f, 0.f, 0.f, 1.f);
  631.     glClearDepth(zclear);
  632.     glEnable(GL_TEXTURE_2D);
  633.     if (alpha == 1.0) glEnable(GL_CULL_FACE);
  634.     else              glDisable(GL_CULL_FACE);
  635.  
  636. }
  637.  
  638. void ClampMouse(struct Window *window, int x, int y, GLboolean left, GLboolean right)
  639. {
  640.     if (left == GL_FALSE && right == GL_FALSE)
  641.     {
  642.         mouse_x = (GLfloat)x/(GLfloat)(window->Width)*8.0;
  643.         mouse_y = (GLfloat)(window->Height - y)/(GLfloat)(window->Height)*8.0;
  644.         if (mouse_x > 8.0) mouse_x = 8.0;
  645.         if (mouse_y > 8.0) mouse_y = 8.0;
  646.         if (mouse_x < 0.0) mouse_x = 0.0;
  647.         if (mouse_y < 0.0) mouse_y = 0.0;
  648.         mouse_x -= 4.0;
  649.         mouse_y -= 4.0;
  650.         mouse_x *= 2.0;
  651.         mouse_y *= 2.0;
  652.     }
  653.     else
  654.     if (left == GL_TRUE && right == GL_FALSE)
  655.     {
  656.         mouse_z = (GLfloat)y/(GLfloat)(window->Height)*15.0;
  657.         if (mouse_z > 15.0) mouse_z = 15.0;
  658.         if (mouse_z < 0.0) mouse_z = 0.0;
  659.         mouse_z -= 5.0;
  660.     }
  661.     else
  662.     if (left == GL_FALSE && right == GL_TRUE)
  663.     {
  664.         mouse_angle_x = (GLfloat)x/(GLfloat)(window->Width)*360.0;
  665.         mouse_angle_y = (GLfloat)y/(GLfloat)(window->Height)*360.0;
  666.     }
  667. }
  668.  
  669.  
  670. GLenum LockMode = MGL_LOCK_SMART;
  671.  
  672. void IdleHandler(void)
  673. {
  674.     angle+=1.0;
  675.     idle();
  676. }
  677.  
  678. void KeyHandler(char key)
  679. {
  680.     struct Window *window;
  681.     static int drawfn = 0;
  682.     static GLenum WHint = GL_DONT_CARE;
  683.  
  684.     window = (struct Window *)mglGetWindowHandle();
  685.  
  686.     switch(key)
  687.     {
  688.         case '1':
  689.             if (WHint == GL_DONT_CARE) WHint = GL_FASTEST;
  690.             else                       WHint = GL_DONT_CARE;
  691.             glHint(MGL_W_ONE_HINT, WHint);
  692.             break;
  693.         case 's':
  694.             if (sync == GL_FALSE)   sync = GL_TRUE;
  695.             else                sync = GL_FALSE;
  696.             mglEnableSync(sync);
  697.             break;
  698.         case '8':
  699.             fog_end++; if (fog_end > zback) fog_end = zback;
  700.             glFogf(GL_FOG_END, fog_end);
  701.             break;
  702.         case '5':
  703.             fog_end--; if (fog_end < fog_start) fog_end = fog_start;
  704.             glFogf(GL_FOG_END, fog_end);
  705.             break;
  706.         case '7':
  707.             fog_start++; if (fog_start > fog_end) fog_start = fog_end;
  708.             glFogf(GL_FOG_START, fog_start);
  709.             break;
  710.         case '4':
  711.             fog_start--; if (fog_start < 1.5) fog_start = 1.5;
  712.             glFogf(GL_FOG_START, fog_start);
  713.             break;
  714.         case 'f':
  715.             if (fogon == GL_FALSE)
  716.             {
  717.                 glEnable(GL_FOG);
  718.                 fogon = GL_TRUE;
  719.             }
  720.             else
  721.             {
  722.                 glDisable(GL_FOG);
  723.                 fogon = GL_FALSE;
  724.             }
  725.             break;
  726.         case 'd':
  727.             drawfn++; if (funcs[drawfn] == NULL) drawfn = 0;
  728.             drawCube = funcs[drawfn];
  729.             break;
  730.         case 'Q':
  731.         case 27:
  732.             mglExit();
  733.             break;
  734.         case '+':
  735.             if (zclear < 1.0) zclear += 0.01;
  736.             if (zclear > 1.0) zclear = 1.0;
  737.             glClearDepth(zclear);
  738.             break;
  739.         case '-':
  740.             if (zclear > 0.0) zclear -= 0.01;
  741.             if (zclear < 0.0) zclear = 0.0;
  742.             glClearDepth(zclear);
  743.             break;
  744.         case 'e':
  745.             mouse_z = 10.0;
  746.             break;
  747.         case 'w':
  748.             offset += 10;
  749.             if (offset >=100) offset = 100;
  750.             reshape((int)window->Width,(int)window->Height, offset, fov);
  751.             break;
  752.         case 'q':
  753.             offset -= 10;
  754.             if (offset <=0) offset = 0;
  755.             reshape((int)window->Width,(int)window->Height, offset, fov);
  756.             break;
  757.         case 'y':
  758.             fov += 2.0;
  759.             if (fov>180.0) fov = 180.0;
  760.             reshape((int)window->Width,(int)window->Height, offset, fov);
  761.             break;
  762.         case 'x':
  763.             fov -= 2.0;
  764.             if (fov<40.0) fov = 40.0;
  765.             reshape((int)window->Width,(int)window->Height, offset, fov);
  766.             break;
  767.         case 'z':
  768.             if (zbuffer == GL_TRUE)
  769.             {
  770.                 zbuffer = GL_FALSE;
  771.                 glDisable(GL_DEPTH_TEST);
  772.             }
  773.             else
  774.             {
  775.                 zbuffer = GL_TRUE;
  776.                 glEnable(GL_DEPTH_TEST);
  777.             }
  778.             break;
  779.         case 'r':
  780.             ReplaceTexture();
  781.             break;
  782.     }
  783. }
  784.  
  785. void MouseHandler(GLint MouseX, GLint MouseY, GLbitfield buttons)
  786. {
  787. }
  788.  
  789. void MainLoop(void)
  790. {
  791.     struct Window *window;
  792.  
  793.  
  794.     window = (struct Window *)mglGetWindowHandle();
  795.  
  796.     TexInit();
  797.  
  798.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  799.  
  800.     reshape((int)window->Width,(int)window->Height, offset, fov);
  801.     ElapsedTime(&eval);
  802.     SetAPen(window->RPort, 2);
  803.     glEnable(GL_DEPTH_TEST);
  804.     glEnable(GL_TEXTURE_2D);
  805.  
  806.     mglLockMode(LockMode);
  807.     mglKeyFunc(KeyHandler);
  808.     mglMouseFunc(MouseHandler);
  809.     mglIdleFunc(IdleHandler);
  810.     mglMainLoop();
  811. }
  812.  
  813. int main(int argc, char **argv)
  814. {
  815.     int i;
  816.     int numb=3, resnr=0;
  817.  
  818.     printf("Calling MGLInit...\n");
  819.     PrInit();
  820.     MGLInit();
  821.  
  822.     for (i=1; i<argc; i++)
  823.     {
  824.         if (0 == strcmp(argv[i], "-window"))
  825.         {
  826.             mglChooseWindowMode(GL_TRUE);
  827.         }
  828.         else if (0 == strcmp(argv[i], "-zback"))
  829.         {
  830.             i++;
  831.             zback = (GLfloat)atof(argv[i]);
  832.         }
  833.         else if (0 == strcmp(argv[i], "-w"))
  834.         {
  835.             i++;
  836.             inf_w = (GLfloat)atof(argv[i]);
  837.         }
  838.         else if (0 == strcmp(argv[i], "-buffers"))
  839.         {
  840.             i++;
  841.             numb = atoi(argv[i]);
  842.         }
  843.         else if (0 == strcmp(argv[i], "-res"))
  844.         {
  845.             i++;
  846.             resnr = atoi(argv[i]);
  847.         }
  848.         else if (0 == strcmp(argv[i], "-alpha"))
  849.         {
  850.             i++;
  851.             alpha = atof(argv[i]);
  852.         }
  853.         else if (0 == strcmp(argv[i], "-lock"))
  854.         {
  855.             i++;
  856.             if (0 == stricmp(argv[i], "manual"))
  857.             {
  858.                 LockMode = MGL_LOCK_MANUAL;
  859.             }
  860.             else if (0 == stricmp(argv[i], "auto"))
  861.             {
  862.                 LockMode = MGL_LOCK_AUTOMATIC;
  863.             }
  864.             else if (0 == stricmp(argv[i], "smart"))
  865.             {
  866.                 LockMode = MGL_LOCK_SMART;
  867.             }
  868.             else printf("Unknown lockmode. Using default\n");
  869.         }
  870.         else
  871.         {
  872.             printf("Unknown option %s\n", argv[i]);
  873.             printf("Usage: %s -zback <float> -w <float> -res <int> -lock (manual|auto|smart) -buffers <int>\n", argv[0]);
  874.             exit(0);
  875.         }
  876.     }
  877.  
  878.     ResPtr = resnr;
  879.     CurrentRes = Resolutions[ResPtr].name;
  880.  
  881.     fog_start = 1.5;
  882.     fog_end = 100.0;
  883.  
  884.     //mglSetDebugLevel(10);
  885.     printf("Setting number of buffers to %d...\n", numb);
  886.     mglChooseNumberOfBuffers(numb);
  887.     printf("Setting pixel depth to 15...\n");
  888.     mglChoosePixelDepth(15);
  889.     printf("Creating context...\n");
  890.     if (mglCreateContext(0,0, Resolutions[ResPtr].width, Resolutions[ResPtr].height))
  891.     {
  892.         if (alpha != 1.0) glShadeModel(GL_FLAT);
  893.         printf("Switching sync...\n");
  894.         mglEnableSync(GL_TRUE);
  895.         printf("Going into main loop...\n");
  896.         MainLoop();
  897.         printf("Done\n");
  898.         mglDeleteContext();
  899.     }
  900.     else
  901.     {
  902.         printf("Error: Can't mglCreateContext()\n");
  903.     }
  904.  
  905.     MGLTerm();
  906.     PrExit();
  907.     return 0;
  908. }
  909.