-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.cpp
403 lines (321 loc) · 11.8 KB
/
Light.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "Light.h"
#include <cstring>
#include "Math.h"
struct LightProperties
{
bool isEnabled;
bool pad1[3];
bool enableDiffuse;
bool pad2[3];
bool enableSpecular;
bool pad3[3];
bool isSpotlight;
bool pad4[3];
bool isPointlight;
bool pad5[3];
bool specularMode;
bool pad6[11];
float ambientLight[3];
bool pad7;
float diffuseLight[3];
bool pad8;
float specularLight[3];
bool pad9;
float shininess;
float strength;
float position[3]; //For directional lights this is instead the direction the light points, yup terrible naming I know.
bool pad10;
float normal[3]; //Direction spotlights are pointing
bool pad11;
float angle; //How many degrees left and right of the light normal does this light `shine` it's in cos(theta) form
float linearAtten; //= 1.0;
float quadAtten; // = 1.0;
float constAtten; // = 0.1;
float spotponent; // = 2.0;
};
Light::Light(int index, GLsizei offset, GLint bufferSize, GLint numElements, const char *uniformNames[numUniforms], GLuint *indices, GLint *sizes, GLint *offsets, GLint *types)
{
this->index = index;
this->dataSize = bufferSize;
lightData = new char[bufferSize];
memset(lightData, 0, bufferSize);
for(int i = 0; i < numElements; i++)
{
std::string lightUniformName(uniformNames[i]);
std::size_t pos = lightUniformName.find('.');
pos++;
std::string uniformName = lightUniformName.substr(pos);
UBOUniform uniformData(indices[i], sizes[i], offsets[i] - offset, types[i]);
uniforms[uniformName] = uniformData;
}
}
Light::Light(const Light ©)
{
dataSize = copy.dataSize;
lightData = new char[dataSize];
this->index = copy.index;
memset(lightData, 0, dataSize);
memcpy(lightData, copy.lightData, dataSize);
for(std::map<std::string, UBOUniform>::const_iterator it = copy.uniforms.begin(); it != copy.uniforms.end(); it++)
{
uniforms[it->first] = it->second;
}
}
Light::~Light()
{
if(lightData)
{
delete[] lightData;
lightData = NULL;
}
}
Light &Light::operator=(const Light &other)
{
if(this != &other)
{
delete[] lightData;
dataSize = other.dataSize;
lightData = new char[dataSize];
this->index = other.index;
memset(lightData, 0, dataSize);
memcpy(lightData, other.lightData, dataSize);
for(std::map<std::string, UBOUniform>::const_iterator it = other.uniforms.begin(); it != other.uniforms.end(); it++)
{
uniforms[it->first] = it->second;
}
}
return *this;
}
void Light::setIsEnabled(GLboolean enabled)
{
UBOUniform isEnabled = uniforms[std::string("isEnabled")];
memcpy(lightData + isEnabled.getOffset(), &enabled, sizeof(GLboolean));
}
void Light::enableDiffuse(GLboolean enabled)
{
UBOUniform diffuse = uniforms[std::string("enableDiffuse")];
size_t size = diffuse.getSize() * typeSize(diffuse.getType());
memcpy(lightData + diffuse.getOffset(), &enabled, sizeof(enabled));
}
void Light::enableSpecular(GLboolean enabled)
{
UBOUniform specular = uniforms[std::string("enableSpecular")];
size_t size = specular.getSize() * typeSize(specular.getType());
memcpy(lightData + specular.getOffset(), &enabled, sizeof(enabled));
}
void Light::setIsSpotLight(GLboolean enabled)
{
UBOUniform spotLight = uniforms[std::string("isSpotlight")];
memcpy(lightData + spotLight.getOffset(), &enabled, sizeof(enabled));
}
void Light::setIsPointLight(GLboolean enabled)
{
UBOUniform pointLightUbo = uniforms[std::string("isPointlight")];
memcpy(lightData + pointLightUbo.getOffset(), &enabled, sizeof(enabled));
}
void Light::setSpecularMode(GLboolean specularMode)
{
UBOUniform specularModeUbo = uniforms[std::string("specularMode")];
memcpy(lightData + specularModeUbo.getOffset(), &specularMode, sizeof(specularMode));
}
void Light::setAmbientLight(Vec4<GLfloat> light)
{
UBOUniform ambientLight = uniforms[std::string("ambientLight")];
size_t size = ambientLight.getSize() * typeSize(ambientLight.getType());
memcpy(lightData + ambientLight.getOffset(), light.getData(), size);
}
void Light::setDiffuseLight(Vec4<GLfloat> light)
{
UBOUniform diffuseLight = uniforms[std::string("diffuseLight")];
struct LightProperties* sss = (struct LightProperties*)0;
int *asdf = (int*)&sss->diffuseLight;
size_t size = diffuseLight.getSize() * typeSize(diffuseLight.getType());
struct LightProperties *blah = (struct LightProperties*)lightData;
size_t test = sizeof(struct LightProperties);
memcpy(lightData + diffuseLight.getOffset(), light.getData(), size);
}
void Light::setSpecularLight(Vec4<GLfloat> light)
{
UBOUniform specularLight = uniforms[std::string("specularLight")];
size_t size = specularLight.getSize() * typeSize(specularLight.getType());
memcpy(lightData + specularLight.getOffset(), light.getData(), size);
}
void Light::setShininess(GLfloat shininess)
{
UBOUniform shininessUBO = uniforms[std::string("shininess")];
memcpy(lightData + shininessUBO.getOffset(), &shininess, sizeof(GLfloat));
}
void Light::setStrength(GLfloat strength)
{
UBOUniform strengthUbo = uniforms[std::string("strength")];
memcpy(lightData + strengthUbo.getOffset(), &strength, sizeof(strength));
}
void Light::setPosition(Vec4<GLfloat> pos)
{
UBOUniform position = uniforms[std::string("position")];
size_t size = position.getSize() * typeSize(position.getType());
memcpy(lightData + position.getOffset(), pos.getData(), size);
}
void Light::setNormal(Vec4<GLfloat> normal)
{
UBOUniform normalUbo = uniforms[std::string("normal")];
size_t size = normalUbo.getSize() * typeSize(normalUbo.getType());
memcpy(lightData + normalUbo.getOffset(), normal.getData(), size);
}
void Light::setAngle(GLfloat angle)
{
GLfloat radians = Math::toRadians(angle);
UBOUniform angleUbo = uniforms[std::string("angle")];
memcpy(lightData + angleUbo.getOffset(), &radians, sizeof(radians));
}
void Light::setLinearAtten(GLfloat linearAtten)
{
UBOUniform linearAttenUbo = uniforms[std::string("linearAtten")];
memcpy(lightData + linearAttenUbo.getOffset(), &linearAtten, sizeof(linearAtten));
}
void Light::setQuadAtten(GLfloat quadAtten)
{
UBOUniform quadAttenUbo = uniforms[std::string("quadAtten")];
memcpy(lightData + quadAttenUbo.getOffset(), &quadAtten, sizeof(quadAtten));
}
void Light::setConstAttenuation(GLfloat constAtten)
{
UBOUniform constAttenUbo = uniforms[std::string("constAtten")];
memcpy(lightData + constAttenUbo.getOffset(), &constAtten, sizeof(constAtten));
}
void Light::setSpotponent(GLfloat spotponent)
{
UBOUniform spotponentUbo = uniforms[std::string("spotponent")];
memcpy(lightData + spotponentUbo.getOffset(), &spotponent, sizeof(spotponent));
}
char *Light::getLightData()
{
return lightData;
}
size_t Light::typeSize(GLenum type)
{
size_t size;
#define CASE(Enum, Count,Type) case Enum: size = Count * sizeof(Type); break
switch(type)
{
CASE(GL_FLOAT, 1, GLfloat);
CASE(GL_BOOL, 1, GLboolean);
CASE(GL_FLOAT_VEC3, 3, GLfloat);
}
return size;
}
GLboolean Light::getIsEnabled()
{
GLboolean enabled;
UBOUniform enabledUBO = uniforms[std::string("isEnabled")];
memcpy(&enabled, lightData + enabledUBO.getOffset(), sizeof(enabled));
return enabled;
}
GLboolean Light::getIsDiffuseEnabled()
{
GLboolean diffuse;
UBOUniform diffuseUbo = uniforms[std::string("enableDiffuse")];
memcpy(&diffuse, lightData + diffuseUbo.getOffset(), sizeof(diffuse));
return diffuse;
}
GLboolean Light::getIsSpecularEnabled()
{
GLboolean specular;
UBOUniform specularUBO = uniforms[std::string("enableSpecular")];
memcpy(&specular, lightData + specularUBO.getOffset(), sizeof(GLboolean));
return specular;
}
GLboolean Light::getIsSpotLight()
{
GLboolean spotLight;
UBOUniform spotLightUbo = uniforms[std::string("isSpotlight")];
memcpy(&spotLight, lightData + spotLightUbo.getOffset(), sizeof(spotLight));
}
GLboolean Light::getIsPointLight()
{
GLboolean pointLight;
UBOUniform pointLightUbo = uniforms[std::string("isPointlight")];
memcpy(&pointLight, lightData + pointLightUbo.getOffset(), sizeof(pointLight));
return pointLight;
}
GLboolean Light::getSpecularMode()
{
GLboolean specularMode;
UBOUniform specularModeUbo = uniforms[std::string("specularMode")];
memcpy(&specularMode, lightData + specularModeUbo.getOffset(), sizeof(specularMode));
return specularMode;
}
Vec4<GLfloat> Light::getAmbientLight()
{
GLfloat ambientLight[3];
UBOUniform ambientLightUbo = uniforms[std::string("ambientLight")];
size_t size = ambientLightUbo.getSize() * typeSize(ambientLightUbo.getType());
memcpy(ambientLight, lightData + ambientLightUbo.getOffset(), size);
return Vec4<GLfloat>(ambientLight[0], ambientLight[1], ambientLight[2], 0.0f);
}
Vec4<GLfloat> Light::getDiffuseLight()
{
GLfloat diffuseLight[4];
UBOUniform diffuseLightUbo = uniforms[std::string("diffuseLight")];
size_t size = diffuseLightUbo.getSize() * typeSize(diffuseLightUbo.getType());
memcpy(diffuseLight, lightData + diffuseLightUbo.getOffset(), size);
return Vec4<GLfloat>(diffuseLight[0], diffuseLight[1], diffuseLight[2], 0.0f);
}
Vec4<GLfloat> Light::getSpecularLight()
{
GLfloat specularLight[4];
UBOUniform specularLightUbo = uniforms[std::string("specularLight")];
size_t size = specularLightUbo.getSize() * typeSize(specularLightUbo.getType());
memcpy(specularLight, lightData + specularLightUbo.getOffset(), size);
return Vec4<GLfloat>(specularLight[0], specularLight[1], specularLight[2], 0.0f);
}
GLfloat Light::getShininess()
{
GLfloat shininess;
UBOUniform shininessUBO = uniforms[std::string("shininess")];
memcpy(&shininess, lightData + shininessUBO.getOffset(), sizeof(shininess));
return shininess;
}
GLfloat Light::getStrength()
{
GLfloat strength;
UBOUniform strengthUBO = uniforms[std::string("strength")];
memcpy(&strength, lightData + strengthUBO.getOffset(), sizeof(strength));
return strength;
}
GLfloat Light::getLinearAtten()
{
GLfloat linearAtten;
UBOUniform linearAttenUBO = uniforms[std::string("linearAtten")];
memcpy(&linearAtten, lightData + linearAttenUBO.getOffset(), sizeof(linearAtten));
return linearAtten;
}
GLfloat Light::getQuadAtten()
{
GLfloat quadAtten;
UBOUniform quadAttenUbo = uniforms[std::string("quadAtten")];
memcpy(&quadAtten, lightData + quadAttenUbo.getOffset(), sizeof(quadAtten));
return quadAtten;
}
GLfloat Light::getSpotponent()
{
GLfloat spotponent;
UBOUniform spotponentUbo = uniforms[std::string("spotponent")];
memcpy(&spotponent, lightData + spotponentUbo.getOffset(), sizeof(spotponent));
return spotponent;
}
GLfloat Light::getConstAttenuation()
{
GLfloat constAtten;
UBOUniform constAttenUbo = uniforms[std::string("constAtten")];
memcpy(&constAtten, lightData + constAttenUbo.getOffset(), sizeof(constAtten));
return constAtten;
}
int Light::getIndex()
{
return index;
}
size_t Light::getDataSize()
{
return dataSize;
}