| 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 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/render.cpp $ |
| 21 | * $Revision: 325 $ |
| 22 | * $Date: 9/10/01 5:16p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Code to render the mine |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/render.cpp $ |
| 28 | * |
| 29 | * $NoKeywords: $ |
| 30 | */ |
| 31 | #include "render.h" |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include "3d.h" |
| 35 | #include "mono.h" |
| 36 | #include "gametexture.h" |
| 37 | #include "game.h" |
| 38 | #include "renderobject.h" |
| 39 | #include "door.h" |
| 40 | #include "terrain.h" |
| 41 | #include "renderer.h" |
| 42 | #include "room.h" |
| 43 | #include "lighting.h" |
| 44 | #include "lightmap.h" |
| 45 | #include "limits.h" |
| 46 | #include "lightmap_info.h" |
| 47 | #include "viseffect.h" |
| 48 | #include "weapon.h" |
| 49 | #include "fireball.h" |
| 50 | #include "scorch.h" |
| 51 | #include "findintersection.h" |
| 52 | #include "special_face.h" |
| 53 | #include "config.h" |
| 54 | #include "gameloop.h" |
| 55 | #include "doorway.h" |
| 56 | #include "TelComAutoMap.h" |
| 57 | #include "postrender.h" |
| 58 | #include "mem.h" |
| 59 | #include "psrand.h" |
| 60 | #include "player.h" |
| 61 | #include "args.h" |
| 62 | #ifdef EDITOR |
| 63 | #include "editor\d3edit.h" |
| 64 | #endif |
| 65 | |
| 66 | #include <algorithm> |
| 67 | |
| 68 | // #define KATMAI |
| 69 | |
| 70 | // Katmai enhanced rotate only in a release build, because not |
| 71 | // everyone has the intel compiler! |
| 72 | #if (defined(RELEASE) && defined(KATMAI)) |
| 73 | // Katmai version -- Rotates all the points in a room |
| 74 | void RotateRoomPoints(room *rp, vector4 *world_vecs); |
| 75 | #endif |
| 76 | |
| 77 | |
| 78 | static int Faces_rendered = 0; |
| 79 | extern float GetFPS(); |
| 80 | extern uint8_t Outline_release_mode; |
| 81 | // 3d point for each vertex for use during rendering a room |
| 82 | uint8_t Room_clips[MAX_VERTS_PER_ROOM]; // used for face culling |
| 83 | // default face reflectivity |
| 84 | float Face_reflectivity = 0.5; |
| 85 | // the position of the viewer - valid while a frame is being rendered |
| 86 | static vector Viewer_eye; |
| 87 | static matrix Viewer_orient; |
| 88 | int Viewer_roomnum; |
| 89 | int Flag_automap, Called_from_terrain; |
| 90 | // Fog zone variables |
| 91 | float Fog_zone_start = FLT_MAX, Fog_zone_end = FLT_MAX; |
| 92 | int Must_render_terrain; |
| 93 | int Global_buffer_index; |
| 94 | int No_render_windows_hack = -1; |
| 95 | #define WALL_PULSE_INCREMENT .01 |
| 96 | // Variables for various debugging features |
| 97 | #if (defined(_DEBUG) || defined(NEWEDITOR) || defined(EDITOR)) |
| 98 | // If true, draw white outline for each polygon |
| 99 | int Render_portals = 0; |
| 100 | uint8_t Outline_mode = 0; |
| 101 | uint8_t Shell_render_flag = 0; |
| 102 | bool Outline_alpha = 0; |
| 103 | bool Outline_lightmaps = 0; |
| 104 | bool Render_floating_triggers = 0; |
| 105 | bool Use_software_zbuffer = 0; |
| 106 | bool Lighting_on = 1; |
| 107 | bool Render_all_external_rooms = 0; |
| 108 | bool In_editor_mode = 0; |
| 109 | bool Render_one_room_only = 0; |
| 110 | bool Render_inside_only = 0; |
| 111 | #else |
| 112 | #define In_editor_mode 0 |
| 113 | #define Outline_lightmaps 0 |
| 114 | #define Outline_alpha 0 |
| 115 | #define Render_floating_triggers 0 |
| 116 | #define Use_software_zbuffer 0 |
| 117 | #define Render_all_external_rooms 0 |
| 118 | #define Render_portals 0 |
| 119 | #define Render_one_room_only 0 |
| 120 | #define Render_inside_only 0 |
| 121 | #define Shell_render_flag 0 |
| 122 | #endif |
| 123 | |
| 124 | #ifndef RELEASE |
| 125 | int Mine_depth; |
| 126 | #endif |
| 127 | #ifndef EDITOR |
| 128 | #define Search_lightmaps 0 |
| 129 | #else // ifndef EDITOR |
| 130 | // Vars for find_seg_side_face() |
| 131 | static int Search_lightmaps = 0; // true if searching for a lightmap |
| 132 | int found_lightmap; |
| 133 | #endif // ifndef EDITOR |
| 134 | bool Vsync_enabled = true; |
| 135 | // Prototypes |
| 136 | void RenderRoomObjects(room *rp); |
| 137 | // The current window width & height (valid while rendering) |
| 138 | static int Render_width, Render_height; |
| 139 | int Clear_window_color = -1; |
| 140 | int Clear_window = 2; // 1 = Clear whole background window, 2 = clear view portals into rest of world, 0 = no clear |
| 141 | #define MAX_RENDER_ROOMS 100 |
| 142 | char Rooms_visited[MAX_ROOMS + MAX_PALETTE_ROOMS]; |
| 143 | int Facing_visited[MAX_ROOMS + MAX_PALETTE_ROOMS]; |
| 144 | // For keeping track of portal recursion |
| 145 | uint8_t Room_depth_list[MAX_ROOMS + MAX_PALETTE_ROOMS]; |
| 146 | int16_t Render_list[MAX_RENDER_ROOMS]; |
| 147 | int16_t External_room_list[MAX_ROOMS]; |
| 148 | int N_external_rooms; |
| 149 | // For rendering specular faces |
| 150 | #define MAX_SPECULAR_FACES 2500 |
| 151 | int16_t Specular_faces[MAX_SPECULAR_FACES]; |
| 152 | int Num_specular_faces_to_render = 0; |
| 153 | int Num_real_specular_faces_to_render = 0; // Non-invisible specular faces |
| 154 | struct smooth_spec_vert { |
| 155 | float r, g, b; |
| 156 | int used; |
| 157 | }; |
| 158 | smooth_spec_vert Smooth_verts[MAX_VERTS_PER_ROOM]; |
| 159 | // For scorch rendering |
| 160 | uint16_t Scorches_to_render[MAX_FACES_PER_ROOM]; |
| 161 | int Num_scorches_to_render = 0; |
| 162 | // For rendering volumetric fog |
| 163 | #define MAX_FOGGED_ROOMS_PER_FRAME 8 |
| 164 | fog_portal_data Fog_portal_data[MAX_FOGGED_ROOMS_PER_FRAME]; |
| 165 | int Num_fogged_rooms_this_frame = 0; |
| 166 | float Room_light_val = 0; |
| 167 | int Room_fog_plane_check = 0; |
| 168 | float Room_fog_distance = 0; |
| 169 | float Room_fog_eye_distance = 0; |
| 170 | vector Room_fog_plane, Room_fog_portal_vert; |
| 171 | int16_t Fog_faces[MAX_FACES_PER_ROOM]; |
| 172 | int Num_fog_faces_to_render = 0; |
| 173 | #define MAX_EXTERNAL_ROOMS 100 |
| 174 | vector External_room_corners[MAX_EXTERNAL_ROOMS][8]; |
| 175 | uint8_t External_room_codes[MAX_EXTERNAL_ROOMS]; |
| 176 | uint8_t External_room_project_net[MAX_EXTERNAL_ROOMS]; |
| 177 | // For light glows |
| 178 | #define MAX_LIGHT_GLOWS 100 |
| 179 | #define LGF_USED 1 |
| 180 | #define LGF_INCREASING 2 |
| 181 | #define LGF_FAST 4 |
| 182 | struct light_glow { |
| 183 | int16_t roomnum; |
| 184 | int16_t facenum; |
| 185 | float size; |
| 186 | vector center; |
| 187 | float scalar; |
| 188 | uint8_t flags; |
| 189 | }; |
| 190 | light_glow LightGlows[MAX_LIGHT_GLOWS]; |
| 191 | light_glow LightGlowsThisFrame[MAX_LIGHT_GLOWS]; |
| 192 | int FastCoronas = 0; |
| 193 | int Num_glows = 0, Num_glows_this_frame = 0; |
| 194 | // For sorting our textures in state limited environments |
| 195 | state_limited_element State_elements[MAX_STATE_ELEMENTS]; |
| 196 | // For terrain portals |
| 197 | int Terrain_portal_left, Terrain_portal_right, Terrain_portal_top, Terrain_portal_bottom; |
| 198 | // For deformation effect |
| 199 | vector Global_alter_vec = {19, -19, 19}; |
| 200 | // For detail stuff (mirrors, specular,etc) |
| 201 | bool Render_mirror_for_room = false; |
| 202 | int Mirror_room; |
| 203 | int Num_mirrored_rooms; |
| 204 | int16_t Mirrored_room_list[MAX_ROOMS]; |
| 205 | uint8_t Mirrored_room_checked[MAX_ROOMS]; |
| 206 | int16_t Mirror_rooms[MAX_ROOMS]; |
| 207 | int Num_mirror_rooms = 0; |
| 208 | // |
| 209 | // UTILITY FUNCS |
| 210 | // |
| 211 | // Determines if a face renders |
| 212 | // Parameters: rp - pointer to room that contains the face |
| 213 | // fp - pointer to the face in question |
| 214 | static inline bool FaceIsRenderable(room *rp, face *fp) { |
| 215 | // Check for a floating trigger, which doesn't get rendered |
| 216 | if ((fp->flags & FF_FLOATING_TRIG) && (!In_editor_mode || !Render_floating_triggers)) |
| 217 | return 0; |
| 218 | // Check for face that's part of a portal |
| 219 | if (fp->portal_num != -1) { |
| 220 | if (rp->portals[fp->portal_num].flags & PF_RENDER_FACES) |
| 221 | return 1; |
| 222 | if (rp->flags & RF_FOG && !In_editor_mode) |
| 223 | return 1; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | // Nothing special, so face renders |
| 228 | return 1; |
| 229 | } |
| 230 | // Flags for GetFaceAlpha() |
| 231 | #define FA_CONSTANT 1 // face has a constant alpha for the whole face |
| 232 | #define FA_VERTEX 2 // face has different alpha per vertex |
| 233 | #define FA_TRANSPARENT 4 // face has transparency (i.e. per pixel 1-bit alpha) |
| 234 | // Determines if a face draws with alpha blending |
| 235 | // Parameters: fp - pointer to the face in question |
| 236 | // bm_handle - the handle for the bitmap for this frame, or -1 if don't care about |
| 237 | // transparence Returns: bitmask describing the alpha blending for the face |
| 238 | // the return bits are the ATF_ flags in renderer.h |
| 239 | static inline int GetFaceAlpha(face *fp, int bm_handle) { |
| 240 | int ret = AT_ALWAYS; |
| 241 | if (GameTextures[fp->tmap].flags & TF_SATURATE) { |
| 242 | ret = AT_SATURATE_TEXTURE; |
| 243 | } else { |
| 244 | // Check the face's texture for an alpha value |
| 245 | if (GameTextures[fp->tmap].alpha < 1.0) |
| 246 | ret |= ATF_CONSTANT; |
| 247 | |
| 248 | // Check for transparency |
| 249 | if (GameBitmaps[bm_handle].format != BITMAP_FORMAT_4444 && GameTextures[fp->tmap].flags & TF_TMAP2) |
| 250 | ret |= ATF_TEXTURE; |
| 251 | } |
| 252 | return ret; |
| 253 | } |
| 254 | // Determine if you should render through a portal |
| 255 | // Parameters: rp - the room the portal is in |
| 256 | // pp - the portal we're checking |
| 257 | // Returns: true if you should render the room to which the portal connects |
| 258 | static inline bool RenderPastPortal(room *rp, portal *pp) { |
| 259 | // If we don't render the portal's faces, then we see through it |
| 260 | if (!(pp->flags & PF_RENDER_FACES)) |
| 261 | return 1; |
| 262 | if (!UseHardware) // Don't render alpha stuff in software |
| 263 | return 0; |
| 264 | // Check if the face's texture has transparency |
| 265 | face *fp = &rp->faces[pp->portal_face]; |
| 266 | if (GameTextures[fp->tmap].flags & TF_PROCEDURAL) |
| 267 | return 1; |
| 268 | int bm_handle = GetTextureBitmap(fp->tmap, 0); |
| 269 | if (GetFaceAlpha(fp, bm_handle)) |
| 270 | return 1; // Face has alpha or transparency, so we can see through it |
| 271 | else |
| 272 | return 0; // Not transparent, so no render past |
| 273 | } |
| 274 | |
| 275 | // used during rendering as count of items in render_list[] |
| 276 | int N_render_rooms; |
| 277 | int first_terminal_room; |
| 278 | #define round(a) (int((a) + 0.5f)) |
| 279 | |
| 280 | #ifdef EDITOR |
| 281 | #define CROSS_WIDTH 8.0 |
| 282 | #define CROSS_HEIGHT 8.0 |
| 283 | #define CURFACE_COLOR GR_RGB(255, 255, 0) |
| 284 | #define CUREDGE_COLOR GR_RGB(0, 255, 0) |
| 285 | #define MARKEDFACE_COLOR GR_RGB(0, 255, 255) |
| 286 | #define MARKEDEDGE_COLOR GR_RGB(0, 150, 150) |
| 287 | #define PLACED_COLOR GR_RGB(255, 0, 255) |
| 288 | // Draw outline for current edge & vertex |
| 289 | void OutlineCurrentFace(room *rp, int facenum, int edgenum, int vertnum, ddgr_color face_color, ddgr_color edge_color) { |
| 290 | face *fp = &rp->faces[facenum]; |
| 291 | g3Point p0, p1; |
| 292 | uint8_t c0, c1; |
| 293 | int v; |
| 294 | for (v = 0; v < fp->num_verts; v++) { |
| 295 | c0 = g3_RotatePoint(&p0, &rp->verts[fp->face_verts[v]]); |
| 296 | c1 = g3_RotatePoint(&p1, &rp->verts[fp->face_verts[(v + 1) % fp->num_verts]]); |
| 297 | if (!(c0 & c1)) { // both not off screen? |
| 298 | // Draw current edge in green |
| 299 | g3_DrawLine((v == edgenum) ? edge_color : face_color, &p0, &p1); |
| 300 | } |
| 301 | if ((v == vertnum) && (c0 == 0)) { |
| 302 | // Draw a little cross at the current vert |
| 303 | g3_ProjectPoint(&p0); // make sure projected |
| 304 | rend_SetFlatColor(edge_color); |
| 305 | rend_DrawLine(p0.p3_sx - CROSS_WIDTH, p0.p3_sy, p0.p3_sx, p0.p3_sy - CROSS_HEIGHT); |
| 306 | rend_DrawLine(p0.p3_sx, p0.p3_sy - CROSS_HEIGHT, p0.p3_sx + CROSS_WIDTH, p0.p3_sy); |
| 307 | rend_DrawLine(p0.p3_sx + CROSS_WIDTH, p0.p3_sy, p0.p3_sx, p0.p3_sy + CROSS_HEIGHT); |
| 308 | rend_DrawLine(p0.p3_sx, p0.p3_sy + CROSS_HEIGHT, p0.p3_sx - CROSS_WIDTH, p0.p3_sy); |
| 309 | } |
| 310 | } |
| 311 | // Draw upper left cross |
| 312 | if (Outline_lightmaps && (rp->faces[facenum].flags & FF_LIGHTMAP)) { |
| 313 | ASSERT(rp->faces[facenum].lmi_handle != BAD_LMI_INDEX); |
| 314 | |
| 315 | p0.p3_flags = 0; |
| 316 | c0 = g3_RotatePoint(&p0, &LightmapInfo[rp->faces[facenum].lmi_handle].upper_left); |
| 317 | if (!c0) { |
| 318 | // Draw a little cross at the current vert |
| 319 | g3_ProjectPoint(&p0); // make sure projected |
| 320 | rend_SetFlatColor(GR_RGB(255, 0, 0)); |
| 321 | rend_DrawLine(p0.p3_sx - CROSS_WIDTH, p0.p3_sy, p0.p3_sx, p0.p3_sy - CROSS_HEIGHT); |
| 322 | rend_DrawLine(p0.p3_sx, p0.p3_sy - CROSS_HEIGHT, p0.p3_sx + CROSS_WIDTH, p0.p3_sy); |
| 323 | rend_DrawLine(p0.p3_sx + CROSS_WIDTH, p0.p3_sy, p0.p3_sx, p0.p3_sy + CROSS_HEIGHT); |
| 324 | rend_DrawLine(p0.p3_sx, p0.p3_sy + CROSS_HEIGHT, p0.p3_sx - CROSS_WIDTH, p0.p3_sy); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Draw a room rotated and placed in space |
| 330 | static void DrawPlacedRoomFace(room *rp, vector *rotpoint, matrix *rotmat, vector *placepoint, int facenum, int color) { |
| 331 | face *fp = &rp->faces[facenum]; |
| 332 | |
| 333 | g3Point p0, p1; |
| 334 | uint8_t c0, c1; |
| 335 | int v; |
| 336 | for (v = 0; v < fp->num_verts; v++) { |
| 337 | vector tv; |
| 338 | |
| 339 | tv = (rp->verts[fp->face_verts[v]] - *rotpoint) * *rotmat + *placepoint; |
| 340 | c0 = g3_RotatePoint(&p0, &tv); |
| 341 | |
| 342 | tv = (rp->verts[fp->face_verts[(v + 1) % fp->num_verts]] - *rotpoint) * *rotmat + *placepoint; |
| 343 | c1 = g3_RotatePoint(&p1, &tv); |
| 344 | |
| 345 | if (!(c0 & c1)) // both not off screen? |
| 346 | g3_DrawLine(color, &p0, &p1); |
| 347 | } |
| 348 | } |
| 349 | #endif // ifdef EDITOR |
| 350 | |
| 351 | struct clip_wnd { |
| 352 | float left, top, right, bot; |
| 353 | }; |
| 354 | |
| 355 | static inline int clip2d(g3Point *pnt, clip_wnd *wnd) { |
| 356 | int ret = 0; |
| 357 | if (pnt->p3_codes & CC_BEHIND) |
| 358 | return CC_BEHIND; |
| 359 | |
| 360 | if (pnt->p3_sx < wnd->left) |
| 361 | ret |= CC_OFF_LEFT; |
| 362 | if (pnt->p3_sx > wnd->right) |
| 363 | ret |= CC_OFF_RIGHT; |
| 364 | if (pnt->p3_sy < wnd->top) |
| 365 | ret |= CC_OFF_TOP; |
| 366 | if (pnt->p3_sy > wnd->bot) |
| 367 | ret |= CC_OFF_BOT; |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | // Returns true if a line intersects another line |
| 372 | static inline bool LineIntersectsLine(g3Point *ls, g3Point *le, float x1, float y1, float x2, float y2) { |
| 373 | float num, denom; |
| 374 | num = ((ls->p3_sy - y1) * (x2 - x1)) - ((ls->p3_sx - x1) * (y2 - y1)); |
| 375 | denom = ((le->p3_sx - ls->p3_sx) * (y2 - y1)) - ((le->p3_sy - ls->p3_sy) * (x2 - x1)); |
| 376 | float r = num / denom; |
| 377 | if (r >= 0.0 && r <= 1.0) |
| 378 | return true; |
| 379 | num = ((ls->p3_sy - y1) * (le->p3_sx - ls->p3_sx)) - ((ls->p3_sx - x1) * (le->p3_sy - ls->p3_sy)); |
| 380 | denom = ((le->p3_sx - ls->p3_sx) * (y2 - y1)) - ((le->p3_sy - ls->p3_sy) * (x2 - x1)); |
| 381 | float s = num / denom; |
| 382 | if (s >= 0.0 && s <= 1.0) |
| 383 | return true; |
| 384 | return false; |
| 385 | } |
| 386 | // Returns true if a face intersects the passed in portal in any way |
| 387 | static inline bool FaceIntersectsPortal(room *rp, face *fp, clip_wnd *wnd) { |
| 388 | g3Codes cc; |
| 389 | int i; |
| 390 | |
| 391 | cc.cc_or = 0; |
| 392 | cc.cc_and = 0xff; |
| 393 | for (i = 0; i < fp->num_verts; i++) { |
| 394 | cc.cc_or |= Room_clips[fp->face_verts[i]]; |
| 395 | cc.cc_and &= Room_clips[fp->face_verts[i]]; |
| 396 | } |
| 397 | if (cc.cc_and) |
| 398 | return false; // completely outside |
| 399 | if (!cc.cc_or) |
| 400 | return true; // completely inside |
| 401 | |
| 402 | // Now we must do a check |
| 403 | for (i = 0; i < fp->num_verts; i++) { |
| 404 | g3Point *p1 = &World_point_buffer[rp->wpb_index + fp->face_verts[i]]; |
| 405 | g3Point *p2 = &World_point_buffer[rp->wpb_index + fp->face_verts[(i + 1) % fp->num_verts]]; |
| 406 | if (LineIntersectsLine(p1, p2, wnd->left, wnd->top, wnd->right, wnd->top)) |
| 407 | return true; |
| 408 | if (LineIntersectsLine(p1, p2, wnd->right, wnd->top, wnd->right, wnd->bot)) |
| 409 | return true; |
| 410 | if (LineIntersectsLine(p1, p2, wnd->right, wnd->bot, wnd->left, wnd->bot)) |
| 411 | return true; |
| 412 | if (LineIntersectsLine(p1, p2, wnd->left, wnd->bot, wnd->left, wnd->top)) |
| 413 | return true; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | // Sets the status of a glow light |
| 418 | void SetGlowStatus(int roomnum, int facenum, vector *center, float size, int fast) { |
| 419 | int i; |
| 420 | int first = 1; |
| 421 | int first_free = -1; |
| 422 | int done = 0; |
| 423 | int count = 0; |
| 424 | int found = 0; |
| 425 | for (i = 0; i < MAX_LIGHT_GLOWS && !done; i++) { |
| 426 | if (count >= Num_glows) { |
| 427 | if (first_free == -1) |
| 428 | first_free = i; |
| 429 | done = 1; |
| 430 | continue; |
| 431 | } |
| 432 | if (LightGlows[i].flags & LGF_USED) { |
| 433 | count++; |
| 434 | if (LightGlows[i].roomnum == roomnum && LightGlows[i].facenum == facenum) { |
| 435 | found = 1; |
| 436 | LightGlows[i].flags |= LGF_INCREASING; |
| 437 | done = 1; |
| 438 | } |
| 439 | } else { |
| 440 | if (first) { |
| 441 | first_free = i; |
| 442 | first = 0; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | if (!found) // couldn't find it - is it a new one? |
| 447 | { |
| 448 | if (first_free == -1) // no free slots |
| 449 | return; |
| 450 | LightGlows[first_free].flags = LGF_USED | LGF_INCREASING; |
| 451 | if (fast) |
| 452 | LightGlows[first_free].flags |= LGF_FAST; |
| 453 | LightGlows[first_free].roomnum = roomnum; |
| 454 | LightGlows[first_free].facenum = facenum; |
| 455 | LightGlows[first_free].scalar = 0; |
| 456 | LightGlows[first_free].size = size; |
| 457 | LightGlows[first_free].center = *center; |
| 458 | Num_glows++; |
| 459 | } |
| 460 | } |
| 461 | // Takes a min,max vector and makes a surrounding cube from it |
| 462 | void MakePointsFromMinMax(vector *corners, vector *minp, vector *maxp) { |
| 463 | corners[0].x = minp->x; |
| 464 | corners[0].y = maxp->y; |
| 465 | corners[0].z = minp->z; |
| 466 | corners[1].x = maxp->x; |
| 467 | corners[1].y = maxp->y; |
| 468 | corners[1].z = minp->z; |
| 469 | corners[2].x = maxp->x; |
| 470 | corners[2].y = minp->y; |
| 471 | corners[2].z = minp->z; |
| 472 | corners[3].x = minp->x; |
| 473 | corners[3].y = minp->y; |
| 474 | corners[3].z = minp->z; |
| 475 | corners[4].x = minp->x; |
| 476 | corners[4].y = maxp->y; |
| 477 | corners[4].z = maxp->z; |
| 478 | corners[5].x = maxp->x; |
| 479 | corners[5].y = maxp->y; |
| 480 | corners[5].z = maxp->z; |
| 481 | corners[6].x = maxp->x; |
| 482 | corners[6].y = minp->y; |
| 483 | corners[6].z = maxp->z; |
| 484 | corners[7].x = minp->x; |
| 485 | corners[7].y = minp->y; |
| 486 | corners[7].z = maxp->z; |
| 487 | } |
| 488 | |
| 489 | // Rotates all the points in a room |
| 490 | void RotateRoomPoints(room *rp, vector *world_vecs) { |
| 491 | int i; |
| 492 | // Jig the vertices a bit if being deformed |
| 493 | if (Viewer_object->effect_info && (Viewer_object->effect_info->type_flags & EF_DEFORM)) { |
| 494 | for (i = 0; i < rp->num_verts; i++) { |
| 495 | vector vec = world_vecs[i]; |
| 496 | float val = ((ps_rand() % 1000) - 500.0) / 500.0; |
| 497 | val *= Viewer_object->effect_info->deform_time; |
| 498 | vec += Global_alter_vec * (Viewer_object->effect_info->deform_range * val); |
| 499 | |
| 500 | g3_RotatePoint(&World_point_buffer[rp->wpb_index + i], &vec); |
| 501 | g3_ProjectPoint(&World_point_buffer[rp->wpb_index + i]); |
| 502 | } |
| 503 | } else { |
| 504 | for (i = 0; i < rp->num_verts; i++) { |
| 505 | g3_RotatePoint(&World_point_buffer[rp->wpb_index + i], &world_vecs[i]); |
| 506 | g3_ProjectPoint(&World_point_buffer[rp->wpb_index + i]); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Given a vector, reflects that vector off of a mirror vector |
| 512 | // Useful for specular and other reflective surfaces |
| 513 | void ReflectRay(vector *dest, vector *src, vector *mirror_norm) { |
| 514 | *dest = *src; |
| 515 | float d = *dest * *mirror_norm; |
| 516 | vector upvec = d * *mirror_norm; |
| 517 | *dest -= (2.0f * upvec); |
| 518 | } |
| 519 | |
| 520 | // This is needed for small view cameras |
| 521 | // It clears the facing array so that it is recomputed |
| 522 | void ResetFacings() { memset(Facing_visited, 0, sizeof(int) * (Highest_room_index + 1)); } |
| 523 | |
| 524 | // Marks all the faces facing us as drawable |
| 525 | void MarkFacingFaces(int roomnum, vector *world_verts) { |
| 526 | room *rp = &Rooms[roomnum]; |
| 527 | face *fp; |
| 528 | vector tvec; |
| 529 | if (Facing_visited[roomnum] == FrameCount) |
| 530 | return; |
| 531 | Facing_visited[roomnum] = FrameCount; |
| 532 | |
| 533 | fp = &rp->faces[0]; |
| 534 | // Go through and mark all non facing faces |
| 535 | if (Render_mirror_for_room) { |
| 536 | room *mirror_rp = &Rooms[Mirror_room]; |
| 537 | face *mirror_fp = &mirror_rp->faces[mirror_rp->mirror_face]; |
| 538 | for (int t = 0; t < rp->num_faces; t++, fp++) { |
| 539 | vector incident_norm; |
| 540 | ReflectRay(&incident_norm, &fp->normal, &mirror_fp->normal); |
| 541 | |
| 542 | tvec = Viewer_eye - world_verts[fp->face_verts[0]]; |
| 543 | if ((tvec * incident_norm) <= 0) |
| 544 | fp->flags |= FF_NOT_FACING; |
| 545 | } |
| 546 | } else { |
| 547 | for (int i = 0; i < rp->num_faces; i++, fp++) { |
| 548 | tvec = Viewer_eye - world_verts[fp->face_verts[0]]; |
| 549 | if ((tvec * fp->normal) <= 0) |
| 550 | fp->flags |= FF_NOT_FACING; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // Returns true if the external room is visible from the passed in portal |
| 556 | int ExternalRoomVisibleFromPortal(int index, clip_wnd *wnd) { |
| 557 | int i; |
| 558 | uint8_t code = 0xff; |
| 559 | g3Point pnt; |
| 560 | // This is a stupid hack to prevent really large buildings from popping in and out of view |
| 561 | if (External_room_project_net[index]) |
| 562 | return 1; |
| 563 | for (i = 0; i < 8; i++) { |
| 564 | pnt.p3_sx = External_room_corners[index][i].x; |
| 565 | pnt.p3_sy = External_room_corners[index][i].y; |
| 566 | pnt.p3_codes = 0; |
| 567 | code &= clip2d(&pnt, wnd); |
| 568 | } |
| 569 | if (code) |
| 570 | return 0; // building can't be seen from this portal |
| 571 | return 1; |
| 572 | } |
| 573 | |
| 574 | // Checks to see what faces intersect the passed in portal |
| 575 | void MarkFacesForRendering(int roomnum, clip_wnd *wnd) { |
| 576 | room *rp = &Rooms[roomnum]; |
| 577 | int i; |
| 578 | MarkFacingFaces(roomnum, rp->verts); |
| 579 | |
| 580 | // Rotate all the points in this room |
| 581 | if (rp->wpb_index == -1) { |
| 582 | rp->wpb_index = Global_buffer_index; |
| 583 | |
| 584 | // Katmai enhanced rotate only in a release build, because not |
| 585 | // everyone has the intel compiler! |
| 586 | #if (defined(RELEASE) && defined(KATMAI)) |
| 587 | if (Katmai) |
| 588 | RotateRoomPoints(rp, rp->verts4); |
| 589 | else |
| 590 | #endif |
| 591 | RotateRoomPoints(rp, rp->verts); |
| 592 | |
| 593 | Global_buffer_index += rp->num_verts; |
| 594 | } |
| 595 | if (rp->flags & RF_DOOR) { |
| 596 | for (i = 0; i < rp->num_faces; i++) |
| 597 | rp->faces[i].flags |= FF_VISIBLE; |
| 598 | } else { |
| 599 | // If this room contains a mirror, just mark all faces as visible |
| 600 | // Else go through and figure out which ones are visible from the current portal |
| 601 | if (rp->mirror_face == -1 || !Detail_settings.Mirrored_surfaces) { |
| 602 | // Do pointer dereferencing instead of array lookup for speed reasons |
| 603 | g3Point *pnt = &World_point_buffer[rp->wpb_index]; |
| 604 | for (i = 0; i < rp->num_verts; i++, pnt++) { |
| 605 | Room_clips[i] = clip2d(pnt, wnd); |
| 606 | } |
| 607 | face *fp = &rp->faces[0]; |
| 608 | for (i = 0; i < rp->num_faces; i++, fp++) { |
| 609 | if (fp->flags & (FF_NOT_FACING | FF_VISIBLE)) |
| 610 | continue; // this face is a backface |
| 611 | |
| 612 | if (FaceIntersectsPortal(rp, fp, wnd)) |
| 613 | fp->flags |= FF_VISIBLE; |
| 614 | } |
| 615 | } else { |
| 616 | if (rp->flags & RF_MIRROR_VISIBLE) // If this room is already mirror, just return |
| 617 | return; |
| 618 | g3Point *pnt = &World_point_buffer[rp->wpb_index]; |
| 619 | for (i = 0; i < rp->num_verts; i++, pnt++) { |
| 620 | Room_clips[i] = clip2d(pnt, wnd); |
| 621 | } |
| 622 | int done = 0; |
| 623 | face *fp; |
| 624 | for (i = 0; i < rp->num_mirror_faces && !done; i++) { |
| 625 | fp = &rp->faces[rp->mirror_faces_list[i]]; |
| 626 | if (FaceIntersectsPortal(rp, fp, wnd)) { |
| 627 | rp->flags |= RF_MIRROR_VISIBLE; |
| 628 | Mirror_rooms[Num_mirror_rooms++] = roomnum; |
| 629 | done = 1; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (rp->flags & RF_MIRROR_VISIBLE) // Mirror is visible, just mark all faces as visible |
| 634 | { |
| 635 | fp = &rp->faces[0]; |
| 636 | for (i = 0; i < rp->num_faces; i++, fp++) { |
| 637 | fp->flags |= FF_VISIBLE; |
| 638 | } |
| 639 | } else { |
| 640 | fp = &rp->faces[0]; |
| 641 | for (i = 0; i < rp->num_faces; i++, fp++) { |
| 642 | if (fp->flags & (FF_NOT_FACING | FF_VISIBLE)) |
| 643 | continue; // this face is a backface |
| 644 | |
| 645 | if (FaceIntersectsPortal(rp, fp, wnd)) |
| 646 | fp->flags |= FF_VISIBLE; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Mark objects for rendering |
| 653 | int objnum; |
| 654 | for (objnum = rp->objects; (objnum != -1); objnum = Objects[objnum].next) { |
| 655 | object *obj = &Objects[objnum]; |
| 656 | uint8_t anded = 0xff; |
| 657 | g3Point pnts[8]; |
| 658 | vector vecs[8]; |
| 659 | uint8_t code; |
| 660 | if (rp->flags & RF_DOOR) // Render all objects in a door room |
| 661 | { |
| 662 | obj->flags |= OF_SAFE_TO_RENDER; |
| 663 | continue; |
| 664 | } |
| 665 | if (rp->flags & RF_MIRROR_VISIBLE) // Render all objects if this mirror is visible |
| 666 | { |
| 667 | obj->flags |= OF_SAFE_TO_RENDER; |
| 668 | continue; |
| 669 | } |
| 670 | |
| 671 | MakePointsFromMinMax(vecs, &obj->min_xyz, &obj->max_xyz); |
| 672 | for (i = 0; i < 8; i++) { |
| 673 | g3_RotatePoint(&pnts[i], &vecs[i]); |
| 674 | g3_ProjectPoint(&pnts[i]); |
| 675 | code = clip2d(&pnts[i], wnd); |
| 676 | anded &= code; |
| 677 | if (pnts[i].p3_codes & CC_BEHIND) |
| 678 | anded = 0; |
| 679 | } |
| 680 | |
| 681 | if (!anded) { |
| 682 | // Object is visible |
| 683 | obj->flags |= OF_SAFE_TO_RENDER; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | // Prerotates all external room points and caches them |
| 688 | void RotateAllExternalRooms() { |
| 689 | // Build the external room list if needed |
| 690 | if (N_external_rooms == -1) { |
| 691 | // Set up our z wall |
| 692 | float zclip = (Detail_settings.Terrain_render_distance) * Matrix_scale.z; |
| 693 | g3_SetFarClipZ(zclip); |
| 694 | N_external_rooms = 0; |
| 695 | |
| 696 | int i; |
| 697 | for (i = 0; i <= Highest_room_index; i++) { |
| 698 | if ((Rooms[i].flags & RF_EXTERNAL) && Rooms[i].used) { |
| 699 | External_room_list[N_external_rooms++] = i; |
| 700 | } |
| 701 | } |
| 702 | ASSERT(N_external_rooms < MAX_EXTERNAL_ROOMS); // Get Jason if hit this |
| 703 | // Rotate all the points |
| 704 | vector corners[8]; |
| 705 | for (i = 0; i < N_external_rooms; i++) { |
| 706 | int roomnum = External_room_list[i]; |
| 707 | room *rp = &Rooms[roomnum]; |
| 708 | MakePointsFromMinMax(corners, &rp->min_xyz, &rp->max_xyz); |
| 709 | |
| 710 | uint8_t andbyte = 0xff; |
| 711 | g3Point pnt; |
| 712 | External_room_codes[i] = 0xff; |
| 713 | External_room_project_net[i] = 0; |
| 714 | bool behind = 0; |
| 715 | bool infront = 0; |
| 716 | for (int t = 0; t < 8; t++) { |
| 717 | g3_RotatePoint(&pnt, &corners[t]); |
| 718 | External_room_codes[i] &= pnt.p3_codes; |
| 719 | if (pnt.p3_codes & CC_BEHIND) |
| 720 | behind = true; |
| 721 | else |
| 722 | infront = true; |
| 723 | pnt.p3_codes &= ~CC_BEHIND; |
| 724 | g3_ProjectPoint(&pnt); |
| 725 | External_room_corners[i][t].x = pnt.p3_sx; |
| 726 | External_room_corners[i][t].y = pnt.p3_sy; |
| 727 | } |
| 728 | if (infront && behind) { |
| 729 | External_room_codes[i] = 0; |
| 730 | External_room_project_net[i] = 1; |
| 731 | } else { |
| 732 | if (behind) { |
| 733 | External_room_codes[i] = CC_BEHIND; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void CheckFogPortalExtents(int roomnum, int portalnum) { |
| 741 | room *rp = &Rooms[roomnum]; |
| 742 | ASSERT(rp->flags & RF_FOG); |
| 743 | |
| 744 | int i, found_room = -1; |
| 745 | for (i = 0; i < Num_fogged_rooms_this_frame; ++i) { |
| 746 | if (Fog_portal_data[i].roomnum != roomnum) |
| 747 | continue; |
| 748 | |
| 749 | found_room = i; |
| 750 | break; |
| 751 | } |
| 752 | |
| 753 | if (found_room == -1) { |
| 754 | // Couldn't find this room in our list, so make a new one |
| 755 | if (Num_fogged_rooms_this_frame >= MAX_FOGGED_ROOMS_PER_FRAME) { |
| 756 | mprintf(0, "Too many fogged rooms in view cone!\n"); |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | found_room = Num_fogged_rooms_this_frame++; |
| 761 | Fog_portal_data[found_room].close_face = NULL; |
| 762 | Fog_portal_data[found_room].close_dist = 10000000.0f; |
| 763 | Fog_portal_data[found_room].roomnum = roomnum; |
| 764 | } |
| 765 | |
| 766 | // get the portal face |
| 767 | int fn = rp->portals[portalnum].portal_face; |
| 768 | face *fp = &rp->faces[fn]; |
| 769 | |
| 770 | // calculate the plane equation for the portal |
| 771 | vector *vec = &rp->verts[fp->face_verts[0]]; |
| 772 | float distance = -vm_DotProduct(&fp->normal, vec); |
| 773 | |
| 774 | // calculate the distance from the camera to the portal |
| 775 | distance = vm_DotProduct(&fp->normal, &Viewer_eye) + distance; |
| 776 | if (distance < Fog_portal_data[found_room].close_dist) { |
| 777 | // this portal is closer to the camera than the previous |
| 778 | Fog_portal_data[found_room].close_dist = distance; |
| 779 | Fog_portal_data[found_room].close_face = fp; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | g3Point Combined_portal_points[MAX_VERTS_PER_FACE * 5]; |
| 784 | void BuildRoomListSub(int start_room_num, clip_wnd *wnd, int depth) { |
| 785 | room *rp = &Rooms[start_room_num]; |
| 786 | int i, t; |
| 787 | if (Render_portals) { |
| 788 | rend_SetTextureType(TT_FLAT); |
| 789 | rend_SetAlphaType(AT_CONSTANT); |
| 790 | rend_SetAlphaValue(255); |
| 791 | rend_SetFlatColor(GR_RGB(255, 255, 255)); |
| 792 | |
| 793 | rend_DrawLine(wnd->left, wnd->top, wnd->right, wnd->top); |
| 794 | rend_DrawLine(wnd->right, wnd->top, wnd->right, wnd->bot); |
| 795 | rend_DrawLine(wnd->right, wnd->bot, wnd->left, wnd->bot); |
| 796 | rend_DrawLine(wnd->left, wnd->bot, wnd->left, wnd->top); |
| 797 | } |
| 798 | if (!Rooms_visited[start_room_num]) { |
| 799 | Render_list[N_render_rooms++] = start_room_num; |
| 800 | } |
| 801 | |
| 802 | ASSERT(N_render_rooms < MAX_RENDER_ROOMS); |
| 803 | #ifndef RELEASE |
| 804 | Mine_depth++; |
| 805 | #endif |
| 806 | Rooms_visited[start_room_num] = 1; |
| 807 | Room_depth_list[start_room_num] = depth; |
| 808 | // If this room is a closed (non-seethrough) door, don't check any of its portals, |
| 809 | //...UNLESS this is the first room we're looking at (meaning the viewer is in this room) |
| 810 | if ((rp->flags & RF_DOOR) && (DoorwayGetPosition(rp) == 0.0) && |
| 811 | !(Doors[rp->doorway_data->doornum].flags & DF_SEETHROUGH)) |
| 812 | if (depth != 0) |
| 813 | return; |
| 814 | |
| 815 | // Check all the portals for this room |
| 816 | for (t = 0; t < rp->num_portals; t++) { |
| 817 | portal *pp = &rp->portals[t]; |
| 818 | int croom = pp->croom; |
| 819 | ASSERT(croom >= 0); |
| 820 | |
| 821 | // If we are an external room portalizing into another external room, then skip! |
| 822 | if ((rp->flags & RF_EXTERNAL) && (Rooms[croom].flags & RF_EXTERNAL)) |
| 823 | continue; |
| 824 | |
| 825 | // Check if we can see through this portal, and if not, skip it |
| 826 | if (!RenderPastPortal(rp, pp)) |
| 827 | continue; |
| 828 | |
| 829 | // If this portal has been visited, skip it |
| 830 | if (Room_depth_list[croom] < Room_depth_list[start_room_num]) |
| 831 | continue; |
| 832 | |
| 833 | // Deal with external portals differently |
| 834 | int external_door_hack = 0; |
| 835 | if (rp->flags & RF_EXTERNAL && Rooms[croom].flags & RF_DOOR) |
| 836 | external_door_hack = 1; |
| 837 | |
| 838 | // Get pointer to this portal's face |
| 839 | face *fp = &rp->faces[pp->portal_face]; |
| 840 | |
| 841 | // See if portal is facing toward us |
| 842 | if (!external_door_hack && !(pp->flags & PF_COMBINED)) { |
| 843 | vector check_v = Viewer_eye - rp->verts[fp->face_verts[0]]; |
| 844 | if (check_v * fp->normal <= 0) { |
| 845 | // not facing us |
| 846 | continue; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | g3Codes cc; |
| 851 | cc.cc_or = 0; |
| 852 | cc.cc_and = 0xff; |
| 853 | int nv = fp->num_verts; |
| 854 | |
| 855 | // Code the face points |
| 856 | // If this is a combined portal, then do that |
| 857 | if ((pp->flags & PF_COMBINED) && !(Rooms[croom].flags & RF_FOG)) { |
| 858 | // If this isn't the portal-combine master, then skip it |
| 859 | if (pp->combine_master != t) |
| 860 | continue; |
| 861 | int num_points = 0; |
| 862 | for (i = 0; i < rp->num_portals; i++) { |
| 863 | if (((rp->portals[i].flags & PF_COMBINED) == 0) || rp->portals[i].combine_master != t) |
| 864 | continue; |
| 865 | |
| 866 | int k; |
| 867 | |
| 868 | face *this_fp = &rp->faces[rp->portals[i].portal_face]; |
| 869 | vector check_v; |
| 870 | check_v = Viewer_eye - rp->verts[this_fp->face_verts[0]]; |
| 871 | if (check_v * this_fp->normal <= 0) // not facing us |
| 872 | continue; |
| 873 | |
| 874 | g3Codes combine_cc; |
| 875 | combine_cc.cc_or = 0; |
| 876 | combine_cc.cc_and = 0xff; |
| 877 | ASSERT((num_points + this_fp->num_verts) < (MAX_VERTS_PER_FACE * 5)); |
| 878 | |
| 879 | // First we must rotate and clip this polygon |
| 880 | for (k = 0; k < this_fp->num_verts; k++) { |
| 881 | uint8_t c = g3_RotatePoint(&Combined_portal_points[num_points + k], &rp->verts[this_fp->face_verts[k]]); |
| 882 | combine_cc.cc_or |= c; |
| 883 | combine_cc.cc_and &= c; |
| 884 | } |
| 885 | if (combine_cc.cc_and) { |
| 886 | continue; // clipped away! |
| 887 | } else if (combine_cc.cc_or) { |
| 888 | g3Point *pointlist[MAX_VERTS_PER_FACE]; |
| 889 | for (k = 0; k < this_fp->num_verts; k++) { |
| 890 | pointlist[k] = &Combined_portal_points[num_points + k]; |
| 891 | ASSERT(!(pointlist[k]->p3_flags & PF_TEMP_POINT)); |
| 892 | } |
| 893 | |
| 894 | // If portal not all on screen, must clip it |
| 895 | int combine_nv = this_fp->num_verts; |
| 896 | g3Point **pl = g3_ClipPolygon(pointlist, &combine_nv, &combine_cc); |
| 897 | if (combine_cc.cc_and) { |
| 898 | g3_FreeTempPoints(pl, combine_nv); |
| 899 | } else { |
| 900 | // save the clipped points |
| 901 | g3Point temp_points[MAX_VERTS_PER_FACE]; |
| 902 | for (k = 0; k < combine_nv; k++) { |
| 903 | temp_points[k] = *pl[k]; |
| 904 | temp_points[k].p3_flags &= ~PF_TEMP_POINT; |
| 905 | } |
| 906 | |
| 907 | // release the temp points |
| 908 | g3_FreeTempPoints(pl, combine_nv); |
| 909 | |
| 910 | // put the points back in the buffer |
| 911 | // NOTE: we have to do it like this because pl points to |
| 912 | // Combined_portal_points pointers |
| 913 | for (k = 0; k < combine_nv; k++) { |
| 914 | Combined_portal_points[k + num_points] = temp_points[k]; |
| 915 | } |
| 916 | |
| 917 | num_points += combine_nv; |
| 918 | } |
| 919 | } else { |
| 920 | // No clipping needed, face is fully on screen |
| 921 | num_points += this_fp->num_verts; |
| 922 | } |
| 923 | } |
| 924 | if (num_points == 0) |
| 925 | continue; |
| 926 | |
| 927 | // Now, figure out a min/max for these points |
| 928 | g3Point four_points[4]; |
| 929 | for (i = 0; i < num_points; i++) { |
| 930 | g3_ProjectPoint(&Combined_portal_points[i]); |
| 931 | } |
| 932 | int left = 0, top = 0, right = 0, bottom = 0; |
| 933 | clip_wnd combine_wnd; |
| 934 | combine_wnd.right = combine_wnd.bot = 0.0; |
| 935 | combine_wnd.left = Render_width; |
| 936 | combine_wnd.top = Render_height; |
| 937 | // make new clip window |
| 938 | for (i = 0; i < num_points; i++) { |
| 939 | float x = Combined_portal_points[i].p3_sx, y = Combined_portal_points[i].p3_sy; |
| 940 | if (x < combine_wnd.left) { |
| 941 | combine_wnd.left = x; |
| 942 | left = i; |
| 943 | } |
| 944 | if (x > combine_wnd.right) { |
| 945 | combine_wnd.right = x; |
| 946 | right = i; |
| 947 | } |
| 948 | if (y < combine_wnd.top) { |
| 949 | combine_wnd.top = y; |
| 950 | top = i; |
| 951 | } |
| 952 | if (y > combine_wnd.bot) { |
| 953 | combine_wnd.bot = y; |
| 954 | bottom = i; |
| 955 | } |
| 956 | } |
| 957 | // Now harvest these points |
| 958 | four_points[0] = Combined_portal_points[left]; |
| 959 | four_points[1] = Combined_portal_points[top]; |
| 960 | four_points[2] = Combined_portal_points[right]; |
| 961 | four_points[3] = Combined_portal_points[bottom]; |
| 962 | for (i = 0; i < 4; i++) { |
| 963 | Combined_portal_points[i] = four_points[i]; |
| 964 | Combined_portal_points[i].p3_flags &= ~(PF_PROJECTED | PF_TEMP_POINT); |
| 965 | uint8_t c = Combined_portal_points[i].p3_codes; |
| 966 | cc.cc_and &= c; |
| 967 | cc.cc_or |= c; |
| 968 | } |
| 969 | nv = 4; |
| 970 | } else { |
| 971 | for (i = 0; i < nv; i++) { |
| 972 | g3_RotatePoint(&Combined_portal_points[i], &rp->verts[fp->face_verts[i]]); |
| 973 | |
| 974 | uint8_t c = Combined_portal_points[i].p3_codes; |
| 975 | cc.cc_and &= c; |
| 976 | cc.cc_or |= c; |
| 977 | } |
| 978 | } |
| 979 | // If points are on screen, see if they're in the clip window |
| 980 | if (cc.cc_and == 0 || external_door_hack) { |
| 981 | bool clipped = 0; |
| 982 | g3Point *pointlist[MAX_VERTS_PER_FACE], **pl = pointlist; |
| 983 | for (i = 0; i < nv; i++) |
| 984 | pointlist[i] = &Combined_portal_points[i]; |
| 985 | // If portal not all on screen, must clip it |
| 986 | if (cc.cc_or) { |
| 987 | pl = g3_ClipPolygon(pl, &nv, &cc); |
| 988 | clipped = 1; |
| 989 | } |
| 990 | cc.cc_and = 0xff; |
| 991 | for (i = 0; i < nv; i++) { |
| 992 | g3_ProjectPoint(pl[i]); |
| 993 | cc.cc_and &= clip2d(pl[i], wnd); |
| 994 | } |
| 995 | // Make sure it didn't get clipped away |
| 996 | if (cc.cc_and == 0 || external_door_hack) { |
| 997 | clip_wnd new_wnd; |
| 998 | new_wnd.right = new_wnd.bot = 0.0; |
| 999 | new_wnd.left = Render_width; |
| 1000 | new_wnd.top = Render_height; |
| 1001 | // make new clip window |
| 1002 | for (i = 0; i < nv; i++) { |
| 1003 | float x = pl[i]->p3_sx, y = pl[i]->p3_sy; |
| 1004 | if (x < new_wnd.left) |
| 1005 | new_wnd.left = x; |
| 1006 | if (x > new_wnd.right) |
| 1007 | new_wnd.right = x; |
| 1008 | if (y < new_wnd.top) |
| 1009 | new_wnd.top = y; |
| 1010 | if (y > new_wnd.bot) |
| 1011 | new_wnd.bot = y; |
| 1012 | } |
| 1013 | // If this room is fogged, see if this portal is closest |
| 1014 | if (Rooms[croom].flags & RF_FOG) { |
| 1015 | CheckFogPortalExtents(croom, pp->cportal); |
| 1016 | } |
| 1017 | // Combine the two windows |
| 1018 | new_wnd.left = std::max(wnd->left, new_wnd.left); |
| 1019 | new_wnd.right = std::min(wnd->right, new_wnd.right); |
| 1020 | new_wnd.top = std::max(wnd->top, new_wnd.top); |
| 1021 | new_wnd.bot = std::min(wnd->bot, new_wnd.bot); |
| 1022 | if (clipped) { // Free up temp points |
| 1023 | g3_FreeTempPoints(pl, nv); |
| 1024 | clipped = 0; |
| 1025 | } |
| 1026 | if (Rooms[croom].flags & RF_EXTERNAL) { |
| 1027 | if (!Called_from_terrain) { |
| 1028 | Must_render_terrain = 1; |
| 1029 | RotateAllExternalRooms(); |
| 1030 | // For this external portal, we must check to see what external |
| 1031 | // rooms are visible from here |
| 1032 | for (i = 0; i < N_external_rooms; i++) { |
| 1033 | if (External_room_codes[i]) { |
| 1034 | continue; |
| 1035 | } |
| 1036 | if (External_room_list[i] != croom) { |
| 1037 | // If this portal has been visited, skip it |
| 1038 | if (Room_depth_list[External_room_list[i]] < Room_depth_list[start_room_num]) { |
| 1039 | continue; |
| 1040 | } |
| 1041 | if (!ExternalRoomVisibleFromPortal(i, &new_wnd)) { |
| 1042 | continue; |
| 1043 | } |
| 1044 | } |
| 1045 | MarkFacesForRendering(External_room_list[i], &new_wnd); |
| 1046 | BuildRoomListSub(External_room_list[i], &new_wnd, depth + 1); |
| 1047 | Room_depth_list[External_room_list[i]] = 255; |
| 1048 | } |
| 1049 | // Combine the two windows |
| 1050 | Terrain_portal_left = std::min<float>(new_wnd.left, Terrain_portal_left); |
| 1051 | Terrain_portal_right = std::max<float>(new_wnd.right, Terrain_portal_right); |
| 1052 | Terrain_portal_top = std::min<float>(new_wnd.top, Terrain_portal_top); |
| 1053 | Terrain_portal_bottom = std::max<float>(new_wnd.bot, Terrain_portal_bottom); |
| 1054 | } |
| 1055 | } else { |
| 1056 | MarkFacesForRendering(croom, &new_wnd); |
| 1057 | BuildRoomListSub(croom, &new_wnd, depth + 1); |
| 1058 | Room_depth_list[croom] = 255; |
| 1059 | } |
| 1060 | } |
| 1061 | if (clipped) // Free up temp points |
| 1062 | g3_FreeTempPoints(pl, nv); |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | // compare function for room z sort |
| 1067 | float Room_z_depth[MAX_ROOMS + MAX_PALETTE_ROOMS]; |
| 1068 | static int Room_z_sort_func(const int16_t *a, const int16_t *b) { |
| 1069 | float az, bz; |
| 1070 | az = Room_z_depth[*a]; |
| 1071 | bz = Room_z_depth[*b]; |
| 1072 | if (az < bz) |
| 1073 | return 1; |
| 1074 | else if (az > bz) |
| 1075 | return -1; |
| 1076 | else |
| 1077 | return 0; |
| 1078 | } |
| 1079 | // build a list of rooms to be rendered |
| 1080 | // fills in Render_list & N_render_rooms |
| 1081 | void BuildRoomList(int start_room_num) { |
| 1082 | clip_wnd wnd; |
| 1083 | room *rp = &Rooms[start_room_num]; |
| 1084 | int i; |
| 1085 | // For now, render all connected rooms |
| 1086 | for (i = 0; i <= Highest_room_index; i++) { |
| 1087 | Rooms_visited[i] = 0; |
| 1088 | Room_depth_list[i] = 255; |
| 1089 | Rooms[i].wpb_index = -1; |
| 1090 | } |
| 1091 | #ifdef EDITOR |
| 1092 | Rooms_visited[start_room_num] = 0; // take care of rooms in the room palette |
| 1093 | Room_depth_list[start_room_num] = 0; |
| 1094 | #endif |
| 1095 | N_external_rooms = -1; |
| 1096 | N_render_rooms = 0; |
| 1097 | Global_buffer_index = 0; |
| 1098 | // Mark all the faces in our start room as renderable |
| 1099 | for (i = 0; i < rp->num_faces; i++) |
| 1100 | rp->faces[i].flags |= FF_VISIBLE; |
| 1101 | |
| 1102 | MarkFacingFaces(start_room_num, rp->verts); |
| 1103 | // Enable mirror if there is one |
| 1104 | if (rp->mirror_face != -1 && Detail_settings.Mirrored_surfaces && |
| 1105 | !(rp->faces[rp->mirror_face].flags & FF_NOT_FACING)) { |
| 1106 | rp->flags |= RF_MIRROR_VISIBLE; |
| 1107 | Mirror_rooms[Num_mirror_rooms++] = start_room_num; |
| 1108 | } |
| 1109 | |
| 1110 | // Get our points rotated, and update the global point list |
| 1111 | rp->wpb_index = Global_buffer_index; |
| 1112 | |
| 1113 | // Katmai enhanced rotate only in a release build, because not |
| 1114 | // everyone has the intel compiler! |
| 1115 | #if (defined(RELEASE) && defined(KATMAI)) |
| 1116 | if (Katmai) |
| 1117 | |
| 1118 | RotateRoomPoints(rp, rp->verts4); |
| 1119 | else |
| 1120 | #endif |
| 1121 | RotateRoomPoints(rp, rp->verts); |
| 1122 | |
| 1123 | Global_buffer_index += rp->num_verts; |
| 1124 | |
| 1125 | // Mark all objects in this room as visible |
| 1126 | for (int objnum = rp->objects; (objnum != -1); objnum = Objects[objnum].next) |
| 1127 | Objects[objnum].flags |= OF_SAFE_TO_RENDER; |
| 1128 | |
| 1129 | // Initial clip window is whole screen |
| 1130 | wnd.left = wnd.top = 0.0; |
| 1131 | wnd.right = Render_width; |
| 1132 | wnd.bot = Render_height; |
| 1133 | BuildRoomListSub(start_room_num, &wnd, 0); |
| 1134 | // mprintf(0,"N_render_rooms = %d ",N_render_rooms); |
| 1135 | #ifdef EDITOR |
| 1136 | // Add all external rooms to render list if that flag set |
| 1137 | if (Editor_view_mode == VM_MINE && In_editor_mode) { |
| 1138 | if (Render_all_external_rooms) { |
| 1139 | int i; |
| 1140 | room *rp; |
| 1141 | for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) { |
| 1142 | if (rp->used && (rp->flags & RF_EXTERNAL)) { |
| 1143 | for (int t = 0; t < rp->num_faces; t++) |
| 1144 | rp->faces[t].flags |= FF_VISIBLE; |
| 1145 | MarkFacingFaces(i, rp->verts); |
| 1146 | |
| 1147 | if (!Rooms_visited[i]) |
| 1148 | Render_list[N_render_rooms++] = i; |
| 1149 | Rooms_visited[i] = 1; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | #endif |
| 1155 | } |
| 1156 | #ifdef EDITOR |
| 1157 | // Returns 1 if x,y is inside the given polygon, else 0 |
| 1158 | int point_in_poly(int nv, g3Point *p, float x, float y) { |
| 1159 | int i, j, c = 0; |
| 1160 | for (i = 0, j = nv - 1; i < nv; j = i++) { |
| 1161 | if ((((p[i].p3_sy <= y) && (y < p[j].p3_sy)) || ((p[j].p3_sy <= y) && (y < p[i].p3_sy))) && |
| 1162 | (x < (p[j].p3_sx - p[i].p3_sx) * (y - p[i].p3_sy) / (p[j].p3_sy - p[i].p3_sy) + p[i].p3_sx)) |
| 1163 | c = !c; |
| 1164 | } |
| 1165 | |
| 1166 | return c; |
| 1167 | } |
| 1168 | /* |
| 1169 | // Only called if editor active. |
| 1170 | // Used to determine which face was clicked on. |
| 1171 | void CheckFace(room *rp,int facenum, int nv, int bm,g3Point **pointlist) |
| 1172 | { |
| 1173 | ddgr_color oldcolor; |
| 1174 | //draw search pixel in bright green |
| 1175 | rend_SetPixel(GR_RGB(0,255,10),search_x,search_y); |
| 1176 | oldcolor = rend_GetPixel(search_x,search_y); //will be different in 15/16-bit color |
| 1177 | // Based on distance, draw in perspective or linear |
| 1178 | // This is gonna be ripped out |
| 1179 | rend_SetTextureType (TT_LINEAR_SPECIAL); |
| 1180 | for (int i=0;i<nv;i++) |
| 1181 | { |
| 1182 | g3Point *t; |
| 1183 | t=pointlist[i]; |
| 1184 | if (t[i].p3_vec.z<35) |
| 1185 | rend_SetTextureType (TT_PERSPECTIVE_SPECIAL); |
| 1186 | } |
| 1187 | g3_DrawPoly(nv,pointlist,bm); |
| 1188 | if (rend_GetPixel(search_x,search_y) != oldcolor) { |
| 1189 | found_room = ROOMNUM(rp); |
| 1190 | found_face = facenum; |
| 1191 | } |
| 1192 | } |
| 1193 | */ |
| 1194 | #endif |
| 1195 | #ifdef EDITOR |
| 1196 | #define STEPSIZE .01f |
| 1197 | #define STEPSIZE_MIN .1f |
| 1198 | void RenderFloatingTrig(room *rp, face *fp) { |
| 1199 | if (!Render_floating_triggers) |
| 1200 | return; |
| 1201 | vector leftvec, rightvec; |
| 1202 | vector left, right; |
| 1203 | vector left_step, right_step; |
| 1204 | int n_steps, i, j; |
| 1205 | g3Point p3; |
| 1206 | float stepsize; |
| 1207 | left = rp->verts[fp->face_verts[0]]; |
| 1208 | right = rp->verts[fp->face_verts[1]]; |
| 1209 | g3_RotatePoint(&p3, &left); |
| 1210 | stepsize = STEPSIZE * p3.p3_z; |
| 1211 | if (stepsize < STEPSIZE_MIN) |
| 1212 | stepsize = STEPSIZE_MIN; |
| 1213 | leftvec = rp->verts[fp->face_verts[3]] - rp->verts[fp->face_verts[0]]; |
| 1214 | rightvec = rp->verts[fp->face_verts[2]] - rp->verts[fp->face_verts[1]]; |
| 1215 | n_steps = (vm_GetMagnitude(&leftvec) / stepsize + 0.5) + 1; |
| 1216 | left_step = leftvec / n_steps; |
| 1217 | right_step = rightvec / n_steps; |
| 1218 | for (i = 0; i < n_steps; i++) { |
| 1219 | vector p; |
| 1220 | g3Point p3; |
| 1221 | vector crossvec, cross_step; |
| 1222 | int n_crosssteps; |
| 1223 | |
| 1224 | crossvec = right - left; |
| 1225 | n_crosssteps = (vm_GetMagnitude(&crossvec) / stepsize + 0.5) + 1; |
| 1226 | cross_step = crossvec / n_steps; |
| 1227 | p = left; |
| 1228 | for (j = 0; j < n_crosssteps; j++) { |
| 1229 | if (g3_RotatePoint(&p3, &p) == 0) { // on screen |
| 1230 | g3_ProjectPoint(&p3); |
| 1231 | rend_SetPixel(GR_RGB(255, 100, 100), p3.p3_sx, p3.p3_sy); |
| 1232 | } |
| 1233 | p += cross_step; |
| 1234 | } |
| 1235 | left += left_step; |
| 1236 | right += right_step; |
| 1237 | } |
| 1238 | } |
| 1239 | #endif // ifdef EDITOR |
| 1240 | void RenderSpecularFaces(room *rp) { |
| 1241 | ASSERT(Num_specular_faces_to_render > 0); |
| 1242 | int i, vn; |
| 1243 | g3Point *pointlist[MAX_VERTS_PER_FACE]; |
| 1244 | g3Point pointbuffer[MAX_VERTS_PER_FACE]; |
| 1245 | rend_SetWrapType(WT_CLAMP); |
| 1246 | rend_SetOverlayType(OT_NONE); |
| 1247 | rend_SetTextureType(TT_PERSPECTIVE); |
| 1248 | rend_SetLighting(LS_NONE); |
| 1249 | rend_SetColorModel(CM_MONO); |
| 1250 | rend_SetAlphaType(AT_SATURATE_TEXTURE); |
| 1251 | rend_SetAlphaValue(255); |
| 1252 | |
| 1253 | for (i = 0; i < Num_specular_faces_to_render; i++) { |
| 1254 | face *fp = &rp->faces[Specular_faces[i]]; |
| 1255 | int lm_handle = LightmapInfo[fp->lmi_handle].spec_map; |
| 1256 | ASSERT(lm_handle != -1); |
| 1257 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1258 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1259 | g3Point *p = &pointbuffer[vn]; |
| 1260 | pointlist[vn] = p; |
| 1261 | p->p3_uvl.u = fp->face_uvls[vn].u2; |
| 1262 | p->p3_uvl.v = fp->face_uvls[vn].v2; |
| 1263 | |
| 1264 | p->p3_flags |= PF_UV; |
| 1265 | } |
| 1266 | |
| 1267 | int save_w = lm_w(lm_handle); |
| 1268 | int save_h = lm_h(lm_handle); |
| 1269 | GameLightmaps[lm_handle].width = lm_w(LightmapInfo[fp->lmi_handle].lm_handle); |
| 1270 | GameLightmaps[lm_handle].height = lm_h(LightmapInfo[fp->lmi_handle].lm_handle); |
| 1271 | g3_DrawPoly(fp->num_verts, pointlist, lm_handle, MAP_TYPE_LIGHTMAP); |
| 1272 | GameLightmaps[lm_handle].width = save_w; |
| 1273 | GameLightmaps[lm_handle].height = save_h; |
| 1274 | } |
| 1275 | rend_SetWrapType(WT_WRAP); |
| 1276 | } |
| 1277 | |
| 1278 | float Specular_scalars[4][4] = {{1.0f}, {1.0f, .66f}, {1.0f, .66f, .33f}, {1.0f, .66f, .33f, .25f}}; |
| 1279 | void RenderSpecularFacesFlat(room *rp) { |
| 1280 | static int first = 1; |
| 1281 | static float lm_red[32], lm_green[32], lm_blue[32]; |
| 1282 | int num_smooth_faces = 0; |
| 1283 | int num_smooth_used = 0; |
| 1284 | uint16_t smooth_faces[MAX_FACES_PER_ROOM]; |
| 1285 | uint16_t smooth_used[MAX_VERTS_PER_ROOM]; |
| 1286 | int i; |
| 1287 | ASSERT(Num_specular_faces_to_render > 0); |
| 1288 | if (!UseHardware) |
| 1289 | return; |
| 1290 | if (Num_real_specular_faces_to_render == 0) { |
| 1291 | for (i = 0; i < Num_specular_faces_to_render; i++) { |
| 1292 | face *fp = &rp->faces[Specular_faces[i]]; |
| 1293 | fp->flags &= ~FF_SPEC_INVISIBLE; |
| 1294 | } |
| 1295 | return; |
| 1296 | } |
| 1297 | if (first) { |
| 1298 | first = 0; |
| 1299 | int i; |
| 1300 | for (i = 0; i < 32; i++) { |
| 1301 | lm_red[i] = (float)i / 31.0; |
| 1302 | lm_green[i] = (float)i / 31.0; |
| 1303 | lm_blue[i] = (float)i / 31.0; |
| 1304 | } |
| 1305 | for (i = 0; i < MAX_VERTS_PER_ROOM; i++) { |
| 1306 | Smooth_verts[i].used = 0; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | int t, vn; |
| 1311 | g3Point *pointlist[MAX_VERTS_PER_FACE]; |
| 1312 | g3Point pointbuffer[MAX_VERTS_PER_FACE]; |
| 1313 | rend_SetOverlayType(OT_NONE); |
| 1314 | rend_SetTextureType(TT_FLAT); |
| 1315 | rend_SetLighting(LS_GOURAUD); |
| 1316 | rend_SetColorModel(CM_RGB); |
| 1317 | rend_SetAlphaType(AT_SPECULAR); |
| 1318 | rend_SetZBufferWriteMask(0); |
| 1319 | |
| 1320 | for (i = 0; i < Num_specular_faces_to_render; i++) { |
| 1321 | face *fp = &rp->faces[Specular_faces[i]]; |
| 1322 | |
| 1323 | int material_type = 0; |
| 1324 | if (GameTextures[fp->tmap].flags & TF_PLASTIC) |
| 1325 | material_type = 1; |
| 1326 | else if (GameTextures[fp->tmap].flags & TF_MARBLE) |
| 1327 | material_type = 2; |
| 1328 | int bm_handle = GetTextureBitmap(fp->tmap, 0); |
| 1329 | if ((fp->flags & FF_DESTROYED) && (GameTextures[fp->tmap].flags & TF_DESTROYABLE)) |
| 1330 | bm_handle = GetTextureBitmap(GameTextures[fp->tmap].destroy_handle, 0); |
| 1331 | if (bm_format(bm_handle) != BITMAP_FORMAT_4444) |
| 1332 | continue; |
| 1333 | |
| 1334 | int lm_handle; |
| 1335 | uint16_t *data; |
| 1336 | int w, h; |
| 1337 | |
| 1338 | if (fp->lmi_handle == 65535) { |
| 1339 | // this face shouldn't be rendered during the specular pass...skip it |
| 1340 | continue; |
| 1341 | } |
| 1342 | |
| 1343 | lm_handle = LightmapInfo[fp->lmi_handle].lm_handle; |
| 1344 | data = (uint16_t *)lm_data(lm_handle); |
| 1345 | w = lm_w(lm_handle); |
| 1346 | h = lm_h(lm_handle); |
| 1347 | |
| 1348 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1349 | |
| 1350 | float rv = 0, gv = 0, bv = 0; |
| 1351 | float scalar = 0; |
| 1352 | float u, v; |
| 1353 | int int_u, int_v; |
| 1354 | uint16_t texel; |
| 1355 | int r, g, b; |
| 1356 | float vr, vg, vb; |
| 1357 | |
| 1358 | u = fp->face_uvls[vn].u2 * w; |
| 1359 | v = fp->face_uvls[vn].v2 * h; |
| 1360 | int_u = u; |
| 1361 | int_v = v; |
| 1362 | texel = data[int_v * w + int_u]; |
| 1363 | r = (texel >> 10) & 0x1f; |
| 1364 | g = (texel >> 5) & 0x1f; |
| 1365 | b = (texel) & 0x1f; |
| 1366 | vr = lm_red[r]; |
| 1367 | vg = lm_green[g]; |
| 1368 | vb = lm_blue[b]; |
| 1369 | |
| 1370 | vector subvec = Viewer_eye - rp->verts[fp->face_verts[vn]]; |
| 1371 | vm_NormalizeVectorFast(&subvec); |
| 1372 | if (!(rp->flags & RF_EXTERNAL)) { |
| 1373 | int limit = SpecialFaces[fp->special_handle].num; |
| 1374 | int spec_index = limit - 1; |
| 1375 | /*if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) |
| 1376 | { |
| 1377 | limit/=2; |
| 1378 | }*/ |
| 1379 | |
| 1380 | for (t = 0; t < limit; t++) { |
| 1381 | uint16_t color; |
| 1382 | vector incident_norm; |
| 1383 | float spec_scalar; |
| 1384 | |
| 1385 | // Use regular static lighting |
| 1386 | color = SpecialFaces[fp->special_handle].spec_instance[t].bright_color; |
| 1387 | if (color == 0) |
| 1388 | continue; |
| 1389 | incident_norm = |
| 1390 | rp->verts[fp->face_verts[vn]] - SpecialFaces[fp->special_handle].spec_instance[t].bright_center; |
| 1391 | spec_scalar = Specular_scalars[spec_index][t]; |
| 1392 | |
| 1393 | vm_NormalizeVectorFast(&incident_norm); |
| 1394 | float d; |
| 1395 | vector upvec; |
| 1396 | if ((GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) && |
| 1397 | (SpecialFaces[fp->special_handle].flags & SFF_SPEC_SMOOTH)) { |
| 1398 | d = incident_norm * SpecialFaces[fp->special_handle].vertnorms[vn]; |
| 1399 | upvec = d * SpecialFaces[fp->special_handle].vertnorms[vn]; |
| 1400 | } else { |
| 1401 | d = incident_norm * fp->normal; |
| 1402 | upvec = d * fp->normal; |
| 1403 | } |
| 1404 | incident_norm -= (2 * upvec); |
| 1405 | float dotp = subvec * incident_norm; |
| 1406 | if (dotp > 1) |
| 1407 | dotp = 1; |
| 1408 | |
| 1409 | if (dotp > 0) { |
| 1410 | int index = ((float)(MAX_SPECULAR_INCREMENTS - 1) * dotp); |
| 1411 | scalar = Specular_tables[material_type][index] * spec_scalar; |
| 1412 | |
| 1413 | float cr = (float)((color >> 10) & 0x1f) / 31.0; |
| 1414 | float cg = (float)((color >> 5) & 0x1f) / 31.0; |
| 1415 | float cb = (float)(color & 0x1f) / 31.0; |
| 1416 | rv = std::min<float>(1.0, (rv + (scalar * cr))); |
| 1417 | gv = std::min<float>(1.0, (gv + (scalar * cg))); |
| 1418 | bv = std::min<float>(1.0, (bv + (scalar * cb))); |
| 1419 | } |
| 1420 | } |
| 1421 | } else { |
| 1422 | vector incident_norm = rp->verts[fp->face_verts[vn]] - Terrain_sky.satellite_vectors[0]; |
| 1423 | vm_NormalizeVectorFast(&incident_norm); |
| 1424 | |
| 1425 | float d = incident_norm * fp->normal; |
| 1426 | vector upvec = d * fp->normal; |
| 1427 | incident_norm -= (2 * upvec); |
| 1428 | float dotp = subvec * incident_norm; |
| 1429 | if (dotp > 1) |
| 1430 | dotp = 1; |
| 1431 | |
| 1432 | if (dotp > 0) { |
| 1433 | int index = ((float)(MAX_SPECULAR_INCREMENTS - 1) * dotp); |
| 1434 | scalar = Specular_tables[material_type][index]; |
| 1435 | rv = scalar; |
| 1436 | gv = scalar; |
| 1437 | bv = scalar; |
| 1438 | } |
| 1439 | } |
| 1440 | // Finally, brighten these value up a bit |
| 1441 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) { |
| 1442 | rv = rv * vr; |
| 1443 | gv = gv * vg; |
| 1444 | bv = bv * vb; |
| 1445 | if (Smooth_verts[fp->face_verts[vn]].used == 0) { |
| 1446 | Smooth_verts[fp->face_verts[vn]].used = 1; |
| 1447 | Smooth_verts[fp->face_verts[vn]].r = rv; |
| 1448 | Smooth_verts[fp->face_verts[vn]].g = gv; |
| 1449 | Smooth_verts[fp->face_verts[vn]].b = bv; |
| 1450 | smooth_used[num_smooth_used++] = fp->face_verts[vn]; |
| 1451 | } else { |
| 1452 | Smooth_verts[fp->face_verts[vn]].r += rv; |
| 1453 | Smooth_verts[fp->face_verts[vn]].g += gv; |
| 1454 | Smooth_verts[fp->face_verts[vn]].b += bv; |
| 1455 | } |
| 1456 | } else { |
| 1457 | rv = std::min(1.0, rv * vr * 4.0); |
| 1458 | gv = std::min(1.0, gv * vg * 4.0); |
| 1459 | bv = std::min(1.0, bv * vb * 4.0); |
| 1460 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1461 | g3Point *p = &pointbuffer[vn]; |
| 1462 | pointlist[vn] = p; |
| 1463 | p->p3_uvl.u = fp->face_uvls[vn].u; |
| 1464 | p->p3_uvl.v = fp->face_uvls[vn].v; |
| 1465 | p->p3_a = 1.0; |
| 1466 | p->p3_r = rv; |
| 1467 | p->p3_g = gv; |
| 1468 | p->p3_b = bv; |
| 1469 | p->p3_flags |= PF_RGBA | PF_UV; |
| 1470 | } |
| 1471 | } |
| 1472 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) { |
| 1473 | smooth_faces[num_smooth_faces] = fp - rp->faces; |
| 1474 | num_smooth_faces++; |
| 1475 | } else { |
| 1476 | if (fp->flags & FF_TRIANGULATED) |
| 1477 | g3_SetTriangulationTest(1); |
| 1478 | |
| 1479 | g3_DrawPoly(fp->num_verts, pointlist, bm_handle); |
| 1480 | |
| 1481 | if (fp->flags & FF_TRIANGULATED) |
| 1482 | g3_SetTriangulationTest(0); |
| 1483 | } |
| 1484 | } |
| 1485 | // Now draw smooth specular faces |
| 1486 | for (i = 0; i < num_smooth_faces; i++) { |
| 1487 | face *fp = &rp->faces[smooth_faces[i]]; |
| 1488 | if (fp->flags & FF_SPEC_INVISIBLE) { |
| 1489 | fp->flags &= ~(FF_SPEC_INVISIBLE); |
| 1490 | continue; |
| 1491 | } |
| 1492 | int bm_handle = GetTextureBitmap(fp->tmap, 0); |
| 1493 | if ((fp->flags & FF_DESTROYED) && (GameTextures[fp->tmap].flags & TF_DESTROYABLE)) |
| 1494 | bm_handle = GetTextureBitmap(GameTextures[fp->tmap].destroy_handle, 0); |
| 1495 | if (bm_format(bm_handle) != BITMAP_FORMAT_4444) |
| 1496 | continue; |
| 1497 | float reflect = GameTextures[fp->tmap].reflectivity * 1.5; |
| 1498 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1499 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1500 | g3Point *p = &pointbuffer[vn]; |
| 1501 | pointlist[vn] = p; |
| 1502 | p->p3_uvl.u = fp->face_uvls[vn].u; |
| 1503 | p->p3_uvl.v = fp->face_uvls[vn].v; |
| 1504 | p->p3_a = 1.0; |
| 1505 | p->p3_r = std::min<float>(1.0, Smooth_verts[fp->face_verts[vn]].r * reflect); |
| 1506 | p->p3_g = std::min<float>(1.0, Smooth_verts[fp->face_verts[vn]].g * reflect); |
| 1507 | p->p3_b = std::min<float>(1.0, Smooth_verts[fp->face_verts[vn]].b * reflect); |
| 1508 | p->p3_flags |= PF_RGBA | PF_UV; |
| 1509 | } |
| 1510 | |
| 1511 | if (fp->flags & FF_TRIANGULATED) |
| 1512 | g3_SetTriangulationTest(1); |
| 1513 | |
| 1514 | g3_DrawPoly(fp->num_verts, pointlist, bm_handle); |
| 1515 | |
| 1516 | if (fp->flags & FF_TRIANGULATED) |
| 1517 | g3_SetTriangulationTest(0); |
| 1518 | } |
| 1519 | for (i = 0; i < num_smooth_used; i++) { |
| 1520 | Smooth_verts[smooth_used[i]].used = 0; |
| 1521 | } |
| 1522 | rend_SetZBufferWriteMask(1); |
| 1523 | } |
| 1524 | |
| 1525 | // Adds a specular face to draw after the mine has been drawn |
| 1526 | void UpdateSpecularFace(room *rp, face *fp) { |
| 1527 | /*if (!(rp->flags & RF_EXTERNAL) |
| 1528 | { |
| 1529 | int handle=GetSpecularLightmapForFace (&Viewer_eye,rp,fp); |
| 1530 | if (handle<0) |
| 1531 | return; |
| 1532 | }*/ |
| 1533 | if (Renderer_type == RENDERER_OPENGL) |
| 1534 | return; |
| 1535 | int n = Num_specular_faces_to_render; |
| 1536 | if (n >= MAX_SPECULAR_FACES) |
| 1537 | return; |
| 1538 | Specular_faces[n] = fp - rp->faces; |
| 1539 | Num_specular_faces_to_render++; |
| 1540 | if (!(fp->flags & FF_SPEC_INVISIBLE)) |
| 1541 | Num_real_specular_faces_to_render++; |
| 1542 | } |
| 1543 | |
| 1544 | #if _DEBUG |
| 1545 | bool Fog_disabled = 0; |
| 1546 | #else |
| 1547 | #define Fog_disabled 0 |
| 1548 | #endif |
| 1549 | |
| 1550 | // Adds a specular face to draw after the mine has been drawn |
| 1551 | void UpdateFogFace(room *rp, face *fp) { |
| 1552 | if (Fog_disabled || !Detail_settings.Fog_enabled) |
| 1553 | return; |
| 1554 | Fog_faces[Num_fog_faces_to_render++] = fp - rp->faces; |
| 1555 | } |
| 1556 | |
| 1557 | // Render a fog layer on top of a face |
| 1558 | void RenderFogFaces(room *rp) { |
| 1559 | int vn; |
| 1560 | float eye_distance; |
| 1561 | g3Point *pointlist[MAX_VERTS_PER_FACE]; |
| 1562 | g3Point pointbuffer[MAX_VERTS_PER_FACE]; |
| 1563 | rend_SetOverlayType(OT_NONE); |
| 1564 | rend_SetTextureType(TT_FLAT); |
| 1565 | rend_SetLighting(LS_NONE); |
| 1566 | rend_SetColorModel(CM_MONO); |
| 1567 | rend_SetAlphaType(AT_VERTEX); |
| 1568 | rend_SetAlphaValue(255); |
| 1569 | rend_SetZBufferWriteMask(0); |
| 1570 | rend_SetCoplanarPolygonOffset(1); |
| 1571 | |
| 1572 | rend_SetFlatColor(GR_RGB((int)(rp->fog_r * 255.0), (int)(rp->fog_g * 255.0), (int)(rp->fog_b * 255.0))); |
| 1573 | for (int i = 0; i < Num_fog_faces_to_render; i++) { |
| 1574 | face *fp = &rp->faces[Fog_faces[i]]; |
| 1575 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1576 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1577 | g3Point *p = &pointbuffer[vn]; |
| 1578 | pointlist[vn] = p; |
| 1579 | |
| 1580 | float mag = 0; |
| 1581 | |
| 1582 | if (Room_fog_plane_check == 0) { |
| 1583 | // Outside of the room |
| 1584 | vector *vec = &rp->verts[fp->face_verts[vn]]; |
| 1585 | // Now we must generate the split point. This is simply |
| 1586 | // an equation in the form Origin + t*Direction |
| 1587 | |
| 1588 | float dist = (*vec * Room_fog_plane) + Room_fog_distance; |
| 1589 | |
| 1590 | vector subvec = *vec - Viewer_eye; |
| 1591 | float t = Room_fog_eye_distance / (Room_fog_eye_distance - dist); |
| 1592 | vector portal_point = Viewer_eye + (t * subvec); |
| 1593 | |
| 1594 | eye_distance = -(vm_DotProduct(&Viewer_orient.fvec, &portal_point)); |
| 1595 | mag = vm_DotProduct(&Viewer_orient.fvec, vec) + eye_distance; |
| 1596 | } else if (Room_fog_plane_check == 1) { |
| 1597 | // In the room, distance from |
| 1598 | vector *vec = &rp->verts[fp->face_verts[vn]]; |
| 1599 | mag = vm_DotProduct(&Room_fog_plane, vec) + Room_fog_distance; |
| 1600 | } |
| 1601 | float scalar = mag / rp->fog_depth; |
| 1602 | if (scalar > 1) |
| 1603 | scalar = 1; |
| 1604 | if (scalar < 0) |
| 1605 | scalar = 0; |
| 1606 | p->p3_a = scalar * Room_light_val; |
| 1607 | |
| 1608 | p->p3_flags |= PF_RGBA; |
| 1609 | } |
| 1610 | if (fp->flags & FF_TRIANGULATED) |
| 1611 | g3_SetTriangulationTest(1); |
| 1612 | g3_DrawPoly(fp->num_verts, pointlist, 0); |
| 1613 | if (fp->flags & FF_TRIANGULATED) |
| 1614 | g3_SetTriangulationTest(0); |
| 1615 | } |
| 1616 | |
| 1617 | rend_SetCoplanarPolygonOffset(0); |
| 1618 | rend_SetZBufferWriteMask(1); |
| 1619 | } |
| 1620 | // MATT! Change this function to sort by state once you change the scorch system! |
| 1621 | void RenderScorchesForRoom(room *rp) { |
| 1622 | int i; |
| 1623 | if (!Detail_settings.Scorches_enabled) |
| 1624 | return; |
| 1625 | // Set alpha, transparency, & lighting for this face |
| 1626 | rend_SetAlphaType(AT_LIGHTMAP_BLEND); |
| 1627 | rend_SetAlphaValue(255); |
| 1628 | rend_SetLighting(LS_NONE); |
| 1629 | rend_SetColorModel(CM_MONO); |
| 1630 | rend_SetOverlayType(OT_NONE); |
| 1631 | rend_SetZBias(-.5); |
| 1632 | rend_SetZBufferWriteMask(0); |
| 1633 | |
| 1634 | // Select texture type |
| 1635 | rend_SetTextureType(TT_LINEAR); |
| 1636 | |
| 1637 | for (i = 0; i < Num_scorches_to_render; i++) { |
| 1638 | DrawScorches(ROOMNUM(rp), Scorches_to_render[i]); |
| 1639 | } |
| 1640 | // Reset rendering states |
| 1641 | rend_SetZBias(0); |
| 1642 | rend_SetZBufferWriteMask(1); |
| 1643 | } |
| 1644 | // Draw the specified face |
| 1645 | // Parameters: rp - pointer to the room the face is un |
| 1646 | // facenum - which face in the specified room |
| 1647 | void RenderLightmapFace(room *rp, int facenum) { |
| 1648 | int vn, drawn = 0; |
| 1649 | face *fp = &rp->faces[facenum]; |
| 1650 | g3Point *pointlist[MAX_VERTS_PER_FACE]; |
| 1651 | g3Point pointbuffer[MAX_VERTS_PER_FACE]; |
| 1652 | uint8_t face_code = 255; |
| 1653 | if (NoLightmaps) |
| 1654 | return; |
| 1655 | if (fp->lmi_handle == BAD_LMI_INDEX) |
| 1656 | return; |
| 1657 | if (!(fp->flags & FF_LIGHTMAP)) |
| 1658 | return; |
| 1659 | // check for render windows hack |
| 1660 | if (No_render_windows_hack == 1) { |
| 1661 | if (fp->portal_num != -1) |
| 1662 | return; |
| 1663 | } |
| 1664 | if (GameTextures[fp->tmap].flags & TF_SATURATE) |
| 1665 | return; |
| 1666 | #ifdef EDITOR |
| 1667 | if (In_editor_mode && !Lighting_on) |
| 1668 | return; |
| 1669 | #endif |
| 1670 | int lm_handle = LightmapInfo[fp->lmi_handle].lm_handle; |
| 1671 | // Setup saturation if needed |
| 1672 | if (GameTextures[fp->tmap].flags & TF_SATURATE_LIGHTMAP) |
| 1673 | rend_SetAlphaType(AT_LIGHTMAP_BLEND_SATURATE); |
| 1674 | // Our lightmaps aren't square, but our destination texture surfaces are |
| 1675 | // Use these scalars to get them into the correct coordinates |
| 1676 | float xscalar = (float)GameLightmaps[lm_handle].width / (float)GameLightmaps[lm_handle].square_res; |
| 1677 | float yscalar = (float)GameLightmaps[lm_handle].height / (float)GameLightmaps[lm_handle].square_res; |
| 1678 | if (!Render_mirror_for_room) { |
| 1679 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1680 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1681 | g3Point *p = &pointbuffer[vn]; |
| 1682 | pointlist[vn] = p; |
| 1683 | p->p3_uvl.u = fp->face_uvls[vn].u2 * xscalar; |
| 1684 | p->p3_uvl.v = fp->face_uvls[vn].v2 * yscalar; |
| 1685 | |
| 1686 | p->p3_flags |= PF_UV + PF_L + PF_RGBA; // has uv and l set |
| 1687 | p->p3_uvl.l = 1.0; |
| 1688 | |
| 1689 | face_code &= p->p3_codes; |
| 1690 | } |
| 1691 | } else { |
| 1692 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1693 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1694 | g3Point *p = &pointbuffer[vn]; |
| 1695 | pointlist[(fp->num_verts - 1) - vn] = p; |
| 1696 | p->p3_uvl.u = fp->face_uvls[vn].u2 * xscalar; |
| 1697 | p->p3_uvl.v = fp->face_uvls[vn].v2 * yscalar; |
| 1698 | |
| 1699 | p->p3_flags |= PF_UV + PF_L + PF_RGBA; // has uv and l set |
| 1700 | p->p3_uvl.l = 1.0; |
| 1701 | |
| 1702 | face_code &= p->p3_codes; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | if (face_code) // This entire face is off the scren |
| 1707 | return; |
| 1708 | rend_SetAlphaValue(GameTextures[fp->tmap].alpha * 255); |
| 1709 | if (fp->flags & FF_TRIANGULATED) |
| 1710 | g3_SetTriangulationTest(1); |
| 1711 | // Draw the damn thing |
| 1712 | drawn = g3_DrawPoly(fp->num_verts, pointlist, lm_handle, MAP_TYPE_LIGHTMAP); |
| 1713 | if (fp->flags & FF_TRIANGULATED) |
| 1714 | g3_SetTriangulationTest(0); |
| 1715 | // Restore if using saturated blending |
| 1716 | if (GameTextures[fp->tmap].flags & TF_SATURATE_LIGHTMAP) |
| 1717 | rend_SetAlphaType(AT_LIGHTMAP_BLEND); |
| 1718 | } |
| 1719 | // Draw the specified face |
| 1720 | // Parameters: rp - pointer to the room the face is un |
| 1721 | // facenum - which face in the specified room |
| 1722 | void RenderFace(room *rp, int facenum) { |
| 1723 | int vn, drawn = 0; |
| 1724 | face *fp = &rp->faces[facenum]; |
| 1725 | g3Point *pointlist[MAX_VERTS_PER_FACE]; |
| 1726 | g3Point pointbuffer[MAX_VERTS_PER_FACE]; |
| 1727 | int bm_handle; |
| 1728 | float uchange = 0, vchange = 0; |
| 1729 | texture_type tt; |
| 1730 | uint8_t do_triangle_test = 0; |
| 1731 | g3Codes face_cc; |
| 1732 | static int first = 1; |
| 1733 | static float lm_red[32], lm_green[32], lm_blue[32]; |
| 1734 | bool spec_face = 0; |
| 1735 | face_cc.cc_and = 0xff; |
| 1736 | face_cc.cc_or = 0; |
| 1737 | #ifdef EDITOR |
| 1738 | if (fp->flags & FF_FLOATING_TRIG) { |
| 1739 | RenderFloatingTrig(rp, fp); |
| 1740 | return; |
| 1741 | } |
| 1742 | #endif |
| 1743 | // Clear triangulation flag |
| 1744 | fp->flags &= ~FF_TRIANGULATED; |
| 1745 | // check for render windows hack |
| 1746 | if (No_render_windows_hack == 1) { |
| 1747 | if (fp->portal_num != -1) |
| 1748 | return; |
| 1749 | } |
| 1750 | if (rp->flags & RF_TRIANGULATE) |
| 1751 | do_triangle_test = 1; |
| 1752 | if (!Render_mirror_for_room && Detail_settings.Specular_lighting && (GameTextures[fp->tmap].flags & TF_SPECULAR) && |
| 1753 | ((fp->special_handle != BAD_SPECIAL_FACE_INDEX) || (rp->flags & RF_EXTERNAL))) |
| 1754 | spec_face = 1; |
| 1755 | // Figure out if there is any texture sliding |
| 1756 | if (GameTextures[fp->tmap].slide_u != 0) { |
| 1757 | int int_time = Gametime / GameTextures[fp->tmap].slide_u; |
| 1758 | float norm_time = Gametime - (int_time * GameTextures[fp->tmap].slide_u); |
| 1759 | norm_time /= GameTextures[fp->tmap].slide_u; |
| 1760 | uchange = norm_time; |
| 1761 | } |
| 1762 | if (GameTextures[fp->tmap].slide_v != 0) { |
| 1763 | int int_time = Gametime / GameTextures[fp->tmap].slide_v; |
| 1764 | float norm_time = Gametime - (int_time * GameTextures[fp->tmap].slide_v); |
| 1765 | norm_time /= GameTextures[fp->tmap].slide_v; |
| 1766 | vchange = norm_time; |
| 1767 | } |
| 1768 | // Build list of points and UVLs for this face |
| 1769 | if (Render_mirror_for_room) // If mirror room, order the vertices counter clockwise |
| 1770 | { |
| 1771 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1772 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1773 | g3Point *p = &pointbuffer[vn]; |
| 1774 | pointlist[(fp->num_verts - 1) - vn] = p; |
| 1775 | p->p3_uvl.u = fp->face_uvls[vn].u; |
| 1776 | p->p3_uvl.v = fp->face_uvls[vn].v; |
| 1777 | p->p3_uvl.u2 = fp->face_uvls[vn].u2; |
| 1778 | p->p3_uvl.v2 = fp->face_uvls[vn].v2; |
| 1779 | |
| 1780 | p->p3_flags |= PF_UV + PF_L + PF_UV2; // has uv and l set |
| 1781 | #ifndef RELEASE |
| 1782 | if ((fp->flags & FF_LIGHTMAP) && UseHardware) |
| 1783 | p->p3_uvl.l = Room_light_val; |
| 1784 | else |
| 1785 | p->p3_uvl.l = 1.0; |
| 1786 | #else |
| 1787 | p->p3_uvl.l = Room_light_val; |
| 1788 | #endif |
| 1789 | |
| 1790 | // do texture sliding |
| 1791 | p->p3_uvl.u += uchange; |
| 1792 | p->p3_uvl.v += vchange; |
| 1793 | face_cc.cc_and &= p->p3_codes; |
| 1794 | face_cc.cc_or |= p->p3_codes; |
| 1795 | } |
| 1796 | } else { |
| 1797 | |
| 1798 | for (vn = 0; vn < fp->num_verts; vn++) { |
| 1799 | pointbuffer[vn] = World_point_buffer[rp->wpb_index + fp->face_verts[vn]]; |
| 1800 | g3Point *p = &pointbuffer[vn]; |
| 1801 | pointlist[vn] = p; |
| 1802 | p->p3_uvl.u = fp->face_uvls[vn].u; |
| 1803 | p->p3_uvl.v = fp->face_uvls[vn].v; |
| 1804 | p->p3_uvl.u2 = fp->face_uvls[vn].u2; |
| 1805 | p->p3_uvl.v2 = fp->face_uvls[vn].v2; |
| 1806 | |
| 1807 | p->p3_flags |= PF_UV + PF_L + PF_UV2; // has uv and l set |
| 1808 | #ifndef RELEASE |
| 1809 | if ((fp->flags & FF_LIGHTMAP) && UseHardware) |
| 1810 | p->p3_uvl.l = Room_light_val; |
| 1811 | else |
| 1812 | p->p3_uvl.l = 1.0; |
| 1813 | #else |
| 1814 | p->p3_uvl.l = Room_light_val; |
| 1815 | #endif |
| 1816 | |
| 1817 | // do texture sliding |
| 1818 | p->p3_uvl.u += uchange; |
| 1819 | p->p3_uvl.v += vchange; |
| 1820 | face_cc.cc_and &= p->p3_codes; |
| 1821 | face_cc.cc_or |= p->p3_codes; |
| 1822 | } |
| 1823 | } |
| 1824 | if (face_cc.cc_and) // This entire face is off the screen |
| 1825 | { |
| 1826 | if (spec_face && GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) { |
| 1827 | fp->flags |= FF_SPEC_INVISIBLE; |
| 1828 | UpdateSpecularFace(rp, fp); |
| 1829 | } |
| 1830 | return; |
| 1831 | } |
| 1832 | // Do stupid gouraud shading for lightmap |
| 1833 | if (NoLightmaps) { |
| 1834 | if (first) { |
| 1835 | first = 0; |
| 1836 | for (int i = 0; i < 32; i++) { |
| 1837 | lm_red[i] = (float)i / 31.0; |
| 1838 | lm_green[i] = (float)i / 31.0; |
| 1839 | lm_blue[i] = (float)i / 31.0; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | if (fp->flags & FF_LIGHTMAP) { |
| 1844 | int lm_handle = LightmapInfo[fp->lmi_handle].lm_handle; |
| 1845 | uint16_t *data = (uint16_t *)lm_data(lm_handle); |
| 1846 | int w = lm_w(lm_handle); |
| 1847 | int h = lm_h(lm_handle); |
| 1848 | |
| 1849 | for (int i = 0; i < fp->num_verts; i++) { |
| 1850 | float u = fp->face_uvls[i].u2 * (w - 1); |
| 1851 | float v = fp->face_uvls[i].v2 * (h - 1); |
| 1852 | g3Point *p = &pointbuffer[i]; |
| 1853 | int int_u = u; |
| 1854 | int int_v = v; |
| 1855 | uint16_t texel = data[int_v * w + int_u]; |
| 1856 | int r = (texel >> 10) & 0x1f; |
| 1857 | int g = (texel >> 5) & 0x1f; |
| 1858 | int b = (texel) & 0x1f; |
| 1859 | p->p3_r = p->p3_l * lm_red[r]; |
| 1860 | p->p3_g = p->p3_l * lm_green[g]; |
| 1861 | p->p3_b = p->p3_l * lm_blue[b]; |
| 1862 | p->p3_flags |= PF_RGBA; |
| 1863 | } |
| 1864 | } else { |
| 1865 | for (int i = 0; i < fp->num_verts; i++) { |
| 1866 | g3Point *p = &pointbuffer[i]; |
| 1867 | p->p3_r = p->p3_l; |
| 1868 | p->p3_g = p->p3_l; |
| 1869 | p->p3_b = p->p3_l; |
| 1870 | p->p3_flags |= PF_RGBA; |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | // Get bitmap handle |
| 1875 | if ((fp->flags & FF_DESTROYED) && (GameTextures[fp->tmap].flags & TF_DESTROYABLE)) { |
| 1876 | bm_handle = GetTextureBitmap(GameTextures[fp->tmap].destroy_handle, 0); |
| 1877 | ASSERT(bm_handle != -1); |
| 1878 | } else |
| 1879 | bm_handle = GetTextureBitmap(fp->tmap, 0); |
| 1880 | // If searching, |
| 1881 | #ifdef EDITOR |
| 1882 | ddgr_color oldcolor; |
| 1883 | if (TSearch_on) { |
| 1884 | rend_SetPixel(GR_RGB(16, 255, 16), TSearch_x, TSearch_y); |
| 1885 | oldcolor = rend_GetPixel(TSearch_x, TSearch_y); |
| 1886 | } |
| 1887 | #endif |
| 1888 | // Set alpha, transparency, & lighting for this face |
| 1889 | rend_SetAlphaType(GetFaceAlpha(fp, bm_handle)); |
| 1890 | |
| 1891 | // If this is a mirror face, and mirrors are turned off, then just make opaque |
| 1892 | if (!Detail_settings.Mirrored_surfaces && rp->mirror_face != -1 && rp->faces[rp->mirror_face].tmap == fp->tmap) |
| 1893 | rend_SetAlphaValue(255); |
| 1894 | else if (Render_mirror_for_room && rp->mirror_face != -1 && fp->tmap == rp->faces[rp->mirror_face].tmap && |
| 1895 | rp != &Rooms[Mirror_room]) |
| 1896 | rend_SetAlphaValue(255); // This prevents mirrors from rendering each other |
| 1897 | else |
| 1898 | rend_SetAlphaValue(GameTextures[fp->tmap].alpha * 255); |
| 1899 | if (!UseHardware) |
| 1900 | rend_SetLighting(Lighting_on ? LS_GOURAUD : LS_NONE); |
| 1901 | else |
| 1902 | rend_SetLighting(LS_GOURAUD); |
| 1903 | if (!NoLightmaps) |
| 1904 | rend_SetColorModel(CM_MONO); |
| 1905 | else |
| 1906 | rend_SetColorModel(CM_RGB); |
| 1907 | // Set lighting map |
| 1908 | if ((fp->flags & FF_LIGHTMAP) && (!StateLimited || UseMultitexture)) { |
| 1909 | if (GameTextures[fp->tmap].flags & TF_SATURATE) |
| 1910 | rend_SetOverlayType(OT_NONE); |
| 1911 | else |
| 1912 | rend_SetOverlayType(OT_BLEND); |
| 1913 | rend_SetOverlayMap(LightmapInfo[fp->lmi_handle].lm_handle); |
| 1914 | } else |
| 1915 | rend_SetOverlayType(OT_NONE); |
| 1916 | // Select texture type |
| 1917 | if (!UseHardware) { |
| 1918 | tt = TT_LINEAR; // default to linear |
| 1919 | for (vn = 0; vn < fp->num_verts; vn++) // select perspective if close |
| 1920 | if (pointlist[vn]->p3_vec.z < 35) { |
| 1921 | tt = TT_PERSPECTIVE; |
| 1922 | break; |
| 1923 | } |
| 1924 | rend_SetTextureType(tt); |
| 1925 | } else |
| 1926 | rend_SetTextureType(TT_PERSPECTIVE); |
| 1927 | if (face_cc.cc_or) // Possible triangulate this face cuz its off screen somewhat |
| 1928 | { |
| 1929 | if (Room_light_val < 1.0) |
| 1930 | do_triangle_test = 1; |
| 1931 | if (Detail_settings.Fog_enabled && (rp->flags & RF_FOG)) |
| 1932 | do_triangle_test = 1; |
| 1933 | if (spec_face) |
| 1934 | do_triangle_test = 1; |
| 1935 | } |
| 1936 | if (do_triangle_test) { |
| 1937 | fp->flags |= FF_TRIANGULATED; |
| 1938 | g3_SetTriangulationTest(1); |
| 1939 | } |
| 1940 | // Do special fog stuff for portal faces |
| 1941 | if (rp->flags & RF_FOG && !In_editor_mode) { |
| 1942 | if (fp->portal_num != -1 && !(rp->portals[fp->portal_num].flags & PF_RENDER_FACES)) { |
| 1943 | drawn = 1; |
| 1944 | goto draw_fog; |
| 1945 | } |
| 1946 | } |
| 1947 | // Draw the damn thing |
| 1948 | drawn = g3_DrawPoly(fp->num_verts, pointlist, bm_handle, MAP_TYPE_BITMAP, &face_cc); |
| 1949 | #ifdef EDITOR |
| 1950 | if (TSearch_on) { |
| 1951 | if (rend_GetPixel(TSearch_x, TSearch_y) != oldcolor) { |
| 1952 | TSearch_found_type = TSEARCH_FOUND_MINE; |
| 1953 | TSearch_seg = rp - Rooms; |
| 1954 | TSearch_face = facenum; |
| 1955 | } |
| 1956 | } |
| 1957 | #endif |
| 1958 | |
| 1959 | // Do light saturation |
| 1960 | if (!Render_mirror_for_room && Rendering_main_view && drawn && fp->portal_num == -1 && |
| 1961 | ((fp->flags & FF_CORONA) || FastCoronas) && (fp->flags & FF_LIGHTMAP) && UseHardware && |
| 1962 | (GameTextures[fp->tmap].flags & TF_LIGHT)) { |
| 1963 | if (Num_glows_this_frame < MAX_LIGHT_GLOWS && Detail_settings.Coronas_enabled) { |
| 1964 | LightGlowsThisFrame[Num_glows_this_frame].roomnum = rp - Rooms; |
| 1965 | LightGlowsThisFrame[Num_glows_this_frame].facenum = facenum; |
| 1966 | Num_glows_this_frame++; |
| 1967 | } |
| 1968 | } |
| 1969 | // Draw a specular face |
| 1970 | if (!Render_mirror_for_room && spec_face) { |
| 1971 | if (drawn) |
| 1972 | UpdateSpecularFace(rp, fp); |
| 1973 | else { |
| 1974 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) { |
| 1975 | fp->flags |= FF_SPEC_INVISIBLE; |
| 1976 | UpdateSpecularFace(rp, fp); |
| 1977 | } |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | // Draw scorches, if any |
| 1982 | if (drawn && fp->flags & FF_SCORCHED && !Render_mirror_for_room) { |
| 1983 | if (!StateLimited) |
| 1984 | DrawScorches(ROOMNUM(rp), facenum); |
| 1985 | else { |
| 1986 | Scorches_to_render[Num_scorches_to_render] = facenum; |
| 1987 | Num_scorches_to_render++; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | draw_fog: |
| 1992 | if (!Render_mirror_for_room && !In_editor_mode && drawn && (rp->flags & RF_FOG) && UseHardware) { |
| 1993 | UpdateFogFace(rp, fp); |
| 1994 | } |
| 1995 | |
| 1996 | if (do_triangle_test) |
| 1997 | g3_SetTriangulationTest(0); |
| 1998 | // Mark it as rendered |
| 1999 | if (drawn) |
| 2000 | fp->renderframe = FrameCount % 256; |
| 2001 | // if (Render_mirror_for_room && (GameTextures[fp->tmap].flags & TF_ALPHA)) |
| 2002 | // rend_SetZBufferWriteMask (1); |
| 2003 | #ifdef EDITOR |
| 2004 | if (OUTLINE_ON(OM_MINE)) // Outline the face |
| 2005 | { |
| 2006 | rend_SetTextureType(TT_FLAT); |
| 2007 | rend_SetAlphaType(AT_ALWAYS); |
| 2008 | rend_SetFlatColor(GR_RGB(255, 255, 255)); |
| 2009 | |
| 2010 | if (UseHardware) { |
| 2011 | rend_SetZBias(-.1f); |
| 2012 | for (int i = 0; i < fp->num_verts; i++) { |
| 2013 | g3_DrawSpecialLine(pointlist[i], pointlist[(i + 1) % fp->num_verts]); |
| 2014 | } |
| 2015 | rend_SetZBias(0); |
| 2016 | } else { |
| 2017 | for (int i = 0; i < fp->num_verts; i++) { |
| 2018 | g3_DrawLine(GR_RGB(255, 255, 255), pointlist[i], pointlist[(i + 1) % fp->num_verts]); |
| 2019 | } |
| 2020 | } |
| 2021 | if ((fp->flags & FF_HAS_TRIGGER) && (fp->num_verts > 3)) { |
| 2022 | g3_DrawLine(CUREDGE_COLOR, pointlist[0], pointlist[2]); |
| 2023 | g3_DrawLine(CUREDGE_COLOR, pointlist[1], pointlist[3]); |
| 2024 | } |
| 2025 | if (fp->special_handle != BAD_SPECIAL_FACE_INDEX) { |
| 2026 | g3Point p1, p2; |
| 2027 | vector verts[MAX_VERTS_PER_FACE]; |
| 2028 | vector center, end; |
| 2029 | for (int t = 0; t < fp->num_verts; t++) |
| 2030 | verts[t] = rp->verts[fp->face_verts[t]]; |
| 2031 | vm_GetCentroid(¢er, verts, fp->num_verts); |
| 2032 | vector subvec = SpecialFaces[fp->special_handle].spec_instance[0].bright_center - center; |
| 2033 | vm_NormalizeVectorFast(&subvec); |
| 2034 | end = center + subvec; |
| 2035 | g3_RotatePoint(&p1, ¢er); |
| 2036 | g3_RotatePoint(&p2, &end); |
| 2037 | g3_DrawLine(GR_RGB(255, 255, 255), &p1, &p2); |
| 2038 | /*for (t=0;t<fp->num_verts;t++) |
| 2039 | { |
| 2040 | end=rp->verts[fp->face_verts[t]]+SpecialFaces[fp->special_handle].vertnorms[t]; |
| 2041 | g3_RotatePoint (&p1,&rp->verts[fp->face_verts[t]]); |
| 2042 | g3_RotatePoint (&p2,&end); |
| 2043 | g3_DrawLine(GR_RGB(255,255,255),&p1,&p2); |
| 2044 | }*/ |
| 2045 | } |
| 2046 | } |
| 2047 | if (Outline_lightmaps) { |
| 2048 | rend_SetTextureType(TT_FLAT); |
| 2049 | rend_SetAlphaType(AT_ALWAYS); |
| 2050 | if (fp == &Curroomp->faces[Curface] && (fp->flags & FF_LIGHTMAP)) { |
| 2051 | ASSERT(fp->lmi_handle != BAD_LMI_INDEX); |
| 2052 | |
| 2053 | lightmap_info *lmi = &LightmapInfo[fp->lmi_handle]; |
| 2054 | uint16_t *src_data = (uint16_t *)lm_data(lmi->lm_handle); |
| 2055 | matrix facematrix; |
| 2056 | vector fvec = -lmi->normal; |
| 2057 | vm_VectorToMatrix(&facematrix, &fvec, NULL, NULL); |
| 2058 | vector rvec = facematrix.rvec * lmi->xspacing; |
| 2059 | vector uvec = facematrix.uvec * lmi->yspacing; |
| 2060 | vm_TransposeMatrix(&facematrix); |
| 2061 | int w = lm_w(lmi->lm_handle); |
| 2062 | int h = lm_h(lmi->lm_handle); |
| 2063 | for (int i = 0; i < w * h; i++) { |
| 2064 | int t; |
| 2065 | g3Point epoints[20]; |
| 2066 | vector evec[20]; |
| 2067 | int y = i / w; |
| 2068 | int x = i % w; |
| 2069 | evec[0] = lmi->upper_left - (y * uvec) + (x * rvec); |
| 2070 | g3_RotatePoint(&epoints[0], &evec[0]); |
| 2071 | pointlist[0] = &epoints[0]; |
| 2072 | evec[1] = lmi->upper_left - (y * uvec) + ((x + 1) * rvec); |
| 2073 | g3_RotatePoint(&epoints[1], &evec[1]); |
| 2074 | pointlist[1] = &epoints[1]; |
| 2075 | evec[2] = lmi->upper_left - ((y + 1) * uvec) + ((x + 1) * rvec); |
| 2076 | g3_RotatePoint(&epoints[2], &evec[2]); |
| 2077 | pointlist[2] = &epoints[2]; |
| 2078 | evec[3] = lmi->upper_left - ((y + 1) * uvec) + (x * rvec); |
| 2079 | g3_RotatePoint(&epoints[3], &evec[3]); |
| 2080 | pointlist[3] = &epoints[3]; |
| 2081 | |
| 2082 | if (!(src_data[y * w + x] & OPAQUE_FLAG)) { |
| 2083 | for (t = 0; t < 4; t++) |
| 2084 | g3_DrawLine(GR_RGB(255, 0, 255), pointlist[t], pointlist[(t + 1) % 4]); |
| 2085 | } else { |
| 2086 | for (t = 0; t < 4; t++) |
| 2087 | g3_DrawLine(GR_RGB(255, 255, 255), pointlist[t], pointlist[(t + 1) % 4]); |
| 2088 | } |
| 2089 | // Draw interpolated normals |
| 2090 | /*if (fp->special_handle!=BAD_SPECIAL_FACE_INDEX && SpecialFaces[fp->special_handle].normal_map!=NULL) |
| 2091 | { |
| 2092 | vector norm,from,to,temp; |
| 2093 | g3Point p1,p2; |
| 2094 | vm_MakeZero (&from); |
| 2095 | norm.x=Normal_table[SpecialFaces[fp->special_handle].normal_map[i*3+0]]; |
| 2096 | norm.y=Normal_table[SpecialFaces[fp->special_handle].normal_map[i*3+1]]; |
| 2097 | norm.z=Normal_table[SpecialFaces[fp->special_handle].normal_map[i*3+2]]; |
| 2098 | vm_MatrixMulVector (&temp,&norm,&facematrix); |
| 2099 | norm=temp; |
| 2100 | for (t=0;t<4;t++) |
| 2101 | from+=evec[t]; |
| 2102 | from/=4; |
| 2103 | to=from+norm; |
| 2104 | g3_RotatePoint (&p1,&from); |
| 2105 | g3_RotatePoint (&p2,&to); |
| 2106 | g3_DrawLine(GR_RGB(255,255,255),&p1,&p2); |
| 2107 | }*/ |
| 2108 | |
| 2109 | if (Search_lightmaps) { |
| 2110 | for (t = 0; t < 4; t++) |
| 2111 | g3_ProjectPoint(&epoints[t]); |
| 2112 | |
| 2113 | if (point_in_poly(4, epoints, TSearch_x, TSearch_y)) { |
| 2114 | found_lightmap = i; |
| 2115 | TSearch_found_type = TSEARCH_FOUND_MINE; |
| 2116 | TSearch_seg = ROOMNUM(rp); |
| 2117 | TSearch_face = facenum; |
| 2118 | } |
| 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | #endif |
| 2124 | } |
| 2125 | static float face_depth[MAX_FACES_PER_ROOM]; |
| 2126 | // compare function for room face sort |
| 2127 | static int room_face_sort_func(const int16_t *a, const int16_t *b) { |
| 2128 | float az, bz; |
| 2129 | az = face_depth[*a]; |
| 2130 | bz = face_depth[*b]; |
| 2131 | if (az < bz) |
| 2132 | return -1; |
| 2133 | else if (az > bz) |
| 2134 | return 1; |
| 2135 | else |
| 2136 | return 0; |
| 2137 | } |
| 2138 | // Sorts the faces of a room before rendering. Used for rendering in the editor. |
| 2139 | // Parameters: rp - pointer to the room to be rendered |
| 2140 | void RenderRoomSorted(room *rp) { |
| 2141 | int vn, fn, i, rcount; |
| 2142 | int render_order[MAX_FACES_PER_ROOM]; |
| 2143 | ASSERT(rp->num_faces <= MAX_FACES_PER_ROOM); |
| 2144 | // Rotate all the points |
| 2145 | if (rp->wpb_index == -1) { |
| 2146 | rp->wpb_index = Global_buffer_index; |
| 2147 | // Katmai enhanced rotate only in a release build, because not |
| 2148 | // everyone has the intel compiler! |
| 2149 | #if (defined(RELEASE) && defined(KATMAI)) |
| 2150 | if (Katmai) |
| 2151 | RotateRoomPoints(rp, rp->verts4); |
| 2152 | else |
| 2153 | #endif |
| 2154 | RotateRoomPoints(rp, rp->verts); |
| 2155 | |
| 2156 | Global_buffer_index += rp->num_verts; |
| 2157 | } |
| 2158 | // Build list of visible (non-backfacing) faces, & compute average face depths |
| 2159 | for (fn = rcount = 0; fn < rp->num_faces; fn++) { |
| 2160 | face *fp = &rp->faces[fn]; |
| 2161 | |
| 2162 | if ((!(fp->flags & FF_VISIBLE)) || ((fp->flags & FF_NOT_FACING))) { |
| 2163 | fp->flags &= ~(FF_NOT_FACING | FF_VISIBLE); |
| 2164 | continue; // this guy shouldn't be rendered |
| 2165 | } |
| 2166 | // Clear visibility flags |
| 2167 | fp->flags &= ~(FF_VISIBLE | FF_NOT_FACING); |
| 2168 | |
| 2169 | if (!FaceIsRenderable(rp, fp)) |
| 2170 | continue; // skip this face |
| 2171 | #ifdef EDITOR |
| 2172 | if (In_editor_mode) { |
| 2173 | if ((Shell_render_flag & SRF_NO_NON_SHELL) && (fp->flags & FF_NOT_SHELL)) |
| 2174 | continue; |
| 2175 | if ((Shell_render_flag & SRF_NO_SHELL) && !(fp->flags & FF_NOT_SHELL)) |
| 2176 | continue; |
| 2177 | } |
| 2178 | #endif |
| 2179 | face_depth[fn] = 0; |
| 2180 | for (vn = 0; vn < fp->num_verts; vn++) |
| 2181 | face_depth[fn] += World_point_buffer[rp->wpb_index + fp->face_verts[vn]].p3_z; |
| 2182 | face_depth[fn] /= fp->num_verts; |
| 2183 | // initialize order list |
| 2184 | render_order[rcount] = fn; |
| 2185 | rcount++; |
| 2186 | } |
| 2187 | // Sort the faces |
| 2188 | qsort(render_order, rcount, sizeof(*render_order), (int (*)(const void *, const void *))room_face_sort_func); |
| 2189 | // Render the faces |
| 2190 | for (i = rcount - 1; i >= 0; i--) |
| 2191 | RenderFace(rp, render_order[i]); |
| 2192 | } |
| 2193 | |
| 2194 | // Sets up fog if this room is fogged |
| 2195 | void SetupRoomFog(room *rp, vector *eye, matrix *orient, int viewer_room) { |
| 2196 | if ((rp->flags & RF_FOG) == 0) |
| 2197 | return; |
| 2198 | |
| 2199 | if (!Detail_settings.Fog_enabled) { |
| 2200 | // fog is disabled |
| 2201 | Room_fog_plane_check = -1; |
| 2202 | return; |
| 2203 | } |
| 2204 | |
| 2205 | if (viewer_room == (rp - Rooms)) { |
| 2206 | // viewer is in the room |
| 2207 | vector *vec = eye; |
| 2208 | Room_fog_plane_check = 1; |
| 2209 | Room_fog_distance = -vm_DotProduct(&orient->fvec, vec); |
| 2210 | Room_fog_plane = orient->fvec; |
| 2211 | return; |
| 2212 | } |
| 2213 | |
| 2214 | // find the 'fogroom' number (we should have put it in here if we will render the room) |
| 2215 | int found_room = -1; |
| 2216 | for (int i = 0; i < Num_fogged_rooms_this_frame && found_room == -1; i++) { |
| 2217 | if (Fog_portal_data[i].roomnum == rp - Rooms) { |
| 2218 | found_room = i; |
| 2219 | break; |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | if (found_room == -1 || Fog_portal_data[found_room].close_face == NULL) { |
| 2224 | // we won't be rendering this room |
| 2225 | Room_fog_plane_check = -1; |
| 2226 | return; |
| 2227 | } |
| 2228 | |
| 2229 | // Use the closest face |
| 2230 | face *close_face = Fog_portal_data[found_room].close_face; |
| 2231 | Room_fog_plane_check = 0; |
| 2232 | Room_fog_plane = close_face->normal; |
| 2233 | Room_fog_portal_vert = rp->verts[close_face->face_verts[0]]; |
| 2234 | Room_fog_distance = -vm_DotProduct(&Room_fog_plane, &Room_fog_portal_vert); |
| 2235 | Room_fog_eye_distance = (*eye * Room_fog_plane) + Room_fog_distance; |
| 2236 | } |
| 2237 | |
| 2238 | // Renders the faces in a room without worrying about sorting. Used in the game when Z-buffering is active |
| 2239 | void RenderRoomUnsorted(room *rp) { |
| 2240 | int fn; |
| 2241 | int rcount = 0; |
| 2242 | ASSERT(rp->num_faces <= MAX_FACES_PER_ROOM); |
| 2243 | |
| 2244 | // Rotate points in this room if need be |
| 2245 | if (rp->wpb_index == -1) { |
| 2246 | rp->wpb_index = Global_buffer_index; |
| 2247 | |
| 2248 | // Katmai enhanced rotate only in a release build, because not |
| 2249 | // everyone has the intel compiler! |
| 2250 | #if (defined(RELEASE) && defined(KATMAI)) |
| 2251 | if (Katmai) { |
| 2252 | RotateRoomPoints(rp, rp->verts4); |
| 2253 | } else |
| 2254 | #endif |
| 2255 | { |
| 2256 | RotateRoomPoints(rp, rp->verts); |
| 2257 | } |
| 2258 | |
| 2259 | Global_buffer_index += rp->num_verts; |
| 2260 | } |
| 2261 | |
| 2262 | // Sort portal faces if this is a fogged room |
| 2263 | if (rp->flags & RF_FOG) { |
| 2264 | SetupRoomFog(rp, &Viewer_eye, &Viewer_orient, Viewer_roomnum); |
| 2265 | } |
| 2266 | |
| 2267 | // Check for visible (non-backfacing) faces, & render |
| 2268 | for (fn = 0; fn < rp->num_faces; fn++) { |
| 2269 | face *fp = &rp->faces[fn]; |
| 2270 | int fogged_portal = 0; |
| 2271 | |
| 2272 | if (!(fp->flags & FF_VISIBLE) || (fp->flags & FF_NOT_FACING)) { |
| 2273 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) { |
| 2274 | if (!Render_mirror_for_room && Detail_settings.Specular_lighting && |
| 2275 | (GameTextures[fp->tmap].flags & TF_SPECULAR) && |
| 2276 | ((fp->special_handle != BAD_SPECIAL_FACE_INDEX) || (rp->flags & RF_EXTERNAL))) { |
| 2277 | fp->flags |= FF_SPEC_INVISIBLE; |
| 2278 | UpdateSpecularFace(rp, fp); |
| 2279 | } |
| 2280 | } |
| 2281 | fp->flags &= ~(FF_NOT_FACING | FF_VISIBLE); |
| 2282 | continue; // this guy shouldn't be rendered |
| 2283 | } |
| 2284 | |
| 2285 | // Clear visibility flags |
| 2286 | if (Render_mirror_for_room == false) { |
| 2287 | fp->flags &= ~(FF_VISIBLE | FF_NOT_FACING); |
| 2288 | } else { |
| 2289 | if (rp == &Rooms[Mirror_room]) { |
| 2290 | if (rp->faces[fn].tmap == rp->faces[rp->mirror_face].tmap) |
| 2291 | continue; // Don't render the mirror face if rendering the mirror |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | if (!FaceIsRenderable(rp, fp)) |
| 2296 | continue; // skip this face |
| 2297 | |
| 2298 | #ifdef EDITOR |
| 2299 | if (In_editor_mode) { |
| 2300 | if ((Shell_render_flag & SRF_NO_NON_SHELL) && (fp->flags & FF_NOT_SHELL)) |
| 2301 | continue; |
| 2302 | if ((Shell_render_flag & SRF_NO_SHELL) && !(fp->flags & FF_NOT_SHELL)) |
| 2303 | continue; |
| 2304 | } |
| 2305 | #endif |
| 2306 | |
| 2307 | if (fp->portal_num != -1 && !(rp->portals[fp->portal_num].flags & PF_RENDER_FACES) && (rp->flags & RF_FOG)) { |
| 2308 | fogged_portal = 1; |
| 2309 | } |
| 2310 | |
| 2311 | if (Render_mirror_for_room == false && (fogged_portal || (GetFaceAlpha(fp, -1) & (ATF_CONSTANT + ATF_VERTEX)))) { |
| 2312 | // Place alpha faces into our postrender list |
| 2313 | if (Num_postrenders < MAX_POSTRENDERS) { |
| 2314 | face_depth[fn] = 0.0f; |
| 2315 | for (int vn = 0; vn < fp->num_verts; vn++) { |
| 2316 | face_depth[fn] += World_point_buffer[rp->wpb_index + fp->face_verts[vn]].p3_z; |
| 2317 | } |
| 2318 | Postrender_list[Num_postrenders].type = PRT_WALL; |
| 2319 | Postrender_list[Num_postrenders].roomnum = rp - Rooms; |
| 2320 | Postrender_list[Num_postrenders].facenum = fn; |
| 2321 | Postrender_list[Num_postrenders++].z = face_depth[fn] /= fp->num_verts; |
| 2322 | ; |
| 2323 | } |
| 2324 | } else { |
| 2325 | if (!StateLimited) { |
| 2326 | RenderFace(rp, fn); |
| 2327 | } else { |
| 2328 | // setup order list |
| 2329 | State_elements[rcount].facenum = fn; |
| 2330 | if (fp->flags & FF_LIGHTMAP) |
| 2331 | State_elements[rcount].sort_key = (LightmapInfo[fp->lmi_handle].lm_handle * MAX_TEXTURES) + fp->tmap; |
| 2332 | else |
| 2333 | State_elements[rcount].sort_key = fp->tmap; |
| 2334 | rcount++; |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | if (StateLimited) { |
| 2340 | // Sort the faces |
| 2341 | SortStates(State_elements, rcount); |
| 2342 | |
| 2343 | // Render the faces |
| 2344 | int i; |
| 2345 | for (i = rcount - 1; i >= 0; i--) { |
| 2346 | RenderFace(rp, State_elements[i].facenum); |
| 2347 | } |
| 2348 | |
| 2349 | if (!UseMultitexture) { |
| 2350 | // Since we're state limited, we have to render lightmap faces completely separate |
| 2351 | // Now render lightmap faces |
| 2352 | rend_SetAlphaType(AT_LIGHTMAP_BLEND); |
| 2353 | rend_SetLighting(LS_GOURAUD); |
| 2354 | rend_SetColorModel(CM_MONO); |
| 2355 | rend_SetOverlayType(OT_NONE); |
| 2356 | rend_SetTextureType(TT_PERSPECTIVE); |
| 2357 | rend_SetWrapType(WT_CLAMP); |
| 2358 | rend_SetMipState(0); |
| 2359 | for (i = rcount - 1; i >= 0; i--) { |
| 2360 | RenderLightmapFace(rp, State_elements[i].facenum); |
| 2361 | } |
| 2362 | rend_SetWrapType(WT_WRAP); |
| 2363 | rend_SetMipState(1); |
| 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | // Figures out a scalar value to apply to all vertices in the room |
| 2369 | void ComputeRoomPulseLight(room *rp) { |
| 2370 | if (rp->pulse_time == 0 || In_editor_mode) |
| 2371 | Room_light_val = 1.0; |
| 2372 | else { |
| 2373 | float ptime = rp->pulse_time * WALL_PULSE_INCREMENT; |
| 2374 | float add_time = rp->pulse_offset * WALL_PULSE_INCREMENT; |
| 2375 | |
| 2376 | int int_time = (int)((Gametime + add_time) / (ptime * 2)); |
| 2377 | float left_time = (Gametime + add_time) - (int_time * ptime * 2); |
| 2378 | float norm_time = left_time / (ptime); |
| 2379 | if (norm_time > 1) |
| 2380 | Room_light_val = 1 - (norm_time - 1.0); |
| 2381 | else |
| 2382 | Room_light_val = norm_time; |
| 2383 | } |
| 2384 | if (!In_editor_mode) { |
| 2385 | if (rp->flags & RF_STROBE) { |
| 2386 | int val = (Gametime * 10) + (rp - Rooms); |
| 2387 | if (val % 2) |
| 2388 | Room_light_val = 0; |
| 2389 | } |
| 2390 | if (rp->flags & RF_FLICKER) { |
| 2391 | ps_srand((Gametime * 1000) + (rp - Rooms)); |
| 2392 | if (ps_rand() % 2) |
| 2393 | Room_light_val = 0; |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | #define CORONA_DIST_CUTOFF 5.0f |
| 2400 | // Draws a glow around a light |
| 2401 | void RenderSingleLightGlow(int index) { |
| 2402 | int bm_handle; |
| 2403 | room *rp = &Rooms[LightGlows[index].roomnum]; |
| 2404 | face *fp = &rp->faces[LightGlows[index].facenum]; |
| 2405 | texture *texp = &GameTextures[fp->tmap]; |
| 2406 | bm_handle = Fireballs[DEFAULT_CORONA_INDEX + texp->corona_type].bm_handle; |
| 2407 | |
| 2408 | // Get size of light |
| 2409 | float size = LightGlows[index].size; |
| 2410 | vector center = LightGlows[index].center; |
| 2411 | |
| 2412 | // Get alpha of light |
| 2413 | vector tvec = Viewer_eye - rp->verts[fp->face_verts[0]]; |
| 2414 | vm_NormalizeVectorFast(&tvec); |
| 2415 | float facing_scalar = (tvec * fp->normal) * 2; |
| 2416 | if (facing_scalar < 0) |
| 2417 | return; |
| 2418 | tvec = center - Viewer_eye; |
| 2419 | |
| 2420 | float dist = vm_GetMagnitudeFast(&tvec); |
| 2421 | if (dist < (size * CORONA_DIST_CUTOFF)) |
| 2422 | return; |
| 2423 | if (dist < (size * (CORONA_DIST_CUTOFF + 15))) { |
| 2424 | float dist_scalar = ((dist - (size * CORONA_DIST_CUTOFF)) / (size * 15)); |
| 2425 | facing_scalar *= dist_scalar; |
| 2426 | } |
| 2427 | |
| 2428 | facing_scalar *= LightGlows[index].scalar; |
| 2429 | facing_scalar = std::min<float>(facing_scalar, 1.0); |
| 2430 | // Take into effect pulsing |
| 2431 | ComputeRoomPulseLight(rp); |
| 2432 | facing_scalar *= Room_light_val; |
| 2433 | rend_SetAlphaValue(facing_scalar * .4 * 255); |
| 2434 | |
| 2435 | float maxc = std::max({texp->r, texp->g, texp->b}); |
| 2436 | float r, g, b; |
| 2437 | if (maxc > 1.0) { |
| 2438 | r = texp->r / maxc; |
| 2439 | g = texp->g / maxc; |
| 2440 | b = texp->b / maxc; |
| 2441 | } else { |
| 2442 | r = texp->r; |
| 2443 | g = texp->g; |
| 2444 | b = texp->b; |
| 2445 | } |
| 2446 | if (LightGlows[index].flags & LGF_FAST) { |
| 2447 | rend_SetZBufferWriteMask(0); |
| 2448 | rend_SetZBias((-size / 2)); |
| 2449 | } else { |
| 2450 | rend_SetZBufferState(0); |
| 2451 | } |
| 2452 | ddgr_color color = GR_RGB(r * 255, g * 255, b * 255); |
| 2453 | g3_DrawBitmap(¢er, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle, color); |
| 2454 | if (LightGlows[index].flags & LGF_FAST) { |
| 2455 | rend_SetZBufferWriteMask(1); |
| 2456 | rend_SetZBias(0); |
| 2457 | } else { |
| 2458 | rend_SetZBufferState(1); |
| 2459 | } |
| 2460 | } |
| 2461 | // Draws a glow around a light |
| 2462 | void RenderSingleLightGlow2(int index) { |
| 2463 | static int first = 1; |
| 2464 | static int normal_handle, star_handle; |
| 2465 | int bm_handle; |
| 2466 | room *rp = &Rooms[LightGlows[index].roomnum]; |
| 2467 | face *fp = &rp->faces[LightGlows[index].facenum]; |
| 2468 | texture *texp = &GameTextures[fp->tmap]; |
| 2469 | |
| 2470 | if (first) { |
| 2471 | int texhandle = FindTextureName("LongCorona"); |
| 2472 | if (texhandle == -1) |
| 2473 | star_handle = 0; |
| 2474 | else |
| 2475 | star_handle = GetTextureBitmap(texhandle, 0); |
| 2476 | first = 0; |
| 2477 | } |
| 2478 | rend_SetAlphaValue(.4 * 255); |
| 2479 | float maxc = std::max({texp->r, texp->g, texp->b}); |
| 2480 | float r, g, b; |
| 2481 | if (maxc > 1.0) { |
| 2482 | r = texp->r / maxc; |
| 2483 | g = texp->g / maxc; |
| 2484 | b = texp->b / maxc; |
| 2485 | } else { |
| 2486 | r = texp->r; |
| 2487 | g = texp->g; |
| 2488 | b = texp->b; |
| 2489 | } |
| 2490 | rend_SetLighting(LS_GOURAUD); |
| 2491 | |
| 2492 | // Get size of light |
| 2493 | float size = LightGlows[index].size; |
| 2494 | vector center = LightGlows[index].center; |
| 2495 | vector corona_pos = center + (fp->normal * size); |
| 2496 | bm_handle = star_handle; |
| 2497 | matrix mat, rot_mat; |
| 2498 | vector fvec, uvec, temp_vec, rvec; |
| 2499 | temp_vec = -fp->normal; |
| 2500 | |
| 2501 | vm_VectorToMatrix(&mat, NULL, &temp_vec, NULL); |
| 2502 | |
| 2503 | // Rotate view vector into billboard space |
| 2504 | fvec = Viewer_eye - corona_pos; |
| 2505 | vm_NormalizeVectorFast(&fvec); |
| 2506 | temp_vec = fvec * mat; |
| 2507 | fvec = temp_vec; |
| 2508 | vm_NormalizeVectorFast(&fvec); |
| 2509 | rvec.x = fvec.z; |
| 2510 | rvec.y = 0; |
| 2511 | rvec.z = -fvec.x; |
| 2512 | uvec.y = 1; |
| 2513 | uvec.x = 0; |
| 2514 | uvec.z = 0; |
| 2515 | vm_VectorToMatrix(&rot_mat, NULL, &uvec, &rvec); |
| 2516 | vm_TransposeMatrix(&mat); |
| 2517 | temp_vec = rot_mat.rvec * mat; |
| 2518 | rot_mat.rvec = temp_vec; |
| 2519 | temp_vec = rot_mat.uvec * mat; |
| 2520 | rot_mat.uvec = temp_vec; |
| 2521 | temp_vec = rot_mat.fvec * mat; |
| 2522 | rot_mat.fvec = temp_vec; |
| 2523 | vector world_vecs[4]; |
| 2524 | g3Point pnts[4], *pntlist[4]; |
| 2525 | world_vecs[0] = corona_pos - (size * rot_mat.rvec); |
| 2526 | world_vecs[0] += (size * rot_mat.uvec); |
| 2527 | world_vecs[1] = corona_pos + (size * rot_mat.rvec); |
| 2528 | world_vecs[1] += (size * rot_mat.uvec); |
| 2529 | world_vecs[2] = corona_pos + (size * rot_mat.rvec); |
| 2530 | world_vecs[2] -= (size * rot_mat.uvec); |
| 2531 | world_vecs[3] = corona_pos - (size * rot_mat.rvec); |
| 2532 | world_vecs[3] -= (size * rot_mat.uvec); |
| 2533 | for (int i = 0; i < 4; i++) { |
| 2534 | g3_RotatePoint(&pnts[i], &world_vecs[i]); |
| 2535 | pnts[i].p3_flags |= PF_UV | PF_RGBA; |
| 2536 | pnts[i].p3_r = r; |
| 2537 | pnts[i].p3_g = g; |
| 2538 | pnts[i].p3_b = b; |
| 2539 | pntlist[i] = &pnts[i]; |
| 2540 | } |
| 2541 | pnts[0].p3_u = 0; |
| 2542 | pnts[0].p3_v = 0; |
| 2543 | pnts[1].p3_u = 1; |
| 2544 | pnts[1].p3_v = 0; |
| 2545 | pnts[2].p3_u = 1; |
| 2546 | pnts[2].p3_v = 1; |
| 2547 | pnts[3].p3_u = 0; |
| 2548 | pnts[3].p3_v = 1; |
| 2549 | g3_DrawPoly(4, pntlist, bm_handle); |
| 2550 | rend_SetZBufferWriteMask(1); |
| 2551 | } |
| 2552 | // Figures out if we can see the center of a light face and adds it to our globa list |
| 2553 | void CheckLightGlowsForRoom(room *rp) { |
| 2554 | int i; |
| 2555 | for (i = 0; i < Num_glows_this_frame; i++) { |
| 2556 | // For each light, see if we can cast a vector to it |
| 2557 | face *fp = &rp->faces[LightGlowsThisFrame[i].facenum]; |
| 2558 | vector verts[MAX_VERTS_PER_FACE]; |
| 2559 | vector center; |
| 2560 | for (int t = 0; t < fp->num_verts; t++) |
| 2561 | verts[t] = rp->verts[fp->face_verts[t]]; |
| 2562 | float size = sqrt(vm_GetCentroid(¢er, verts, fp->num_verts)); |
| 2563 | size *= 2; |
| 2564 | center += (fp->normal / 4); |
| 2565 | if (vm_VectorDistanceQuick(¢er, &Viewer_eye) < (size * CORONA_DIST_CUTOFF)) |
| 2566 | continue; |
| 2567 | // Check if we can see this light |
| 2568 | fvi_info hit_info; |
| 2569 | fvi_query fq; |
| 2570 | // shoot a ray from the light position to the current vertex |
| 2571 | if (FastCoronas) { |
| 2572 | if (rp->flags & RF_EXTERNAL) { |
| 2573 | SetGlowStatus(rp - Rooms, LightGlowsThisFrame[i].facenum, ¢er, size, FastCoronas); |
| 2574 | continue; |
| 2575 | } |
| 2576 | vector subvec = Viewer_eye - center; |
| 2577 | vm_NormalizeVectorFast(&subvec); |
| 2578 | subvec *= size; |
| 2579 | subvec += center; |
| 2580 | fq.p0 = ¢er; |
| 2581 | fq.p1 = &subvec; |
| 2582 | fq.startroom = rp - Rooms; |
| 2583 | } else { |
| 2584 | fq.p0 = &Viewer_eye; |
| 2585 | fq.p1 = ¢er; |
| 2586 | fq.startroom = Viewer_object->roomnum; |
| 2587 | } |
| 2588 | |
| 2589 | fq.rad = 0.0f; |
| 2590 | fq.flags = FQ_CHECK_OBJS | FQ_NO_RELINK | FQ_IGNORE_WEAPONS | FQ_ROBOTS_AS_SPHERE | FQ_PLAYERS_AS_SPHERE; |
| 2591 | fq.thisobjnum = -1; |
| 2592 | fq.ignore_obj_list = NULL; |
| 2593 | int fate = fvi_FindIntersection(&fq, &hit_info); |
| 2594 | if (fate != HIT_NONE) |
| 2595 | continue; |
| 2596 | SetGlowStatus(rp - Rooms, LightGlowsThisFrame[i].facenum, ¢er, size, FastCoronas); |
| 2597 | } |
| 2598 | } |
| 2599 | // Called before a frame starts to render - sets all of our light glows to decreasing |
| 2600 | void PreUpdateAllLightGlows() { |
| 2601 | int i, count; |
| 2602 | for (i = 0, count = 0; i < MAX_LIGHT_GLOWS && count < Num_glows; i++) { |
| 2603 | if (LightGlows[i].flags & LGF_USED) { |
| 2604 | count++; |
| 2605 | LightGlows[i].flags &= ~LGF_INCREASING; |
| 2606 | } |
| 2607 | } |
| 2608 | } |
| 2609 | // Called after a frame has been rendered - slowly morphs our light glows into nothing |
| 2610 | void PostUpdateAllLightGlows() { |
| 2611 | int i, count; |
| 2612 | for (i = 0, count = 0; i < MAX_LIGHT_GLOWS && count < Num_glows; i++) { |
| 2613 | if (LightGlows[i].flags & LGF_USED) { |
| 2614 | count++; |
| 2615 | if (LightGlows[i].flags & LGF_INCREASING) { |
| 2616 | LightGlows[i].scalar += (Frametime * 4); |
| 2617 | if (LightGlows[i].scalar > 1) |
| 2618 | LightGlows[i].scalar = 1; |
| 2619 | } else { |
| 2620 | LightGlows[i].scalar -= (Frametime * 4); |
| 2621 | if (LightGlows[i].scalar < 0) { |
| 2622 | LightGlows[i].scalar = 0; |
| 2623 | LightGlows[i].flags &= ~LGF_USED; |
| 2624 | Num_glows--; |
| 2625 | count--; |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | ASSERT(Num_glows >= 0); |
| 2632 | } |
| 2633 | // Recursive function that mirrored rooms use |
| 2634 | void BuildMirroredRoomListSub(int start_room_num, clip_wnd *wnd) { |
| 2635 | room *rp = &Rooms[start_room_num]; |
| 2636 | g3Point portal_points[MAX_VERTS_PER_FACE]; |
| 2637 | int i, t; |
| 2638 | if (!Mirrored_room_checked[start_room_num]) { |
| 2639 | Mirrored_room_list[Num_mirrored_rooms++] = start_room_num; |
| 2640 | } |
| 2641 | Mirrored_room_checked[start_room_num] = 1; |
| 2642 | |
| 2643 | // If this room is a closed (non-seethrough) door, don't check any of its portals, |
| 2644 | //...UNLESS this is the first room we're looking at (meaning the viewer is in this room) |
| 2645 | if ((rp->flags & RF_DOOR) && (DoorwayGetPosition(rp) == 0.0) && |
| 2646 | !(Doors[rp->doorway_data->doornum].flags & DF_SEETHROUGH)) |
| 2647 | return; |
| 2648 | room *mirror_rp = &Rooms[Mirror_room]; |
| 2649 | vector *mirror_vec = &mirror_rp->verts[mirror_rp->faces[mirror_rp->mirror_face].face_verts[0]]; |
| 2650 | face *mirror_fp = &mirror_rp->faces[mirror_rp->mirror_face]; |
| 2651 | vector *mirror_norm = &mirror_fp->normal; |
| 2652 | // This is how far the mirror face is from the normalized plane |
| 2653 | float mirror_dist = |
| 2654 | -(mirror_vec->x * mirror_norm->x + mirror_vec->y * mirror_norm->y + mirror_vec->z * mirror_norm->z); |
| 2655 | // Check all the portals for this room |
| 2656 | for (t = 0; t < rp->num_portals; t++) { |
| 2657 | portal *pp = &rp->portals[t]; |
| 2658 | int croom = pp->croom; |
| 2659 | ASSERT(croom >= 0); |
| 2660 | // If we are an external room portalizing into another external room, then skip! |
| 2661 | if ((rp->flags & RF_EXTERNAL) && (Rooms[croom].flags & RF_EXTERNAL)) |
| 2662 | continue; |
| 2663 | // Check if we can see through this portal, and if not, skip it |
| 2664 | if (!RenderPastPortal(rp, pp)) |
| 2665 | continue; |
| 2666 | // If this portal has been visited, skip it |
| 2667 | if (Mirrored_room_checked[croom]) |
| 2668 | continue; |
| 2669 | // Deal with external portals differently |
| 2670 | int external_door_hack = 0; |
| 2671 | if (rp->flags & RF_EXTERNAL && Rooms[croom].flags & RF_DOOR) |
| 2672 | external_door_hack = 1; |
| 2673 | // Get pointer to this portal's face |
| 2674 | face *fp = &rp->faces[pp->portal_face]; |
| 2675 | // See if portal is facing toward us |
| 2676 | if (!external_door_hack) { |
| 2677 | vector temp_vec; |
| 2678 | vector *vec = &rp->verts[fp->face_verts[0]]; |
| 2679 | float dist_from_mirror = |
| 2680 | vec->x * mirror_norm->x + vec->y * mirror_norm->y + vec->z * mirror_norm->z + mirror_dist; |
| 2681 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 2682 | temp_vec = *vec - (*mirror_norm * (dist_from_mirror * 2)); |
| 2683 | vector incident_norm; |
| 2684 | ReflectRay(&incident_norm, &fp->normal, &mirror_fp->normal); |
| 2685 | |
| 2686 | vector tvec = Viewer_eye - temp_vec; |
| 2687 | if ((tvec * incident_norm) <= 0) |
| 2688 | continue; // not facing |
| 2689 | } |
| 2690 | |
| 2691 | g3Codes cc; |
| 2692 | cc.cc_or = 0; |
| 2693 | cc.cc_and = 0xff; |
| 2694 | int nv = fp->num_verts; |
| 2695 | // Code the face points |
| 2696 | for (i = 0; i < nv; i++) { |
| 2697 | vector temp_vec; |
| 2698 | vector *vec = &rp->verts[fp->face_verts[i]]; |
| 2699 | float dist_from_mirror = |
| 2700 | vec->x * mirror_norm->x + vec->y * mirror_norm->y + vec->z * mirror_norm->z + mirror_dist; |
| 2701 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 2702 | temp_vec = *vec - (*mirror_norm * (dist_from_mirror * 2)); |
| 2703 | g3_RotatePoint(&portal_points[i], &temp_vec); |
| 2704 | |
| 2705 | uint8_t c = portal_points[i].p3_codes; |
| 2706 | cc.cc_and &= c; |
| 2707 | cc.cc_or |= c; |
| 2708 | } |
| 2709 | // If points are on screen, see if they're in the clip window |
| 2710 | if (cc.cc_and == 0 || external_door_hack) { |
| 2711 | bool clipped = 0; |
| 2712 | g3Point *pointlist[MAX_VERTS_PER_FACE], **pl = pointlist; |
| 2713 | for (i = 0; i < nv; i++) |
| 2714 | pointlist[i] = &portal_points[i]; |
| 2715 | // If portal not all on screen, must clip it |
| 2716 | if (cc.cc_or) { |
| 2717 | pl = g3_ClipPolygon(pl, &nv, &cc); |
| 2718 | clipped = 1; |
| 2719 | } |
| 2720 | cc.cc_and = 0xff; |
| 2721 | for (i = 0; i < nv; i++) { |
| 2722 | g3_ProjectPoint(pl[i]); |
| 2723 | cc.cc_and &= clip2d(pl[i], wnd); |
| 2724 | } |
| 2725 | // Make sure it didn't get clipped away |
| 2726 | if (cc.cc_and == 0 || external_door_hack) { |
| 2727 | clip_wnd new_wnd; |
| 2728 | new_wnd.right = new_wnd.bot = 0.0; |
| 2729 | new_wnd.left = Render_width; |
| 2730 | new_wnd.top = Render_height; |
| 2731 | // make new clip window |
| 2732 | for (i = 0; i < nv; i++) { |
| 2733 | float x = pl[i]->p3_sx, y = pl[i]->p3_sy; |
| 2734 | if (x < new_wnd.left) |
| 2735 | new_wnd.left = x; |
| 2736 | if (x > new_wnd.right) |
| 2737 | new_wnd.right = x; |
| 2738 | if (y < new_wnd.top) |
| 2739 | new_wnd.top = y; |
| 2740 | if (y > new_wnd.bot) |
| 2741 | new_wnd.bot = y; |
| 2742 | } |
| 2743 | |
| 2744 | // Combine the two windows |
| 2745 | new_wnd.left = std::max(wnd->left, new_wnd.left); |
| 2746 | new_wnd.right = std::min(wnd->right, new_wnd.right); |
| 2747 | new_wnd.top = std::max(wnd->top, new_wnd.top); |
| 2748 | new_wnd.bot = std::min(wnd->bot, new_wnd.bot); |
| 2749 | if (clipped) { // Free up temp points |
| 2750 | g3_FreeTempPoints(pl, nv); |
| 2751 | clipped = 0; |
| 2752 | } |
| 2753 | BuildMirroredRoomListSub(croom, &new_wnd); |
| 2754 | } |
| 2755 | if (clipped) // Free up temp points |
| 2756 | g3_FreeTempPoints(pl, nv); |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | // Goes through and builds a list of rooms that can be seen from a mirror |
| 2761 | void BuildMirroredRoomList() { |
| 2762 | room *rp = &Rooms[Mirror_room]; |
| 2763 | clip_wnd wnd; |
| 2764 | Num_mirrored_rooms = 0; |
| 2765 | memset(Mirrored_room_checked, 0, MAX_ROOMS); |
| 2766 | Mirrored_room_checked[Mirror_room] = 1; |
| 2767 | Mirrored_room_list[Num_mirrored_rooms++] = Mirror_room; |
| 2768 | |
| 2769 | // Initial clip window is whole screen |
| 2770 | wnd.left = wnd.top = 0.0; |
| 2771 | wnd.right = Render_width; |
| 2772 | wnd.bot = Render_height; |
| 2773 | g3Point portal_points[MAX_VERTS_PER_FACE], temp_points[MAX_VERTS_PER_FACE * 2]; |
| 2774 | g3Point *pointlist[MAX_VERTS_PER_FACE * 2], **pl = pointlist; |
| 2775 | room *mirror_rp = &Rooms[Mirror_room]; |
| 2776 | vector *mirror_vec = &mirror_rp->verts[mirror_rp->faces[mirror_rp->mirror_face].face_verts[0]]; |
| 2777 | face *mirror_fp = &mirror_rp->faces[mirror_rp->mirror_face]; |
| 2778 | vector *mirror_norm = &mirror_fp->normal; |
| 2779 | // This is how far the mirror face is from the normalized plane |
| 2780 | float mirror_dist = |
| 2781 | -(mirror_vec->x * mirror_norm->x + mirror_vec->y * mirror_norm->y + mirror_vec->z * mirror_norm->z); |
| 2782 | int total_points = 0; |
| 2783 | for (int t = 0; t < rp->num_mirror_faces; t++) { |
| 2784 | face *fp = &rp->faces[rp->mirror_faces_list[t]]; |
| 2785 | int i; |
| 2786 | ASSERT(total_points + fp->num_verts <= MAX_VERTS_PER_FACE * 2); |
| 2787 | g3Codes cc; |
| 2788 | cc.cc_and = 0xff; |
| 2789 | cc.cc_or = 0; |
| 2790 | int nv = fp->num_verts; |
| 2791 | int clipped = 0; |
| 2792 | |
| 2793 | for (i = 0; i < fp->num_verts; i++) { |
| 2794 | vector temp_vec; |
| 2795 | vector *vec = &rp->verts[fp->face_verts[i]]; |
| 2796 | float dist_from_mirror = |
| 2797 | vec->x * mirror_norm->x + vec->y * mirror_norm->y + vec->z * mirror_norm->z + mirror_dist; |
| 2798 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 2799 | temp_vec = *vec - (*mirror_norm * (dist_from_mirror * 2)); |
| 2800 | g3_RotatePoint(&portal_points[i], &temp_vec); |
| 2801 | cc.cc_and &= portal_points[i].p3_codes; |
| 2802 | cc.cc_or |= portal_points[i].p3_codes; |
| 2803 | pointlist[i] = &portal_points[i]; |
| 2804 | } |
| 2805 | |
| 2806 | // Clipped away |
| 2807 | if (cc.cc_and) |
| 2808 | continue; |
| 2809 | if (cc.cc_or) { |
| 2810 | // Must clip |
| 2811 | pl = g3_ClipPolygon(pl, &nv, &cc); |
| 2812 | |
| 2813 | if (cc.cc_and) { |
| 2814 | g3_FreeTempPoints(pl, nv); |
| 2815 | continue; |
| 2816 | } else { |
| 2817 | for (i = 0; i < nv; i++) { |
| 2818 | temp_points[i + total_points] = *pl[i]; |
| 2819 | temp_points[i + total_points].p3_flags &= ~PF_TEMP_POINT; |
| 2820 | } |
| 2821 | g3_FreeTempPoints(pl, nv); |
| 2822 | } |
| 2823 | } else { |
| 2824 | for (i = 0; i < nv; i++) { |
| 2825 | temp_points[i + total_points] = *pl[i]; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | for (i = 0; i < nv; i++) |
| 2830 | g3_ProjectPoint(&temp_points[total_points + i]); |
| 2831 | |
| 2832 | total_points += nv; |
| 2833 | } |
| 2834 | // make new clip window |
| 2835 | clip_wnd new_wnd; |
| 2836 | new_wnd.right = new_wnd.bot = 0.0; |
| 2837 | new_wnd.left = Render_width; |
| 2838 | new_wnd.top = Render_height; |
| 2839 | for (int i = 0; i < total_points; i++) { |
| 2840 | float x = temp_points[i].p3_sx, y = temp_points[i].p3_sy; |
| 2841 | |
| 2842 | if (x < new_wnd.left) |
| 2843 | new_wnd.left = x; |
| 2844 | if (x > new_wnd.right) |
| 2845 | new_wnd.right = x; |
| 2846 | if (y < new_wnd.top) |
| 2847 | new_wnd.top = y; |
| 2848 | if (y > new_wnd.bot) |
| 2849 | new_wnd.bot = y; |
| 2850 | } |
| 2851 | new_wnd.left = std::max(wnd.left, new_wnd.left); |
| 2852 | new_wnd.right = std::min(wnd.right, new_wnd.right); |
| 2853 | new_wnd.top = std::max(wnd.top, new_wnd.top); |
| 2854 | new_wnd.bot = std::min(wnd.bot, new_wnd.bot); |
| 2855 | /*rend_SetTextureType (TT_FLAT); |
| 2856 | rend_SetAlphaType (AT_CONSTANT); |
| 2857 | rend_SetAlphaValue (255); |
| 2858 | rend_SetFlatColor (GR_RGB(255,255,255)); |
| 2859 | |
| 2860 | rend_DrawLine (new_wnd.left,new_wnd.top,new_wnd.right,new_wnd.top); |
| 2861 | rend_DrawLine (new_wnd.right,new_wnd.top,new_wnd.right,new_wnd.bot); |
| 2862 | rend_DrawLine (new_wnd.right,new_wnd.bot,new_wnd.left,new_wnd.bot); |
| 2863 | rend_DrawLine (new_wnd.left,new_wnd.bot,new_wnd.left,new_wnd.top);*/ |
| 2864 | |
| 2865 | BuildMirroredRoomListSub(Mirror_room, &new_wnd); |
| 2866 | } |
| 2867 | vector mirror_dest_vecs[MAX_VERTS_PER_ROOM]; |
| 2868 | g3Point mirror_save_points[MAX_VERTS_PER_ROOM]; |
| 2869 | // Renders a mirror flipped about the mirrored plane |
| 2870 | void RenderMirroredRoom(room *rp) { |
| 2871 | int i; |
| 2872 | #if (defined(RELEASE) && defined(KATMAI)) |
| 2873 | vector4 kat_vecs[MAX_VERTS_PER_ROOM]; |
| 2874 | #endif |
| 2875 | uint16_t save_flags[MAX_FACES_PER_ROOM]; |
| 2876 | bool restore_index = true; |
| 2877 | int save_index = Global_buffer_index; |
| 2878 | |
| 2879 | // Save old rotated points |
| 2880 | if (rp->wpb_index == -1) { |
| 2881 | restore_index = false; |
| 2882 | rp->wpb_index = Global_buffer_index; |
| 2883 | } else { |
| 2884 | for (i = 0; i < rp->num_verts; i++) |
| 2885 | mirror_save_points[i] = World_point_buffer[rp->wpb_index + i]; |
| 2886 | } |
| 2887 | |
| 2888 | // Find facing faces for this mirror |
| 2889 | face *fp = &rp->faces[0]; |
| 2890 | for (i = 0; i < rp->num_faces; i++, fp++) { |
| 2891 | save_flags[i] = fp->flags; |
| 2892 | fp->flags &= ~FF_NOT_FACING; |
| 2893 | fp->flags |= FF_VISIBLE; |
| 2894 | } |
| 2895 | room *mirror_rp = &Rooms[Mirror_room]; |
| 2896 | vector *mirror_vec = &mirror_rp->verts[mirror_rp->faces[mirror_rp->mirror_face].face_verts[0]]; |
| 2897 | face *mirror_fp = &mirror_rp->faces[mirror_rp->mirror_face]; |
| 2898 | vector *norm = &mirror_fp->normal; |
| 2899 | // This is how far the mirror face is from the normalized plane |
| 2900 | float mirror_dist = -(mirror_vec->x * norm->x + mirror_vec->y * norm->y + mirror_vec->z * norm->z); |
| 2901 | |
| 2902 | for (i = 0; i < rp->num_verts; i++) { |
| 2903 | vector *vec = &rp->verts[i]; |
| 2904 | float dist_from_mirror = vec->x * norm->x + vec->y * norm->y + vec->z * norm->z + mirror_dist; |
| 2905 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 2906 | mirror_dest_vecs[i] = *vec - (*norm * (dist_from_mirror * 2)); |
| 2907 | #if (defined(RELEASE) && defined(KATMAI)) |
| 2908 | if (Katmai) { |
| 2909 | kat_vecs[i].x = mirror_dest_vecs[i].x; |
| 2910 | kat_vecs[i].y = mirror_dest_vecs[i].y; |
| 2911 | kat_vecs[i].z = mirror_dest_vecs[i].z; |
| 2912 | } |
| 2913 | #endif |
| 2914 | } |
| 2915 | // Rotate our mirror points |
| 2916 | vector revnorm = -*norm; |
| 2917 | g3_SetCustomClipPlane(1, mirror_vec, &revnorm); |
| 2918 | |
| 2919 | // Katmai enhanced rotate only in a release build, because not |
| 2920 | // everyone has the intel compiler! |
| 2921 | #if (defined(RELEASE) && defined(KATMAI)) |
| 2922 | if (Katmai) |
| 2923 | RotateRoomPoints(rp, kat_vecs); |
| 2924 | else |
| 2925 | #endif |
| 2926 | RotateRoomPoints(rp, mirror_dest_vecs); |
| 2927 | |
| 2928 | // Mark facing faces |
| 2929 | int save_frame = Facing_visited[rp - Rooms]; |
| 2930 | Facing_visited[rp - Rooms] = 0; |
| 2931 | |
| 2932 | MarkFacingFaces(rp - Rooms, mirror_dest_vecs); |
| 2933 | Facing_visited[rp - Rooms] = save_frame; |
| 2934 | // Render the mirror room |
| 2935 | rend_SetColorModel(CM_MONO); |
| 2936 | rend_SetLighting(LS_GOURAUD); |
| 2937 | rend_SetWrapType(WT_WRAP); |
| 2938 | RenderRoomUnsorted(rp); |
| 2939 | if (restore_index == false) { |
| 2940 | rp->wpb_index = -1; |
| 2941 | } else { |
| 2942 | for (i = 0; i < rp->num_verts; i++) |
| 2943 | World_point_buffer[rp->wpb_index + i] = mirror_save_points[i]; |
| 2944 | } |
| 2945 | fp = &rp->faces[0]; |
| 2946 | for (i = 0; i < rp->num_faces; i++, fp++) |
| 2947 | fp->flags = save_flags[i]; |
| 2948 | |
| 2949 | RenderRoomObjects(rp); |
| 2950 | rp->last_render_time = Gametime; |
| 2951 | g3_SetCustomClipPlane(0, NULL, NULL); |
| 2952 | } |
| 2953 | // Renders a specific room. If pos_offset is not NULL, adds that offset to each of the |
| 2954 | // rooms vertices |
| 2955 | void RenderRoom(room *rp) { |
| 2956 | Num_glows_this_frame = 0; |
| 2957 | |
| 2958 | // Set up rendering states |
| 2959 | rend_SetColorModel(CM_MONO); |
| 2960 | rend_SetLighting(LS_GOURAUD); |
| 2961 | rend_SetWrapType(WT_WRAP); |
| 2962 | if (rp->used == 0) { |
| 2963 | Int3(); // Trying to draw a room that isn't in use! |
| 2964 | return; |
| 2965 | } |
| 2966 | |
| 2967 | // Figure out pulse lighting for room |
| 2968 | ComputeRoomPulseLight(rp); |
| 2969 | |
| 2970 | // Mark it visible for automap |
| 2971 | AutomapVisMap[rp - Rooms] = 1; |
| 2972 | |
| 2973 | #ifdef EDITOR |
| 2974 | if (!UseHardware) { |
| 2975 | RenderRoomSorted(rp); |
| 2976 | } else |
| 2977 | #endif |
| 2978 | { |
| 2979 | // NOTE LINK TO ABOVE ELSE |
| 2980 | RenderRoomUnsorted(rp); |
| 2981 | } |
| 2982 | |
| 2983 | rp->last_render_time = Gametime; |
| 2984 | rp->flags &= ~RF_MIRROR_VISIBLE; |
| 2985 | |
| 2986 | CheckLightGlowsForRoom(rp); |
| 2987 | |
| 2988 | if (Num_scorches_to_render > 0) { |
| 2989 | RenderScorchesForRoom(rp); |
| 2990 | Num_scorches_to_render = 0; |
| 2991 | } |
| 2992 | |
| 2993 | if (Num_specular_faces_to_render > 0) { |
| 2994 | RenderSpecularFacesFlat(rp); |
| 2995 | Num_specular_faces_to_render = 0; |
| 2996 | Num_real_specular_faces_to_render = 0; |
| 2997 | } |
| 2998 | |
| 2999 | if (Num_fog_faces_to_render > 0) { |
| 3000 | RenderFogFaces(rp); |
| 3001 | Num_fog_faces_to_render = 0; |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | #define MAX_OBJECTS_PER_ROOM 2000 |
| 3006 | struct obj_sort_item { |
| 3007 | int vis_effect; |
| 3008 | int objnum; |
| 3009 | float dist; |
| 3010 | }; |
| 3011 | obj_sort_item obj_sort_list[MAX_OBJECTS_PER_ROOM]; |
| 3012 | // Compare function for room face sort |
| 3013 | static int obj_sort_func(const obj_sort_item *a, const obj_sort_item *b) { |
| 3014 | if (a->dist < b->dist) |
| 3015 | return -1; |
| 3016 | else if (a->dist > b->dist) |
| 3017 | return 1; |
| 3018 | else |
| 3019 | return 0; |
| 3020 | } |
| 3021 | static inline void IsRoomDynamicValid(room *rp, int x, int y, int z, float *r, float *g, float *b) { |
| 3022 | int w = rp->volume_width; |
| 3023 | int h = rp->volume_height; |
| 3024 | int d = rp->volume_depth; |
| 3025 | |
| 3026 | uint8_t color = rp->volume_lights[(z * w * h) + (y * w) + x]; |
| 3027 | |
| 3028 | *r = (float)((color >> 5) / 7.0); |
| 3029 | *g = (float)((color >> 2) & 0x07) / 7.0; |
| 3030 | *b = (float)((float)(color & 0x03) / 3.0); |
| 3031 | } |
| 3032 | // Gets the dynamic light value for this position |
| 3033 | void GetRoomDynamicScalar(vector *pos, room *rp, float *r, float *g, float *b) { |
| 3034 | float front_values_r[10]; |
| 3035 | float back_values_r[10]; |
| 3036 | float front_values_g[10]; |
| 3037 | float back_values_g[10]; |
| 3038 | float front_values_b[10]; |
| 3039 | float back_values_b[10]; |
| 3040 | if (!rp->volume_lights) { |
| 3041 | *r = 1; |
| 3042 | *g = 1; |
| 3043 | *b = 1; |
| 3044 | return; |
| 3045 | } |
| 3046 | float fl_x = (pos->x - rp->min_xyz.x) / VOLUME_SPACING; |
| 3047 | float fl_y = (pos->y - rp->min_xyz.y) / VOLUME_SPACING; |
| 3048 | float fl_z = (pos->z - rp->min_xyz.z) / VOLUME_SPACING; |
| 3049 | fl_x = std::max<float>(fl_x, 0); |
| 3050 | fl_y = std::max<float>(fl_y, 0); |
| 3051 | fl_z = std::max<float>(fl_z, 0); |
| 3052 | fl_x = std::min<float>(fl_x, rp->volume_width - 1); |
| 3053 | fl_y = std::min<float>(fl_y, rp->volume_height - 1); |
| 3054 | fl_z = std::min<float>(fl_z, rp->volume_depth - 1); |
| 3055 | int int_x = fl_x; |
| 3056 | int int_y = fl_y; |
| 3057 | int int_z = fl_z; |
| 3058 | fl_x -= int_x; |
| 3059 | fl_y -= int_y; |
| 3060 | fl_z -= int_z; |
| 3061 | int next_x = int_x + 1; |
| 3062 | int next_y = int_y + 1; |
| 3063 | int next_z = int_z + 1; |
| 3064 | next_x = std::min(rp->volume_width - 1, next_x); |
| 3065 | next_y = std::min(rp->volume_height - 1, next_y); |
| 3066 | next_z = std::min(rp->volume_depth - 1, next_z); |
| 3067 | |
| 3068 | float left_norm_r, left_norm_g, left_norm_b; |
| 3069 | float right_norm_r, right_norm_g, right_norm_b; |
| 3070 | float front_norm_r, front_norm_g, front_norm_b; |
| 3071 | float back_norm_r, back_norm_g, back_norm_b; |
| 3072 | |
| 3073 | IsRoomDynamicValid(rp, int_x, int_y, int_z, &front_values_r[0], &front_values_g[0], &front_values_b[0]); |
| 3074 | IsRoomDynamicValid(rp, int_x, next_y, int_z, &front_values_r[1], &front_values_g[1], &front_values_b[1]); |
| 3075 | IsRoomDynamicValid(rp, next_x, next_y, int_z, &front_values_r[2], &front_values_g[2], &front_values_b[2]); |
| 3076 | IsRoomDynamicValid(rp, next_x, int_y, int_z, &front_values_r[3], &front_values_g[3], &front_values_b[3]); |
| 3077 | IsRoomDynamicValid(rp, int_x, int_y, next_z, &back_values_r[0], &back_values_g[0], &back_values_b[0]); |
| 3078 | IsRoomDynamicValid(rp, int_x, next_y, next_z, &back_values_r[1], &back_values_g[1], &back_values_b[1]); |
| 3079 | IsRoomDynamicValid(rp, next_x, next_y, next_z, &back_values_r[2], &back_values_g[2], &back_values_b[2]); |
| 3080 | IsRoomDynamicValid(rp, next_x, int_y, next_z, &back_values_r[3], &back_values_g[3], &back_values_b[3]); |
| 3081 | // Do front edge |
| 3082 | int left_out = 0; |
| 3083 | int right_out = 0; |
| 3084 | // Left edge |
| 3085 | left_norm_r = ((1 - fl_y) * front_values_r[0]) + (fl_y * front_values_r[1]); |
| 3086 | left_norm_g = ((1 - fl_y) * front_values_g[0]) + (fl_y * front_values_g[1]); |
| 3087 | left_norm_b = ((1 - fl_y) * front_values_b[0]) + (fl_y * front_values_b[1]); |
| 3088 | |
| 3089 | // Right edge |
| 3090 | right_norm_r = ((1 - fl_y) * front_values_r[3]) + (fl_y * front_values_r[2]); |
| 3091 | right_norm_g = ((1 - fl_y) * front_values_g[3]) + (fl_y * front_values_g[2]); |
| 3092 | right_norm_b = ((1 - fl_y) * front_values_b[3]) + (fl_y * front_values_b[2]); |
| 3093 | // Figure out front edge |
| 3094 | front_norm_r = ((1 - fl_x) * left_norm_r) + (fl_x * right_norm_r); |
| 3095 | front_norm_g = ((1 - fl_x) * left_norm_g) + (fl_x * right_norm_g); |
| 3096 | front_norm_b = ((1 - fl_x) * left_norm_b) + (fl_x * right_norm_b); |
| 3097 | |
| 3098 | // Do back edge |
| 3099 | left_norm_r = ((1 - fl_y) * back_values_r[0]) + (fl_y * back_values_r[1]); |
| 3100 | left_norm_g = ((1 - fl_y) * back_values_g[0]) + (fl_y * back_values_g[1]); |
| 3101 | left_norm_b = ((1 - fl_y) * back_values_b[0]) + (fl_y * back_values_b[1]); |
| 3102 | |
| 3103 | right_norm_r = ((1 - fl_y) * back_values_r[3]) + (fl_y * back_values_r[2]); |
| 3104 | right_norm_g = ((1 - fl_y) * back_values_g[3]) + (fl_y * back_values_g[2]); |
| 3105 | right_norm_b = ((1 - fl_y) * back_values_b[3]) + (fl_y * back_values_b[2]); |
| 3106 | back_norm_r = ((1 - fl_x) * left_norm_r) + (fl_x * right_norm_r); |
| 3107 | back_norm_g = ((1 - fl_x) * left_norm_g) + (fl_x * right_norm_g); |
| 3108 | back_norm_b = ((1 - fl_x) * left_norm_b) + (fl_x * right_norm_b); |
| 3109 | *r = ((1 - fl_z) * front_norm_r) + (fl_z * back_norm_r); |
| 3110 | *g = ((1 - fl_z) * front_norm_g) + (fl_z * back_norm_g); |
| 3111 | *b = ((1 - fl_z) * front_norm_b) + (fl_z * back_norm_b); |
| 3112 | // Factor in flickering |
| 3113 | ComputeRoomPulseLight(rp); |
| 3114 | (*r) *= Room_light_val; |
| 3115 | (*g) *= Room_light_val; |
| 3116 | (*b) *= Room_light_val; |
| 3117 | } |
| 3118 | uint8_t Trick_type = 0; |
| 3119 | // Render the objects and viseffects in a room. Do a simple sort |
| 3120 | void RenderRoomObjects(room *rp) { |
| 3121 | int n_objs = 0, objnum, i, visnum; |
| 3122 | float zdist; |
| 3123 | if (!Render_mirror_for_room && UseHardware) |
| 3124 | return; // This function only works for mirrors now |
| 3125 | |
| 3126 | // Add objects to sort list |
| 3127 | for (objnum = rp->objects; (objnum != -1) && (n_objs < MAX_OBJECTS_PER_ROOM); objnum = Objects[objnum].next) { |
| 3128 | ASSERT(objnum != Objects[objnum].next); |
| 3129 | object *obj = &Objects[objnum]; |
| 3130 | |
| 3131 | if (obj->render_type == RT_NONE) |
| 3132 | continue; |
| 3133 | if (obj == Viewer_object && !Render_mirror_for_room) |
| 3134 | continue; |
| 3135 | float size = obj->size; |
| 3136 | // Special case weapons with streamers |
| 3137 | if (obj->type == OBJ_WEAPON && (Weapons[obj->id].flags & WF_STREAMER)) |
| 3138 | size = Weapons[obj->id].phys_info.velocity.z; |
| 3139 | // Check if object is trivially rejected |
| 3140 | bool isVisible = IsPointVisible(&obj->pos, size, &zdist) ? true : false; |
| 3141 | if (Render_mirror_for_room || (obj->type == OBJ_WEAPON && Weapons[obj->id].flags & WF_ELECTRICAL) || isVisible) { |
| 3142 | obj_sort_list[n_objs].vis_effect = 0; |
| 3143 | obj_sort_list[n_objs].objnum = objnum; |
| 3144 | obj_sort_list[n_objs].dist = zdist; |
| 3145 | n_objs++; |
| 3146 | } |
| 3147 | } |
| 3148 | // Add vis effects to sort list |
| 3149 | for (visnum = rp->vis_effects; (visnum != -1) && (n_objs < MAX_OBJECTS_PER_ROOM); visnum = VisEffects[visnum].next) { |
| 3150 | ASSERT(visnum != VisEffects[visnum].next); |
| 3151 | if (VisEffects[visnum].type == VIS_NONE || VisEffects[visnum].flags & VF_DEAD) |
| 3152 | continue; |
| 3153 | |
| 3154 | bool pointIsVisible = IsPointVisible(&VisEffects[visnum].pos, VisEffects[visnum].size, &zdist) ? true : false; |
| 3155 | if (Render_mirror_for_room || pointIsVisible) { |
| 3156 | obj_sort_list[n_objs].vis_effect = 1; |
| 3157 | obj_sort_list[n_objs].objnum = visnum; |
| 3158 | obj_sort_list[n_objs].dist = zdist; |
| 3159 | n_objs++; |
| 3160 | } |
| 3161 | } |
| 3162 | ASSERT(objnum == -1); // if not -1, ran out of space in render_order[] |
| 3163 | ASSERT(visnum == -1); // if not -1, ran out of space in render_order[] |
| 3164 | // Sort the objects |
| 3165 | qsort(obj_sort_list, n_objs, sizeof(*obj_sort_list), (int (*)(const void *, const void *))obj_sort_func); |
| 3166 | #ifdef _DEBUG |
| 3167 | bool save_polymodel_outline_mode = Polymodel_outline_mode; |
| 3168 | Polymodel_outline_mode = OUTLINE_ON(OM_OBJECTS); |
| 3169 | #endif |
| 3170 | // Render the objects |
| 3171 | if (UseHardware && Render_mirror_for_room) { |
| 3172 | // Render the mirror stuff if present |
| 3173 | room *mirror_rp = &Rooms[Mirror_room]; |
| 3174 | vector *mirror_vec = &mirror_rp->verts[mirror_rp->faces[mirror_rp->mirror_face].face_verts[0]]; |
| 3175 | face *mirror_fp = &mirror_rp->faces[mirror_rp->mirror_face]; |
| 3176 | vector *norm = &mirror_fp->normal; |
| 3177 | // This is how far the mirror face is from the normalized plane |
| 3178 | float mirror_dist = -(mirror_vec->x * norm->x + mirror_vec->y * norm->y + mirror_vec->z * norm->z); |
| 3179 | // Setup mirror matrix |
| 3180 | |
| 3181 | vector negz_vec = {0, 0, -1}; |
| 3182 | |
| 3183 | matrix mirror_matrix, inv_mirror_matrix, dest_matrix, negz_matrix; |
| 3184 | vm_VectorToMatrix(&mirror_matrix, norm, NULL, NULL); |
| 3185 | inv_mirror_matrix = mirror_matrix; |
| 3186 | vm_TransposeMatrix(&inv_mirror_matrix); |
| 3187 | vm_VectorToMatrix(&negz_matrix, &negz_vec, NULL, NULL); |
| 3188 | negz_matrix.rvec *= -1; |
| 3189 | |
| 3190 | for (i = n_objs - 1; i >= 0; i--) { |
| 3191 | objnum = obj_sort_list[i].objnum; |
| 3192 | if (obj_sort_list[i].vis_effect) { |
| 3193 | vis_effect *vis = &VisEffects[objnum]; |
| 3194 | vector save_vec = vis->pos; |
| 3195 | vector save_end_vec = vis->end_pos; |
| 3196 | vector *vec = &vis->pos; |
| 3197 | vector *end_vec = &vis->end_pos; |
| 3198 | float dist_from_mirror = vec->x * norm->x + vec->y * norm->y + vec->z * norm->z + mirror_dist; |
| 3199 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 3200 | vis->pos = *vec - (*norm * (dist_from_mirror * 2)); |
| 3201 | dist_from_mirror = end_vec->x * norm->x + end_vec->y * norm->y + end_vec->z * norm->z + mirror_dist; |
| 3202 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 3203 | vis->end_pos = *end_vec - (*norm * (dist_from_mirror * 2)); |
| 3204 | |
| 3205 | DrawVisEffect(vis); |
| 3206 | vis->pos = save_vec; |
| 3207 | vis->end_pos = save_end_vec; |
| 3208 | } else { |
| 3209 | object *objp = &Objects[objnum]; |
| 3210 | |
| 3211 | vector save_vec = objp->pos; |
| 3212 | matrix save_orient = objp->orient; |
| 3213 | matrix temp_mat; |
| 3214 | |
| 3215 | vector *vec = &objp->pos; |
| 3216 | float dist_from_mirror = vec->x * norm->x + vec->y * norm->y + vec->z * norm->z + mirror_dist; |
| 3217 | // dest_vecs contains the point on the other side of the mirror (ie the reflected point) |
| 3218 | objp->pos = *vec - (*norm * (dist_from_mirror * 2)); |
| 3219 | // Check for rear view |
| 3220 | if (objp == Viewer_object && Viewer_object == Player_object && |
| 3221 | (Players[Player_num].flags & PLAYER_FLAGS_REARVIEW)) { |
| 3222 | objp->orient.fvec = -objp->orient.fvec; |
| 3223 | objp->orient.rvec = -objp->orient.rvec; |
| 3224 | } |
| 3225 | // Get new orientation |
| 3226 | temp_mat = mirror_matrix * negz_matrix * inv_mirror_matrix; |
| 3227 | dest_matrix.rvec = objp->orient.rvec * temp_mat; |
| 3228 | dest_matrix.uvec = objp->orient.uvec * temp_mat; |
| 3229 | dest_matrix.fvec = objp->orient.fvec * temp_mat; |
| 3230 | objp->orient = dest_matrix; |
| 3231 | bool save_render = false; |
| 3232 | if (objp->flags & OF_SAFE_TO_RENDER) |
| 3233 | save_render = true; |
| 3234 | objp->flags |= OF_SAFE_TO_RENDER; |
| 3235 | |
| 3236 | RenderObject(objp); |
| 3237 | if (save_render) |
| 3238 | objp->flags |= OF_SAFE_TO_RENDER; |
| 3239 | else |
| 3240 | objp->flags &= ~OF_SAFE_TO_RENDER; |
| 3241 | objp->pos = save_vec; |
| 3242 | objp->orient = save_orient; |
| 3243 | } |
| 3244 | } |
| 3245 | return; |
| 3246 | } |
| 3247 | for (i = n_objs - 1; i >= 0; i--) { |
| 3248 | objnum = obj_sort_list[i].objnum; |
| 3249 | if (obj_sort_list[i].vis_effect) { |
| 3250 | DrawVisEffect(&VisEffects[objnum]); |
| 3251 | } else { |
| 3252 | object *objp = &Objects[objnum]; |
| 3253 | if (objp == Viewer_object) |
| 3254 | continue; |
| 3255 | RenderObject(objp); |
| 3256 | } |
| 3257 | } |
| 3258 | #ifdef _DEBUG |
| 3259 | Polymodel_outline_mode = save_polymodel_outline_mode; |
| 3260 | #endif |
| 3261 | } |
| 3262 | // Either renders the objects in a room, or stuffs them into our postrender list |
| 3263 | void CheckToRenderMineObjects(int roomnum) { |
| 3264 | |
| 3265 | if (UseHardware) { |
| 3266 | int index; |
| 3267 | float zdist; |
| 3268 | if (Render_mirror_for_room) |
| 3269 | return; |
| 3270 | for (index = Rooms[roomnum].objects; index != -1; index = Objects[index].next) { |
| 3271 | object *obj = &Objects[index]; |
| 3272 | if (Objects[index].render_type == RT_NONE) |
| 3273 | continue; |
| 3274 | if (obj == Viewer_object) |
| 3275 | continue; |
| 3276 | // Don't draw piggybacked objects |
| 3277 | if (Viewer_object->type == OBJ_OBSERVER && index == Players[Viewer_object->id].piggy_objnum) |
| 3278 | continue; |
| 3279 | float size = Objects[index].size; |
| 3280 | // Special case weapons with streamers |
| 3281 | if (Objects[index].type == OBJ_WEAPON && (Weapons[Objects[index].id].flags & WF_STREAMER)) |
| 3282 | size = Weapons[Objects[index].id].phys_info.velocity.z; |
| 3283 | // Check if object is trivially rejected |
| 3284 | int visible = IsPointVisible(&obj->pos, size, &zdist); // calculate zdist |
| 3285 | if ((obj->type == OBJ_WEAPON && Weapons[obj->id].flags & WF_ELECTRICAL) || visible) { |
| 3286 | if (Num_postrenders < MAX_POSTRENDERS) { |
| 3287 | Postrender_list[Num_postrenders].type = PRT_OBJECT; |
| 3288 | Postrender_list[Num_postrenders].z = zdist; |
| 3289 | Postrender_list[Num_postrenders++].objnum = index; |
| 3290 | } |
| 3291 | } |
| 3292 | } |
| 3293 | // Now do viseffects |
| 3294 | for (index = Rooms[roomnum].vis_effects; index != -1; index = VisEffects[index].next) { |
| 3295 | if (VisEffects[index].type == VIS_NONE || VisEffects[index].flags & VF_DEAD) |
| 3296 | continue; |
| 3297 | |
| 3298 | if (IsPointVisible(&VisEffects[index].pos, VisEffects[index].size, &zdist)) { |
| 3299 | if (Num_postrenders < MAX_POSTRENDERS) { |
| 3300 | Postrender_list[Num_postrenders].type = PRT_VISEFFECT; |
| 3301 | Postrender_list[Num_postrenders].z = zdist; |
| 3302 | Postrender_list[Num_postrenders++].visnum = index; |
| 3303 | } |
| 3304 | } |
| 3305 | } |
| 3306 | } else |
| 3307 | RenderRoomObjects(&Rooms[roomnum]); |
| 3308 | } |
| 3309 | // Renders all the mirrored rooms for this frame |
| 3310 | void RenderMirrorRooms() { |
| 3311 | int i; |
| 3312 | if (!UseHardware || !Detail_settings.Mirrored_surfaces) |
| 3313 | return; |
| 3314 | if (Num_mirror_rooms == 0) |
| 3315 | return; |
| 3316 | for (i = 0; i < Num_mirror_rooms; i++) { |
| 3317 | room *rp = &Rooms[Mirror_rooms[i]]; |
| 3318 | // Reset mirrors |
| 3319 | Render_mirror_for_room = false; |
| 3320 | bool do_mirror_face = false; |
| 3321 | rp->flags &= ~RF_MIRROR_VISIBLE; |
| 3322 | // Make sure its really ok to render this mirrored room |
| 3323 | if (rp->mirror_face != -1) |
| 3324 | do_mirror_face = true; |
| 3325 | if (rp->mirror_face >= rp->num_faces) |
| 3326 | do_mirror_face = false; |
| 3327 | if (do_mirror_face) { |
| 3328 | if (!(GameTextures[rp->faces[rp->mirror_face].tmap].flags & TF_ALPHA)) |
| 3329 | do_mirror_face = false; |
| 3330 | } |
| 3331 | if (do_mirror_face) { |
| 3332 | int on_screen = 0; |
| 3333 | // See if any of the faces that share the mirror texture are on the screen |
| 3334 | for (int k = 0; k < rp->num_mirror_faces && on_screen == 0; k++) { |
| 3335 | face *fp = &rp->faces[rp->mirror_faces_list[k]]; |
| 3336 | |
| 3337 | // See if this face is on screen |
| 3338 | int anded = 0xff; |
| 3339 | g3Point pnt; |
| 3340 | for (int t = 0; t < fp->num_verts; t++) |
| 3341 | anded &= g3_RotatePoint(&pnt, &rp->verts[fp->face_verts[t]]); |
| 3342 | if (!anded) |
| 3343 | on_screen = 1; |
| 3344 | } |
| 3345 | if (!on_screen) |
| 3346 | do_mirror_face = false; |
| 3347 | } |
| 3348 | if (do_mirror_face) // This room has a mirror...render it first |
| 3349 | { |
| 3350 | Render_mirror_for_room = true; |
| 3351 | Mirror_room = rp - Rooms; |
| 3352 | |
| 3353 | BuildMirroredRoomList(); |
| 3354 | for (int t = Num_mirrored_rooms - 1; t >= 0; t--) |
| 3355 | RenderMirroredRoom(&Rooms[Mirrored_room_list[t]]); |
| 3356 | Render_mirror_for_room = false; |
| 3357 | } |
| 3358 | } |
| 3359 | // Do z buffer trick |
| 3360 | rend_ClearZBuffer(); |
| 3361 | rend_SetZBufferWriteMask(1); |
| 3362 | rend_SetZBufferState(1); |
| 3363 | // Draw mirror faces now |
| 3364 | for (i = 0; i < Num_mirror_rooms; i++) { |
| 3365 | room *rp = &Rooms[Mirror_rooms[i]]; |
| 3366 | face *fp = &rp->faces[rp->mirror_face]; |
| 3367 | g3Point save_points[MAX_VERTS_PER_FACE]; |
| 3368 | int save_index; |
| 3369 | |
| 3370 | // Now do the same for all faces in this room that share that texture of the mirror |
| 3371 | for (int k = 0; k < rp->num_faces; k++) { |
| 3372 | face *this_fp = &rp->faces[k]; |
| 3373 | if (this_fp->tmap == fp->tmap) { |
| 3374 | int t; |
| 3375 | |
| 3376 | save_index = rp->wpb_index; |
| 3377 | for (t = 0; t < fp->num_verts; t++) |
| 3378 | save_points[t] = World_point_buffer[fp->face_verts[t]]; |
| 3379 | DrawPostrenderFace(Mirror_rooms[i], rp->mirror_face, false); |
| 3380 | |
| 3381 | rp->wpb_index = save_index; |
| 3382 | for (t = 0; t < fp->num_verts; t++) |
| 3383 | World_point_buffer[fp->face_verts[t]] = save_points[t]; |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | } |
| 3388 | // Renders a room in just outline form |
| 3389 | void RenderRoomOutline(room *rp) { |
| 3390 | int fn; |
| 3391 | ddgr_color back_line_color, face_line_color; |
| 3392 | back_line_color = GR_RGB(100, 100, 100); |
| 3393 | face_line_color = GR_RGB(255, 255, 255); |
| 3394 | for (fn = 0; fn < rp->num_faces; fn++) { |
| 3395 | face *fp = &rp->faces[fn]; |
| 3396 | g3Point p0, p1; |
| 3397 | uint8_t c0, c1; |
| 3398 | int v; |
| 3399 | ddgr_color color; |
| 3400 | for (v = 0; v < fp->num_verts; v++) { |
| 3401 | c0 = g3_RotatePoint(&p0, &rp->verts[fp->face_verts[v]]); |
| 3402 | c1 = g3_RotatePoint(&p1, &rp->verts[fp->face_verts[(v + 1) % fp->num_verts]]); |
| 3403 | if ((!(fp->flags & FF_VISIBLE)) || ((fp->flags & FF_NOT_FACING))) { |
| 3404 | // wouldn't normally be rendered |
| 3405 | color = back_line_color; |
| 3406 | } else { |
| 3407 | color = face_line_color; |
| 3408 | } |
| 3409 | if (!(c0 & c1)) { // both not off screen? |
| 3410 | // Draw current edge in green |
| 3411 | g3_DrawLine(color, &p0, &p1); |
| 3412 | } |
| 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | // Draws the mine, starting at a the specified room |
| 3417 | // The rendering surface must be set up, and g3_StartFrame() must have been called |
| 3418 | // Parameters: viewer_roomnum - what room the viewer is in |
| 3419 | // flag_automap - if true, flag segments as visited when rendered |
| 3420 | // called_from_terrain - set if calling this routine from the terrain renderer |
| 3421 | void RenderMine(int viewer_roomnum, int flag_automap, int called_from_terrain) { |
| 3422 | #ifdef EDITOR |
| 3423 | In_editor_mode = (GetFunctionMode() == EDITOR_MODE); |
| 3424 | #endif |
| 3425 | // check to see if we should render windows |
| 3426 | if (No_render_windows_hack == -1) { |
| 3427 | if (FindArg("-NoRenderWindows")) |
| 3428 | No_render_windows_hack = 1; |
| 3429 | else |
| 3430 | No_render_windows_hack = 0; |
| 3431 | } |
| 3432 | // Get the viewer eye so functions down the line can look at it |
| 3433 | g3_GetViewPosition(&Viewer_eye); |
| 3434 | g3_GetUnscaledMatrix(&Viewer_orient); |
| 3435 | // set these globals so functions down the line can look at them |
| 3436 | Viewer_roomnum = viewer_roomnum; |
| 3437 | Flag_automap = flag_automap; |
| 3438 | Called_from_terrain = called_from_terrain; |
| 3439 | // Assume no terrain |
| 3440 | Must_render_terrain = 0; |
| 3441 | |
| 3442 | // Get the width & height of the render window |
| 3443 | rend_GetProjectionParameters(&Render_width, &Render_height); |
| 3444 | if (!Called_from_terrain) { |
| 3445 | Terrain_portal_top = Render_height; |
| 3446 | Terrain_portal_bottom = 0; |
| 3447 | Terrain_portal_right = 0; |
| 3448 | Terrain_portal_left = Render_width; |
| 3449 | } |
| 3450 | // Build the list of visible rooms |
| 3451 | BuildRoomList(viewer_roomnum); // fills in Render_list & N_render_segs |
| 3452 | |
| 3453 | // If we determined that the terrain is visible, render it |
| 3454 | if (Must_render_terrain && !Called_from_terrain && !(In_editor_mode && Render_inside_only)) { |
| 3455 | RenderTerrain(1, Terrain_portal_left, Terrain_portal_top, Terrain_portal_right, Terrain_portal_bottom); |
| 3456 | // Mark all room points to be rerotated due to terrain trashing our point list |
| 3457 | for (int i = 0; i <= Highest_room_index; i++) { |
| 3458 | Rooms[i].wpb_index = -1; |
| 3459 | Global_buffer_index = 0; |
| 3460 | } |
| 3461 | // Setup fog if needed |
| 3462 | g3_SetFarClipZ(VisibleTerrainZ); |
| 3463 | |
| 3464 | if ((Terrain_sky.flags & TF_FOG) && (UseHardware || (!UseHardware && Lighting_on))) { |
| 3465 | rend_SetZValues(0, VisibleTerrainZ); |
| 3466 | rend_SetFogState(1); |
| 3467 | rend_SetFogBorders(VisibleTerrainZ * .85, VisibleTerrainZ); |
| 3468 | rend_SetFogColor(Terrain_sky.fog_color); |
| 3469 | } else |
| 3470 | rend_SetZValues(0, 5000); |
| 3471 | } |
| 3472 | |
| 3473 | // First render mirrored rooms |
| 3474 | RenderMirrorRooms(); |
| 3475 | |
| 3476 | Num_mirror_rooms = 0; |
| 3477 | |
| 3478 | // Render the list of rooms |
| 3479 | for (int nn = N_render_rooms - 1; nn >= 0; nn--) { |
| 3480 | int roomnum; |
| 3481 | roomnum = Render_list[nn]; |
| 3482 | #ifdef _DEBUG |
| 3483 | if (In_editor_mode && Render_one_room_only && (roomnum != viewer_roomnum)) |
| 3484 | continue; |
| 3485 | #endif |
| 3486 | if (roomnum != -1) { |
| 3487 | ASSERT(Rooms_visited[roomnum] != 255); |
| 3488 | if (Outline_release_mode & 1) { |
| 3489 | RenderRoomOutline(&Rooms[roomnum]); |
| 3490 | } |
| 3491 | RenderRoom(&Rooms[roomnum]); |
| 3492 | Rooms_visited[roomnum] = (char)255; |
| 3493 | // Stuff objects into our postrender list |
| 3494 | CheckToRenderMineObjects(roomnum); |
| 3495 | } |
| 3496 | } |
| 3497 | rend_SetOverlayType(OT_NONE); // turn off lightmap blending |
| 3498 | if (Must_render_terrain && !Called_from_terrain) |
| 3499 | rend_SetFogState(0); |
| 3500 | |
| 3501 | #ifdef EDITOR |
| 3502 | if (OUTLINE_ON(OM_MINE)) { |
| 3503 | OutlineCurrentFace(Curroomp, Curface, Curedge, Curvert, CURFACE_COLOR, CUREDGE_COLOR); |
| 3504 | if (Markedroomp) |
| 3505 | OutlineCurrentFace(Markedroomp, Markedface, Markededge, Markedvert, MARKEDFACE_COLOR, MARKEDEDGE_COLOR); |
| 3506 | if (Placed_room != -1) |
| 3507 | DrawPlacedRoomFace(&Rooms[Placed_room], &Placed_room_origin, &Placed_room_rotmat, &Placed_room_attachpoint, |
| 3508 | Placed_room_face, PLACED_COLOR); |
| 3509 | } |
| 3510 | #endif |
| 3511 | } |
| 3512 | // Simply sets the number of glows to zero |
| 3513 | void ResetLightGlows() { |
| 3514 | Num_glows = 0; |
| 3515 | for (int i = 0; i < MAX_LIGHT_GLOWS; i++) { |
| 3516 | LightGlows[i].flags = 0; |
| 3517 | } |
| 3518 | } |
| 3519 | // Renders all the lights glows for this frame |
| 3520 | void RenderLightGlows() { |
| 3521 | if (!UseHardware) |
| 3522 | return; |
| 3523 | // Render all the glows for this mine |
| 3524 | rend_SetColorModel(CM_RGB); |
| 3525 | rend_SetLighting(LS_GOURAUD); |
| 3526 | rend_SetTextureType(TT_LINEAR); |
| 3527 | rend_SetAlphaType(AT_SATURATE_TEXTURE); |
| 3528 | rend_SetOverlayType(OT_NONE); |
| 3529 | rend_SetFogState(0); |
| 3530 | int count = 0; |
| 3531 | |
| 3532 | for (int i = 0; i < MAX_LIGHT_GLOWS && count < Num_glows; i++) { |
| 3533 | if (LightGlows[i].flags & LGF_USED) { |
| 3534 | RenderSingleLightGlow(i); |
| 3535 | count++; |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | rend_SetZBufferWriteMask(1); |
| 3540 | rend_SetZBufferState(1); |
| 3541 | } |
| 3542 | |
| 3543 | /* |
| 3544 | // Sets fogzone start and end points |
| 3545 | void SetFogZoneStart(float z) |
| 3546 | { |
| 3547 | Fog_zone_start=z; |
| 3548 | } |
| 3549 | void SetFogZoneEnd (float z) |
| 3550 | { |
| 3551 | Fog_zone_end=z; |
| 3552 | } |
| 3553 | g3Point SolidFogPoints[128],AlphaFogPoints[128]; |
| 3554 | // Adds a point to the fog zone face |
| 3555 | void FogClipPoints (g3Point *on_pnt,g3Point *off_pnt,g3Point *dest,float zval,int ending) |
| 3556 | { |
| 3557 | float z_on,z_off; |
| 3558 | float k; |
| 3559 | g3Point *tmp=dest; |
| 3560 | tmp->p3_flags=0; |
| 3561 | z_on=on_pnt->p3_z; |
| 3562 | z_off=off_pnt->p3_z; |
| 3563 | k = 1.0-((z_off-zval) / (z_off-z_on)); |
| 3564 | tmp->p3_z = on_pnt->p3_z + ((off_pnt->p3_z-on_pnt->p3_z) * k); |
| 3565 | |
| 3566 | tmp->p3_x = on_pnt->p3_x + ((off_pnt->p3_x-on_pnt->p3_x) * k); |
| 3567 | tmp->p3_y = on_pnt->p3_y + ((off_pnt->p3_y-on_pnt->p3_y) * k); |
| 3568 | if (on_pnt->p3_flags & PF_UV) { |
| 3569 | tmp->p3_u = on_pnt->p3_u + ((off_pnt->p3_u-on_pnt->p3_u) * k); |
| 3570 | tmp->p3_v = on_pnt->p3_v + ((off_pnt->p3_v-on_pnt->p3_v) * k); |
| 3571 | tmp->p3_flags |= PF_UV; |
| 3572 | } |
| 3573 | if (on_pnt->p3_flags & PF_UV2) { |
| 3574 | tmp->p3_u2 = on_pnt->p3_u2 + ((off_pnt->p3_u2-on_pnt->p3_u2) * k); |
| 3575 | tmp->p3_v2 = on_pnt->p3_v2 + ((off_pnt->p3_v2-on_pnt->p3_v2) * k); |
| 3576 | tmp->p3_flags |= PF_UV2; |
| 3577 | } |
| 3578 | if (on_pnt->p3_flags & PF_L) { |
| 3579 | tmp->p3_l = on_pnt->p3_l + ((off_pnt->p3_l-on_pnt->p3_l) * k); |
| 3580 | tmp->p3_flags |= PF_L; |
| 3581 | } |
| 3582 | if (on_pnt->p3_flags & PF_RGBA) { |
| 3583 | tmp->p3_r = on_pnt->p3_r + ((off_pnt->p3_r-on_pnt->p3_r) * k); |
| 3584 | tmp->p3_g = on_pnt->p3_g + ((off_pnt->p3_g-on_pnt->p3_g) * k); |
| 3585 | tmp->p3_b = on_pnt->p3_b + ((off_pnt->p3_b-on_pnt->p3_b) * k); |
| 3586 | tmp->p3_flags |= PF_RGBA; |
| 3587 | } |
| 3588 | if (ending) |
| 3589 | tmp->p3_a = 0.0; |
| 3590 | else |
| 3591 | tmp->p3_a = 1.0; |
| 3592 | g3_CodePoint(tmp); |
| 3593 | } |
| 3594 | */ |
| 3595 | |
| 3596 | #define STATE_PUSH(val) \ |
| 3597 | { \ |
| 3598 | state_stack[state_stack_counter] = val; \ |
| 3599 | state_stack_counter++; \ |
| 3600 | ASSERT(state_stack_counter < 2000); \ |
| 3601 | } |
| 3602 | #define STATE_POP() \ |
| 3603 | { \ |
| 3604 | state_stack_counter--; \ |
| 3605 | pop_val = state_stack[state_stack_counter]; \ |
| 3606 | } |
| 3607 | // Sorts our texture states using the quicksort algorithm |
| 3608 | void SortStates(state_limited_element *state_array, int cellcount) { |
| 3609 | state_limited_element v, t; |
| 3610 | int pop_val; |
| 3611 | int i, j; |
| 3612 | int l, r; |
| 3613 | l = 0; |
| 3614 | r = cellcount - 1; |
| 3615 | uint16_t state_stack_counter = 0; |
| 3616 | uint16_t state_stack[2000]; |
| 3617 | |
| 3618 | while (1) { |
| 3619 | while (r > l) { |
| 3620 | i = l - 1; |
| 3621 | j = r; |
| 3622 | v = state_array[r]; |
| 3623 | while (1) { |
| 3624 | while (state_array[++i].sort_key < v.sort_key) |
| 3625 | ; |
| 3626 | while (state_array[--j].sort_key > v.sort_key) |
| 3627 | ; |
| 3628 | if (i >= j) |
| 3629 | break; |
| 3630 | t = state_array[i]; |
| 3631 | state_array[i] = state_array[j]; |
| 3632 | state_array[j] = t; |
| 3633 | } |
| 3634 | t = state_array[i]; |
| 3635 | state_array[i] = state_array[r]; |
| 3636 | state_array[r] = t; |
| 3637 | |
| 3638 | if (i - l > r - i) { |
| 3639 | STATE_PUSH(l); |
| 3640 | STATE_PUSH(i - 1); |
| 3641 | l = i + 1; |
| 3642 | } else { |
| 3643 | STATE_PUSH(i + 1); |
| 3644 | STATE_PUSH(r); |
| 3645 | r = i - 1; |
| 3646 | } |
| 3647 | } |
| 3648 | if (!state_stack_counter) |
| 3649 | break; |
| 3650 | STATE_POP(); |
| 3651 | r = pop_val; |
| 3652 | STATE_POP(); |
| 3653 | l = pop_val; |
| 3654 | } |
| 3655 | } |
| 3656 | // Builds a list of mirror faces for each room and allocs memory accordingly |
| 3657 | void ConsolidateMineMirrors() { |
| 3658 | int i, t; |
| 3659 | mprintf(0, "Consolidating mine mirrors!\n"); |
| 3660 | for (i = 0; i < MAX_ROOMS; i++) { |
| 3661 | room *rp = &Rooms[i]; |
| 3662 | if (!rp->used) |
| 3663 | continue; |
| 3664 | if (rp->mirror_faces_list) { |
| 3665 | mem_free(rp->mirror_faces_list); |
| 3666 | rp->mirror_faces_list = NULL; |
| 3667 | rp->num_mirror_faces = 0; |
| 3668 | } |
| 3669 | if (rp->mirror_face == -1) |
| 3670 | continue; |
| 3671 | // Count the number of faces that have the same texture as the mirror face |
| 3672 | int num_mirror_faces = 0; |
| 3673 | for (t = 0; t < rp->num_faces; t++) { |
| 3674 | face *fp = &rp->faces[t]; |
| 3675 | if (fp->tmap == rp->faces[rp->mirror_face].tmap) |
| 3676 | num_mirror_faces++; |
| 3677 | } |
| 3678 | if (num_mirror_faces == 0) { |
| 3679 | // No faces found? Weird. |
| 3680 | rp->mirror_face = 0; |
| 3681 | continue; |
| 3682 | } |
| 3683 | rp->mirror_faces_list = (uint16_t *)mem_malloc(num_mirror_faces * sizeof(uint16_t)); |
| 3684 | ASSERT(rp->mirror_faces_list); |
| 3685 | rp->num_mirror_faces = num_mirror_faces; |
| 3686 | // Now go through and fill in our list |
| 3687 | int count = 0; |
| 3688 | for (t = 0; t < rp->num_faces; t++) { |
| 3689 | face *fp = &rp->faces[t]; |
| 3690 | if (fp->tmap == rp->faces[rp->mirror_face].tmap) |
| 3691 | rp->mirror_faces_list[count++] = t; |
| 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | /* |
| 3697 | // Takes a face and adds the appropriate vertices for drawing in the fog zone |
| 3698 | // Returns number of points in new polygon |
| 3699 | // New polygon points are in FogPoints array |
| 3700 | int FogBlendFace (g3Point **src,int nv,int *num_solid,int *num_alpha) |
| 3701 | { |
| 3702 | int alpha_nv=0,solid_nv=0,temp_nv=0; |
| 3703 | g3Point temp_buf[128],temp_pnt; |
| 3704 | float fog_zone_range=Fog_zone_end-Fog_zone_start; |
| 3705 | int i; |
| 3706 | |
| 3707 | for (i=0;i<nv;i++) |
| 3708 | { |
| 3709 | int next=(i+1)%nv; |
| 3710 | int prev=i-1; |
| 3711 | if (prev<0) |
| 3712 | prev=nv-1; |
| 3713 | g3Point *thispnt=src[i]; |
| 3714 | g3Point *nextpnt=src[next]; |
| 3715 | g3Point *prevpnt=src[prev]; |
| 3716 | if (thispnt->p3_z>Fog_zone_end) |
| 3717 | { |
| 3718 | if (prevpnt->p3_z<Fog_zone_end) |
| 3719 | { |
| 3720 | FogClipPoints (prevpnt,thispnt,&temp_buf[temp_nv],Fog_zone_end,1); |
| 3721 | temp_nv++; |
| 3722 | } |
| 3723 | if (nextpnt->p3_z<Fog_zone_end) |
| 3724 | { |
| 3725 | FogClipPoints (nextpnt,thispnt,&temp_buf[temp_nv],Fog_zone_end,1); |
| 3726 | temp_nv++; |
| 3727 | } |
| 3728 | } |
| 3729 | else |
| 3730 | { |
| 3731 | temp_buf[temp_nv]=*thispnt; |
| 3732 | temp_buf[temp_nv].p3_a=0; |
| 3733 | temp_nv++; |
| 3734 | } |
| 3735 | } |
| 3736 | // At this stage all vertices are clipped to be nearer that Fog_zone_end |
| 3737 | nv=temp_nv; |
| 3738 | for (i=0;i<nv;i++) |
| 3739 | { |
| 3740 | int next=(i+1)%nv; |
| 3741 | int prev=i-1; |
| 3742 | if (prev<0) |
| 3743 | prev=nv-1; |
| 3744 | g3Point *thispnt=&temp_buf[i]; |
| 3745 | g3Point *nextpnt=&temp_buf[next]; |
| 3746 | g3Point *prevpnt=&temp_buf[prev]; |
| 3747 | if (thispnt->p3_z>Fog_zone_start) |
| 3748 | { |
| 3749 | if (prevpnt->p3_z<Fog_zone_start) |
| 3750 | { |
| 3751 | FogClipPoints (prevpnt,thispnt,&temp_pnt,Fog_zone_start,0); |
| 3752 | SolidFogPoints[solid_nv]=temp_pnt; |
| 3753 | solid_nv++; |
| 3754 | AlphaFogPoints[alpha_nv]=temp_pnt; |
| 3755 | alpha_nv++; |
| 3756 | } |
| 3757 | // Add this point to the list |
| 3758 | //--------------------------- |
| 3759 | float alpha=1.0-((thispnt->p3_z-Fog_zone_start)/fog_zone_range); |
| 3760 | if (alpha<0) |
| 3761 | alpha=0; |
| 3762 | ASSERT (alpha<=1.0); |
| 3763 | AlphaFogPoints[alpha_nv]=*thispnt; |
| 3764 | AlphaFogPoints[alpha_nv].p3_a=alpha; |
| 3765 | alpha_nv++; |
| 3766 | //------------------------------ |
| 3767 | if (nextpnt->p3_z<Fog_zone_start) |
| 3768 | { |
| 3769 | FogClipPoints (nextpnt,thispnt,&temp_pnt,Fog_zone_start,0); |
| 3770 | SolidFogPoints[solid_nv]=temp_pnt; |
| 3771 | solid_nv++; |
| 3772 | AlphaFogPoints[alpha_nv]=temp_pnt; |
| 3773 | alpha_nv++; |
| 3774 | |
| 3775 | } |
| 3776 | } |
| 3777 | else |
| 3778 | { |
| 3779 | SolidFogPoints[solid_nv]=*thispnt; |
| 3780 | SolidFogPoints[solid_nv].p3_a=1.0; |
| 3781 | solid_nv++; |
| 3782 | } |
| 3783 | } |
| 3784 | *num_alpha=alpha_nv; |
| 3785 | *num_solid=solid_nv; |
| 3786 | return (solid_nv>0); |
| 3787 | } |
| 3788 | */ |
| 3789 | |
| 3790 | // RenderBlankScreen |
| 3791 | // Renders a blank screen, to be used for UI callbacks to prevent Hall of mirrors with mouse cursor |
| 3792 | void RenderBlankScreen(void) { rend_ClearScreen(GR_BLACK); } |
| 3793 | #ifdef EDITOR |
| 3794 | // Finds what room & face is visible at a given screen x & y |
| 3795 | // Everything must be set up just like for RenderMineRoom(), and presumably is the same as |
| 3796 | // for the last frame rendered (though it doesn't have to be) |
| 3797 | // Parameters: x,y - the screen coordinates |
| 3798 | // start_roomnum - where to start rendering |
| 3799 | // roomnum,facenum - these are filled in with the found values |
| 3800 | // if room<0, then an object was found, and the object number is -room-1 |
| 3801 | // Returns: 1 if found a room, else 0 |
| 3802 | /*int FindRoomFace(int16_t x,int16_t y,int start_roomnum,int *roomnum,int *facenum) |
| 3803 | { |
| 3804 | //Init search mode |
| 3805 | search_mode = -1; |
| 3806 | search_x = x; search_y = y; |
| 3807 | found_room = INT_MAX; |
| 3808 | //Render and search |
| 3809 | RenderMine(start_roomnum,0,0); |
| 3810 | //Turn search off |
| 3811 | search_mode = 0; |
| 3812 | //Set return values |
| 3813 | *roomnum = found_room; |
| 3814 | *facenum = found_face; |
| 3815 | return (found_room != INT_MAX); |
| 3816 | }*/ |
| 3817 | // finds what room,face,lumel is visible at a given screen x & y |
| 3818 | // Everything must be set up just like for RenderMineRoom(), and presumably is the same as |
| 3819 | // for the last frame rendered (though it doesn't have to be) |
| 3820 | // Parameters: x,y - the screen coordinates |
| 3821 | // start_roomnum - where to start rendering |
| 3822 | // roomnum,facenum,lumel_num - these are filled in with the found values |
| 3823 | // Returns: 1 if found a room, else 0 |
| 3824 | /*int FindLightmapFace(int16_t x,int16_t y,int start_roomnum,int *roomnum,int *facenum,int *lumel_num) |
| 3825 | { |
| 3826 | Search_lightmaps=1; |
| 3827 | search_x = x; search_y = y; |
| 3828 | found_room = INT_MAX; |
| 3829 | //Get the width & height of the render window |
| 3830 | rend_GetProjectionParameters(&Render_width,&Render_height); |
| 3831 | RenderMine(start_roomnum,0,0); |
| 3832 | Search_lightmaps=0; |
| 3833 | *roomnum = found_room; |
| 3834 | *facenum = found_face; |
| 3835 | *lumel_num=found_lightmap; |
| 3836 | return (found_room != INT_MAX); |
| 3837 | }*/ |
| 3838 | #endif |