diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ede6c9a52..2fb046924 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -826,6 +826,7 @@ set( FASTMATH_PCH_SOURCES r_segs.cpp r_sky.cpp r_things.cpp + r_voxel.cpp r_walldraw.cpp s_advsound.cpp s_environment.cpp diff --git a/src/r_things.cpp b/src/r_things.cpp index 4e63372e2..472ecc8ea 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -65,6 +65,7 @@ #include "r_data/voxels.h" #include "p_local.h" #include "p_maputl.h" +#include "r_voxel.h" EXTERN_CVAR(Bool, st_scale) EXTERN_CVAR(Bool, r_shadercolormaps) @@ -691,9 +692,9 @@ void R_WallSpriteColumn () rw_light += rw_lightstep; } +#if 0 void R_DrawVisVoxel(vissprite_t *spr, int minslabz, int maxslabz, short *cliptop, short *clipbot) { -#if 0 int flags = 0; // Do setup for blending. @@ -761,8 +762,8 @@ void R_DrawVisVoxel(vissprite_t *spr, int minslabz, int maxslabz, short *cliptop R_FinishSetPatchStyle(); NetUpdate(); -#endif } +#endif // // R_ProjectSprite @@ -2842,12 +2843,12 @@ inline int sgn(int v) return v < 0 ? -1 : v > 0 ? 1 : 0; } +#if 0 void R_DrawVoxel(const FVector3 &globalpos, FAngle viewangle, const FVector3 &dasprpos, DAngle dasprang, fixed_t daxscale, fixed_t dayscale, FVoxel *voxobj, FSWColormap *colormap, int colormapnum, short *daumost, short *dadmost, int minslabz, int maxslabz, int flags) { -#if 0 int i, j, k, x, y, syoff, ggxstart, ggystart, nxoff; fixed_t cosang, sinang, sprcosang, sprsinang; int backx, backy, gxinc, gyinc; @@ -3175,8 +3176,8 @@ void R_DrawVoxel(const FVector3 &globalpos, FAngle viewangle, } } } -#endif } +#endif //========================================================================== // diff --git a/src/r_things.h b/src/r_things.h index ac8345c70..2fa8786a9 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -144,11 +144,6 @@ void R_CheckOffscreenBuffer(int width, int height, bool spansonly); enum { DVF_OFFSCREEN = 1, DVF_SPANSONLY = 2, DVF_MIRRORED = 4 }; -void R_DrawVoxel(const FVector3 &viewpos, FAngle viewangle, - const FVector3 &sprpos, DAngle dasprang, - fixed_t daxscale, fixed_t dayscale, struct FVoxel *voxobj, - FSWColormap *colormap, int colormapnum, short *daumost, short *dadmost, int minslabz, int maxslabz, int flags); - void R_ClipVisSprite (vissprite_t *vis, int xl, int xh); } diff --git a/src/r_voxel.cpp b/src/r_voxel.cpp new file mode 100644 index 000000000..115ff610c --- /dev/null +++ b/src/r_voxel.cpp @@ -0,0 +1,124 @@ +/* +** Voxel rendering +** Copyright (c) 2016 Magnus Norddahl +** +** This software is provided 'as-is', without any express or implied +** warranty. In no event will the authors be held liable for any damages +** arising from the use of this software. +** +** Permission is granted to anyone to use this software for any purpose, +** including commercial applications, and to alter it and redistribute it +** freely, subject to the following restrictions: +** +** 1. The origin of this software must not be misrepresented; you must not +** claim that you wrote the original software. If you use this software +** in a product, an acknowledgment in the product documentation would be +** appreciated but is not required. +** 2. Altered source versions must be plainly marked as such, and must not be +** misrepresented as being the original software. +** 3. This notice may not be removed or altered from any source distribution. +** +*/ + +#include +#include "templates.h" +#include "doomdef.h" +#include "sbar.h" +#include "r_data/r_translate.h" +#include "r_data/colormaps.h" +#include "r_data/voxels.h" +#include "r_data/sprites.h" +#include "d_net.h" +#include "po_man.h" +#include "r_things.h" +#include "r_draw.h" +#include "r_thread.h" +#include "r_utility.h" +#include "r_main.h" +#include "r_voxel.h" + +namespace swrenderer +{ + void R_DrawVisVoxel(vissprite_t *sprite, int minZ, int maxZ, short *cliptop, short *clipbottom) + { + R_SetColorMapLight(sprite->Style.BaseColormap, 0, sprite->Style.ColormapNum << FRACBITS); + bool visible = R_SetPatchStyle(sprite->Style.RenderStyle, sprite->Style.Alpha, sprite->Translation, sprite->FillColor); + if (!visible) + return; + + FVector3 view_origin = sprite->pa.vpos; + FAngle view_angle = sprite->pa.vang; + DVector3 sprite_origin = { sprite->gpos.X, sprite->gpos.Y, sprite->gpos.Z }; + DAngle sprite_angle = sprite->Angle; + double sprite_xscale = FIXED2DBL(sprite->xscale); + double sprite_yscale = sprite->yscale; + FVoxel *voxel = sprite->voxel; + + int miplevel = 0;//voxel->NumMips; + const FVoxelMipLevel &mip = voxel->Mips[miplevel]; + if (mip.SlabData == nullptr) + return; + + /* + double spriteSin = sprite_angle.Sin(); + double spriteCos = sprite_angle.Cos(); + double pivotX = (mip.Pivot.X * spriteSin - mip.Pivot.Y * spriteCos); + double pivotY = (mip.Pivot.X * spriteCos + mip.Pivot.Y * spriteSin); + double pivotZ = -mip.Pivot.Z; + */ + + R_FillBox(sprite_origin, 5.0 * sprite_xscale, 5.0 * sprite_yscale, 16, cliptop, clipbottom, false, false); + } + + void R_FillBox(DVector3 origin, double extentX, double extentY, int color, short *cliptop, short *clipbottom, bool viewspace, bool pixelstretch) + { + double viewX, viewY, viewZ; + if (viewspace) + { + viewX = origin.X; + viewY = origin.Y; + viewZ = origin.Z; + } + else // world space + { + double translatedX = origin.X - ViewPos.X; + double translatedY = origin.Y - ViewPos.Y; + double translatedZ = origin.Z - ViewPos.Z; + viewX = translatedX * ViewSin - translatedY * ViewCos; + viewY = translatedZ; + viewZ = translatedX * ViewTanCos + translatedY * ViewTanSin; + } + + if (viewZ < 0.01f) + return; + + double screenX = CenterX + viewX / viewZ * CenterX; + double screenY = CenterY - viewY / viewZ * InvZtoScale; + double screenExtentX = extentX / viewZ * CenterX; + double screenExtentY = pixelstretch ? screenExtentX * YaspectMul : screenExtentX; + + int x1 = MAX((int)(screenX - screenExtentX), 0); + int x2 = MIN((int)(screenX + screenExtentX + 0.5f), viewwidth - 1); + int y1 = MAX((int)(screenY - screenExtentY), 0); + int y2 = MIN((int)(screenY + screenExtentY + 0.5f), viewheight - 1); + + int pixelsize = r_swtruecolor ? 4 : 1; + + if (y1 < y2) + { + for (int x = x1; x < x2; x++) + { + int columnY1 = MAX(y1, (int)cliptop[x]); + int columnY2 = MIN(y2, (int)clipbottom[x]); + if (columnY1 < columnY2) + { + using namespace drawerargs; + dc_dest = dc_destorg + (dc_pitch * columnY1 + x) * pixelsize; + dc_color = color; + dc_count = columnY2 - columnY1; + R_FillColumn(); + } + } + } + } +} diff --git a/src/r_voxel.h b/src/r_voxel.h new file mode 100644 index 000000000..159e26f6e --- /dev/null +++ b/src/r_voxel.h @@ -0,0 +1,31 @@ +/* +** Voxel rendering +** Copyright (c) 2016 Magnus Norddahl +** +** This software is provided 'as-is', without any express or implied +** warranty. In no event will the authors be held liable for any damages +** arising from the use of this software. +** +** Permission is granted to anyone to use this software for any purpose, +** including commercial applications, and to alter it and redistribute it +** freely, subject to the following restrictions: +** +** 1. The origin of this software must not be misrepresented; you must not +** claim that you wrote the original software. If you use this software +** in a product, an acknowledgment in the product documentation would be +** appreciated but is not required. +** 2. Altered source versions must be plainly marked as such, and must not be +** misrepresented as being the original software. +** 3. This notice may not be removed or altered from any source distribution. +** +*/ + +#pragma once + +namespace swrenderer +{ + struct vissprite_t; + + void R_DrawVisVoxel(vissprite_t *sprite, int minZ, int maxZ, short *cliptop, short *clipbottom); + void R_FillBox(DVector3 origin, double extentX, double extentY, int color, short *cliptop, short *clipbottom, bool viewspace, bool pixelstretch); +}