Skip to content

Commit

Permalink
#5893: Move LightInteractions class implementation to separate source…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
codereader committed Jan 27, 2022
1 parent df10ec6 commit 328e9bc
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 136 deletions.
1 change: 1 addition & 0 deletions radiantcore/CMakeLists.txt
Expand Up @@ -217,6 +217,7 @@ add_library(radiantcore MODULE
rendersystem/backend/glprogram/GLSLDepthFillAlphaProgram.cpp
rendersystem/backend/BuiltInShader.cpp
rendersystem/backend/ColourShader.cpp
rendersystem/backend/LightInteractions.cpp
rendersystem/backend/OpenGLShader.cpp
rendersystem/backend/OpenGLShaderPass.cpp
rendersystem/backend/DepthFillPass.cpp
Expand Down
131 changes: 0 additions & 131 deletions radiantcore/rendersystem/LightInteraction.h

This file was deleted.

5 changes: 3 additions & 2 deletions radiantcore/rendersystem/OpenGLRenderSystem.cpp
Expand Up @@ -10,9 +10,9 @@
#include "backend/GLProgramFactory.h"
#include "backend/BuiltInShader.h"
#include "backend/ColourShader.h"
#include "backend/LightInteractions.h"
#include "debugging/debugging.h"
#include "LightingModeRenderResult.h"
#include "LightInteraction.h"

#include <functional>

Expand Down Expand Up @@ -299,7 +299,8 @@ IRenderResult::Ptr OpenGLRenderSystem::renderLitScene(RenderStateFlags globalFla
setupViewMatrices(view.GetModelview(), view.GetProjection());

std::size_t visibleLights = 0;
std::vector<LightInteractionList> interactionLists;
std::vector<LightInteractions> interactionLists;
interactionLists.reserve(_lights.size());

// Gather all visible lights and render the surfaces touched by them
for (const auto& light : _lights)
Expand Down
100 changes: 100 additions & 0 deletions radiantcore/rendersystem/backend/LightInteractions.cpp
@@ -0,0 +1,100 @@
#include "LightInteractions.h"

#include "OpenGLShader.h"

namespace render
{

void LightInteractions::addSurface(IRenderableSurface& surface, IRenderEntity& entity, OpenGLShader& shader)
{
auto& surfacesByMaterial = _surfacesByEntity.emplace(
&entity, SurfacesByMaterial{}).first->second;

auto& surfaces = surfacesByMaterial.emplace(
&shader, SurfaceList{}).first->second;

surfaces.emplace_back(std::ref(surface));
}

void LightInteractions::render(OpenGLState& state, RenderStateFlags globalFlagsMask, const IRenderView& view, std::size_t renderTime)
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

// Render surfaces without any vertex colours(?)
glDisableClientState(GL_COLOR_ARRAY);

glFrontFace(GL_CCW);

for (auto& pair : _surfacesByEntity)
{
auto entity = pair.first;

for (auto& pair : pair.second)
{
auto shader = pair.first;
auto& surfaceList = pair.second;

if (!shader->isVisible()) continue;

shader->foreachPass([&](OpenGLShaderPass& pass)
{
// Reset the texture matrix
glMatrixMode(GL_TEXTURE);
glLoadMatrixd(Matrix4::getIdentity());

glMatrixMode(GL_MODELVIEW);

// Apply our state to the current state object
pass.applyState(state, globalFlagsMask, view.getViewer(), renderTime, entity);

RenderInfo info(state.getRenderFlags(), view.getViewer(), state.cubeMapMode);

for (auto surface : surfaceList)
{
if (state.glProgram)
{
OpenGLShaderPass::setUpLightingCalculation(state, &_light,
view.getViewer(), surface.get().getSurfaceTransform(), renderTime, state.isColourInverted());
}

if (surface.get().getSurfaceTransform().getHandedness() == Matrix4::RIGHTHANDED)
{
glFrontFace(GL_CW);
}
else
{
glFrontFace(GL_CCW);
}

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glMultMatrixd(surface.get().getSurfaceTransform());

const auto& vertices = surface.get().getVertices();
const auto& indices = surface.get().getIndices();

glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().vertex);
glColorPointer(4, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().colour);

glVertexAttribPointer(ATTR_NORMAL, 3, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().normal);
glVertexAttribPointer(ATTR_TEXCOORD, 2, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().texcoord);
glVertexAttribPointer(ATTR_TANGENT, 3, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().tangent);
glVertexAttribPointer(ATTR_BITANGENT, 3, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().bitangent);

glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indices.size()), GL_UNSIGNED_INT, &indices.front());

glPopMatrix();
}
});
}
}

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}

}
45 changes: 45 additions & 0 deletions radiantcore/rendersystem/backend/LightInteractions.h
@@ -0,0 +1,45 @@
#pragma once

#include <map>
#include <vector>
#include "irender.h"
#include "isurfacerenderer.h"
#include "irenderview.h"

namespace render
{

class OpenGLShader;

/**
* Defines interactions between a light and one or more entity surfaces
* It only lives through the course of a single render pass, therefore direct
* references without ref-counting are used.
*
* Surfaces are grouped by entity, then by shader.
*/
class LightInteractions
{
private:
RendererLight& _light;

// A flat list of surfaces
using SurfaceList = std::vector<std::reference_wrapper<IRenderableSurface>>;

// All surfaces, grouped by material
using SurfacesByMaterial = std::map<OpenGLShader*, SurfaceList>;

// SurfaceLists, grouped by entity
std::map<IRenderEntity*, SurfacesByMaterial> _surfacesByEntity;

public:
LightInteractions(RendererLight& light) :
_light(light)
{}

void addSurface(IRenderableSurface& surface, IRenderEntity& entity, OpenGLShader& shader);

void render(OpenGLState& state, RenderStateFlags globalFlagsMask, const IRenderView& view, std::size_t renderTime);
};

}
3 changes: 2 additions & 1 deletion tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -631,6 +631,7 @@
<ClCompile Include="..\..\radiantcore\rendersystem\backend\glprogram\GLSLDepthFillAlphaProgram.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\glprogram\GLSLDepthFillProgram.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\glprogram\GLSLProgramBase.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\LightInteractions.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\OpenGLShader.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\OpenGLShaderPass.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\debug\SpacePartitionRenderer.cpp" />
Expand Down Expand Up @@ -992,6 +993,7 @@
<ClInclude Include="..\..\radiantcore\rendersystem\backend\glprogram\GLSLDepthFillAlphaProgram.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\glprogram\GLSLDepthFillProgram.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\glprogram\GLSLProgramBase.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\LightInteractions.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\OpenGLShader.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\OpenGLShaderPass.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\OpenGLStateLess.h" />
Expand All @@ -1001,7 +1003,6 @@
<ClInclude Include="..\..\radiantcore\rendersystem\debug\SpacePartitionRenderer.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\GLFont.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\LightingModeRenderResult.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\LightInteraction.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\OpenGLModule.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\OpenGLRenderSystem.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\RenderSystemFactory.h" />
Expand Down
7 changes: 5 additions & 2 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -1120,6 +1120,9 @@
<ClCompile Include="..\..\radiantcore\rendersystem\backend\ColourShader.cpp">
<Filter>src\rendersystem\backend</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\rendersystem\backend\LightInteractions.cpp">
<Filter>src\rendersystem\backend</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\radiantcore\modulesystem\ModuleLoader.h">
Expand Down Expand Up @@ -2298,8 +2301,8 @@
<ClInclude Include="..\..\radiantcore\entity\RenderableSurfaceCollection.h">
<Filter>src\entity</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\rendersystem\LightInteraction.h">
<Filter>src\rendersystem</Filter>
<ClInclude Include="..\..\radiantcore\rendersystem\backend\LightInteractions.h">
<Filter>src\rendersystem\backend</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 328e9bc

Please sign in to comment.