| 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 |
* Header file for 3d library |
| 23 |
* except for functions implemented in interp.c |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#pragma once |
| 28 |
|
| 29 |
#include <cstdint> |
| 30 |
#include "dxxsconf.h" |
| 31 |
#include "dsx-ns.h" |
| 32 |
#include "maths.h" |
| 33 |
#include "vecmat.h" //the vector/matrix library |
| 34 |
#include "fwd-gr.h" |
| 35 |
#include <array> |
| 36 |
#include <span> |
| 37 |
|
| 38 |
#if DXX_USE_OGL |
| 39 |
#if defined(__APPLE__) && defined(__MACH__) |
| 40 |
#include <OpenGL/gl.h> |
| 41 |
#else |
| 42 |
#include <GL/gl.h> |
| 43 |
#endif |
| 44 |
#endif |
| 45 |
|
| 46 |
//Structure for storing u,v,light values. This structure doesn't have a |
| 47 |
//prefix because it was defined somewhere else before it was moved here |
| 48 |
struct g3s_uvl { |
| 49 |
fix u,v,l; |
| 50 |
}; |
| 51 |
|
| 52 |
//Structure for storing light color. Also uses l of g3s-uvl to add/compute mono (white) light |
| 53 |
struct g3s_lrgb { |
| 54 |
fix r,g,b; |
| 55 |
}; |
| 56 |
|
| 57 |
//clipping codes flags |
| 58 |
|
| 59 |
enum class clipping_code : uint8_t |
| 60 |
{ |
| 61 |
None = 0, |
| 62 |
off_left = 1, |
| 63 |
off_right = 2, |
| 64 |
off_bot = 4, |
| 65 |
off_top = 8, |
| 66 |
behind = 0x80, |
| 67 |
}; |
| 68 |
|
| 69 |
static constexpr clipping_code operator&(const clipping_code a, const clipping_code b) |
| 70 |
{ |
| 71 |
return static_cast<clipping_code>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b)); |
| 72 |
} |
| 73 |
|
| 74 |
static constexpr clipping_code &operator&=(clipping_code &a, const clipping_code b) |
| 75 |
{ |
| 76 |
return a = (a & b); |
| 77 |
} |
| 78 |
|
| 79 |
static constexpr clipping_code operator|(const clipping_code a, const clipping_code b) |
| 80 |
{ |
| 81 |
return static_cast<clipping_code>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b)); |
| 82 |
} |
| 83 |
|
| 84 |
static constexpr clipping_code &operator|=(clipping_code &a, const clipping_code b) |
| 85 |
{ |
| 86 |
return a = (a | b); |
| 87 |
} |
| 88 |
|
| 89 |
static constexpr clipping_code operator~(const clipping_code a) |
| 90 |
{ |
| 91 |
return static_cast<clipping_code>(~static_cast<uint8_t>(a)); |
| 92 |
} |
| 93 |
|
| 94 |
//Stucture to store clipping codes in a word |
| 95 |
struct g3s_codes { |
| 96 |
//or is low byte, and is high byte |
| 97 |
clipping_code uor{0}; |
| 98 |
clipping_code uand{0xff}; |
| 99 |
}; |
| 100 |
|
| 101 |
enum class projection_flag : uint8_t |
| 102 |
{ |
| 103 |
//flags for point structure |
| 104 |
projected = 1, //has been projected, so sx,sy valid |
| 105 |
overflow = 2, //can't project |
| 106 |
#if !DXX_USE_OGL |
| 107 |
temp_point = 4, //created during clip |
| 108 |
uvs = 8, //has uv values set |
| 109 |
ls = 16, //has lighting values set |
| 110 |
#endif |
| 111 |
}; |
| 112 |
|
| 113 |
static constexpr uint8_t operator&(const projection_flag a, const projection_flag b) |
| 114 |
{ |
| 115 |
return static_cast<uint8_t>(a) & static_cast<uint8_t>(b); |
| 116 |
} |
| 117 |
|
| 118 |
static constexpr projection_flag &operator&=(projection_flag &a, const projection_flag b) |
| 119 |
{ |
| 120 |
return a = static_cast<projection_flag>(a & b); |
| 121 |
} |
| 122 |
|
| 123 |
static constexpr projection_flag operator|(const projection_flag a, const projection_flag b) |
| 124 |
{ |
| 125 |
return static_cast<projection_flag>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b)); |
| 126 |
} |
| 127 |
|
| 128 |
static constexpr projection_flag &operator|=(projection_flag &a, const projection_flag b) |
| 129 |
{ |
| 130 |
return a = (a | b); |
| 131 |
} |
| 132 |
|
| 133 |
static constexpr projection_flag operator~(const projection_flag a) |
| 134 |
{ |
| 135 |
return static_cast<projection_flag>(~static_cast<uint8_t>(a)); |
| 136 |
} |
| 137 |
|
| 138 |
namespace dcx { |
| 139 |
|
| 140 |
struct g3_instance_context |
| 141 |
{ |
| 142 |
const vms_matrix matrix; |
| 143 |
const vms_vector position; |
| 144 |
}; |
| 145 |
|
| 146 |
/* |
| 147 |
* This stores a point that has been rotated about the Viewer's orientation |
| 148 |
* matrix. |
| 149 |
*/ |
| 150 |
struct g3_rotated_point |
| 151 |
{ |
| 152 |
struct from_relative_position {}; |
| 153 |
vms_vector p3_vec; /* viewer relative position */ |
| 154 |
g3_rotated_point(const g3_instance_context &viewer, const vms_vector &absolute_position); |
| 155 |
/* For use by rod.cpp */ |
| 156 |
g3_rotated_point(const vms_vector &relative_position, from_relative_position) : |
| 157 |
p3_vec{relative_position} |
| 158 |
{ |
| 159 |
} |
| 160 |
protected: |
| 161 |
g3_rotated_point() = default; |
| 162 |
}; |
| 163 |
|
| 164 |
/* |
| 165 |
* p3_flags tracks whether the point has been projected into screen |
| 166 |
* coordinates (`projected`) and, if so, whether the resulting coordinates are |
| 167 |
* outside the screen's viewable area (`overflow`). |
| 168 |
*/ |
| 169 |
struct g3_projected_point |
| 170 |
{ |
| 171 |
#if !DXX_USE_OGL |
| 172 |
fix p3_u, p3_v, p3_l; //u,v,l coords |
| 173 |
#endif |
| 174 |
fix p3_sx, p3_sy; //screen x&y |
| 175 |
clipping_code p3_codes; //clipping codes |
| 176 |
projection_flag p3_flags; //projected? |
| 177 |
g3_projected_point(const g3_instance_context &viewer, const vms_vector &absolute_position); |
| 178 |
explicit g3_projected_point(const g3_rotated_point &p); |
| 179 |
protected: |
| 180 |
static g3_projected_point build(const g3_rotated_point &p); |
| 181 |
g3_projected_point() = default; |
| 182 |
#if !DXX_USE_OGL |
| 183 |
g3_projected_point(const vms_vector &relative_position, g3_rotated_point::from_relative_position rel); |
| 184 |
#endif |
| 185 |
}; |
| 186 |
|
| 187 |
struct g3s_point : g3_rotated_point, g3_projected_point |
| 188 |
{ |
| 189 |
g3s_point(const g3_instance_context &viewer, const vms_vector &absolute_position); |
| 190 |
explicit g3s_point(const g3_rotated_point &); |
| 191 |
g3s_point() = default; |
| 192 |
#if !DXX_USE_OGL |
| 193 |
g3s_point(const vms_vector &relative_position, from_relative_position rel); |
| 194 |
#endif |
| 195 |
}; |
| 196 |
|
| 197 |
/* Store the frame count at which this point was last updated. If the stored |
| 198 |
* count matches the current frame generation, then the other members are valid |
| 199 |
* for this frame and do not need to be recomputed. If the stored count does |
| 200 |
* not match the current frame generation, then the data is stale, may be |
| 201 |
* invalid, and should be recomputed. |
| 202 |
* |
| 203 |
* In the unlikely case that the viewer has not moved or rotated at all since |
| 204 |
* the frame in which this was computed, stale data might match what will be |
| 205 |
* recomputed. However, this case is sufficiently uncommon that it is not |
| 206 |
* worth detecting it solely to avoid recomputing the rotation. |
| 207 |
*/ |
| 208 |
struct g3s_reusable_point : g3s_point |
| 209 |
{ |
| 210 |
uint16_t p3_last_generation; |
| 211 |
g3s_reusable_point() = default; |
| 212 |
}; |
| 213 |
|
| 214 |
#if DXX_USE_OGL |
| 215 |
using g3_draw_line_point = const g3_rotated_point; |
| 216 |
using g3_draw_sphere_point = const g3_rotated_point; |
| 217 |
using g3_draw_tmap_point = const g3_rotated_point; // also applies to g3_draw_poly |
| 218 |
#else |
| 219 |
using g3_draw_line_point = g3s_point; |
| 220 |
using g3_draw_sphere_point = g3s_point; |
| 221 |
using g3_draw_tmap_point = g3s_point; // also applies to g3_draw_poly |
| 222 |
#endif |
| 223 |
|
| 224 |
//start the frame |
| 225 |
void g3_start_frame(grs_canvas &); |
| 226 |
|
| 227 |
//set view from x,y,z, viewer matrix, and zoom. Must call one of g3_set_view_*() |
| 228 |
void g3_set_view_matrix(const vms_vector &view_pos,const vms_matrix &view_matrix,fix zoom); |
| 229 |
|
| 230 |
//end the frame |
| 231 |
#if DXX_USE_OGL |
| 232 |
#define g3_end_frame() ogl_end_frame() |
| 233 |
#else |
| 234 |
#define g3_end_frame() |
| 235 |
#endif |
| 236 |
|
| 237 |
//Instancing |
| 238 |
|
| 239 |
//instance at specified point with specified orientation |
| 240 |
[[nodiscard]] |
| 241 |
g3_instance_context g3_start_instance_matrix(); |
| 242 |
[[nodiscard]] |
| 243 |
g3_instance_context g3_start_instance_matrix(const vms_vector &pos, const vms_matrix &orient); |
| 244 |
|
| 245 |
//instance at specified point with specified orientation |
| 246 |
[[nodiscard]] |
| 247 |
g3_instance_context g3_start_instance_angles(const vms_vector &pos, const vms_angvec &angles); |
| 248 |
|
| 249 |
//pops the old context |
| 250 |
void g3_done_instance(const g3_instance_context &); |
| 251 |
|
| 252 |
//Misc utility functions: |
| 253 |
|
| 254 |
//returns true if a plane is facing the viewer. takes the unrotated surface |
| 255 |
//normal of the plane, and a point on it. The normal need not be normalized |
| 256 |
bool g3_check_normal_facing(const vms_vector &v,const vms_vector &norm); |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
//Point definition and rotation functions: |
| 261 |
|
| 262 |
namespace dcx { |
| 263 |
|
| 264 |
//rotates a point. returns codes. does not check if already rotated |
| 265 |
clipping_code g3_rotate_point(g3s_point &dest,const vms_vector &src); |
| 266 |
|
| 267 |
[[nodiscard]] |
| 268 |
static inline g3s_point g3_rotate_point(const vms_vector &src) |
| 269 |
{ |
| 270 |
g3s_point dest; |
| 271 |
return g3_rotate_point(dest, src), dest; |
| 272 |
} |
| 273 |
|
| 274 |
//projects a point |
| 275 |
void g3_project_point(g3s_point &point); |
| 276 |
|
| 277 |
//calculate the depth of a point - returns the z coord of the rotated point |
| 278 |
[[nodiscard]] |
| 279 |
fix g3_calc_point_depth(const vms_vector &pnt); |
| 280 |
|
| 281 |
#if DXX_USE_EDITOR |
| 282 |
//from a 2d point, compute the vector through that point |
| 283 |
[[nodiscard]] |
| 284 |
vms_vector g3_point_2_vec(short sx, short sy); |
| 285 |
#endif |
| 286 |
|
| 287 |
clipping_code build_g3_clipping_code_from_viewer_relative_position(const vms_vector &viewer_relative_position); |
| 288 |
|
| 289 |
//code a point. fills in the p3_codes field of the point, and returns the codes |
| 290 |
clipping_code g3_code_point(g3s_point &point); |
| 291 |
|
| 292 |
//delta rotation functions |
| 293 |
[[nodiscard]] |
| 294 |
vms_vector g3_rotate_delta_vec(const vms_vector &src); |
| 295 |
|
| 296 |
void g3_add_delta_vec(g3s_point &dest, const g3_rotated_point &src, const vms_vector &deltav); |
| 297 |
|
| 298 |
//Drawing functions: |
| 299 |
|
| 300 |
//draw a flat-shaded face. |
| 301 |
void _g3_draw_poly(grs_canvas &, std::span<g3_draw_tmap_point *const> pointlist, uint8_t color); |
| 302 |
template <std::size_t N> |
| 303 |
static inline void g3_draw_poly(grs_canvas &canvas, const uint_fast32_t nv, const std::array<g3_draw_tmap_point *, N> &pointlist, const uint8_t color) |
| 304 |
{ |
| 305 |
_g3_draw_poly(canvas, std::span(pointlist).first(nv), color); |
| 306 |
} |
| 307 |
|
| 308 |
constexpr std::integral_constant<std::size_t, 64> MAX_POINTS_PER_POLY{}; |
| 309 |
|
| 310 |
//draw a sortof sphere - i.e., the 2d radius is proportional to the 3d |
| 311 |
//radius, but not to the distance from the eye |
| 312 |
void g3_draw_sphere(grs_canvas &, g3_draw_sphere_point &pnt, fix rad, uint8_t color); |
| 313 |
|
| 314 |
#if !DXX_USE_OGL |
| 315 |
static inline void g3_draw_sphere(grs_canvas &canvas, g3_draw_sphere_point &&pnt, fix rad, uint8_t color) |
| 316 |
{ |
| 317 |
g3_draw_sphere(canvas, pnt, rad, color); |
| 318 |
} |
| 319 |
#endif |
| 320 |
|
| 321 |
//@@//return ligting value for a point |
| 322 |
//@@fix g3_compute_lighting_value(g3s_point *rotated_point,fix normval); |
| 323 |
|
| 324 |
//like g3_draw_poly(), but checks to see if facing. If surface normal is |
| 325 |
//NULL, this routine must compute it, which will be slow. It is better to |
| 326 |
//pre-compute the normal, and pass it to this function. When the normal |
| 327 |
//is passed, this function works like g3_check_normal_facing() plus |
| 328 |
//g3_draw_poly(). |
| 329 |
//returns -1 if not facing, 1 if off screen, 0 if drew |
| 330 |
bool do_facing_check(const std::array<g3_draw_tmap_point *, 3> &vertlist); |
| 331 |
|
| 332 |
//like g3_draw_poly(), but checks to see if facing. If surface normal is |
| 333 |
//NULL, this routine must compute it, which will be slow. It is better to |
| 334 |
//pre-compute the normal, and pass it to this function. When the normal |
| 335 |
//is passed, this function works like g3_check_normal_facing() plus |
| 336 |
//g3_draw_poly(). |
| 337 |
//returns -1 if not facing, 1 if off screen, 0 if drew |
| 338 |
static inline void g3_check_and_draw_poly(grs_canvas &canvas, const std::array<g3_draw_tmap_point *, 3> &pointlist, const uint8_t color) |
| 339 |
{ |
| 340 |
if (do_facing_check(pointlist)) |
| 341 |
g3_draw_poly(canvas, pointlist.size(), pointlist, color); |
| 342 |
} |
| 343 |
|
| 344 |
//draws a bitmap with the specified 3d width & height |
| 345 |
//returns 1 if off screen, 0 if drew |
| 346 |
void g3_draw_bitmap(grs_canvas &, const vms_vector &pos, fix width, fix height, grs_bitmap &bm); |
| 347 |
|
| 348 |
#if DXX_USE_OGL |
| 349 |
struct g3_draw_line_colors |
| 350 |
{ |
| 351 |
const std::array<GLfloat, 8> color_array; |
| 352 |
g3_draw_line_colors(color_palette_index color); |
| 353 |
}; |
| 354 |
#endif |
| 355 |
|
| 356 |
class g3_draw_line_context |
| 357 |
#if DXX_USE_OGL |
| 358 |
: public g3_draw_line_colors |
| 359 |
#endif |
| 360 |
{ |
| 361 |
public: |
| 362 |
grs_canvas &canvas; |
| 363 |
const color_palette_index color; |
| 364 |
g3_draw_line_context(grs_canvas &canvas, color_palette_index color) : |
| 365 |
#if DXX_USE_OGL |
| 366 |
g3_draw_line_colors{color}, |
| 367 |
#endif |
| 368 |
canvas{canvas}, color{color} |
| 369 |
{ |
| 370 |
} |
| 371 |
g3_draw_line_context(const g3_draw_line_context &) = delete; |
| 372 |
g3_draw_line_context &operator=(const g3_draw_line_context &) = delete; |
| 373 |
}; |
| 374 |
|
| 375 |
//specifies 2d drawing routines to use instead of defaults. Passing |
| 376 |
//NULL for either or both restores defaults |
| 377 |
#if DXX_USE_OGL |
| 378 |
enum class tmap_drawer_type : bool |
| 379 |
{ |
| 380 |
polygon, |
| 381 |
flat, |
| 382 |
draw_tmap = polygon, |
| 383 |
draw_tmap_flat = flat, |
| 384 |
}; |
| 385 |
|
| 386 |
using tmap_drawer_type::draw_tmap_flat; |
| 387 |
using tmap_drawer_type::draw_tmap; |
| 388 |
|
| 389 |
#else |
| 390 |
struct temporary_points_t; |
| 391 |
|
| 392 |
//draws a line. takes two points. |
| 393 |
void g3_draw_line(const g3_draw_line_context &, g3_draw_line_point &p0, g3_draw_line_point &p1, temporary_points_t &); |
| 394 |
|
| 395 |
constexpr std::integral_constant<std::size_t, 100> MAX_POINTS_IN_POLY{}; |
| 396 |
|
| 397 |
using tmap_drawer_type = void (*)(grs_canvas &, const grs_bitmap &bm, std::span<const g3_draw_tmap_point *const> vertlist); |
| 398 |
|
| 399 |
// This is the gr_upoly-like interface to the texture mapper which uses texture-mapper compatible |
| 400 |
// (ie, avoids cracking) edge/delta computation. |
| 401 |
void gr_upoly_tmap(grs_canvas &, uint_fast32_t nverts, const std::array<fix, MAX_POINTS_IN_POLY * 2> &vert, uint8_t color); |
| 402 |
#endif |
| 403 |
|
| 404 |
void g3_draw_line(const g3_draw_line_context &context, g3_draw_line_point &p0, g3_draw_line_point &p1); |
| 405 |
|
| 406 |
//draw a bitmap object that is always facing you |
| 407 |
void g3_draw_rod_tmap(grs_canvas &, grs_bitmap &bitmap, const g3_rotated_point &bot_point, fix bot_width, const g3_rotated_point &top_point, fix top_width, g3s_lrgb light, tmap_drawer_type tmap_drawer_ptr); |
| 408 |
|
| 409 |
//draw a texture-mapped face. |
| 410 |
void _g3_draw_tmap(grs_canvas &canvas, std::span<g3_draw_tmap_point *const> pointlist, const g3s_uvl *uvl_list, const g3s_lrgb *light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr); |
| 411 |
|
| 412 |
template <std::size_t N> |
| 413 |
static inline void g3_draw_tmap(grs_canvas &canvas, unsigned nv, const std::array<g3_draw_tmap_point *, N> &pointlist, const std::array<g3s_uvl, N> &uvl_list, const std::array<g3s_lrgb, N> &light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr) |
| 414 |
{ |
| 415 |
static_assert(N <= MAX_POINTS_PER_POLY, "too many points in tmap"); |
| 416 |
#ifdef DXX_CONSTANT_TRUE |
| 417 |
if (DXX_CONSTANT_TRUE(nv > N)) |
| 418 |
DXX_ALWAYS_ERROR_FUNCTION("reading beyond array"); |
| 419 |
#endif |
| 420 |
if (nv > N) |
| 421 |
return; |
| 422 |
_g3_draw_tmap(canvas, std::span(pointlist).first(nv), uvl_list.data(), light_rgb.data(), bm, tmap_drawer_ptr); |
| 423 |
} |
| 424 |
|
| 425 |
template <std::size_t N> |
| 426 |
requires(N <= MAX_POINTS_PER_POLY) |
| 427 |
static inline void g3_draw_tmap(grs_canvas &canvas, const std::array<g3_draw_tmap_point *, N> &pointlist, const std::array<g3s_uvl, N> &uvl_list, const std::array<g3s_lrgb, N> &light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr) |
| 428 |
{ |
| 429 |
_g3_draw_tmap(canvas, pointlist, uvl_list.data(), light_rgb.data(), bm, tmap_drawer_ptr); |
| 430 |
} |
| 431 |
|
| 432 |
template <std::size_t N> |
| 433 |
static inline void g3_check_and_draw_tmap(grs_canvas &canvas, unsigned nv, const std::array<g3_draw_tmap_point *, N> &pointlist, const std::array<g3s_uvl, N> &uvl_list, const std::array<g3s_lrgb, N> &light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr) |
| 434 |
{ |
| 435 |
if (do_facing_check(pointlist)) |
| 436 |
g3_draw_tmap(canvas, nv, pointlist, uvl_list, light_rgb, bm, tmap_drawer_ptr); |
| 437 |
} |
| 438 |
|
| 439 |
template <std::size_t N> |
| 440 |
static inline void g3_check_and_draw_tmap(grs_canvas &canvas, const std::array<g3_draw_tmap_point *, N> &pointlist, const std::array<g3s_uvl, N> &uvl_list, const std::array<g3s_lrgb, N> &light_rgb, grs_bitmap &bm, const tmap_drawer_type tmap_drawer_ptr) |
| 441 |
{ |
| 442 |
g3_check_and_draw_tmap(canvas, N, pointlist, uvl_list, light_rgb, bm, tmap_drawer_ptr); |
| 443 |
} |
| 444 |
|
| 445 |
} |