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 /*!
34   \class SoLight SoLight.h Inventor/nodes/SoLight.h
35   \brief The SoLight class is the base class for light emitting nodes.
36 
37   \ingroup nodes
38 
39   This node type is abstract and does not in itself provide any light
40   sources to the scene, you need to use one of its subclasses.
41 
42   There are a few important things to know about light sources in
43   Coin. First of all, the more light sources you have in your scene,
44   the slower the rendering will be. The impact on rendering speed is
45   highly dependent on the graphics hardware and/or rendering subsystem
46   software implementation (i.e. how optimized an OpenGL you or your
47   users are running), but it could be severe.
48 
49   Another issue which is important to know is that OpenGL rendering
50   engines usually have a fixed maximum number of available light
51   sources which can be present in the state at the same time. If you
52   reach the maximum number, further light sources will simply be
53   ignored. The maximum number of light sources for OpenGL rendering
54   can be found by using:
55 
56   \code
57       #include <Inventor/elements/SoGLLightIdElement.h>
58 #include <Inventor/nodes/SoSubNodeP.h>
59       // ...[snip]...
60       int nrlights = SoGLLightIdElement::getMaxGLSources();
61   \endcode
62 
63   If you are clever with how you use light sources, you can get away
64   with using a lot more lights in a scene graph than the max available
65   from the rendering system. This is because light sources are stacked
66   on the traversal state, just like other appearance data. So if you
67   put light sources under SoSeparator nodes, they will be popped off
68   and "forgotten" for the remaining geometry of the scene graph after
69   the subgraph below an SoSeparator has been traversed.
70 */
71 
72 #include <Inventor/nodes/SoLight.h>
73 
74 #include <Inventor/actions/SoCallbackAction.h>
75 #include <Inventor/actions/SoGLRenderAction.h>
76 #include <Inventor/elements/SoGLLightIdElement.h>
77 #include <Inventor/elements/SoLightAttenuationElement.h>
78 #include <Inventor/elements/SoLightElement.h>
79 #include <Inventor/elements/SoModelMatrixElement.h>
80 #include <Inventor/elements/SoViewingMatrixElement.h>
81 
82 #include "nodes/SoSubNodeP.h"
83 
84 /*!
85   \var SoSFBool SoLight::on
86 
87   Whether light source should be on or off. The on-flag defaults to \c
88   TRUE.
89 */
90 /*!
91   \var SoSFFloat SoLight::intensity
92 
93   Intensity of light source. This decides how much the light source
94   should affect the colors etc of the scene geometry. Valid range is
95   0.0 (none) to 1.0 (maximum). Default value is 1.0.
96 */
97 /*!
98   \var SoSFColor SoLight::color
99 
100   Color of light source. Default is an all-white light source.
101 */
102 
103 
104 SO_NODE_ABSTRACT_SOURCE(SoLight);
105 
106 /*!
107   Constructor.
108 */
SoLight(void)109 SoLight::SoLight(void)
110 {
111   SO_NODE_INTERNAL_CONSTRUCTOR(SoLight);
112 
113   SO_NODE_ADD_FIELD(on, (TRUE));
114   SO_NODE_ADD_FIELD(intensity, (1.0f));
115   SO_NODE_ADD_FIELD(color, (SbColor(1.0f, 1.0f, 1.0f)));
116 }
117 
118 /*!
119   Destructor.
120 */
~SoLight()121 SoLight::~SoLight()
122 {
123 }
124 
125 // Doc from superclass.
126 void
initClass(void)127 SoLight::initClass(void)
128 {
129   SO_NODE_INTERNAL_INIT_ABSTRACT_CLASS(SoLight, SO_FROM_INVENTOR_1);
130 
131   SO_ENABLE(SoGLRenderAction, SoLightAttenuationElement);
132   SO_ENABLE(SoGLRenderAction, SoGLLightIdElement);
133   SO_ENABLE(SoGLRenderAction, SoLightElement);
134 
135   SO_ENABLE(SoCallbackAction, SoLightAttenuationElement);
136   SO_ENABLE(SoCallbackAction, SoLightElement);
137 }
138 
139 // Doc from superclass.
140 void
callback(SoCallbackAction * action)141 SoLight::callback(SoCallbackAction * action)
142 {
143   SoState * state = action->getState();
144   SoLightElement::add(state, this, (SoModelMatrixElement::get(state) *
145                                     SoViewingMatrixElement::get(state)));
146 }
147