home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / rayce27s / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-02  |  5.1 KB  |  214 lines

  1.  
  2. /*
  3.  * texture.c -- standard manipulations for textures
  4.  * 
  5.  * (c) 1993, 1994 by Han-Wen Nienhuys <hanwen@stack.urc.tue.nl>
  6.  * 
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License as published by the
  9.  * Free Software Foundation;
  10.  * 
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License along
  17.  * with this program; if not, write to the Free Software Foundation, Inc.,
  18.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include "ray.h"
  23. #include "proto.h"
  24. #include "extern.h"
  25.  
  26. PRIVATE void
  27. init_texture(struct texture_data *td)
  28. {
  29.     color           black;
  30.  
  31.     /* initialize a texture to black, riennada f.. nothing */
  32.     td->PoVcolor = NULL;
  33.     td->type = T_EGAL;
  34.     td->shadingtype = PHONG_SHADING;
  35.  
  36.     td->refl_diffuse =
  37.     td->refr_diffuse =
  38.     td->roughness = 0.00;
  39.  
  40.     td->brilliance = td->index = 1.0;
  41.  
  42.     setcolor(black, 0, 0, 0);
  43.  
  44.     td->refl_color = black;
  45.     td->refr_color = black;
  46.     td->ambient = black;
  47.     td->diffuse = black;
  48.     td->specular = black;
  49.  
  50.     unit_matrix(td->inv_trans);
  51. }
  52.  
  53. PUBLIC struct texture_data *
  54. get_new_texture(void)
  55. {
  56.     struct texture_data *td;
  57.  
  58.     td = ALLOC(struct texture_data);
  59.  
  60.     CHECK_MEM(td, "texture");
  61.     init_texture(td);
  62.  
  63.     return td;
  64. }
  65.  
  66. /* standard routine for a texture */
  67. PUBLIC struct texture_data *
  68. copy_texture(struct texture_data *dst, struct texture_data *src)
  69. {
  70.     assert(dst != NULL && src != NULL);
  71.  
  72.     *dst = *src;
  73.     if (src->PoVcolor != NULL) {
  74.     dst->PoVcolor = get_new_color();
  75.     copy_color(dst->PoVcolor, src->PoVcolor);
  76.     }
  77.     if (src->type == T_IMAGEMAP) {
  78.     dst->exttext.image = get_new_image_map();
  79.     copy_image_map(dst->exttext.image, src->exttext.image);
  80.     }
  81.     return dst;
  82. }
  83.  
  84. /* translate a texture. */
  85. PUBLIC void
  86. translate_texture(struct texture_data *t, vector v)
  87. {
  88.     matrix          m;
  89.  
  90.     if (t->type == T_EGAL)
  91.     return;
  92.  
  93.     inv_translate_matrix(m, v);
  94.  
  95.     mmproduct(t->inv_trans, t->inv_trans, m);
  96. }
  97.  
  98. /* rotation */
  99. PUBLIC void
  100. rotate_texture(struct texture_data *t, matrix rotmat)
  101. {
  102.     matrix          m;
  103.  
  104.     if (t->type == T_EGAL)
  105.     return;
  106.  
  107.     transpose_matrix(m, rotmat);
  108.     mmproduct(t->inv_trans, t->inv_trans, m);
  109. }
  110.  
  111. /* scale. Not needed for egal textures */
  112. PUBLIC void
  113. scale_texture(struct texture_data *t, vector s)
  114. {
  115.     matrix          m;
  116.  
  117.     if (t->type == T_EGAL)
  118.     return;
  119.  
  120.     inv_scale_matrix(m, s);
  121.     mmproduct(t->inv_trans, t->inv_trans, m);
  122. }
  123.  
  124. /* zapit. Don't forget to free imagemaps as well */
  125. PUBLIC void
  126. free_texture(struct texture_data *t)
  127. {
  128.     if (t->PoVcolor != NULL)
  129.     free_color(t->PoVcolor);
  130.     switch (t->type) {
  131.     case T_EGAL:
  132.     break;
  133.     case T_IMAGEMAP:
  134.     free_image_map(t->exttext.image);
  135.     break;
  136.     default:
  137.     assert(FALSE);
  138.     }
  139.  
  140.     free((void *) t);
  141. }
  142.  
  143. /*
  144.  * get homogenous equivalent of of orgtext at point x
  145.  */
  146. PUBLIC void
  147. fill_texture(struct texture_data *tp, struct texture_data *orgtext, struct intersect i)
  148. {
  149.     color           c;
  150.     vector          x = i.x;
  151.  
  152.     *tp = *orgtext;
  153.     if (orgtext->type == T_EGAL) {
  154.  
  155.     /* pov compatibility */
  156.     if (orgtext->PoVcolor != NULL) {
  157.         c = *orgtext->PoVcolor;
  158.         tp->ambient = scolorproduct(color_length(orgtext->ambient), c);
  159.         tp->diffuse = scolorproduct(color_length(orgtext->diffuse), c);
  160.  
  161.         /* specular always looks white in PoV */
  162.         setcolor(tp->specular, 1, 1, 1);
  163.         scale_color(&tp->specular, color_length(orgtext->specular));
  164.  
  165.         tp->refl_color = scolorproduct(color_length(orgtext->refl_color), c);
  166.         tp->refr_color = scolorproduct(color_length(orgtext->refr_color), c);
  167.         return;
  168.     }
  169.     } else if (orgtext->type == T_IMAGEMAP) {
  170.     x = mvproduct(orgtext->inv_trans, x);
  171.     c = get_image_map_color(orgtext->exttext.image, i);
  172.     tp->ambient = scolorproduct(color_length(orgtext->ambient), c);
  173.     tp->diffuse = scolorproduct(color_length(orgtext->diffuse), c);
  174.  
  175.     /* specular always looks white in PoV */
  176.     setcolor(tp->specular, 1, 1, 1);
  177.     scale_color(&tp->specular, color_length(orgtext->specular));
  178.  
  179.     tp->refl_color = scolorproduct(color_length(orgtext->refl_color), c);
  180.     tp->refr_color = scolorproduct(color_length(orgtext->refr_color), c);
  181.     return;
  182.     } else
  183.     assert(FALSE);
  184. }
  185.  
  186. PUBLIC void
  187. print_texture(struct texture_data *tx)
  188. {
  189. #ifdef DEBUG
  190.     printf("\n\tTEXTURE TYPE %d\n", tx->type);
  191.     if (tx->type == T_IMAGEMAP) {
  192.     print_image_map(tx->exttext.image);
  193.     printf("transformation:\n");
  194.     print_matrix(tx->inv_trans);
  195.     }
  196.     if (tx->PoVcolor != NULL) {
  197.     printf("POV compatibility\n");
  198.     print_c("POV color", *tx->PoVcolor);
  199.     }
  200.     print_c("ambient", tx->ambient);
  201.     print_c("diffuse", tx->diffuse);
  202.     printf("brilliance %lf\n", tx->brilliance);
  203.  
  204.     print_c("specular", tx->specular);
  205.     printf("roughness %lf\n", tx->roughness);
  206.  
  207.     printf("reflang %lf ", tx->refl_diffuse);
  208.     print_c("reflcolor", tx->refl_color);
  209.  
  210.     printf("ior %lf refrang %lf, ", tx->index, tx->refr_diffuse);
  211.     print_c("refrcolor", tx->refr_color);
  212. #endif
  213. }
  214.