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

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

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

  1 /**@(#)
  2 **/
  3 #include <math.h>
  4 #include <values.h>
  5 #include <tina/sys.h>
  6 #include <tina/sysfuncs.h>
  7 #include <tina/math.h>
  8 #include <tina/mathfuncs.h>
  9 #include <tina/vision.h>
 10 #include <tina/visionfuncs.h>
 11 #include <tina/tv.h>
 12 #include <tina/tvfuncs.h>
 13 
 14 static double epsilon = 2.0e-6;
 15 
 16 void    tv_zbuff_epsilon_set(double eps)
 17 {
 18     epsilon = eps;
 19 }
 20 
 21 /* z-buffer draw 3D point */
 22 void    tv_zbuff_point3(Tv * tv, Vec3 p)
 23 {
 24     Ipos    pos = {Ipos_id};
 25     int     z;
 26 
 27     if (tv == NULL)
 28         return;
 29     pos = tv_zbuff_proj3(tv, p, &z);
 30     (void) tv_zbuff_point(tv, pos, z);
 31 }
 32 
 33 /* z-buffer draw flat 3D dot */
 34 void    tv_zbuff_dot3(Tv * tv, Vec3 p)
 35 {
 36     Ipos    pos = {Ipos_id};
 37     int     x, y, z;
 38 
 39     if (tv == NULL)
 40         return;
 41     pos = tv_zbuff_proj3(tv, p, &z);
 42     x = ipos_x(pos);
 43     y = ipos_y(pos);
 44     (void) tv_zbuff_point(tv, ipos(x - 1, y - 1), z);
 45     (void) tv_zbuff_point(tv, ipos(x, y - 1), z);
 46     (void) tv_zbuff_point(tv, ipos(x + 1, y - 1), z);
 47     (void) tv_zbuff_point(tv, ipos(x - 1, y), z);
 48     (void) tv_zbuff_point(tv, ipos(x, y), z);
 49     (void) tv_zbuff_point(tv, ipos(x + 1, y), z);
 50     (void) tv_zbuff_point(tv, ipos(x - 1, y + 1), z);
 51     (void) tv_zbuff_point(tv, ipos(x, y + 1), z);
 52     (void) tv_zbuff_point(tv, ipos(x + 1, y + 1), z);
 53 }
 54 
 55 /* z-buffer draw parametrised 3D-curve section curve(t, data) */
 56 void    tv_zbuff_curve3(Tv * tv, Vec3(*curve) (), void *data, double t1, double t2)
 57 {
 58     double  tm = 0.5 * (t1 + t2);
 59 
 60     tv_zbuff_seg3(tv, curve, data, t1, tm, false);
 61     tv_zbuff_seg3(tv, curve, data, tm, t2, true);
 62 }
 63 
 64 static Vec3 line_point(Vec3 * line, double t)
 65 {
 66     return (vec3_sum(vec3_times((1.0 - t), line[0]),
 67                      vec3_times(t, line[1])));
 68 }
 69 
 70 /* z-buffer draw line segment p1, p2 */
 71 void    tv_zbuff_line3(Tv * tv, Vec3 p1, Vec3 p2)
 72 {
 73     Vec3    line[2] = {{Vec3_id}, {Vec3_id}};
 74 
 75     line[0] = p1;
 76     line[1] = p2;
 77     tv_zbuff_curve3(tv, line_point, (void *) line, 0.0, 1.0);
 78 }
 79 
 80 /* z-buffer draw flat text string at p */
 81 void    tv_zbuff_text3(Tv * tv, char *string, Vec3 p)
 82 {
 83     void   *mask;
 84     int     x, y, xmin, xmax, ymin, ymax, z;
 85     Ipos    pos = {Ipos_id};
 86 
 87     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
 88     pos = tv_zbuff_proj3(tv, p, &z);
 89     x = ipos_x(pos);
 90     y = ipos_y(pos);
 91     xmin += x;
 92     ymin += y;
 93     xmax += x;
 94     ymax += y;
 95     mask = tv_mask_get(tv, xmin, ymin, xmax, ymax);
 96     for(x = xmin; x < xmax; x++)
 97         for(y = ymin; y < ymax; y++)
 98         {
 99             pos = ipos(x, y);
100             if(tv_zbuff(tv, pos, z))
101                 tv_mask_point(tv, mask, pos, 1); 
102         }
103     tv_mask_use(tv, mask);
104     tv_text3(tv, string, p);
105     tv_mask_free(tv, mask);
106 }
107 
108 /* z-buffer draw interpolated intensities in convex 3D quadrilateral */
109 void    tv_zbuff_interp_quad3(Tv * tv, Vec3 p00, Vec3 p10, Vec3 p01, Vec3 p11, double g00, double g10, double g01, double g11)
110 {
111     Ipos    p[4] = {{Ipos_id}, {Ipos_id}, {Ipos_id}, {Ipos_id}};
112     int     z[4];
113     double  g[4];
114 
115     p[0] = tv_zbuff_proj3(tv, p00, &z[0]);
116     p[1] = tv_zbuff_proj3(tv, p10, &z[1]);
117     p[2] = tv_zbuff_proj3(tv, p11, &z[2]);
118     p[3] = tv_zbuff_proj3(tv, p01, &z[3]);
119     g[0] = g00;
120     g[1] = g10;
121     g[3] = g01;
122     g[2] = g11;
123 
124     tv_zbuff_interp_convex_poly(tv, 4, p, z, g);
125 }
126 
127 /* z-buffer draw interpolated image intensities in convex 3D
128  * quadrilateral */
129 void    tv_zbuff_image_quad3(Tv * tv, Vec3 p00, Vec3 p10, Vec3 p01, Vec3 p11, double r00, double r10, double r01, double r11, double c00, double c10, double c01, double c11, Imrect * im)
130 {
131     Ipos    p[4] = {{Ipos_id}, {Ipos_id}, {Ipos_id}, {Ipos_id}};
132     int     z[4];
133     float   r[4];
134     float   c[4];
135 
136     p[0] = tv_zbuff_proj3(tv, p00, &z[0]);
137     p[1] = tv_zbuff_proj3(tv, p10, &z[1]);
138     p[2] = tv_zbuff_proj3(tv, p11, &z[2]);
139     p[3] = tv_zbuff_proj3(tv, p01, &z[3]);
140     r[0] = (float)r00;
141     r[1] = (float)r10;
142     r[3] = (float)r01;
143     r[2] = (float)r11;
144     c[0] = (float)c00;
145     c[1] = (float)c10;
146     c[3] = (float)c01;
147     c[2] = (float)c11;
148 
149     tv_zbuff_image_convex_poly(tv, 4, p, z, r, c, im);
150 }
151 
152 /* default formula for normal */
153 Vec3    interp_normal(Vec3(*surf) (), void *data, double u, double v, Vec3 x)
154 {
155     double  delta = epsilon / 2.0;
156     Vec3    x1 = {Vec3_id};
157     Vec3    x2 = {Vec3_id};
158 
159     x1 = surf(data, u + delta, v);
160     x2 = surf(data, u, v + delta);
161     x1 = vec3_times(1.0 / delta, vec3_diff(x1, x));
162     x2 = vec3_times(1.0 / delta, vec3_diff(x2, x));
163     return (vec3_unitcross(x1, x2));
164 }
165 
166 /* z-buffer draw  surface patch (up to limit ni sub-divisions) surf(u,
167  * v) returns surface point norm(u, v) returns surface normal range
168  * [u1, u2] by [v1, v2] nu, nv reasonable subdivisions to get patch
169  * very roughly a rectangle (or may not all be drawn) ni max number of
170  * patch subdivisions */
171 void    tv_zbuff_surf3(Tv * tv, Vec3(*surf) (), Vec3(*normal) (), void *data, double u1, double v1, double u2, double v2, int nu, int nv, int ni)
172 {
173     int     i, j;
174     double  du = (u2 - u1) / nu;
175     double  dv = (v2 - v1) / nv;
176 
177     if (u2 > u1)
178     {
179         SWAP(double, u1, u2);
180         du = -du;
181     }
182     if (v2 > v1)
183     {
184         SWAP(double, v1, v2);
185         dv = -dv;
186     }
187     if (normal == NULL)
188     {
189         u1 += epsilon;
190         v1 += epsilon;
191         u2 -= epsilon;
192         v2 -= epsilon;
193     }
194     for (i = 0; i < nu; i++)
195         for (j = 0; j < nv; j++)
196         {
197             double  u = u1 + i * du;
198             double  v = v1 + j * dv;
199 
200             tv_zbuff_patch3(tv, surf, normal, data, u, v, u + du, v + dv, ni);
201         }
202 }
203 
204 /* parametrisation of ruled surface */
205 static Vec3 ruled_point(Vec3 * r, double u, double v)
206 {
207     Vec3    p1 = {Vec3_id};
208     Vec3    p2 = {Vec3_id};
209 
210     p1 = vec3_sum(vec3_times((1.0 - u), r[0]), vec3_times(u, r[1]));
211     p2 = vec3_sum(vec3_times((1.0 - u), r[2]), vec3_times(u, r[3]));
212     return (vec3_sum(vec3_times((1.0 - v), p1), vec3_times(v, p2)));
213 }
214 
215 /* z-buffer draws ruled quadrilateral */
216 void    tv_zbuff_ruled3(Tv * tv, Vec3 p11, Vec3 p21, Vec3 p12, Vec3 p22)
217 {
218     Vec3    r[4] = {{Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}};
219 
220     r[0] = p11;
221     r[1] = p21;
222     r[2] = p12;
223     r[3] = p22;
224     tv_zbuff_patch3(tv, ruled_point, (Vec3(*) ()) NULL, (void *) r,
225                     0.0, 0.0, 1.0, 1.0, 1);
226 }
227 
228 /* bi-linear normal estimate for quadrilateral, corner normals packed
229  * into r[4]->r[7] */
230 static Vec3 gouraud_normal(Vec3 * r, double u, double v)
231 {
232     Vec3    n1 = {Vec3_id};
233     Vec3    n2 = {Vec3_id};
234 
235     n1 = vec3_sum(vec3_times((1.0 - u), r[4]), vec3_times(u, r[5]));
236     n2 = vec3_sum(vec3_times((1.0 - u), r[6]), vec3_times(u, r[7]));
237     return (vec3_unit(vec3_sum(vec3_times((1.0 - v), n1), vec3_times(v, n2))));
238 }
239 
240 /* z-buffer draw gouraud shaded quadrilateral */
241 void    tv_zbuff_gouraud(Tv * tv, Vec3 p11, Vec3 p21, Vec3 p12, Vec3 p22, Vec3 n11, Vec3 n21, Vec3 n12, Vec3 n22)
242 {
243     Vec3    r[8] = {{Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}};
244 
245     r[0] = p11;
246     r[1] = p21;
247     r[2] = p12;
248     r[3] = p22;
249     r[4] = n11;
250     r[5] = n21;
251     r[6] = n12;
252     r[7] = n22;
253     tv_zbuff_patch3(tv, ruled_point, gouraud_normal, (void *) r,
254                     0.0, 0.0, 1.0, 1.0, 1);
255 }
256 

~ [ 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.