1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif // HAVE_CONFIG_H
36 
37 #ifdef HAVE_VRML97
38 
39 /*!
40   \class SoVRMLDirectionalLight SoVRMLDirectionalLight.h Inventor/VRMLnodes/SoVRMLDirectionalLight.h
41   \brief The SoVRMLDirectionalLight class is a node type for specifying directional light sources.
42 
43   \ingroup VRMLnodes
44 
45   \WEB3DCOPYRIGHT
46 
47   \verbatim
48   DirectionalLight {
49     exposedField SFFloat ambientIntensity  0        # [0,1]
50     exposedField SFColor color             1 1 1    # [0,1]
51     exposedField SFVec3f direction         0 0 -1   # (-inf,inf)
52     exposedField SFFloat intensity         1        # [0,1]
53     exposedField SFBool  on                TRUE
54   }
55   \endverbatim
56 
57   The DirectionalLight node defines a directional light source that
58   illuminates along rays parallel to a given 3-dimensional vector. A
59   description of the ambientIntensity, color, intensity, and on fields
60   is in 4.6.6, Light sources
61   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.6>).
62 
63   The direction field specifies the direction vector of the
64   illumination emanating from the light source in the local coordinate
65   system. Light is emitted along parallel rays from an infinite
66   distance away. A directional light source illuminates only the
67   objects in its enclosing parent group.  The light may illuminate
68   everything within this coordinate system, including all children and
69   descendants of its parent group. The accumulated transformations of
70   the parent nodes affect the light.  DirectionalLight nodes do not
71   attenuate with distance. A precise description of VRML's lighting
72   equations is contained in 4.14, Lighting model
73   (<http://www.web3d.org/documents/specifications/14772/V2.0/part1/concepts.html#4.6.14>).
74 
75 */
76 
77 /*!
78   \var SoSFVec3f SoVRMLDirectionalLight::direction
79   The light direction.
80 */
81 
82 #include <Inventor/VRMLnodes/SoVRMLDirectionalLight.h>
83 
84 #include <Inventor/VRMLnodes/SoVRMLMacros.h>
85 #include <Inventor/SbColor4f.h>
86 #include <Inventor/SbVec4f.h>
87 #include <Inventor/actions/SoGLRenderAction.h>
88 #include <Inventor/elements/SoEnvironmentElement.h>
89 #include <Inventor/elements/SoGLLightIdElement.h>
90 #include <Inventor/system/gl.h>
91 #if COIN_DEBUG
92 #include <Inventor/errors/SoDebugError.h>
93 #endif // COIN_DEBUG
94 
95 #include "nodes/SoSubNodeP.h"
96 
97 SO_NODE_SOURCE(SoVRMLDirectionalLight);
98 
99 // Doc in parent
100 void
initClass(void)101 SoVRMLDirectionalLight::initClass(void) // static
102 {
103   SO_NODE_INTERNAL_INIT_CLASS(SoVRMLDirectionalLight, SO_VRML97_NODE_TYPE);
104 }
105 
106 /*!
107   Constructor.
108 */
SoVRMLDirectionalLight(void)109 SoVRMLDirectionalLight::SoVRMLDirectionalLight(void)
110 {
111   SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLDirectionalLight);
112 
113   SO_VRMLNODE_ADD_EXPOSED_FIELD(direction, (0.0f, 0.0f, -1.0f));
114 }
115 
116 /*!
117   Destructor.
118 */
~SoVRMLDirectionalLight()119 SoVRMLDirectionalLight::~SoVRMLDirectionalLight()
120 {
121 }
122 
123 // Doc in parent
124 void
GLRender(SoGLRenderAction * action)125 SoVRMLDirectionalLight::GLRender(SoGLRenderAction * action)
126 {
127   if (!this->on.getValue()) return;
128 
129   SoState * state = action->getState();
130   int idx = SoGLLightIdElement::increment(state);
131 
132   if (idx < 0) {
133 #if COIN_DEBUG
134     SoDebugError::postWarning("SoDirectionalLight::GLRender",
135                               "Max # of OpenGL lights exceeded :(");
136 #endif // COIN_DEBUG
137     return;
138   }
139 
140   GLenum light = (GLenum) (idx + GL_LIGHT0);
141 
142   SbColor4f lightcolor(0.0f, 0.0f, 0.0f, 1.0f);
143   lightcolor.setRGB(this->color.getValue());
144   lightcolor *= this->ambientIntensity.getValue();
145   glLightfv(light, GL_AMBIENT, lightcolor.getValue());
146 
147   lightcolor.setRGB(this->color.getValue());
148   lightcolor *= this->intensity.getValue();
149 
150   glLightfv(light, GL_DIFFUSE, lightcolor.getValue());
151   glLightfv(light, GL_SPECULAR, lightcolor.getValue());
152 
153   // GL directional light is specified towards light source
154   SbVec3f dir = - this->direction.getValue();
155   dir.normalize();
156 
157   // directional when w = 0.0
158   SbVec4f dirvec(dir[0], dir[1], dir[2], 0.0f);
159   glLightfv(light, GL_POSITION, dirvec.getValue());
160 
161   glLightf(light, GL_SPOT_EXPONENT, 0.0);
162   glLightf(light, GL_SPOT_CUTOFF, 180.0);
163   glLightf(light, GL_CONSTANT_ATTENUATION, 1);
164   glLightf(light, GL_LINEAR_ATTENUATION, 0);
165   glLightf(light, GL_QUADRATIC_ATTENUATION, 0);
166 
167 }
168 
169 #endif // HAVE_VRML97
170