diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index adce10023..7d84323bc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -470,6 +470,7 @@ if(${SRB2_CONFIG_HWRENDER}) ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_clip.c ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_draw.c ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_main.c + ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_portal.c ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_md2.c ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_md2load.c ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_md3load.c @@ -487,6 +488,7 @@ if(${SRB2_CONFIG_HWRENDER}) ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_drv.h ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_glob.h ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_main.h + ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_portal.c ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_md2.h ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_md2load.h ${CMAKE_CURRENT_SOURCE_DIR}/hardware/hw_md3load.h diff --git a/src/Makefile b/src/Makefile index 12afce57b..63239dfbe 100644 --- a/src/Makefile +++ b/src/Makefile @@ -293,7 +293,7 @@ ifdef NOHW else OPTS+=-DHWRENDER OBJS+=$(OBJDIR)/hw_bsp.o $(OBJDIR)/hw_draw.o \ - $(OBJDIR)/hw_main.o $(OBJDIR)/hw_clip.o $(OBJDIR)/hw_md2.o $(OBJDIR)/hw_cache.o \ + $(OBJDIR)/hw_main.o $(OBJDIR)/hw_clip.o $(OBJDIR)/hw_md2.o $(OBJDIR)/hw_cache.o $(OBJDIR)/hw_portal.o\ $(OBJDIR)/hw_md2load.o $(OBJDIR)/hw_md3load.o $(OBJDIR)/hw_model.o $(OBJDIR)/u_list.o $(OBJDIR)/hw_batching.o $(OBJDIR)/hw_shaders.o endif @@ -739,7 +739,7 @@ ifdef MINGW $(OBJDIR)/r_opengl.o: hardware/r_opengl/r_opengl.c hardware/r_opengl/r_opengl.h \ doomdef.h doomtype.h g_state.h m_swap.h hardware/hw_drv.h screen.h \ command.h hardware/hw_data.h hardware/hw_defs.h hardware/hw_md2.h \ - hardware/hw_glob.h hardware/hw_main.h hardware/hw_clip.h \ + hardware/hw_glob.h hardware/hw_main.h hardware/hw_clip.h hardware/hw_portal.h\ hardware/hw_md2load.h hardware/hw_md3load.h hardware/hw_model.h hardware/u_list.h \ am_map.h d_event.h d_player.h p_pspr.h m_fixed.h tables.h info.h d_think.h \ p_mobj.h doomdata.h d_ticcmd.h r_defs.h hardware/hw_dll.h @@ -748,7 +748,7 @@ else $(OBJDIR)/r_opengl.o: hardware/r_opengl/r_opengl.c hardware/r_opengl/r_opengl.h \ doomdef.h doomtype.h g_state.h m_swap.h hardware/hw_drv.h screen.h \ command.h hardware/hw_data.h hardware/hw_defs.h hardware/hw_md2.h \ - hardware/hw_glob.h hardware/hw_main.h hardware/hw_clip.h \ + hardware/hw_glob.h hardware/hw_main.h hardware/hw_clip.h hardware/hw_portal.h \ hardware/hw_md2load.h hardware/hw_md3load.h hardware/hw_model.h hardware/u_list.h \ am_map.h d_event.h d_player.h p_pspr.h m_fixed.h tables.h info.h d_think.h \ p_mobj.h doomdata.h d_ticcmd.h r_defs.h hardware/hw_dll.h diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 8f1fde3d8..38d56ae5e 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -24,6 +24,7 @@ #include "hw_batching.h" #include "hw_md2.h" #include "hw_clip.h" +#include "hw_portal.h" #include "r_opengl/r_opengl.h" @@ -65,6 +66,8 @@ boolean gr_shadersavailable = false; // Whether the internal state is set to palette rendering or not. static boolean gr_palette_rendering_state = false; +boolean gl_drawing_stencil = false; + // ========================================================================== // Commands and console variables // ========================================================================== @@ -271,11 +274,11 @@ static float gr_windowcentery; static float gr_pspritexscale, gr_pspriteyscale; -static seg_t *gr_curline; -static side_t *gr_sidedef; -static line_t *gr_linedef; -static sector_t *gr_frontsector; -static sector_t *gr_backsector; +seg_t *gr_curline; +side_t *gr_sidedef; +line_t *gr_linedef; +sector_t *gr_frontsector; +sector_t *gr_backsector; static void HWR_SplitWall(sector_t *sector, FOutVector *wallVerts, INT32 texnum, boolean noencore, FSurfaceInfo* Surf, INT32 cutflag, ffloor_t *pfloor, FBITFIELD polyflags); @@ -315,185 +318,6 @@ static float gr_viewludsin, gr_viewludcos; static angle_t gr_aimingangle; static void HWR_SetTransformAiming(FTransform *trans); -// ========================================================================== -// Visual portals (attr. Hannu Hanhi) -// ========================================================================== - -static boolean HWR_PortalCheckPointSide(fixed_t x, fixed_t y); -static boolean HWR_PortalCheckBBox(const fixed_t *bspcoord); - -enum -{ - GRPORTAL_OFF, - GRPORTAL_SEARCH, - GRPORTAL_STENCIL, - GRPORTAL_DEPTH, - GRPORTAL_INSIDE, -}; - -static int gr_portal = GRPORTAL_OFF; -static boolean gl_drawing_stencil = false; -typedef struct gl_portal_s -{ - struct gl_portal_s *next; - - // Viewport. - fixed_t viewx; - fixed_t viewy; - fixed_t viewz; - angle_t viewangle; - - UINT8 pass; /**< Keeps track of the portal's recursion depth. */ - INT32 startline; - INT32 clipline; /**< Optional clipline for line-based portals. */ - INT32 drawcount; /**< For OpenGL. */ - - seg_t *seg; -} gl_portal_t; - -typedef struct gl_portallist_s -{ - gl_portal_t *base; - gl_portal_t *cap; -} gl_portallist_t; - -// new thing -gl_portallist_t *currentportallist; - -// More precise version of R_PointToAngle2 using floats and atan2. -static angle_t R_PointToAngle2Precise(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y) -{ - fixed_t dx = x - pviewx; - fixed_t dy = y - pviewy; - float radians; - - if (!dx && !dy) - return 0; - - // no need for correct scale with FIXED_TO_FLOAT here - // since we're just calculating the angle - radians = atan2(dy, dx); - - return (angle_t)(radians / M_PI * ANGLE_180); -} - -// More precise version of R_PointToDist2 using floats and sqrt. -static fixed_t R_PointToDist2Precise(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1) -{ - // float-fixed conversions can be omitted here - // because they cancel each other out in this case - - float dx = px1 - px2; - float dy = py1 - py2; - double result = sqrt(dx*dx + dy*dy); - - return (fixed_t)result; -} - -static void HWR_Portal_Add2Lines(const INT32 line1, const INT32 line2, seg_t *seg) -{ - line_t *start, *dest; - - angle_t dangle; - - fixed_t disttopoint; - angle_t angtopoint; - - vertex_t dest_c, start_c; - - gl_portal_t *portal; - - portal = Z_Malloc(sizeof(gl_portal_t), PU_STATIC, NULL); - - // Linked list. - if (!currentportallist->base) - { - currentportallist->base = portal; - currentportallist->cap = portal; - } - else - { - currentportallist->cap->next = portal; - currentportallist->cap = portal; - } - portal->next = NULL; - - // Increase recursion level. - portal->pass = portalrender+1; - - portal->seg = seg; - - // Offset the portal view by the linedef centers - start = &lines[line1]; - dest = &lines[line2]; - - // Most fixed-point calculations and trigonometric function tables are replaced by - // floats and cmath library calls in this part to improve the precision of the - // location and angle of the new viewpoint. - // - // This reduces artefacts on the edges of portals, showing thin lines/pixels - // of the underlying graphics. (for example the sky texture) It's not 100% - // perfectly aligned and artefact-free, but looks noticeably - // better than the original code. I'm not even sure if it's this - // code or the nodebuilder or hw_map or something else causing the remaining issues.. - - dangle = R_PointToAngle2Precise(0,0,dest->dx,dest->dy) - R_PointToAngle2Precise(start->dx,start->dy,0,0); - - // looking glass center - start_c.x = start->v1->x/2 + start->v2->x/2; - start_c.y = start->v1->y/2 + start->v2->y/2; - - // other side center - dest_c.x = dest->v1->x/2 + dest->v2->x/2; - dest_c.y = dest->v1->y/2 + dest->v2->y/2; - - disttopoint = R_PointToDist2Precise(start_c.x, start_c.y, viewx, viewy); - angtopoint = R_PointToAngle2Precise(start_c.x, start_c.y, viewx, viewy); - angtopoint += dangle; - - float fang = ((float)angtopoint / 4294967296.0f) * 2.0f * M_PI; - - if (dangle == 0) - { - portal->viewx = viewx + dest_c.x - start_c.x; - portal->viewy = viewy + dest_c.y - start_c.y; - } - else - { - portal->viewx = dest_c.x + (fixed_t)(cos(fang) * disttopoint); - portal->viewy = dest_c.y + (fixed_t)(sin(fang) * disttopoint); - } - portal->viewz = viewz + dest->frontsector->floorheight - start->frontsector->floorheight; - portal->viewangle = viewangle + dangle; - - portal->startline = line1; - portal->clipline = line2; -} - -static void HWR_PortalFrame(gl_portal_t* portal) -{ - viewx = portal->viewx; - viewy = portal->viewy; - viewz = portal->viewz; - - viewangle = portal->viewangle; - viewsin = FINESINE(viewangle>>ANGLETOFINESHIFT); - viewcos = FINECOSINE(viewangle>>ANGLETOFINESHIFT); - - if (portal->clipline != -1) - { - portalclipline = &lines[portal->clipline]; - portalcullsector = portalclipline->frontsector; - viewsector = portalclipline->frontsector; - } - else - { - portalclipline = NULL; - portalcullsector = NULL; - viewsector = R_PointInSubsector(viewx, viewy)->sector; - } -} - // ========================================================================== // Lighting // ========================================================================== @@ -3242,60 +3066,6 @@ skip_stuff_for_portals: sub->validcount = validcount; } -// idea for fixing fakery map: one portal pillar works, 2 pillars have left/right bug wall, 1 pillar has both sides bugged. -// bounding box is probably right on the edge, maybe could check for this with P_ClosestPointOnLine -// so: for each side check, if it passes then also check distance to line, -// if its zero (or very close?) then dont return true, instead continue to next side check -// it helped with center pillars! but other parts still have issues, probably because some of bounding box is on correct side. - -// idea for further clipping improvement: -// have a separate xyz coordinate for portal view side checking: one that is derived by moving the viewxyz forward -// the new coords would be at the intersection of line_a and line_b, where -// line_a = line of view, pointing forward from the center of the camera -// line_b = a line orthogonal to line_a, defined so that the nearest vertex of portalclipline lies within it -// maybe if the seg to be drawn has these new coords on one side and the normal viewxyz on the other side then it can be culled? - -// looks like P_Thrust in p_user.c has code for moving point forward towards a direction -// maybe P_InterceptVector could be used for intersect point -// use returned value as multiplier for the added values from p_thrust thing -// P_InterceptVector needs divlines which need dx and dy, dx=x2-x1 dy=y2-y1 - -// returns true if the point is on the correct (viewable) side of the -// portal destination line -static boolean HWR_PortalCheckPointSide(fixed_t x, fixed_t y) -{ - // we are checking if the point is on the viewable side of the portal exit. - // being exactly on the portal exit line is not enough to pass the test. - // P_PointOnLineSide could behave differently from this expectation on this case, - // so first check if the point is precisely on the line, and then if not, check the side. - - vertex_t closest_point; - P_ClosestPointOnLine(x, y, portalclipline, &closest_point); - if (closest_point.x != x || closest_point.y != y) - { - if (P_PointOnLineSide(x, y, portalclipline) != 1) - return true; - } - return false; -} - -static boolean HWR_PortalCheckBBox(const fixed_t *bspcoord) -{ - if (!portalclipline) - return true; - - if (HWR_PortalCheckPointSide(bspcoord[BOXLEFT], bspcoord[BOXTOP]) || - HWR_PortalCheckPointSide(bspcoord[BOXLEFT], bspcoord[BOXBOTTOM]) || - HWR_PortalCheckPointSide(bspcoord[BOXRIGHT], bspcoord[BOXTOP]) || - HWR_PortalCheckPointSide(bspcoord[BOXRIGHT], bspcoord[BOXBOTTOM])) - { - return true; - } - - // we did not find any reason to pass the check, so return failure - return false; -} - // // Renders all subsectors below a given node, // traversing subtree recursively. @@ -3318,9 +3088,9 @@ static void HWR_RenderBSPNode(INT32 bspnum) if (HWR_PortalCheckBBox(bsp->bbox[side])) HWR_RenderBSPNode(bsp->children[side]); - // Possibly divide back space - if (!(HWR_CheckBBox(bsp->bbox[side^1]) && HWR_PortalCheckBBox(bsp->bbox[side^1]))) - return; + // Possibly divide back space + if (!(HWR_CheckBBox(bsp->bbox[side^1]) && HWR_PortalCheckBBox(bsp->bbox[side^1]))) + return; bspnum = bsp->children[side^1]; } @@ -5248,98 +5018,20 @@ void HWR_ClearClipper(void) #endif } -// Adds an entry to the clipper for portal rendering -static void HWR_PortalClipping(gl_portal_t *portal) -{ - angle_t angle1, angle2; - - line_t *line = &lines[portal->clipline]; - - angle1 = R_PointToAngle64(line->v1->x, line->v1->y); - angle2 = R_PointToAngle64(line->v2->x, line->v2->y); - - // clip things that are not inside the portal window from our viewpoint - gld_clipper_SafeAddClipRange(angle2, angle1); -} - -enum -{ - HWR_STENCIL_NORMAL, - HWR_STENCIL_BEGIN, - HWR_STENCIL_REVERSE, - HWR_STENCIL_DEPTH, - HWR_STENCIL_SKY -}; - // Changes the current stencil state. -static void HWR_SetStencilState(int state, int level) +void HWR_SetStencilState(int state, int level) { if (level > -1) HWD.pfnSetSpecialState(HWD_SET_STENCIL_LEVEL, level); HWD.pfnSetSpecialState(HWD_SET_PORTAL_MODE, state); } -// Renders a portal segment. -static void HWR_RenderPortalSeg(gl_portal_t* portal, int state) -{ - gl_drawing_stencil = true; // do not draw outside of the stencil buffer, idiot. - // set our portal state and prepare to render the seg - gr_portal = state; - gr_curline = portal->seg; - gr_frontsector = portal->seg->frontsector; - gr_backsector = portal->seg->backsector; - HWR_ProcessSeg(); - gl_drawing_stencil = false; - // need to work around the r_opengl PF_Invisible bug with this call - // similarly as in the linkdraw hack in HWR_DrawSprites - HWD.pfnSetBlend(PF_Translucent|PF_Occlude|PF_Masked); -} - // Renders the current viewpoint, though takes portal arguments for recursive portals. -static void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, player_t *player, int stencil_level, boolean allow_portals); - -// Renders a single portal from the current viewpoint. -static void HWR_RenderPortal(gl_portal_t* portal, gl_portal_t* rootportal, const float fpov, player_t *player, int stencil_level) -{ - // draw portal seg to stencil buffer with increment - HWR_SetTransform(fpov, player); - HWR_ClearClipper(); - - HWR_SetStencilState(HWR_STENCIL_BEGIN, stencil_level); - HWR_RenderPortalSeg(portal, GRPORTAL_STENCIL); - - // go to portal frame lmao - HWR_PortalFrame(portal); - // call HWR_RenderViewpoint - HWR_RenderViewpoint(portal, fpov, player, stencil_level + 1, true); - // return to current frame - if (rootportal) - HWR_PortalFrame(rootportal); - else // current frame is not a portal frame but the main view! - { - R_SetupFrame(player, false); - portalclipline = NULL; - } - - // remove portal seg from stencil buffer - HWR_SetTransform(fpov, player); - HWR_ClearClipper(); - - HWR_SetStencilState(HWR_STENCIL_REVERSE, stencil_level); - HWR_RenderPortalSeg(portal, GRPORTAL_STENCIL); - - // draw portal seg to depth buffer - HWR_ClearClipper(); - HWR_SetStencilState(HWR_STENCIL_DEPTH, stencil_level); - HWR_RenderPortalSeg(portal, GRPORTAL_DEPTH); -} - -// Renders the current viewpoint, though takes portal arguments for recursive portals. -static void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, player_t *player, int stencil_level, boolean allow_portals) +void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, player_t *player, int stencil_level, boolean allow_portals) { gl_portallist_t portallist; gl_portal_t *portal; - gl_portal_t *gl_portal_temp; + const boolean skybox = (skyboxmo[0] && cv_skybox.value); const boolean useportals = cv_grportals.value && gr_maphasportals && allow_portals; @@ -5457,14 +5149,7 @@ static void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, playe ps_hw_nodedrawtime.value.p = 0; HWR_RenderDrawNodes(); - // free memory from portal list allocated by calls to Add2Lines - gl_portal_temp = portallist.base; - while (gl_portal_temp) - { - gl_portal_t *nextportal = gl_portal_temp->next; - Z_Free(gl_portal_temp); - gl_portal_temp = nextportal; - } + HWR_FreePortalList(portallist); } diff --git a/src/hardware/hw_main.h b/src/hardware/hw_main.h index 3ef901941..4b3c5af66 100644 --- a/src/hardware/hw_main.h +++ b/src/hardware/hw_main.h @@ -23,6 +23,7 @@ #include "hw_glob.h" #include "hw_data.h" #include "hw_defs.h" +#include "hw_portal.h" #include "../am_map.h" #include "../d_player.h" @@ -45,6 +46,23 @@ extern boolean a2c; extern FTransform atransform; extern float gr_viewsin, gr_viewcos; +extern boolean gl_drawing_stencil; + +extern seg_t *gr_curline; +extern side_t *gr_sidedef; +extern line_t *gr_linedef; +extern sector_t *gr_frontsector; +extern sector_t *gr_backsector; + +enum +{ + HWR_STENCIL_NORMAL, + HWR_STENCIL_BEGIN, + HWR_STENCIL_REVERSE, + HWR_STENCIL_DEPTH, + HWR_STENCIL_SKY +}; + // Performance stats extern ps_metric_t ps_hw_nodesorttime; extern ps_metric_t ps_hw_nodedrawtime; @@ -88,6 +106,10 @@ void HWR_AddCommands(void); void HWR_SetTransform(float fpov, player_t *player); void HWR_ClearClipper(void); +void HWR_SetStencilState(int state, int level); + +void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, player_t *player, int stencil_level, boolean allow_portals); + boolean HWR_UseShader(void); boolean HWR_ShouldUsePaletteRendering(void); boolean HWR_PalRenderFlashpal(void); diff --git a/src/hardware/hw_portal.c b/src/hardware/hw_portal.c new file mode 100644 index 000000000..f75d4365e --- /dev/null +++ b/src/hardware/hw_portal.c @@ -0,0 +1,300 @@ +// SONIC ROBO BLAST 2 KART +//----------------------------------------------------------------------------- +// Copyright (C) 2024 by Kart Krew. +// Copyright (C) 2020 by Sonic Team Junior. +// Copyright (C) 2000 by DooM Legacy Team. +// Copyright (C) 1996 by id Software, Inc. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file hw_portal.c +/// \brief OpenGL renderer portals. + +// ========================================================================== +// Visual portals (attr. Hannu Hanhi) +// ========================================================================== + +#ifdef HWRENDER + +#include "../r_portal.h" + +#include "../z_zone.h" + +#include "hw_clip.h" +#include "hw_drv.h" +#include "hw_defs.h" +#include "hw_main.h" +#include "hw_portal.h" + +SINT8 gr_portal = GRPORTAL_OFF; +gl_portallist_t *currentportallist; + +// More precise version of R_PointToAngle2 using floats and atan2. +static angle_t R_PointToAngle2Precise(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y) +{ + fixed_t dx = x - pviewx; + fixed_t dy = y - pviewy; + float radians; + + if (!dx && !dy) + return 0; + + // no need for correct scale with FIXED_TO_FLOAT here + // since we're just calculating the angle + radians = atan2(dy, dx); + + return (angle_t)(radians / M_PI * ANGLE_180); +} + +// More precise version of R_PointToDist2 using floats and sqrt. +static fixed_t R_PointToDist2Precise(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1) +{ + // float-fixed conversions can be omitted here + // because they cancel each other out in this case + + float dx = px1 - px2; + float dy = py1 - py2; + double result = sqrt(dx*dx + dy*dy); + + return (fixed_t)result; +} + +// Adds an entry to the clipper for portal rendering +void HWR_PortalClipping(gl_portal_t *portal) +{ + angle_t angle1, angle2; + + line_t *line = &lines[portal->clipline]; + + angle1 = R_PointToAngle64(line->v1->x, line->v1->y); + angle2 = R_PointToAngle64(line->v2->x, line->v2->y); + + // clip things that are not inside the portal window from our viewpoint + gld_clipper_SafeAddClipRange(angle2, angle1); +} + +void HWR_Portal_Add2Lines(const INT32 line1, const INT32 line2, seg_t *seg) +{ + line_t *start, *dest; + + angle_t dangle; + + fixed_t disttopoint; + angle_t angtopoint; + + vertex_t dest_c, start_c; + + gl_portal_t *portal; + + portal = Z_Malloc(sizeof(gl_portal_t), PU_STATIC, NULL); + + // Linked list. + if (!currentportallist->base) + { + currentportallist->base = portal; + currentportallist->cap = portal; + } + else + { + currentportallist->cap->next = portal; + currentportallist->cap = portal; + } + portal->next = NULL; + + // Increase recursion level. + portal->pass = portalrender+1; + + portal->seg = seg; + + // Offset the portal view by the linedef centers + start = &lines[line1]; + dest = &lines[line2]; + + // Most fixed-point calculations and trigonometric function tables are replaced by + // floats and cmath library calls in this part to improve the precision of the + // location and angle of the new viewpoint. + // + // This reduces artefacts on the edges of portals, showing thin lines/pixels + // of the underlying graphics. (for example the sky texture) It's not 100% + // perfectly aligned and artefact-free, but looks noticeably + // better than the original code. I'm not even sure if it's this + // code or the nodebuilder or hw_map or something else causing the remaining issues.. + + dangle = R_PointToAngle2Precise(0,0,dest->dx,dest->dy) - R_PointToAngle2Precise(start->dx,start->dy,0,0); + + // looking glass center + start_c.x = start->v1->x/2 + start->v2->x/2; + start_c.y = start->v1->y/2 + start->v2->y/2; + + // other side center + dest_c.x = dest->v1->x/2 + dest->v2->x/2; + dest_c.y = dest->v1->y/2 + dest->v2->y/2; + + disttopoint = R_PointToDist2Precise(start_c.x, start_c.y, viewx, viewy); + angtopoint = R_PointToAngle2Precise(start_c.x, start_c.y, viewx, viewy); + angtopoint += dangle; + + float fang = ((float)angtopoint / 4294967296.0f) * 2.0f * M_PI; + + if (dangle == 0) + { + portal->viewx = viewx + dest_c.x - start_c.x; + portal->viewy = viewy + dest_c.y - start_c.y; + } + else + { + portal->viewx = dest_c.x + (fixed_t)(cos(fang) * disttopoint); + portal->viewy = dest_c.y + (fixed_t)(sin(fang) * disttopoint); + } + portal->viewz = viewz + dest->frontsector->floorheight - start->frontsector->floorheight; + portal->viewangle = viewangle + dangle; + + portal->startline = line1; + portal->clipline = line2; +} + +void HWR_PortalFrame(gl_portal_t* portal) +{ + viewx = portal->viewx; + viewy = portal->viewy; + viewz = portal->viewz; + + viewangle = portal->viewangle; + viewsin = FINESINE(viewangle>>ANGLETOFINESHIFT); + viewcos = FINECOSINE(viewangle>>ANGLETOFINESHIFT); + + if (portal->clipline != -1) + { + portalclipline = &lines[portal->clipline]; + portalcullsector = portalclipline->frontsector; + viewsector = portalclipline->frontsector; + } + else + { + portalclipline = NULL; + portalcullsector = NULL; + viewsector = R_PointInSubsector(viewx, viewy)->sector; + } +} + +// Renders a portal segment. +void HWR_RenderPortalSeg(gl_portal_t* portal, SINT8 state) +{ + gl_drawing_stencil = true; // do not draw outside of the stencil buffer, idiot. + // set our portal state and prepare to render the seg + gr_portal = state; + gr_curline = portal->seg; + gr_frontsector = portal->seg->frontsector; + gr_backsector = portal->seg->backsector; + HWR_ProcessSeg(); + gl_drawing_stencil = false; + // need to work around the r_opengl PF_Invisible bug with this call + // similarly as in the linkdraw hack in HWR_DrawSprites + HWD.pfnSetBlend(PF_Translucent|PF_Occlude|PF_Masked); +} + +// Renders a single portal from the current viewpoint. +void HWR_RenderPortal(gl_portal_t* portal, gl_portal_t* rootportal, const float fpov, player_t *player, int stencil_level) +{ + // draw portal seg to stencil buffer with increment + HWR_SetTransform(fpov, player); + HWR_ClearClipper(); + + HWR_SetStencilState(HWR_STENCIL_BEGIN, stencil_level); + HWR_RenderPortalSeg(portal, GRPORTAL_STENCIL); + + // go to portal frame lmao + HWR_PortalFrame(portal); + // call HWR_RenderViewpoint + HWR_RenderViewpoint(portal, fpov, player, stencil_level + 1, true); + // return to current frame + if (rootportal) + HWR_PortalFrame(rootportal); + else // current frame is not a portal frame but the main view! + { + R_SetupFrame(viewssnum); + portalclipline = NULL; + } + + // remove portal seg from stencil buffer + HWR_SetTransform(fpov, player); + HWR_ClearClipper(); + + HWR_SetStencilState(HWR_STENCIL_REVERSE, stencil_level); + HWR_RenderPortalSeg(portal, GRPORTAL_STENCIL); + + // draw portal seg to depth buffer + HWR_ClearClipper(); + HWR_SetStencilState(HWR_STENCIL_DEPTH, stencil_level); + HWR_RenderPortalSeg(portal, GRPORTAL_DEPTH); +} + +void HWR_FreePortalList(gl_portallist_t portallist) +{ + // free memory from portal list allocated by calls to Add2Lines + gl_portal_t *gl_portal_temp = portallist.base; + while (gl_portal_temp) + { + gl_portal_t *nextportal = gl_portal_temp->next; + Z_Free(gl_portal_temp); + gl_portal_temp = nextportal; + } +} + +// idea for fixing fakery map: one portal pillar works, 2 pillars have left/right bug wall, 1 pillar has both sides bugged. +// bounding box is probably right on the edge, maybe could check for this with P_ClosestPointOnLine +// so: for each side check, if it passes then also check distance to line, +// if its zero (or very close?) then dont return true, instead continue to next side check +// it helped with center pillars! but other parts still have issues, probably because some of bounding box is on correct side. + +// idea for further clipping improvement: +// have a separate xyz coordinate for portal view side checking: one that is derived by moving the viewxyz forward +// the new coords would be at the intersection of line_a and line_b, where +// line_a = line of view, pointing forward from the center of the camera +// line_b = a line orthogonal to line_a, defined so that the nearest vertex of portalclipline lies within it +// maybe if the seg to be drawn has these new coords on one side and the normal viewxyz on the other side then it can be culled? + +// looks like P_Thrust in p_user.c has code for moving point forward towards a direction +// maybe P_InterceptVector could be used for intersect point +// use returned value as multiplier for the added values from p_thrust thing +// P_InterceptVector needs divlines which need dx and dy, dx=x2-x1 dy=y2-y1 + +// returns true if the point is on the correct (viewable) side of the +// portal destination line +boolean HWR_PortalCheckPointSide(fixed_t x, fixed_t y) +{ + // we are checking if the point is on the viewable side of the portal exit. + // being exactly on the portal exit line is not enough to pass the test. + // P_PointOnLineSide could behave differently from this expectation on this case, + // so first check if the point is precisely on the line, and then if not, check the side. + + vertex_t closest_point; + P_ClosestPointOnLine(x, y, portalclipline, &closest_point); + if (closest_point.x != x || closest_point.y != y) + { + if (P_PointOnLineSide(x, y, portalclipline) != 1) + return true; + } + return false; +} + +boolean HWR_PortalCheckBBox(const fixed_t *bspcoord) +{ + if (!portalclipline) + return true; + + if (HWR_PortalCheckPointSide(bspcoord[BOXLEFT], bspcoord[BOXTOP]) || + HWR_PortalCheckPointSide(bspcoord[BOXLEFT], bspcoord[BOXBOTTOM]) || + HWR_PortalCheckPointSide(bspcoord[BOXRIGHT], bspcoord[BOXTOP]) || + HWR_PortalCheckPointSide(bspcoord[BOXRIGHT], bspcoord[BOXBOTTOM])) + { + return true; + } + + // we did not find any reason to pass the check, so return failure + return false; +} + +#endif diff --git a/src/hardware/hw_portal.h b/src/hardware/hw_portal.h new file mode 100644 index 000000000..c4631a5a4 --- /dev/null +++ b/src/hardware/hw_portal.h @@ -0,0 +1,76 @@ +// SONIC ROBO BLAST 2 +//----------------------------------------------------------------------------- +// Copyright (C) 1993-1996 by id Software, Inc. +// Copyright (C) 1998-2000 by DooM Legacy Team. +// Copyright (C) 1999-2018 by Sonic Team Junior. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file r_portal.h +/// \brief Software renderer portal struct, functions, linked list extern. + +#ifndef __HW_PORTAL__ +#define __HW_PORTAL__ + +#ifdef HWRENDER + +#include "../doomstat.h" +#include "../r_data.h" +#include "../r_bsp.h" + +/** Portal structure. + */ +enum +{ + GRPORTAL_OFF, + GRPORTAL_SEARCH, + GRPORTAL_STENCIL, + GRPORTAL_DEPTH, + GRPORTAL_INSIDE, +}; + +extern SINT8 gr_portal; + +typedef struct gl_portal_s +{ + struct gl_portal_s *next; + + // Viewport. + fixed_t viewx; + fixed_t viewy; + fixed_t viewz; + angle_t viewangle; + + UINT8 pass; /**< Keeps track of the portal's recursion depth. */ + INT32 startline; + INT32 clipline; /**< Optional clipline for line-based portals. */ + INT32 drawcount; /**< For OpenGL. */ + + seg_t *seg; +} gl_portal_t; + +typedef struct gl_portallist_s +{ + gl_portal_t *base; + gl_portal_t *cap; +} gl_portallist_t; + +// new thing +extern gl_portallist_t *currentportallist; + +// Adds an entry to the clipper for portal rendering +void HWR_PortalClipping(gl_portal_t *portal); + +void HWR_Portal_Add2Lines(const INT32 line1, const INT32 line2, seg_t *seg); +void HWR_PortalFrame(gl_portal_t* portal); +void HWR_RenderPortalSeg(gl_portal_t* portal, SINT8 state); +void HWR_RenderPortal(gl_portal_t* portal, gl_portal_t* rootportal, const float fpov, player_t *player, int stencil_level); +void HWR_FreePortalList(gl_portallist_t portallist); + +boolean HWR_PortalCheckPointSide(fixed_t x, fixed_t y); +boolean HWR_PortalCheckBBox(const fixed_t *bspcoord); + +#endif +#endif