home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / TEXTURE.C < prev    next >
Text File  |  1995-03-04  |  7KB  |  446 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. //#include <unistd.h>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include "tk.h"
  10.  
  11.  
  12. GLenum doubleBuffer, directRender;
  13.  
  14. char *texFileName = 0;
  15. TK_RGBImageRec *image;
  16.  
  17. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  18. float decal[] = {GL_DECAL};
  19. float modulate[] = {GL_MODULATE};
  20. float repeat[] = {GL_REPEAT};
  21. float clamp[] = {GL_CLAMP};
  22. float nr[] = {GL_NEAREST};
  23. float ln[] = {GL_LINEAR};
  24. float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
  25. float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
  26. float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
  27. float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
  28. GLint sphereMap[] = {GL_SPHERE_MAP};
  29.  
  30. GLenum doSphere = GL_FALSE;
  31. float xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125;
  32.  
  33. GLint cube;
  34. float c[6][4][3] = {
  35.     {
  36.     {
  37.         1.0, 1.0, -1.0
  38.     },
  39.     {
  40.         -1.0, 1.0, -1.0
  41.     },
  42.     {
  43.         -1.0, -1.0, -1.0
  44.     },
  45.     {
  46.         1.0, -1.0, -1.0
  47.     }
  48.     },
  49.     {
  50.     {
  51.         1.0, 1.0, 1.0
  52.     },
  53.     {
  54.         1.0, 1.0, -1.0
  55.     },
  56.     {
  57.         1.0, -1.0, -1.0
  58.     },
  59.     {
  60.         1.0, -1.0, 1.0
  61.     }
  62.     },
  63.     {
  64.     {
  65.         -1.0, 1.0, 1.0
  66.     },
  67.     {
  68.         1.0, 1.0, 1.0
  69.     },
  70.     {
  71.         1.0, -1.0, 1.0
  72.     },
  73.     {
  74.         -1.0, -1.0, 1.0
  75.     }
  76.     },
  77.     {
  78.     {
  79.         -1.0, 1.0, -1.0
  80.     },
  81.     {
  82.         -1.0, 1.0, 1.0
  83.     },
  84.     {
  85.         -1.0, -1.0, 1.0
  86.     },
  87.     {
  88.         -1.0, -1.0, -1.0
  89.     }
  90.     },
  91.     {
  92.     {
  93.         -1.0, 1.0, 1.0
  94.     },
  95.     {
  96.         -1.0, 1.0, -1.0
  97.     },
  98.     {
  99.         1.0, 1.0, -1.0
  100.     },
  101.     {
  102.         1.0, 1.0, 1.0
  103.     }
  104.     },
  105.     {
  106.     {
  107.         -1.0, -1.0, -1.0
  108.     },
  109.     {
  110.         -1.0, -1.0, 1.0
  111.     },
  112.     {
  113.         1.0, -1.0, 1.0
  114.     },
  115.     {
  116.         1.0, -1.0, -1.0
  117.     }
  118.     }
  119. };
  120. static float n[6][3] = {
  121.     {
  122.     0.0, 0.0, -1.0
  123.     },
  124.     {
  125.     1.0, 0.0, 0.0
  126.     },
  127.     {
  128.     0.0, 0.0, 1.0
  129.     },
  130.     {
  131.     -1.0, 0.0, 0.0
  132.     },
  133.     {
  134.     0.0, 1.0, 0.0
  135.     },
  136.     {
  137.     0.0, -1.0, 0.0
  138.     }
  139. };
  140. static float t[6][4][2] = {
  141.     {
  142.     {
  143.         1.1,  1.1
  144.     },
  145.     {
  146.         -0.1, 1.1
  147.     },
  148.     {
  149.         -0.1, -0.1
  150.     },
  151.     {
  152.         1.1,  -0.1
  153.     }
  154.     },
  155.     {
  156.     {
  157.         1.1,  1.1
  158.     },
  159.     {
  160.         -0.1, 1.1
  161.     },
  162.     {
  163.         -0.1, -0.1
  164.     },
  165.     {
  166.         1.1,  -0.1
  167.     }
  168.     },
  169.     {
  170.     {
  171.         -0.1,  1.1
  172.     },
  173.     {
  174.         1.1, 1.1
  175.     },
  176.     {
  177.         1.1, -0.1
  178.     },
  179.     {
  180.         -0.1,  -0.1
  181.     }
  182.     },
  183.     {
  184.     {
  185.         1.1,  1.1
  186.     },
  187.     {
  188.         -0.1, 1.1
  189.     },
  190.     {
  191.         -0.1, -0.1
  192.     },
  193.     {
  194.         1.1,  -0.1
  195.     }
  196.     },
  197.     {
  198.     {
  199.         1.1,  1.1
  200.     },
  201.     {
  202.         -0.1, 1.1
  203.     },
  204.     {
  205.         -0.1, -0.1
  206.     },
  207.     {
  208.         1.1,  -0.1
  209.     }
  210.     },
  211.     {
  212.     {
  213.         1.1,  1.1
  214.     },
  215.     {
  216.         -0.1, 1.1
  217.     },
  218.     {
  219.         -0.1, -0.1
  220.     },
  221.     {
  222.         1.1,  -0.1
  223.     }
  224.     },
  225. };
  226.  
  227.  
  228. static void BuildCube(void)
  229. {
  230.     GLint i;
  231.  
  232.     glNewList(cube, GL_COMPILE);
  233.     for (i = 0; i < 6; i++) {
  234.     glBegin(GL_POLYGON);
  235.         glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]);
  236.         glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]);
  237.         glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]);
  238.         glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]);
  239.     glEnd();
  240.     }
  241.     glEndList();
  242. }
  243.  
  244. static void BuildLists(void)
  245. {
  246.  
  247.     cube = glGenLists(1);
  248.     BuildCube();
  249. }
  250.  
  251. static void Init(void)
  252. {
  253.  
  254.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  255.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
  256.               GL_RGB, GL_UNSIGNED_BYTE, image->data);
  257.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  258.     glEnable(GL_TEXTURE_2D);
  259.  
  260.     glFrontFace(GL_CCW);
  261.     glCullFace(GL_FRONT);
  262.     glEnable(GL_CULL_FACE);
  263.  
  264.     BuildLists();
  265.  
  266.     glClearColor(0.0, 0.0, 0.0, 0.0);
  267.  
  268.     magFilter = nr;
  269.     minFilter = nr;
  270.     sWrapMode = repeat;
  271.     tWrapMode = repeat;
  272. }
  273.  
  274. static void Reshape(int width, int height)
  275. {
  276.  
  277.     glViewport(0, 0, (GLint)width, (GLint)height);
  278.  
  279.     glMatrixMode(GL_PROJECTION);
  280.     glLoadIdentity();
  281.     gluPerspective(145.0, 1.0, 0.01, 1000);
  282.     glMatrixMode(GL_MODELVIEW);
  283. }
  284.  
  285. static GLenum Key(int key, GLenum mask)
  286. {
  287.  
  288.     switch (key) {
  289.       case TK_ESCAPE:
  290.     tkQuit();
  291.  
  292.       case TK_LEFT:
  293.     yRotation -= 0.5;
  294.     break;
  295.       case TK_RIGHT:
  296.     yRotation += 0.5;
  297.     break;
  298.       case TK_UP:
  299.     xRotation -= 0.5;
  300.     break;
  301.       case TK_DOWN:
  302.     xRotation += 0.5;
  303.     break;
  304.       case TK_T:
  305.     zTranslate += 0.25;
  306.     break;
  307.       case TK_t:
  308.     zTranslate -= 0.25;
  309.     break;
  310.  
  311.       case TK_s:
  312.     doSphere = !doSphere;
  313.     if (doSphere) {
  314.         glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  315.         glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  316.         glEnable(GL_TEXTURE_GEN_S);
  317.         glEnable(GL_TEXTURE_GEN_T);
  318.     } else {
  319.         glDisable(GL_TEXTURE_GEN_S);
  320.         glDisable(GL_TEXTURE_GEN_T);
  321.     }
  322.     break;
  323.  
  324.       case TK_0:
  325.     magFilter = nr;
  326.     break;
  327.       case TK_1:
  328.     magFilter = ln;
  329.     break;
  330.       case TK_2:
  331.     minFilter = nr;
  332.     break;
  333.       case TK_3:
  334.     minFilter = ln;
  335.     break;
  336.       case TK_4:
  337.     minFilter = nr_mipmap_nr;
  338.     break;
  339.       case TK_5:
  340.     minFilter = nr_mipmap_ln;
  341.     break;
  342.       case TK_6:
  343.     minFilter = ln_mipmap_nr;
  344.     break;
  345.       case TK_7:
  346.     minFilter = ln_mipmap_ln;
  347.     break;
  348.  
  349.       default:
  350.     return GL_FALSE;
  351.     }
  352.     return GL_TRUE;
  353. }
  354.  
  355. static void Draw(void)
  356. {
  357.  
  358.     glClear(GL_COLOR_BUFFER_BIT);
  359.  
  360.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  361.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  362.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  363.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  364.  
  365.     glPushMatrix();
  366.  
  367.     glTranslatef(0.0, 0.0, zTranslate);
  368.     glRotatef(xRotation, 1, 0, 0);
  369.     glRotatef(yRotation, 0, 1, 0);
  370.     glCallList(cube);
  371.  
  372.     glPopMatrix();
  373.  
  374.     glFlush();
  375.  
  376.     if (doubleBuffer) {
  377.     tkSwapBuffers();
  378.     }
  379. }
  380.  
  381. static GLenum Args(int argc, char **argv)
  382. {
  383.     GLint i;
  384.  
  385.     doubleBuffer = GL_FALSE;
  386.     directRender = GL_TRUE;
  387.  
  388.     for (i = 1; i < argc; i++) {
  389.     if (strcmp(argv[i], "-sb") == 0) {
  390.         doubleBuffer = GL_FALSE;
  391.     } else if (strcmp(argv[i], "-db") == 0) {
  392.         doubleBuffer = GL_TRUE;
  393.     } else if (strcmp(argv[i], "-dr") == 0) {
  394.         directRender = GL_TRUE;
  395.     } else if (strcmp(argv[i], "-ir") == 0) {
  396.         directRender = GL_FALSE;
  397.     } else if (strcmp(argv[i], "-f") == 0) {
  398.         if (i+1 >= argc || argv[i+1][0] == '-') {
  399.         printf("-f (No file name).\n");
  400.         return GL_FALSE;
  401.         } else {
  402.         texFileName = argv[++i];
  403.         }
  404.     } else {
  405.         printf("%s (Bad option).\n", argv[i]);
  406.         return GL_FALSE;
  407.     }
  408.     }
  409.     return GL_TRUE;
  410. }
  411.  
  412. void main(int argc, char **argv)
  413. {
  414.     GLenum type;
  415.  
  416.     if (Args(argc, argv) == GL_FALSE) {
  417.     tkQuit();
  418.     }
  419.  
  420.     if (texFileName == 0) {
  421.     printf("No image file.\n");
  422.     tkQuit();
  423.     }
  424.  
  425.     image = tkRGBImageLoad(texFileName);
  426.  
  427.     tkInitPosition(0, 0, 300, 300);
  428.  
  429.     type = TK_RGB;
  430.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  431.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  432.     tkInitDisplayMode(type);
  433.  
  434.     if (tkInitWindow("Texture Test") == GL_FALSE) {
  435.     tkQuit();
  436.     }
  437.  
  438.     Init();
  439.  
  440.     tkExposeFunc(Reshape);
  441.     tkReshapeFunc(Reshape);
  442.     tkKeyDownFunc(Key);
  443.     tkDisplayFunc(Draw);
  444.     tkExec();
  445. }
  446.