| 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/postrender.cpp $ |
| 21 | * $Revision: 15 $ |
| 22 | * $Date: 4/19/00 5:16p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * program state info |
| 26 | * |
| 27 | * $Log: /DescentIII/main/postrender.cpp $ |
| 28 | * |
| 29 | * 15 4/19/00 5:16p Matt |
| 30 | * From Duane for 1.4 |
| 31 | * Removed Mac OpenGL hack |
| 32 | * |
| 33 | * 14 10/21/99 9:28p Jeff |
| 34 | * B.A. Macintosh code merge |
| 35 | * |
| 36 | * 13 6/08/99 12:12p Jeff |
| 37 | * fixed warning messages when compiling new editor |
| 38 | * |
| 39 | * 12 5/12/99 4:55p Gwar |
| 40 | * added an include for the NEWEDITOR |
| 41 | * |
| 42 | * 11 3/22/99 5:51p Jason |
| 43 | * enhancements to mirrors |
| 44 | * |
| 45 | * 10 2/17/99 1:59p Jason |
| 46 | * fixed some postrender issues |
| 47 | * |
| 48 | * 9 2/16/99 5:25p Matt |
| 49 | * zero out Num_postrenders after rendering |
| 50 | * |
| 51 | * 8 2/10/99 1:34p Dan |
| 52 | * fixed another assert |
| 53 | * |
| 54 | * 7 2/10/99 11:41a Dan |
| 55 | * fixed assertion |
| 56 | * |
| 57 | * 6 2/09/99 1:12p Jason |
| 58 | * fixed sorting problem with postrender |
| 59 | * |
| 60 | * 5 2/09/99 12:59p Jason |
| 61 | * spedup indoor renderer |
| 62 | * |
| 63 | * 4 2/09/99 12:09p Jason |
| 64 | * rewriting indoor engine... |
| 65 | * |
| 66 | * 3 2/09/99 9:59a Chris |
| 67 | * Quick compile patch |
| 68 | * |
| 69 | * 2 2/08/99 6:39p Jason |
| 70 | * first pass at new indoor engine |
| 71 | * |
| 72 | * $NoKeywords: $ |
| 73 | */ |
| 74 | |
| 75 | #ifdef NEWEDITOR // include first to get rid of ugly warning message about macro redfinitions |
| 76 | #include "..\neweditor\globals.h" |
| 77 | #endif |
| 78 | |
| 79 | #include <stdlib.h> |
| 80 | #include "object.h" |
| 81 | #include "viseffect.h" |
| 82 | #include "render.h" |
| 83 | #include "renderobject.h" |
| 84 | #include "room.h" |
| 85 | #include "postrender.h" |
| 86 | #include "config.h" |
| 87 | #include "terrain.h" |
| 88 | #include "renderer.h" |
| 89 | |
| 90 | postrender_struct Postrender_list[MAX_POSTRENDERS]; |
| 91 | int Num_postrenders = 0; |
| 92 | |
| 93 | static vector Viewer_eye; |
| 94 | static matrix Viewer_orient; |
| 95 | static int Viewer_roomnum; |
| 96 | |
| 97 | // Resets out postrender list for a new frame |
| 98 | void ResetPostrenderList() { Num_postrenders = 0; } |
| 99 | |
| 100 | // Compare function for room face sort |
| 101 | static int Postrender_sort_func(const postrender_struct *a, const postrender_struct *b) { |
| 102 | if (a->z < b->z) |
| 103 | return -1; |
| 104 | else if (a->z > b->z) |
| 105 | return 1; |
| 106 | else |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | #define STATE_PUSH(val) \ |
| 111 | { \ |
| 112 | state_stack[state_stack_counter] = val; \ |
| 113 | state_stack_counter++; \ |
| 114 | ASSERT(state_stack_counter < 2000); \ |
| 115 | } |
| 116 | #define STATE_POP() \ |
| 117 | { \ |
| 118 | state_stack_counter--; \ |
| 119 | pop_val = state_stack[state_stack_counter]; \ |
| 120 | } |
| 121 | // Sorts our texture states using the quicksort algorithm |
| 122 | void SortPostrenders() { |
| 123 | postrender_struct v, t; |
| 124 | int pop_val; |
| 125 | int i, j; |
| 126 | int l, r; |
| 127 | l = 0; |
| 128 | r = Num_postrenders - 1; |
| 129 | |
| 130 | uint16_t state_stack_counter = 0; |
| 131 | uint16_t state_stack[MAX_POSTRENDERS]; |
| 132 | |
| 133 | while (1) { |
| 134 | while (r > l) { |
| 135 | i = l - 1; |
| 136 | j = r; |
| 137 | v = Postrender_list[r]; |
| 138 | while (1) { |
| 139 | while (Postrender_list[++i].z < v.z) |
| 140 | ; |
| 141 | |
| 142 | while (Postrender_list[--j].z > v.z) |
| 143 | ; |
| 144 | |
| 145 | if (i >= j) |
| 146 | break; |
| 147 | |
| 148 | t = Postrender_list[i]; |
| 149 | Postrender_list[i] = Postrender_list[j]; |
| 150 | Postrender_list[j] = t; |
| 151 | } |
| 152 | |
| 153 | t = Postrender_list[i]; |
| 154 | Postrender_list[i] = Postrender_list[r]; |
| 155 | Postrender_list[r] = t; |
| 156 | |
| 157 | if (i - l > r - i) { |
| 158 | STATE_PUSH(l); |
| 159 | STATE_PUSH(i - 1); |
| 160 | l = i + 1; |
| 161 | } else { |
| 162 | STATE_PUSH(i + 1); |
| 163 | STATE_PUSH(r); |
| 164 | r = i - 1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!state_stack_counter) |
| 169 | break; |
| 170 | STATE_POP(); |
| 171 | r = pop_val; |
| 172 | STATE_POP(); |
| 173 | l = pop_val; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void SetupPostrenderRoom(room *rp) { |
| 178 | // Setup faces if this is a fogged room |
| 179 | if (rp->flags & RF_FOG) |
| 180 | SetupRoomFog(rp, &Viewer_eye, &Viewer_orient, Viewer_roomnum); |
| 181 | } |
| 182 | |
| 183 | // Rotates a face, and then renders it |
| 184 | void DrawPostrenderFace(int roomnum, int facenum, bool change_z) { |
| 185 | int i; |
| 186 | |
| 187 | // Always draw as non state limited |
| 188 | bool save_state = StateLimited; |
| 189 | StateLimited = false; |
| 190 | |
| 191 | ASSERT(roomnum >= 0 && roomnum < (MAX_ROOMS + MAX_PALETTE_ROOMS)); |
| 192 | ASSERT(Rooms[roomnum].used); |
| 193 | |
| 194 | room *rp = &Rooms[roomnum]; |
| 195 | |
| 196 | ASSERT(facenum >= 0 && facenum < rp->num_faces); |
| 197 | |
| 198 | face *fp = &rp->faces[facenum]; |
| 199 | |
| 200 | // Rotate points |
| 201 | rp->wpb_index = 0; |
| 202 | for (i = 0; i < fp->num_verts; i++) { |
| 203 | g3_RotatePoint(&World_point_buffer[fp->face_verts[i]], &rp->verts[fp->face_verts[i]]); |
| 204 | g3_ProjectPoint(&World_point_buffer[fp->face_verts[i]]); |
| 205 | } |
| 206 | |
| 207 | SetupPostrenderRoom(rp); |
| 208 | |
| 209 | // Render! |
| 210 | if (change_z) |
| 211 | rend_SetZBufferWriteMask(0); |
| 212 | RenderFace(rp, facenum); |
| 213 | |
| 214 | // Render any effects for this face |
| 215 | if (Num_specular_faces_to_render > 0) { |
| 216 | RenderSpecularFacesFlat(rp); |
| 217 | Num_specular_faces_to_render = 0; |
| 218 | } |
| 219 | |
| 220 | if (Num_fog_faces_to_render > 0) { |
| 221 | RenderFogFaces(rp); |
| 222 | Num_fog_faces_to_render = 0; |
| 223 | } |
| 224 | |
| 225 | // Restore statelimited setting |
| 226 | StateLimited = save_state; |
| 227 | |
| 228 | if (change_z) |
| 229 | rend_SetZBufferWriteMask(1); |
| 230 | } |
| 231 | |
| 232 | // Renders all the objects/viseffects/walls we have in our postrender list |
| 233 | void PostRender(int roomnum) { |
| 234 | g3_GetViewPosition(&Viewer_eye); |
| 235 | g3_GetUnscaledMatrix(&Viewer_orient); |
| 236 | |
| 237 | Viewer_roomnum = roomnum; |
| 238 | |
| 239 | int i, index; |
| 240 | // Sort the objects |
| 241 | SortPostrenders(); |
| 242 | // qsort(Postrender_list,Num_postrenders,sizeof(*Postrender_list),(int (cdecl *)(const void*,const |
| 243 | // void*))Postrender_sort_func); |
| 244 | |
| 245 | for (i = Num_postrenders - 1; i >= 0; i--) { |
| 246 | |
| 247 | if (Postrender_list[i].type == PRT_VISEFFECT) { |
| 248 | index = Postrender_list[i].visnum; |
| 249 | DrawVisEffect(&VisEffects[index]); |
| 250 | } else if (Postrender_list[i].type == PRT_OBJECT) { |
| 251 | object *objp = &Objects[Postrender_list[i].objnum]; |
| 252 | |
| 253 | if (!OBJECT_OUTSIDE(&Objects[Postrender_list[i].objnum])) |
| 254 | SetupPostrenderRoom(&Rooms[Objects[Postrender_list[i].objnum].roomnum]); |
| 255 | RenderObject(objp); |
| 256 | } else { |
| 257 | // Do room face |
| 258 | DrawPostrenderFace(Postrender_list[i].roomnum, Postrender_list[i].facenum); |
| 259 | } |
| 260 | } |
| 261 | Num_postrenders = 0; |
| 262 | rend_SetFogState(0); |
| 263 | RenderLightGlows(); |
| 264 | } |