1 /* 2 Copyright (C) 1997-2001 Id Software, Inc. 3 4 This program is free software; you can redistribute it and/or 5 modify it under the terms of the GNU General Public License 6 as published by the Free Software Foundation; either version 2 7 of the License, or (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 13 See the 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, write to the Free Software 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 */ 20 21 #include "q_shared.h" 22 #include "qfiles.h" 23 #include "com_public.h" 24 #include "ref_public.h" 25 #include "in_public.h" 26 #include "vid_public.h" 27 #include "q_list.h" 28 #include "r_shared.h" 29 30 #define REF_VERSION "SOFT 0.01" 31 32 //=================================================================== 33 34 typedef unsigned char pixel_t; 35 36 typedef struct vrectSoft_s { 37 int x, y, width, height; 38 int dummy; 39 } vrectSoft_t; 40 41 typedef struct { 42 pixel_t *buffer; // invisible buffer 43 pixel_t *colormap; // 256 * VID_GRADES size 44 pixel_t *alphamap; // 256 * 256 translucency map 45 int rowbytes; // may be > width if displayed in a window 46 // can be negative for stupid dibs 47 int width; 48 int height; 49 } viddef_t; 50 51 extern viddef_t vid; 52 53 // !!! if this is changed, it must be changed in asm_draw.h too !!! 54 typedef struct { 55 vrectSoft_t vrect; // subwindow in video for refresh 56 // FIXME: not need vrect next field here? 57 vrectSoft_t aliasvrect; // scaled Alias version 58 int vrectright, vrectbottom; // right & bottom screen coords 59 int aliasvrectright, aliasvrectbottom; // scaled Alias versions 60 float vrectrightedge; // rightmost right edge we care about, 61 // for use in edge list 62 float fvrectx, fvrecty; // for floating-point compares 63 float fvrectx_adj, fvrecty_adj; // left and top edges, for clamping 64 int vrect_x_adj_shift20; // (vrect.x + 0.5 - epsilon) << 20 65 int vrectright_adj_shift20; // (vrectright + 0.5 - epsilon) << 20 66 float fvrectright_adj, fvrectbottom_adj; 67 // right and bottom edges, for clamping 68 float fvrectright; // rightmost edge, for Alias clamping 69 float fvrectbottom; // bottommost edge, for Alias clamping 70 float horizontalFieldOfView; // at Z = 1.0, this many X is visible 71 // 2.0 = 90 degrees 72 float xOrigin; // should probably always be 0.5 73 float yOrigin; // between be around 0.3 to 0.5 74 75 vec3_t vieworg; 76 vec3_t viewangles; 77 78 int ambientlight; 79 } oldrefdef_t; 80 81 extern oldrefdef_t r_refdef; 82 83 #include "sw_model.h" 84 85 /* 86 ==================================================== 87 88 CONSTANTS 89 90 ==================================================== 91 */ 92 93 #define CACHE_SIZE 32 94 95 #define VID_CBITS 6 96 #define VID_GRADES (1 << VID_CBITS) 97 98 99 // r_shared.h: general refresh-related stuff shared between the refresh and the 100 // driver 101 102 103 #define MAXVERTS 64 // max points in a surface polygon 104 #define MAXWORKINGVERTS (MAXVERTS+4) // max points in an intermediate 105 // polygon (while processing) 106 // !!! if this is changed, it must be changed in d_ifacea.h too !!! 107 #define MAXHEIGHT 1200 108 #define MAXWIDTH 1600 109 110 #define INFINITE_DISTANCE 0x10000 // distance that's always guaranteed to 111 // be farther away than anything in 112 // the scene 113 114 115 // d_iface.h: interface header file for rasterization driver modules 116 117 #define WARP_WIDTH 320 118 #define WARP_HEIGHT 240 119 120 #define MAX_LBM_HEIGHT 480 121 122 123 #define PARTICLE_Z_CLIP 8.0 124 125 // !!! must be kept the same as in quakeasm.h !!! 126 #define TRANSPARENT_COLOR 0xFF 127 128 129 // !!! if this is changed, it must be changed in d_ifacea.h too !!! 130 #define TURB_TEX_SIZE 64 // base turbulent texture size 131 132 // !!! if this is changed, it must be changed in d_ifacea.h too !!! 133 #define CYCLE 128 // turbulent cycle size 134 135 #define SCANBUFFERPAD 0x1000 136 137 #define DS_SPAN_LIST_END -128 138 139 #define NUMSTACKEDGES 3000 140 #define MINEDGES NUMSTACKEDGES 141 #define NUMSTACKSURFACES 1000 142 #define MINSURFACES NUMSTACKSURFACES 143 #define MAXSPANS 3000 144 145 // flags in finalvert_t.flags 146 #define ALIAS_LEFT_CLIP 0x0001 147 #define ALIAS_TOP_CLIP 0x0002 148 #define ALIAS_RIGHT_CLIP 0x0004 149 #define ALIAS_BOTTOM_CLIP 0x0008 150 #define ALIAS_Z_CLIP 0x0010 151 #define ALIAS_XY_CLIP_MASK 0x000F 152 153 #define SURFCACHE_SIZE_AT_320X240 1024*768 154 155 #define BMODEL_FULLY_CLIPPED 0x10 // value returned by R_BmodelCheckBBox () 156 // if bbox is trivially rejected 157 158 #define XCENTERING (1.0 / 2.0) 159 #define YCENTERING (1.0 / 2.0) 160 161 #define CLIP_EPSILON 0.001 162 163 #define BACKFACE_EPSILON 0.01 164 165 // !!! if this is changed, it must be changed in asm_draw.h too !!! 166 #define NEAR_CLIP 0.01 167 168 169 #define MAXALIASVERTS 2000 // TODO: tune this 170 #define ALIAS_Z_CLIP_PLANE 4 171 172 // turbulence stuff 173 174 #define AMP 8*0x10000 175 #define AMP2 3 176 #define SPEED 20 177 178 179 /* 180 ==================================================== 181 182 TYPES 183 184 ==================================================== 185 */ 186 187 typedef struct { 188 float u, v; 189 float s, t; 190 float zi; 191 } emitpoint_t; 192 193 /* 194 ** if you change this structure be sure to change the #defines 195 ** listed after it! 196 */ 197 typedef struct finalvert_s { 198 int u, v, s, t; 199 int l; 200 int zi; 201 int flags; 202 float xyz[3]; // eye space 203 } finalvert_t; 204 205 #define FINALVERT_V0 0 206 #define FINALVERT_V1 4 207 #define FINALVERT_V2 8 208 #define FINALVERT_V3 12 209 #define FINALVERT_V4 16 210 #define FINALVERT_V5 20 211 #define FINALVERT_FLAGS 24 212 #define FINALVERT_X 28 213 #define FINALVERT_Y 32 214 #define FINALVERT_Z 36 215 #define FINALVERT_SIZE 40 216 217 typedef struct { 218 void *pskin; 219 int pskindesc; 220 int skinwidth; 221 int skinheight; 222 dtriangle_t *ptriangles; 223 finalvert_t *pfinalverts; 224 int numtriangles; 225 int drawtype; 226 int seamfixupX16; 227 qboolean do_vis_thresh; 228 int vis_thresh; 229 } affinetridesc_t; 230 231 typedef struct drawsurf_s { 232 byte *surfdat; // destination for generated surface 233 int rowbytes; // destination logical width in bytes 234 msurface_t *surf; // description for surface to generate 235 fixed8_t lightadj[MAXLIGHTMAPS]; 236 // adjust for lightmap levels for dynamic lighting 237 image_t *image; 238 int surfmip; // mipmapped ratio of surface texels / world pixels 239 int surfwidth; // in mipmapped texels 240 int surfheight; // in mipmapped texels 241 } drawsurf_t; 242 243 typedef struct { 244 int ambientlight; 245 int shadelight; 246 float *plightvec; 247 } alight_t; 248 249 // clipped bmodel edges 250 251 typedef struct bedge_s { 252 mvertex_t *v[2]; 253 struct bedge_s *pnext; 254 } bedge_t; 255 256 257 // !!! if this is changed, it must be changed in asm_draw.h too !!! 258 typedef struct clipplane_s { 259 vec3_t normal; 260 float dist; 261 struct clipplane_s *next; 262 byte leftedge; 263 byte rightedge; 264 byte reserved[2]; 265 } clipplane_t; 266 267 #ifdef TRUECOLOR_RENDERER 268 #define MAX_BLOCKLIGHTS 4096 269 #define LIGHTMAP_BYTES 3 270 #define blocklight_t short 271 #else 272 #define MAX_BLOCKLIGHTS 1024 273 #define LIGHTMAP_BYTES 1 274 #define blocklight_t int 275 #endif 276 277 typedef struct surfcache_s { 278 struct surfcache_s *next; 279 struct surfcache_s **owner; // NULL is an empty chunk of memory 280 int lightadj[MAXLIGHTMAPS]; // checked for strobe flush 281 int dlight; 282 int size; // including header 283 uint32 width; 284 uint32 height; // DEBUG only needed for debug 285 float mipscale; 286 image_t *image; 287 byte data[4]; // width*height elements 288 } surfcache_t; 289 290 // !!! if this is changed, it must be changed in asm_draw.h too !!! 291 typedef struct espan_s { 292 int u, v, count; 293 struct espan_s *pnext; 294 } espan_t; 295 296 // used by the polygon drawer (R_POLY.C) and sprite setup code (R_SPRITE.C) 297 typedef struct { 298 int nump; 299 emitpoint_t *pverts; 300 byte *pixels; // image 301 int pixel_width; // image width 302 int pixel_height; // image height 303 vec3_t vup, vright, vpn; // in worldspace, for plane eq 304 float dist; 305 float s_offset, t_offset; 306 float viewer_position[3]; 307 void (*drawspanlet)( void ); 308 int stipple_parity; 309 } polydesc_t; 310 311 // FIXME: compress, make a union if that will help 312 // insubmodel is only 1, flags is fewer than 32, spanstate could be a byte 313 typedef struct surf_s { 314 struct surf_s *next; // active surface stack in r_edge.c 315 struct surf_s *prev; // used in r_edge.c for active surf stack 316 struct espan_s *spans; // pointer to linked list of spans to draw 317 int key; // sorting key (BSP order) 318 int last_u; // set during tracing 319 int spanstate; // 0 = not in span 320 // 1 = in span 321 // -1 = in inverted span (end before 322 // start) 323 int flags; // currentface flags 324 msurface_t *msurf; 325 entity_t *entity; 326 float nearzi; // nearest 1/z on surface, for mipmapping 327 qboolean insubmodel; 328 float d_ziorigin, d_zistepu, d_zistepv; 329 330 int pad[2]; // to 64 bytes 331 } surf_t; 332 333 // !!! if this is changed, it must be changed in asm_draw.h too !!! 334 typedef struct edge_s { 335 fixed16_t u; 336 fixed16_t u_step; 337 struct edge_s *prev, *next; 338 uint16 surfs[2]; 339 struct edge_s *nextremove; 340 float nearzi; 341 medge_t *owner; 342 } edge_t; 343 344 345 /* 346 ==================================================== 347 348 VARS 349 350 ==================================================== 351 */ 352 353 extern int d_spanpixcount; 354 extern int r_framecount; // sequence # of current frame since Quake 355 // started 356 extern float r_aliasuvscale; // scale-up factor for screen u and v 357 // on Alias vertices passed to driver 358 extern qboolean r_dowarp; 359 360 extern affinetridesc_t r_affinetridesc; 361 362 extern vec3_t r_pright, r_pup, r_ppn; 363 364 void D_DrawSurfaces (void); 365 void R_DrawParticle( void ); 366 void D_ViewChanged (void); 367 void D_WarpScreen (void); 368 void R_PolysetUpdateTables (void); 369 370 //=======================================================================// 371 372 // callbacks to Quake 373 374 extern drawsurf_t r_drawsurf; 375 376 void R_DrawSurface (void); 377 378 extern int c_surf; 379 380 extern byte r_warpbuffer[WARP_WIDTH * WARP_HEIGHT]; 381 382 extern float scale_for_mip; 383 384 extern qboolean d_roverwrapped; 385 extern surfcache_t *sc_rover; 386 extern surfcache_t *d_initial_rover; 387 388 extern float d_sdivzstepu, d_tdivzstepu, d_zistepu; 389 extern float d_sdivzstepv, d_tdivzstepv, d_zistepv; 390 extern float d_sdivzorigin, d_tdivzorigin, d_ziorigin; 391 392 extern fixed16_t sadjust, tadjust; 393 extern fixed16_t bbextents, bbextentt; 394 395 396 void D_DrawSpans16 (espan_t *pspans); 397 void D_DrawZSpans (espan_t *pspans); 398 void Turbulent8 (espan_t *pspan); 399 void NonTurbulent8 (espan_t *pspan); //PGM 400 401 surfcache_t *D_CacheSurface (msurface_t *surface, int miplevel); 402 403 extern int d_vrectx, d_vrecty, d_vrectright_particle, d_vrectbottom_particle; 404 405 extern int d_pix_min, d_pix_max, d_pix_shift; 406 407 extern pixel_t *d_viewbuffer; 408 extern short *d_pzbuffer; 409 extern unsigned int d_zrowbytes, d_zwidth; 410 extern short *zspantable[MAXHEIGHT]; 411 extern int d_scantable[MAXHEIGHT]; 412 413 extern int d_minmip; 414 extern float d_scalemip[3]; 415 416 //=================================================================== 417 418 extern int cachewidth; 419 extern pixel_t *cacheblock; 420 extern int r_screenwidth; 421 422 extern int r_drawnpolycount; 423 424 extern int sintable[CYCLE*2]; 425 extern int intsintable[CYCLE*2]; 426 extern int blanktable[CYCLE*2]; // PGM 427 428 extern vec3_t vup, base_vup; 429 extern vec3_t vpn, base_vpn; 430 extern vec3_t vright, base_vright; 431 432 extern surf_t *surfaces, *surface_p, *surf_max; 433 434 // surfaces are generated in back to front order by the bsp, so if a surf 435 // pointer is greater than another one, it should be drawn in front 436 // surfaces[1] is the background, and is used as the active surface stack. 437 // surfaces[0] is a dummy, because index 0 is used to indicate no surface 438 // attached to an edge_t 439 440 //=================================================================== 441 442 extern vec3_t sxformaxis[4]; // s axis transformed into viewspace 443 extern vec3_t txformaxis[4]; // t axis transformed into viewspac 444 445 extern float xcenter, ycenter; 446 extern float xscale, yscale; 447 extern float xscaleinv, yscaleinv; 448 extern float xscaleshrink, yscaleshrink; 449 450 extern void TransformVector (vec3_t in, vec3_t out); 451 extern void SetUpForLineScan(fixed8_t startvertu, fixed8_t startvertv, 452 fixed8_t endvertu, fixed8_t endvertv); 453 454 extern int ubasestep, errorterm, erroradjustup, erroradjustdown; 455 456 //=========================================================================== 457 458 extern cvar_t *sw_aliasstats; 459 extern cvar_t *sw_clearcolor; 460 extern cvar_t *sw_drawflat; 461 extern cvar_t *sw_draworder; 462 extern cvar_t *sw_maxedges; 463 extern cvar_t *sw_maxsurfs; 464 extern cvar_t *sw_mipcap; 465 extern cvar_t *sw_mipscale; 466 extern cvar_t *sw_mode; 467 extern cvar_t *sw_reportsurfout; 468 extern cvar_t *sw_reportedgeout; 469 extern cvar_t *sw_stipplealpha; 470 extern cvar_t *sw_surfcacheoverride; 471 extern cvar_t *sw_waterwarp; 472 extern cvar_t *sw_drawsird; 473 474 extern cvar_t *r_fullbright; 475 extern cvar_t *r_drawentities; 476 extern cvar_t *r_drawworld; 477 extern cvar_t *r_dspeeds; 478 extern cvar_t *r_lerpmodels; 479 480 extern cvar_t *r_speeds; 481 482 extern cvar_t *vid_fullscreen; 483 extern cvar_t *vid_gamma; 484 485 486 extern clipplane_t view_clipplanes[4]; 487 extern int *pfrustum_indexes[4]; 488 489 490 //============================================================================= 491 492 void R_RenderWorld (void); 493 494 //============================================================================= 495 496 extern mplane_t screenedge[4]; 497 498 extern vec3_t r_origin; 499 500 extern entity_t r_worldentity; 501 extern model_t *currentmodel; 502 extern entity_t *currententity; 503 extern vec3_t modelorg; 504 extern vec3_t r_entorigin; 505 506 extern float verticalFieldOfView; 507 extern float xOrigin, yOrigin; 508 509 extern int r_visframecount; 510 511 extern msurface_t *r_alpha_surfaces; 512 513 //============================================================================= 514 515 void R_ClearPolyList (void); 516 void R_DrawPolyList (void); 517 518 // 519 // current entity info 520 // 521 extern qboolean insubmodel; 522 523 void R_DrawAlphaSurfaces( void ); 524 525 void R_DrawSprite (void); 526 void R_DrawBeam( entity_t *e ); 527 528 void R_RenderFace (msurface_t *fa, int clipflags); 529 void R_RenderBmodelFace (bedge_t *pedges, msurface_t *psurf); 530 void R_TransformPlane (mplane_t *p, float *normal, float *dist); 531 void R_TransformFrustum (void); 532 void R_DrawSurfaceBlock16 (void); 533 void R_DrawSurfaceBlock8 (void); 534 535 void R_GenSkyTile (void *pdest); 536 void R_GenSkyTile16 (void *pdest); 537 void R_Surf8Patch (void); 538 void R_Surf16Patch (void); 539 void R_DrawSubmodelPolygons (model_t *pmodel, int clipflags, mnode_t *topnode); 540 void R_DrawSolidClippedSubmodelPolygons (model_t *pmodel, mnode_t *topnode); 541 542 void R_AddPolygonEdges (emitpoint_t *pverts, int numverts, int miplevel); 543 surf_t *R_GetSurf (void); 544 void R_AliasDrawModel (void); 545 void R_BeginEdgeFrame (void); 546 void R_ScanEdges (void); 547 void D_DrawSurfaces (void); 548 void R_InsertNewEdges (edge_t *edgestoadd, edge_t *edgelist); 549 void R_StepActiveU (edge_t *pedge); 550 void R_RemoveEdges (edge_t *pedge); 551 void R_PushDlights (model_t *model); 552 553 extern void R_Surf8Start (void); 554 extern void R_Surf8End (void); 555 extern void R_Surf16Start (void); 556 extern void R_Surf16End (void); 557 extern void R_EdgeCodeStart (void); 558 extern void R_EdgeCodeEnd (void); 559 560 extern void R_RotateBmodel (void); 561 562 extern int c_faceclip; 563 extern int r_polycount; 564 extern int r_wholepolycount; 565 566 extern int ubasestep, errorterm, erroradjustup, erroradjustdown; 567 568 extern fixed16_t sadjust, tadjust; 569 extern fixed16_t bbextents, bbextentt; 570 571 extern mvertex_t *r_ptverts, *r_ptvertsmax; 572 573 extern float entity_rotation[3][3]; 574 575 extern int r_currentkey; 576 extern int r_currentbkey; 577 578 void R_InitTurb (void); 579 580 void R_DrawParticles (void); 581 void R_SurfacePatch (void); 582 583 void R_BuildGammaTable( void ); 584 585 extern int r_amodels_drawn; 586 extern edge_t *auxedges; 587 extern int r_numallocatededges; 588 extern edge_t *r_edges, *edge_p, *edge_max; 589 590 extern edge_t *newedges[MAXHEIGHT]; 591 extern edge_t *removeedges[MAXHEIGHT]; 592 593 // FIXME: make stack vars when debugging done 594 extern edge_t edge_head; 595 extern edge_t edge_tail; 596 extern edge_t edge_aftertail; 597 598 #ifdef TRUECOLOR_RENDERER 599 color_t r_aliasblendcolor; 600 byte *r_aliasAlphaTable, *r_aliasOneMinusAlphaTable; 601 #else 602 extern int r_aliasblendcolor; 603 #endif 604 605 extern float aliasxscale, aliasyscale, aliasxcenter, aliasycenter; 606 607 extern int r_outofsurfaces; 608 extern int r_outofedges; 609 610 extern mvertex_t *r_pcurrentvertbase; 611 extern int r_maxvalidedgeoffset; 612 613 typedef struct { 614 finalvert_t *a, *b, *c; 615 } aliastriangleparms_t; 616 617 extern aliastriangleparms_t aliastriangleparms; 618 619 void R_DrawTriangle( void ); 620 void R_AliasClipTriangle (finalvert_t *index0, finalvert_t *index1, finalvert_t *index2); 621 622 623 extern float r_time1; 624 extern float da_time1, da_time2; 625 extern float dp_time1, dp_time2, db_time1, db_time2, rw_time1, rw_time2; 626 extern float se_time1, se_time2, de_time1, de_time2, dv_time1, dv_time2; 627 extern int r_frustum_indexes[4*6]; 628 extern int r_maxsurfsseen, r_maxedgesseen, r_cnumsurfs; 629 extern qboolean r_surfsonstack; 630 631 extern mleaf_t *r_viewleaf; 632 extern int r_viewcluster, r_oldviewcluster; 633 634 extern int r_clipflags; 635 extern int r_dlightframecount; 636 extern qboolean r_fov_greater_than_90; 637 638 extern image_t *r_notexture_mip; 639 extern model_t *r_worldmodel; 640 641 void R_PrintAliasStats (void); 642 void R_PrintTimes (void); 643 void R_PrintDSpeeds (void); 644 void R_AnimateLight (void); 645 void R_LightPoint (vec3_t p, vec3_t color); 646 void R_SetupFrame (void); 647 void R_cshift_f (void); 648 void R_EmitEdge (mvertex_t *pv0, mvertex_t *pv1); 649 void R_ClipEdge (mvertex_t *pv0, mvertex_t *pv1, clipplane_t *clip); 650 void R_SplitEntityOnNode2 (mnode_t *node); 651 652 extern refdef_t r_newrefdef; 653 654 extern surfcache_t *sc_rover, *sc_base; 655 656 //==================================================================== 657 658 float R_DLightPoint (vec3_t p); 659 660 void R_NewMap (void); 661 void R_Register (void); 662 void R_UnRegister (void); 663 void Draw_Init (void); 664 qboolean R_Init( qboolean total ); 665 void R_Shutdown( qboolean total ); 666 void R_InitCaches (void); 667 void D_FlushCaches (void); 668 669 qhandle_t R_RegisterModel( const char *name ); 670 671 void R_ScreenShot_f( void ); 672 673 void R_RenderFrame (refdef_t *fd); 674 675 void R_BeginFrame( float camera_separation ); 676 677 void R_CinematicSetPalette( const unsigned char *palette ); 678 679 void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length); 680 void Sys_SetFPCW (void); 681 682 void R_InitImages (void); 683 void R_ShutdownImages (void); 684 685 void R_GammaCorrectAndSetPalette( const byte *pal ); 686 687 extern mtexinfo_t *sky_texinfo[6]; 688 689 void R_InitSkyBox (void); 690 691 void R_ApplySIRDAlgorithum( void ); 692 693 typedef struct swstate_s { 694 qboolean fullscreen; 695 int prev_mode; // last valid SW mode 696 697 byte gammatable[256]; 698 byte currentpalette[1024]; 699 } swstate_t; 700 701 void R_IMFlatShadedQuad( vec3_t a, vec3_t b, vec3_t c, vec3_t d, int color, float alpha ); 702 703 int R_IndexForColor( const color_t color ); 704 image_t *R_ImageForHandle( qhandle_t hPic ); 705 706 void Draw_Fill (int x, int y, int w, int h, int c); 707 708 extern swstate_t sw_state; 709 710 711 712 713