2014-10-14 13:11:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2023-08-06 21:15:48 +00:00
|
|
|
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-08-22 14:40:02 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
2014-10-14 13:11:37 +00:00
|
|
|
*
|
|
|
|
* 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://gnu.org/licenses
|
|
|
|
*/
|
2015-08-07 06:31:34 +00:00
|
|
|
|
|
|
|
#include "common/make_unique.h"
|
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
#include "graphics/core/device.h"
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
#include "graphics/engine/lightman.h"
|
|
|
|
|
2013-02-25 20:58:01 +00:00
|
|
|
#include <gtest/gtest.h>
|
2015-05-12 22:52:18 +00:00
|
|
|
#include <hippomocks.h>
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
using namespace Gfx;
|
|
|
|
using namespace HippoMocks;
|
|
|
|
namespace ph = std::placeholders;
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
class CLightManagerUT : public testing::Test
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
|
|
|
protected:
|
2015-10-03 20:05:14 +00:00
|
|
|
CLightManagerUT() :
|
2015-05-12 22:52:18 +00:00
|
|
|
m_engine(nullptr),
|
2015-08-14 21:11:24 +00:00
|
|
|
m_device(nullptr),
|
|
|
|
m_maxLightsCount(0)
|
2015-05-12 22:52:18 +00:00
|
|
|
{}
|
2015-10-03 20:05:14 +00:00
|
|
|
~CLightManagerUT() NOEXCEPT
|
2013-02-25 20:58:01 +00:00
|
|
|
{}
|
|
|
|
|
2015-06-25 22:24:32 +00:00
|
|
|
void SetUp() override;
|
2015-05-12 22:52:18 +00:00
|
|
|
|
2022-01-04 00:39:55 +00:00
|
|
|
void PrepareLightTesting(int maxLights, glm::vec3 eyePos);
|
2013-02-25 20:58:01 +00:00
|
|
|
void CheckLightSorting(EngineObjectType objectType, const std::vector<int>& expectedLights);
|
|
|
|
void CheckLight(int index, const Light& light);
|
|
|
|
void AddLight(int type, LightPriority priority, bool used, bool enabled,
|
2022-01-04 00:39:55 +00:00
|
|
|
glm::vec3 pos, EngineObjectType includeType, EngineObjectType excludeType);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
std::unique_ptr<CLightManager> m_lightManager;
|
|
|
|
MockRepository m_mocks;
|
|
|
|
CEngine* m_engine;
|
|
|
|
CDevice* m_device;
|
2013-02-25 20:58:01 +00:00
|
|
|
|
|
|
|
private:
|
2015-05-12 22:52:18 +00:00
|
|
|
std::vector<DynamicLight> m_dynamicLights;
|
|
|
|
std::vector<int> m_expectedLightTypes;
|
|
|
|
int m_maxLightsCount;
|
2013-02-25 20:58:01 +00:00
|
|
|
};
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
void CLightManagerUT::SetUp()
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
2015-05-12 22:52:18 +00:00
|
|
|
m_engine = m_mocks.Mock<CEngine>();
|
|
|
|
m_device = m_mocks.Mock<CDevice>();
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2023-08-13 15:39:12 +00:00
|
|
|
m_lightManager = std::make_unique<CLightManager>(m_engine);
|
2013-02-25 20:58:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 00:39:55 +00:00
|
|
|
void CLightManagerUT::PrepareLightTesting(int maxLights, glm::vec3 eyePos)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
2015-05-12 22:52:18 +00:00
|
|
|
m_maxLightsCount = maxLights;
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
m_mocks.OnCall(m_device, CDevice::GetMaxLightCount).Return(maxLights);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
m_lightManager->SetDevice(m_device);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
m_mocks.OnCall(m_device, CDevice::SetLight).Do(std::bind(&CLightManagerUT::CheckLight, this, ph::_1, ph::_2));
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
m_mocks.OnCall(m_engine, CEngine::GetEyePt).Return(eyePos);
|
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
void CLightManagerUT::CheckLightSorting(EngineObjectType objectType, const std::vector<int>& expectedLights)
|
2015-05-12 22:52:18 +00:00
|
|
|
{
|
|
|
|
m_expectedLightTypes = expectedLights;
|
|
|
|
|
|
|
|
for (int i = 0; i < m_maxLightsCount; ++i)
|
|
|
|
{
|
|
|
|
if (i < static_cast<int>( expectedLights.size() ))
|
|
|
|
{
|
|
|
|
m_mocks.ExpectCall(m_device, CDevice::SetLight).With(i, _);
|
|
|
|
m_mocks.ExpectCall(m_device, CDevice::SetLightEnabled).With(i, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_mocks.ExpectCall(m_device, CDevice::SetLightEnabled).With(i, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lightManager->UpdateDeviceLights(objectType);
|
2013-02-25 20:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
void CLightManagerUT::CheckLight(int index, const Light& light)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
2015-05-12 22:52:18 +00:00
|
|
|
ASSERT_TRUE(index >= 0 && index < static_cast<int>( m_expectedLightTypes.size() ));
|
|
|
|
ASSERT_EQ(m_expectedLightTypes[index], light.type);
|
2013-02-25 20:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
void CLightManagerUT::AddLight(int type, LightPriority priority, bool used, bool enabled,
|
2022-01-04 00:39:55 +00:00
|
|
|
glm::vec3 pos, EngineObjectType includeType, EngineObjectType excludeType)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
2015-05-12 22:52:18 +00:00
|
|
|
int rank = m_lightManager->CreateLight(priority);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
|
|
|
Light light;
|
|
|
|
light.type = static_cast<LightType>(type);
|
2013-02-28 20:26:09 +00:00
|
|
|
light.position = pos;
|
2015-05-12 22:52:18 +00:00
|
|
|
m_lightManager->SetLight(rank, light);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2015-05-12 22:52:18 +00:00
|
|
|
m_lightManager->SetLightEnabled(rank, enabled);
|
|
|
|
m_lightManager->SetLightIncludeType(rank, includeType);
|
|
|
|
m_lightManager->SetLightExcludeType(rank, excludeType);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
|
|
|
if (!used)
|
2015-05-12 22:52:18 +00:00
|
|
|
m_lightManager->DeleteLight(rank);
|
2013-02-25 20:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
TEST_F(CLightManagerUT, LightSorting_UnusedOrDisabledAreSkipped)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
|
|
|
const int lightCount = 10;
|
2022-01-04 00:39:55 +00:00
|
|
|
const glm::vec3 eyePos(0.0f, 0.0f, 0.0f);
|
2013-02-25 20:58:01 +00:00
|
|
|
PrepareLightTesting(lightCount, eyePos);
|
|
|
|
|
2022-01-04 00:39:55 +00:00
|
|
|
AddLight(1, LIGHT_PRI_LOW, false, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(2, LIGHT_PRI_LOW, true, false, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(3, LIGHT_PRI_LOW, false, false, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
|
|
|
std::vector<int> expectedLights;
|
|
|
|
CheckLightSorting(ENG_OBJTYPE_TERRAIN, expectedLights);
|
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
TEST_F(CLightManagerUT, LightSorting_IncludeTypesAreIncluded)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
|
|
|
const int lightCount = 10;
|
2022-01-04 00:39:55 +00:00
|
|
|
const glm::vec3 eyePos(0.0f, 0.0f, 0.0f);
|
2013-02-25 20:58:01 +00:00
|
|
|
PrepareLightTesting(lightCount, eyePos);
|
|
|
|
|
2022-01-04 00:39:55 +00:00
|
|
|
AddLight(1, LIGHT_PRI_LOW, true, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(2, LIGHT_PRI_LOW, true, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_TERRAIN, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(3, LIGHT_PRI_LOW, true, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_QUARTZ, ENG_OBJTYPE_NULL);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2013-03-10 14:44:21 +00:00
|
|
|
std::vector<int> expectedLights = { 1, 2 };
|
2013-02-25 20:58:01 +00:00
|
|
|
CheckLightSorting(ENG_OBJTYPE_TERRAIN, expectedLights);
|
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
TEST_F(CLightManagerUT, LightSorting_ExcludeTypesAreExcluded)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
|
|
|
const int lightCount = 10;
|
2022-01-04 00:39:55 +00:00
|
|
|
const glm::vec3 eyePos(0.0f, 0.0f, 0.0f);
|
2013-02-25 20:58:01 +00:00
|
|
|
PrepareLightTesting(lightCount, eyePos);
|
|
|
|
|
2022-01-04 00:39:55 +00:00
|
|
|
AddLight(1, LIGHT_PRI_LOW, true, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(2, LIGHT_PRI_LOW, true, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_TERRAIN);
|
|
|
|
AddLight(3, LIGHT_PRI_LOW, true, true, glm::vec3(0.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_QUARTZ);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2013-03-10 14:44:21 +00:00
|
|
|
std::vector<int> expectedLights = { 1, 3 };
|
2013-02-25 20:58:01 +00:00
|
|
|
CheckLightSorting(ENG_OBJTYPE_TERRAIN, expectedLights);
|
|
|
|
}
|
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
TEST_F(CLightManagerUT, LightSorting_SortingAccordingToDistance)
|
2013-02-25 20:58:01 +00:00
|
|
|
{
|
|
|
|
const int lightCount = 3;
|
2022-01-04 00:39:55 +00:00
|
|
|
const glm::vec3 eyePos(0.0f, 0.0f, 0.0f);
|
2013-02-25 20:58:01 +00:00
|
|
|
PrepareLightTesting(lightCount, eyePos);
|
|
|
|
|
2022-01-04 00:39:55 +00:00
|
|
|
AddLight(1, LIGHT_PRI_HIGH, true, true, glm::vec3(10.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(2, LIGHT_PRI_LOW, true, true, glm::vec3(4.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(3, LIGHT_PRI_HIGH, true, true, glm::vec3(20.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(4, LIGHT_PRI_LOW, true, true, glm::vec3(11.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(5, LIGHT_PRI_LOW, true, true, glm::vec3(100.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
|
|
|
AddLight(6, LIGHT_PRI_HIGH, true, true, glm::vec3(21.0f, 0.0f, 0.0f), ENG_OBJTYPE_NULL, ENG_OBJTYPE_NULL);
|
2013-02-25 20:58:01 +00:00
|
|
|
|
2013-02-28 20:26:09 +00:00
|
|
|
std::vector<int> expectedLights = { 2, 1, 3 };
|
2013-02-25 20:58:01 +00:00
|
|
|
CheckLightSorting(ENG_OBJTYPE_TERRAIN, expectedLights);
|
|
|
|
}
|