1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 
18 #ifndef GLWIDGET_LIGHT_H
19 #define GLWIDGET_LIGHT_H
20 
21 #include <cmath> // for sqrt
22 #include <QString>
23 
24 class GLWidget_Light
25 {
26 public:
27     // default Light
28     GLWidget_Light(QString s = QString("unnammed light")) :
name_(s)29         name_(s),
30 
31         ambient_r(0.3f),
32         ambient_g(0.3f),
33         ambient_b(0.3f),
34         ambient_a(1.0f),
35 
36         diffuse_r(0.5f),
37         diffuse_g(0.5f),
38         diffuse_b(0.5f),
39         diffuse_a(1.0f),
40 
41         specular_r(0.5f),
42         specular_g(0.5f),
43         specular_b(0.5f),
44         specular_a(1.0f),
45 
46         position_x(0.0f),
47         position_y(0.0f),
48         position_z(0.0f),
49         position_w(1.0f),
50 
51         spotDirection_x(0.0f),
52         spotDirection_y(0.0f),
53         spotDirection_z(-1.0f),
54 
55         spotExponent(0.0f),
56         spotCutoff(180.0f),
57         constantAttenuation(1.0f),
58         linearAttenuation(0.0f),
59         quadraticAttenuation(0.0f)
60     {
61     }
62 
63     // diffuse Light
64     GLWidget_Light(float x, float y, float z, float r, float g, float b, QString s = QString("unnammed light")) :
name_(s)65         name_(s),
66 
67         ambient_r(0.1f),
68         ambient_g(0.1f),
69         ambient_b(0.1f),
70         ambient_a(1.0f),
71 
72         diffuse_r(r),
73         diffuse_g(g),
74         diffuse_b(b),
75         diffuse_a(1.0f),
76 
77         specular_r(r),
78         specular_g(g),
79         specular_b(b),
80         specular_a(1.0f),
81 
82         position_x(x),
83         position_y(y),
84         position_z(z),
85         position_w(1.0f),
86 
87         spotDirection_x(0.0f),
88         spotDirection_y(0.0f),
89         spotDirection_z(-1.0f),
90 
91         spotExponent(0.0f),
92         spotCutoff(180.0f),
93         constantAttenuation(1.0f),
94         linearAttenuation(0.0f),
95         quadraticAttenuation(0.0f)
96     {
97         lookAt(0,0,0);
98     }
99 
100     // ambiant Light
101     GLWidget_Light(float r, float g, float b, QString s = QString("unnammed light")) :
name_(s)102         name_(s),
103 
104         ambient_r(r),
105         ambient_g(g),
106         ambient_b(b),
107         ambient_a(1.0f),
108 
109         diffuse_r(0.0f),
110         diffuse_g(0.0f),
111         diffuse_b(0.0f),
112         diffuse_a(1.0f),
113 
114         specular_r(0.5f),
115         specular_g(0.5f),
116         specular_b(0.5f),
117         specular_a(1.0f),
118 
119         position_x(0.0f),
120         position_y(0.0f),
121         position_z(0.0f),
122         position_w(1.0f),
123 
124         spotDirection_x(0.0f),
125         spotDirection_y(0.0f),
126         spotDirection_z(-1.0f),
127 
128         spotExponent(0.0f),
129         spotCutoff(180.0f),
130         constantAttenuation(1.0f),
131         linearAttenuation(0.0f),
132         quadraticAttenuation(0.0f)
133     {
134         lookAt(0,0,0);
135     }
136 
lookAt(float x,float y,float z)137     void lookAt(float x, float y, float z)
138     {
139         spotDirection_x = x - position_x;
140         spotDirection_y = y - position_y;
141         spotDirection_z = z - position_z;
142 
143         float length = sqrt(
144                     spotDirection_x*spotDirection_x +
145                     spotDirection_y*spotDirection_y +
146                     spotDirection_z*spotDirection_z);
147         if(length > 0)
148         {
149             spotDirection_x /= length;
150             spotDirection_y /= length;
151             spotDirection_z /= length;
152         }
153         else
154         {
155             spotDirection_z = - 1;
156         }
157     }
158 
name()159     QString name() const {return name_;}
160 
161     QString name_;
162 
163     float ambient_r,
164     ambient_g,
165     ambient_b,
166     ambient_a,
167 
168     diffuse_r,
169     diffuse_g,
170     diffuse_b,
171     diffuse_a,
172 
173     specular_r,
174     specular_g,
175     specular_b,
176     specular_a,
177 
178     position_x,
179     position_y,
180     position_z,
181     position_w,
182 
183     spotDirection_x,
184     spotDirection_y,
185     spotDirection_z,
186 
187     spotExponent,
188     spotCutoff,
189     constantAttenuation,
190     linearAttenuation,
191     quadraticAttenuation;
192 };
193 
194 #endif
195