0029516: Visualization - eliminate redundant property Graphic3d_MaterialAspect::Refle...
[occt.git] / src / Graphic3d / Graphic3d_CLight.cxx
CommitLineData
992ed6b3 1// Copyright (c) 2017 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
14#include <Graphic3d_CLight.hxx>
15
16#include <Standard_Atomic.hxx>
17#include <Standard_OutOfRange.hxx>
18
19IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_CLight, Standard_Transient)
20
21namespace
22{
23 static volatile Standard_Integer THE_LIGHT_COUNTER = 0;
24}
25
26// =======================================================================
27// function : makeId
28// purpose :
29// =======================================================================
30void Graphic3d_CLight::makeId()
31{
32 TCollection_AsciiString aTypeSuffix;
33 switch (myType)
34 {
35 case Graphic3d_TOLS_AMBIENT: aTypeSuffix = "amb"; break;
36 case Graphic3d_TOLS_DIRECTIONAL: aTypeSuffix = "dir"; break;
37 case Graphic3d_TOLS_POSITIONAL: aTypeSuffix = "pos"; break;
38 case Graphic3d_TOLS_SPOT: aTypeSuffix = "spot"; break;
39 }
40
41 myId = TCollection_AsciiString ("Graphic3d_CLight_") + aTypeSuffix
42 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_LIGHT_COUNTER));
43}
44
45// =======================================================================
46// function : Graphic3d_CLight
47// purpose :
48// =======================================================================
49Graphic3d_CLight::Graphic3d_CLight (Graphic3d_TypeOfLightSource theType)
50: myPosition (0.0, 0.0, 0.0),
51 myColor (1.0f, 1.0f, 1.0f, 1.0f),
52 myDirection (0.0f, 0.0f, 0.0f, 0.0f),
53 myParams (0.0f, 0.0f, 0.0f, 0.0f),
54 mySmoothness (0.0f),
55 myIntensity (1.0f),
56 myType (theType),
57 myRevision (0),
58 myIsHeadlight(false),
59 myIsEnabled (true)
60{
61 switch (theType)
62 {
63 case Graphic3d_TOLS_AMBIENT:
64 {
65 break;
66 }
67 case Graphic3d_TOLS_DIRECTIONAL:
68 {
69 mySmoothness = 0.2f;
70 myIntensity = 20.0f;
71 break;
72 }
73 case Graphic3d_TOLS_POSITIONAL:
74 {
75 changeConstAttenuation() = 1.0f;
76 changeLinearAttenuation() = 0.0f;
77 break;
78 }
79 case Graphic3d_TOLS_SPOT:
80 {
81 changeConstAttenuation() = 1.0f;
82 changeLinearAttenuation() = 0.0f;
83 changeConcentration() = 1.0f;
84 changeAngle() = 0.523599f;
85 break;
86 }
87 }
88 makeId();
89}
90
91// =======================================================================
92// function : SetColor
93// purpose :
94// =======================================================================
95void Graphic3d_CLight::SetColor (const Quantity_Color& theColor)
96{
97 updateRevisionIf (myColor.GetRGB().IsDifferent (theColor));
98 myColor.SetRGB (theColor);
99}
100
101// =======================================================================
102// function : SetEnabled
103// purpose :
104// =======================================================================
105void Graphic3d_CLight::SetEnabled (Standard_Boolean theIsOn)
106{
107 updateRevisionIf (myIsEnabled != theIsOn);
108 myIsEnabled = theIsOn;
109}
110
111// =======================================================================
112// function : SetHeadlight
113// purpose :
114// =======================================================================
115void Graphic3d_CLight::SetHeadlight (Standard_Boolean theValue)
116{
117 updateRevisionIf (myIsHeadlight != theValue);
118 myIsHeadlight = theValue;
119}
120
121// =======================================================================
122// function : SetDirection
123// purpose :
124// =======================================================================
125void Graphic3d_CLight::SetDirection (const gp_Dir& theDir)
126{
127 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_SPOT
128 && myType != Graphic3d_TOLS_DIRECTIONAL,
129 "Graphic3d_CLight::SetDirection(), incorrect light type");
130 updateRevisionIf (Abs (myDirection.x() - static_cast<Standard_ShortReal> (theDir.X())) > ShortRealEpsilon()
131 || Abs (myDirection.y() - static_cast<Standard_ShortReal> (theDir.Y())) > ShortRealEpsilon()
132 || Abs (myDirection.z() - static_cast<Standard_ShortReal> (theDir.Z())) > ShortRealEpsilon());
133
134 myDirection.x() = static_cast<Standard_ShortReal> (theDir.X());
135 myDirection.y() = static_cast<Standard_ShortReal> (theDir.Y());
136 myDirection.z() = static_cast<Standard_ShortReal> (theDir.Z());
137}
138
139// =======================================================================
140// function : SetPosition
141// purpose :
142// =======================================================================
143void Graphic3d_CLight::SetPosition (const gp_Pnt& thePosition)
144{
145 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_SPOT
146 && myType != Graphic3d_TOLS_POSITIONAL,
147 "Graphic3d_CLight::SetDirection(), incorrect light type");
148 updateRevisionIf (!myPosition.IsEqual (thePosition, gp::Resolution()));
149 myPosition = thePosition;
150}
151
152// =======================================================================
153// function : SetIntensity
154// purpose :
155// =======================================================================
156void Graphic3d_CLight::SetIntensity (Standard_ShortReal theValue)
157{
158 Standard_OutOfRange_Raise_if (theValue <= 0.0f, "Graphic3d_CLight::SetIntensity(), Negative value for intensity");
159 updateRevisionIf (Abs (myIntensity - theValue) > ShortRealEpsilon());
160 myIntensity = theValue;
161}
162
163// =======================================================================
164// function : SetAngle
165// purpose :
166// =======================================================================
167void Graphic3d_CLight::SetAngle (Standard_ShortReal theAngle)
168{
169 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_SPOT,
170 "Graphic3d_CLight::SetAngle(), incorrect light type");
171 Standard_OutOfRange_Raise_if (theAngle <= 0.0 || theAngle >= M_PI,
172 "Graphic3d_CLight::SetAngle(), bad angle");
173 updateRevisionIf (Abs (changeAngle() - theAngle) > ShortRealEpsilon());
174 changeAngle() = theAngle;
175}
176
177// =======================================================================
178// function : SetAttenuation
179// purpose :
180// =======================================================================
181void Graphic3d_CLight::SetAttenuation (Standard_ShortReal theConstAttenuation,
182 Standard_ShortReal theLinearAttenuation)
183{
184 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_POSITIONAL
185 && myType != Graphic3d_TOLS_SPOT,
186 "Graphic3d_CLight::SetAttenuation(), incorrect light type");
187 Standard_OutOfRange_Raise_if (theConstAttenuation < 0.0f
188 || theLinearAttenuation < 0.0f
189 || theConstAttenuation + theLinearAttenuation == 0.0f, "Graphic3d_CLight::SetAttenuation(), bad coefficient");
190 updateRevisionIf (Abs (changeConstAttenuation() - theConstAttenuation) > ShortRealEpsilon()
191 || Abs (changeLinearAttenuation() - theLinearAttenuation) > ShortRealEpsilon());
192 changeConstAttenuation() = theConstAttenuation;
193 changeLinearAttenuation() = theLinearAttenuation;
194}
195
196// =======================================================================
197// function : SetConcentration
198// purpose :
199// =======================================================================
200void Graphic3d_CLight::SetConcentration (Standard_ShortReal theConcentration)
201{
202 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_SPOT, "Graphic3d_CLight::SetConcentration(), incorrect light type");
203 Standard_OutOfRange_Raise_if (theConcentration < 0.0f || theConcentration > 1.0f,
204 "Graphic3d_CLight::SetConcentration(), bad coefficient");
205 updateRevisionIf (Abs (changeConcentration() - theConcentration) > ShortRealEpsilon());
206 changeConcentration() = theConcentration;
207}
208
209// =======================================================================
210// function : SetSmoothRadius
211// purpose :
212// =======================================================================
213void Graphic3d_CLight::SetSmoothRadius (Standard_ShortReal theValue)
214{
215 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_POSITIONAL
216 && myType != Graphic3d_TOLS_SPOT,
217 "Graphic3d_CLight::SetSmoothRadius(), incorrect light type");
218 Standard_OutOfRange_Raise_if (theValue < 0.0f, "Graphic3d_CLight::SetSmoothRadius(), Bad value for smoothing radius");
219 updateRevisionIf (Abs (mySmoothness - theValue) > ShortRealEpsilon());
220 mySmoothness = theValue;
221}
222
223// =======================================================================
224// function : SetSmoothAngle
225// purpose :
226// =======================================================================
227void Graphic3d_CLight::SetSmoothAngle (Standard_ShortReal theValue)
228{
229 Standard_ProgramError_Raise_if (myType != Graphic3d_TOLS_DIRECTIONAL,
230 "Graphic3d_CLight::SetSmoothAngle(), incorrect light type");
231 Standard_OutOfRange_Raise_if (theValue < 0.0f || theValue > Standard_ShortReal(M_PI / 2.0),
232 "Graphic3d_CLight::SetSmoothAngle(), Bad value for smoothing angle");
233 updateRevisionIf (Abs (mySmoothness - theValue) > ShortRealEpsilon());
234 mySmoothness = theValue;
235}