| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "3d.h" |
| 9 |
#include "globvars.h" |
| 10 |
#include "clipper.h" |
| 11 |
#include "dxxerror.h" |
| 12 |
|
| 13 |
#include "compiler-range_for.h" |
| 14 |
#include <stdexcept> |
| 15 |
|
| 16 |
namespace dcx { |
| 17 |
|
| 18 |
#if !DXX_USE_OGL |
| 19 |
temporary_points_t::temporary_points_t() = default; |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
static g3s_point &get_temp_point(temporary_points_t &t) |
| 24 |
{ |
| 25 |
auto &p{t.free_points_used |
| 26 |
? *t.free_points.at(--t.free_points_used) |
| 27 |
: t.temp_points.at(t.temporary_points_used++) |
| 28 |
}; |
| 29 |
p.p3_flags = projection_flag::temp_point; |
| 30 |
return p; |
| 31 |
} |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
void temporary_points_t::free_temp_point(g3s_point &p) |
| 36 |
{ |
| 37 |
if (!(p.p3_flags & projection_flag::temp_point)) |
| 38 |
throw std::invalid_argument("freeing non-temporary point"); |
| 39 |
p.p3_flags &= ~projection_flag::temp_point; |
| 40 |
free_points.at(free_points_used++) = &p; |
| 41 |
} |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
//clips an edge against one plane. |
| 46 |
static g3s_point &clip_edge(const clipping_code plane_flag, g3s_point *on_pnt, g3s_point *off_pnt, temporary_points_t &tp) |
| 47 |
{ |
| 48 |
fix psx_ratio; |
| 49 |
fix a,b,kn,kd; |
| 50 |
|
| 51 |
//compute clipping value k = (xs-zs) / (xs-xe-zs+ze) |
| 52 |
//use x or y as appropriate, and negate x/y value as appropriate |
| 53 |
|
| 54 |
if ((plane_flag & (clipping_code::off_right | clipping_code::off_left)) != clipping_code::None) |
| 55 |
{ |
| 56 |
a = on_pnt->p3_vec.x; |
| 57 |
b = off_pnt->p3_vec.x; |
| 58 |
} |
| 59 |
else { |
| 60 |
a = on_pnt->p3_vec.y; |
| 61 |
b = off_pnt->p3_vec.y; |
| 62 |
} |
| 63 |
|
| 64 |
if ((plane_flag & (clipping_code::off_left | clipping_code::off_bot)) != clipping_code::None) |
| 65 |
{ |
| 66 |
a = -a; |
| 67 |
b = -b; |
| 68 |
} |
| 69 |
|
| 70 |
kn = a - on_pnt->p3_vec.z; //xs-zs |
| 71 |
kd = kn - b + off_pnt->p3_vec.z; //xs-zs-xe+ze |
| 72 |
|
| 73 |
auto &tmp = get_temp_point(tp); |
| 74 |
|
| 75 |
psx_ratio = fixdiv( kn, kd ); |
| 76 |
tmp.p3_vec.x = on_pnt->p3_vec.x + fixmul( (off_pnt->p3_vec.x-on_pnt->p3_vec.x), psx_ratio); |
| 77 |
tmp.p3_vec.y = on_pnt->p3_vec.y + fixmul( (off_pnt->p3_vec.y-on_pnt->p3_vec.y), psx_ratio); |
| 78 |
|
| 79 |
if ((plane_flag & (clipping_code::off_top | clipping_code::off_bot)) != clipping_code::None) |
| 80 |
tmp.p3_vec.z = tmp.p3_vec.y; |
| 81 |
else |
| 82 |
tmp.p3_vec.z = tmp.p3_vec.x; |
| 83 |
|
| 84 |
if ((plane_flag & (clipping_code::off_left | clipping_code::off_bot)) != clipping_code::None) |
| 85 |
tmp.p3_vec.z = -tmp.p3_vec.z; |
| 86 |
|
| 87 |
if (on_pnt->p3_flags & projection_flag::uvs) { |
| 88 |
// PSX_HACK!!!! |
| 89 |
// tmp.p3_u = on_pnt->p3_u + fixmuldiv(off_pnt->p3_u-on_pnt->p3_u,kn,kd); |
| 90 |
// tmp.p3_v = on_pnt->p3_v + fixmuldiv(off_pnt->p3_v-on_pnt->p3_v,kn,kd); |
| 91 |
tmp.p3_u = on_pnt->p3_u + fixmul((off_pnt->p3_u-on_pnt->p3_u), psx_ratio); |
| 92 |
tmp.p3_v = on_pnt->p3_v + fixmul((off_pnt->p3_v-on_pnt->p3_v), psx_ratio); |
| 93 |
|
| 94 |
tmp.p3_flags |= projection_flag::uvs; |
| 95 |
} |
| 96 |
|
| 97 |
if (on_pnt->p3_flags & projection_flag::ls) { |
| 98 |
// PSX_HACK |
| 99 |
// tmp.p3_r = on_pnt->p3_r + fixmuldiv(off_pnt->p3_r-on_pnt->p3_r,kn,kd); |
| 100 |
// tmp.p3_g = on_pnt->p3_g + fixmuldiv(off_pnt->p3_g-on_pnt->p3_g,kn,kd); |
| 101 |
// tmp.p3_b = on_pnt->p3_b + fixmuldiv(off_pnt->p3_b-on_pnt->p3_b,kn,kd); |
| 102 |
tmp.p3_l = on_pnt->p3_l + fixmul((off_pnt->p3_l-on_pnt->p3_l), psx_ratio); |
| 103 |
tmp.p3_flags |= projection_flag::ls; |
| 104 |
} |
| 105 |
g3_code_point(tmp); |
| 106 |
return tmp; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
//clips a line to the viewing pyramid. |
| 112 |
void clip_line(g3_draw_line_point *&p0, g3_draw_line_point *&p1, const clipping_code codes_or, temporary_points_t &tp) |
| 113 |
{ |
| 114 |
//might have these left over |
| 115 |
p0->p3_flags &= ~(projection_flag::uvs|projection_flag::ls); |
| 116 |
p1->p3_flags &= ~(projection_flag::uvs|projection_flag::ls); |
| 117 |
|
| 118 |
for (uint8_t plane_step = 1; plane_step < 16; plane_step <<= 1) |
| 119 |
{ |
| 120 |
const clipping_code plane_flag{plane_step}; |
| 121 |
if ((codes_or & plane_flag) != clipping_code::None) |
| 122 |
{ |
| 123 |
if ((p0->p3_codes & plane_flag) != clipping_code::None) |
| 124 |
std::swap(p0, p1); |
| 125 |
auto &old_p1 = *std::exchange(p1, &clip_edge(plane_flag, p0, p1, tp)); |
| 126 |
if (old_p1.p3_flags & projection_flag::temp_point) |
| 127 |
tp.free_temp_point(old_p1); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
namespace { |
| 133 |
|
| 134 |
static int clip_plane(const clipping_code plane_flag, polygon_clip_points &src, polygon_clip_points &dest, std::size_t *const nv, g3s_codes *const cc, temporary_points_t &tp) |
| 135 |
{ |
| 136 |
//copy first two verts to end |
| 137 |
src[*nv] = src[0]; |
| 138 |
src[*nv+1] = src[1]; |
| 139 |
|
| 140 |
*cc = {}; |
| 141 |
|
| 142 |
uint_fast32_t j{0}; |
| 143 |
for (int i=1;i<=*nv;i++) { |
| 144 |
|
| 145 |
if ((src[i]->p3_codes & plane_flag) != clipping_code::None) |
| 146 |
{ //cur point off? |
| 147 |
|
| 148 |
if ((src[i-1]->p3_codes & plane_flag) == clipping_code::None) |
| 149 |
{ //prev not off? |
| 150 |
dest[j] = &clip_edge(plane_flag,src[i-1],src[i],tp); |
| 151 |
cc->uor |= dest[j]->p3_codes; |
| 152 |
cc->uand &= dest[j]->p3_codes; |
| 153 |
++j; |
| 154 |
} |
| 155 |
|
| 156 |
if ((src[i+1]->p3_codes & plane_flag) == clipping_code::None) |
| 157 |
{ |
| 158 |
dest[j] = &clip_edge(plane_flag,src[i+1],src[i],tp); |
| 159 |
cc->uor |= dest[j]->p3_codes; |
| 160 |
cc->uand &= dest[j]->p3_codes; |
| 161 |
++j; |
| 162 |
} |
| 163 |
|
| 164 |
//see if must free discarded point |
| 165 |
|
| 166 |
if (src[i]->p3_flags & projection_flag::temp_point) |
| 167 |
tp.free_temp_point(*src[i]); |
| 168 |
} |
| 169 |
else { //cur not off, copy to dest buffer |
| 170 |
|
| 171 |
dest[j++] = src[i]; |
| 172 |
|
| 173 |
cc->uor |= src[i]->p3_codes; |
| 174 |
cc->uand &= src[i]->p3_codes; |
| 175 |
} |
| 176 |
} |
| 177 |
return j; |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
const polygon_clip_points &clip_polygon(polygon_clip_points &rsrc, polygon_clip_points &rdest, std::size_t *nv, g3s_codes *const cc, temporary_points_t &tp) |
| 183 |
{ |
| 184 |
polygon_clip_points *src = &rsrc, *dest = &rdest; |
| 185 |
for (uint8_t plane_step = 1; plane_step < 16; plane_step <<= 1) |
| 186 |
{ |
| 187 |
const clipping_code plane_flag{plane_step}; |
| 188 |
if ((cc->uor & plane_flag) != clipping_code::None) |
| 189 |
{ |
| 190 |
*nv = clip_plane(plane_flag,*src,*dest,nv,cc,tp); |
| 191 |
if (cc->uand != clipping_code::None) //clipped away |
| 192 |
return *dest; |
| 193 |
|
| 194 |
std::swap(src, dest); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return *src; //we swapped after we copied |
| 199 |
} |
| 200 |
#endif |
| 201 |
|
| 202 |
} |