1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 /***********************************************************************
17 Classe V3d_SpotLight :
18 HISTORIQUE DES MODIFICATIONS :
19 --------------------------------
20 00-09-92 : GG ; Creation.
21 ************************************************************************/
22 /*----------------------------------------------------------------------*/
27 #include <Graphic3d_Structure.hxx>
28 #include <Quantity_Color.hxx>
29 #include <Standard_Type.hxx>
31 #include <V3d_BadValue.hxx>
32 #include <V3d_Light.hxx>
33 #include <V3d_View.hxx>
34 #include <V3d_Viewer.hxx>
36 // =======================================================================
37 // function : V3d_Light
39 // =======================================================================
40 V3d_Light::V3d_Light (const Handle(V3d_Viewer)& theViewer)
42 SetType (V3d_AMBIENT);
43 theViewer->AddLight (this);
46 // =======================================================================
49 // =======================================================================
50 void V3d_Light::SetType (const V3d_TypeOfLight theType)
52 myLight.Type = (Graphic3d_TypeOfLightSource)theType;
55 // =======================================================================
56 // function : SetColor
58 // =======================================================================
59 void V3d_Light::SetColor (const Quantity_TypeOfColor theType,
60 const Standard_Real theValue1,
61 const Standard_Real theValue2,
62 const Standard_Real theValue3)
64 Standard_Real aValue1 = Max (0., Min (1., theValue1));
65 Standard_Real aValue2 = Max (0., Min (1., theValue2));
66 Standard_Real aValue3 = Max (0., Min (1., theValue3));
67 Quantity_Color aColor (aValue1, aValue2, aValue3, theType);
71 // =======================================================================
72 // function : SetColor
74 // =======================================================================
75 void V3d_Light::SetColor (const Quantity_NameOfColor theName)
77 SetColor (Quantity_Color (theName));
80 // =======================================================================
81 // function : SetColor
83 // =======================================================================
84 void V3d_Light::SetColor (const Quantity_Color& theColor)
86 myLight.Color.r() = static_cast<Standard_ShortReal> (theColor.Red());
87 myLight.Color.g() = static_cast<Standard_ShortReal> (theColor.Green());
88 myLight.Color.b() = static_cast<Standard_ShortReal> (theColor.Blue());
91 // =======================================================================
94 // =======================================================================
95 void V3d_Light::Color (const Quantity_TypeOfColor theType,
96 Standard_Real& theValue1,
97 Standard_Real& theValue2,
98 Standard_Real& theValue3) const
100 Color().Values (theValue1, theValue2, theValue3, theType);
103 // =======================================================================
106 // =======================================================================
107 void V3d_Light::Color (Quantity_NameOfColor& theName) const
109 theName = Color().Name();
112 // =======================================================================
115 // =======================================================================
116 Quantity_Color V3d_Light::Color() const
118 return Quantity_Color (Standard_Real (myLight.Color.r()),
119 Standard_Real (myLight.Color.g()),
120 Standard_Real (myLight.Color.b()),
124 // =======================================================================
127 // =======================================================================
128 V3d_TypeOfLight V3d_Light::Type() const
130 return (V3d_TypeOfLight)myLight.Type;
133 // =======================================================================
134 // function : SetIntensity
136 // =======================================================================
137 void V3d_Light::SetIntensity (const Standard_Real theValue)
139 Standard_ASSERT_RAISE (theValue > 0.,
140 "V3d_Light::SetIntensity, "
141 "Negative value for intensity");
143 myLight.Intensity = static_cast<Standard_ShortReal> (theValue);
146 // =======================================================================
147 // function : Intensity
149 // =======================================================================
150 Standard_Real V3d_Light::Intensity() const
152 return myLight.Intensity;
155 // =======================================================================
156 // function : Smoothness
158 // =======================================================================
159 Standard_Real V3d_Light::Smoothness() const
161 return myLight.Smoothness;
164 // =======================================================================
165 // function : Headlight
167 // =======================================================================
168 Standard_Boolean V3d_Light::Headlight() const
170 return myLight.IsHeadlight;
173 // =======================================================================
174 // function : SetHeadlight
176 // =======================================================================
177 void V3d_Light::SetHeadlight (const Standard_Boolean theValue)
179 myLight.IsHeadlight = theValue;
182 // =======================================================================
183 // function : SymetricPointOnSphere
185 // =======================================================================
186 void V3d_Light::SymetricPointOnSphere (const Handle(V3d_View)& aView, const Graphic3d_Vertex &Center, const Graphic3d_Vertex &aPoint, const Standard_Real Rayon, Standard_Real& X, Standard_Real& Y, Standard_Real& Z, Standard_Real& VX, Standard_Real& VY, Standard_Real& VZ ) {
188 Standard_Real X0,Y0,Z0,XP,YP,ZP;
189 Standard_Real PXP,PYP,DeltaX,DeltaY,DeltaZ;
190 Standard_Real A,B,C,Delta,Lambda;
191 Standard_Integer IPX,IPY;
193 Center.Coord(X0,Y0,Z0);
194 aPoint.Coord(XP,YP,ZP);
195 aView->Project(XP,YP,ZP,PXP,PYP);
196 aView->Convert(PXP,PYP,IPX,IPY);
197 aView->ProjReferenceAxe(IPX,IPY,X,Y,Z,VX,VY,VZ);
202 // The point of intersection of straight lines defined by :
203 // - Straight line passing by the point of projection and the eye
204 // if this is a perspective, parralel to the normal of the view
205 // if this is an axonometric view.
206 // position in the view is parallel to the normal of the view
207 // - The distance position of the target camera is equal to the radius.
209 A = VX*VX + VY*VY + VZ*VZ ;
210 B = -2. * (VX*DeltaX + VY*DeltaY + VZ*DeltaZ);
211 C = DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ
213 Delta = B*B - 4.*A*C;
215 Lambda = (-B + Sqrt(Delta))/(2.*A);
216 if ( Lambda >= -0.0001 && Lambda <= 0.0001 )
217 Lambda = (-B - Sqrt(Delta))/(2.*A);
223 X = XP; Y = YP; Z = ZP;
227 // =======================================================================
228 // function : IsDisplayed
230 // =======================================================================
231 Standard_Boolean V3d_Light::IsDisplayed() const
233 if (myGraphicStructure.IsNull())
235 return Standard_False;
238 return myGraphicStructure->IsDisplayed();