| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #ifndef TERRAIN_H |
| 20 | #define TERRAIN_H |
| 21 | |
| 22 | #include "vecmat.h" |
| 23 | #include "pstypes.h" |
| 24 | #include "3d.h" |
| 25 | #include "grdefs.h" |
| 26 | #include "object.h" |
| 27 | |
| 28 | // How far we texture out to...after this distance we draw flat shaded polygons |
| 29 | #define DEFAULT_TEXTURE_DISTANCE 9999 |
| 30 | |
| 31 | #define TERRAIN_WIDTH 256 // How many cells across |
| 32 | #define TERRAIN_DEPTH 256 // How many cells down |
| 33 | #define TERRAIN_SIZE 16.0 // The size of each segment, must be a power of 2 |
| 34 | #define TERRAIN_TEX_WIDTH 32 |
| 35 | #define TERRAIN_TEX_DEPTH 32 |
| 36 | |
| 37 | // LOD STUFF |
| 38 | #define MAX_TERRAIN_LOD 4 // How many levels of detail we support |
| 39 | #define SHUTOFF_LOD_DELTA 800000.0f // Forces lod engine not to work for a particular cell |
| 40 | #define SHUTOFF_LOD_INVISIBLE 900000.0f // This LOD is totally invisible |
| 41 | |
| 42 | #define MAX_LOD_SIZE (1 << (MAX_TERRAIN_LOD - 1)) |
| 43 | |
| 44 | #define MAX_CELLS_TO_RENDER 32768 // The maximum number of cells we can render in a frame |
| 45 | |
| 46 | #define MAX_TERRAIN_HEIGHT 350.0f // Max height of our terrain |
| 47 | #define TERRAIN_HEIGHT_INCREMENT (MAX_TERRAIN_HEIGHT / 255.0f) // Incremental jumps |
| 48 | #define DEFAULT_VISIBLE_TERRAIN_DISTANCE 80.0 * TERRAIN_SIZE |
| 49 | |
| 50 | // Sky defines |
| 51 | #define MAX_STARS 600 // how many stars in our sky |
| 52 | #define MAX_SATELLITES 5 // max satellites in our sky |
| 53 | #define MAX_HORIZON_PIECES 16 // how many segments of the horizon there are around our sphere |
| 54 | |
| 55 | // Sky flags |
| 56 | #define TF_STARS 1 // whether or not our terrain is starred |
| 57 | #define TF_SATELLITES 2 // Draw satellites or no? |
| 58 | #define TF_FOG 4 // Draw fog? |
| 59 | #define TF_ROTATE_STARS 8 |
| 60 | #define TF_ROTATE_SKY 16 |
| 61 | |
| 62 | // Satellite flags |
| 63 | #define TSF_HALO 1 // Draw halo? |
| 64 | #define TSF_ATMOSPHERE 2 // Draw atmosphere |
| 65 | |
| 66 | // occlusion stuff |
| 67 | #define OCCLUSION_SIZE 16 |
| 68 | |
| 69 | // Mine/terrain joining |
| 70 | #define MAX_LINK_TILES 4 // how many terrain segments can be attached to mines |
| 71 | |
| 72 | // Terrain segment flags |
| 73 | #define TF_DYNAMIC 1 |
| 74 | #define TF_SPECIAL_WATER 4 // Water |
| 75 | #define TF_SPECIAL_MINE 8 // This segment has a mine attached to it |
| 76 | #define TF_INVISIBLE 16 // This segment is invisible |
| 77 | #define TFM_REGION_MASK (32 + 64 + 128) |
| 78 | // NOTE: 32 64 and 128 are reserved for AI stuff (terrain region partitioning) |
| 79 | |
| 80 | // Terrain cells are on a fixed grid so they have no x and z positions. If you want the x and z |
| 81 | // positions you must calculate them yourself: gridx*TERRAIN_SIZE and gridz*TERRAIN_SIZE |
| 82 | |
| 83 | struct terrain_segment { |
| 84 | float y; // Y position of the lower left corner of the terrain cell |
| 85 | float mody; // The modified y position of this cell - used for LOD |
| 86 | |
| 87 | uint8_t l, r, g, b; |
| 88 | |
| 89 | int16_t objects; // Index of the first object in this cell |
| 90 | int16_t texseg_index; // index into the tex_segment array |
| 91 | |
| 92 | uint8_t flags; // various flags |
| 93 | uint8_t lm_quad; // which lightmap quad this index belongs to |
| 94 | uint8_t ypos; // this is so we don't have to constantly convert |
| 95 | // floats to ints when traversing the terrain |
| 96 | // it's the integer version of pos.y |
| 97 | uint8_t pad; // for alignment |
| 98 | }; |
| 99 | |
| 100 | struct terrain_tex_segment { |
| 101 | uint8_t rotation; |
| 102 | int16_t tex_index; |
| 103 | }; |
| 104 | |
| 105 | // Data for LOD shutoff code |
| 106 | struct lodoff { |
| 107 | int cellnum; |
| 108 | float save_delta[MAX_TERRAIN_LOD]; |
| 109 | }; |
| 110 | |
| 111 | // Data for the sky spherical map |
| 112 | struct terrain_sky { |
| 113 | int textured; // 1=use textures, 0=use gouraud shaded polygon |
| 114 | |
| 115 | // The two subscripts correspond to the top, middle, and bottom of the horizon piece |
| 116 | vector horizon_vectors[MAX_HORIZON_PIECES][6]; |
| 117 | float horizon_u[MAX_HORIZON_PIECES][5]; |
| 118 | float horizon_v[MAX_HORIZON_PIECES][5]; |
| 119 | |
| 120 | int16_t dome_texture; |
| 121 | |
| 122 | float radius; |
| 123 | float rotate_rate; |
| 124 | |
| 125 | ddgr_color sky_color; |
| 126 | ddgr_color horizon_color; |
| 127 | ddgr_color fog_color; |
| 128 | |
| 129 | float satellite_r[MAX_SATELLITES]; |
| 130 | float satellite_g[MAX_SATELLITES]; |
| 131 | float satellite_b[MAX_SATELLITES]; |
| 132 | |
| 133 | vector star_vectors[MAX_STARS]; |
| 134 | vector satellite_vectors[MAX_SATELLITES]; |
| 135 | uint8_t satellite_flags[MAX_SATELLITES]; |
| 136 | float satellite_size[MAX_SATELLITES]; |
| 137 | |
| 138 | uint8_t num_satellites; |
| 139 | uint8_t num_stars; |
| 140 | |
| 141 | int16_t satellite_texture[MAX_SATELLITES]; |
| 142 | |
| 143 | vector lightsource; |
| 144 | angle lightangle; |
| 145 | |
| 146 | float damage_per_second; |
| 147 | float fog_scalar; |
| 148 | |
| 149 | int star_color[MAX_STARS]; |
| 150 | int flags; |
| 151 | }; |
| 152 | |
| 153 | struct link_tile { |
| 154 | int mine_seg; |
| 155 | int mine_side; |
| 156 | int portal_num; |
| 157 | int terrain_seg; |
| 158 | |
| 159 | }; |
| 160 | |
| 161 | struct terrain_mine_list { |
| 162 | int terrain_seg; |
| 163 | uint8_t num_segs; |
| 164 | int16_t mine_segs[50]; |
| 165 | }; |
| 166 | |
| 167 | struct terrain_render_info { |
| 168 | float z; |
| 169 | uint16_t right_edge, left_edge, top_edge, bottom_edge; // for fixing tjoint problems |
| 170 | uint8_t right_count, left_count, top_count, bottom_count; |
| 171 | uint16_t segment; // what segment to render |
| 172 | uint8_t lod; // what level of detail: 0=16x16, 1=8x8, 2=4x4, 3=2x2, 4=just this segment (1x1) |
| 173 | uint8_t pad; |
| 174 | }; |
| 175 | |
| 176 | struct terrain_normals { |
| 177 | vector normal1; // Upper left triangle |
| 178 | vector normal2; // Lower right triangle |
| 179 | }; |
| 180 | |
| 181 | extern uint8_t Terrain_dynamic_table[]; |
| 182 | extern terrain_normals *TerrainNormals[MAX_TERRAIN_LOD]; |
| 183 | |
| 184 | // Occlusion data for knowing what to draw |
| 185 | extern uint8_t Terrain_occlusion_map[256][32]; |
| 186 | extern int Terrain_occlusion_checksum; |
| 187 | |
| 188 | extern int Check_terrain_portal; |
| 189 | extern int Terrain_checksum; |
| 190 | |
| 191 | // Our lighting maps for the terrain, one for each quadrant (starting at lower left) |
| 192 | extern int TerrainLightmaps[4]; |
| 193 | |
| 194 | extern int GlobalTransCount, TotalDepth; |
| 195 | extern int TerrainEdgeTest[MAX_TERRAIN_LOD][16]; |
| 196 | |
| 197 | extern terrain_render_info Terrain_list[]; |
| 198 | extern uint16_t TS_FrameCount; |
| 199 | |
| 200 | extern float VisibleTerrainZ; |
| 201 | extern float Terrain_average_height; |
| 202 | |
| 203 | extern float Clip_scale_left, Clip_scale_right, Clip_scale_top, Clip_scale_bot; |
| 204 | extern uint8_t Terrain_from_mine; |
| 205 | |
| 206 | extern float Last_terrain_render_time; |
| 207 | |
| 208 | extern terrain_segment Terrain_seg[TERRAIN_WIDTH * TERRAIN_DEPTH]; |
| 209 | extern terrain_tex_segment Terrain_tex_seg[TERRAIN_TEX_WIDTH * TERRAIN_TEX_DEPTH]; |
| 210 | |
| 211 | // first object to render after cell has been rendered (only used for SW renderer) |
| 212 | extern int16_t Terrain_seg_render_objs[]; |
| 213 | |
| 214 | #ifdef RELEASE |
| 215 | #define TERRAIN_REGION(x) ((Terrain_seg[0x7FFFFFFF & x].flags & TFM_REGION_MASK) >> 5) |
| 216 | #else // debug(-ish) builds - check if x is valid |
| 217 | static inline int TERRAIN_REGION(int x) { |
| 218 | ASSERT(x != -1 && "invalid/unset room number (-1)!"); |
| 219 | // Note: due to the 0x7FFFFFFF mask, terrSegIdx will be >= 0 |
| 220 | int terrSegIdx = 0x7FFFFFFF & x; |
| 221 | // catch other invalid cell/segment numbers than -1 as well |
| 222 | ASSERT((terrSegIdx < TERRAIN_WIDTH * TERRAIN_DEPTH) && "invalid cellnum!"); |
| 223 | return (Terrain_seg[terrSegIdx].flags & TFM_REGION_MASK) >> 5; |
| 224 | } |
| 225 | #endif |
| 226 | |
| 227 | extern terrain_sky Terrain_sky; |
| 228 | |
| 229 | #if (defined(EDITOR) || defined(NEWEDITOR)) |
| 230 | extern int Editor_LOD_engine_off; |
| 231 | extern bool Terrain_render_ext_room_objs; |
| 232 | #endif |
| 233 | |
| 234 | extern int Terrain_LOD_engine_off; |
| 235 | |
| 236 | extern float Terrain_texture_distance; // how far we should texture before going to flat shad |
| 237 | |
| 238 | extern uint8_t TerrainJoinMap[]; |
| 239 | extern float *TerrainDeltaBlocks[]; |
| 240 | extern uint8_t *Terrain_max_height_int[]; |
| 241 | extern uint8_t *Terrain_min_height_int[]; |
| 242 | extern uint8_t Fast_terrain; |
| 243 | extern uint8_t Flat_terrain; |
| 244 | extern uint8_t Show_invisible_terrain; |
| 245 | |
| 246 | extern int Camera_direction, Sort_direction; |
| 247 | |
| 248 | #if (defined(_DEBUG) || defined(EDITOR) || defined(NEWEDITOR)) |
| 249 | extern uint8_t TerrainSelected[]; |
| 250 | extern int Num_terrain_selected; |
| 251 | #endif |
| 252 | |
| 253 | extern uint16_t *Terrain_rotate_list; // which points have been sub/rotated this frame |
| 254 | extern g3Point *World_point_buffer; // Rotated points |
| 255 | |
| 256 | #define TSEARCH_FOUND_TERRAIN 0 |
| 257 | #define TSEARCH_FOUND_MINE 1 |
| 258 | #define TSEARCH_FOUND_SATELLITE 2 |
| 259 | #define TSEARCH_FOUND_SKY_DOME 3 |
| 260 | #define TSEARCH_FOUND_SKY_BAND 4 |
| 261 | #define TSEARCH_FOUND_OBJECT 5 |
| 262 | #ifdef NEWEDITOR |
| 263 | #define TSEARCH_FOUND_NODE 6 |
| 264 | #define TSEARCH_FOUND_BNODE 7 |
| 265 | #endif |
| 266 | |
| 267 | extern int TSearch_on, TSearch_found_type, TSearch_x, TSearch_y, TSearch_seg, TSearch_face; |
| 268 | |
| 269 | extern void InitTerrain(); |
| 270 | |
| 271 | // Called whenever a new level is initted |
| 272 | extern void ResetTerrain(int force = 0); |
| 273 | |
| 274 | extern int GetVisibleTerrain(vector *, matrix *); |
| 275 | extern void DisplayTerrainList(int, bool from_automap = 0); |
| 276 | extern int CheckToRenderSky(int); |
| 277 | |
| 278 | // left,top,right,bot are optional parameters. Omiting them (or setting them to -1) will |
| 279 | // render to the whole screen. Passing valid values will only render tiles visible in the |
| 280 | // specified window (though it won't clip those tiles to the window) |
| 281 | void RenderTerrain(uint8_t from_mine, int left = -1, int top = -1, int right = -1, int bot = -1); |
| 282 | |
| 283 | void GetPreRotatedPoint(g3Point *, int, int, int); |
| 284 | void GenerateTerrainLight(); |
| 285 | |
| 286 | void BuildMinMaxTerrain(); |
| 287 | void BuildTerrainNormals(); |
| 288 | |
| 289 | int DrawTerrainTriangles(int n); |
| 290 | int LoadPCXTerrain(char *); |
| 291 | |
| 292 | // Given a position, returns the terrain segment that that position is in/over |
| 293 | // The return value is a pure cell number, meaning it doesn't have the outside flag set |
| 294 | int GetTerrainCellFromPos(vector *pos); |
| 295 | |
| 296 | // Given a position, returns the terrain segment that that position is in/over |
| 297 | // The return value is a valid room number, meaning it has the outside flag set |
| 298 | int GetTerrainRoomFromPos(vector *pos); |
| 299 | |
| 300 | // Given a position, returns the collision terrain segment that that position is in/over |
| 301 | int GetColTerrainSegFromPos(vector *pos); |
| 302 | // Given a terrain cell, returns the collision terrain segment that that position is in/over |
| 303 | int GetColTerrainSegFromTerrainSeg(int cell_index); |
| 304 | |
| 305 | // Computes the center of the segment in x,z and also sets y touching the ground |
| 306 | void ComputeTerrainSegmentCenter(vector *pos, int segnum); |
| 307 | // Given an position, returns the terrain Y coord at that location |
| 308 | // Also now can return the normal at that ground point |
| 309 | float GetTerrainGroundPoint(vector *pos, vector *normal = NULL); |
| 310 | |
| 311 | void SetupSky(float radius, int flags, uint8_t randit = 0); |
| 312 | |
| 313 | // Builds the surface normal for terrain segment n |
| 314 | void BuildNormalForTerrainSegment(int n); |
| 315 | |
| 316 | // Builds the vertex normal for terrain segment n |
| 317 | void BuildLightingNormalForSegment(int n); |
| 318 | |
| 319 | // Gets a prerotated point that does not fall exactly on one of our 255 height values |
| 320 | void GetSpecialRotatedPoint(g3Point *dest, int x, int z, float yvalue); |
| 321 | |
| 322 | // Takes our light angle and fills in the appropriate values in the lightsource vector |
| 323 | void GenerateLightSource(); |
| 324 | |
| 325 | // Returns the terrain segment index of the farthest point that mine segment "mine_seg" |
| 326 | // touches. Based on Camera_direction |
| 327 | int GetFurthestMineSegment(int mine_seg); |
| 328 | |
| 329 | // codes a point for visibility in the window passed to RenderTerrain() |
| 330 | uint8_t CodeTerrainPoint(g3Point *p); |
| 331 | |
| 332 | // Generates info for the LOD engine...must be called after the geometry changes |
| 333 | void GenerateLODDeltas(); |
| 334 | |
| 335 | // Generates lightmaps based on the info given by the .light field of each Terrain_seg |
| 336 | void UpdateTerrainLightmaps(); |
| 337 | |
| 338 | // Makes a four sided rectangle pinched into a triangle |
| 339 | void MakePinchBitmap(int dest_bm, int src_bm); |
| 340 | |
| 341 | // Gets the dynamic light value for this position |
| 342 | float GetTerrainDynamicScalar(vector *pos, int seg); |
| 343 | |
| 344 | // Shuts off LOD for a given cell |
| 345 | void TurnOffLODForCell(int cellnum); |
| 346 | |
| 347 | // Restores the terrain deltas to their original state |
| 348 | void ClearLODOffs(); |
| 349 | |
| 350 | // Gets the checksum for this terrain |
| 351 | int GetTerrainGeometryChecksum(); |
| 352 | |
| 353 | #endif |