~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/tv/tv_zbuff.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)
  2 Basic zbuff utilities:
  3     default color & shading functions
  4     buffer allocation
  5     visibility testing
  6 **/
  7 
  8 #include <math.h>
  9 #include <values.h>
 10 #include <tina/sys.h>
 11 #include <tina/sysfuncs.h>
 12 #include <tina/math.h>
 13 #include <tina/mathfuncs.h>
 14 #include <tina/vision.h>
 15 #include <tina/visionfuncs.h>
 16 #include <tina/tv.h>
 17 #include <tina/tvfuncs.h>
 18 
 19 /* sets color function: this returns Tina color number to be associated
 20  * with intensity 0 < g < 1 */
 21 void    tv_zbuff_color_set(Tv * tv, int (*color) ())
 22 {
 23     if (tv == NULL || tv->zbuff == NULL)
 24         return;
 25     tv->zbuff->color = color;
 26 }
 27 
 28 /* default color function: */
 29 int     grey_color(double g)
 30 {
 31     return ((int) (255.0 * g));
 32 }
 33 
 34 /* sets shade function: this returns intensity at point and normal */
 35 void    tv_zbuff_shade_set(Tv * tv, double (*func) ())
 36 {
 37     if (tv == NULL || tv->zbuff == NULL)
 38         return;
 39     tv->zbuff->shade = func;
 40 }
 41 
 42 /* default shade function */
 43 /* ARGSUSED quieten lint */
 44 double  spec_shade(Tv * tv, Vec3 n, Vec3 x)
 45 {
 46     double  c = vec3_dot(tv->ez3, n);
 47     double  gl;
 48 
 49     gl = 0.2+0.4 * fabs(c) + 0.4 * c * c * c * c;
 50     gl = MIN(gl, 1.0);
 51     return (gl);
 52 }
 53 
 54 /* shade inside darker function */
 55 /* ARGSUSED quieten lint */
 56 double  spec_shade_inside_1(Tv * tv, Vec3 n, Vec3 x)
 57 {
 58     double  c = vec3_dot(tv->ez3, n);
 59     double  gl;
 60 
 61     if (c >= 0.0)
 62         gl = 0.3 * fabs(c) + 0.3 * c * c * c * c;
 63     else
 64         gl = 0.4 + 0.3 * fabs(c) + 0.3 * c * c * c * c;
 65     gl = MIN(gl, 1.0);
 66     return (gl);
 67 }
 68 
 69 /* shade inside darker function */
 70 /* ARGSUSED quieten lint */
 71 double  spec_shade_inside_2(Tv * tv, Vec3 n, Vec3 x)
 72 {
 73     double  c = vec3_dot(tv->ez3, n);
 74     double  gl;
 75 
 76     if (c < 0.0)
 77         gl = 0.3 * fabs(c) + 0.3 * c * c * c * c;
 78     else
 79         gl = 0.4 + 0.3 * fabs(c) + 0.3 * c * c * c * c;
 80     gl = MIN(gl, 1.0);
 81     return (gl);
 82 }
 83 
 84 Tv_zbuff *zbuff_alloc(int height, int width, double zmin, double zmax)
 85 {
 86     int     i, j;
 87     Tv_zbuff *zbuff = ts_ralloc(Tv_zbuff);
 88     Imrect *z;
 89 
 90     z = zbuff->z = im_alloc(height, width, (Imregion *) NULL, int_v);
 91     for (i = 0; i < height; ++i)
 92         for (j = 0; j < width; ++j)
 93             IM_INT(z, i, j) = MAXINT;
 94 
 95     zbuff->zmin = zmin;
 96     zbuff->zmax = zmax;
 97     zbuff->color = grey_color;
 98     zbuff->shade = spec_shade;
 99     zbuff->backdraw = true;
100     return (zbuff);
101 }
102 
103 void    zbuff_free(Tv_zbuff * zbuff)
104 {
105     if (zbuff == NULL)
106         return;
107     im_free(zbuff->z);
108     rfree((void *) zbuff);
109 }
110 
111 /* give tv a z-buffer z-range zmin to zmax */
112 void    tv_set_zbuff(Tv * tv, double zmin, double zmax)
113 {
114     if (tv == NULL)
115         return;
116     zbuff_free(tv->zbuff);
117     tv->zbuff = zbuff_alloc(tv->height, tv->width, zmin, zmax);
118 }
119 
120 void    tv_free_zbuff(Tv * tv)
121 {
122     if (tv == NULL)
123         return;
124     zbuff_free(tv->zbuff);
125     tv->zbuff = NULL;
126 }
127 
128 /* clear depths from z-buffer */
129 void    tv_clear_zbuff(Tv * tv)
130 {
131     int     i, j;
132     Imrect *z;
133 
134     if (tv == NULL || tv->zbuff == NULL || (z = tv->zbuff->z) == NULL)
135         return;
136     for (i = 0; i < z->height; ++i)
137         for (j = 0; j < z->width; ++j)
138             IM_INT(z, i, j) = MAXINT;
139 }
140 
141 /* whether to draw backs of surfaces (intensities < 0) */
142 void    tv_zbuff_backdraw_set(Tv * tv, Bool flag)
143 {
144     if (tv == NULL || tv->zbuff == NULL)
145         return;
146     tv->zbuff->backdraw = flag;
147 }
148 
149 /* z-buffer without drawing whether point is visible */
150 Bool    tv_zbuff(Tv * tv, Ipos pos, int z)
151 {
152     int     z0;
153     Imrect *zbuff = tv->zbuff->z;
154     int     x = ipos_x(pos);
155     int     y = ipos_y(pos);
156 
157     if (!IN_TV(tv, pos))
158         return (false);
159     z0 = IM_INT(zbuff, y, x);
160     if (z < z0)
161     {
162         IM_INT(zbuff, y, x) = z;
163         return (true);
164     } else
165         return (false);
166 }
167 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.