/* Emacs style mode select -*- C -*- *----------------------------------------------------------------------------- * * * PrBoom: a Doom port merged with LxDoom and LSDLDoom * based on BOOM, a modified and improved DOOM engine * Copyright (C) 1999 by * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman * Copyright (C) 1999-2000 by * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze * Copyright 2005, 2006 by * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * DESCRIPTION: * Rendering main loop and setup functions, * utility functions (BSP, geometry, trigonometry). * See tables.c, too. * *-----------------------------------------------------------------------------*/ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "SDL.h" #include "doomstat.h" #include "d_net.h" #include "w_wad.h" #include "r_main.h" #include "r_things.h" #include "r_plane.h" #include "r_bsp.h" #include "r_draw.h" #include "m_bbox.h" #include "r_sky.h" #include "v_video.h" #include "lprintf.h" #include "st_stuff.h" #include "i_main.h" #include "i_system.h" #include "i_video.h" #include "g_game.h" #include "smooth.h" #include "r_fps.h" #include #include "e6y.h"//e6y #include "xs_Float.h" #include "dsda/configuration.h" #include "dsda/exhud.h" #include "dsda/features.h" #include "dsda/map_format.h" #include "dsda/mapinfo.h" #include "dsda/render_stats.h" #include "dsda/settings.h" #include "dsda/signal_context.h" #include "dsda/stretch.h" #include "dsda/gl/render_scale.h" #include "hexen/a_action.h" // e6y // Now they are variables. Depends from render_doom_lightmaps variable. // Unify colour maping logic by cph is removed, because of bugs. int LIGHTLEVELS = 32; int LIGHTSEGSHIFT = 3; int LIGHTBRIGHT = 2; int r_frame_count; // Fineangles in the SCREENWIDTH wide window. #define FIELDOFVIEW 2048 #define HEXEN_PI 3.141592657 #define FIXED2DOUBLE(x) ((x)/(double)FRACUNIT) int validcount = 1; // increment every time a check is made int validcount2 = 1; const lighttable_t *fixedcolormap; int centerx, centery; // e6y: wide-res int wide_centerx; fixed_t focallength; fixed_t focallengthy; fixed_t globaluclip, globaldclip; fixed_t centerxfrac, centeryfrac; fixed_t yaspectmul; fixed_t viewheightfrac; //e6y: for correct cliping of things fixed_t projection; // proff 11/06/98: Added for high-res fixed_t projectiony; fixed_t skyiscale; fixed_t viewx, viewy, viewz; angle_t viewangle; fixed_t viewcos, viewsin; fixed_t viewtancos, viewtansin; player_t *viewplayer; // e6y: Added for more precise flats drawing fixed_t viewfocratio; int r_nearclip = 5; int FieldOfView; int viewport[4]; float modelMatrix[16]; float projMatrix[16]; extern const lighttable_t **walllights; // // precalculated math tables // angle_t clipangle; // The viewangletox[viewangle + FINEANGLES/4] lookup // maps the visible view angles to screen X coordinates, // flattening the arc to a flat projection plane. // There will be many angles mapped to the same X. int viewangletox[FINEANGLES/2]; // The xtoviewangleangle[] table maps a screen pixel // to the lowest viewangle that maps back to x ranges // from clipangle to -clipangle. // e6y: resolution limitation is removed angle_t *xtoviewangle; // killough 2/8/98 // [FG] linear horizontal sky scrolling angle_t *linearskyangle; // killough 3/20/98: Support dynamic colormaps, e.g. deep water // killough 4/4/98: support dynamic number of them as well int numcolormaps; const lighttable_t *(*c_scalelight)[LIGHTLEVELS_MAX][MAXLIGHTSCALE]; const lighttable_t *(*c_zlight)[LIGHTLEVELS_MAX][MAXLIGHTZ]; const lighttable_t *(*scalelight)[MAXLIGHTSCALE]; const lighttable_t *(*zlight)[MAXLIGHTZ]; const lighttable_t *fullcolormap; const lighttable_t **colormaps; const byte* colormap_lump; // killough 3/20/98, 4/4/98: end dynamic colormaps //e6y: for Boom colormaps in OpenGL mode dboolean use_boom_cm; int boom_cm; // current colormap int frame_fixedcolormap = 0; int extralight; // bumped light from gun blasts // // R_PointOnSide // Traverse BSP (sub) tree, // check point against partition plane. // Returns side 0 (front) or 1 (back). // // killough 5/2/98: reformatted // // Workaround for optimization bug in clang // fixes desync in competn/doom/fp2-3655.lmp and in dmnsns.wad dmn01m909.lmp #if defined(__clang__) PUREFUNC int R_CompatiblePointOnSide(volatile fixed_t x, volatile fixed_t y, const node_t *node) #else PUREFUNC int R_CompatiblePointOnSide(fixed_t x, fixed_t y, const node_t *node) #endif { if (!node->dx) return x <= node->x ? node->dy > 0 : node->dy < 0; if (!node->dy) return y <= node->y ? node->dx < 0 : node->dx > 0; x -= node->x; y -= node->y; // Try to quickly decide by looking at sign bits. if ((node->dy ^ node->dx ^ x ^ y) < 0) return (node->dy ^ x) < 0; // (left is negative) return FixedMul(y, node->dx>>FRACBITS) >= FixedMul(node->dy>>FRACBITS, x); } #if defined(__clang__) PUREFUNC int R_ZDoomPointOnSide(volatile fixed_t x, volatile fixed_t y, const node_t *node) #else PUREFUNC int R_ZDoomPointOnSide(fixed_t x, fixed_t y, const node_t *node) #endif { if (!node->dx) return x <= node->x ? node->dy > 0 : node->dy < 0; if (!node->dy) return y <= node->y ? node->dx < 0 : node->dx > 0; x -= node->x; y -= node->y; // Try to quickly decide by looking at sign bits. if ((node->dy ^ node->dx ^ x ^ y) < 0) return (node->dy ^ x) < 0; // (left is negative) return (long long) y * node->dx >= (long long) x * node->dy; } int (*R_PointOnSide)(fixed_t x, fixed_t y, const node_t *node); // killough 5/2/98: reformatted PUREFUNC int R_CompatiblePointOnSegSide(fixed_t x, fixed_t y, const seg_t *line) { fixed_t lx = line->v1->x; fixed_t ly = line->v1->y; fixed_t ldx = line->v2->x - lx; fixed_t ldy = line->v2->y - ly; if (!ldx) return x <= lx ? ldy > 0 : ldy < 0; if (!ldy) return y <= ly ? ldx < 0 : ldx > 0; x -= lx; y -= ly; // Try to quickly decide by looking at sign bits. if ((ldy ^ ldx ^ x ^ y) < 0) return (ldy ^ x) < 0; // (left is negative) return FixedMul(y, ldx>>FRACBITS) >= FixedMul(ldy>>FRACBITS, x); } PUREFUNC int R_ZDoomPointOnSegSide(fixed_t x, fixed_t y, const seg_t *line) { fixed_t lx = line->v1->x; fixed_t ly = line->v1->y; fixed_t ldx = line->v2->x - lx; fixed_t ldy = line->v2->y - ly; if (!ldx) return x <= lx ? ldy > 0 : ldy < 0; if (!ldy) return y <= ly ? ldx < 0 : ldx > 0; x -= lx; y -= ly; // Try to quickly decide by looking at sign bits. if ((ldy ^ ldx ^ x ^ y) < 0) return (ldy ^ x) < 0; // (left is negative) return (long long) y * ldx >= (long long) x * ldy; } int (*R_PointOnSegSide)(fixed_t x, fixed_t y, const seg_t *line); // // R_PointToAngle // To get a global angle from cartesian coordinates, // the coordinates are flipped until they are in // the first octant of the coordinate system, then // the y (<=x) is scaled and divided by x to get a // tangent (slope) value which is looked up in the // tantoangle[] table. The +1 size of tantoangle[] // is to handle the case when x==y without additional // checking. // // killough 5/2/98: reformatted, cleaned up angle_t R_PointToAngleSlope(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y, slope_div_fn slope_div) { return (y -= y1, (x -= x1) || y) ? x >= 0 ? y >= 0 ? (x > y) ? tantoangle[slope_div(y,x)] : // octant 0 ANG90-1-tantoangle[slope_div(x,y)] : // octant 1 x > (y = -y) ? 0-tantoangle[slope_div(y,x)] : // octant 8 ANG270+tantoangle[slope_div(x,y)] : // octant 7 y >= 0 ? (x = -x) > y ? ANG180-1-tantoangle[slope_div(y,x)] : // octant 3 ANG90 + tantoangle[slope_div(x,y)] : // octant 2 (x = -x) > (y = -y) ? ANG180+tantoangle[slope_div(y,x)] : // octant 4 ANG270-1-tantoangle[slope_div(x,y)] : // octant 5 0; } angle_t R_PointToAngle2(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y) { return R_PointToAngleSlope(x1, y1, x, y, SlopeDiv); } angle_t R_PointToAngleEx(fixed_t x, fixed_t y) { return R_PointToAngleEx2(viewx, viewy, x, y); } angle_t R_PointToAngleEx2(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y) { // [crispy] fix overflows for very long distances int64_t y_viewy = (int64_t)y - y1; int64_t x_viewx = (int64_t)x - x1; // [crispy] the worst that could happen is e.g. INT_MIN-INT_MAX = 2*INT_MIN if (x_viewx < INT_MIN || x_viewx > INT_MAX ||y_viewy < INT_MIN || y_viewy > INT_MAX) { // [crispy] preserving the angle by halfing the distance in both directions x = (int)(x_viewx / 2 + x1); y = (int)(y_viewy / 2 + y1); } return R_PointToAngleSlope(x1, y1, x, y, SlopeDivEx); } //----------------------------------------------------------------------------- // // ! Returns the pseudoangle between the line p1 to (infinity, p1.y) and the // line from p1 to p2. The pseudoangle has the property that the ordering of // points by true angle anround p1 and ordering of points by pseudoangle are the // same. // // For clipping exact angles are not needed. Only the ordering matters. // This is about as fast as the fixed point R_PointToAngle2 but without // the precision issues associated with that function. // //----------------------------------------------------------------------------- angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y) { // Note: float won't work here as it's less precise than the BAM values being passed as parameters double vecx = (double)x - viewx; double vecy = (double)y - viewy; if (vecx == 0 && vecy == 0) { return 0; } else { double result = vecy / (fabs(vecx) + fabs(vecy)); if (vecx < 0) { result = 2.0 - result; } return (angle_t)xs_CRoundToInt(result * (1 << 30)); } } // // R_InitTextureMapping // // killough 5/2/98: reformatted static void R_InitTextureMapping (void) { int i,x,angle; double linearskyfactor; FieldOfView = FIELDOFVIEW; // For widescreen displays, increase the FOV so that the middle part of the // screen that would be visible on a 4:3 display has the requested FOV. if (wide_centerx != centerx) { // wide_centerx is what centerx would be if the display was not widescreen FieldOfView = (int)(atan((double)centerx * tan((double)FieldOfView * M_PI / FINEANGLES) / (double)wide_centerx) * FINEANGLES / M_PI); if (FieldOfView > 160 * FINEANGLES / 360) FieldOfView = 160 * FINEANGLES / 360; } // Use tangent table to generate viewangletox: // viewangletox will give the next greatest x // after the view angle. // // Calc focallength // so FIELDOFVIEW angles covers SCREENWIDTH. focallength = FixedDiv(centerxfrac, finetangent[FINEANGLES/4 + FieldOfView/2]); focallengthy = Scale(centerxfrac, yaspectmul, finetangent[FINEANGLES/4 + FieldOfView/2]); for (i=0 ; i limit) t = -1; else if (finetangent[i] < -limit) t = viewwidth+1; else { t = FixedMul(finetangent[i], focallength); t = (centerxfrac - t + FRACUNIT-1) >> FRACBITS; if (t < -1) t = -1; else if (t > viewwidth+1) t = viewwidth+1; } viewangletox[i] = t; } // Scan viewangletox[] to generate xtoviewangle[]: // xtoviewangle will give the smallest view angle // that maps to x. linearskyfactor = FIXED2DOUBLE(finetangent[FINEANGLES/4 + FieldOfView/2]) * ANG90; for (x=0; x<=viewwidth; x++) { for (i=0; viewangletox[i] > x; i++) ; xtoviewangle[x] = (i<= 0) ? angle : ANGLE_MAX + angle; } // Take out the fencepost cases from viewangletox. for (i=0; i>= LIGHTSCALESHIFT)/DISTMAP; if (level < 0) level = 0; else if (level >= NUMCOLORMAPS) level = NUMCOLORMAPS-1; // killough 3/20/98: Initialize multiple colormaps level *= 256; for (t=0; t>ANGLETOFINESHIFT) * 360.0f / FINEANGLES; yaw *= (float)M_PI / 180.0f; pitch = 0; if (V_IsOpenGLMode()) { pitch = (float)(viewpitch>>ANGLETOFINESHIFT) * 360.0f / FINEANGLES; pitch *= (float)M_PI / 180.0f; } x = (float)viewx / MAP_SCALE; z = -(float)viewy / MAP_SCALE; y = -(float)viewz / MAP_SCALE; /* R_LoadIdentity(modelMatrix); R_Rotate(modelMatrix, pitch, 0); R_Rotate(modelMatrix, yaw, 1); R_Translate(modelMatrix, x, y, z); */ A = (float)cos(pitch); B = (float)sin(pitch); C = (float)cos(yaw); D = (float)sin(yaw); m[0] = C; m[1] = D*B; m[2] = -D*A; m[3] = 0; m[4] = 0; m[5] = A; m[6] = B; m[7] = 0; m[8] = D; m[9] = -C*B; m[10] = A*C; m[11] = 0; m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1; m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; } // // R_ExecuteSetViewSize // void R_ExecuteSetViewSize (void) { int i; int cheight; setsizeneeded = false; SetRatio(SCREENWIDTH, SCREENHEIGHT); if (setblocks == 11) { viewheight = SCREENHEIGHT; freelookviewheight = viewheight; } // proff 09/24/98: Added for high-res else { viewheight = SCREENHEIGHT - ST_SCALED_HEIGHT; freelookviewheight = SCREENHEIGHT; } viewwidth = SCREENWIDTH; viewheightfrac = viewheight<4228 projectiony = (fixed_t)((((int64_t)cheight * centerx * 320) / 200) / SCREENWIDTH * FRACUNIT); // e6y: this is a precalculated value for more precise flats drawing (see R_MapPlane) viewfocratio = projectiony / wide_centerx; dsda_SetupStretchParams(); R_InitBuffer (SCREENWIDTH, viewheight); R_InitTextureMapping(); // psprite scales // proff 08/17/98: Changed for high-res // proff 11/06/98: Added for high-res // e6y: wide-res pspritexscale = (wide_centerx << FRACBITS) / 160; pspriteyscale = (cheight << FRACBITS) / 200; pspriteiscale = FixedDiv (FRACUNIT, pspritexscale); // [FG] make sure that the product of the weapon sprite scale factor // and its reciprocal is always at least FRACUNIT to // fix garbage lines at the top of weapon sprites pspriteiyscale = FixedDiv (FRACUNIT, pspriteyscale); while (FixedMul(pspriteiyscale, pspriteyscale) < FRACUNIT) pspriteiyscale++; //e6y: added for GL pspritexscale_f = (float)wide_centerx/160.0f; pspriteyscale_f = (float)cheight / 200.0f; skyiscale = (200 << FRACBITS) / SCREENHEIGHT; // [RH] Sky height fix for screens not 200 (or 240) pixels tall R_InitSkyMap(); // thing clipping for (i=0 ; i>ANGLETOFINESHIFT]); distscale[i] = FixedDiv(FRACUNIT,cosadj); } // e6y // Calculate the light levels to use // for each level / scale combination. // Calculate the light levels to use // for each level / scale combination. for (i=0; i= NUMCOLORMAPS) level = NUMCOLORMAPS-1; // killough 3/20/98: initialize multiple colormaps level *= 256; for (t=0; tsector; } void R_SectorCenter(fixed_t *x, fixed_t *y, sector_t *sec) { int i; *x = 0; *y = 0; if (!sec->linecount) return; for (i = 0; i < sec->linecount; ++i) { *x += sec->lines[i]->v1->x; *y += sec->lines[i]->v1->y; } *x /= sec->linecount; *y /= sec->linecount; if (R_PointInSector(*x, *y) != sec) { *x = sec->lines[0]->v1->x; *y = sec->lines[0]->v1->y; } } void R_LineCenter(fixed_t *x, fixed_t *y, line_t *line) { if (!line->v1 || !line->v2) { *x = 0; *y = 0; return; } *x = (line->v1->x + line->v2->x) / 2; *y = (line->v1->y + line->v2->y) / 2; } // // R_SetupFreelook // void R_SetupFreelook(void) { if (V_IsSoftwareMode()) { fixed_t InvZtoScale; fixed_t dy; int i; centery = viewheight / 2; if (raven || dsda_MouseLook()) { dy = FixedMul(focallengthy, finetangent[(ANG90-viewpitch)>>ANGLETOFINESHIFT]); centery += dy >> FRACBITS; } centeryfrac = centery<extralight; if (extra_brightness < 0 || extra_brightness > 4) { extra_brightness = 0; } if (extra_brightness != 0) { dsda_TrackFeature(uf_levelbrightness); } extralight += extra_brightness; viewsin = finesine[viewangle>>ANGLETOFINESHIFT]; viewcos = finecosine[viewangle>>ANGLETOFINESHIFT]; viewtansin = FixedMul(FocalTangent, viewsin); viewtancos = FixedMul(FocalTangent, viewcos); R_SetupFreelook(); // killough 3/20/98, 4/4/98: select colormap based on player status if (player->mo->subsector->sector->colormap) cm = player->mo->subsector->sector->colormap; else if (player->mo->subsector->sector->heightsec != -1) { const sector_t *s = player->mo->subsector->sector->heightsec + sectors; cm = viewz < s->floorheight ? s->bottommap : viewz > s->ceilingheight ? s->topmap : s->midmap; if (cm < 0 || cm > numcolormaps) cm = 0; } else cm = map_info.default_colormap; //e6y: save previous and current colormap boom_cm = cm; fullcolormap = colormaps[cm]; zlight = c_zlight[cm]; scalelight = c_scalelight[cm]; //e6y frame_fixedcolormap = player->fixedcolormap; if (frame_fixedcolormap < 0 || frame_fixedcolormap > NUMCOLORMAPS) { I_Error(" value out of range: %d\n", player->fixedcolormap); } if (player->fixedcolormap) { // killough 3/20/98: localize scalelightfixed (readability/optimization) static const lighttable_t *scalelightfixed[MAXLIGHTSCALE]; fixedcolormap = fullcolormap // killough 3/20/98: use fullcolormap + player->fixedcolormap*256*sizeof(lighttable_t); walllights = scalelightfixed; for (i=0 ; iflags2 |= MF2_DONTDRAW; R_RenderBSPNode(numnodes - 1); // head node is the last node output players[displayplayer].mo->flags2 &= ~MF2_DONTDRAW; } else { // The head node is the last node output. R_RenderBSPNode(numnodes - 1); } if (map_format.zdoom && V_IsOpenGLMode()) { R_ForceRenderPolyObjs(); } } // // R_RenderView // void R_RenderPlayerView (player_t* player) { r_frame_count++; DSDA_ADD_CONTEXT(sf_setup_frame); R_SetupFrame (player); DSDA_REMOVE_CONTEXT(sf_setup_frame); DSDA_ADD_CONTEXT(sf_clear); R_ClearClipSegs (); R_ClearDrawSegs (); R_ClearPlanes (); R_ClearSprites (); DSDA_REMOVE_CONTEXT(sf_clear); DSDA_ADD_CONTEXT(sf_init_scene); R_InitDrawScene(); DSDA_REMOVE_CONTEXT(sf_init_scene); FakeNetUpdate(); if (V_IsOpenGLMode()) { DSDA_ADD_CONTEXT(sf_gl_frustum); gld_FrustumSetup(); DSDA_REMOVE_CONTEXT(sf_gl_frustum); } DSDA_ADD_CONTEXT(sf_bsp_nodes); R_RenderBSPNodes(); DSDA_REMOVE_CONTEXT(sf_bsp_nodes); FakeNetUpdate(); if (V_IsSoftwareMode()) { DSDA_ADD_CONTEXT(sf_draw_planes); R_DrawPlanes(); DSDA_REMOVE_CONTEXT(sf_draw_planes); } DSDA_ADD_CONTEXT(sf_reset_column_buffer); R_ResetColumnBuffer(); DSDA_REMOVE_CONTEXT(sf_reset_column_buffer); FakeNetUpdate(); if (V_IsSoftwareMode()) { DSDA_ADD_CONTEXT(sf_draw_masked); R_DrawMasked (); R_ResetColumnBuffer(); DSDA_REMOVE_CONTEXT(sf_draw_masked); } FakeNetUpdate(); if (V_IsOpenGLMode() && !automap_on) { DSDA_ADD_CONTEXT(sf_draw_scene); gld_DrawScene(player); gld_EndDrawScene(); DSDA_REMOVE_CONTEXT(sf_draw_scene); } }