0024776: Visualization - inherit OpenGl_View from Graphic3d_CView
[occt.git] / src / V3d / V3d_Light.cxx
1 // Copyright (c) 1999-2014 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 /***********************************************************************
15      FONCTION :
16      ----------
17         Classe V3d_SpotLight :
18      HISTORIQUE DES MODIFICATIONS   :
19      --------------------------------
20       00-09-92 : GG  ; Creation.
21 ************************************************************************/
22 /*----------------------------------------------------------------------*/
23 /*
24  * Includes
25  */
26
27 #include <Graphic3d_Structure.hxx>
28 #include <Quantity_Color.hxx>
29 #include <Standard_Type.hxx>
30 #include <V3d.hxx>
31 #include <V3d_BadValue.hxx>
32 #include <V3d_Light.hxx>
33 #include <V3d_View.hxx>
34 #include <V3d_Viewer.hxx>
35
36 // =======================================================================
37 // function : V3d_Light
38 // purpose  :
39 // =======================================================================
40 V3d_Light::V3d_Light (const Handle(V3d_Viewer)& theViewer)
41 {
42   SetType (V3d_AMBIENT);
43   theViewer->AddLight (this);
44 }
45
46 // =======================================================================
47 // function : SetType
48 // purpose  :
49 // =======================================================================
50 void V3d_Light::SetType (const V3d_TypeOfLight theType)
51 {
52   myLight.Type = (Graphic3d_TypeOfLightSource)theType;
53 }
54
55 // =======================================================================
56 // function : SetColor
57 // purpose  :
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)
63 {
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);
68   SetColor (aColor);
69 }
70
71 // =======================================================================
72 // function : SetColor
73 // purpose  :
74 // =======================================================================
75 void V3d_Light::SetColor (const Quantity_NameOfColor theName)
76 {
77   SetColor (Quantity_Color (theName));
78 }
79
80 // =======================================================================
81 // function : SetColor
82 // purpose  :
83 // =======================================================================
84 void V3d_Light::SetColor (const Quantity_Color& theColor)
85 {
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());
89 }
90
91 // =======================================================================
92 // function : Color
93 // purpose  :
94 // =======================================================================
95 void V3d_Light::Color (const Quantity_TypeOfColor theType,
96                        Standard_Real& theValue1,
97                        Standard_Real& theValue2,
98                        Standard_Real& theValue3) const
99 {
100   Color().Values (theValue1, theValue2, theValue3, theType);
101 }
102
103 // =======================================================================
104 // function : Color
105 // purpose  :
106 // =======================================================================
107 void V3d_Light::Color (Quantity_NameOfColor& theName) const
108 {
109   theName = Color().Name();
110 }
111
112 // =======================================================================
113 // function : Color
114 // purpose  :
115 // =======================================================================
116 Quantity_Color V3d_Light::Color() const
117 {
118   return Quantity_Color (Standard_Real (myLight.Color.r()),
119                          Standard_Real (myLight.Color.g()),
120                          Standard_Real (myLight.Color.b()),
121                          Quantity_TOC_RGB);
122 }
123
124 // =======================================================================
125 // function : Type
126 // purpose  :
127 // =======================================================================
128 V3d_TypeOfLight V3d_Light::Type() const
129 {
130   return (V3d_TypeOfLight)myLight.Type;
131 }
132
133 // =======================================================================
134 // function : SetIntensity
135 // purpose  :
136 // =======================================================================
137 void V3d_Light::SetIntensity (const Standard_Real theValue)
138 {
139   Standard_ASSERT_RAISE (theValue > 0.,
140                          "V3d_Light::SetIntensity, "
141                          "Negative value for intensity");
142
143   myLight.Intensity = static_cast<Standard_ShortReal> (theValue);
144 }
145
146 // =======================================================================
147 // function : Intensity
148 // purpose  :
149 // =======================================================================
150 Standard_Real V3d_Light::Intensity() const
151 {
152   return myLight.Intensity;
153 }
154
155 // =======================================================================
156 // function : Smoothness
157 // purpose  :
158 // =======================================================================
159 Standard_Real V3d_Light::Smoothness() const
160 {
161   return myLight.Smoothness;
162 }
163
164 // =======================================================================
165 // function : Headlight
166 // purpose  :
167 // =======================================================================
168 Standard_Boolean V3d_Light::Headlight() const
169 {
170   return myLight.IsHeadlight;
171 }
172
173 // =======================================================================
174 // function : SetHeadlight
175 // purpose  :
176 // =======================================================================
177 void V3d_Light::SetHeadlight (const Standard_Boolean theValue)
178 {
179   myLight.IsHeadlight = theValue;
180 }
181
182 // =======================================================================
183 // function : SymetricPointOnSphere
184 // purpose  :
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 ) {
187
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;
192
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);
198   DeltaX = X0 - XP;
199   DeltaY = Y0 - YP;
200   DeltaZ = Z0 - ZP;
201
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.
208
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 
212     - Rayon*Rayon ;
213   Delta = B*B - 4.*A*C;
214   if ( Delta >= 0 ) {
215     Lambda = (-B + Sqrt(Delta))/(2.*A);
216     if ( Lambda >= -0.0001 && Lambda <= 0.0001 ) 
217       Lambda = (-B - Sqrt(Delta))/(2.*A);
218     X = XP + Lambda*VX;
219     Y = YP + Lambda*VY;
220     Z = ZP + Lambda*VZ;
221   }
222   else {
223     X = XP; Y = YP; Z = ZP;
224   }
225 }
226
227 // =======================================================================
228 // function : IsDisplayed
229 // purpose  :
230 // =======================================================================
231 Standard_Boolean V3d_Light::IsDisplayed() const
232 {
233   if (myGraphicStructure.IsNull())
234   {
235     return Standard_False;
236   }
237
238   return myGraphicStructure->IsDisplayed();
239 }