| 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/model/newstyle.cpp $ |
| 21 | * $Revision: 122 $ |
| 22 | * $Date: 3/20/00 12:28p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Polygon model code |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/model/newstyle.cpp $ |
| 28 | * |
| 29 | * 122 3/20/00 12:28p Matt |
| 30 | * Merge of Duane's post-1.3 changes. |
| 31 | * Check for NULL pointer. |
| 32 | * |
| 33 | * 121 7/08/99 5:47p Jason |
| 34 | * changes for new bumpmapping system in 1.1 update patch |
| 35 | * |
| 36 | * 120 6/08/99 1:00p Jason |
| 37 | * changes for bumpmapping |
| 38 | * |
| 39 | * 119 5/18/99 10:31a Jason |
| 40 | * polymodel stuff wasn't getting paged in before drawing |
| 41 | * |
| 42 | * 118 5/14/99 2:03p Jason |
| 43 | * added polymodel errors to catch bugs |
| 44 | * |
| 45 | * 117 5/14/99 12:02a Jason |
| 46 | * more speedups for g3_DrawPoly |
| 47 | * |
| 48 | * 116 5/13/99 8:33p Matt |
| 49 | * Consolidated Mac & Windows code. |
| 50 | * |
| 51 | * 115 5/13/99 5:07p Ardussi |
| 52 | * changes for compiling on the Mac |
| 53 | * |
| 54 | * 114 5/04/99 4:34p Jason |
| 55 | * changes for bumpmapping |
| 56 | * |
| 57 | * 113 4/22/99 8:29p Kevin |
| 58 | * made psrand.h come after stdlib.h |
| 59 | * |
| 60 | * 112 4/21/99 11:05a Kevin |
| 61 | * new ps_rand and ps_srand to replace rand & srand |
| 62 | * |
| 63 | * 111 4/16/99 5:32p Jason |
| 64 | * fixed smooth specularity with objects |
| 65 | * |
| 66 | * 110 4/14/99 1:36a Jeff |
| 67 | * fixed case mismatched #includes |
| 68 | * |
| 69 | * 109 4/09/99 12:06p Jason |
| 70 | * made model setup code faster |
| 71 | * |
| 72 | * 108 3/31/99 12:26p Jason |
| 73 | * fixed some issues related to non wbuffer cards |
| 74 | * |
| 75 | * 107 3/24/99 10:22a Matt |
| 76 | * Fixed null-pointer bug |
| 77 | * |
| 78 | */ |
| 79 | |
| 80 | #include <cstdlib> |
| 81 | |
| 82 | #include "3d.h" |
| 83 | #include "findintersection.h" |
| 84 | #include "fireball.h" |
| 85 | #include "game.h" |
| 86 | #include "lighting.h" |
| 87 | #include "lightmap.h" |
| 88 | #include "lightmap_info.h" |
| 89 | #include "polymodel.h" |
| 90 | #include "pserror.h" |
| 91 | #include "psrand.h" |
| 92 | #include "render.h" |
| 93 | #include "vecmat.h" |
| 94 | |
| 95 | static float face_depth[MAX_POLYGON_VECS]; |
| 96 | static uint8_t triangulated_faces[MAX_FACES_PER_ROOM]; |
| 97 | |
| 98 | static uint8_t FacingPass = 0; |
| 99 | static int Multicolor_texture = -1; |
| 100 | |
| 101 | static vector Fog_plane; |
| 102 | static float Fog_distance, Fog_eye_distance; |
| 103 | static vector Fog_view_pos, Specular_view_pos, Bump_view_pos; |
| 104 | static matrix Unscaled_bumpmap_matrix; |
| 105 | |
| 106 | static int ModelFaceSortFunc(const int16_t *a, const int16_t *b); |
| 107 | static inline void RenderSubmodelFace(poly_model *pm, bsp_info *sm, int facenum); |
| 108 | static inline void RenderSubmodelLightmapFace(poly_model *pm, bsp_info *sm, int facenum); |
| 109 | static inline void RenderSubmodelFaceFogged(poly_model *pm, bsp_info *sm, int facenum); |
| 110 | static inline void RenderSubmodelFaceSpecular(poly_model *pm, bsp_info *sm, int facenum); |
| 111 | |
| 112 | /// Draws a glowing cone of light. |
| 113 | static void DrawGlowEffect(vector *pos, float r, float g, float b, vector *norm, float size); |
| 114 | |
| 115 | /// Draws a glowing cone of light that represents thrusters |
| 116 | static void DrawThrusterEffect(vector *pos, float r, float g, float b, vector *norm, float size, float length); |
| 117 | |
| 118 | static void RenderSubmodelFacesSorted(poly_model *pm, bsp_info *sm); |
| 119 | static void RenderSubmodelFacesUnsorted(poly_model *pm, bsp_info *sm); |
| 120 | |
| 121 | /// Rotates all of the points of a submodel, plus supplies color info. |
| 122 | static void RotateModelPoints(poly_model *pm, bsp_info *sm); |
| 123 | |
| 124 | static float ComputeDefaultSizeFunc(int handle, float *size_ptr, vector *offset_ptr, bool f_use_all_frames); |
| 125 | |
| 126 | int ModelFaceSortFunc(const int16_t *a, const int16_t *b) { |
| 127 | float az, bz; |
| 128 | |
| 129 | az = face_depth[*a]; |
| 130 | bz = face_depth[*b]; |
| 131 | |
| 132 | if (az < bz) |
| 133 | return -1; |
| 134 | else if (az > bz) |
| 135 | return 1; |
| 136 | else |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | #ifdef _DEBUG |
| 141 | static void model_draw_outline(int nverts, g3Point **pointlist) { |
| 142 | int i; |
| 143 | |
| 144 | for (i = 0; i < nverts - 1; i++) |
| 145 | g3_DrawLine(GR_RGB(255, 255, 255), pointlist[i], pointlist[i + 1]); |
| 146 | |
| 147 | g3_DrawLine(GR_RGB(255, 255, 255), pointlist[i], pointlist[0]); |
| 148 | } |
| 149 | |
| 150 | static void DrawSubmodelFaceOutline(int nv, g3Point **pointlist) { |
| 151 | int i; |
| 152 | g3Point tpnt[64]; |
| 153 | g3Point *tpnt_list[64]; |
| 154 | |
| 155 | ASSERT(nv < 64); |
| 156 | |
| 157 | for (i = 0; i < nv; i++) { |
| 158 | tpnt[i] = *pointlist[i]; |
| 159 | tpnt_list[i] = &tpnt[i]; |
| 160 | } |
| 161 | |
| 162 | for (i = 0; i < nv - 1; i++) |
| 163 | g3_DrawLine(GR_RGB(255, 255, 0), tpnt_list[i], tpnt_list[i + 1]); |
| 164 | g3_DrawLine(GR_RGB(255, 255, 0), tpnt_list[i], tpnt_list[0]); |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | #define CROSS_WIDTH 8.0 |
| 169 | int Lightmap_debug_subnum = -1; |
| 170 | int Lightmap_debug_facenum = -1; |
| 171 | int Lightmap_debug_model = -1; |
| 172 | |
| 173 | inline void RenderSubmodelFace(poly_model *pm, bsp_info *sm, int facenum) { |
| 174 | |
| 175 | g3Point *pointlist[100]; |
| 176 | int bm_handle; |
| 177 | int smooth = 0; |
| 178 | polyface *fp = &sm->faces[facenum]; |
| 179 | int modelnum = sm - pm->submodel; |
| 180 | texture *texp = nullptr; |
| 181 | int t; |
| 182 | int custom = 0; |
| 183 | g3Codes face_cc; |
| 184 | int triface = 0; |
| 185 | |
| 186 | face_cc.cc_and = 0xff; |
| 187 | face_cc.cc_or = 0; |
| 188 | |
| 189 | triangulated_faces[facenum] = 0; |
| 190 | |
| 191 | if (sm->flags & SOF_CUSTOM) |
| 192 | custom = 1; |
| 193 | |
| 194 | // Setup texturing |
| 195 | if (fp->texnum != -1) |
| 196 | texp = &GameTextures[pm->textures[fp->texnum]]; |
| 197 | |
| 198 | if (texp && custom && Polymodel_use_effect && (Polymodel_effect.type & PEF_CUSTOM_TEXTURE)) |
| 199 | texp = &GameTextures[Polymodel_effect.custom_texture]; |
| 200 | |
| 201 | // Set radiosity lightmaps if needed |
| 202 | if (Polymodel_light_type == POLYMODEL_LIGHTING_LIGHTMAP) { |
| 203 | rend_SetOverlayMap( |
| 204 | LightmapInfo[Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].lmi_handle].lm_handle); |
| 205 | } |
| 206 | |
| 207 | // Do bump mapping |
| 208 | if ((Polymodel_effect.type & PEF_BUMPMAPPED) && texp && texp->bumpmap != -1 && |
| 209 | Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) { |
| 210 | rend_SetOverlayType(OT_NONE); |
| 211 | rend_SetBumpmapReadyState(1, texp->bumpmap); |
| 212 | if ((GameTextures[fp->texnum].flags & TF_SMOOTH_SPECULAR)) |
| 213 | smooth = 1; |
| 214 | } |
| 215 | |
| 216 | float uchange = 0, vchange = 0; |
| 217 | |
| 218 | // Figure out if there is any texture sliding |
| 219 | if (texp && texp->slide_u != 0) { |
| 220 | int int_time = Gametime / texp->slide_u; |
| 221 | float norm_time = Gametime - (int_time * texp->slide_u); |
| 222 | norm_time /= texp->slide_u; |
| 223 | |
| 224 | uchange = norm_time; |
| 225 | } |
| 226 | |
| 227 | if (texp && texp->slide_v != 0) { |
| 228 | int int_time = Gametime / texp->slide_v; |
| 229 | float norm_time = Gametime - (int_time * texp->slide_v); |
| 230 | norm_time /= texp->slide_v; |
| 231 | vchange = norm_time; |
| 232 | } |
| 233 | |
| 234 | ASSERT(fp->nverts < 100); |
| 235 | |
| 236 | // Setup the points for this face |
| 237 | for (t = 0; t < fp->nverts; t++) { |
| 238 | g3Point *p = &Robot_points[fp->vertnums[t]]; |
| 239 | pointlist[t] = p; |
| 240 | |
| 241 | if (texp) { |
| 242 | p->p3_uvl.u = fp->u[t] + uchange; |
| 243 | p->p3_uvl.v = fp->v[t] + vchange; |
| 244 | p->p3_uvl.a = sm->alpha[fp->vertnums[t]]; |
| 245 | p->p3_flags |= PF_UV + PF_RGBA + PF_L; |
| 246 | |
| 247 | // Assign bump mapping coords |
| 248 | if ((Polymodel_effect.type & PEF_BUMPMAPPED) && texp->bumpmap != -1 && |
| 249 | Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) { |
| 250 | p->p3_flags |= PF_UV2; |
| 251 | |
| 252 | vector vert = sm->verts[fp->vertnums[t]]; |
| 253 | |
| 254 | vector vertnorm; |
| 255 | |
| 256 | if (smooth) |
| 257 | vertnorm = sm->vertnorms[fp->vertnums[t]]; |
| 258 | else |
| 259 | vertnorm = fp->normal; |
| 260 | |
| 261 | vector subvec = Bump_view_pos - vert; |
| 262 | vm_NormalizeVectorFast(&subvec); |
| 263 | |
| 264 | vector incident_norm = vert - Polymodel_bump_pos; |
| 265 | vm_NormalizeVectorFast(&incident_norm); |
| 266 | |
| 267 | float d = incident_norm * vertnorm; |
| 268 | vector upvec = d * vertnorm; |
| 269 | incident_norm -= (2 * upvec); |
| 270 | |
| 271 | float dotp = (subvec * incident_norm); |
| 272 | |
| 273 | if (dotp < 0) |
| 274 | dotp = 0; |
| 275 | if (dotp > 1) |
| 276 | dotp = 1; |
| 277 | |
| 278 | float val = dotp * .5; |
| 279 | |
| 280 | p->p3_uvl.u2 = val; |
| 281 | p->p3_uvl.v2 = val; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (Polymodel_light_type == POLYMODEL_LIGHTING_LIGHTMAP) { |
| 286 | p->p3_flags |= PF_UV2; |
| 287 | p->p3_uvl.u2 = Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].u2[t]; |
| 288 | p->p3_uvl.v2 = Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].v2[t]; |
| 289 | } |
| 290 | |
| 291 | face_cc.cc_or |= p->p3_codes; |
| 292 | face_cc.cc_and &= p->p3_codes; |
| 293 | } |
| 294 | |
| 295 | if (face_cc.cc_or && Polymodel_use_effect && |
| 296 | (Polymodel_effect.type & (PEF_FOGGED_MODEL | PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES))) { |
| 297 | triface = 1; |
| 298 | triangulated_faces[facenum] = 1; |
| 299 | } |
| 300 | |
| 301 | // If there is a texture, set it up |
| 302 | if (texp) { |
| 303 | bm_handle = GetTextureBitmap(texp - GameTextures, 0); |
| 304 | |
| 305 | rend_SetTextureType(TT_LINEAR); |
| 306 | if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) |
| 307 | rend_SetLighting(LS_GOURAUD); |
| 308 | |
| 309 | // If this is a light texture, make the texture full bright |
| 310 | if (texp->flags & TF_LIGHT) { |
| 311 | rend_SetLighting(LS_FLAT_GOURAUD); |
| 312 | rend_SetFlatColor(GR_RGB(255, 255, 255)); |
| 313 | } |
| 314 | |
| 315 | // Setup custom color if there is one |
| 316 | if (Polymodel_use_effect && (Polymodel_effect.type & PEF_CUSTOM_COLOR) && |
| 317 | (texp - GameTextures) == Multicolor_texture) { |
| 318 | int r, g, b; |
| 319 | |
| 320 | rend_SetLighting(LS_FLAT_GOURAUD); |
| 321 | |
| 322 | r = GR_COLOR_RED(Polymodel_effect.custom_color); |
| 323 | g = GR_COLOR_GREEN(Polymodel_effect.custom_color); |
| 324 | b = GR_COLOR_BLUE(Polymodel_effect.custom_color); |
| 325 | |
| 326 | if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) { |
| 327 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_COLOR) { |
| 328 | r = Polymodel_effect.r * (float)r * Polylighting_static_red; |
| 329 | g = Polymodel_effect.g * (float)g * Polylighting_static_green; |
| 330 | b = Polymodel_effect.b * (float)b * Polylighting_static_blue; |
| 331 | } else { |
| 332 | r = (float)r * Polylighting_static_red; |
| 333 | g = (float)g * Polylighting_static_green; |
| 334 | b = (float)b * Polylighting_static_blue; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | rend_SetFlatColor(GR_RGB(r, g, b)); |
| 339 | } |
| 340 | |
| 341 | if (Polymodel_use_effect && (Polymodel_effect.type & PEF_ALPHA)) |
| 342 | rend_SetAlphaValue(texp->alpha * Polymodel_effect.alpha * 255); |
| 343 | else |
| 344 | rend_SetAlphaValue(texp->alpha * 255); |
| 345 | |
| 346 | if (texp->flags & TF_SATURATE) |
| 347 | rend_SetAlphaType(AT_SATURATE_CONSTANT_VERTEX); |
| 348 | else { |
| 349 | if ((texp->flags & TF_ALPHA) || (Polymodel_use_effect && (Polymodel_effect.type & PEF_ALPHA))) |
| 350 | rend_SetAlphaType(ATF_CONSTANT + ATF_VERTEX); |
| 351 | else |
| 352 | rend_SetAlphaType(ATF_TEXTURE + ATF_VERTEX); |
| 353 | } |
| 354 | } else { |
| 355 | rend_SetAlphaType(ATF_CONSTANT + ATF_VERTEX); |
| 356 | if (Polymodel_use_effect && (Polymodel_effect.type & PEF_ALPHA)) |
| 357 | rend_SetAlphaValue(Polymodel_effect.alpha * 255); |
| 358 | else |
| 359 | rend_SetAlphaValue(255); |
| 360 | |
| 361 | rend_SetLighting(LS_NONE); |
| 362 | rend_SetTextureType(TT_FLAT); |
| 363 | |
| 364 | int r, g, b; |
| 365 | |
| 366 | r = GR_COLOR_RED(fp->color); |
| 367 | g = GR_COLOR_GREEN(fp->color); |
| 368 | b = GR_COLOR_BLUE(fp->color); |
| 369 | |
| 370 | if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) { |
| 371 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_COLOR) { |
| 372 | r = Polymodel_effect.r * (float)r * Polylighting_static_red; |
| 373 | g = Polymodel_effect.g * (float)g * Polylighting_static_green; |
| 374 | b = Polymodel_effect.b * (float)b * Polylighting_static_blue; |
| 375 | } else { |
| 376 | r = (float)r * Polylighting_static_red; |
| 377 | g = (float)g * Polylighting_static_green; |
| 378 | b = (float)b * Polylighting_static_blue; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | rend_SetFlatColor(GR_RGB(r, g, b)); |
| 383 | |
| 384 | bm_handle = 0; |
| 385 | } |
| 386 | |
| 387 | if (triface) |
| 388 | g3_SetTriangulationTest(1); |
| 389 | |
| 390 | g3_DrawPoly(fp->nverts, pointlist, bm_handle, MAP_TYPE_BITMAP, &face_cc); |
| 391 | |
| 392 | if (triface) |
| 393 | g3_SetTriangulationTest(0); |
| 394 | |
| 395 | if (texp && (Polymodel_effect.type & PEF_BUMPMAPPED) && texp->bumpmap != -1 && |
| 396 | Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) { |
| 397 | rend_SetBumpmapReadyState(0, 0); |
| 398 | } |
| 399 | |
| 400 | #ifdef _DEBUG |
| 401 | if (Polymodel_outline_mode) |
| 402 | DrawSubmodelFaceOutline(fp->nverts, pointlist); |
| 403 | |
| 404 | /* if (Lightmap_debug_model==(pm-Poly_models) && Polymodel_light_type==POLYMODEL_LIGHTING_LIGHTMAP && |
| 405 | Lightmap_debug_subnum==modelnum && Lightmap_debug_facenum==facenum) |
| 406 | { |
| 407 | |
| 408 | int lmi_handle=Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].lmi_handle; |
| 409 | lightmap_object_face *lfp=&Polylighting_lightmap_object->lightmap_faces[modelnum][facenum]; |
| 410 | lightmap_info *lmi_ptr=&LightmapInfo[lmi_handle]; |
| 411 | int w=lmi_w (lmi_handle); |
| 412 | int h=lmi_h (lmi_handle); |
| 413 | vector rvec=lfp->rvec*lmi_ptr->xspacing; |
| 414 | vector uvec=lfp->uvec*lmi_ptr->yspacing; |
| 415 | uint16_t *src_data=(uint16_t *)lm_data(lmi_ptr->lm_handle); |
| 416 | |
| 417 | for (int i=0;i<w*h;i++) |
| 418 | { |
| 419 | int t; |
| 420 | g3Point epoints[20]; |
| 421 | vector evec[20]; |
| 422 | int y=i/w; |
| 423 | int x=i%w; |
| 424 | |
| 425 | evec[0]=lmi_ptr->upper_left-(y*uvec)+(x*rvec); |
| 426 | g3_RotatePoint(&epoints[0],&evec[0]); |
| 427 | pointlist[0] = &epoints[0]; |
| 428 | |
| 429 | evec[1]=lmi_ptr->upper_left-(y*uvec)+((x+1)*rvec); |
| 430 | g3_RotatePoint(&epoints[1],&evec[1]); |
| 431 | pointlist[1] = &epoints[1]; |
| 432 | |
| 433 | evec[2]=lmi_ptr->upper_left-((y+1)*uvec)+((x+1)*rvec); |
| 434 | g3_RotatePoint(&epoints[2],&evec[2]); |
| 435 | pointlist[2] = &epoints[2]; |
| 436 | |
| 437 | evec[3]=lmi_ptr->upper_left-((y+1)*uvec)+(x*rvec); |
| 438 | g3_RotatePoint(&epoints[3],&evec[3]); |
| 439 | pointlist[3] = &epoints[3]; |
| 440 | |
| 441 | if (!(src_data[y*w+x] & OPAQUE_FLAG)) |
| 442 | { |
| 443 | for (t=0;t<4;t++) |
| 444 | g3_DrawLine(GR_RGB(255,0,255),pointlist[t],pointlist[(t+1)%4]); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | for (t=0;t<4;t++) |
| 449 | g3_DrawLine(GR_RGB(255,255,255),pointlist[t],pointlist[(t+1)%4]); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // Draw red cross where upper left is |
| 454 | uint8_t c0; |
| 455 | g3Point p0; |
| 456 | p0.p3_flags=0; |
| 457 | c0 = g3_RotatePoint(&p0,&LightmapInfo[lmi_handle].upper_left); |
| 458 | |
| 459 | if (! c0) |
| 460 | { |
| 461 | |
| 462 | //Draw a little cross at the current vert |
| 463 | g3_ProjectPoint(&p0); //make sure projected |
| 464 | rend_SetFlatColor(GR_RGB(255,0,0)); |
| 465 | rend_DrawLine(p0.p3_sx-CROSS_WIDTH,p0.p3_sy,p0.p3_sx,p0.p3_sy-CROSS_WIDTH); |
| 466 | rend_DrawLine(p0.p3_sx,p0.p3_sy-CROSS_WIDTH,p0.p3_sx+CROSS_WIDTH,p0.p3_sy); |
| 467 | rend_DrawLine(p0.p3_sx+CROSS_WIDTH,p0.p3_sy,p0.p3_sx,p0.p3_sy+CROSS_WIDTH); |
| 468 | rend_DrawLine(p0.p3_sx,p0.p3_sy+CROSS_WIDTH,p0.p3_sx-CROSS_WIDTH,p0.p3_sy); |
| 469 | } |
| 470 | |
| 471 | }*/ |
| 472 | |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | inline void RenderSubmodelLightmapFace(poly_model *pm, bsp_info *sm, int facenum) { |
| 477 | g3Point *pointlist[100]; |
| 478 | |
| 479 | polyface *fp = &sm->faces[facenum]; |
| 480 | int modelnum = sm - pm->submodel; |
| 481 | int t; |
| 482 | |
| 483 | int lm_handle = LightmapInfo[Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].lmi_handle].lm_handle; |
| 484 | float xscalar = (float)GameLightmaps[lm_handle].width / (float)GameLightmaps[lm_handle].square_res; |
| 485 | float yscalar = (float)GameLightmaps[lm_handle].height / (float)GameLightmaps[lm_handle].square_res; |
| 486 | |
| 487 | ASSERT(fp->nverts < 100); |
| 488 | |
| 489 | // Setup the points for this face |
| 490 | for (t = 0; t < fp->nverts; t++) { |
| 491 | g3Point *p = &Robot_points[fp->vertnums[t]]; |
| 492 | pointlist[t] = p; |
| 493 | |
| 494 | p->p3_uvl.u = Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].u2[t] * xscalar; |
| 495 | p->p3_uvl.v = Polylighting_lightmap_object->lightmap_faces[modelnum][facenum].v2[t] * yscalar; |
| 496 | p->p3_uvl.l = 1.0; |
| 497 | |
| 498 | p->p3_flags |= PF_UV2 + PF_RGBA + PF_L; |
| 499 | } |
| 500 | |
| 501 | if (triangulated_faces[facenum]) |
| 502 | g3_SetTriangulationTest(1); |
| 503 | |
| 504 | g3_DrawPoly(fp->nverts, pointlist, lm_handle, MAP_TYPE_LIGHTMAP); |
| 505 | |
| 506 | if (triangulated_faces[facenum]) |
| 507 | g3_SetTriangulationTest(0); |
| 508 | } |
| 509 | |
| 510 | inline void RenderSubmodelFaceFogged(poly_model *pm, bsp_info *sm, int facenum) { |
| 511 | g3Point *pointlist[100]; |
| 512 | polyface *fp = &sm->faces[facenum]; |
| 513 | int modelnum = sm - pm->submodel; |
| 514 | int t; |
| 515 | |
| 516 | for (t = 0; t < fp->nverts; t++) { |
| 517 | g3Point *p = &Robot_points[fp->vertnums[t]]; |
| 518 | pointlist[t] = p; |
| 519 | |
| 520 | float mag; |
| 521 | |
| 522 | if (Polymodel_effect.fog_plane_check == 1) { |
| 523 | mag = vm_DotProduct(&Fog_plane, &sm->verts[fp->vertnums[t]]) + Fog_distance; |
| 524 | } else { |
| 525 | vector *vec = &sm->verts[fp->vertnums[t]]; |
| 526 | |
| 527 | // Now we must generate the split point. This is simply |
| 528 | // an equation in the form Origin + t*Direction |
| 529 | |
| 530 | float dist = (*vec * Polymodel_fog_plane) + Fog_distance; |
| 531 | |
| 532 | vector subvec = *vec - Fog_view_pos; |
| 533 | |
| 534 | float t = Fog_eye_distance / (Fog_eye_distance - dist); |
| 535 | vector portal_point = Fog_view_pos + (t * subvec); |
| 536 | |
| 537 | float eye_distance = -(vm_DotProduct(&Fog_plane, &portal_point)); |
| 538 | mag = vm_DotProduct(&Fog_plane, vec) + eye_distance; |
| 539 | } |
| 540 | |
| 541 | float scalar = mag / Polymodel_effect.fog_depth; |
| 542 | |
| 543 | if (scalar > 1) |
| 544 | scalar = 1; |
| 545 | if (scalar < 0) |
| 546 | scalar = 0; |
| 547 | p->p3_a = scalar; |
| 548 | |
| 549 | p->p3_flags |= PF_RGBA; |
| 550 | } |
| 551 | |
| 552 | if (triangulated_faces[facenum]) |
| 553 | g3_SetTriangulationTest(1); |
| 554 | |
| 555 | g3_DrawPoly(fp->nverts, pointlist, 0); |
| 556 | |
| 557 | if (triangulated_faces[facenum]) |
| 558 | g3_SetTriangulationTest(0); |
| 559 | } |
| 560 | |
| 561 | inline void RenderSubmodelFaceSpecular(poly_model *pm, bsp_info *sm, int facenum) { |
| 562 | g3Point *pointlist[100]; |
| 563 | polyface *fp = &sm->faces[facenum]; |
| 564 | int modelnum = sm - pm->submodel; |
| 565 | int t; |
| 566 | bool smooth = 0; |
| 567 | |
| 568 | if ((Polymodel_effect.type & PEF_SPECULAR_FACES) && (GameTextures[fp->texnum].flags & TF_SMOOTH_SPECULAR)) |
| 569 | smooth = 1; |
| 570 | |
| 571 | for (t = 0; t < fp->nverts; t++) { |
| 572 | g3Point *p = &Robot_points[fp->vertnums[t]]; |
| 573 | vector vert = sm->verts[fp->vertnums[t]]; |
| 574 | |
| 575 | vector vertnorm; |
| 576 | |
| 577 | if (smooth) |
| 578 | vertnorm = sm->vertnorms[fp->vertnums[t]]; |
| 579 | else |
| 580 | vertnorm = fp->normal; |
| 581 | |
| 582 | pointlist[t] = p; |
| 583 | |
| 584 | p->p3_flags |= PF_RGBA; |
| 585 | p->p3_a = 0.0; |
| 586 | |
| 587 | vector subvec = Specular_view_pos - vert; |
| 588 | vm_NormalizeVectorFast(&subvec); |
| 589 | |
| 590 | vector incident_norm = vert - Polymodel_specular_pos; |
| 591 | vm_NormalizeVectorFast(&incident_norm); |
| 592 | |
| 593 | float d = incident_norm * vertnorm; |
| 594 | vector upvec = d * vertnorm; |
| 595 | incident_norm -= (2 * upvec); |
| 596 | |
| 597 | float dotp = subvec * incident_norm; |
| 598 | |
| 599 | if (dotp < 0) |
| 600 | continue; |
| 601 | if (dotp > 1) |
| 602 | dotp = 1; |
| 603 | |
| 604 | if (dotp > 0) { |
| 605 | int index = ((float)(MAX_SPECULAR_INCREMENTS - 1) * dotp); |
| 606 | float val = Specular_tables[2][index]; |
| 607 | |
| 608 | p->p3_a = val * Polymodel_effect.spec_scalar; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (triangulated_faces[facenum]) |
| 613 | g3_SetTriangulationTest(1); |
| 614 | |
| 615 | g3_DrawPoly(fp->nverts, pointlist, 0); |
| 616 | |
| 617 | if (triangulated_faces[facenum]) |
| 618 | g3_SetTriangulationTest(0); |
| 619 | } |
| 620 | |
| 621 | #define MAX_PARTS 100 |
| 622 | |
| 623 | // Draws a glowing cone of light that represents thrusters |
| 624 | void DrawThrusterEffect(vector *pos, float r, float g, float b, vector *norm, float size, float length) { |
| 625 | vector cur_pos = *pos; |
| 626 | float cur_length = 0; |
| 627 | vector glow_pos[MAX_PARTS]; |
| 628 | float glow_size[MAX_PARTS]; |
| 629 | int total_parts = 0; |
| 630 | |
| 631 | if (length < .1) |
| 632 | return; |
| 633 | |
| 634 | int num_divs = length * 3; |
| 635 | if (num_divs > MAX_PARTS) |
| 636 | num_divs = MAX_PARTS; |
| 637 | |
| 638 | float size_change = size / num_divs; |
| 639 | float pos_change = length / num_divs; |
| 640 | |
| 641 | if (!UseHardware) |
| 642 | return; // No software stuff here! |
| 643 | |
| 644 | rend_SetZBufferWriteMask(0); |
| 645 | rend_SetAlphaType(AT_SATURATE_TEXTURE); |
| 646 | rend_SetAlphaValue(.3 * 255); |
| 647 | |
| 648 | rend_SetLighting(LS_GOURAUD); |
| 649 | rend_SetColorModel(CM_RGB); |
| 650 | |
| 651 | ddgr_color color = GR_RGB(r * 255, g * 255, b * 255); |
| 652 | int bm_handle = Fireballs[GRADIENT_BALL_INDEX].bm_handle; |
| 653 | int t; |
| 654 | |
| 655 | // We must draw the small ones first, but we're starting the iteration from the |
| 656 | // large one. Consequently, we must store our variables so we can draw in reverse order |
| 657 | for (t = 0; t < num_divs && size > .05; t++) { |
| 658 | glow_pos[t] = cur_pos; |
| 659 | glow_size[t] = size; |
| 660 | |
| 661 | size -= size_change; |
| 662 | cur_pos += ((*norm) * pos_change); |
| 663 | |
| 664 | total_parts++; |
| 665 | } |
| 666 | |
| 667 | for (t = total_parts - 1; t >= 0; t--) { |
| 668 | rend_SetZBias(-glow_size[t]); |
| 669 | g3_DrawBitmap(&glow_pos[t], glow_size[t], (glow_size[t] * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle, |
| 670 | color); |
| 671 | } |
| 672 | rend_SetZBias(0.0); |
| 673 | rend_SetZBufferWriteMask(1); |
| 674 | } |
| 675 | |
| 676 | // Draws a glowing cone of light |
| 677 | void DrawGlowEffect(vector *pos, float r, float g, float b, vector *norm, float size) { |
| 678 | if (!UseHardware) |
| 679 | return; // No software stuff here! |
| 680 | |
| 681 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_NO_GLOWS) |
| 682 | return; |
| 683 | |
| 684 | rend_SetZBufferWriteMask(0); |
| 685 | rend_SetAlphaType(AT_SATURATE_TEXTURE); |
| 686 | rend_SetAlphaValue(.8 * 255); |
| 687 | rend_SetLighting(LS_GOURAUD); |
| 688 | rend_SetColorModel(CM_RGB); |
| 689 | |
| 690 | ddgr_color color = GR_RGB(r * 255, g * 255, b * 255); |
| 691 | int bm_handle = Fireballs[GRADIENT_BALL_INDEX].bm_handle; |
| 692 | |
| 693 | rend_SetZBias(-size); |
| 694 | g3_DrawBitmap(pos, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle, color); |
| 695 | |
| 696 | rend_SetZBias(0.0); |
| 697 | rend_SetZBufferWriteMask(1); |
| 698 | } |
| 699 | |
| 700 | void RenderSubmodelFacesSorted(poly_model *pm, bsp_info *sm) { |
| 701 | int i, t; |
| 702 | |
| 703 | int rcount; |
| 704 | int model_render_order[MAX_POLYGON_VECS]; |
| 705 | int modelnum = sm - pm->submodel; |
| 706 | |
| 707 | ASSERT(sm->nverts < MAX_POLYGON_VECS); |
| 708 | |
| 709 | // Build list of visible (non-backfacing) faces, & compute average face depths |
| 710 | for (i = rcount = 0; i < sm->num_faces; i++) { |
| 711 | polyface *fp = &sm->faces[i]; |
| 712 | |
| 713 | // check for visible face |
| 714 | if (g3_CheckNormalFacing(&sm->verts[fp->vertnums[0]], &fp->normal)) { |
| 715 | face_depth[i] = 0; |
| 716 | for (t = 0; t < fp->nverts; t++) |
| 717 | face_depth[i] += Robot_points[fp->vertnums[t]].p3_z; |
| 718 | |
| 719 | face_depth[i] /= fp->nverts; |
| 720 | |
| 721 | // initialize order list |
| 722 | model_render_order[rcount] = i; |
| 723 | |
| 724 | rcount++; |
| 725 | |
| 726 | ASSERT(rcount < MAX_POLYGON_VECS); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Sort the faces |
| 731 | qsort(model_render_order, rcount, sizeof(*model_render_order), |
| 732 | (int (*)(const void *, const void *))ModelFaceSortFunc); |
| 733 | |
| 734 | for (i = rcount - 1; i >= 0; i--) { |
| 735 | int facenum = model_render_order[i]; |
| 736 | |
| 737 | RenderSubmodelFace(pm, sm, facenum); |
| 738 | } |
| 739 | } |
| 740 | void RenderSubmodelFacesUnsorted(poly_model *pm, bsp_info *sm) { |
| 741 | int i; |
| 742 | int modelnum = sm - pm->submodel; |
| 743 | int16_t alpha_faces[MAX_FACES_PER_ROOM], num_alpha_faces = 0; |
| 744 | int rcount = 0; |
| 745 | vector view_pos; |
| 746 | |
| 747 | g3_GetViewPosition(&view_pos); |
| 748 | g3_GetUnscaledMatrix(&Unscaled_bumpmap_matrix); |
| 749 | |
| 750 | Specular_view_pos = view_pos; |
| 751 | Bump_view_pos = view_pos; |
| 752 | |
| 753 | if (modelnum < 0 || modelnum >= pm->n_models) { |
| 754 | Error("Got bad model number %d from polymodel %s!", modelnum, pm->name); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | if (sm->flags & SOF_CUSTOM) |
| 759 | rend_SetZBias(-.5); |
| 760 | |
| 761 | for (i = 0; i < sm->num_faces; i++) { |
| 762 | vector tempv; |
| 763 | polyface *fp = &sm->faces[i]; |
| 764 | texture *texp; |
| 765 | |
| 766 | // Check to see if this face even faces us! |
| 767 | tempv = view_pos - sm->verts[fp->vertnums[0]]; |
| 768 | if ((tempv * fp->normal) < 0) |
| 769 | continue; |
| 770 | |
| 771 | if (fp->texnum != -1) { |
| 772 | texp = &GameTextures[pm->textures[fp->texnum]]; |
| 773 | |
| 774 | if (texp->flags & TF_ALPHA || texp->flags & TF_SATURATE) { |
| 775 | alpha_faces[num_alpha_faces++] = i; |
| 776 | continue; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (StateLimited) { |
| 781 | State_elements[rcount].facenum = i; |
| 782 | State_elements[rcount].sort_key = pm->textures[fp->texnum]; |
| 783 | rcount++; |
| 784 | } else |
| 785 | RenderSubmodelFace(pm, sm, i); |
| 786 | } |
| 787 | |
| 788 | if (StateLimited) { |
| 789 | SortStates(State_elements, rcount); |
| 790 | for (i = rcount - 1; i >= 0; i--) { |
| 791 | int facenum = State_elements[i].facenum; |
| 792 | RenderSubmodelFace(pm, sm, facenum); |
| 793 | } |
| 794 | |
| 795 | if (!NoLightmaps) { |
| 796 | if (!UseMultitexture && Polymodel_light_type == POLYMODEL_LIGHTING_LIGHTMAP) { |
| 797 | rend_SetAlphaType(AT_LIGHTMAP_BLEND); |
| 798 | rend_SetLighting(LS_GOURAUD); |
| 799 | rend_SetColorModel(CM_MONO); |
| 800 | rend_SetOverlayType(OT_NONE); |
| 801 | rend_SetTextureType(TT_PERSPECTIVE); |
| 802 | rend_SetWrapType(WT_CLAMP); |
| 803 | rend_SetMipState(0); |
| 804 | |
| 805 | for (i = rcount - 1; i >= 0; i--) { |
| 806 | int facenum = State_elements[i].facenum; |
| 807 | RenderSubmodelLightmapFace(pm, sm, facenum); |
| 808 | } |
| 809 | |
| 810 | rend_SetWrapType(WT_WRAP); |
| 811 | rend_SetMipState(1); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | // Now render all alpha faces |
| 817 | // rend_SetZBufferWriteMask (0); |
| 818 | for (i = 0; i < num_alpha_faces; i++) |
| 819 | RenderSubmodelFace(pm, sm, alpha_faces[i]); |
| 820 | // rend_SetZBufferWriteMask (1); |
| 821 | |
| 822 | if (sm->flags & SOF_CUSTOM) |
| 823 | rend_SetZBias(0); |
| 824 | |
| 825 | // Draw specular faces if needed |
| 826 | if (Polymodel_use_effect && Polymodel_effect.type & (PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES)) { |
| 827 | rend_SetOverlayType(OT_NONE); |
| 828 | rend_SetTextureType(TT_FLAT); |
| 829 | rend_SetLighting(LS_NONE); |
| 830 | rend_SetColorModel(CM_MONO); |
| 831 | rend_SetAlphaType(AT_SATURATE_VERTEX); |
| 832 | rend_SetAlphaValue(255); |
| 833 | rend_SetZBufferWriteMask(0); |
| 834 | |
| 835 | rend_SetFlatColor(GR_RGB((int)(Polymodel_effect.spec_r * 255.0), (int)(Polymodel_effect.spec_g * 255.0), |
| 836 | (int)(Polymodel_effect.spec_b * 255.0))); |
| 837 | |
| 838 | for (i = 0; i < sm->num_faces; i++) { |
| 839 | polyface *fp = &sm->faces[i]; |
| 840 | |
| 841 | if (!g3_CheckNormalFacing(&sm->verts[fp->vertnums[0]], &fp->normal)) |
| 842 | continue; |
| 843 | |
| 844 | /* vector subvec=sm->verts[fp->vertnums[0]]-Polymodel_specular_pos; |
| 845 | if ((fp->normal * subvec)> 0) |
| 846 | continue;*/ |
| 847 | |
| 848 | if ((Polymodel_effect.type & PEF_SPECULAR_MODEL) || |
| 849 | (fp->texnum != -1 && GameTextures[pm->textures[fp->texnum]].flags & TF_SPECULAR)) |
| 850 | RenderSubmodelFaceSpecular(pm, sm, i); |
| 851 | } |
| 852 | |
| 853 | rend_SetZBufferWriteMask(1); |
| 854 | } |
| 855 | |
| 856 | // Draw fog if need be |
| 857 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_FOGGED_MODEL) { |
| 858 | matrix mat; |
| 859 | |
| 860 | g3_GetUnscaledMatrix(&mat); |
| 861 | g3_GetViewPosition(&Fog_view_pos); |
| 862 | Fog_plane = mat.fvec; |
| 863 | |
| 864 | if (Polymodel_effect.fog_plane_check == 1) |
| 865 | Fog_distance = -(vm_DotProduct(&Fog_plane, &Fog_view_pos)); |
| 866 | else { |
| 867 | Fog_distance = -(vm_DotProduct(&Polymodel_fog_plane, &Polymodel_fog_portal_vert)); |
| 868 | Fog_eye_distance = (Fog_view_pos * Polymodel_fog_plane) + Fog_distance; |
| 869 | } |
| 870 | |
| 871 | rend_SetOverlayType(OT_NONE); |
| 872 | rend_SetTextureType(TT_FLAT); |
| 873 | rend_SetLighting(LS_NONE); |
| 874 | rend_SetColorModel(CM_MONO); |
| 875 | rend_SetAlphaType(AT_VERTEX); |
| 876 | rend_SetAlphaValue(255); |
| 877 | rend_SetZBufferWriteMask(0); |
| 878 | rend_SetCoplanarPolygonOffset(1); |
| 879 | rend_SetFlatColor(GR_RGB((int)(Polymodel_effect.fog_r * 255.0), (int)(Polymodel_effect.fog_g * 255.0), |
| 880 | (int)(Polymodel_effect.fog_b * 255.0))); |
| 881 | |
| 882 | for (i = 0; i < sm->num_faces; i++) { |
| 883 | polyface *fp = &sm->faces[i]; |
| 884 | |
| 885 | if (!g3_CheckNormalFacing(&sm->verts[fp->vertnums[0]], &fp->normal)) |
| 886 | continue; |
| 887 | |
| 888 | RenderSubmodelFaceFogged(pm, sm, i); |
| 889 | } |
| 890 | |
| 891 | rend_SetCoplanarPolygonOffset(0); |
| 892 | rend_SetZBufferWriteMask(1); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | // Rotates all of the points of a submodel, plus supplies color info |
| 897 | void RotateModelPoints(poly_model *pm, bsp_info *sm) { |
| 898 | |
| 899 | // Figure out lighting |
| 900 | if (Polymodel_light_type == POLYMODEL_LIGHTING_STATIC) { |
| 901 | if ((Polymodel_use_effect && (Polymodel_effect.type & PEF_DEFORM)) || (sm->flags & SOF_JITTER)) { |
| 902 | for (int i = 0; i < sm->nverts; i++) { |
| 903 | vector vec = sm->verts[i]; |
| 904 | |
| 905 | float val = ((ps_rand() % 1000) - 500.0) / 500.0; |
| 906 | vec *= 1.0 + (Polymodel_effect.deform_range * val); |
| 907 | |
| 908 | g3_RotatePoint(&Robot_points[i], &vec); |
| 909 | } |
| 910 | } else { |
| 911 | for (int i = 0; i < sm->nverts; i++) |
| 912 | g3_RotatePoint(&Robot_points[i], &sm->verts[i]); |
| 913 | } |
| 914 | } else if (Polymodel_light_type == POLYMODEL_LIGHTING_LIGHTMAP) { |
| 915 | if ((Polymodel_use_effect && (Polymodel_effect.type & PEF_DEFORM)) || (sm->flags & SOF_JITTER)) { |
| 916 | for (int i = 0; i < sm->nverts; i++) { |
| 917 | vector vec = sm->verts[i]; |
| 918 | float val = ((ps_rand() % 1000) - 500.0) / 500.0; |
| 919 | vec *= 1.0 + (Polymodel_effect.deform_range * val); |
| 920 | |
| 921 | g3_RotatePoint(&Robot_points[i], &vec); |
| 922 | |
| 923 | Robot_points[i].p3_r = 1.0; |
| 924 | Robot_points[i].p3_g = 1.0; |
| 925 | Robot_points[i].p3_b = 1.0; |
| 926 | } |
| 927 | } else { |
| 928 | for (int i = 0; i < sm->nverts; i++) { |
| 929 | g3_RotatePoint(&Robot_points[i], &sm->verts[i]); |
| 930 | Robot_points[i].p3_r = 1.0; |
| 931 | Robot_points[i].p3_g = 1.0; |
| 932 | Robot_points[i].p3_b = 1.0; |
| 933 | } |
| 934 | } |
| 935 | } else if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD) { |
| 936 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_COLOR) { |
| 937 | if ((Polymodel_use_effect && (Polymodel_effect.type & PEF_DEFORM)) || (sm->flags & SOF_JITTER)) { |
| 938 | for (int i = 0; i < sm->nverts; i++) { |
| 939 | vector vec = sm->verts[i]; |
| 940 | float val = ((ps_rand() % 1000) - 500.0) / 500.0; |
| 941 | vec *= 1.0 + (Polymodel_effect.deform_range * val); |
| 942 | |
| 943 | g3_RotatePoint(&Robot_points[i], &vec); |
| 944 | |
| 945 | vector normvec = sm->vertnorms[i]; |
| 946 | val = (-vm_DotProduct(Polymodel_light_direction, &normvec) + 1.0) / 2; |
| 947 | |
| 948 | Robot_points[i].p3_r = Polymodel_effect.r * val * Polylighting_static_red; |
| 949 | Robot_points[i].p3_g = Polymodel_effect.g * val * Polylighting_static_green; |
| 950 | Robot_points[i].p3_b = Polymodel_effect.b * val * Polylighting_static_blue; |
| 951 | } |
| 952 | } else { |
| 953 | if ((Polymodel_use_effect && (Polymodel_effect.type & PEF_DEFORM)) || (sm->flags & SOF_JITTER)) { |
| 954 | for (int i = 0; i < sm->nverts; i++) { |
| 955 | vector vec = sm->verts[i]; |
| 956 | float val = ((ps_rand() % 1000) - 500.0) / 500.0; |
| 957 | vec *= 1.0 + (Polymodel_effect.deform_range * val); |
| 958 | |
| 959 | g3_RotatePoint(&Robot_points[i], &vec); |
| 960 | |
| 961 | vector normvec = sm->vertnorms[i]; |
| 962 | val = (-vm_DotProduct(Polymodel_light_direction, &normvec) + 1.0) / 2; |
| 963 | |
| 964 | Robot_points[i].p3_r = Polymodel_effect.r * val * Polylighting_static_red; |
| 965 | Robot_points[i].p3_g = Polymodel_effect.g * val * Polylighting_static_green; |
| 966 | Robot_points[i].p3_b = Polymodel_effect.b * val * Polylighting_static_blue; |
| 967 | } |
| 968 | } else { |
| 969 | for (int i = 0; i < sm->nverts; i++) { |
| 970 | g3_RotatePoint(&Robot_points[i], &sm->verts[i]); |
| 971 | vector normvec = sm->vertnorms[i]; |
| 972 | float val = (-vm_DotProduct(Polymodel_light_direction, &normvec) + 1.0) / 2; |
| 973 | |
| 974 | Robot_points[i].p3_r = Polymodel_effect.r * val * Polylighting_static_red; |
| 975 | Robot_points[i].p3_g = Polymodel_effect.g * val * Polylighting_static_green; |
| 976 | Robot_points[i].p3_b = Polymodel_effect.b * val * Polylighting_static_blue; |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | } else { |
| 981 | if ((Polymodel_use_effect && (Polymodel_effect.type & PEF_DEFORM)) || (sm->flags & SOF_JITTER)) { |
| 982 | for (int i = 0; i < sm->nverts; i++) { |
| 983 | vector vec = sm->verts[i]; |
| 984 | float val = ((ps_rand() % 1000) - 500.0) / 500.0; |
| 985 | vec *= 1.0 + (Polymodel_effect.deform_range * val); |
| 986 | |
| 987 | g3_RotatePoint(&Robot_points[i], &vec); |
| 988 | vector normvec = sm->vertnorms[i]; |
| 989 | val = (-vm_DotProduct(Polymodel_light_direction, &normvec) + 1.0) / 2; |
| 990 | |
| 991 | Robot_points[i].p3_r = val * Polylighting_static_red; |
| 992 | Robot_points[i].p3_g = val * Polylighting_static_green; |
| 993 | Robot_points[i].p3_b = val * Polylighting_static_blue; |
| 994 | } |
| 995 | } else { |
| 996 | for (int i = 0; i < sm->nverts; i++) { |
| 997 | g3_RotatePoint(&Robot_points[i], &sm->verts[i]); |
| 998 | vector normvec = sm->vertnorms[i]; |
| 999 | float val = (-vm_DotProduct(Polymodel_light_direction, &normvec) + 1.0) / 2; |
| 1000 | |
| 1001 | Robot_points[i].p3_r = val * Polylighting_static_red; |
| 1002 | Robot_points[i].p3_g = val * Polylighting_static_green; |
| 1003 | Robot_points[i].p3_b = val * Polylighting_static_blue; |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | #ifndef RELEASE |
| 1010 | if (!UseHardware) { |
| 1011 | for (int i = 0; i < sm->nverts; i++) |
| 1012 | Robot_points[i].p3_l = Robot_points[i].p3_g; |
| 1013 | rend_SetColorModel(CM_MONO); |
| 1014 | } |
| 1015 | #endif |
| 1016 | } |
| 1017 | |
| 1018 | void RenderSubmodel(poly_model *pm, bsp_info *sm, uint32_t f_render_sub) { |
| 1019 | int i; |
| 1020 | matrix lightmatrix; |
| 1021 | |
| 1022 | // Don't render door housings |
| 1023 | if (IsNonRenderableSubmodel(pm, sm - pm->submodel)) |
| 1024 | return; |
| 1025 | |
| 1026 | if (Polymodel_light_type != POLYMODEL_LIGHTING_LIGHTMAP) { |
| 1027 | // Turn off bumpmapping if not needed |
| 1028 | rend_SetBumpmapReadyState(0, 0); |
| 1029 | } else { |
| 1030 | if (!StateLimited || UseMultitexture) |
| 1031 | rend_SetOverlayType(OT_BLEND); |
| 1032 | } |
| 1033 | |
| 1034 | if (Multicolor_texture == -1 && Polymodel_use_effect && (Polymodel_effect.type & PEF_CUSTOM_COLOR)) |
| 1035 | Multicolor_texture = FindTextureName("MultiColor"); |
| 1036 | |
| 1037 | rend_SetColorModel(CM_RGB); |
| 1038 | StartPolyModelPosInstance(&sm->mod_pos); |
| 1039 | vector temp_vec = sm->mod_pos + sm->offset; |
| 1040 | g3_StartInstanceAngles(&temp_vec, &sm->angs); |
| 1041 | |
| 1042 | vm_AnglesToMatrix(&lightmatrix, sm->angs.p, sm->angs.h, sm->angs.b); |
| 1043 | StartLightInstance(&temp_vec, &lightmatrix); |
| 1044 | |
| 1045 | // Check my bit to see if I get drawn |
| 1046 | if (f_render_sub & (0x00000001 << (sm - pm->submodel))) { |
| 1047 | if (sm->flags & SOF_CUSTOM) { |
| 1048 | if (!(Polymodel_effect.type & PEF_CUSTOM_TEXTURE)) |
| 1049 | goto pop_lighting; |
| 1050 | } |
| 1051 | |
| 1052 | // Check to draw glow faces |
| 1053 | if (sm->flags & (SOF_GLOW | SOF_THRUSTER)) { |
| 1054 | if (!FacingPass) |
| 1055 | goto pop_lighting; |
| 1056 | |
| 1057 | vector zero_pos = {0, 0, 0}; |
| 1058 | rend_SetOverlayType(OT_NONE); |
| 1059 | |
| 1060 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_GLOW_SCALAR) { |
| 1061 | if (Polymodel_effect.type & PEF_CUSTOM_GLOW) |
| 1062 | DrawThrusterEffect(&zero_pos, Polymodel_effect.glow_r, Polymodel_effect.glow_g, Polymodel_effect.glow_b, |
| 1063 | &sm->glow_info->normal, sm->glow_info->glow_size * Polymodel_effect.glow_size_scalar, |
| 1064 | 3 * Polymodel_effect.glow_length_scalar); |
| 1065 | else |
| 1066 | DrawThrusterEffect(&zero_pos, sm->glow_info->glow_r, sm->glow_info->glow_g, sm->glow_info->glow_b, |
| 1067 | &sm->glow_info->normal, sm->glow_info->glow_size * Polymodel_effect.glow_size_scalar, |
| 1068 | 3 * Polymodel_effect.glow_length_scalar); |
| 1069 | } else { |
| 1070 | if (Polymodel_use_effect && Polymodel_effect.type & PEF_CUSTOM_GLOW) |
| 1071 | DrawGlowEffect(&zero_pos, Polymodel_effect.glow_r, Polymodel_effect.glow_g, Polymodel_effect.glow_b, |
| 1072 | &sm->glow_info->normal, sm->glow_info->glow_size); |
| 1073 | else |
| 1074 | DrawGlowEffect(&zero_pos, sm->glow_info->glow_r, sm->glow_info->glow_g, sm->glow_info->glow_b, |
| 1075 | &sm->glow_info->normal, sm->glow_info->glow_size); |
| 1076 | } |
| 1077 | |
| 1078 | goto pop_lighting; |
| 1079 | } else if (sm->flags & SOF_FACING) { |
| 1080 | if (!FacingPass) |
| 1081 | goto pop_lighting; |
| 1082 | |
| 1083 | vector pos; |
| 1084 | rend_SetLighting(LS_NONE); |
| 1085 | rend_SetColorModel(CM_MONO); |
| 1086 | rend_SetOverlayType(OT_NONE); |
| 1087 | |
| 1088 | int bm_handle = GetTextureBitmap(pm->textures[sm->faces[0].texnum], 0); |
| 1089 | rend_SetAlphaValue(GameTextures[pm->textures[sm->faces[0].texnum]].alpha * 255); |
| 1090 | |
| 1091 | vm_MakeZero(&pos); |
| 1092 | |
| 1093 | if (GameTextures[pm->textures[sm->faces[0].texnum]].flags & TF_SATURATE) |
| 1094 | rend_SetAlphaType(AT_SATURATE_TEXTURE); |
| 1095 | else |
| 1096 | rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE); |
| 1097 | |
| 1098 | rend_SetZBufferWriteMask(0); |
| 1099 | g3_DrawBitmap(&pos, sm->rad, (sm->rad * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle); |
| 1100 | rend_SetZBufferWriteMask(1); |
| 1101 | |
| 1102 | goto pop_lighting; |
| 1103 | } else { |
| 1104 | if (FacingPass) |
| 1105 | goto pop_lighting; |
| 1106 | } |
| 1107 | |
| 1108 | RotateModelPoints(pm, sm); |
| 1109 | |
| 1110 | if (!UseHardware) |
| 1111 | RenderSubmodelFacesSorted(pm, sm); |
| 1112 | else |
| 1113 | RenderSubmodelFacesUnsorted(pm, sm); |
| 1114 | } |
| 1115 | |
| 1116 | pop_lighting: |
| 1117 | |
| 1118 | for (i = 0; i < sm->num_children; i++) { |
| 1119 | RenderSubmodel(pm, &pm->submodel[sm->children[i]], f_render_sub); |
| 1120 | } |
| 1121 | |
| 1122 | g3_DoneInstance(); |
| 1123 | DonePolyModelPosInstance(); |
| 1124 | DoneLightInstance(); |
| 1125 | } |
| 1126 | |
| 1127 | int RenderPolygonModel(poly_model *pm, uint32_t f_render_sub) { |
| 1128 | ASSERT(pm->new_style == 1); |
| 1129 | int i = 0; |
| 1130 | |
| 1131 | rend_SetAlphaType(ATF_CONSTANT + ATF_VERTEX); |
| 1132 | rend_SetWrapType(WT_WRAP); |
| 1133 | |
| 1134 | FacingPass = 0; |
| 1135 | for (i = 0; i < pm->n_models; i++) { |
| 1136 | bsp_info *sm = &pm->submodel[i]; |
| 1137 | if (sm->parent == -1) |
| 1138 | RenderSubmodel(pm, sm, f_render_sub); |
| 1139 | } |
| 1140 | |
| 1141 | // Now render any facing submodels |
| 1142 | if (pm->flags & PMF_FACING) { |
| 1143 | // Don't render if we have it set for no glows |
| 1144 | FacingPass = 1; |
| 1145 | rend_SetOverlayType(OT_NONE); |
| 1146 | for (i = 0; i < pm->n_models; i++) { |
| 1147 | bsp_info *sm = &pm->submodel[i]; |
| 1148 | if (sm->parent == -1) |
| 1149 | RenderSubmodel(pm, sm, f_render_sub); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | FacingPass = 0; |
| 1154 | |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
| 1158 | float ComputeDefaultSizeFunc(int handle, float *size_ptr, vector *offset_ptr, bool f_use_all_frames) { |
| 1159 | poly_model *pm; |
| 1160 | matrix m; |
| 1161 | float normalized_time[MAX_SUBOBJECTS]; |
| 1162 | int model_index, sm_vert_index, frame_index; |
| 1163 | float cur_dist; |
| 1164 | float size = 0.0; |
| 1165 | int start_frame = 0; |
| 1166 | int end_frame = 0; |
| 1167 | |
| 1168 | vector geometric_center = Zero_vector; |
| 1169 | |
| 1170 | // Chris: Come see me when you are ready to deal with the paging problem - JL |
| 1171 | pm = GetPolymodelPointer(handle); |
| 1172 | |
| 1173 | ASSERT(start_frame <= end_frame); |
| 1174 | ASSERT(end_frame <= pm->frame_max); |
| 1175 | |
| 1176 | if (f_use_all_frames) { |
| 1177 | end_frame = pm->frame_max; |
| 1178 | } |
| 1179 | |
| 1180 | if (offset_ptr) { |
| 1181 | vector min_xyz; |
| 1182 | vector max_xyz; |
| 1183 | bool first_pnt = true; |
| 1184 | |
| 1185 | for (frame_index = start_frame; frame_index <= end_frame; frame_index++) { |
| 1186 | // Because size changes with animation, we need the worst case point -- so, check every keyframe |
| 1187 | // NOTE: This code does not currently account for all $turret and $rotate positions |
| 1188 | |
| 1189 | SetNormalizedTimeAnim(frame_index, normalized_time, pm); |
| 1190 | |
| 1191 | SetModelAnglesAndPos(pm, normalized_time); |
| 1192 | |
| 1193 | for (model_index = 0; model_index < pm->n_models; model_index++) { |
| 1194 | bsp_info *sm = &pm->submodel[model_index]; |
| 1195 | |
| 1196 | // For every vertex |
| 1197 | for (sm_vert_index = 0; sm_vert_index < sm->nverts; sm_vert_index++) { |
| 1198 | vector pnt; |
| 1199 | int mn; |
| 1200 | |
| 1201 | // Get the point and its current sub-object |
| 1202 | pnt = sm->verts[sm_vert_index]; |
| 1203 | mn = model_index; |
| 1204 | |
| 1205 | // Instance up the tree |
| 1206 | while (mn != -1) { |
| 1207 | vector tpnt; |
| 1208 | |
| 1209 | vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b); |
| 1210 | vm_TransposeMatrix(&m); |
| 1211 | |
| 1212 | tpnt = pnt * m; |
| 1213 | |
| 1214 | pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos; |
| 1215 | |
| 1216 | mn = pm->submodel[mn].parent; |
| 1217 | } |
| 1218 | |
| 1219 | // Maybe use for other code -- Accounts for world coordinates |
| 1220 | // m = obj->orient; |
| 1221 | // vm_TransposeMatrix(&m); |
| 1222 | // |
| 1223 | // pnt = pnt * m; |
| 1224 | // |
| 1225 | // *gun_point += obj->pos; |
| 1226 | |
| 1227 | // Find the min_xyz and max_xyz |
| 1228 | if (first_pnt) { |
| 1229 | first_pnt = false; |
| 1230 | min_xyz = max_xyz = pnt; |
| 1231 | } else { |
| 1232 | if (pnt.x < min_xyz.x) |
| 1233 | min_xyz.x = pnt.x; |
| 1234 | else if (pnt.x > max_xyz.x) |
| 1235 | max_xyz.x = pnt.x; |
| 1236 | |
| 1237 | if (pnt.y < min_xyz.y) |
| 1238 | min_xyz.y = pnt.y; |
| 1239 | else if (pnt.y > max_xyz.y) |
| 1240 | max_xyz.y = pnt.y; |
| 1241 | |
| 1242 | if (pnt.z < min_xyz.z) |
| 1243 | min_xyz.z = pnt.z; |
| 1244 | else if (pnt.z > max_xyz.z) |
| 1245 | max_xyz.z = pnt.z; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | geometric_center = (max_xyz + min_xyz) / 2.0; |
| 1252 | *offset_ptr = geometric_center; |
| 1253 | } |
| 1254 | |
| 1255 | for (frame_index = start_frame; frame_index <= end_frame; frame_index++) { |
| 1256 | // Because size changes with animation, we need the worst case point -- so, check every keyframe |
| 1257 | // NOTE: This code does not currently account for all $turret and $rotate positions |
| 1258 | |
| 1259 | SetNormalizedTimeAnim(frame_index, normalized_time, pm); |
| 1260 | |
| 1261 | SetModelAnglesAndPos(pm, normalized_time); |
| 1262 | |
| 1263 | for (model_index = 0; model_index < pm->n_models; model_index++) { |
| 1264 | bsp_info *sm = &pm->submodel[model_index]; |
| 1265 | |
| 1266 | // For every vertex |
| 1267 | for (sm_vert_index = 0; sm_vert_index < sm->nverts; sm_vert_index++) { |
| 1268 | vector pnt; |
| 1269 | int mn; |
| 1270 | |
| 1271 | // Get the point and its current sub-object |
| 1272 | pnt = sm->verts[sm_vert_index]; |
| 1273 | mn = model_index; |
| 1274 | |
| 1275 | // Instance up the tree |
| 1276 | while (mn != -1) { |
| 1277 | vector tpnt; |
| 1278 | |
| 1279 | vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b); |
| 1280 | vm_TransposeMatrix(&m); |
| 1281 | |
| 1282 | tpnt = pnt * m; |
| 1283 | |
| 1284 | pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos; |
| 1285 | |
| 1286 | mn = pm->submodel[mn].parent; |
| 1287 | } |
| 1288 | |
| 1289 | // Maybe use for other code -- Accounts for world coordinates |
| 1290 | // m = obj->orient; |
| 1291 | // vm_TransposeMatrix(&m); |
| 1292 | // |
| 1293 | // pnt = pnt * m; |
| 1294 | // |
| 1295 | // *gun_point += obj->pos; |
| 1296 | |
| 1297 | cur_dist = vm_VectorDistance(&geometric_center, &pnt); |
| 1298 | if (cur_dist > size) |
| 1299 | size = cur_dist; |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | // This is a arbitary value. It allows for some turret and rotations to be caught |
| 1305 | size = size + 0.01f; |
| 1306 | |
| 1307 | if (size_ptr) // DAJ |
| 1308 | *size_ptr = size; |
| 1309 | |
| 1310 | return size; |
| 1311 | } |
| 1312 | |
| 1313 | float ComputeDefaultSize(int type, int handle, float *size_ptr) { |
| 1314 | float size = ComputeDefaultSizeFunc(handle, size_ptr, nullptr, true); |
| 1315 | |
| 1316 | if (type != OBJ_WEAPON && type != OBJ_DEBRIS && type != OBJ_POWERUP) { |
| 1317 | ComputeDefaultSizeFunc(handle, &Poly_models[handle].wall_size, &Poly_models[handle].wall_size_offset, false); |
| 1318 | ComputeDefaultSizeFunc(handle, &Poly_models[handle].anim_size, &Poly_models[handle].anim_size_offset, true); |
| 1319 | |
| 1320 | if (type == OBJ_PLAYER) { |
| 1321 | Poly_models[handle].anim_size *= PLAYER_SIZE_SCALAR; |
| 1322 | Poly_models[handle].anim_size_offset = Zero_vector; |
| 1323 | } |
| 1324 | } else { |
| 1325 | if (type == OBJ_POWERUP) { |
| 1326 | size *= 2.0f; |
| 1327 | *size_ptr *= 2.0f; |
| 1328 | } |
| 1329 | |
| 1330 | Poly_models[handle].wall_size = size; |
| 1331 | Poly_models[handle].wall_size_offset = Zero_vector; |
| 1332 | |
| 1333 | Poly_models[handle].anim_size = size; |
| 1334 | Poly_models[handle].anim_size_offset = Zero_vector; |
| 1335 | } |
| 1336 | |
| 1337 | Poly_models[handle].flags |= PMF_SIZE_COMPUTED; |
| 1338 | |
| 1339 | return size; |
| 1340 | } |