-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightSource.h
More file actions
107 lines (86 loc) · 2.37 KB
/
LightSource.h
File metadata and controls
107 lines (86 loc) · 2.37 KB
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
#pragma once
#include "Shape.h"
#include "Sphere.h"
#include "Cuboid.h"
#include "ScreenLogger.h"
namespace engine
{
class LightSource
{
public:
LightSource(GLenum id, float radius = 1.0f)
: m_light_id(id), m_emission(),
m_ambient(), m_specular(), m_diffuse(),
m_cell(new Sphere(radius))
{}
LightSource(GLenum id, float width, float height, float depth)
: m_light_id(id), m_emission(),
m_ambient(), m_specular(), m_diffuse(),
m_cell(new Cuboid(width, height, depth))
{}
~LightSource() {
delete m_cell;
}
LightSource& emission(const vector4f params) {
memmove(m_emission, params, sizeof(vector4f));
return (*this);
}
LightSource& emission(std::array<float, 4> params) {
return emission(params.data());
}
LightSource& materialv(GLenum pname, const vector4f params) {
m_cell->materialv(pname, params);
return (*this);
}
LightSource& materialv(GLenum pname, const std::array<float, 4>& params) {
return materialv(pname, params.data());
}
LightSource& lightv(GLenum pname, const vector4f params)
{
float* light_type;
if (pname == GL_AMBIENT)
light_type = m_ambient;
else if (pname == GL_SPECULAR)
light_type = m_specular;
else if (pname == GL_DIFFUSE)
light_type = m_diffuse;
else {
logger.logWarning(
"WARNING: undefined lighting type in shape's \"%X\" meterial", this);
return (*this);
}
memmove(light_type, params, sizeof(vector4f));
return (*this);
}
LightSource& lightv(GLenum pname, const std::array<float, 4>& params) {
return lightv(pname, params.data());
}
LightSource& spawn(float x, float y, float z)
{
vector4f position = { x, y, z, 1.0f };
glPushMatrix();
{
//Create sun's materials for color and light
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, m_emission);
m_cell->shininess(100).spawn(x, y, z);
//Create light as a directional spotlight
glLightfv(m_light_id, GL_POSITION, position);
glLightfv(m_light_id, GL_DIFFUSE, m_diffuse);
glLightfv(m_light_id, GL_SPECULAR, m_specular);
}
glPopMatrix();
return (*this);
}
LightSource& spawn(const vector3f pos) {
return spawn(pos[0], pos[1], pos[2]);
}
private:
vector4f m_emission;
vector4f m_ambient;
vector4f m_specular;
vector4f m_diffuse;
GLenum m_light_id;
Shape* m_cell;
inline static ScreenLogger& logger = ScreenLogger::getInstance();
};
}