2018-08-23 02:46:40 +02:00
|
|
|
#ifndef RAYCASTLIB_H
|
|
|
|
#define RAYCASTLIB_H
|
|
|
|
|
2018-08-21 13:49:45 +02:00
|
|
|
/**
|
2018-09-17 17:55:53 +02:00
|
|
|
raycastlib (RCL) - Small C header-only raycasting library for embedded and
|
|
|
|
low performance computers, such as Arduino. Only uses integer math and stdint
|
2018-09-01 07:27:17 +02:00
|
|
|
standard library.
|
|
|
|
|
2018-09-14 12:25:59 +02:00
|
|
|
Check the defines below to fine-tune accuracy vs performance! Don't forget
|
|
|
|
to compile with optimizations.
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
Before including the library define RCL_PIXEL_FUNCTION to the name of the
|
|
|
|
function (with RCL_PixelFunction signature) that will render your pixels!
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
- All public (and most private) library identifiers start with RCL_.
|
2018-08-21 13:49:45 +02:00
|
|
|
- Game field's bottom left corner is at [0,0].
|
2018-09-12 08:39:52 +02:00
|
|
|
- X axis goes right in the ground plane.
|
|
|
|
- Y axis goes up in the ground plane.
|
|
|
|
- Height means the Z (vertical) coordinate.
|
2018-09-17 17:55:53 +02:00
|
|
|
- Each game square is RCL_UNITS_PER_SQUARE * RCL_UNITS_PER_SQUARE points.
|
|
|
|
- Angles are in RCL_Units, 0 means pointing right (x+) and positively rotates
|
|
|
|
clockwise. A full angle has RCL_UNITS_PER_SQUARE RCL_Units.
|
2018-09-17 18:03:48 +02:00
|
|
|
- Most things are normalized with RCL_UNITS_PER_SQUARE (sin, cos, vector
|
|
|
|
unit length, texture coordinates etc.).
|
|
|
|
- Screen coordinates are normal: [0,0] = top left, x goes right, y goes down.
|
2018-09-17 17:55:53 +02:00
|
|
|
|
|
|
|
author: Miloslav "drummyfish" Ciz
|
2019-07-21 20:39:11 +02:00
|
|
|
license: CC0 1.0
|
2020-02-08 20:17:40 +01:00
|
|
|
version: 0.902
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-09-01 07:27:17 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-07-21 20:39:11 +02:00
|
|
|
#ifndef RCL_RAYCAST_TINY /** Turns on super efficient version of this library.
|
|
|
|
Only use if neccesarry, looks ugly. Also not done
|
|
|
|
yet. */
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_UNITS_PER_SQUARE 1024 /**< Number of RCL_Units in a side of a
|
|
|
|
spatial square. */
|
|
|
|
typedef int32_t RCL_Unit; /**< Smallest spatial unit, there is
|
|
|
|
RCL_UNITS_PER_SQUARE units in a square's
|
|
|
|
length. This effectively serves the purpose of
|
|
|
|
a fixed-point arithmetic. */
|
2018-09-25 08:13:01 +02:00
|
|
|
#define RCL_INFINITY 2000000000
|
2018-09-02 13:58:46 +02:00
|
|
|
#else
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_UNITS_PER_SQUARE 32
|
|
|
|
typedef int16_t RCL_Unit;
|
2018-09-25 08:13:01 +02:00
|
|
|
#define RCL_INFINITY 30000
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_USE_DIST_APPROX 2
|
2018-09-02 13:58:46 +02:00
|
|
|
#endif
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_COMPUTE_WALL_TEXCOORDS
|
|
|
|
#define RCL_COMPUTE_WALL_TEXCOORDS 1
|
2018-09-16 14:24:32 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-18 10:25:14 +02:00
|
|
|
#ifndef RCL_COMPUTE_FLOOR_TEXCOORDS
|
|
|
|
#define RCL_COMPUTE_FLOOR_TEXCOORDS 0
|
|
|
|
#endif
|
|
|
|
|
2018-09-29 09:33:51 +02:00
|
|
|
#ifndef RCL_FLOOR_TEXCOORDS_HEIGHT
|
|
|
|
#define RCL_FLOOR_TEXCOORDS_HEIGHT 0 /** If RCL_COMPUTE_FLOOR_TEXCOORDS == 1,
|
|
|
|
this says for what height level the
|
|
|
|
texture coords will be computed for
|
|
|
|
(for simplicity/performance only one
|
|
|
|
level is allowed). */
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_USE_COS_LUT
|
|
|
|
#define RCL_USE_COS_LUT 0 /**< type of look up table for cos function:
|
2018-09-12 08:39:52 +02:00
|
|
|
0: none (compute)
|
|
|
|
1: 64 items
|
2018-09-14 14:40:31 +02:00
|
|
|
2: 128 items */
|
2018-09-12 08:39:52 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_USE_DIST_APPROX
|
|
|
|
#define RCL_USE_DIST_APPROX 0 /**< What distance approximation to use:
|
2018-09-14 14:40:31 +02:00
|
|
|
0: none (compute full Euclidean distance)
|
|
|
|
1: accurate approximation
|
|
|
|
2: octagonal approximation (LQ) */
|
2018-09-14 12:25:59 +02:00
|
|
|
#endif
|
|
|
|
|
2019-08-31 18:28:02 +02:00
|
|
|
#ifndef RCL_RECTILINEAR
|
|
|
|
#define RCL_RECTILINEAR 1 /**< Whether to use rectilinear perspective (normally
|
2019-08-16 20:41:03 +02:00
|
|
|
used), or curvilinear perspective (fish eye). */
|
|
|
|
#endif
|
|
|
|
|
2018-09-22 21:53:10 +02:00
|
|
|
#ifndef RCL_TEXTURE_VERTICAL_STRETCH
|
|
|
|
#define RCL_TEXTURE_VERTICAL_STRETCH 1 /**< Whether textures should be
|
|
|
|
stretched to wall height (possibly
|
|
|
|
slightly slower if on). */
|
|
|
|
#endif
|
|
|
|
|
2018-09-21 08:48:15 +02:00
|
|
|
#ifndef RCL_COMPUTE_FLOOR_DEPTH
|
|
|
|
#define RCL_COMPUTE_FLOOR_DEPTH 1 /**< Whether depth should be computed for
|
|
|
|
floor pixels - turns this off if not
|
|
|
|
needed. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RCL_COMPUTE_CEILING_DEPTH
|
2019-08-31 03:14:30 +02:00
|
|
|
#define RCL_COMPUTE_CEILING_DEPTH 1 /**< As RCL_COMPUTE_FLOOR_DEPTH but for
|
2018-09-21 08:48:15 +02:00
|
|
|
ceiling. */
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_ROLL_TEXTURE_COORDS
|
|
|
|
#define RCL_ROLL_TEXTURE_COORDS 1 /**< Says whether rolling doors should also
|
|
|
|
roll the texture coordinates along (mostly
|
2018-09-15 11:56:20 +02:00
|
|
|
desired for doors). */
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_VERTICAL_FOV
|
|
|
|
#define RCL_VERTICAL_FOV (RCL_UNITS_PER_SQUARE / 2)
|
2018-09-05 16:26:13 +02:00
|
|
|
#endif
|
2018-09-03 16:38:49 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_HORIZONTAL_FOV
|
|
|
|
#define RCL_HORIZONTAL_FOV (RCL_UNITS_PER_SQUARE / 4)
|
2018-09-08 07:23:21 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_HORIZONTAL_FOV_HALF (RCL_HORIZONTAL_FOV / 2)
|
2018-09-08 07:23:21 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_CAMERA_COLL_RADIUS
|
|
|
|
#define RCL_CAMERA_COLL_RADIUS RCL_UNITS_PER_SQUARE / 4
|
2018-09-08 07:52:55 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_CAMERA_COLL_HEIGHT_BELOW
|
|
|
|
#define RCL_CAMERA_COLL_HEIGHT_BELOW RCL_UNITS_PER_SQUARE
|
2018-09-08 07:52:55 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_CAMERA_COLL_HEIGHT_ABOVE
|
|
|
|
#define RCL_CAMERA_COLL_HEIGHT_ABOVE (RCL_UNITS_PER_SQUARE / 3)
|
2018-09-08 07:52:55 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_CAMERA_COLL_STEP_HEIGHT
|
|
|
|
#define RCL_CAMERA_COLL_STEP_HEIGHT (RCL_UNITS_PER_SQUARE / 2)
|
2018-09-08 07:52:55 +02:00
|
|
|
#endif
|
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
#ifndef RCL_TEXTURE_INTERPOLATION_SCALE
|
|
|
|
#define RCL_TEXTURE_INTERPOLATION_SCALE 1024 /**< This says scaling of fixed
|
|
|
|
poit vertical texture coord
|
|
|
|
computation. This should be power
|
|
|
|
of two! Higher number can look more
|
|
|
|
accurate but may cause overflow. */
|
2018-09-16 17:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-25 07:48:16 +02:00
|
|
|
#define RCL_HORIZON_DEPTH (11 * RCL_UNITS_PER_SQUARE) /**< What depth the
|
|
|
|
horizon has (the floor
|
|
|
|
depth is only
|
|
|
|
approximated with the
|
|
|
|
help of this
|
|
|
|
constant). */
|
2018-09-22 14:36:40 +02:00
|
|
|
#ifndef RCL_VERTICAL_DEPTH_MULTIPLY
|
|
|
|
#define RCL_VERTICAL_DEPTH_MULTIPLY 2 /**< Defines a multiplier of height
|
|
|
|
difference when approximating floor/ceil
|
|
|
|
depth. */
|
|
|
|
#endif
|
|
|
|
|
2018-09-22 15:17:07 +02:00
|
|
|
#define RCL_min(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
#define RCL_max(a,b) ((a) > (b) ? (a) : (b))
|
2019-10-14 01:08:38 +02:00
|
|
|
#define RCL_nonZero(v) ((v) + ((v) == 0)) ///< To prevent zero divisions.
|
|
|
|
#define RCL_zeroClamp(x) ((x) * ((x) >= 0))
|
2019-10-12 16:29:13 +02:00
|
|
|
#define RCL_likely(cond) __builtin_expect(!!(cond),1)
|
|
|
|
#define RCL_unlikely(cond) __builtin_expect(!!(cond),0)
|
2018-09-22 15:17:07 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_logV2D(v)\
|
2018-08-23 03:04:52 +02:00
|
|
|
printf("[%d,%d]\n",v.x,v.y);
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_logRay(r){\
|
2018-08-23 03:04:52 +02:00
|
|
|
printf("ray:\n");\
|
|
|
|
printf(" start: ");\
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_logV2D(r.start);\
|
2018-08-23 03:04:52 +02:00
|
|
|
printf(" dir: ");\
|
2018-09-30 14:35:14 +02:00
|
|
|
RCL_logV2D(r.direction);}
|
2018-08-23 03:04:52 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_logHitResult(h){\
|
2018-08-23 03:04:52 +02:00
|
|
|
printf("hit:\n");\
|
2018-09-24 16:02:00 +02:00
|
|
|
printf(" square: ");\
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_logV2D(h.square);\
|
2018-08-23 03:04:52 +02:00
|
|
|
printf(" pos: ");\
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_logV2D(h.position);\
|
2018-08-30 08:51:53 +02:00
|
|
|
printf(" dist: %d\n", h.distance);\
|
2018-09-24 16:02:00 +02:00
|
|
|
printf(" dir: %d\n", h.direction);\
|
2018-09-30 14:35:14 +02:00
|
|
|
printf(" texcoord: %d\n", h.textureCoord);}
|
2018-08-23 03:04:52 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_logPixelInfo(p){\
|
2018-09-04 13:01:14 +02:00
|
|
|
printf("pixel:\n");\
|
|
|
|
printf(" position: ");\
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_logV2D(p.position);\
|
2018-09-30 14:35:14 +02:00
|
|
|
printf(" texCoord: ");\
|
|
|
|
RCL_logV2D(p.texCoords);\
|
2018-09-04 13:01:14 +02:00
|
|
|
printf(" depth: %d\n", p.depth);\
|
2018-09-30 14:35:14 +02:00
|
|
|
printf(" height: %d\n", p.height);\
|
2018-09-04 13:01:14 +02:00
|
|
|
printf(" wall: %d\n", p.isWall);\
|
|
|
|
printf(" hit: ");\
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_logHitResult(p.hit);\
|
2018-09-30 14:35:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RCL_logCamera(c){\
|
|
|
|
printf("camera:\n");\
|
|
|
|
printf(" position: ");\
|
|
|
|
RCL_logV2D(c.position);\
|
|
|
|
printf(" height: %d\n",c.height);\
|
|
|
|
printf(" direction: %d\n",c.direction);\
|
|
|
|
printf(" shear: %d\n",c.shear);\
|
|
|
|
printf(" resolution: %d x %d\n",c.resolution.x,c.resolution.y);\
|
|
|
|
}
|
2018-09-04 13:01:14 +02:00
|
|
|
|
2018-08-21 13:49:45 +02:00
|
|
|
/// Position in 2D space.
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit x;
|
|
|
|
RCL_Unit y;
|
|
|
|
} RCL_Vector2D;
|
2018-08-21 13:49:45 +02:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D start;
|
|
|
|
RCL_Vector2D direction;
|
|
|
|
} RCL_Ray;
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-08-22 08:21:38 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2019-08-17 13:15:57 +02:00
|
|
|
RCL_Unit distance; /**< Distance to the hit position, or -1 if no
|
2019-08-31 18:28:02 +02:00
|
|
|
collision happened. If RCL_RECTILINEAR != 0, then
|
2019-08-17 13:15:57 +02:00
|
|
|
the distance is perpendicular to the projection
|
|
|
|
plane (fish eye correction), otherwise it is
|
|
|
|
the straight distance to the ray start
|
|
|
|
position. */
|
2019-08-31 18:42:52 +02:00
|
|
|
uint8_t direction; /**< Direction of hit. The convention for angle
|
|
|
|
units is explained above. */
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit textureCoord; /**< Normalized (0 to RCL_UNITS_PER_SQUARE - 1)
|
|
|
|
texture coordinate (horizontal). */
|
|
|
|
RCL_Vector2D square; ///< Collided square coordinates.
|
|
|
|
RCL_Vector2D position; ///< Exact collision position in RCL_Units.
|
|
|
|
RCL_Unit arrayValue; /** Value returned by array function (most often
|
|
|
|
this will be the floor height). */
|
|
|
|
RCL_Unit type; /**< Integer identifying type of square (number
|
|
|
|
returned by type function, e.g. texture
|
|
|
|
index).*/
|
|
|
|
RCL_Unit doorRoll; ///< Holds value of door roll.
|
|
|
|
} RCL_HitResult;
|
2018-08-22 08:21:38 +02:00
|
|
|
|
2018-08-31 11:51:03 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D position;
|
|
|
|
RCL_Unit direction;
|
|
|
|
RCL_Vector2D resolution;
|
|
|
|
int16_t shear; /**< Shear offset in pixels (0 => no shear), can simulate
|
|
|
|
looking up/down. */
|
|
|
|
RCL_Unit height;
|
|
|
|
} RCL_Camera;
|
2018-08-31 13:29:50 +02:00
|
|
|
|
2018-09-06 14:04:36 +02:00
|
|
|
/**
|
|
|
|
Holds an information about a single rendered pixel (for a pixel function
|
2018-09-17 08:26:25 +02:00
|
|
|
that works as a fragment shader).
|
2018-09-06 14:04:36 +02:00
|
|
|
*/
|
2018-08-31 13:29:50 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D position; ///< On-screen position.
|
|
|
|
int8_t isWall; ///< Whether the pixel is a wall or a floor/ceiling.
|
|
|
|
int8_t isFloor; ///< Whether the pixel is floor or ceiling.
|
|
|
|
int8_t isHorizon; ///< If the pixel belongs to horizon segment.
|
|
|
|
RCL_Unit depth; ///< Corrected depth.
|
2019-09-28 02:08:53 +02:00
|
|
|
RCL_Unit wallHeight;///< Only for wall pixels, says its height.
|
2018-09-28 20:20:40 +02:00
|
|
|
RCL_Unit height; ///< World height (mostly for floor).
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult hit; ///< Corresponding ray hit.
|
|
|
|
RCL_Vector2D texCoords; /**< Normalized (0 to RCL_UNITS_PER_SQUARE - 1)
|
|
|
|
texture coordinates. */
|
|
|
|
} RCL_PixelInfo;
|
|
|
|
|
|
|
|
void RCL_PIXEL_FUNCTION (RCL_PixelInfo *pixel);
|
2018-09-17 07:15:06 +02:00
|
|
|
|
2018-08-31 13:29:50 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint16_t maxHits;
|
|
|
|
uint16_t maxSteps;
|
2018-09-17 17:55:53 +02:00
|
|
|
} RCL_RayConstraints;
|
2018-08-31 13:29:50 +02:00
|
|
|
|
2018-09-01 07:27:17 +02:00
|
|
|
/**
|
2018-09-06 14:04:36 +02:00
|
|
|
Function used to retrieve some information about cells of the rendered scene.
|
|
|
|
It should return a characteristic of given square as an integer (e.g. square
|
|
|
|
height, texture index, ...) - between squares that return different numbers
|
|
|
|
there is considered to be a collision.
|
2018-09-09 17:20:44 +02:00
|
|
|
|
|
|
|
This function should be as fast as possible as it will typically be called
|
|
|
|
very often.
|
2018-09-01 07:27:17 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
typedef RCL_Unit (*RCL_ArrayFunction)(int16_t x, int16_t y);
|
2019-10-11 15:02:13 +02:00
|
|
|
/*
|
|
|
|
TODO: maybe array functions should be replaced by defines of funtion names
|
|
|
|
like with pixelFunc? Could be more efficient than function pointers.
|
|
|
|
*/
|
2018-09-01 07:27:17 +02:00
|
|
|
|
2018-09-09 17:20:44 +02:00
|
|
|
/**
|
|
|
|
Function that renders a single pixel at the display. It is handed an info
|
|
|
|
about the pixel it should draw.
|
|
|
|
|
|
|
|
This function should be as fast as possible as it will typically be called
|
|
|
|
very often.
|
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
typedef void (*RCL_PixelFunction)(RCL_PixelInfo *info);
|
2018-08-31 11:51:03 +02:00
|
|
|
|
2018-09-01 07:27:17 +02:00
|
|
|
typedef void
|
2018-09-17 17:55:53 +02:00
|
|
|
(*RCL_ColumnFunction)(RCL_HitResult *hits, uint16_t hitCount, uint16_t x,
|
|
|
|
RCL_Ray ray);
|
2018-09-01 07:27:17 +02:00
|
|
|
|
2018-08-23 00:50:19 +02:00
|
|
|
/**
|
2018-09-06 14:04:36 +02:00
|
|
|
Simple-interface function to cast a single ray.
|
|
|
|
@return The first collision result.
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult RCL_castRay(RCL_Ray ray, RCL_ArrayFunction arrayFunc);
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-09-04 11:42:27 +02:00
|
|
|
/**
|
2018-09-06 14:04:36 +02:00
|
|
|
Maps a single point in the world to the screen (2D position + depth).
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_PixelInfo RCL_mapToScreen(RCL_Vector2D worldPosition, RCL_Unit height,
|
|
|
|
RCL_Camera camera);
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2018-08-23 02:36:51 +02:00
|
|
|
/**
|
2018-09-06 14:04:36 +02:00
|
|
|
Casts a single ray and returns a list of collisions.
|
2018-09-15 11:19:53 +02:00
|
|
|
|
2019-08-31 18:28:02 +02:00
|
|
|
@param ray ray to be cast, if RCL_RECTILINEAR != 0 then the computed hit
|
2019-08-17 13:15:57 +02:00
|
|
|
distance is divided by the ray direction vector length (to correct
|
|
|
|
the fish eye effect)
|
2018-09-15 11:19:53 +02:00
|
|
|
@param arrayFunc function that will be used to determine collisions (hits)
|
|
|
|
with the ray (squares for which this function returns different values
|
|
|
|
are considered to have a collision between them), this will typically
|
|
|
|
be a function returning floor height
|
|
|
|
@param typeFunc optional (can be 0) function - if provided, it will be used
|
|
|
|
to mark the hit result with the number returned by this function
|
|
|
|
(it can be e.g. a texture index)
|
|
|
|
@param hitResults array in which the hit results will be stored (has to be
|
|
|
|
preallocated with at space for at least as many hit results as
|
|
|
|
maxHits specified with the constraints parameter)
|
|
|
|
@param hitResultsLen in this variable the number of hit results will be
|
|
|
|
returned
|
|
|
|
@param constraints specifies constraints for the ray cast
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
|
|
|
|
RCL_ArrayFunction typeFunc, RCL_HitResult *hitResults,
|
|
|
|
uint16_t *hitResultsLen, RCL_RayConstraints constraints);
|
2018-08-23 02:36:51 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D RCL_angleToDirection(RCL_Unit angle);
|
2018-09-09 17:20:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Cos function.
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
@param input to cos in RCL_Units (RCL_UNITS_PER_SQUARE = 2 * pi = 360 degrees)
|
|
|
|
@return RCL_normalized output in RCL_Units (from -RCL_UNITS_PER_SQUARE to
|
|
|
|
RCL_UNITS_PER_SQUARE)
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_cosInt(RCL_Unit input);
|
2018-09-09 17:20:44 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_sinInt(RCL_Unit input);
|
2018-08-30 18:31:08 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
/// Normalizes given vector to have RCL_UNITS_PER_SQUARE length.
|
|
|
|
RCL_Vector2D RCL_normalize(RCL_Vector2D v);
|
2018-08-30 18:31:08 +02:00
|
|
|
|
|
|
|
/// Computes a cos of an angle between two vectors.
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_vectorsAngleCos(RCL_Vector2D v1, RCL_Vector2D v2);
|
2018-08-30 18:31:08 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
uint16_t RCL_sqrtInt(RCL_Unit value);
|
|
|
|
RCL_Unit RCL_dist(RCL_Vector2D p1, RCL_Vector2D p2);
|
|
|
|
RCL_Unit RCL_len(RCL_Vector2D v);
|
2018-08-23 09:25:34 +02:00
|
|
|
|
2018-08-30 08:51:53 +02:00
|
|
|
/**
|
2018-09-17 17:55:53 +02:00
|
|
|
Converts an angle in whole degrees to an angle in RCL_Units that this library
|
2018-09-06 14:04:36 +02:00
|
|
|
uses.
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_degreesToUnitsAngle(int16_t degrees);
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2019-07-21 20:39:11 +02:00
|
|
|
///< Computes the change in size of an object due to perspective.
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_perspectiveScale(RCL_Unit originalSize, RCL_Unit distance);
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_perspectiveScaleInverse(RCL_Unit originalSize,
|
|
|
|
RCL_Unit scaledSize);
|
2018-09-17 15:05:38 +02:00
|
|
|
|
2018-08-30 08:51:53 +02:00
|
|
|
/**
|
2018-09-06 14:04:36 +02:00
|
|
|
Casts rays for given camera view and for each hit calls a user provided
|
|
|
|
function.
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_castRaysMultiHit(RCL_Camera cam, RCL_ArrayFunction arrayFunc,
|
|
|
|
RCL_ArrayFunction typeFunction, RCL_ColumnFunction columnFunc,
|
|
|
|
RCL_RayConstraints constraints);
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-05 12:20:46 +02:00
|
|
|
/**
|
2018-09-21 08:48:15 +02:00
|
|
|
Using provided functions, renders a complete complex (multilevel) camera
|
|
|
|
view.
|
2018-09-05 12:20:46 +02:00
|
|
|
|
2018-09-12 17:37:15 +02:00
|
|
|
This function should render each screen pixel exactly once.
|
|
|
|
|
2018-09-17 16:45:02 +02:00
|
|
|
function rendering summary:
|
|
|
|
- performance: slower
|
|
|
|
- accuracy: higher
|
|
|
|
- wall textures: yes
|
|
|
|
- different wall heights: yes
|
2018-09-18 14:49:02 +02:00
|
|
|
- floor/ceiling textures: no
|
2018-09-17 16:45:02 +02:00
|
|
|
- floor geometry: yes, multilevel
|
|
|
|
- ceiling geometry: yes (optional), multilevel
|
|
|
|
- rolling door: no
|
2018-09-18 14:49:02 +02:00
|
|
|
- camera shearing: yes
|
2018-09-17 18:03:48 +02:00
|
|
|
- rendering order: left-to-right, not specifically ordered vertically
|
2018-09-17 16:45:02 +02:00
|
|
|
|
2018-09-06 14:04:36 +02:00
|
|
|
@param cam camera whose view to render
|
2018-09-17 17:55:53 +02:00
|
|
|
@param floorHeightFunc function that returns floor height (in RCL_Units)
|
2018-09-06 14:04:36 +02:00
|
|
|
@param ceilingHeightFunc same as floorHeightFunc but for ceiling, can also be
|
2018-09-09 17:20:44 +02:00
|
|
|
0 (no ceiling will be rendered)
|
2018-09-06 14:04:36 +02:00
|
|
|
@param typeFunction function that says a type of square (e.g. its texture
|
2018-09-05 16:26:13 +02:00
|
|
|
index), can be 0 (no type in hit result)
|
2018-09-06 14:04:36 +02:00
|
|
|
@param pixelFunc callback function to draw a single pixel on screen
|
|
|
|
@param constraints constraints for each cast ray
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-21 08:48:15 +02:00
|
|
|
void RCL_renderComplex(RCL_Camera cam, RCL_ArrayFunction floorHeightFunc,
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_ArrayFunction ceilingHeightFunc, RCL_ArrayFunction typeFunction,
|
|
|
|
RCL_RayConstraints constraints);
|
2018-08-31 18:26:51 +02:00
|
|
|
|
2018-09-11 09:07:24 +02:00
|
|
|
/**
|
|
|
|
Renders given camera view, with help of provided functions. This function is
|
2018-09-21 08:48:15 +02:00
|
|
|
simpler and faster than RCL_renderComplex(...) and is meant to be rendering
|
|
|
|
flat levels.
|
2018-09-12 17:37:15 +02:00
|
|
|
|
2018-09-17 16:45:02 +02:00
|
|
|
function rendering summary:
|
|
|
|
- performance: faster
|
|
|
|
- accuracy: lower
|
|
|
|
- wall textures: yes
|
|
|
|
- different wall heights: yes
|
2018-09-18 14:49:02 +02:00
|
|
|
- floor/ceiling textures: yes (only floor, you can mirror it for ceiling)
|
2018-09-17 16:45:02 +02:00
|
|
|
- floor geometry: no (just flat floor, with depth information)
|
|
|
|
- ceiling geometry: no (just flat ceiling, with depth information)
|
|
|
|
- rolling door: yes
|
2018-09-18 14:49:02 +02:00
|
|
|
- camera shearing: no
|
2018-09-17 18:03:48 +02:00
|
|
|
- rendering order: left-to-right, top-to-bottom
|
2018-09-17 16:45:02 +02:00
|
|
|
|
2018-09-14 19:42:59 +02:00
|
|
|
Additionally this function supports rendering rolling doors.
|
|
|
|
|
2018-09-12 17:37:15 +02:00
|
|
|
This function should render each screen pixel exactly once.
|
2018-09-14 19:42:59 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
@param rollFunc function that for given square says its door roll in
|
|
|
|
RCL_Units (0 = no roll, RCL_UNITS_PER_SQUARE = full roll right,
|
|
|
|
-RCL_UNITS_PER_SQUARE = full roll left), can be zero (no rolling door,
|
|
|
|
rendering should also be faster as fewer intersections will be tested)
|
2018-09-11 09:07:24 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_renderSimple(RCL_Camera cam, RCL_ArrayFunction floorHeightFunc,
|
|
|
|
RCL_ArrayFunction typeFunc, RCL_ArrayFunction rollFunc,
|
|
|
|
RCL_RayConstraints constraints);
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-09 17:20:44 +02:00
|
|
|
/**
|
|
|
|
Function that moves given camera and makes it collide with walls and
|
|
|
|
potentially also floor and ceilings. It's meant to help implement player
|
|
|
|
movement.
|
|
|
|
|
|
|
|
@param camera camera to move
|
|
|
|
@param planeOffset offset to move the camera in
|
|
|
|
@param heightOffset height offset to move the camera in
|
|
|
|
@param floorHeightFunc function used to retrieve the floor height
|
|
|
|
@param ceilingHeightFunc function for retrieving ceiling height, can be 0
|
|
|
|
(camera won't collide with ceiling)
|
|
|
|
@param computeHeight whether to compute height - if false (0), floor and
|
|
|
|
ceiling functions won't be used and the camera will
|
|
|
|
only collide horizontally with walls (good for simpler
|
|
|
|
game, also faster)
|
2018-09-12 07:55:29 +02:00
|
|
|
@param force if true, forces to recompute collision even if position doesn't
|
|
|
|
change
|
2018-09-09 17:20:44 +02:00
|
|
|
*/
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_moveCameraWithCollision(RCL_Camera *camera, RCL_Vector2D planeOffset,
|
|
|
|
RCL_Unit heightOffset, RCL_ArrayFunction floorHeightFunc,
|
|
|
|
RCL_ArrayFunction ceilingHeightFunc, int8_t computeHeight, int8_t force);
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_initCamera(RCL_Camera *camera);
|
|
|
|
void RCL_initRayConstraints(RCL_RayConstraints *constraints);
|
2018-09-15 14:35:21 +02:00
|
|
|
|
2018-08-21 13:49:45 +02:00
|
|
|
//=============================================================================
|
|
|
|
// privates
|
|
|
|
|
2019-09-27 03:57:12 +02:00
|
|
|
#define _RCL_UNUSED(what) (void)(what);
|
|
|
|
|
2018-09-14 20:17:15 +02:00
|
|
|
// global helper variables, for precomputing stuff etc.
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Camera _RCL_camera;
|
|
|
|
RCL_Unit _RCL_horizontalDepthStep = 0;
|
|
|
|
RCL_Unit _RCL_startFloorHeight = 0;
|
|
|
|
RCL_Unit _RCL_startCeil_Height = 0;
|
|
|
|
RCL_Unit _RCL_camResYLimit = 0;
|
|
|
|
RCL_Unit _RCL_middleRow = 0;
|
|
|
|
RCL_ArrayFunction _RCL_floorFunction = 0;
|
|
|
|
RCL_ArrayFunction _RCL_ceilFunction = 0;
|
|
|
|
RCL_Unit _RCL_fHorizontalDepthStart = 0;
|
|
|
|
RCL_Unit _RCL_cHorizontalDepthStart = 0;
|
|
|
|
int16_t _RCL_cameraHeightScreen = 0;
|
|
|
|
RCL_ArrayFunction _RCL_rollFunction = 0; // says door rolling
|
2018-09-18 10:25:14 +02:00
|
|
|
RCL_Unit *_RCL_floorPixelDistances = 0;
|
2018-09-14 20:17:15 +02:00
|
|
|
|
2018-09-21 09:20:57 +02:00
|
|
|
#ifdef RCL_PROFILE
|
2018-08-31 15:38:02 +02:00
|
|
|
// function call counters for profiling
|
2018-09-17 17:55:53 +02:00
|
|
|
uint32_t profile_RCL_sqrtInt = 0;
|
|
|
|
uint32_t profile_RCL_clamp = 0;
|
|
|
|
uint32_t profile_RCL_cosInt = 0;
|
|
|
|
uint32_t profile_RCL_angleToDirection = 0;
|
|
|
|
uint32_t profile_RCL_dist = 0;
|
|
|
|
uint32_t profile_RCL_len = 0;
|
2019-10-06 21:20:40 +02:00
|
|
|
uint32_t profile_RCL_pointIsLeftOfRay = 0;
|
2018-09-17 17:55:53 +02:00
|
|
|
uint32_t profile_RCL_castRayMultiHit = 0;
|
|
|
|
uint32_t profile_RCL_castRay = 0;
|
|
|
|
uint32_t profile_RCL_absVal = 0;
|
|
|
|
uint32_t profile_RCL_normalize = 0;
|
|
|
|
uint32_t profile_RCL_vectorsAngleCos = 0;
|
|
|
|
uint32_t profile_RCL_perspectiveScale = 0;
|
|
|
|
uint32_t profile_RCL_wrap = 0;
|
|
|
|
uint32_t profile_RCL_divRoundDown = 0;
|
|
|
|
#define RCL_profileCall(c) profile_##c += 1
|
2018-08-31 15:38:02 +02:00
|
|
|
|
|
|
|
#define printProfile() {\
|
|
|
|
printf("profile:\n");\
|
2018-09-17 17:55:53 +02:00
|
|
|
printf(" RCL_sqrtInt: %d\n",profile_RCL_sqrtInt);\
|
|
|
|
printf(" RCL_clamp: %d\n",profile_RCL_clamp);\
|
|
|
|
printf(" RCL_cosInt: %d\n",profile_RCL_cosInt);\
|
|
|
|
printf(" RCL_angleToDirection: %d\n",profile_RCL_angleToDirection);\
|
|
|
|
printf(" RCL_dist: %d\n",profile_RCL_dist);\
|
|
|
|
printf(" RCL_len: %d\n",profile_RCL_len);\
|
2019-10-06 21:20:40 +02:00
|
|
|
printf(" RCL_pointIsLeftOfRay: %d\n",profile_RCL_pointIsLeftOfRay);\
|
2018-09-17 17:55:53 +02:00
|
|
|
printf(" RCL_castRayMultiHit : %d\n",profile_RCL_castRayMultiHit);\
|
|
|
|
printf(" RCL_castRay: %d\n",profile_RCL_castRay);\
|
|
|
|
printf(" RCL_normalize: %d\n",profile_RCL_normalize);\
|
|
|
|
printf(" RCL_vectorsAngleCos: %d\n",profile_RCL_vectorsAngleCos);\
|
|
|
|
printf(" RCL_absVal: %d\n",profile_RCL_absVal);\
|
|
|
|
printf(" RCL_perspectiveScale: %d\n",profile_RCL_perspectiveScale);\
|
|
|
|
printf(" RCL_wrap: %d\n",profile_RCL_wrap);\
|
|
|
|
printf(" RCL_divRoundDown: %d\n",profile_RCL_divRoundDown); }
|
2018-08-31 15:38:02 +02:00
|
|
|
#else
|
2018-09-17 17:55:53 +02:00
|
|
|
#define RCL_profileCall(c)
|
2018-08-31 15:38:02 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_clamp(RCL_Unit value, RCL_Unit valueMin, RCL_Unit valueMax)
|
2018-08-23 03:04:52 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_clamp);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-04 19:32:47 +02:00
|
|
|
if (value >= valueMin)
|
|
|
|
{
|
|
|
|
if (value <= valueMax)
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
return valueMax;
|
|
|
|
}
|
|
|
|
else
|
2018-08-23 03:04:52 +02:00
|
|
|
return valueMin;
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
static inline RCL_Unit RCL_absVal(RCL_Unit value)
|
2018-09-03 08:14:36 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_absVal);
|
2018-09-04 18:55:18 +02:00
|
|
|
|
2019-10-12 01:49:35 +02:00
|
|
|
return value * (((value >= 0) << 1) - 1);
|
2018-09-03 08:14:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-03 16:55:30 +02:00
|
|
|
/// Like mod, but behaves differently for negative values.
|
2018-09-17 17:55:53 +02:00
|
|
|
static inline RCL_Unit RCL_wrap(RCL_Unit value, RCL_Unit mod)
|
2018-09-03 16:55:30 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_wrap);
|
2019-10-12 16:29:13 +02:00
|
|
|
RCL_Unit cmp = value < 0;
|
|
|
|
return cmp * mod + (value % mod) - cmp;
|
2018-09-03 16:55:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-03 09:52:50 +02:00
|
|
|
/// Performs division, rounding down, NOT towards zero.
|
2018-09-17 17:55:53 +02:00
|
|
|
static inline RCL_Unit RCL_divRoundDown(RCL_Unit value, RCL_Unit divisor)
|
2018-09-03 09:52:50 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_divRoundDown);
|
2018-09-04 19:32:47 +02:00
|
|
|
|
|
|
|
return value / divisor - ((value >= 0) ? 0 : 1);
|
2018-09-03 09:52:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 09:25:34 +02:00
|
|
|
// Bhaskara's cosine approximation formula
|
2018-09-17 17:55:53 +02:00
|
|
|
#define trigHelper(x) (((RCL_Unit) RCL_UNITS_PER_SQUARE) *\
|
|
|
|
(RCL_UNITS_PER_SQUARE / 2 * RCL_UNITS_PER_SQUARE / 2 - 4 * (x) * (x)) /\
|
|
|
|
(RCL_UNITS_PER_SQUARE / 2 * RCL_UNITS_PER_SQUARE / 2 + (x) * (x)))
|
2018-08-23 09:25:34 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#if RCL_USE_COS_LUT == 1
|
2018-09-15 17:53:16 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifdef RCL_RAYCAST_TINY
|
|
|
|
const RCL_Unit cosLUT[64] =
|
2018-09-15 17:53:16 +02:00
|
|
|
{
|
|
|
|
16,14,11,6,0,-6,-11,-14,-15,-14,-11,-6,0,6,11,14
|
|
|
|
};
|
|
|
|
#else
|
2018-09-17 17:55:53 +02:00
|
|
|
const RCL_Unit cosLUT[64] =
|
2018-09-15 17:53:16 +02:00
|
|
|
{
|
|
|
|
1024,1019,1004,979,946,903,851,791,724,649,568,482,391,297,199,100,0,-100,
|
|
|
|
-199,-297,-391,-482,-568,-649,-724,-791,-851,-903,-946,-979,-1004,-1019,
|
|
|
|
-1023,-1019,-1004,-979,-946,-903,-851,-791,-724,-649,-568,-482,-391,-297,
|
|
|
|
-199,-100,0,100,199,297,391,482,568,649,724,791,851,903,946,979,1004,1019
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#elif RCL_USE_COS_LUT == 2
|
|
|
|
const RCL_Unit cosLUT[128] =
|
2018-09-12 08:39:52 +02:00
|
|
|
{
|
|
|
|
1024,1022,1019,1012,1004,993,979,964,946,925,903,878,851,822,791,758,724,
|
|
|
|
687,649,609,568,526,482,437,391,344,297,248,199,150,100,50,0,-50,-100,-150,
|
|
|
|
-199,-248,-297,-344,-391,-437,-482,-526,-568,-609,-649,-687,-724,-758,-791,
|
|
|
|
-822,-851,-878,-903,-925,-946,-964,-979,-993,-1004,-1012,-1019,-1022,-1023,
|
|
|
|
-1022,-1019,-1012,-1004,-993,-979,-964,-946,-925,-903,-878,-851,-822,-791,
|
|
|
|
-758,-724,-687,-649,-609,-568,-526,-482,-437,-391,-344,-297,-248,-199,-150,
|
|
|
|
-100,-50,0,50,100,150,199,248,297,344,391,437,482,526,568,609,649,687,724,
|
|
|
|
758,791,822,851,878,903,925,946,964,979,993,1004,1012,1019,1022
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_cosInt(RCL_Unit input)
|
2018-08-23 09:25:34 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_cosInt);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
input = RCL_wrap(input,RCL_UNITS_PER_SQUARE);
|
2018-08-23 09:25:34 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#if RCL_USE_COS_LUT == 1
|
2018-09-15 17:53:16 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifdef RCL_RAYCAST_TINY
|
2018-09-15 17:53:16 +02:00
|
|
|
return cosLUT[input];
|
|
|
|
#else
|
|
|
|
return cosLUT[input / 16];
|
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#elif RCL_USE_COS_LUT == 2
|
2018-09-12 08:39:52 +02:00
|
|
|
return cosLUT[input / 8];
|
|
|
|
#else
|
2018-09-17 17:55:53 +02:00
|
|
|
if (input < RCL_UNITS_PER_SQUARE / 4)
|
2018-08-23 09:25:34 +02:00
|
|
|
return trigHelper(input);
|
2018-09-17 17:55:53 +02:00
|
|
|
else if (input < RCL_UNITS_PER_SQUARE / 2)
|
|
|
|
return -1 * trigHelper(RCL_UNITS_PER_SQUARE / 2 - input);
|
|
|
|
else if (input < 3 * RCL_UNITS_PER_SQUARE / 4)
|
|
|
|
return -1 * trigHelper(input - RCL_UNITS_PER_SQUARE / 2);
|
2018-08-23 09:25:34 +02:00
|
|
|
else
|
2018-09-17 17:55:53 +02:00
|
|
|
return trigHelper(RCL_UNITS_PER_SQUARE - input);
|
2018-09-12 08:39:52 +02:00
|
|
|
#endif
|
2018-08-23 09:25:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef trigHelper
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_sinInt(RCL_Unit input)
|
2018-08-23 09:25:34 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
return RCL_cosInt(input - RCL_UNITS_PER_SQUARE / 4);
|
2018-08-23 09:35:10 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D RCL_angleToDirection(RCL_Unit angle)
|
2018-08-23 09:35:10 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_angleToDirection);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D result;
|
2018-08-23 09:35:10 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
result.x = RCL_cosInt(angle);
|
|
|
|
result.y = -1 * RCL_sinInt(angle);
|
2018-08-23 09:35:10 +02:00
|
|
|
|
|
|
|
return result;
|
2018-08-23 09:25:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
uint16_t RCL_sqrtInt(RCL_Unit value)
|
2018-08-23 00:50:19 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_sqrtInt);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifdef RCL_RAYCAST_TINY
|
2018-09-14 15:00:31 +02:00
|
|
|
uint16_t result = 0;
|
|
|
|
uint16_t a = value;
|
|
|
|
uint16_t b = 1u << 14;
|
2018-09-02 13:58:46 +02:00
|
|
|
#else
|
2018-09-14 15:00:31 +02:00
|
|
|
uint32_t result = 0;
|
|
|
|
uint32_t a = value;
|
2018-09-13 16:13:59 +02:00
|
|
|
uint32_t b = 1u << 30;
|
2018-09-02 13:58:46 +02:00
|
|
|
#endif
|
2018-08-23 00:50:19 +02:00
|
|
|
|
|
|
|
while (b > a)
|
|
|
|
b >>= 2;
|
|
|
|
|
|
|
|
while (b != 0)
|
|
|
|
{
|
|
|
|
if (a >= result + b)
|
|
|
|
{
|
|
|
|
a -= result + b;
|
|
|
|
result = result + 2 * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
b >>= 2;
|
|
|
|
result >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_dist(RCL_Vector2D p1, RCL_Vector2D p2)
|
2018-08-23 00:50:19 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_dist);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit dx = p2.x - p1.x;
|
|
|
|
RCL_Unit dy = p2.y - p1.y;
|
2018-08-30 11:15:52 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#if RCL_USE_DIST_APPROX == 2
|
2018-09-14 14:40:31 +02:00
|
|
|
// octagonal approximation
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
dx = RCL_absVal(dx);
|
|
|
|
dy = RCL_absVal(dy);
|
2018-09-14 14:40:31 +02:00
|
|
|
|
|
|
|
return dy > dx ? dx / 2 + dy : dy / 2 + dx;
|
2018-09-17 17:55:53 +02:00
|
|
|
#elif RCL_USE_DIST_APPROX == 1
|
2018-09-14 14:40:31 +02:00
|
|
|
// more accurate approximation
|
2018-09-14 12:25:59 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit a, b, result;
|
2018-09-14 12:25:59 +02:00
|
|
|
|
2019-10-12 16:29:13 +02:00
|
|
|
dx = ((dx < 0) * 2 - 1) * dx;
|
|
|
|
dy = ((dy < 0) * 2 - 1) * dy;
|
2018-09-14 12:25:59 +02:00
|
|
|
|
|
|
|
if (dx < dy)
|
|
|
|
{
|
|
|
|
a = dy;
|
|
|
|
b = dx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a = dx;
|
|
|
|
b = dy;
|
|
|
|
}
|
|
|
|
|
2018-09-14 15:18:34 +02:00
|
|
|
result = a + (44 * b) / 102;
|
2018-09-13 17:36:42 +02:00
|
|
|
|
2018-09-14 12:25:59 +02:00
|
|
|
if (a < (b << 4))
|
2018-09-14 15:18:34 +02:00
|
|
|
result -= (5 * a) / 128;
|
|
|
|
|
|
|
|
return result;
|
2018-09-13 17:36:42 +02:00
|
|
|
#else
|
2018-08-30 11:15:52 +02:00
|
|
|
dx = dx * dx;
|
|
|
|
dy = dy * dy;
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
return RCL_sqrtInt((RCL_Unit) (dx + dy));
|
2018-09-13 17:36:42 +02:00
|
|
|
#endif
|
2018-08-23 00:50:19 +02:00
|
|
|
}
|
2018-08-21 13:49:45 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_len(RCL_Vector2D v)
|
2018-08-30 14:44:14 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_len);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D zero;
|
2018-09-14 15:18:34 +02:00
|
|
|
zero.x = 0;
|
|
|
|
zero.y = 0;
|
2018-08-30 14:44:14 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
return RCL_dist(zero,v);
|
2018-08-30 14:44:14 +02:00
|
|
|
}
|
|
|
|
|
2019-10-06 21:20:40 +02:00
|
|
|
static inline int8_t RCL_pointIsLeftOfRay(RCL_Vector2D point, RCL_Ray ray)
|
2018-08-21 13:49:45 +02:00
|
|
|
{
|
2019-10-06 21:20:40 +02:00
|
|
|
RCL_profileCall(RCL_pointIsLeftOfRay);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit dX = point.x - ray.start.x;
|
|
|
|
RCL_Unit dY = point.y - ray.start.y;
|
2018-08-21 13:49:45 +02:00
|
|
|
return (ray.direction.x * dY - ray.direction.y * dX) > 0;
|
|
|
|
// ^ Z component of cross-product
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_castRayMultiHit(RCL_Ray ray, RCL_ArrayFunction arrayFunc,
|
|
|
|
RCL_ArrayFunction typeFunc, RCL_HitResult *hitResults,
|
|
|
|
uint16_t *hitResultsLen, RCL_RayConstraints constraints)
|
2018-08-22 08:21:38 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_castRayMultiHit);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D currentPos = ray.start;
|
|
|
|
RCL_Vector2D currentSquare;
|
2018-08-23 00:50:19 +02:00
|
|
|
|
2018-09-24 16:02:00 +02:00
|
|
|
currentSquare.x = RCL_divRoundDown(ray.start.x,RCL_UNITS_PER_SQUARE);
|
2018-09-17 17:55:53 +02:00
|
|
|
currentSquare.y = RCL_divRoundDown(ray.start.y,RCL_UNITS_PER_SQUARE);
|
2018-09-02 21:43:44 +02:00
|
|
|
|
2018-08-23 02:36:51 +02:00
|
|
|
*hitResultsLen = 0;
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit squareType = arrayFunc(currentSquare.x,currentSquare.y);
|
2018-08-23 00:50:19 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
// DDA variables
|
|
|
|
RCL_Vector2D nextSideDist; // dist. from start to the next side in given axis
|
|
|
|
RCL_Vector2D delta;
|
|
|
|
RCL_Vector2D step; // -1 or 1 for each axis
|
|
|
|
int8_t stepHorizontal = 0; // whether the last step was hor. or vert.
|
2018-08-31 13:29:50 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
nextSideDist.x = 0;
|
|
|
|
nextSideDist.y = 0;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2019-08-16 20:41:03 +02:00
|
|
|
RCL_Unit dirVecLengthNorm = RCL_len(ray.direction) * RCL_UNITS_PER_SQUARE;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2019-08-16 20:41:03 +02:00
|
|
|
delta.x = RCL_absVal(dirVecLengthNorm / RCL_nonZero(ray.direction.x));
|
|
|
|
delta.y = RCL_absVal(dirVecLengthNorm / RCL_nonZero(ray.direction.y));
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
// init DDA
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
if (ray.direction.x < 0)
|
|
|
|
{
|
|
|
|
step.x = -1;
|
|
|
|
nextSideDist.x = (RCL_wrap(ray.start.x,RCL_UNITS_PER_SQUARE) * delta.x) /
|
|
|
|
RCL_UNITS_PER_SQUARE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
step.x = 1;
|
|
|
|
nextSideDist.x =
|
|
|
|
((RCL_wrap(RCL_UNITS_PER_SQUARE - ray.start.x,RCL_UNITS_PER_SQUARE)) *
|
|
|
|
delta.x) / RCL_UNITS_PER_SQUARE;
|
|
|
|
}
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
if (ray.direction.y < 0)
|
|
|
|
{
|
|
|
|
step.y = -1;
|
|
|
|
nextSideDist.y = (RCL_wrap(ray.start.y,RCL_UNITS_PER_SQUARE) * delta.y) /
|
|
|
|
RCL_UNITS_PER_SQUARE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
step.y = 1;
|
|
|
|
nextSideDist.y =
|
|
|
|
((RCL_wrap(RCL_UNITS_PER_SQUARE - ray.start.y,RCL_UNITS_PER_SQUARE)) *
|
|
|
|
delta.y) / RCL_UNITS_PER_SQUARE;
|
|
|
|
}
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
// DDA loop
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2019-10-12 16:29:13 +02:00
|
|
|
#define RECIP_SCALE 65536
|
|
|
|
|
|
|
|
RCL_Unit rayDirXRecip = RECIP_SCALE / RCL_nonZero(ray.direction.x);
|
|
|
|
RCL_Unit rayDirYRecip = RECIP_SCALE / RCL_nonZero(ray.direction.y);
|
|
|
|
// ^ we precompute reciprocals to avoid divisions in the loop
|
|
|
|
|
2018-08-31 16:46:55 +02:00
|
|
|
for (uint16_t i = 0; i < constraints.maxSteps; ++i)
|
2018-08-22 08:21:38 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit currentType = arrayFunc(currentSquare.x,currentSquare.y);
|
2018-08-23 02:36:51 +02:00
|
|
|
|
2019-10-12 16:29:13 +02:00
|
|
|
if (RCL_unlikely(currentType != squareType))
|
2018-08-23 00:50:19 +02:00
|
|
|
{
|
2018-08-23 02:36:51 +02:00
|
|
|
// collision
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult h;
|
2018-08-23 00:50:19 +02:00
|
|
|
|
2018-09-15 11:19:53 +02:00
|
|
|
h.arrayValue = currentType;
|
2018-09-14 20:17:15 +02:00
|
|
|
h.doorRoll = 0;
|
2018-08-23 02:36:51 +02:00
|
|
|
h.position = currentPos;
|
|
|
|
h.square = currentSquare;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
if (stepHorizontal)
|
|
|
|
{
|
|
|
|
h.position.x = currentSquare.x * RCL_UNITS_PER_SQUARE;
|
|
|
|
h.direction = 3;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
if (step.x == -1)
|
|
|
|
{
|
|
|
|
h.direction = 1;
|
|
|
|
h.position.x += RCL_UNITS_PER_SQUARE;
|
|
|
|
}
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
RCL_Unit diff = h.position.x - ray.start.x;
|
2019-10-12 16:29:13 +02:00
|
|
|
|
|
|
|
h.position.y = // avoid division by multiplying with reciprocal
|
2020-02-25 17:04:11 +01:00
|
|
|
ray.start.y + (ray.direction.y * diff * rayDirXRecip) / RECIP_SCALE;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2019-08-31 18:28:02 +02:00
|
|
|
#if RCL_RECTILINEAR
|
2019-08-17 13:15:57 +02:00
|
|
|
/* Here we compute the fish eye corrected distance (perpendicular to
|
|
|
|
the projection plane) as the Euclidean distance divided by the length
|
|
|
|
of the ray direction vector. This can be computed without actually
|
|
|
|
computing Euclidean distances as a hypothenuse A (distance) divided
|
|
|
|
by hypothenuse B (length) is equal to leg A (distance along one axis)
|
|
|
|
divided by leg B (length along the same axis). */
|
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
h.distance =
|
2019-10-21 17:37:35 +02:00
|
|
|
(((h.position.x - ray.start.x) / 4) *
|
|
|
|
RCL_UNITS_PER_SQUARE * rayDirXRecip)
|
|
|
|
/ (RECIP_SCALE / 4);
|
|
|
|
|
|
|
|
// ^ / 4 is here to prevent overflow
|
2019-08-16 20:41:03 +02:00
|
|
|
#endif
|
2018-09-24 18:33:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h.position.y = currentSquare.y * RCL_UNITS_PER_SQUARE;
|
|
|
|
h.direction = 2;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
if (step.y == -1)
|
|
|
|
{
|
|
|
|
h.direction = 0;
|
|
|
|
h.position.y += RCL_UNITS_PER_SQUARE;
|
|
|
|
}
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
RCL_Unit diff = h.position.y - ray.start.y;
|
2019-10-12 16:29:13 +02:00
|
|
|
|
|
|
|
h.position.x =
|
2020-02-25 17:04:11 +01:00
|
|
|
ray.start.x + (ray.direction.x * diff * rayDirYRecip) / RECIP_SCALE;
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2019-08-31 18:28:02 +02:00
|
|
|
#if RCL_RECTILINEAR
|
2018-09-24 18:33:40 +02:00
|
|
|
h.distance =
|
2019-10-21 17:37:35 +02:00
|
|
|
(((h.position.y - ray.start.y) / 4) *
|
|
|
|
RCL_UNITS_PER_SQUARE * rayDirYRecip)
|
|
|
|
/ (RECIP_SCALE / 4);
|
|
|
|
|
|
|
|
// ^ / 4 is here to prevent overflow
|
2019-08-16 20:41:03 +02:00
|
|
|
#endif
|
2018-09-24 18:33:40 +02:00
|
|
|
}
|
2018-08-23 02:36:51 +02:00
|
|
|
|
2019-08-31 18:28:02 +02:00
|
|
|
#if !RCL_RECTILINEAR
|
2019-08-16 20:41:03 +02:00
|
|
|
h.distance = RCL_dist(h.position,ray.start);
|
|
|
|
#endif
|
|
|
|
|
2018-09-05 16:26:13 +02:00
|
|
|
if (typeFunc != 0)
|
|
|
|
h.type = typeFunc(currentSquare.x,currentSquare.y);
|
2018-09-24 18:33:40 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#if RCL_COMPUTE_WALL_TEXCOORDS == 1
|
2018-09-27 12:39:30 +02:00
|
|
|
switch (h.direction)
|
|
|
|
{
|
|
|
|
case 0: h.textureCoord =
|
|
|
|
RCL_wrap(-1 * h.position.x,RCL_UNITS_PER_SQUARE); break;
|
|
|
|
|
|
|
|
case 1: h.textureCoord =
|
|
|
|
RCL_wrap(h.position.y,RCL_UNITS_PER_SQUARE); break;
|
|
|
|
|
|
|
|
case 2: h.textureCoord =
|
|
|
|
RCL_wrap(h.position.x,RCL_UNITS_PER_SQUARE); break;
|
|
|
|
|
|
|
|
case 3: h.textureCoord =
|
|
|
|
RCL_wrap(-1 * h.position.y,RCL_UNITS_PER_SQUARE); break;
|
|
|
|
|
|
|
|
default: h.textureCoord = 0; break;
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
if (_RCL_rollFunction != 0)
|
2019-07-21 20:39:11 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
h.doorRoll = _RCL_rollFunction(currentSquare.x,currentSquare.y);
|
2019-07-21 20:39:11 +02:00
|
|
|
|
|
|
|
if (h.direction == 0 || h.direction == 1)
|
|
|
|
h.doorRoll *= -1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 12:39:30 +02:00
|
|
|
#else
|
|
|
|
h.textureCoord = 0;
|
2018-09-16 14:24:32 +02:00
|
|
|
#endif
|
2018-09-14 20:17:15 +02:00
|
|
|
|
2018-08-23 02:36:51 +02:00
|
|
|
hitResults[*hitResultsLen] = h;
|
|
|
|
|
|
|
|
*hitResultsLen += 1;
|
|
|
|
|
|
|
|
squareType = currentType;
|
|
|
|
|
2018-08-31 13:29:50 +02:00
|
|
|
if (*hitResultsLen >= constraints.maxHits)
|
2018-08-23 02:36:51 +02:00
|
|
|
break;
|
2018-08-23 00:50:19 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
// DDA step
|
2018-09-24 16:02:00 +02:00
|
|
|
|
2018-09-24 18:33:40 +02:00
|
|
|
if (nextSideDist.x < nextSideDist.y)
|
|
|
|
{
|
|
|
|
nextSideDist.x += delta.x;
|
|
|
|
currentSquare.x += step.x;
|
|
|
|
stepHorizontal = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nextSideDist.y += delta.y;
|
|
|
|
currentSquare.y += step.y;
|
|
|
|
stepHorizontal = 0;
|
|
|
|
}
|
2018-08-22 08:21:38 +02:00
|
|
|
}
|
2018-08-23 02:36:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult RCL_castRay(RCL_Ray ray, RCL_ArrayFunction arrayFunc)
|
2018-08-23 02:36:51 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_castRay);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult result;
|
|
|
|
uint16_t RCL_len;
|
|
|
|
RCL_RayConstraints c;
|
2018-08-31 13:29:50 +02:00
|
|
|
|
|
|
|
c.maxSteps = 1000;
|
|
|
|
c.maxHits = 1;
|
2018-08-23 02:36:51 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_castRayMultiHit(ray,arrayFunc,0,&result,&RCL_len,c);
|
2018-08-23 02:36:51 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
if (RCL_len == 0)
|
2018-08-23 02:36:51 +02:00
|
|
|
result.distance = -1;
|
2018-08-22 08:21:38 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_castRaysMultiHit(RCL_Camera cam, RCL_ArrayFunction arrayFunc,
|
|
|
|
RCL_ArrayFunction typeFunction, RCL_ColumnFunction columnFunc,
|
|
|
|
RCL_RayConstraints constraints)
|
2018-08-30 08:51:53 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D dir1 =
|
|
|
|
RCL_angleToDirection(cam.direction - RCL_HORIZONTAL_FOV_HALF);
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D dir2 =
|
|
|
|
RCL_angleToDirection(cam.direction + RCL_HORIZONTAL_FOV_HALF);
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit dX = dir2.x - dir1.x;
|
|
|
|
RCL_Unit dY = dir2.y - dir1.y;
|
|
|
|
|
|
|
|
RCL_HitResult hits[constraints.maxHits];
|
2018-08-31 11:51:03 +02:00
|
|
|
uint16_t hitCount;
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Ray r;
|
2018-08-31 13:29:50 +02:00
|
|
|
r.start = cam.position;
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit currentDX = 0;
|
|
|
|
RCL_Unit currentDY = 0;
|
2018-09-15 17:56:59 +02:00
|
|
|
|
2018-09-15 17:53:16 +02:00
|
|
|
for (int16_t i = 0; i < cam.resolution.x; ++i)
|
2018-08-30 08:51:53 +02:00
|
|
|
{
|
2019-08-17 13:15:57 +02:00
|
|
|
/* Here by linearly interpolating the direction vector its length changes,
|
|
|
|
which in result achieves correcting the fish eye effect (computing
|
|
|
|
perpendicular distance). */
|
|
|
|
|
2018-09-15 17:56:59 +02:00
|
|
|
r.direction.x = dir1.x + currentDX / cam.resolution.x;
|
|
|
|
r.direction.y = dir1.y + currentDY / cam.resolution.x;
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_castRayMultiHit(r,arrayFunc,typeFunction,hits,&hitCount,constraints);
|
2018-08-30 08:51:53 +02:00
|
|
|
|
2018-08-31 18:11:32 +02:00
|
|
|
columnFunc(hits,hitCount,i,r);
|
2018-09-15 17:56:59 +02:00
|
|
|
|
|
|
|
currentDX += dX;
|
|
|
|
currentDY += dY;
|
2018-08-30 08:51:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:04:36 +02:00
|
|
|
/**
|
|
|
|
Helper function that determines intersection with both ceiling and floor.
|
|
|
|
*/
|
2018-09-22 14:07:07 +02:00
|
|
|
RCL_Unit _RCL_floorCeilFunction(int16_t x, int16_t y)
|
2018-09-05 11:35:52 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit f = _RCL_floorFunction(x,y);
|
2018-09-08 18:39:47 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
if (_RCL_ceilFunction == 0)
|
2018-09-08 18:39:47 +02:00
|
|
|
return f;
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit c = _RCL_ceilFunction(x,y);
|
2018-09-05 11:35:52 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#ifndef RCL_RAYCAST_TINY
|
2018-09-05 11:35:52 +02:00
|
|
|
return ((f & 0x0000ffff) << 16) | (c & 0x0000ffff);
|
2018-09-13 16:13:59 +02:00
|
|
|
#else
|
|
|
|
return ((f & 0x00ff) << 8) | (c & 0x00ff);
|
|
|
|
#endif
|
2018-09-05 11:35:52 +02:00
|
|
|
}
|
2018-08-31 18:26:51 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit _floorHeightNotZeroFunction(int16_t x, int16_t y)
|
2018-09-14 17:46:31 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
return _RCL_floorFunction(x,y) == 0 ? 0 :
|
2019-07-21 20:39:11 +02:00
|
|
|
RCL_nonZero((x & 0x00FF) | ((y & 0x00FF) << 8));
|
2018-09-14 19:42:59 +02:00
|
|
|
// ^ this makes collisions between all squares - needed for rolling doors
|
2018-09-14 17:46:31 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_adjustDistance(RCL_Unit distance, RCL_Camera *camera,
|
|
|
|
RCL_Ray *ray)
|
2018-09-11 08:09:34 +02:00
|
|
|
{
|
|
|
|
/* FIXME/TODO: The adjusted (=orthogonal, camera-space) distance could
|
|
|
|
possibly be computed more efficiently by not computing Euclidean
|
|
|
|
distance at all, but rather compute the distance of the collision
|
|
|
|
point from the projection plane (line). */
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit result =
|
2018-09-11 08:09:34 +02:00
|
|
|
(distance *
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_vectorsAngleCos(RCL_angleToDirection(camera->direction),
|
|
|
|
ray->direction)) / RCL_UNITS_PER_SQUARE;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-21 11:07:29 +02:00
|
|
|
return RCL_nonZero(result);
|
|
|
|
// ^ prevent division by zero
|
2018-09-11 08:09:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:40:17 +02:00
|
|
|
/// Helper for drawing floor or ceiling. Returns the last drawn pixel position.
|
2019-10-11 15:02:13 +02:00
|
|
|
static inline int16_t _RCL_drawHorizontalColumn(
|
2018-09-22 14:36:40 +02:00
|
|
|
RCL_Unit yCurrent,
|
|
|
|
RCL_Unit yTo,
|
2018-09-23 18:40:17 +02:00
|
|
|
RCL_Unit limit1, // TODO: int16_t?
|
2018-09-22 14:36:40 +02:00
|
|
|
RCL_Unit limit2,
|
|
|
|
RCL_Unit verticalOffset,
|
|
|
|
int16_t increment,
|
|
|
|
int8_t computeDepth,
|
2018-09-23 22:38:14 +02:00
|
|
|
int8_t computeCoords,
|
2018-09-23 18:50:28 +02:00
|
|
|
int16_t depthIncrementMultiplier,
|
2018-09-23 22:38:14 +02:00
|
|
|
RCL_Ray *ray,
|
2018-09-22 14:36:40 +02:00
|
|
|
RCL_PixelInfo *pixelInfo
|
|
|
|
)
|
|
|
|
{
|
2019-09-27 03:57:12 +02:00
|
|
|
_RCL_UNUSED(ray);
|
|
|
|
|
2018-09-23 22:38:14 +02:00
|
|
|
RCL_Unit depthIncrement;
|
|
|
|
RCL_Unit dx;
|
|
|
|
RCL_Unit dy;
|
|
|
|
|
2018-09-22 15:17:07 +02:00
|
|
|
pixelInfo->isWall = 0;
|
|
|
|
|
2018-09-22 14:36:40 +02:00
|
|
|
int16_t limit = RCL_clamp(yTo,limit1,limit2);
|
|
|
|
|
2019-10-11 15:02:13 +02:00
|
|
|
RCL_Unit depth = 0; /* TODO: this is for clamping depth to 0 so that we don't
|
|
|
|
have negative depths, but we should do it more
|
|
|
|
elegantly and efficiently */
|
|
|
|
|
|
|
|
_RCL_UNUSED(depth);
|
|
|
|
|
2018-09-23 22:38:14 +02:00
|
|
|
/* for performance reasons have different version of the critical loop
|
|
|
|
to be able to branch early */
|
|
|
|
#define loop(doDepth,doCoords)\
|
|
|
|
{\
|
|
|
|
if (doDepth) /*constant condition - compiler should optimize it out*/\
|
|
|
|
{\
|
2019-10-11 15:02:13 +02:00
|
|
|
depth = pixelInfo->depth + RCL_absVal(verticalOffset) *\
|
2018-09-23 22:38:14 +02:00
|
|
|
RCL_VERTICAL_DEPTH_MULTIPLY;\
|
|
|
|
depthIncrement = depthIncrementMultiplier *\
|
|
|
|
_RCL_horizontalDepthStep;\
|
|
|
|
}\
|
|
|
|
if (doCoords) /*constant condition - compiler should optimize it out*/\
|
|
|
|
{\
|
|
|
|
dx = pixelInfo->hit.position.x - _RCL_camera.position.x;\
|
|
|
|
dy = pixelInfo->hit.position.y - _RCL_camera.position.y;\
|
|
|
|
}\
|
|
|
|
for (int16_t i = yCurrent + increment;\
|
|
|
|
increment == -1 ? i >= limit : i <= limit; /* TODO: is efficient? */\
|
|
|
|
i += increment)\
|
|
|
|
{\
|
|
|
|
pixelInfo->position.y = i;\
|
2018-09-28 20:20:40 +02:00
|
|
|
if (doDepth) /*constant condition - compiler should optimize it out*/\
|
2019-10-11 15:02:13 +02:00
|
|
|
{\
|
|
|
|
depth += depthIncrement;\
|
2019-10-12 01:49:35 +02:00
|
|
|
pixelInfo->depth = RCL_zeroClamp(depth); \
|
2019-10-11 18:10:12 +02:00
|
|
|
/* ^ int comparison is fast, it is not braching! (= test instr.) */\
|
2019-10-11 15:02:13 +02:00
|
|
|
}\
|
2018-09-28 20:20:40 +02:00
|
|
|
if (doCoords) /*constant condition - compiler should optimize it out*/\
|
2018-09-23 22:38:14 +02:00
|
|
|
{\
|
2018-09-28 23:27:21 +02:00
|
|
|
RCL_Unit d = _RCL_floorPixelDistances[i];\
|
2018-09-29 13:09:41 +02:00
|
|
|
RCL_Unit d2 = RCL_nonZero(pixelInfo->hit.distance);\
|
2018-09-23 22:38:14 +02:00
|
|
|
pixelInfo->texCoords.x =\
|
2018-09-29 13:09:41 +02:00
|
|
|
_RCL_camera.position.x + ((d * dx) / d2);\
|
2018-09-23 22:38:14 +02:00
|
|
|
pixelInfo->texCoords.y =\
|
2018-09-29 13:09:41 +02:00
|
|
|
_RCL_camera.position.y + ((d * dy) / d2);\
|
2018-09-23 22:38:14 +02:00
|
|
|
}\
|
|
|
|
RCL_PIXEL_FUNCTION(pixelInfo);\
|
|
|
|
}\
|
|
|
|
}
|
2018-09-22 14:36:40 +02:00
|
|
|
|
2018-09-23 22:38:14 +02:00
|
|
|
if (computeDepth) // branch early
|
|
|
|
{
|
|
|
|
if (!computeCoords)
|
|
|
|
loop(1,0)
|
|
|
|
else
|
|
|
|
loop(1,1)
|
2018-09-22 14:36:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-23 22:38:14 +02:00
|
|
|
if (!computeCoords)
|
|
|
|
loop(0,0)
|
|
|
|
else
|
|
|
|
loop(1,1)
|
2018-09-22 14:36:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 22:03:52 +02:00
|
|
|
#undef loop
|
|
|
|
|
2018-09-22 14:36:40 +02:00
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:40:17 +02:00
|
|
|
/// Helper for drawing walls. Returns the last drawn pixel position.
|
2018-09-22 14:07:07 +02:00
|
|
|
static inline int16_t _RCL_drawWall(
|
2018-09-22 10:44:12 +02:00
|
|
|
RCL_Unit yCurrent,
|
|
|
|
RCL_Unit yFrom,
|
|
|
|
RCL_Unit yTo,
|
2018-09-22 14:36:40 +02:00
|
|
|
RCL_Unit limit1, // TODO: int16_t?
|
2018-09-22 10:44:12 +02:00
|
|
|
RCL_Unit limit2,
|
2018-09-22 21:53:10 +02:00
|
|
|
RCL_Unit height,
|
2018-09-22 10:01:19 +02:00
|
|
|
int16_t increment,
|
2018-09-22 10:44:12 +02:00
|
|
|
RCL_PixelInfo *pixelInfo
|
2018-09-22 10:01:19 +02:00
|
|
|
)
|
|
|
|
{
|
2019-09-27 03:57:12 +02:00
|
|
|
_RCL_UNUSED(height)
|
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
height = RCL_absVal(height);
|
|
|
|
|
2018-09-22 15:17:07 +02:00
|
|
|
pixelInfo->isWall = 1;
|
|
|
|
|
2018-09-22 10:44:12 +02:00
|
|
|
RCL_Unit limit = RCL_clamp(yTo,limit1,limit2);
|
2018-09-22 10:01:19 +02:00
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
RCL_Unit wallLength = RCL_nonZero(RCL_absVal(yTo - yFrom - 1));
|
2018-09-22 10:01:19 +02:00
|
|
|
|
|
|
|
RCL_Unit wallPosition = RCL_absVal(yFrom - yCurrent) - increment;
|
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
RCL_Unit heightScaled = height * RCL_TEXTURE_INTERPOLATION_SCALE;
|
|
|
|
_RCL_UNUSED(heightScaled);
|
|
|
|
|
|
|
|
RCL_Unit coordStepScaled = RCL_COMPUTE_WALL_TEXCOORDS ?
|
2018-09-22 21:53:10 +02:00
|
|
|
#if RCL_TEXTURE_VERTICAL_STRETCH == 1
|
2019-09-30 17:15:54 +02:00
|
|
|
((RCL_UNITS_PER_SQUARE * RCL_TEXTURE_INTERPOLATION_SCALE) / wallLength)
|
2018-09-22 21:53:10 +02:00
|
|
|
#else
|
2019-09-30 17:15:54 +02:00
|
|
|
(heightScaled / wallLength)
|
2018-09-22 21:53:10 +02:00
|
|
|
#endif
|
2019-09-30 17:15:54 +02:00
|
|
|
: 0;
|
2018-09-22 10:01:19 +02:00
|
|
|
|
|
|
|
pixelInfo->texCoords.y = RCL_COMPUTE_WALL_TEXCOORDS ?
|
2019-09-30 17:15:54 +02:00
|
|
|
(wallPosition * coordStepScaled) : 0;
|
2018-09-22 10:01:19 +02:00
|
|
|
|
2019-09-28 02:08:53 +02:00
|
|
|
if (increment < 0)
|
|
|
|
{
|
2019-09-30 17:15:54 +02:00
|
|
|
coordStepScaled *= -1;
|
2019-09-28 02:08:53 +02:00
|
|
|
pixelInfo->texCoords.y =
|
|
|
|
#if RCL_TEXTURE_VERTICAL_STRETCH == 1
|
2019-09-30 17:15:54 +02:00
|
|
|
(RCL_UNITS_PER_SQUARE * RCL_TEXTURE_INTERPOLATION_SCALE)
|
|
|
|
- pixelInfo->texCoords.y;
|
2019-09-28 02:08:53 +02:00
|
|
|
#else
|
2019-09-30 17:15:54 +02:00
|
|
|
heightScaled - pixelInfo->texCoords.y;
|
2019-09-28 02:08:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
2019-10-07 21:43:16 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// with floor wall, don't start under 0
|
2019-10-12 01:49:35 +02:00
|
|
|
pixelInfo->texCoords.y = RCL_zeroClamp(pixelInfo->texCoords.y);
|
2019-10-07 21:43:16 +02:00
|
|
|
}
|
2019-09-28 02:08:53 +02:00
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
RCL_Unit textureCoordScaled = pixelInfo->texCoords.y;
|
2018-09-22 10:44:12 +02:00
|
|
|
|
2020-02-25 17:04:11 +01:00
|
|
|
#if RCL_RECTILINEAR
|
|
|
|
RCL_Unit tmp = pixelInfo->depth;
|
|
|
|
pixelInfo->depth = (pixelInfo->depth * 23) / 32;
|
|
|
|
|
|
|
|
/* ^ UGLY HACK
|
|
|
|
|
|
|
|
For some reason the computed distance with rectilinear is larger, the
|
|
|
|
correct distance is about 0.711 (~= 23/32) of the computed distance, so
|
|
|
|
we correct it here in this ugly way.
|
|
|
|
|
|
|
|
TODO: investigate why, fix nicely
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
for (RCL_Unit i = yCurrent + increment;
|
|
|
|
increment == -1 ? i >= limit : i <= limit; // TODO: is efficient?
|
|
|
|
i += increment)
|
2018-09-22 10:01:19 +02:00
|
|
|
{
|
2019-09-30 17:15:54 +02:00
|
|
|
pixelInfo->position.y = i;
|
2018-09-22 10:44:12 +02:00
|
|
|
|
|
|
|
#if RCL_COMPUTE_WALL_TEXCOORDS == 1
|
2019-09-30 17:15:54 +02:00
|
|
|
pixelInfo->texCoords.y =
|
|
|
|
textureCoordScaled / RCL_TEXTURE_INTERPOLATION_SCALE;
|
|
|
|
|
|
|
|
textureCoordScaled += coordStepScaled;
|
2018-09-22 10:44:12 +02:00
|
|
|
#endif
|
2018-09-22 10:01:19 +02:00
|
|
|
|
2019-09-30 17:15:54 +02:00
|
|
|
RCL_PIXEL_FUNCTION(pixelInfo);
|
2018-09-22 10:01:19 +02:00
|
|
|
}
|
2020-02-25 17:04:11 +01:00
|
|
|
|
|
|
|
#if RCL_RECTILINEAR
|
|
|
|
pixelInfo->depth = tmp;
|
|
|
|
#endif
|
|
|
|
|
2018-09-22 10:44:12 +02:00
|
|
|
return limit;
|
2018-09-22 10:01:19 +02:00
|
|
|
}
|
2018-09-22 10:44:12 +02:00
|
|
|
|
2018-09-28 23:01:00 +02:00
|
|
|
/// Fills a RCL_HitResult struct with info for a hit at infinity.
|
|
|
|
static inline void _RCL_makeInfiniteHit(RCL_HitResult *hit, RCL_Ray *ray)
|
|
|
|
{
|
|
|
|
hit->distance = RCL_UNITS_PER_SQUARE * RCL_UNITS_PER_SQUARE;
|
|
|
|
/* ^ horizon is at infinity, but we can't use too big infinity
|
|
|
|
(RCL_INFINITY) because it would overflow in the following mult. */
|
|
|
|
hit->position.x = (ray->direction.x * hit->distance) / RCL_UNITS_PER_SQUARE;
|
|
|
|
hit->position.y = (ray->direction.y * hit->distance) / RCL_UNITS_PER_SQUARE;
|
|
|
|
|
|
|
|
hit->direction = 0;
|
|
|
|
hit->textureCoord = 0;
|
|
|
|
hit->arrayValue = 0;
|
|
|
|
hit->doorRoll = 0;
|
|
|
|
hit->type = 0;
|
|
|
|
}
|
|
|
|
|
2018-09-22 14:07:07 +02:00
|
|
|
void _RCL_columnFunctionComplex(RCL_HitResult *hits, uint16_t hitCount, uint16_t x,
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Ray ray)
|
2018-08-31 18:26:51 +02:00
|
|
|
{
|
2018-09-13 11:16:28 +02:00
|
|
|
// last written Y position, can never go backwards
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit fPosY = _RCL_camera.resolution.y;
|
|
|
|
RCL_Unit cPosY = -1;
|
2018-09-13 10:09:21 +02:00
|
|
|
|
2018-09-29 09:17:23 +02:00
|
|
|
// world coordinates (relative to camera height though)
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit fZ1World = _RCL_startFloorHeight;
|
|
|
|
RCL_Unit cZ1World = _RCL_startCeil_Height;
|
2018-09-01 09:46:19 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_PixelInfo p;
|
2018-09-01 12:04:29 +02:00
|
|
|
p.position.x = x;
|
2018-09-28 20:20:40 +02:00
|
|
|
p.height = 0;
|
2019-09-28 02:08:53 +02:00
|
|
|
p.wallHeight = 0;
|
2018-09-16 17:12:09 +02:00
|
|
|
p.texCoords.x = 0;
|
|
|
|
p.texCoords.y = 0;
|
2018-09-01 12:04:29 +02:00
|
|
|
|
2018-09-08 18:45:57 +02:00
|
|
|
// we'll be simulatenously drawing the floor and the ceiling now
|
2018-09-17 17:55:53 +02:00
|
|
|
for (RCL_Unit j = 0; j <= hitCount; ++j)
|
2018-09-29 09:17:23 +02:00
|
|
|
{ // ^ = add extra iteration for horizon plane
|
2018-09-13 11:16:28 +02:00
|
|
|
int8_t drawingHorizon = j == hitCount;
|
2018-09-05 19:49:39 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult hit;
|
2018-09-30 15:31:18 +02:00
|
|
|
RCL_Unit distance = 1;
|
2018-09-01 12:04:29 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit fWallHeight = 0, cWallHeight = 0;
|
|
|
|
RCL_Unit fZ2World = 0, cZ2World = 0;
|
|
|
|
RCL_Unit fZ1Screen = 0, cZ1Screen = 0;
|
|
|
|
RCL_Unit fZ2Screen = 0, cZ2Screen = 0;
|
2018-09-12 13:45:08 +02:00
|
|
|
|
2018-09-13 11:16:28 +02:00
|
|
|
if (!drawingHorizon)
|
2018-09-08 18:39:47 +02:00
|
|
|
{
|
2018-09-13 11:16:28 +02:00
|
|
|
hit = hits[j];
|
2018-09-29 13:17:04 +02:00
|
|
|
distance = RCL_nonZero(hit.distance);
|
2018-09-29 13:09:41 +02:00
|
|
|
p.hit = hit;
|
2018-09-17 17:55:53 +02:00
|
|
|
|
|
|
|
fWallHeight = _RCL_floorFunction(hit.square.x,hit.square.y);
|
|
|
|
fZ2World = fWallHeight - _RCL_camera.height;
|
|
|
|
fZ1Screen = _RCL_middleRow - RCL_perspectiveScale(
|
|
|
|
(fZ1World * _RCL_camera.resolution.y) /
|
|
|
|
RCL_UNITS_PER_SQUARE,distance);
|
|
|
|
fZ2Screen = _RCL_middleRow - RCL_perspectiveScale(
|
|
|
|
(fZ2World * _RCL_camera.resolution.y) /
|
|
|
|
RCL_UNITS_PER_SQUARE,distance);
|
|
|
|
|
|
|
|
if (_RCL_ceilFunction != 0)
|
2018-09-13 11:16:28 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
cWallHeight = _RCL_ceilFunction(hit.square.x,hit.square.y);
|
|
|
|
cZ2World = cWallHeight - _RCL_camera.height;
|
|
|
|
cZ1Screen = _RCL_middleRow - RCL_perspectiveScale(
|
|
|
|
(cZ1World * _RCL_camera.resolution.y) /
|
|
|
|
RCL_UNITS_PER_SQUARE,distance);
|
|
|
|
cZ2Screen = _RCL_middleRow - RCL_perspectiveScale(
|
|
|
|
(cZ2World * _RCL_camera.resolution.y) /
|
|
|
|
RCL_UNITS_PER_SQUARE,distance);
|
2018-09-13 11:16:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
fZ1Screen = _RCL_middleRow;
|
|
|
|
cZ1Screen = _RCL_middleRow + 1;
|
2018-09-28 23:01:00 +02:00
|
|
|
_RCL_makeInfiniteHit(&p.hit,&ray);
|
2018-09-12 13:45:08 +02:00
|
|
|
}
|
2018-09-02 23:26:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit limit;
|
2018-09-02 23:26:02 +02:00
|
|
|
|
2018-09-01 08:17:01 +02:00
|
|
|
p.isWall = 0;
|
2018-09-13 11:18:31 +02:00
|
|
|
p.isHorizon = drawingHorizon;
|
2018-08-31 18:26:51 +02:00
|
|
|
|
2018-09-13 11:16:28 +02:00
|
|
|
// draw floor until wall
|
|
|
|
p.isFloor = 1;
|
2018-09-28 20:20:40 +02:00
|
|
|
p.height = fZ1World + _RCL_camera.height;
|
2019-09-28 02:08:53 +02:00
|
|
|
p.wallHeight = 0;
|
2018-09-21 08:48:15 +02:00
|
|
|
|
|
|
|
#if RCL_COMPUTE_FLOOR_DEPTH == 1
|
2018-09-17 17:55:53 +02:00
|
|
|
p.depth = (_RCL_fHorizontalDepthStart - fPosY) * _RCL_horizontalDepthStep;
|
2018-09-21 08:48:15 +02:00
|
|
|
#else
|
|
|
|
p.depth = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-10-11 15:02:13 +02:00
|
|
|
limit = _RCL_drawHorizontalColumn(fPosY,fZ1Screen,cPosY + 1,
|
2018-09-28 23:01:00 +02:00
|
|
|
_RCL_camera.resolution.y,fZ1World,-1,RCL_COMPUTE_FLOOR_DEPTH,
|
2018-09-22 14:36:40 +02:00
|
|
|
// ^ purposfully allow outside screen bounds
|
2018-09-29 09:33:51 +02:00
|
|
|
RCL_COMPUTE_FLOOR_TEXCOORDS && p.height == RCL_FLOOR_TEXCOORDS_HEIGHT,
|
|
|
|
1,&ray,&p);
|
2018-09-22 14:36:40 +02:00
|
|
|
|
|
|
|
if (fPosY > limit)
|
|
|
|
fPosY = limit;
|
2018-09-01 09:55:35 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
if (_RCL_ceilFunction != 0 || drawingHorizon)
|
2018-08-31 19:13:15 +02:00
|
|
|
{
|
2018-09-13 11:16:28 +02:00
|
|
|
// draw ceiling until wall
|
|
|
|
p.isFloor = 0;
|
2018-09-28 20:20:40 +02:00
|
|
|
p.height = cZ1World + _RCL_camera.height;
|
2018-09-21 08:48:15 +02:00
|
|
|
|
|
|
|
#if RCL_COMPUTE_CEILING_DEPTH == 1
|
2018-09-17 17:55:53 +02:00
|
|
|
p.depth = (cPosY - _RCL_cHorizontalDepthStart) *
|
|
|
|
_RCL_horizontalDepthStep;
|
2018-09-21 08:48:15 +02:00
|
|
|
#endif
|
|
|
|
|
2019-10-11 15:02:13 +02:00
|
|
|
limit = _RCL_drawHorizontalColumn(cPosY,cZ1Screen,
|
2018-09-23 22:38:14 +02:00
|
|
|
-1,fPosY - 1,cZ1World,1,RCL_COMPUTE_CEILING_DEPTH,0,1,&ray,&p);
|
2018-09-22 14:36:40 +02:00
|
|
|
// ^ purposfully allow outside screen bounds here
|
2018-09-01 08:17:01 +02:00
|
|
|
|
2018-09-22 14:36:40 +02:00
|
|
|
if (cPosY < limit)
|
|
|
|
cPosY = limit;
|
|
|
|
}
|
2018-09-02 23:26:02 +02:00
|
|
|
|
2018-09-13 11:16:28 +02:00
|
|
|
if (!drawingHorizon) // don't draw walls for horizon plane
|
2018-09-03 08:14:36 +02:00
|
|
|
{
|
2018-09-13 11:16:28 +02:00
|
|
|
p.isWall = 1;
|
|
|
|
p.depth = distance;
|
|
|
|
p.isFloor = 1;
|
2018-09-22 10:01:19 +02:00
|
|
|
p.texCoords.x = hit.textureCoord;
|
2019-09-27 04:11:52 +02:00
|
|
|
p.height = fZ1World + _RCL_camera.height;
|
2019-09-28 02:08:53 +02:00
|
|
|
p.wallHeight = fWallHeight;
|
2018-09-05 07:07:12 +02:00
|
|
|
|
2018-09-13 11:16:28 +02:00
|
|
|
// draw floor wall
|
2018-09-05 11:44:15 +02:00
|
|
|
|
2018-09-21 11:07:29 +02:00
|
|
|
if (fPosY > 0) // still pixels left?
|
2018-09-12 13:45:08 +02:00
|
|
|
{
|
2018-09-13 11:16:28 +02:00
|
|
|
p.isFloor = 1;
|
2018-09-22 10:44:12 +02:00
|
|
|
|
2018-09-22 14:07:07 +02:00
|
|
|
limit = _RCL_drawWall(fPosY,fZ1Screen,fZ2Screen,cPosY + 1,
|
2018-09-22 21:53:10 +02:00
|
|
|
_RCL_camera.resolution.y,
|
2018-09-22 10:44:12 +02:00
|
|
|
// ^ purposfully allow outside screen bounds here
|
2018-09-22 21:53:10 +02:00
|
|
|
#if RCL_TEXTURE_VERTICAL_STRETCH == 1
|
|
|
|
RCL_UNITS_PER_SQUARE
|
|
|
|
#else
|
|
|
|
fZ2World - fZ1World
|
|
|
|
#endif
|
|
|
|
,-1,&p);
|
|
|
|
|
2018-09-22 10:44:12 +02:00
|
|
|
|
|
|
|
if (fPosY > limit)
|
|
|
|
fPosY = limit;
|
|
|
|
|
|
|
|
fZ1World = fZ2World; // for the next iteration
|
2018-09-13 11:16:28 +02:00
|
|
|
} // ^ purposfully allow outside screen bounds here
|
2018-09-12 13:45:08 +02:00
|
|
|
|
2018-09-13 11:16:28 +02:00
|
|
|
// draw ceiling wall
|
2018-09-05 11:44:15 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
if (_RCL_ceilFunction != 0 && cPosY < _RCL_camResYLimit) // pixels left?
|
2018-09-05 12:20:46 +02:00
|
|
|
{
|
2018-09-13 11:16:28 +02:00
|
|
|
p.isFloor = 0;
|
2019-09-27 04:11:52 +02:00
|
|
|
p.height = cZ1World + _RCL_camera.height;
|
2019-09-28 02:08:53 +02:00
|
|
|
p.wallHeight = cWallHeight;
|
2018-09-22 10:44:12 +02:00
|
|
|
|
2018-09-22 14:07:07 +02:00
|
|
|
limit = _RCL_drawWall(cPosY,cZ1Screen,cZ2Screen,
|
2018-09-22 21:53:10 +02:00
|
|
|
-1,fPosY - 1,
|
2018-09-22 10:44:12 +02:00
|
|
|
// ^ puposfully allow outside screen bounds here
|
2018-09-22 21:53:10 +02:00
|
|
|
#if RCL_TEXTURE_VERTICAL_STRETCH == 1
|
|
|
|
RCL_UNITS_PER_SQUARE
|
|
|
|
#else
|
2019-09-28 02:08:53 +02:00
|
|
|
cZ1World - cZ2World
|
2018-09-22 21:53:10 +02:00
|
|
|
#endif
|
|
|
|
,1,&p);
|
|
|
|
|
2018-09-22 10:44:12 +02:00
|
|
|
if (cPosY < limit)
|
|
|
|
cPosY = limit;
|
|
|
|
|
|
|
|
cZ1World = cZ2World; // for the next iteration
|
2018-09-21 11:07:29 +02:00
|
|
|
} // ^ puposfully allow outside screen bounds here
|
2018-09-03 08:14:36 +02:00
|
|
|
}
|
2018-09-01 12:04:29 +02:00
|
|
|
}
|
2018-08-31 18:26:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 07:48:16 +02:00
|
|
|
void _RCL_columnFunctionSimple(RCL_HitResult *hits, uint16_t hitCount,
|
|
|
|
uint16_t x, RCL_Ray ray)
|
2018-09-11 08:09:34 +02:00
|
|
|
{
|
2018-09-22 19:09:10 +02:00
|
|
|
RCL_Unit y = 0;
|
|
|
|
RCL_Unit wallHeightScreen = 0;
|
|
|
|
RCL_Unit wallStart = _RCL_middleRow;
|
|
|
|
RCL_Unit heightOffset = 0;
|
2018-09-11 08:24:48 +02:00
|
|
|
|
2018-09-21 13:50:27 +02:00
|
|
|
RCL_Unit dist = 1;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_PixelInfo p;
|
2018-09-11 08:09:34 +02:00
|
|
|
p.position.x = x;
|
2019-09-28 02:08:53 +02:00
|
|
|
p.wallHeight = RCL_UNITS_PER_SQUARE;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
|
|
|
if (hitCount > 0)
|
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_HitResult hit = hits[0];
|
2018-09-14 19:42:59 +02:00
|
|
|
|
|
|
|
uint8_t goOn = 1;
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
if (_RCL_rollFunction != 0 && RCL_COMPUTE_WALL_TEXCOORDS == 1)
|
2018-09-14 19:42:59 +02:00
|
|
|
{
|
2018-09-15 11:19:53 +02:00
|
|
|
if (hit.arrayValue == 0)
|
|
|
|
{
|
|
|
|
// standing inside door square, looking out => move to the next hit
|
2018-09-14 19:42:59 +02:00
|
|
|
|
2018-09-15 11:19:53 +02:00
|
|
|
if (hitCount > 1)
|
|
|
|
hit = hits[1];
|
|
|
|
else
|
|
|
|
goOn = 0;
|
|
|
|
}
|
|
|
|
else
|
2018-09-14 19:42:59 +02:00
|
|
|
{
|
2018-09-15 11:19:53 +02:00
|
|
|
// normal hit, check the door roll
|
2018-09-14 19:42:59 +02:00
|
|
|
|
2018-09-25 08:06:49 +02:00
|
|
|
RCL_Unit texCoordMod = hit.textureCoord % RCL_UNITS_PER_SQUARE;
|
|
|
|
|
2018-09-15 11:56:20 +02:00
|
|
|
int8_t unrolled = hit.doorRoll >= 0 ?
|
2019-07-21 20:39:11 +02:00
|
|
|
(hit.doorRoll > texCoordMod) :
|
|
|
|
(texCoordMod > RCL_UNITS_PER_SQUARE + hit.doorRoll);
|
2018-09-15 11:19:53 +02:00
|
|
|
|
|
|
|
if (unrolled)
|
2018-09-14 19:42:59 +02:00
|
|
|
{
|
2018-09-15 11:19:53 +02:00
|
|
|
goOn = 0;
|
|
|
|
|
|
|
|
if (hitCount > 1) /* should probably always be true (hit on square
|
|
|
|
exit) */
|
2018-09-14 19:42:59 +02:00
|
|
|
{
|
2018-09-15 11:19:53 +02:00
|
|
|
if (hit.direction % 2 != hits[1].direction % 2)
|
|
|
|
{
|
|
|
|
// hit on the inner side
|
|
|
|
hit = hits[1];
|
|
|
|
goOn = 1;
|
|
|
|
}
|
|
|
|
else if (hitCount > 2)
|
|
|
|
{
|
|
|
|
// hit on the opposite side
|
|
|
|
hit = hits[2];
|
|
|
|
goOn = 1;
|
|
|
|
}
|
2018-09-14 19:42:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 08:09:34 +02:00
|
|
|
p.hit = hit;
|
2018-09-14 17:46:31 +02:00
|
|
|
|
2018-09-14 19:42:59 +02:00
|
|
|
if (goOn)
|
|
|
|
{
|
2018-09-25 07:48:16 +02:00
|
|
|
dist = hit.distance;
|
2018-09-14 17:46:31 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
int16_t wallHeightWorld = _RCL_floorFunction(hit.square.x,hit.square.y);
|
2018-09-14 17:46:31 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
wallHeightScreen = RCL_perspectiveScale((wallHeightWorld *
|
2018-09-21 13:50:27 +02:00
|
|
|
_RCL_camera.resolution.y) / RCL_UNITS_PER_SQUARE,dist);
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
int16_t RCL_normalizedWallHeight = wallHeightWorld != 0 ?
|
|
|
|
((RCL_UNITS_PER_SQUARE * wallHeightScreen) / wallHeightWorld) : 0;
|
2018-09-14 17:46:31 +02:00
|
|
|
|
2018-09-21 13:50:27 +02:00
|
|
|
heightOffset = RCL_perspectiveScale(_RCL_cameraHeightScreen,dist);
|
2018-09-11 08:24:48 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
wallStart = _RCL_middleRow - wallHeightScreen + heightOffset +
|
|
|
|
RCL_normalizedWallHeight;
|
2018-09-14 19:42:59 +02:00
|
|
|
}
|
2018-09-11 08:24:48 +02:00
|
|
|
}
|
2018-09-25 07:48:16 +02:00
|
|
|
else
|
|
|
|
{
|
2018-09-28 23:01:00 +02:00
|
|
|
_RCL_makeInfiniteHit(&p.hit,&ray);
|
2018-09-25 07:48:16 +02:00
|
|
|
}
|
2018-09-11 08:09:34 +02:00
|
|
|
|
|
|
|
// draw ceiling
|
|
|
|
|
|
|
|
p.isWall = 0;
|
|
|
|
p.isFloor = 0;
|
|
|
|
p.isHorizon = 1;
|
|
|
|
p.depth = 1;
|
2018-09-28 20:20:40 +02:00
|
|
|
p.height = RCL_UNITS_PER_SQUARE;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2019-10-11 15:02:13 +02:00
|
|
|
y = _RCL_drawHorizontalColumn(-1,wallStart,-1,_RCL_middleRow,_RCL_camera.height,1,
|
2018-09-23 22:38:14 +02:00
|
|
|
RCL_COMPUTE_CEILING_DEPTH,0,1,&ray,&p);
|
2018-09-11 08:09:34 +02:00
|
|
|
|
|
|
|
// draw wall
|
|
|
|
|
|
|
|
p.isWall = 1;
|
|
|
|
p.isFloor = 1;
|
2018-09-21 14:03:31 +02:00
|
|
|
p.depth = dist;
|
2018-09-28 20:20:40 +02:00
|
|
|
p.height = 0;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
#if RCL_ROLL_TEXTURE_COORDS == 1 && RCL_COMPUTE_WALL_TEXCOORDS == 1
|
2018-09-15 11:56:20 +02:00
|
|
|
p.hit.textureCoord -= p.hit.doorRoll;
|
|
|
|
#endif
|
|
|
|
|
2018-09-16 17:12:09 +02:00
|
|
|
p.texCoords.x = p.hit.textureCoord;
|
2018-09-25 07:48:16 +02:00
|
|
|
p.texCoords.y = 0;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-25 07:48:16 +02:00
|
|
|
RCL_Unit limit = _RCL_drawWall(y,wallStart,wallStart + wallHeightScreen - 1,
|
|
|
|
-1,_RCL_camResYLimit,p.hit.arrayValue,1,&p);
|
2018-09-23 18:40:17 +02:00
|
|
|
|
2018-09-23 22:38:14 +02:00
|
|
|
y = RCL_max(y,limit); // take max, in case no wall was drawn
|
2018-09-29 08:13:00 +02:00
|
|
|
y = RCL_max(y,wallStart);
|
2018-09-11 08:09:34 +02:00
|
|
|
|
|
|
|
// draw floor
|
|
|
|
|
|
|
|
p.isWall = 0;
|
2018-09-21 08:48:15 +02:00
|
|
|
|
|
|
|
#if RCL_COMPUTE_FLOOR_DEPTH == 1
|
2018-09-17 17:55:53 +02:00
|
|
|
p.depth = (_RCL_camera.resolution.y - y) * _RCL_horizontalDepthStep + 1;
|
2018-09-21 08:48:15 +02:00
|
|
|
#endif
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2019-10-11 15:02:13 +02:00
|
|
|
_RCL_drawHorizontalColumn(y,_RCL_camResYLimit,-1,_RCL_camResYLimit,
|
2018-09-23 22:38:14 +02:00
|
|
|
_RCL_camera.height,1,RCL_COMPUTE_FLOOR_DEPTH,RCL_COMPUTE_FLOOR_TEXCOORDS,
|
|
|
|
-1,&ray,&p);
|
2018-09-11 08:09:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 07:07:27 +02:00
|
|
|
/**
|
|
|
|
Precomputes a distance from camera to the floor at each screen row into an
|
|
|
|
array (must be preallocated with sufficient (camera.resolution.y) length).
|
|
|
|
*/
|
2018-09-28 23:27:21 +02:00
|
|
|
static inline void _RCL_precomputeFloorDistances(RCL_Camera camera,
|
2018-09-29 07:07:27 +02:00
|
|
|
RCL_Unit *dest, uint16_t startIndex)
|
2018-09-28 20:20:40 +02:00
|
|
|
{
|
|
|
|
RCL_Unit camHeightScreenSize =
|
2018-09-28 23:27:21 +02:00
|
|
|
(camera.height * camera.resolution.y) / RCL_UNITS_PER_SQUARE;
|
2018-09-28 20:20:40 +02:00
|
|
|
|
2020-02-08 20:17:40 +01:00
|
|
|
for (uint16_t i = startIndex; i < camera.resolution.y; ++i)
|
2018-09-28 23:27:21 +02:00
|
|
|
dest[i] = RCL_perspectiveScaleInverse(camHeightScreenSize,
|
|
|
|
RCL_absVal(i - _RCL_middleRow));
|
2018-09-28 20:20:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 08:48:15 +02:00
|
|
|
void RCL_renderComplex(RCL_Camera cam, RCL_ArrayFunction floorHeightFunc,
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_ArrayFunction ceilingHeightFunc, RCL_ArrayFunction typeFunction,
|
|
|
|
RCL_RayConstraints constraints)
|
2018-08-31 18:26:51 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_floorFunction = floorHeightFunc;
|
|
|
|
_RCL_ceilFunction = ceilingHeightFunc;
|
|
|
|
_RCL_camera = cam;
|
|
|
|
_RCL_camResYLimit = cam.resolution.y - 1;
|
2018-09-07 20:31:30 +02:00
|
|
|
|
2018-09-28 23:01:00 +02:00
|
|
|
uint16_t halfResY = cam.resolution.y / 2;
|
2018-09-08 07:23:21 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_middleRow = halfResY + cam.shear;
|
2018-09-08 07:23:21 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_fHorizontalDepthStart = _RCL_middleRow + halfResY;
|
|
|
|
_RCL_cHorizontalDepthStart = _RCL_middleRow - halfResY;
|
2018-09-07 20:31:30 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_startFloorHeight = floorHeightFunc(
|
|
|
|
RCL_divRoundDown(cam.position.x,RCL_UNITS_PER_SQUARE),
|
|
|
|
RCL_divRoundDown(cam.position.y,RCL_UNITS_PER_SQUARE)) -1 * cam.height;
|
2018-09-05 11:35:52 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_startCeil_Height =
|
2018-09-05 12:20:46 +02:00
|
|
|
ceilingHeightFunc != 0 ?
|
|
|
|
ceilingHeightFunc(
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_divRoundDown(cam.position.x,RCL_UNITS_PER_SQUARE),
|
|
|
|
RCL_divRoundDown(cam.position.y,RCL_UNITS_PER_SQUARE)) -1 * cam.height
|
|
|
|
: RCL_INFINITY;
|
2018-09-01 12:04:29 +02:00
|
|
|
|
2018-09-25 07:48:16 +02:00
|
|
|
_RCL_horizontalDepthStep = RCL_HORIZON_DEPTH / cam.resolution.y;
|
2018-09-01 12:04:29 +02:00
|
|
|
|
2018-09-28 23:01:00 +02:00
|
|
|
#if RCL_COMPUTE_FLOOR_TEXCOORDS == 1
|
2018-09-28 23:27:21 +02:00
|
|
|
RCL_Unit floorPixelDistances[cam.resolution.y];
|
2018-09-29 07:07:27 +02:00
|
|
|
_RCL_precomputeFloorDistances(cam,floorPixelDistances,0);
|
2018-09-28 23:01:00 +02:00
|
|
|
_RCL_floorPixelDistances = floorPixelDistances; // pass to column function
|
|
|
|
#endif
|
|
|
|
|
2018-09-22 14:07:07 +02:00
|
|
|
RCL_castRaysMultiHit(cam,_RCL_floorCeilFunction,typeFunction,
|
|
|
|
_RCL_columnFunctionComplex,constraints);
|
2018-08-31 18:26:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_renderSimple(RCL_Camera cam, RCL_ArrayFunction floorHeightFunc,
|
|
|
|
RCL_ArrayFunction typeFunc, RCL_ArrayFunction rollFunc,
|
|
|
|
RCL_RayConstraints constraints)
|
2018-09-11 08:09:34 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_floorFunction = floorHeightFunc;
|
|
|
|
_RCL_camera = cam;
|
|
|
|
_RCL_camResYLimit = cam.resolution.y - 1;
|
|
|
|
_RCL_middleRow = cam.resolution.y / 2;
|
|
|
|
_RCL_rollFunction = rollFunc;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
_RCL_cameraHeightScreen =
|
|
|
|
(_RCL_camera.resolution.y * (_RCL_camera.height - RCL_UNITS_PER_SQUARE)) /
|
|
|
|
RCL_UNITS_PER_SQUARE;
|
2018-09-11 08:24:48 +02:00
|
|
|
|
2018-09-25 07:48:16 +02:00
|
|
|
_RCL_horizontalDepthStep = RCL_HORIZON_DEPTH / cam.resolution.y;
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-14 19:42:59 +02:00
|
|
|
constraints.maxHits =
|
2018-09-21 16:36:58 +02:00
|
|
|
_RCL_rollFunction == 0 ?
|
|
|
|
1 : // no door => 1 hit is enough
|
|
|
|
3; // for correctly rendering rolling doors we'll need 3 hits (NOT 2)
|
2018-09-11 08:09:34 +02:00
|
|
|
|
2018-09-18 10:25:14 +02:00
|
|
|
#if RCL_COMPUTE_FLOOR_TEXCOORDS == 1
|
2018-09-28 23:27:21 +02:00
|
|
|
RCL_Unit floorPixelDistances[cam.resolution.y];
|
2018-09-29 07:07:27 +02:00
|
|
|
_RCL_precomputeFloorDistances(cam,floorPixelDistances,_RCL_middleRow);
|
2018-09-28 20:20:40 +02:00
|
|
|
_RCL_floorPixelDistances = floorPixelDistances; // pass to column function
|
2018-09-18 10:25:14 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_castRaysMultiHit(cam,_floorHeightNotZeroFunction,typeFunc,
|
2018-09-22 14:07:07 +02:00
|
|
|
_RCL_columnFunctionSimple, constraints);
|
2018-09-18 10:25:14 +02:00
|
|
|
|
|
|
|
#if RCL_COMPUTE_FLOOR_TEXCOORDS == 1
|
|
|
|
_RCL_floorPixelDistances = 0;
|
|
|
|
#endif
|
2018-09-11 08:09:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D RCL_normalize(RCL_Vector2D v)
|
2018-08-30 18:31:08 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_normalize);
|
2018-08-30 18:31:08 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D result;
|
|
|
|
RCL_Unit l = RCL_len(v);
|
2018-09-21 11:07:29 +02:00
|
|
|
l = RCL_nonZero(l);
|
2018-09-02 13:58:46 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
result.x = (v.x * RCL_UNITS_PER_SQUARE) / l;
|
|
|
|
result.y = (v.y * RCL_UNITS_PER_SQUARE) / l;
|
2018-08-30 18:31:08 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_vectorsAngleCos(RCL_Vector2D v1, RCL_Vector2D v2)
|
2018-08-30 18:31:08 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_vectorsAngleCos);
|
2018-08-31 15:38:02 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
v1 = RCL_normalize(v1);
|
|
|
|
v2 = RCL_normalize(v2);
|
2018-08-30 18:31:08 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
return (v1.x * v2.x + v1.y * v2.y) / RCL_UNITS_PER_SQUARE;
|
2018-08-30 18:31:08 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_PixelInfo RCL_mapToScreen(RCL_Vector2D worldPosition, RCL_Unit height,
|
|
|
|
RCL_Camera camera)
|
2018-09-04 11:42:27 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_PixelInfo result;
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D toPoint;
|
2018-09-04 11:42:27 +02:00
|
|
|
|
|
|
|
toPoint.x = worldPosition.x - camera.position.x;
|
|
|
|
toPoint.y = worldPosition.y - camera.position.y;
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit middleColumn = camera.resolution.x / 2;
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2019-10-07 02:41:26 +02:00
|
|
|
// rotate the point
|
2019-10-06 21:20:40 +02:00
|
|
|
|
2019-10-07 02:41:26 +02:00
|
|
|
RCL_Unit cos = RCL_cosInt(camera.direction);
|
|
|
|
RCL_Unit sin = RCL_sinInt(camera.direction);
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2019-10-07 02:41:26 +02:00
|
|
|
RCL_Unit tmp = toPoint.x;
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2019-10-07 02:41:26 +02:00
|
|
|
toPoint.x = (toPoint.x * cos - toPoint.y * sin) / RCL_UNITS_PER_SQUARE;
|
|
|
|
toPoint.y = (tmp * sin + toPoint.y * cos) / RCL_UNITS_PER_SQUARE;
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2019-10-07 02:41:26 +02:00
|
|
|
result.depth = toPoint.x;
|
2018-09-04 11:42:27 +02:00
|
|
|
|
2019-10-06 21:20:40 +02:00
|
|
|
result.position.x =
|
2019-10-07 02:41:26 +02:00
|
|
|
middleColumn + (-1 * toPoint.y * middleColumn) / RCL_nonZero(result.depth);
|
|
|
|
|
|
|
|
result.position.y = camera.resolution.y / 2 -
|
2020-02-25 17:04:11 +01:00
|
|
|
(((3 * camera.resolution.y) / 4 ) *
|
2019-10-07 02:41:26 +02:00
|
|
|
RCL_perspectiveScale(height - camera.height,result.depth))
|
|
|
|
/ RCL_UNITS_PER_SQUARE + camera.shear;
|
2018-09-04 13:38:04 +02:00
|
|
|
|
2018-09-04 11:42:27 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_degreesToUnitsAngle(int16_t degrees)
|
2018-08-30 08:51:53 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
return (degrees * RCL_UNITS_PER_SQUARE) / 360;
|
2018-08-30 08:51:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_perspectiveScale(RCL_Unit originalSize, RCL_Unit distance)
|
2018-08-30 08:51:53 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_profileCall(RCL_perspectiveScale);
|
2018-09-04 18:55:18 +02:00
|
|
|
|
2018-09-04 19:32:47 +02:00
|
|
|
return distance != 0 ?
|
2018-09-17 17:55:53 +02:00
|
|
|
(originalSize * RCL_UNITS_PER_SQUARE) /
|
|
|
|
((RCL_VERTICAL_FOV * 2 * distance) / RCL_UNITS_PER_SQUARE)
|
2018-09-04 19:32:47 +02:00
|
|
|
: 0;
|
2018-08-30 08:51:53 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit RCL_perspectiveScaleInverse(RCL_Unit originalSize,
|
|
|
|
RCL_Unit scaledSize)
|
2018-09-17 15:05:38 +02:00
|
|
|
{
|
|
|
|
return scaledSize != 0 ?
|
2018-09-28 17:07:32 +02:00
|
|
|
(originalSize * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2) /
|
|
|
|
// ^ take the middle
|
2018-09-17 17:55:53 +02:00
|
|
|
((RCL_VERTICAL_FOV * 2 * scaledSize) / RCL_UNITS_PER_SQUARE)
|
2018-09-28 17:07:32 +02:00
|
|
|
: RCL_INFINITY;
|
2018-09-17 15:05:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_moveCameraWithCollision(RCL_Camera *camera, RCL_Vector2D planeOffset,
|
|
|
|
RCL_Unit heightOffset, RCL_ArrayFunction floorHeightFunc,
|
|
|
|
RCL_ArrayFunction ceilingHeightFunc, int8_t computeHeight, int8_t force)
|
2018-09-06 17:41:09 +02:00
|
|
|
{
|
2018-09-08 07:28:47 +02:00
|
|
|
int8_t movesInPlane = planeOffset.x != 0 || planeOffset.y != 0;
|
|
|
|
int16_t xSquareNew, ySquareNew;
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-12 07:55:29 +02:00
|
|
|
if (movesInPlane || force)
|
2018-09-08 07:28:47 +02:00
|
|
|
{
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Vector2D corner; // BBox corner in the movement direction
|
|
|
|
RCL_Vector2D cornerNew;
|
2018-09-08 07:28:47 +02:00
|
|
|
|
2018-09-20 14:07:27 +02:00
|
|
|
int16_t xDir = planeOffset.x > 0 ? 1 : -1;
|
|
|
|
int16_t yDir = planeOffset.y > 0 ? 1 : -1;
|
2018-09-08 07:28:47 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
corner.x = camera->position.x + xDir * RCL_CAMERA_COLL_RADIUS;
|
|
|
|
corner.y = camera->position.y + yDir * RCL_CAMERA_COLL_RADIUS;
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
int16_t xSquare = RCL_divRoundDown(corner.x,RCL_UNITS_PER_SQUARE);
|
|
|
|
int16_t ySquare = RCL_divRoundDown(corner.y,RCL_UNITS_PER_SQUARE);
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-08 07:28:47 +02:00
|
|
|
cornerNew.x = corner.x + planeOffset.x;
|
|
|
|
cornerNew.y = corner.y + planeOffset.y;
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
xSquareNew = RCL_divRoundDown(cornerNew.x,RCL_UNITS_PER_SQUARE);
|
|
|
|
ySquareNew = RCL_divRoundDown(cornerNew.y,RCL_UNITS_PER_SQUARE);
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-20 14:07:27 +02:00
|
|
|
RCL_Unit bottomLimit = -1 * RCL_INFINITY;
|
|
|
|
RCL_Unit topLimit = RCL_INFINITY;
|
|
|
|
|
|
|
|
if (computeHeight)
|
|
|
|
{
|
|
|
|
bottomLimit = camera->height - RCL_CAMERA_COLL_HEIGHT_BELOW +
|
|
|
|
RCL_CAMERA_COLL_STEP_HEIGHT;
|
|
|
|
|
|
|
|
topLimit = camera->height + RCL_CAMERA_COLL_HEIGHT_ABOVE;
|
|
|
|
}
|
2018-09-06 19:17:31 +02:00
|
|
|
|
2018-09-08 07:28:47 +02:00
|
|
|
// checks a single square for collision against the camera
|
|
|
|
#define collCheck(dir,s1,s2)\
|
2018-09-08 18:07:36 +02:00
|
|
|
if (computeHeight)\
|
2018-09-06 19:17:31 +02:00
|
|
|
{\
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit height = floorHeightFunc(s1,s2);\
|
2018-09-08 07:28:47 +02:00
|
|
|
if (height > bottomLimit)\
|
2018-09-11 17:19:50 +02:00
|
|
|
dir##Collides = 1;\
|
2018-09-08 07:28:47 +02:00
|
|
|
else if (ceilingHeightFunc != 0)\
|
|
|
|
{\
|
2020-02-08 20:17:40 +01:00
|
|
|
RCL_Unit height2 = ceilingHeightFunc(s1,s2);\
|
|
|
|
if ((height2 < topLimit) || ((height2 - height) < \
|
|
|
|
(RCL_CAMERA_COLL_HEIGHT_ABOVE + RCL_CAMERA_COLL_HEIGHT_BELOW)))\
|
2018-09-11 17:19:50 +02:00
|
|
|
dir##Collides = 1;\
|
2018-09-08 07:28:47 +02:00
|
|
|
}\
|
2018-09-06 19:17:31 +02:00
|
|
|
}\
|
2018-09-08 18:07:36 +02:00
|
|
|
else\
|
2018-09-17 17:55:53 +02:00
|
|
|
dir##Collides = floorHeightFunc(s1,s2) > RCL_CAMERA_COLL_STEP_HEIGHT;
|
2018-09-07 16:42:55 +02:00
|
|
|
|
2018-09-08 07:28:47 +02:00
|
|
|
// check a collision against non-diagonal square
|
|
|
|
#define collCheckOrtho(dir,dir2,s1,s2,x)\
|
|
|
|
if (dir##SquareNew != dir##Square)\
|
2018-09-15 12:10:53 +02:00
|
|
|
{\
|
2018-09-08 07:28:47 +02:00
|
|
|
collCheck(dir,s1,s2)\
|
2018-09-15 12:10:53 +02:00
|
|
|
}\
|
2018-09-08 07:28:47 +02:00
|
|
|
if (!dir##Collides)\
|
|
|
|
{ /* now also check for coll on the neighbouring square */ \
|
2018-09-17 17:55:53 +02:00
|
|
|
int16_t dir2##Square2 = RCL_divRoundDown(corner.dir2 - dir2##Dir *\
|
|
|
|
RCL_CAMERA_COLL_RADIUS * 2,RCL_UNITS_PER_SQUARE);\
|
2018-09-08 07:28:47 +02:00
|
|
|
if (dir2##Square2 != dir2##Square)\
|
|
|
|
{\
|
|
|
|
if (x)\
|
|
|
|
collCheck(dir,dir##SquareNew,dir2##Square2)\
|
|
|
|
else\
|
|
|
|
collCheck(dir,dir2##Square2,dir##SquareNew)\
|
|
|
|
}\
|
|
|
|
}
|
2018-09-07 16:42:55 +02:00
|
|
|
|
2018-09-11 15:29:58 +02:00
|
|
|
int8_t xCollides = 0;
|
2018-09-08 07:28:47 +02:00
|
|
|
collCheckOrtho(x,y,xSquareNew,ySquare,1)
|
2018-09-07 16:42:55 +02:00
|
|
|
|
2018-09-11 15:29:58 +02:00
|
|
|
int8_t yCollides = 0;
|
2018-09-08 07:28:47 +02:00
|
|
|
collCheckOrtho(y,x,xSquare,ySquareNew,0)
|
2018-09-07 16:42:55 +02:00
|
|
|
|
2018-09-08 07:28:47 +02:00
|
|
|
#define collHandle(dir)\
|
|
|
|
if (dir##Collides)\
|
2018-09-17 17:55:53 +02:00
|
|
|
cornerNew.dir = (dir##Square) * RCL_UNITS_PER_SQUARE +\
|
|
|
|
RCL_UNITS_PER_SQUARE / 2 + dir##Dir * (RCL_UNITS_PER_SQUARE / 2) -\
|
|
|
|
dir##Dir;\
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-08 10:08:14 +02:00
|
|
|
if (!xCollides && !yCollides) /* if non-diagonal collision happend, corner
|
2018-09-08 07:28:47 +02:00
|
|
|
collision can't happen */
|
2018-09-07 16:42:55 +02:00
|
|
|
{
|
2018-09-08 07:28:47 +02:00
|
|
|
if (xSquare != xSquareNew && ySquare != ySquareNew) // corner?
|
|
|
|
{
|
2018-09-11 15:29:58 +02:00
|
|
|
int8_t xyCollides = 0;
|
2018-09-08 07:28:47 +02:00
|
|
|
collCheck(xy,xSquareNew,ySquareNew)
|
|
|
|
|
|
|
|
if (xyCollides)
|
2018-09-08 10:08:14 +02:00
|
|
|
{
|
2018-09-08 10:53:15 +02:00
|
|
|
// normally should slide, but let's KISS
|
|
|
|
cornerNew = corner;
|
2018-09-08 10:08:14 +02:00
|
|
|
}
|
2018-09-08 07:28:47 +02:00
|
|
|
}
|
2018-09-07 16:42:55 +02:00
|
|
|
}
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-08 10:08:14 +02:00
|
|
|
collHandle(x)
|
|
|
|
collHandle(y)
|
|
|
|
|
2018-09-08 07:28:47 +02:00
|
|
|
#undef collCheck
|
|
|
|
#undef collHandle
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
camera->position.x = cornerNew.x - xDir * RCL_CAMERA_COLL_RADIUS;
|
|
|
|
camera->position.y = cornerNew.y - yDir * RCL_CAMERA_COLL_RADIUS;
|
2018-09-08 07:28:47 +02:00
|
|
|
}
|
2018-09-06 17:41:09 +02:00
|
|
|
|
2018-09-12 07:55:29 +02:00
|
|
|
if (computeHeight && (movesInPlane || heightOffset != 0 || force))
|
2018-09-08 07:28:47 +02:00
|
|
|
{
|
|
|
|
camera->height += heightOffset;
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
int16_t xSquare1 = RCL_divRoundDown(camera->position.x -
|
|
|
|
RCL_CAMERA_COLL_RADIUS,RCL_UNITS_PER_SQUARE);
|
|
|
|
|
|
|
|
int16_t xSquare2 = RCL_divRoundDown(camera->position.x +
|
|
|
|
RCL_CAMERA_COLL_RADIUS,RCL_UNITS_PER_SQUARE);
|
|
|
|
|
|
|
|
int16_t ySquare1 = RCL_divRoundDown(camera->position.y -
|
|
|
|
RCL_CAMERA_COLL_RADIUS,RCL_UNITS_PER_SQUARE);
|
|
|
|
|
|
|
|
int16_t ySquare2 = RCL_divRoundDown(camera->position.y +
|
|
|
|
RCL_CAMERA_COLL_RADIUS,RCL_UNITS_PER_SQUARE);
|
2018-09-08 17:55:46 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit bottomLimit = floorHeightFunc(xSquare1,ySquare1);
|
|
|
|
RCL_Unit topLimit = ceilingHeightFunc != 0 ?
|
|
|
|
ceilingHeightFunc(xSquare1,ySquare1) : RCL_INFINITY;
|
2018-09-08 17:55:46 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
RCL_Unit height;
|
2018-09-08 17:55:46 +02:00
|
|
|
|
2018-09-09 16:29:48 +02:00
|
|
|
#define checkSquares(s1,s2)\
|
|
|
|
{\
|
|
|
|
height = floorHeightFunc(xSquare##s1,ySquare##s2);\
|
2018-09-22 15:17:07 +02:00
|
|
|
bottomLimit = RCL_max(bottomLimit,height);\
|
2018-09-09 16:29:48 +02:00
|
|
|
height = ceilingHeightFunc != 0 ?\
|
2018-09-17 17:55:53 +02:00
|
|
|
ceilingHeightFunc(xSquare##s1,ySquare##s2) : RCL_INFINITY;\
|
2018-09-22 15:17:07 +02:00
|
|
|
topLimit = RCL_min(topLimit,height);\
|
2018-09-08 17:55:46 +02:00
|
|
|
}
|
2018-09-08 07:28:47 +02:00
|
|
|
|
2018-09-09 16:29:48 +02:00
|
|
|
if (xSquare2 != xSquare1)
|
|
|
|
checkSquares(2,1)
|
|
|
|
|
2018-09-08 17:55:46 +02:00
|
|
|
if (ySquare2 != ySquare1)
|
2018-09-09 16:29:48 +02:00
|
|
|
checkSquares(1,2)
|
2018-09-08 17:55:46 +02:00
|
|
|
|
|
|
|
if (xSquare2 != xSquare1 && ySquare2 != ySquare1)
|
2018-09-09 16:29:48 +02:00
|
|
|
checkSquares(2,2)
|
2018-09-08 17:55:46 +02:00
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
camera->height = RCL_clamp(camera->height,
|
|
|
|
bottomLimit + RCL_CAMERA_COLL_HEIGHT_BELOW,
|
|
|
|
topLimit - RCL_CAMERA_COLL_HEIGHT_ABOVE);
|
2018-09-30 15:31:18 +02:00
|
|
|
|
2018-09-09 16:29:48 +02:00
|
|
|
#undef checkSquares
|
2018-09-08 07:28:47 +02:00
|
|
|
}
|
2018-09-06 17:41:09 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_initCamera(RCL_Camera *camera)
|
2018-09-15 14:35:21 +02:00
|
|
|
{
|
|
|
|
camera->position.x = 0;
|
|
|
|
camera->position.y = 0;
|
|
|
|
camera->direction = 0;
|
|
|
|
camera->resolution.x = 20;
|
|
|
|
camera->resolution.y = 15;
|
|
|
|
camera->shear = 0;
|
2018-09-17 17:55:53 +02:00
|
|
|
camera->height = RCL_UNITS_PER_SQUARE;
|
2018-09-15 14:35:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:55:53 +02:00
|
|
|
void RCL_initRayConstraints(RCL_RayConstraints *constraints)
|
2018-09-15 14:35:21 +02:00
|
|
|
{
|
|
|
|
constraints->maxHits = 1;
|
|
|
|
constraints->maxSteps = 20;
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:46:40 +02:00
|
|
|
#endif
|