home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Source / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-18  |  3.4 KB  |  161 lines  |  [TEXT/CWIE]

  1. /*
  2. **  MacWT -- a 3d game engine for the Macintosh
  3. **  © 1995, Bill Hayden and Nikol Software
  4. **  Free for non-commercial use - address questions to the e-mail address below
  5. **
  6. **  Mail:           afn28988@freenet.ufl.edu (Bill Hayden)
  7. **    MacWT FTP site: ftp.circa.ufl.edu/pub/software/ufmug/mirrors/LocalSW/Hayden/
  8. **  WWW Page:       http://grove.ufl.edu:80/~nikolsw
  9. **
  10. **    All of the above addresses are due to changes sometime in 1996, so stay tuned
  11. **
  12. **  based on wt, by Chris Laurel
  13. **
  14. **  This program is distributed in the hope that it will be useful,
  15. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. */
  18.  
  19. /* GIF87a code from Thomas Hamren (d0hampe@dtek.chalmers.se) */
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include "wt.h"
  27. #include "wtmem.h"
  28. #include "error.h"
  29. #include "color.h"
  30. #include "graphfile.h"
  31. #include "framebuf.h"
  32. #include "graphics.h"
  33. #include "texture.h"
  34. #include "MacWT.h"
  35.  
  36.  
  37. static short ilog2(short x);
  38. static void quantize_texture(Graphic_file *gfile, Texture *t);
  39.  
  40.  
  41.  
  42.  
  43. Texture *new_texture(short width, short height)
  44. {
  45.     Texture *t;
  46.  
  47.     if (height <= 0 || width <= 0)
  48.         fatal_error("new_texture:  bad texture dimensions");
  49.  
  50.     t = (Texture *)wtmalloc(sizeof(Texture));
  51.  
  52.     if (gTrueColor)
  53.         {
  54.         t->texels16 = (Pixel16 *)wtmalloc(height * width * gSizeOfPixel);
  55.         t->texels = NULL;
  56.         }
  57.     else
  58.         {
  59.         t->texels = (Pixel *)wtmalloc(height * width * gSizeOfPixel);
  60.         t->texels16 = NULL;
  61.         }
  62.  
  63.     t->width = width;
  64.     t->height = height;
  65.     t->log2height = ilog2(height);
  66.      
  67.     return t;
  68. }
  69.  
  70.  
  71.  
  72. Texture *read_texture_file(char *filename)
  73. {
  74.     Graphic_file *gfile;
  75.     Texture *t;
  76.  
  77.  
  78.     gfile = read_graphic_file(filename);
  79.     if (gfile == NULL)
  80.         fatal_error("Error reading texture %s.", filename);
  81.  
  82.     /* The height and width should be powers of two for efficient
  83.     **   texture mapping.  Here, we enforce this.
  84.     */
  85.     if (ilog2(gfile->height) == -1)
  86.         fatal_error("Height of texture %s is not a power of two.", filename);
  87.      
  88.     t = new_texture(gfile->width, gfile->height);
  89.     quantize_texture(gfile, t);
  90.  
  91.     free_graphic_file(gfile);
  92.      
  93.     return t;
  94. }
  95.  
  96.  
  97.  
  98. /* Return the log base 2 of the argument, or -1 if the argument is not
  99. **   an integer power of 2.
  100. */
  101. static short ilog2(short x)
  102. {
  103.     short i;
  104.     unsigned short n;
  105.  
  106.     if (x <= 0)
  107.         return -1;
  108.     else
  109.         n = (unsigned short) x;
  110.      
  111.     for (i = 0; (n & 0x1) == 0; i++, n >>= 1);
  112.  
  113.     if (n == 1)
  114.         return i;
  115.     else
  116.         return -1;
  117. }
  118.  
  119.  
  120. static void quantize_texture(Graphic_file *gfile, Texture *t)
  121. {
  122.     RGBcolor    pixel;        // Generic Color
  123.     RGBColor    macColor;    // Macintosh Color  (notice the case)
  124.     short        x, y;
  125.     
  126.  
  127.     if (gTrueColor)
  128.         {
  129.         Pixel16        *dest_pixel = t->texels16;
  130.  
  131.         for (x = gfile->width - 1; x >= 0; x--)
  132.             for (y = gfile->height - 1; y >= 0; y--)
  133.                 {
  134.                 if (!graphic_file_pixel(gfile, x, y, &pixel))
  135.                     *dest_pixel++ = (Pixel16) ~0;
  136.                 else
  137.                     *dest_pixel++ = (Pixel16)(( (pixel.red >> 3) << 10) +
  138.                                               ( (pixel.green >> 3) << 5) +
  139.                                                 (pixel.blue >> 3));
  140.                 }
  141.         }
  142.     else
  143.         {
  144.         Pixel        *dest_pixel = t->texels;
  145.  
  146.         for (x = gfile->width - 1; x >= 0; x--)
  147.             for (y = gfile->height - 1; y >= 0; y--)
  148.                 {
  149.                 if (!graphic_file_pixel(gfile, x, y, &pixel))
  150.                     *dest_pixel++ = (Pixel) ~0;
  151.                 else
  152.                     {
  153.                     macColor.red = pixel.red << 8;
  154.                     macColor.green = pixel.green << 8;
  155.                     macColor.blue = pixel.blue << 8;
  156.                     *dest_pixel++ = (Pixel)Color2Index( &macColor );
  157.                     }
  158.                 }
  159.         }
  160. }
  161.