OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
lightsource.cpp
Go to the documentation of this file.
1#include "lightsource.h"
2
3#include <cstring>
4
5using namespace Tempest;
6
7static Vec3 toVec3(uint32_t v) {
8 uint8_t cl[4];
9 std::memcpy(cl,&v,4);
10 return Vec3(cl[0]/255.f,cl[1]/255.f,cl[2]/255.f);
11 }
12
15
16void LightSource::setDir(const Tempest::Vec3& d) {
17 float l = d.length();
18 if(l>0){
19 ldir = d/l;
20 } else {
21 ldir = Vec3();
22 }
23 }
24
25void LightSource::setDir(float x, float y, float z) {
26 setDir({x,y,z});
27 }
28
29void LightSource::setColor(const Vec3& cl) {
30 clr = cl;
31 curClr = clr;
32 colorAniListFpsInv = 0;
33 }
34
35void LightSource::setColor(const std::vector<uint32_t>& arr, float fps, bool smooth) {
36 colorSmooth = smooth;
37 if(arr.size()==1) {
38 setColor(toVec3(arr[0]));
39 colorAniListFpsInv = 0;
40 return;
41 }
42
43 colorAniList.resize(arr.size());
44 for(size_t i=0; i<arr.size(); ++i)
45 colorAniList[i] = toVec3(arr[i]);
46 colorAniListFpsInv = arr.size()>0 ? uint64_t(1000.0/fps) : 0;
47 }
48
49void LightSource::setColor(const std::vector<zenkit::Color>& arr, float fps, bool smooth) {
50 colorSmooth = smooth;
51 if(arr.size()==1) {
52 setColor({static_cast<float>(arr[0].r)/255.f, static_cast<float>(arr[0].g)/255.f, static_cast<float>(arr[0].b)/255.f});
53 colorAniListFpsInv = 0;
54 return;
55 }
56
57 colorAniList.resize(arr.size());
58 for(size_t i=0; i<arr.size(); ++i)
59 colorAniList[i] = {static_cast<float>(arr[i].r)/255.f, static_cast<float>(arr[i].g)/255.f, static_cast<float>(arr[i].b)/255.f};
60 colorAniListFpsInv = !arr.empty() ? uint64_t(1000.0/fps) : 0;
61 }
62
63void LightSource::setColor(const std::vector<Vec3>& arr, float fps, bool smooth) {
64 colorSmooth = smooth;
65 if(arr.size()==0) {
66 setColor(Vec3());
67 colorAniListFpsInv = 0;
68 return;
69 }
70 if(arr.size()==1) {
71 setColor(arr[0]);
72 colorAniListFpsInv = 0;
73 return;
74 }
75 colorAniList = arr;
76 colorAniListFpsInv = arr.size()>0 ? uint64_t(1000.0/fps) : 0;
77 }
78
79void LightSource::setRange(float r) {
80 rgn = r;
81 curRgn = rgn;
82 rangeAniFPSInv = 0;
83 }
84
85void LightSource::setRange(const std::vector<float>& arr, float base, float fps, bool smooth) {
86 rangeSmooth = smooth;
87 if(arr.size()==1) {
88 setRange(arr[0]*base);
89 return;
90 }
91 rangeAniScale = arr;
92 for(auto& i:rangeAniScale)
93 i*=base;
94
95 rangeAniFPSInv = arr.size() > 0 ? uint64_t(1000.0/fps) : 0;
96 if(rangeAniScale.size()>0) {
97 rgn = rangeAniScale[0];
98 for(auto i:rangeAniScale)
99 rgn = std::max(rgn,i);
100 }
101 }
102
104 enable = e;
105 }
106
107void LightSource::update(uint64_t time) {
108 if(timeOff<time)
109 time -= timeOff; else
110 time = 0;
111
112 if(rangeAniFPSInv==0) {
113 curRgn = rgn;
114 } else {
115 size_t frame = size_t(time/rangeAniFPSInv), mod = size_t(time%rangeAniFPSInv);
116 float a = float(mod)/float(rangeAniFPSInv);
117 if(!rangeSmooth)
118 a = 0;
119
120 float r0 = rangeAniScale[(frame )%rangeAniScale.size()];
121 float r1 = rangeAniScale[(frame+1)%rangeAniScale.size()];
122 curRgn = r0+(r1-r0)*a;
123 }
124
125 if(colorAniListFpsInv==0) {
126 curClr = clr;
127 } else {
128 size_t frame = size_t(time/colorAniListFpsInv), mod = size_t(time%colorAniListFpsInv);
129 float a = float(mod)/float(colorAniListFpsInv);
130 if(!colorSmooth)
131 a = 0;
132
133 Vec3 cl0 = colorAniList[(frame )%colorAniList.size()];
134 Vec3 cl1 = colorAniList[(frame+1)%colorAniList.size()];
135 curClr = cl0+(cl1-cl0)*a;
136 }
137 }
138
140 return rangeAniFPSInv!=0 || colorAniListFpsInv!=0;
141 }
142
144 return enable;
145 }
146
148 timeOff = t;
149 }
150
152 uint64_t t0 = colorAniList .size()*colorAniListFpsInv;
153 uint64_t t1 = rangeAniScale.size()*rangeAniFPSInv;
154 return std::max(t0,t1);
155 }
156
157void LightSource::setDebugName(std::string_view hint) {
158#ifndef NDEBUG
159 dbgHint = hint;
160#else
161 (void)hint;
162#endif
163 }
164
165std::string_view LightSource::debugName() const {
166#ifndef NDEBUG
167 return dbgHint;
168#else
169 return "";
170#endif
171 }
void setDebugName(std::string_view hint)
void update(uint64_t time)
void setEnabled(bool e)
uint64_t effectPrefferedTime() const
void setDir(const Tempest::Vec3 &d)
std::string_view debugName() const
bool isDynamic() const
bool isEnabled() const
void setColor(const Tempest::Vec3 &cl)
void setRange(float r)
void setTimeOffset(uint64_t t)
static Vec3 toVec3(uint32_t v)