2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 * $Source: /cvs/cvsroot/d2x/texmap/tmapflat.c,v $
17 * $Date: 2001-01-31 15:18:05 $
19 * Flat shader derived from texture mapper (a little slow)
21 * $Log: not supported by cvs2svn $
22 * Revision 1.2 2001/01/31 14:04:46 bradleyb
23 * Fix compiler warnings
25 * Revision 1.1.1.1 2001/01/19 03:30:16 bradleyb
28 * Revision 1.2 1999/10/07 21:03:29 donut
29 * OGL rendering of cloaked stuff
31 * Revision 1.1.1.1 1999/06/14 22:14:10 donut
32 * Import of d1x 1.37 source.
34 * Revision 1.13 1995/02/20 18:23:24 john
35 * Added new module for C versions of inner loops.
37 * Revision 1.12 1995/02/20 17:09:17 john
38 * Added code so that you can build the tmapper with no assembly!
40 * Revision 1.11 1994/11/30 00:58:01 mike
43 * Revision 1.10 1994/11/28 13:34:32 mike
46 * Revision 1.9 1994/11/19 15:21:46 mike
47 * rip out unused code.
49 * Revision 1.8 1994/11/12 16:41:41 mike
50 * *** empty log message ***
52 * Revision 1.7 1994/11/09 23:05:12 mike
53 * do lighting on texture maps which get flat shaded instead.
55 * Revision 1.6 1994/10/06 19:53:07 matt
56 * Added function that takes same parms as draw_tmap(), but renders flat
58 * Revision 1.5 1994/10/06 18:38:12 john
59 * Added the ability to fade a scanline by calling gr_upoly_tmap
60 * with Gr_scanline_darkening_level with a value < MAX_FADE_LEVELS.
62 * Revision 1.4 1994/05/25 18:46:32 matt
63 * Added gr_upoly_tmap_ylr(), which generates ylr's for a polygon
65 * Revision 1.3 1994/04/08 16:25:58 mike
66 * Comment out some includes (of header files)
67 * call init_interface_vars_to_assembler.
69 * Revision 1.2 1994/03/31 08:33:44 mike
70 * Fixup flat shading version of texture mapper (get it?)
71 * (Or maybe not, I admit to not testing my code...hahahah!)
73 * Revision 1.1 1993/09/08 17:29:10 mike
93 //#include "tmapext.h"
97 void (*scanline_func)(int,fix,fix);
99 // -------------------------------------------------------------------------------------
100 // Texture map current scanline.
101 // Uses globals Du_dx and Dv_dx to incrementally compute u,v coordinates
102 // -------------------------------------------------------------------------------------
103 void tmap_scanline_flat(int y, fix xleft, fix xright)
108 // setup to call assembler scanline renderer
111 fx_xleft = f2i(xleft);
112 fx_xright = f2i(xright);
114 if ( Gr_scanline_darkening_level >= GR_FADE_LEVELS )
116 c_tmap_scanline_flat();
118 asm_tmap_scanline_flat();
121 tmap_flat_shade_value = Gr_scanline_darkening_level;
123 c_tmap_scanline_shaded();
125 asm_tmap_scanline_shaded();
131 //--unused-- void tmap_scanline_shaded(int y, fix xleft, fix xright)
135 //--unused-- dx = xright - xleft;
137 //--unused-- // setup to call assembler scanline renderer
139 //--unused-- fx_y = y << 16;
140 //--unused-- fx_xleft = xleft;
141 //--unused-- fx_xright = xright;
143 //--unused-- asm_tmap_scanline_shaded();
147 // -------------------------------------------------------------------------------------
148 // Render a texture map.
149 // Linear in outer loop, linear in inner loop.
150 // -------------------------------------------------------------------------------------
151 void texture_map_flat(g3ds_tmap *t, int color)
153 int vlt,vrt,vlb,vrb; // vertex left top, vertex right top, vertex left bottom, vertex right bottom
155 fix dx_dy_left,dx_dy_right;
163 tmap_flat_color = color;
165 // Determine top and bottom y coords.
166 compute_y_bounds(t,&vlt,&vlb,&vrt,&vrb,&max_y_vertex);
168 // Set top and bottom (of entire texture map) y coordinates.
169 topy = f2i(v3d[vlt].y2d);
170 boty = f2i(v3d[max_y_vertex].y2d);
172 // Set amount to change x coordinate for each advance to next scanline.
173 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
174 if (dy < FIX_RECIP_TABLE_SIZE)
175 recip_dy = fix_recip[dy];
179 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy);
181 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
182 if (dy < FIX_RECIP_TABLE_SIZE)
183 recip_dy = fix_recip[dy];
187 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy);
189 // Set initial values for x, u, v
190 xleft = v3d[vlt].x2d;
191 xright = v3d[vrt].x2d;
193 // scan all rows in texture map from top through first break.
194 // @mk: Should we render the scanline for y==boty? This violates Matt's spec.
196 for (y = topy; y < boty; y++) {
198 // See if we have reached the end of the current left edge, and if so, set
199 // new values for dx_dy and x,u,v
200 if (y == f2i(v3d[vlb].y2d)) {
201 // Handle problem of double points. Search until y coord is different. Cannot get
202 // hung in an infinite loop because we know there is a vertex with a lower y coordinate
203 // because in the for loop, we don't scan all spanlines.
204 while (y == f2i(v3d[vlb].y2d)) {
206 vlb = prevmod(vlb,t->nv);
208 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
209 if (dy < FIX_RECIP_TABLE_SIZE)
210 recip_dy = fix_recip[dy];
214 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy);
216 xleft = v3d[vlt].x2d;
219 // See if we have reached the end of the current left edge, and if so, set
220 // new values for dx_dy and x. Not necessary to set new values for u,v.
221 if (y == f2i(v3d[vrb].y2d)) {
222 while (y == f2i(v3d[vrb].y2d)) {
224 vrb = succmod(vrb,t->nv);
227 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
228 if (dy < FIX_RECIP_TABLE_SIZE)
229 recip_dy = fix_recip[dy];
233 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy);
235 xright = v3d[vrt].x2d;
239 //tmap_scanline_flat(y, xleft, xright);
240 (*scanline_func)(y, xleft, xright);
243 xright += dx_dy_right;
246 //tmap_scanline_flat(y, xleft, xright);
247 (*scanline_func)(y, xleft, xright);
251 // -----------------------------------------------------------------------------------------
252 // This is the gr_upoly-like interface to the texture mapper which uses texture-mapper compatible
253 // (ie, avoids cracking) edge/delta computation.
254 void gr_upoly_tmap(int nverts, int *vert )
256 gr_upoly_tmap_ylr(nverts, vert, tmap_scanline_flat);
262 typedef struct pnt2d {
267 #pragma off (unreferenced) //bp not referenced
270 //this takes the same partms as draw_tmap, but draws a flat-shaded polygon
271 void draw_tmap_flat(grs_bitmap *bp,int nverts,g3s_point **vertbuf)
273 pnt2d points[MAX_TMAP_VERTS];
278 Assert(nverts < MAX_TMAP_VERTS);
280 average_light = vertbuf[0]->p3_l;
281 for (i=1; i<nverts; i++)
282 average_light += vertbuf[i]->p3_l;
285 average_light = f2i(average_light * NUM_LIGHTING_LEVELS/4);
287 average_light = f2i(average_light * NUM_LIGHTING_LEVELS/nverts);
289 if (average_light < 0)
291 else if (average_light > NUM_LIGHTING_LEVELS-1)
292 average_light = NUM_LIGHTING_LEVELS-1;
294 color = gr_fade_table[average_light*256 + bp->avg_color];
297 for (i=0;i<nverts;i++) {
298 points[i].x = vertbuf[i]->p3_sx;
299 points[i].y = vertbuf[i]->p3_sy;
302 gr_upoly_tmap(nverts,(int *) points);
306 #pragma on (unreferenced)
309 // -----------------------------------------------------------------------------------------
310 //This is like gr_upoly_tmap() but instead of drawing, it calls the specified
311 //function with ylr values
312 void gr_upoly_tmap_ylr(int nverts, int *vert, void (*ylr_func)(int,fix,fix) )
317 //--now called from g3_start_frame-- init_interface_vars_to_assembler();
321 for (i=0; i<nverts; i++) {
322 my_tmap.verts[i].x2d = *vert++;
323 my_tmap.verts[i].y2d = *vert++;
326 scanline_func = ylr_func;
328 texture_map_flat(&my_tmap, COLOR);