Skip to content

Commit

Permalink
many lights
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Dec 3, 2023
1 parent 5941050 commit d9dc7b4
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 23 deletions.
19 changes: 6 additions & 13 deletions sources/scenes/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,21 @@ void sceneReload()
for (int i = 0; i < directionalLightsCount; i++)
{
directionalLights[i] = engineEntities()->createAnonymous();
TransformComponent &ts = directionalLights[i]->value<TransformComponent>();
ts.orientation = Quat(Degs(randomChance() * -20 - 30), Degs(i * 360.0f / (float)directionalLightsCount + randomChance() * 180 / (float)directionalLightsCount), Degs());
directionalLights[i]->value<TransformComponent>().orientation = Quat(Degs(randomChance() * -20 - 30), Degs(i * 360.0f / (float)directionalLightsCount + randomChance() * 180 / (float)directionalLightsCount), Degs());
LightComponent &ls = directionalLights[i]->value<LightComponent>();
ls.color = Vec3(1);
ls.intensity = 2.5;
ls.lightType = LightTypeEnum::Directional;
ShadowmapComponent &ss = directionalLights[i]->value<ShadowmapComponent>();
ss.resolution = 2048;
directionalLights[i]->value<ShadowmapComponent>().resolution = 2048;
}

// point lights
for (int i = 0; i < pointLightsCount; i++)
{
pointLights[i] = engineEntities()->createAnonymous();
TransformComponent &ts = pointLights[i]->value<TransformComponent>();
ts.position = (Vec3(randomChance(), randomChance(), randomChance()) * 2 - 1) * 15;
LightComponent &ls = pointLights[i]->value<LightComponent>();
ls.color = colorHsvToRgb(Vec3(randomChance(), 1, 1));
ls.lightType = LightTypeEnum::Point;
RenderComponent &r = pointLights[i]->value<RenderComponent>();
r.color = ls.color;
r.object = HashString("scenes/common/lightbulb.obj");
Entity *e = pointLights[i] = engineEntities()->createAnonymous();
e->value<TransformComponent>().position = (Vec3(randomChance(), randomChance(), randomChance()) * 2 - 1) * 15;
e->value<RenderComponent>().color = e->value<LightComponent>().color = colorHsvToRgb(Vec3(randomChance(), 1, 1));
e->value<RenderComponent>().object = HashString("scenes/common/lightbulb.obj");
}
}

Expand Down
1 change: 0 additions & 1 deletion sources/simple/explosions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ void makeExplosion(const Vec3 &position)
l.color += randomRange3(-0.05, 0.05);
l.color = clamp(l.color, 0, 1);
l.intensity = randomRange(2.0, 4.0);
l.attenuation = Vec3(0, 0.5, 0.5);
}
}
{
Expand Down
3 changes: 1 addition & 2 deletions sources/simple/lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ int main(int argc, char *args[])
l.lightType = LightTypeEnum::Spot;
l.spotAngle = Degs(40);
l.spotExponent = 40;
l.attenuation = Vec3(1, 0, 0.005);
l.intensity = 3;
l.intensity = 100;
ShadowmapComponent &s = e->value<ShadowmapComponent>();
s.resolution = 1024;
s.worldSize = Vec3(3, 50, 0);
Expand Down
115 changes: 115 additions & 0 deletions sources/simple/manyLights.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <cage-core/assetManager.h>
#include <cage-core/entitiesVisitor.h>
#include <cage-core/hashString.h>
#include <cage-core/logger.h>
#include <cage-engine/highPerformanceGpuHint.h>
#include <cage-engine/scene.h>
#include <cage-engine/window.h>
#include <cage-simple/engine.h>
#include <cage-simple/fpsCamera.h>
#include <cage-simple/statisticsGui.h>

using namespace cage;
constexpr uint32 assetsName = HashString("cage-tests/translucent/translucent.pack");
constexpr uint32 lightsCount = 500;

struct MovingComponent
{
Vec3 velocity;
};

void update()
{
entitiesVisitor(
[](TransformComponent &t, MovingComponent &mv)
{
t.position += mv.velocity;
if (length(t.position) > 10)
mv.velocity *= -1;
},
engineEntities(), false);
}

int main(int argc, char *args[])
{
try
{
// log to console
Holder<Logger> log1 = newLogger();
log1->format.bind<logFormatConsole>();
log1->output.bind<logOutputStdOut>();

engineInitialize(EngineCreateConfig());

// events
const auto updateListener = controlThread().update.listen(&update);
const auto closeListener = engineWindow()->events.listen(inputListener<InputClassEnum::WindowClose, InputWindow>([](auto) { engineStop(); }));

// window
engineWindow()->setMaximized();
engineWindow()->title("many lights");

// entities
EntityManager *ents = engineEntities();
ents->defineComponent(MovingComponent());
{ // camera
Entity *e = ents->create(1);
TransformComponent &t = e->value<TransformComponent>();
t.position = Vec3(8, 6, 10);
t.orientation = Quat(Degs(-30), Degs(40), Degs());
CameraComponent &c = e->value<CameraComponent>();
c.near = 0.1;
c.far = 1000;
c.ambientColor = Vec3(1);
c.ambientIntensity = 0.2;
}
{ // sun
Entity *e = ents->create(2);
TransformComponent &t = e->value<TransformComponent>();
t.orientation = Quat(Degs(-50), Degs(-42 + 180), Degs());
t.position = Vec3(0, 5, 0);
LightComponent &l = e->value<LightComponent>();
l.lightType = LightTypeEnum::Directional;
l.color = Vec3(1, 1, 0.5);
l.intensity = 2;
ShadowmapComponent &s = e->value<ShadowmapComponent>();
s.resolution = 2048;
s.worldSize = Vec3(15);
}
{ // floor
Entity *e = ents->create(5);
e->value<RenderComponent>().object = HashString("cage-tests/translucent/shapes.blend?Ground");
e->value<TransformComponent>().position = Vec3(0, -0.2, 0);
}
// lights
for (uint32 i = 0; i < lightsCount; i++)
{
Entity *e = ents->createAnonymous();
e->value<TransformComponent>().position = (randomChance3() * 2 - 1) * 5 * Vec3(1, 0, 1);
e->value<TransformComponent>().scale = 0.03;
e->value<MovingComponent>().velocity = randomDirection3() * 0.02 * Vec3(1, 0, 1);
e->value<LightComponent>().color = randomChance3() * 0.5 + 0.5;
e->value<LightComponent>().intensity = randomRange(0.01, 3.0);
e->value<RenderComponent>().object = HashString("cage-tests/translucent/shapes.blend?Bulb");
e->value<RenderComponent>().color = e->value<LightComponent>().color;
e->value<RenderComponent>().intensity = e->value<LightComponent>().intensity;
}

Holder<FpsCamera> cameraCtrl = newFpsCamera(ents->get(1));
cameraCtrl->mouseButton = MouseButtonsFlags::Left;
cameraCtrl->movementSpeed = 0.3;
Holder<StatisticsGui> statistics = newStatisticsGui();

engineAssets()->add(assetsName);
engineRun();
engineAssets()->remove(assetsName);
engineFinalize();

return 0;
}
catch (...)
{
detail::logCurrentCaughtException();
return 1;
}
}
3 changes: 0 additions & 3 deletions sources/simple/screamers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ void makeExplosion(const Vec3 &position)
l.color += randomRange3(-0.05, 0.05);
l.color = saturate(l.color);
l.intensity = 8;
l.attenuation = Vec3(0, 1, 0);
}
}
{
Expand All @@ -120,7 +119,6 @@ void makeExplosion(const Vec3 &position)
l.color += randomRange3(-0.05, 0.05);
l.color = saturate(l.color);
l.intensity = 5;
l.attenuation = Vec3(0, 1, 0);
}
}
{
Expand Down Expand Up @@ -180,7 +178,6 @@ void spawnScreamer()
snd.startTime = engineControlTime();

LightComponent &lig = e->value<LightComponent>();
lig.attenuation = Vec3(0, 1, 0);
render.color = lig.color = randomDirection3() * 0.5 + 0.5;
lig.intensity = 5;

Expand Down
4 changes: 1 addition & 3 deletions sources/simple/translucent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ int main(int argc, char *args[])
l.lightType = LightTypeEnum::Spot;
l.spotAngle = Degs(60);
l.spotExponent = 40;
l.attenuation = Vec3(0, 0, 0.03);
l.color = Vec3(1);
l.intensity = 15;
ShadowmapComponent &s = e->value<ShadowmapComponent>();
Expand Down Expand Up @@ -247,8 +246,7 @@ int main(int argc, char *args[])
LightComponent &l = e->value<LightComponent>();
l.lightType = LightTypeEnum::Point;
r.color = l.color = colorHsvToRgb(randomChance3() * Vec3(1, .5, .5) + Vec3(0, .5, .5));
l.intensity = 3;
l.attenuation = Vec3(0.5, 0, 0.07);
l.intensity = 20;
}
for (uint32 j = 0; j < 3; j++)
{
Expand Down

0 comments on commit d9dc7b4

Please sign in to comment.