-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.cpp
165 lines (137 loc) · 3.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
/**
* https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLight.xml
*/
#include "Light.h"
#include <math.h>
TLight::TLight(GLenum _light, TLightStyle _style, TVector3D *pos) : TObject3D(pos)
{
num_light = _light;
SetStyle(_style);
Changed = true;
enabled = true;
if (pos)
SetPosition(pos);
else
position = {50.0, 150.0, 100.0};
Ambient = {0.2, 0.2, 0.2, 1.0};
Diffuse = {1.0, 1.0, 1.0, 1.0};
Specular = {0.0, 0.0, 0.0, 1.0}; // Not brilliant
model_ambient = {0.2, 0.2, 0.2, 1.0};
local_view = {0.0, 0.0, 0.0, 0.0};
}
// Render light
void TLight::Render()
{
if (!Changed || !enabled)
{
return;
}
Changed = false;
// Position
glLightfv(num_light, GL_POSITION, TVector4D(position, 0).Array());
// Style
glLightfv(num_light, GL_AMBIENT, Ambient.Array());
glLightfv(num_light, GL_DIFFUSE, Diffuse.Array());
glLightfv(num_light, GL_SPECULAR, Specular.Array());
glLightfv(num_light, GL_CONSTANT_ATTENUATION, &const_at);
glLightfv(num_light, GL_LINEAR_ATTENUATION, &lin_at);
glLightfv(num_light, GL_QUADRATIC_ATTENUATION, &quad_at);
if (style == Spot)
{
glLightfv(num_light, GL_SPOT_DIRECTION, spot_direction.Array());
glLightf(num_light, GL_SPOT_CUTOFF, spot_cutoff);
glLightf(num_light, GL_SPOT_EXPONENT, spot_expo);
}
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient.Array());
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view.Array());
if (enabled)
glEnable(num_light);
// Activation
glEnable(GL_LIGHTING);
}
/**
* The default color for GL_LIGHT0
* Note the default position is {0.0, 0.0, 1.0, 0.0}. Light is directional, parallel to z axis
*/
void TLight::SetDefaultColor()
{
Ambient = {0.0, 0.0, 0.0, 1.0};
Diffuse = {1.0, 1.0, 1.0, 1.0};
Specular = {1.0, 1.0, 1.0, 1.0};
model_ambient = {0.2, 0.2, 0.2, 1.0};
local_view = {0.0, 0.0, 0.0, 0.0}; // only the first parameter is used
Changed = true;
}
/**
* Change the color of the light
*/
void TLight::SetLightColor(const TLightColor _light, const TVector4D _color)
{
Changed = true;
switch (_light)
{
case lcAmbient: Ambient = _color; break;
case lcDiffuse: Diffuse = _color; break;
case lcSpecular: Specular = _color; break;
case lcModelAmbient: model_ambient = _color; break;
default : Changed = false;
}
}
bool TLight::CheckIncrement(GLfloat x, GLfloat _inc)
{
x += _inc;
return ((x >= 0.0) && (x <= 1.0));
}
void TLight::SetLightColor(const TLightColor _light, const GLfloat _inc)
{
Changed = true;
switch (_light)
{
case lcAmbient: if (CheckIncrement(Ambient.X, _inc)) Ambient += _inc; break;
case lcDiffuse: if (CheckIncrement(Diffuse.X, _inc)) Diffuse += _inc; break;
case lcSpecular: if (CheckIncrement(Specular.X, _inc)) Specular += _inc; break;
case lcModelAmbient: if (CheckIncrement(model_ambient.X, _inc)) model_ambient += _inc; break;
default : Changed = false;
}
}
/**
* Change the type of the light
*/
void TLight::SetStyle(TLightStyle _style)
{
style = _style;
const_at = 1.0;
lin_at = 0.0;
quad_at = 0.0;
if (style == Spot)
{
lin_at = 0.4;
quad_at = 0.1;
direction = {-1.0, -1.0, 0.0};
spot_cutoff = CUTOFF;
spot_expo = EXPO;
}
Changed = true;
};
void TLight::Disable()
{
Changed = true;
enabled = false;
glDisable(num_light);
}
void TLight::Enable()
{
Changed = true;
enabled = true;
glEnable(num_light);
}
void TLight::DoDisplay(TDisplayMode mode)
{
if (mode == dmSelect)
{
// nothing to display
}
}
// ********************************************************************************
// End of file
// ********************************************************************************