Visual Servoing Platform  version 3.6.1 under development (2024-10-18)
vpPanda3DLight.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30 
31 #ifndef VP_PANDA3D_LIGHT_H
32 #define VP_PANDA3D_LIGHT_H
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #if defined(VISP_HAVE_PANDA3D)
37 
38 #include <string>
39 #include <visp3/core/vpPoint.h>
40 #include <visp3/core/vpRGBf.h>
41 #include <visp3/ar/vpPanda3DBaseRenderer.h>
42 
43 #include "nodePath.h"
44 #include "ambientLight.h"
45 #include "directionalLight.h"
46 #include "pointLight.h"
47 #include "directionalLight.h"
48 
49 BEGIN_VISP_NAMESPACE
61 class VISP_EXPORT vpPanda3DLight
62 {
63 public:
71  vpPanda3DLight(const std::string &name, const vpRGBf &color) : m_name(name), m_color(color) { }
72 
78  const std::string &getName() const { return m_name; }
84  const vpRGBf &getColor() const { return m_color; }
85 
91  virtual void addToScene(NodePath &scene) const = 0;
92 
93 protected:
94  std::string m_name;
96 };
97 
109 class VISP_EXPORT vpPanda3DAmbientLight : public vpPanda3DLight
110 {
111 public:
112  vpPanda3DAmbientLight(const std::string &name, const vpRGBf &color) : vpPanda3DLight(name, color) { }
113 
114  void addToScene(NodePath &scene) const VP_OVERRIDE
115  {
116  PT(AmbientLight) light = new AmbientLight(m_name);
117  light->set_color(LColor(m_color.R, m_color.G, m_color.B, 1));
118  NodePath alnp = scene.attach_new_node(light);
119  scene.set_light(alnp);
120  }
121 };
122 
131 class VISP_EXPORT vpPanda3DPointLight : public vpPanda3DLight
132 {
133 public:
148  vpPanda3DPointLight(const std::string &name, const vpRGBf &color, const vpColVector &position, const vpColVector &attenuation)
149  : vpPanda3DLight(name, color), m_attenuation(attenuation)
150  {
151  if (position.size() != 3) {
152  throw vpException(vpException::dimensionError, "Point light position must be a 3 dimensional vector");
153  }
154  m_position.resize(4, false);
155  m_position.insert(0, position);
156  m_position[3] = 1.0;
157  if (attenuation.size() != 3) {
158  throw vpException(vpException::dimensionError, "Point light attenuation components must be a 3 dimensional vector");
159  }
160  }
161 
162  void addToScene(NodePath &scene) const VP_OVERRIDE
163  {
164  PT(PointLight) light = new PointLight(m_name);
165  light->set_color(LColor(m_color.R, m_color.G, m_color.B, 1));
166  light->set_attenuation(LVecBase3(m_attenuation[0], m_attenuation[1], m_attenuation[2]));
167  NodePath np = scene.attach_new_node(light);
168  //vpColVector posPanda = vpPanda3DBaseRenderer::vispPointToPanda(m_position);
169  np.set_pos(m_position[0], m_position[1], m_position[2]);
170  scene.set_light(np);
171  }
172 
173 private:
174  vpColVector m_position;
175  vpColVector m_attenuation;
176 };
186 class VISP_EXPORT vpPanda3DDirectionalLight : public vpPanda3DLight
187 {
188 public:
198  vpPanda3DDirectionalLight(const std::string &name, const vpRGBf &color, const vpColVector &direction)
199  : vpPanda3DLight(name, color), m_direction(direction)
200  {
201  if (m_direction.size() != 3) {
202  throw vpException(vpException::dimensionError, "Direction light direction must be a 3 dimensional vector");
203  }
204  m_direction.normalize();
205  }
206 
207  void addToScene(NodePath &scene) const VP_OVERRIDE
208  {
209  PT(DirectionalLight) light = new DirectionalLight(m_name);
210  light->set_color(LColor(m_color.R, m_color.G, m_color.B, 1));
212  light->set_direction(LVector3f(m_direction[0], m_direction[1], m_direction[2]));
213  NodePath np = scene.attach_new_node(light);
214  scene.set_light(np);
215  }
216 
217 private:
218  vpColVector m_direction;
219 };
220 
226 class VISP_EXPORT vpPanda3DLightable
227 {
228 public:
229  virtual ~vpPanda3DLightable() = default;
235  virtual void addLight(const vpPanda3DLight &light) = 0;
236 };
243 class VISP_EXPORT vpPanda3DLightableScene : public vpPanda3DLightable
244 {
245 public:
247  { }
248 
249  vpPanda3DLightableScene(NodePath &scene) : vpPanda3DLightable(), m_lightableScene(scene)
250  { }
251 
258  void addLight(const vpPanda3DLight &light) VP_OVERRIDE
259  {
260  if (m_lightableScene.is_empty()) {
261  throw vpException(vpException::notInitialized, "Tried to add a light to a scene that is not initialized.");
262  }
263  light.addToScene(m_lightableScene);
264  }
265 protected:
266  void setLightableScene(NodePath &scene) { m_lightableScene = scene; }
267 private:
268  NodePath m_lightableScene;
269 };
270 
271 END_VISP_NAMESPACE
272 
273 #endif
274 #endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition: vpException.h:74
@ dimensionError
Bad dimension.
Definition: vpException.h:71
Class representing an ambient light.
void addToScene(NodePath &scene) const VP_OVERRIDE
Add the light to the scene.
vpPanda3DAmbientLight(const std::string &name, const vpRGBf &color)
static vpColVector vispVectorToPanda(const vpColVector &vec)
Class representing a directional light.
void addToScene(NodePath &scene) const VP_OVERRIDE
Add the light to the scene.
vpPanda3DDirectionalLight(const std::string &name, const vpRGBf &color, const vpColVector &direction)
Build a new directional light.
Base class for a Light that can be added to a Panda3D scene.
std::string m_name
const vpRGBf & getColor() const
Get the light's color.
virtual void addToScene(NodePath &scene) const =0
Add the light to the scene.
vpRGBf m_color
Name of the light. Should be unique in the scene.
const std::string & getName() const
Get the name of the light.
vpPanda3DLight(const std::string &name, const vpRGBf &color)
Build a new Panda3D light, given a unique name and an RGB color.
Implementation of vpPanda3DLightable for a panda scene with a root node.
void addLight(const vpPanda3DLight &light) VP_OVERRIDE
Add a light to the scene. All of the objects in the scene will be lit.
void setLightableScene(NodePath &scene)
vpPanda3DLightableScene(NodePath &scene)
Interface for objects, scenes or other Panda3D related data that can be lit by a vpPanda3DLight.
virtual ~vpPanda3DLightable()=default
virtual void addLight(const vpPanda3DLight &light)=0
Light this lightable object with a new light.
Class representing a Point Light.
void addToScene(NodePath &scene) const VP_OVERRIDE
Add the light to the scene.
vpPanda3DPointLight(const std::string &name, const vpRGBf &color, const vpColVector &position, const vpColVector &attenuation)
Build a new point light.
Definition: vpRGBf.h:60