home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / raytrace / dbw_render / source / c / TEX < prev    next >
Encoding:
Text File  |  1992-10-23  |  10.0 KB  |  264 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  
  24. #define MODULE_TEXTURE
  25. #include "ray.h"
  26.  
  27. vector  white   = 
  28. {
  29.      1.0 ,1.0 ,1.0 
  30. }
  31. ;
  32.  
  33. void gettex(diffuse,np,p,n)
  34. vector  diffuse,
  35.   p,
  36.   n;
  37. node    *np;
  38. {
  39.      int         whichtexture,
  40.        i,
  41.        j,
  42.        ibx,
  43.        iby,
  44.        ibz;
  45.      float       texture,
  46.        bx,
  47.        by,
  48.        bz;
  49.      long        curs;
  50.  
  51.      /* keep track of maximum stack extent */
  52.      curs = curstack();
  53.      if (curs < stackbot)
  54.           stackbot = curs;
  55.  
  56.      whichtexture = np->attr.tex;
  57.  
  58.      /* This is copied to a temp so that local solid texturing may be applied
  59.      non-destructively. */
  60.  
  61.      veccopy(np->attr.dif,diffuse); /* start with the object's defined color */
  62.  
  63.      if (whichtexture != 0) 
  64.      { /* don't bother checking if there's no texture */
  65.           if (whichtexture >= 20 && whichtexture < 30) 
  66.           { /* MARBLE */
  67.                j = whichtexture - 20;  /* select the desired marble */
  68.                texture  = p[0] * marble[j].xscale;
  69.                texture += turbulence(p) * marble[j].turbscale;
  70.                texture  = (sin(texture) + 1.0) / 2.0;
  71.                for (i = 0; i < marble[j].squeeze; i++)
  72.                     texture = texture * texture;  /* squeeze peaks thinner */
  73.                blendcolor(diffuse,marble[j].veincolor,texture,diffuse);
  74.           }
  75.  
  76.           else if (whichtexture >= 40 && whichtexture < 50) 
  77.           { /* WOOD */
  78.                /*mulvm(p,woodorient,tp);*/
  79.                j = whichtexture - 40;  /* select the desired wood */
  80.                texture  = sqrt(p[0]*p[0] + p[2]*p[2]);
  81.                texture *= wood[j].ringspacing;
  82.                texture += turbulence(p) * wood[j].turbscale;
  83.                texture = (sin(texture) + 1.0) / 2.0;
  84.                for (i = 0; i < wood[j].squeeze; i++)
  85.                     texture = texture * texture;  /* squeeze peaks thinner */
  86.                texture *= wood[j].thickscale;  /* scale 0..n */
  87.                if (texture > 1.0)
  88.                     texture = 1.0;  /* clip to 0..1 */
  89.  
  90.                blendcolor(diffuse,wood[j].othercolor,texture,diffuse);
  91.           }
  92.  
  93.           else if (whichtexture >= 50 && whichtexture < 60) 
  94.           { /* CHECKERBOARD */
  95.                j = whichtexture - 50;  /* select the desired checker */
  96.                ibx = 0;
  97.                iby = 0;
  98.                ibz = 0;
  99.  
  100.                if (checker[j].x != 0.0) 
  101.                {
  102.                     texture = p[0] / checker[j].x;
  103.                     if (texture < 0.0)
  104.                          texture = 1.0 - texture;
  105.                     ibx = (int) texture;
  106.                }
  107.                if (checker[j].y != 0.0) 
  108.                {
  109.                     texture = p[1] / checker[j].y;
  110.                     if (texture < 0.0)
  111.                          texture = 1.0 - texture;
  112.                     iby = (int) texture;
  113.                }
  114.                if (checker[j].z != 0.0) 
  115.                {
  116.                     texture = p[2] / checker[j].z;
  117.                     if (texture < 0.0)
  118.                          texture = 1.0 - texture;
  119.                     ibz = (int) texture;
  120.                }
  121.  
  122.                if ((ibx+iby+ibz) & 1) 
  123.                {
  124.                     veccopy(checker[j].color,diffuse);
  125.                }
  126.           }
  127.  
  128.           else if (whichtexture >= 70 && whichtexture < 80) 
  129.           { /* BLEND */
  130.                j = whichtexture - 70;  /* select the desired blend */
  131.                texture = (p[1] - blend[j].start) / blend[j].scale;
  132.                if (texture > 1.0)
  133.                     texture = 1.0;
  134.                if (texture < 0.0)
  135.                     texture = 0.0;
  136.                blendcolor(diffuse,blend[j].color,texture,diffuse);
  137.           }
  138.  
  139.           else if (whichtexture >= 80 && whichtexture < 90) 
  140.           { /* SNOW */
  141.                j = whichtexture - 80;  /* select the desired snow */
  142.                texture  = p[1] - snow[j].start;
  143.                texture /= snow[j].altscale;
  144.                texture *= snow[j].altfactor;
  145.                texture *= DOT(n,yaxis);
  146.                if (texture > 1.0)
  147.                     texture = 1.0;
  148.                if (texture < 0.0)
  149.                     texture = 0.0;
  150.                if (texture > snow[j].threshhold)
  151.                     texture = 1.0;
  152.                blendcolor(diffuse,white,texture,diffuse);
  153.           }
  154.  
  155.           else
  156.                switch (whichtexture) 
  157.                { /* MISC SINGULAR TEXTURES */
  158.                case 1  :
  159.                     /* Y axis color gradiation */
  160.                     hls(p[1]/20.0,0.5,1.0,diffuse);
  161.                     break;
  162.  
  163.                case 3  :
  164.                     /* mottled diffuse */
  165.                     vecscale(noise(p),diffuse,diffuse);
  166.                     break;
  167.  
  168.                case 5  :
  169.                     /* Brick pattern texture */
  170.  
  171.                     /* First,check mortar layers in the X-Z plane */
  172.                     by  = p[1] / brickheight;
  173.                     iby = (int) by;
  174.                     by -= (float) iby; /* just save fraction */
  175.                     if (by < 0.0)
  176.                          by += 1.0;
  177.                     if (by <= brickmortarheight) 
  178.                     {
  179.                          /* This point is on the X-Z plane */
  180.                          veccopy(mortardiffuse,diffuse);
  181.                          break;
  182.                     }
  183.  
  184.                     /* Check odd mortar layers in the Y-Z plane */
  185.                     /* First calculate a common factor */
  186.                     by  = p[1];
  187.                     by /= brickheight;
  188.                     by /= 2.0;
  189.                     iby = (int) by;
  190.                     by -= (float) iby; /* just save fraction */
  191.                     if (by < 0.0)
  192.                          by += 1.0;
  193.  
  194.                     bx  = p[0] / brickwidth;
  195.                     ibx = (int) bx;
  196.                     bx -= (float) ibx; /* just save fraction */
  197.                     if (bx < 0.0)
  198.                          bx += 1.0;
  199.                     if (bx <= brickmortarwidth) 
  200.                     {
  201.                          /* So this point is on the Y-Z mortar plane.  We now must
  202.                          factor in the odd staggered brick effect */
  203.                          if (by <= 0.5) 
  204.                          {
  205.                               veccopy(mortardiffuse,diffuse);
  206.                               break;
  207.                          }
  208.                     }
  209.                     /* Check even mortar layers in the Y-Z plane */
  210.                     bx  = (p[0] / brickwidth) + 0.5;
  211.                     ibx = (int) bx;
  212.                     bx -= (float) ibx; /* just save fraction */
  213.                     if (bx < 0.0)
  214.                          bx += 1.0;
  215.                     if (bx <= brickmortarwidth) 
  216.                     {
  217.                          /* So this point is on the Y-Z mortar plane.  We now must
  218.                          factor in the even staggered brick effect */
  219.                          if (by > 0.5) 
  220.                          {
  221.                               veccopy(mortardiffuse,diffuse);
  222.                               break;
  223.                          }
  224.                     }
  225.  
  226.                     /* Check odd mortar layers in the Y-X plane */
  227.                     bz  = p[2] / brickdepth;
  228.                     ibz = (int) bz;
  229.                     bz -= (float) ibz; /* just save fraction */
  230.                     if (bz < 0.0)
  231.                          bz += 1.0;
  232.                     if (bz <= brickmortardepth) 
  233.                     {
  234.                          /* So this point is on the Y-X mortar plane.  We now must
  235.                          factor in the odd staggered brick effect */
  236.                          if (by > 0.5) 
  237.                          {
  238.                               veccopy(mortardiffuse,diffuse);
  239.                               break;
  240.                          }
  241.                     }
  242.                     /* Check even mortar layers in the Y-X plane */
  243.                     bz  = (p[2] / brickdepth) + 0.5;
  244.                     ibz = (int) bz;
  245.                     bz -= (float) ibz; /* just save fraction */
  246.                     if (bz < 0.0)
  247.                          bz += 1.0;
  248.                     if (bz <= brickmortardepth) 
  249.                     {
  250.                          /* So this point is on the Y-X mortar plane.  We now must
  251.                          factor in the even staggered brick effect */
  252.                          if (by <= 0.5) 
  253.                          {
  254.                               veccopy(mortardiffuse,diffuse);
  255.                               break;
  256.                          }
  257.                     }
  258.                     /* So we're inside a brick */
  259.                     break;
  260.                }
  261.      }
  262. }
  263.  
  264.