Skip to content

Commit

Permalink
Merge pull request #527 from tophyr/pr/modernize-renderer
Browse files Browse the repository at this point in the history
Modernize Renderer
  • Loading branch information
winterheart authored Aug 20, 2024
2 parents 3cb1e89 + 8bd1b67 commit 1026a6e
Show file tree
Hide file tree
Showing 22 changed files with 853 additions and 1,351 deletions.
4 changes: 2 additions & 2 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ Once CMake finishes, the built files will be put in `builds/mac/Descent3/Debug`
* If you would like to manage the code dependencies yourself:
* APT users
```sh
sudo apt install -y --no-install-recommends libsdl2-dev zlib1g-dev libgtest-dev
sudo apt install -y --no-install-recommends libsdl2-dev zlib1g-dev libgtest-dev libglm-dev
```
* DNF users
```sh
sudo dnf install -y SDL2-devel zlib-devel gtest
sudo dnf install -y SDL2-devel zlib-devel gtest glm-devel
```
3. **Clone the Descent3 source code.**
Expand Down
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED ENV{CMAKE_BUILD_TYPE})
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "default build type")
endif()

option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja)." OFF)
option(FORCE_PORTABLE_INSTALL "Install all files into local directory defined by CMAKE_INSTALL_PREFIX" ON)
option(BUILD_TESTING "Enable testing. Requires GTest." OFF)
option(ENABLE_LOGGER "Enable logging to the terminal" OFF)
option(ENABLE_MEM_RTL "Enable Real-time library memory management functions (disable to verbose memory allocations)" ON)
option(BUILD_TESTING "Enable testing. Requires GTest." OFF)
option(FATAL_GL_ERRORS "Check OpenGL calls and raise exceptions on errors." OFF)
option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja)." OFF)
option(FORCE_PORTABLE_INSTALL "Install all files into local directory defined by CMAKE_INSTALL_PREFIX" ON)
set(USE_VCPKG "DEFAULT" CACHE STRING "Use vcpkg for dependency management. DEFAULT defers to existence of $VCPKG_ROOT environment variable.")
set_property(CACHE USE_VCPKG PROPERTY STRINGS "DEFAULT" "ON" "OFF")

Expand Down Expand Up @@ -131,6 +132,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
add_compile_options("-Wno-multichar;-Wall")
endif()

find_package(glm REQUIRED)
find_package(SDL2 REQUIRED)
# Some versions of the SDL2 find_package set SDL2_INCLUDE_DIR and some set a plural SDL2_INCLUDE_DIRS. Check both.
message("SDL2 Include Dir is ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS}")
Expand Down Expand Up @@ -196,6 +198,11 @@ if(ENABLE_LOGGER)
add_compile_definitions(LOGGER)
endif()

if(FATAL_GL_ERRORS)
message("Raising exceptions on OpenGL errors")
add_compile_definitions(FATAL_GL_ERRORS)
endif()

include_directories(
"cfile" # TODO: Remove after untying all modules
"ddebug" # -*-
Expand Down
3 changes: 0 additions & 3 deletions Descent3/TelComAutoMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,6 @@ void TCAMCallback(void) {
rend_SetOverlayType(OT_NONE);
rend_SetZBufferState(1);

if (AM_terrain)
rend_SetZValues(0, VisibleTerrainZ);

TCAMBuildRoomList(Viewer_object->roomnum);
// Render all rooms in view
for (int i = 0; i < Num_AM_rooms; i++) {
Expand Down
23 changes: 2 additions & 21 deletions Descent3/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ void RenderFace(room *rp, int facenum) {
else
rend_SetColorModel(CM_RGB);
// Set lighting map
if ((fp->flags & FF_LIGHTMAP) && (!StateLimited || UseMultitexture)) {
if ((fp->flags & FF_LIGHTMAP) != 0) {
if (GameTextures[fp->tmap].flags & TF_SATURATE)
rend_SetOverlayType(OT_NONE);
else
Expand Down Expand Up @@ -2345,23 +2345,6 @@ void RenderRoomUnsorted(room *rp) {
for (i = rcount - 1; i >= 0; i--) {
RenderFace(rp, State_elements[i].facenum);
}

if (!UseMultitexture) {
// Since we're state limited, we have to render lightmap faces completely separate
// Now render lightmap faces
rend_SetAlphaType(AT_LIGHTMAP_BLEND);
rend_SetLighting(LS_GOURAUD);
rend_SetColorModel(CM_MONO);
rend_SetOverlayType(OT_NONE);
rend_SetTextureType(TT_PERSPECTIVE);
rend_SetWrapType(WT_CLAMP);
rend_SetMipState(0);
for (i = rcount - 1; i >= 0; i--) {
RenderLightmapFace(rp, State_elements[i].facenum);
}
rend_SetWrapType(WT_WRAP);
rend_SetMipState(1);
}
}
}

Expand Down Expand Up @@ -3462,12 +3445,10 @@ void RenderMine(int viewer_roomnum, int flag_automap, int called_from_terrain) {
g3_SetFarClipZ(VisibleTerrainZ);

if ((Terrain_sky.flags & TF_FOG) && (UseHardware || (!UseHardware && Lighting_on))) {
rend_SetZValues(0, VisibleTerrainZ);
rend_SetFogState(1);
rend_SetFogBorders(VisibleTerrainZ * .85, VisibleTerrainZ);
rend_SetFogColor(Terrain_sky.fog_color);
} else
rend_SetZValues(0, 5000);
}
}

// First render mirrored rooms
Expand Down
9 changes: 2 additions & 7 deletions Descent3/terrainrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1406,15 +1406,11 @@ void RenderTerrain(uint8_t from_mine, int left, int top, int right, int bot) {

#ifndef NEWEDITOR
if ((Terrain_sky.flags & TF_FOG) && (UseHardware || (!UseHardware && Lighting_on))) {
rend_SetZValues(0, VisibleTerrainZ);
rend_SetFogState(1);
rend_SetFogBorders(VisibleTerrainZ * Terrain_sky.fog_scalar, Far_fog_border);
rend_SetFogColor(Terrain_sky.fog_color);
} else
#endif
{
rend_SetZValues(0, 5000);
}
#endif

// And display!
if (nt > 0) {
Expand Down Expand Up @@ -2586,8 +2582,7 @@ void DisplayTerrainList(int cellcount, bool from_automap) {
rend_SetTextureType(TT_LINEAR);
rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
rend_SetLighting(LS_NONE);
if (!StateLimited || UseMultitexture)
draw_lightmap = true;
draw_lightmap = true;
}
RotateTerrainList(cellcount, from_automap);
if (!UseHardware) {
Expand Down
31 changes: 31 additions & 0 deletions cmake/shaders.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
file(WRITE ${shaders_h} "#pragma once
#include <string_view>
namespace shaders {
")

set(idx 0)
set(idx_is_path FALSE)
while(${idx} LESS ${CMAKE_ARGC})
if(idx_is_path)
set(shader_path ${CMAKE_ARGV${idx}})
cmake_path(GET shader_path STEM shader_name)
file(READ ${shader_path} content)
file(APPEND ${shaders_h} "
inline constexpr std::string_view ${shader_name} = R\"shader(${content}
)shader\";
")
else()
if(${CMAKE_ARGV${idx}} STREQUAL "--")
set(idx_is_path TRUE)
endif()
endif()
math(EXPR idx "${idx} + 1")
endwhile()

foreach(idx RANGE ${CMAKE_ARGC})
endforeach()

file(APPEND ${shaders_h} "
} // namespace shaders
")

1 change: 0 additions & 1 deletion editor/TextureGrWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ void CTextureGrWnd::TexGrStartOpenGL() {
// rend_SetOpenGLWindowState (1,Descent,NULL); // LGT: not defined anymore
rend_ClearScreen(0);
StateLimited = 1;
UseMultitexture = 0;
NoLightmaps = false;
}
}
Expand Down
9 changes: 0 additions & 9 deletions lib/3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,6 @@ struct g3Point {

// Functions in library

// 3d system startup and shutdown:

// initialize the 3d system
void g3_Init(void);

// close down the 3d system
void g3_Close(void);

// Frame setup functions:

// start the frame, specifying view position, matrix, & zoom
Expand Down Expand Up @@ -430,7 +422,6 @@ void g3_DrawSpecialLine(g3Point *p0, g3Point *p1);
// Draws a bitmap on a specific plane. Also does rotation. Angle of rotation is passed as 'rot_angle'
void g3_DrawPlanarRotatedBitmap(vector *pos, vector *norm, angle rot_angle, float width, float height, int bm);

void g3_TransformVert(float res[4], float pt[4], float a[4][4]);
void g3_TransformMult(float res[4][4], float a[4][4], float b[4][4]);
void g3_TransformTrans(float res[4][4], float t[4][4]);
void g3_GetModelViewMatrix(const vector *viewPos, const matrix *viewMatrix, float *mvMat);
Expand Down
46 changes: 1 addition & 45 deletions lib/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,6 @@ class oeApplication;

#define TEXTURE_WIDTH 128
#define TEXTURE_HEIGHT 128
#define TEXTURE_BPP 2

#define FLAT_SHADE_COLOR 0x7C01
// If an incoming texture has the above color in it, change that color to this color
#define REPLACEMENT_COLOR 0x07C0

extern int Triangles_drawn;

// Is this hardware or software rendered?
enum renderer_type {
Expand All @@ -327,29 +320,15 @@ extern renderer_type Renderer_type;

// renderer clear flags
#define RF_CLEAR_ZBUFFER 1
#define RF_CLEAR_COLOR 2

// Overlay texture settings
#define OT_NONE 0 // No overlay
#define OT_BLEND 1 // Draw a lightmap texture afterwards
#define OT_REPLACE 2 // Draw a tmap2 style texture afterwards
#define OT_FLAT_BLEND 3 // Draw a gouraud shaded polygon afterwards
#define OT_BLEND_VERTEX 4 // Like OT_BLEND, but take constant alpha into account
#define OT_BUMPMAP 5 // Draw a saturated bumpmap afterwards
#define OT_BLEND_SATURATE 6 // Add a lightmap in

extern uint8_t Overlay_type;
extern int Overlay_map;
extern int Bumpmap_ready, Bump_map;

extern float Z_bias;
extern bool UseHardware;
extern bool StateLimited;
extern bool NoLightmaps;
extern bool UseMultitexture;
extern bool UseWBuffer;
extern bool UseMipmap; // DAJ
extern bool ATIRagePro; // DAJ
extern bool Formac; // DAJ

class NewBitmap;

Expand All @@ -361,8 +340,6 @@ void rend_SetRendererType(renderer_type state);

#define MAP_TYPE_BITMAP 0
#define MAP_TYPE_LIGHTMAP 1
#define MAP_TYPE_BUMPMAP 2
#define MAP_TYPE_UNKNOWN 3

// lighting state
enum light_state {
Expand Down Expand Up @@ -406,19 +383,12 @@ enum texture_type {
#define AT_CONSTANT_TEXTURE_VERTEX 7 // Use all three (texture constant vertex)
#define AT_LIGHTMAP_BLEND 8 // dest*src colors
#define AT_SATURATE_TEXTURE 9 // Saturate up to white when blending
#define AT_FLAT_BLEND 10 // Like lightmap blend, but uses gouraud shaded flat polygon
#define AT_ANTIALIAS 11 // Draws an antialiased polygon
#define AT_SATURATE_VERTEX 12 // Saturation with vertices
#define AT_SATURATE_CONSTANT_VERTEX 13 // Constant*vertex saturation
#define AT_SATURATE_TEXTURE_VERTEX 14 // Texture * vertex saturation
#define AT_LIGHTMAP_BLEND_VERTEX 15 // Like AT_LIGHTMAP_BLEND, but take vertex alpha into account
#define AT_LIGHTMAP_BLEND_CONSTANT 16 // Like AT_LIGHTMAP_BLEND, but take constant alpha into account
#define AT_SPECULAR 32
#define AT_LIGHTMAP_BLEND_SATURATE 33 // Light lightmap blend, but add instead of multiply

#define LFB_LOCK_READ 0
#define LFB_LOCK_WRITE 1

enum wrap_type {
WT_WRAP, // Texture repeats
WT_CLAMP, // Texture clamps
Expand All @@ -430,8 +400,6 @@ struct rendering_state {

int8_t cur_bilinear_state;
int8_t cur_zbuffer_state;
int8_t cur_fog_state;
int8_t cur_mip_state;

texture_type cur_texture_type;
color_model cur_color_model;
Expand All @@ -440,13 +408,8 @@ struct rendering_state {

wrap_type cur_wrap_type;

float cur_fog_start, cur_fog_end;
float cur_near_z, cur_far_z;
float gamma_value;

int cur_alpha;
ddgr_color cur_color;
ddgr_color cur_fog_color;

int8_t cur_texture_quality; // 0-none, 1-linear, 2-perspective

Expand All @@ -467,7 +430,6 @@ struct renderer_preferred_state {
struct renderer_lfb {
int type;
uint16_t *data;
int bytes_per_row;
};

struct tRendererStats {
Expand Down Expand Up @@ -523,9 +485,6 @@ void rend_SetFiltering(int8_t state);
// Sets the state of zbuffering to on or off
void rend_SetZBufferState(int8_t state);

// Sets the near and far planes for z buffer
void rend_SetZValues(float nearz, float farz);

// Sets a bitmap as an overlay to rendered on top of the next texture map
void rend_SetOverlayMap(int handle);

Expand Down Expand Up @@ -641,9 +600,6 @@ int rend_InitOpenGLWindow(oeApplication *app, renderer_preferred_state *pref_sta
// Shuts down OpenGL in a window
void rend_CloseOpenGLWindow();

// Sets the state of the OpenGLWindow to on or off
void rend_SetOpenGLWindowState(int state, oeApplication *app, renderer_preferred_state *pref_state);

// Sets the hardware bias level for coplanar polygons
// This helps reduce z buffer artifaces
void rend_SetCoplanarPolygonOffset(float factor);
Expand Down
41 changes: 41 additions & 0 deletions misc/holder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Descent 3
* Copyright (C) 2024 Descent Developers
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <utility>

template <typename T, void (*Deleter)(T)>
struct MoveOnlyHolder {
MoveOnlyHolder(T t)
: t_{std::move(t)} {}

MoveOnlyHolder(MoveOnlyHolder&& other)
: t_{std::exchange(other.t_, {})} {}

~MoveOnlyHolder() noexcept {
Deleter(t_);
}

operator T() const {
return t_;
}

private:
T t_;
};
Loading

0 comments on commit 1026a6e

Please sign in to comment.