Skip to content

Commit

Permalink
#5893: Add separate entry point to RenderSystem for drawing the scene…
Browse files Browse the repository at this point in the history
… in lighting mode.
  • Loading branch information
codereader committed Jan 27, 2022
1 parent c1996a6 commit da0a46f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 41 deletions.
26 changes: 26 additions & 0 deletions include/irender.h
Expand Up @@ -2,6 +2,7 @@

#include "imodule.h"
#include "ivolumetest.h"
#include "irenderview.h"
#include "iwindingrenderer.h"
#include "igeometryrenderer.h"
#include "isurfacerenderer.h"
Expand Down Expand Up @@ -627,6 +628,16 @@ enum class ColourShaderType
CameraAndOrthoview,
};

class IRenderResult
{
public:
using Ptr = std::shared_ptr<IRenderResult>;

virtual ~IRenderResult() {}

virtual std::string toString() = 0;
};

/**
* \brief
* The main interface for the backend renderer.
Expand Down Expand Up @@ -726,6 +737,21 @@ class RenderSystem
const Vector3& viewer,
const VolumeTest& volume) = 0;

/**
* Render the scene based on the light-entity interactions.
* All the active lights and entities must have added themselves
* to this rendersystem at this point, using addEntity().
*
* \param globalFlagsMask
* The mask of render flags which are permitted during this render pass. Any
* render flag which is 0 in this mask will not be enabled during rendering,
* even if the particular shader requests it.
*
* \returns A result object which can be used to display a statistics summary.
*/
virtual IRenderResult::Ptr renderLitScene(RenderStateFlags globalFlagsMask,
const render::IRenderView& view) = 0;

virtual void realise() = 0;
virtual void unrealise() = 0;

Expand Down
40 changes: 29 additions & 11 deletions radiant/camera/CamWnd.cpp
Expand Up @@ -784,6 +784,8 @@ void CamWnd::Cam_Draw()
| RENDER_POLYGONSTIPPLE;
}

IRenderResult::Ptr result;

// Main scene render
{
_renderer->prepare();
Expand All @@ -802,12 +804,20 @@ void CamWnd::Cam_Draw()
i.second->render(GlobalRenderSystem(), *_renderer, _view);
}

// Back end (submit to shaders and do the actual render)
_renderer->submitToShaders(
getCameraSettings()->getRenderMode() == RENDER_MODE_LIGHTING
);
GlobalRenderSystem().render(RenderViewType::Camera, allowedRenderFlags,
_camera->getModelView(), _camera->getProjection(), _view.getViewer(), _view);
if (getCameraSettings()->getRenderMode() == RENDER_MODE_LIGHTING)
{
// Lit mode
result = GlobalRenderSystem().renderLitScene(allowedRenderFlags, _view);
}
else
{
// Back end (submit to shaders and do the actual render)
_renderer->submitToShaders(
getCameraSettings()->getRenderMode() == RENDER_MODE_LIGHTING
);
GlobalRenderSystem().render(RenderViewType::Camera, allowedRenderFlags,
_camera->getModelView(), _camera->getProjection(), _view.getViewer(), _view);
}

_renderer->cleanup();
}
Expand Down Expand Up @@ -876,11 +886,19 @@ void CamWnd::Cam_Draw()
// Render the stats and timing text. This may include culling stats in
// debug builds.
glRasterPos3f(4.0f, static_cast<float>(_camera->getDeviceHeight()) - 4.0f, 0.0f);
std::string statString = _view.getCullStats();
if (!statString.empty())
statString += " | ";
statString += _renderStats.getStatString();
_glFont->drawString(statString);

if (result)
{
_glFont->drawString(result->toString());
}
else
{
std::string statString = _view.getCullStats();
if (!statString.empty())
statString += " | ";
statString += _renderStats.getStatString();
_glFont->drawString(statString);
}

drawTime();

Expand Down
22 changes: 22 additions & 0 deletions radiantcore/rendersystem/LightingModeRenderResult.h
@@ -0,0 +1,22 @@
#pragma once

#include "irender.h"
#include "fmt/format.h"

namespace render
{

class LightingModeRenderResult :
public IRenderResult
{
public:
std::size_t visibleLights = 0;
std::size_t skippedLights = 0;

std::string toString() override
{
return fmt::format("Lights: {0} of {1}", visibleLights, skippedLights);
}
};

}
109 changes: 79 additions & 30 deletions radiantcore/rendersystem/OpenGLRenderSystem.cpp
Expand Up @@ -11,6 +11,7 @@
#include "backend/BuiltInShader.h"
#include "backend/ColourShader.h"
#include "debugging/debugging.h"
#include "LightingModeRenderResult.h"

#include <functional>

Expand Down Expand Up @@ -161,26 +162,10 @@ ShaderPtr OpenGLRenderSystem::capture(ColourShaderType type, const Colour4& colo
});
}

/*
* Render all states in the ShaderCache along with their renderables. This
* is where the actual OpenGL rendering starts.
*/
void OpenGLRenderSystem::render(RenderViewType renderViewType,
RenderStateFlags globalstate,
const Matrix4& modelview,
const Matrix4& projection,
const Vector3& viewer,
const VolumeTest& view)
void OpenGLRenderSystem::beginRendering(OpenGLState& state)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);

// Set the projection and modelview matrices
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(projection);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(modelview);

// global settings that are not set in renderstates
glFrontFace(GL_CW);
glCullFace(GL_BACK);
Expand All @@ -204,18 +189,13 @@ void OpenGLRenderSystem::render(RenderViewType renderViewType,
glDisableVertexAttribArrayARB(c_attr_Binormal);
}

if (globalstate & RENDER_TEXTURE_2D) {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

// Construct default OpenGL state
OpenGLState current;
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// Set up initial GL state. This MUST MATCH the defaults in the OpenGLState
// object, otherwise required state changes may not occur.
glLineStipple(current.m_linestipple_factor,
current.m_linestipple_pattern);
glLineStipple(state.m_linestipple_factor,
state.m_linestipple_pattern);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
Expand Down Expand Up @@ -243,14 +223,47 @@ void OpenGLRenderSystem::render(RenderViewType renderViewType,
glDisable(GL_POLYGON_OFFSET_FILL); // greebo: otherwise tiny gap lines between brushes are visible

glBindTexture(GL_TEXTURE_2D, 0);
glColor4f(1,1,1,1);
glDepthFunc(current.getDepthFunc());
glColor4f(1, 1, 1, 1);
glDepthFunc(state.getDepthFunc());
glAlphaFunc(GL_ALWAYS, 0);
glLineWidth(1);
glPointSize(1);

glHint(GL_FOG_HINT, GL_NICEST);
glDisable(GL_FOG);
}

void OpenGLRenderSystem::setupViewMatrices(const Matrix4& modelview, const Matrix4& projection)
{
// Set the projection and modelview matrices
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(projection);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(modelview);
}

void OpenGLRenderSystem::finishRendering()
{
glPopAttrib();
}

/*
* Render all states in the ShaderCache along with their renderables. This
* is where the actual OpenGL rendering starts.
*/
void OpenGLRenderSystem::render(RenderViewType renderViewType,
RenderStateFlags globalstate,
const Matrix4& modelview,
const Matrix4& projection,
const Vector3& viewer,
const VolumeTest& view)
{
// Construct default OpenGL state
OpenGLState current;
beginRendering(current);

setupViewMatrices(modelview, projection);

// Iterate over the sorted mapping between OpenGLStates and their
// OpenGLShaderPasses (containing the renderable geometry), and render the
Expand All @@ -269,15 +282,51 @@ void OpenGLRenderSystem::render(RenderViewType renderViewType,
pair.second->clearRenderables();
}

renderText();

finishRendering();
}

IRenderResult::Ptr OpenGLRenderSystem::renderLitScene(RenderStateFlags globalFlagsMask,
const IRenderView& view)
{
auto result = std::make_shared<LightingModeRenderResult>();

// Construct default OpenGL state
OpenGLState current;
beginRendering(current);
setupViewMatrices(view.GetModelview(), view.GetProjection());

std::size_t visibleLights = 0;

// Gather all visible lights and render the surfaces touched by them
for (const auto& light : _lights)
{
if (view.TestAABB(light->lightAABB()) == VOLUME_OUTSIDE)
{
result->skippedLights++;
continue;
}

result->visibleLights++;

// TODO: Insert rendering code here
}

finishRendering();

return result;
}

void OpenGLRenderSystem::renderText()
{
// Render all text
glDisable(GL_DEPTH_TEST);

for (const auto& [_, textRenderer] : _textRenderers)
{
textRenderer->render();
}

glPopAttrib();
}

void OpenGLRenderSystem::realise()
Expand Down
11 changes: 11 additions & 0 deletions radiantcore/rendersystem/OpenGLRenderSystem.h
Expand Up @@ -76,6 +76,8 @@ class OpenGLRenderSystem final
const Matrix4& projection,
const Vector3& viewer,
const VolumeTest& view) override;
IRenderResult::Ptr renderLitScene(RenderStateFlags globalFlagsMask,
const IRenderView& view) override;
void realise() override;
void unrealise() override;

Expand Down Expand Up @@ -120,6 +122,15 @@ class OpenGLRenderSystem final
void shutdownModule() override;

private:
// Set up initial GL states, will push all attrib states
void beginRendering(OpenGLState& state);

// Will pop attrib states
void finishRendering();

void setupViewMatrices(const Matrix4& modelview, const Matrix4& projection);
void renderText();

ShaderPtr capture(const std::string& name, const std::function<OpenGLShaderPtr()>& createShader);
};

Expand Down
1 change: 1 addition & 0 deletions tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -999,6 +999,7 @@
<ClInclude Include="..\..\radiantcore\rendersystem\backend\TextRenderer.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\debug\SpacePartitionRenderer.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\GLFont.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\LightingModeRenderResult.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\OpenGLModule.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\OpenGLRenderSystem.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\RenderSystemFactory.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -2292,5 +2292,8 @@
<ClInclude Include="..\..\radiantcore\rendersystem\backend\ColourShader.h">
<Filter>src\rendersystem\backend</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\rendersystem\LightingModeRenderResult.h">
<Filter>src\rendersystem</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit da0a46f

Please sign in to comment.