0024310: TKOpenGl - GLSL compatibility issues
[occt.git] / src / V3d / V3d_Light.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 /***********************************************************************
19  
20      FONCTION :
21      ----------
22         Classe V3d_SpotLight :
23  
24      HISTORIQUE DES MODIFICATIONS   :
25      --------------------------------
26       00-09-92 : GG  ; Creation.
27       30-03-98 : ZOV ; PRO6774 (reconstruction of the class hierarchy and suppressing useless methods)
28       IMP230300: GG Add SetColor() and Color() methods
29       IMP231100: GG Add IsDisplayed() method
30
31 ************************************************************************/
32
33 /*----------------------------------------------------------------------*/
34 /*
35  * Includes
36  */
37
38 #include <V3d.hxx>
39 #include <V3d_Light.ixx>
40
41 //-Declarations
42
43
44 //-Aliases
45
46 //-Global data definitions
47
48 //-Local data definitions
49
50 //-Constructors
51
52 V3d_Light::V3d_Light(const Handle(V3d_Viewer)& VM) {
53
54         MyType = V3d_AMBIENT ;
55         VM->AddLight(this) ;
56 }
57
58 //-Methods, in order
59
60 Handle(Visual3d_Light) V3d_Light::Light() const {
61
62         return MyLight ;
63 }
64
65 void V3d_Light::SetColor(const Quantity_TypeOfColor Type, const Standard_Real v1, const Standard_Real v2, const Standard_Real v3) {
66   Standard_Real V1 = v1 ;
67   Standard_Real V2 = v2 ;
68   Standard_Real V3 = v3 ;
69
70   if( V1 < 0. ) V1 = 0. ; else if( V1 > 1. ) V1 = 1. ;
71   if( V2 < 0. ) V2 = 0. ; else if( V2 > 1. ) V2 = 1. ;
72   if( V3 < 0. ) V3 = 0. ; else if( V3 > 1. ) V3 = 1. ;
73
74
75   Quantity_Color C(V1,V2,V3,Type) ;
76   MyLight->SetColor(C) ;
77 }
78
79 void V3d_Light::SetColor(const Quantity_NameOfColor Name) {
80   Quantity_Color C(Name) ;
81   MyLight->SetColor(C) ;
82 }
83
84 void V3d_Light::SetColor(const Quantity_Color& aColor) {
85   MyLight->SetColor(aColor) ;
86 }
87
88 void V3d_Light::Color(const Quantity_TypeOfColor Type, Standard_Real& V1, Standard_Real& V2, Standard_Real& V3) const {
89
90   Quantity_Color C ;
91   C = MyLight->Color() ;
92   C.Values(V1,V2,V3,Type) ;
93 }
94
95 void V3d_Light::Color(Quantity_NameOfColor& Name) const{
96
97   Quantity_Color C ;
98   C = MyLight->Color() ;
99   Name = C.Name();
100 }
101
102 Quantity_Color V3d_Light::Color() const{
103
104   return MyLight->Color();
105 }
106
107 V3d_TypeOfLight V3d_Light::Type() const {
108
109   return MyType;
110 }
111
112
113 Standard_Boolean V3d_Light::Headlight() const {
114   return MyLight->Headlight();
115 }
116
117 void V3d_Light::SetHeadlight (const Standard_Boolean theValue)
118 {
119   MyLight->SetHeadlight (theValue);
120 }
121
122 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 ) {
123
124   Standard_Real X0,Y0,Z0,XP,YP,ZP;
125   Standard_Real PXP,PYP,DeltaX,DeltaY,DeltaZ;
126   Standard_Real A,B,C,Delta,Lambda;
127   Standard_Integer IPX,IPY;
128
129   Center.Coord(X0,Y0,Z0);
130   aPoint.Coord(XP,YP,ZP);
131   aView->Project(XP,YP,ZP,PXP,PYP);
132   aView->Convert(PXP,PYP,IPX,IPY);
133   aView->ProjReferenceAxe(IPX,IPY,X,Y,Z,VX,VY,VZ);
134   DeltaX = X0 - XP;
135   DeltaY = Y0 - YP;
136   DeltaZ = Z0 - ZP;
137
138 //      The point of intersection of straight lines defined by :
139 //      - Straight line passing by the point of projection and the eye
140 //        if this is a perspective, parralel to the normal of the view 
141 //        if this is an axonometric view.
142 //        position in the view is parallel to the normal of the view
143 //      - The distance position of the target camera is equal to the radius.
144
145   A = VX*VX + VY*VY + VZ*VZ ;
146   B = -2. * (VX*DeltaX + VY*DeltaY + VZ*DeltaZ);
147   C = DeltaX*DeltaX + DeltaY*DeltaY + DeltaZ*DeltaZ 
148     - Rayon*Rayon ;
149   Delta = B*B - 4.*A*C;
150   if ( Delta >= 0 ) {
151     Lambda = (-B + Sqrt(Delta))/(2.*A);
152     if ( Lambda >= -0.0001 && Lambda <= 0.0001 ) 
153       Lambda = (-B - Sqrt(Delta))/(2.*A);
154     X = XP + Lambda*VX;
155     Y = YP + Lambda*VY;
156     Z = ZP + Lambda*VZ;
157   }
158   else {
159     X = XP; Y = YP; Z = ZP;
160   }
161 }
162
163 Standard_Boolean V3d_Light::IsDisplayed() const {
164   if( MyGraphicStructure.IsNull() ) return Standard_False;
165   return MyGraphicStructure->IsDisplayed();
166 }