| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Flat shader derived from texture mapper (a little slow) |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "maths.h" |
| 27 |
#include "vecmat.h" |
| 28 |
#include "gr.h" |
| 29 |
#include "grdef.h" |
| 30 |
#include "texmap.h" |
| 31 |
#include "texmapl.h" |
| 32 |
#include "scanline.h" |
| 33 |
|
| 34 |
#include "compiler-range_for.h" |
| 35 |
#include "partial_range.h" |
| 36 |
|
| 37 |
#if !DXX_USE_OGL |
| 38 |
#include "3d.h" |
| 39 |
#include "dxxerror.h" |
| 40 |
#include "d_zip.h" |
| 41 |
|
| 42 |
namespace dcx { |
| 43 |
|
| 44 |
namespace { |
| 45 |
|
| 46 |
static void gr_upoly_tmap_ylr(grs_canvas &, uint_fast32_t nverts, const int *vert, uint8_t color); |
| 47 |
|
| 48 |
// ------------------------------------------------------------------------------------- |
| 49 |
// Texture map current scanline. |
| 50 |
// Uses globals Du_dx and Dv_dx to incrementally compute u,v coordinates |
| 51 |
// ------------------------------------------------------------------------------------- |
| 52 |
static void tmap_scanline_flat(grs_canvas &canvas, int y, fix xleft, fix xright) |
| 53 |
{ |
| 54 |
if (xright < xleft) |
| 55 |
return; |
| 56 |
|
| 57 |
// setup to call assembler scanline renderer |
| 58 |
|
| 59 |
fx_y = y; |
| 60 |
fx_xleft = xleft/F1_0; // (xleft >> 16) != xleft/F1_0 for negative numbers, f2i caused random crashes |
| 61 |
fx_xright = xright/F1_0; |
| 62 |
|
| 63 |
if (canvas.cv_fade_level >= GR_FADE_OFF) |
| 64 |
c_tmap_scanline_flat(); |
| 65 |
else { |
| 66 |
c_tmap_scanline_shaded(canvas.cv_fade_level); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
//--unused-- void tmap_scanline_shaded(int y, fix xleft, fix xright) |
| 71 |
//--unused-- { |
| 72 |
//--unused-- fix dx; |
| 73 |
//--unused-- |
| 74 |
//--unused-- dx = xright - xleft; |
| 75 |
//--unused-- |
| 76 |
//--unused-- // setup to call assembler scanline renderer |
| 77 |
//--unused-- |
| 78 |
//--unused-- fx_y = y << 16; |
| 79 |
//--unused-- fx_xleft = xleft; |
| 80 |
//--unused-- fx_xright = xright; |
| 81 |
//--unused-- |
| 82 |
//--unused-- asm_tmap_scanline_shaded(); |
| 83 |
//--unused-- } |
| 84 |
|
| 85 |
// ------------------------------------------------------------------------------------- |
| 86 |
// Render a texture map. |
| 87 |
// Linear in outer loop, linear in inner loop. |
| 88 |
// ------------------------------------------------------------------------------------- |
| 89 |
static void texture_map_flat(grs_canvas &canvas, const g3ds_tmap &t, int color) |
| 90 |
{ |
| 91 |
int vlt,vrt,vlb,vrb; // vertex left top, vertex right top, vertex left bottom, vertex right bottom |
| 92 |
int topy,boty,dy; |
| 93 |
fix dx_dy_left,dx_dy_right; |
| 94 |
int max_y_vertex; |
| 95 |
fix xleft,xright; |
| 96 |
fix recip_dy; |
| 97 |
auto &v3d = t.verts; |
| 98 |
|
| 99 |
tmap_flat_color = color; |
| 100 |
|
| 101 |
// Determine top and bottom y coords. |
| 102 |
compute_y_bounds(t,vlt,vlb,vrt,vrb,max_y_vertex); |
| 103 |
|
| 104 |
// Set top and bottom (of entire texture map) y coordinates. |
| 105 |
topy = f2i(v3d[vlt].y2d); |
| 106 |
boty = f2i(v3d[max_y_vertex].y2d); |
| 107 |
|
| 108 |
// Set amount to change x coordinate for each advance to next scanline. |
| 109 |
dy = f2i(t.verts[vlb].y2d) - f2i(t.verts[vlt].y2d); |
| 110 |
recip_dy = fix_recip(dy); |
| 111 |
|
| 112 |
dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy); |
| 113 |
|
| 114 |
dy = f2i(t.verts[vrb].y2d) - f2i(t.verts[vrt].y2d); |
| 115 |
recip_dy = fix_recip(dy); |
| 116 |
|
| 117 |
dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy); |
| 118 |
|
| 119 |
// Set initial values for x, u, v |
| 120 |
xleft = v3d[vlt].x2d; |
| 121 |
xright = v3d[vrt].x2d; |
| 122 |
|
| 123 |
// scan all rows in texture map from top through first break. |
| 124 |
// @mk: Should we render the scanline for y==boty? This violates Matt's spec. |
| 125 |
|
| 126 |
for (int y = topy; y < boty; y++) { |
| 127 |
|
| 128 |
// See if we have reached the end of the current left edge, and if so, set |
| 129 |
// new values for dx_dy and x,u,v |
| 130 |
if (y == f2i(v3d[vlb].y2d)) { |
| 131 |
// Handle problem of double points. Search until y coord is different. Cannot get |
| 132 |
// hung in an infinite loop because we know there is a vertex with a lower y coordinate |
| 133 |
// because in the for loop, we don't scan all spanlines. |
| 134 |
while (y == f2i(v3d[vlb].y2d)) { |
| 135 |
vlt = vlb; |
| 136 |
vlb = prevmod(vlb,t.nv); |
| 137 |
} |
| 138 |
dy = f2i(t.verts[vlb].y2d) - f2i(t.verts[vlt].y2d); |
| 139 |
recip_dy = fix_recip(dy); |
| 140 |
|
| 141 |
dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy); |
| 142 |
|
| 143 |
xleft = v3d[vlt].x2d; |
| 144 |
} |
| 145 |
|
| 146 |
// See if we have reached the end of the current left edge, and if so, set |
| 147 |
// new values for dx_dy and x. Not necessary to set new values for u,v. |
| 148 |
if (y == f2i(v3d[vrb].y2d)) { |
| 149 |
while (y == f2i(v3d[vrb].y2d)) { |
| 150 |
vrt = vrb; |
| 151 |
vrb = succmod(vrb,t.nv); |
| 152 |
} |
| 153 |
|
| 154 |
dy = f2i(t.verts[vrb].y2d) - f2i(t.verts[vrt].y2d); |
| 155 |
recip_dy = fix_recip(dy); |
| 156 |
|
| 157 |
dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy); |
| 158 |
|
| 159 |
xright = v3d[vrt].x2d; |
| 160 |
|
| 161 |
} |
| 162 |
tmap_scanline_flat(canvas, y, xleft, xright); |
| 163 |
|
| 164 |
xleft += dx_dy_left; |
| 165 |
xright += dx_dy_right; |
| 166 |
|
| 167 |
} |
| 168 |
tmap_scanline_flat(canvas, boty, xleft, xright); |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
// ----------------------------------------------------------------------------------------- |
| 174 |
// This is the gr_upoly-like interface to the texture mapper which uses texture-mapper compatible |
| 175 |
// (ie, avoids cracking) edge/delta computation. |
| 176 |
void gr_upoly_tmap(grs_canvas &canvas, uint_fast32_t nverts, const std::array<fix, MAX_POINTS_IN_POLY * 2> &vert, const uint8_t color) |
| 177 |
{ |
| 178 |
gr_upoly_tmap_ylr(canvas, nverts, vert.data(), color); |
| 179 |
} |
| 180 |
|
| 181 |
namespace { |
| 182 |
|
| 183 |
struct pnt2d { |
| 184 |
fix x,y; |
| 185 |
}; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
//this takes the same partms as draw_tmap, but draws a flat-shaded polygon |
| 190 |
void draw_tmap_flat(grs_canvas &canvas, const grs_bitmap &bp, const std::span<const g3_draw_tmap_point *const> vertbuf) |
| 191 |
{ |
| 192 |
union { |
| 193 |
std::array<pnt2d, MAX_TMAP_VERTS> points; |
| 194 |
std::array<int, MAX_TMAP_VERTS * (sizeof(pnt2d) / sizeof(int))> ipoints; |
| 195 |
}; |
| 196 |
static_assert(sizeof(points) == sizeof(ipoints), "array size mismatch"); |
| 197 |
fix average_light; |
| 198 |
assert(vertbuf.size() < MAX_TMAP_VERTS); |
| 199 |
average_light = 0; |
| 200 |
for (const auto vb : vertbuf) |
| 201 |
average_light += vb->p3_l; |
| 202 |
|
| 203 |
if (vertbuf.size() == 4) |
| 204 |
average_light = f2i(average_light * NUM_LIGHTING_LEVELS/4); |
| 205 |
else |
| 206 |
average_light = f2i(average_light * NUM_LIGHTING_LEVELS / vertbuf.size()); |
| 207 |
|
| 208 |
if (average_light < 0) |
| 209 |
average_light = 0; |
| 210 |
else if (average_light > NUM_LIGHTING_LEVELS-1) |
| 211 |
average_light = NUM_LIGHTING_LEVELS-1; |
| 212 |
|
| 213 |
const auto color = gr_fade_table[static_cast<gr_fade_level>(average_light)][bp.avg_color]; |
| 214 |
|
| 215 |
for (auto &&[vb, pt] : zip(vertbuf, points)) |
| 216 |
{ |
| 217 |
pt.x = vb->p3_sx; |
| 218 |
pt.y = vb->p3_sy; |
| 219 |
} |
| 220 |
gr_upoly_tmap_ylr(canvas, vertbuf.size(), ipoints.data(), color); |
| 221 |
} |
| 222 |
|
| 223 |
namespace { |
| 224 |
|
| 225 |
// ----------------------------------------------------------------------------------------- |
| 226 |
//This is like gr_upoly_tmap() but instead of drawing, it calls the specified |
| 227 |
//function with ylr values |
| 228 |
static void gr_upoly_tmap_ylr(grs_canvas &canvas, uint_fast32_t nverts, const int *vert, const uint8_t color) |
| 229 |
{ |
| 230 |
g3ds_tmap my_tmap; |
| 231 |
my_tmap.nv = nverts; |
| 232 |
|
| 233 |
range_for (auto &i, partial_range(my_tmap.verts, nverts)) |
| 234 |
{ |
| 235 |
i.x2d = *vert++; |
| 236 |
i.y2d = *vert++; |
| 237 |
} |
| 238 |
texture_map_flat(canvas, my_tmap, color); |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
} |
| 244 |
#endif //!OGL |