Visual Servoing Platform  version 3.6.1 under development (2025-01-24)
vpPanda3DRendererSet.cpp
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 #include <visp3/ar/vpPanda3DRendererSet.h>
32 
33 #if defined(VISP_HAVE_PANDA3D)
34 
35 #include "load_prc_file.h"
36 
37 BEGIN_VISP_NAMESPACE
39 {
40  load_prc_file_data("", "textures-power-2 none");
41  load_prc_file_data("", "gl-version 3 2");
42  load_prc_file_data("", "no-singular-invert");
43 }
44 
46 {
47  m_renderParameters = renderParameters;
48  load_prc_file_data("", "textures-power-2 none");
49  load_prc_file_data("", "gl-version 3 2");
50  load_prc_file_data("", "no-singular-invert");
51 }
52 
54 {
55 
56  if (!frameworkIsOpen) {
57  frameworkIsOpen = true;
58  framework.open_framework();
59  }
60  m_isWindowOwner = true;
61 
62  WindowProperties winProps;
63  winProps.set_size(LVecBase2i(m_renderParameters.getImageWidth(), m_renderParameters.getImageHeight()));
64  int flags = GraphicsPipe::BF_refuse_window;
65  m_window = framework.open_window(winProps, flags);
66  if (m_window == nullptr) {
67  winProps.set_minimized(true);
68  m_window = framework.open_window(winProps, 0);
69  }
70  if (m_window == nullptr) {
71  throw vpException(vpException::fatalError, "Could not open Panda3D window (hidden or visible)");
72  }
73 
74  m_window->set_background_type(WindowFramework::BackgroundType::BT_black);
75  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
76  renderer->initFromParent(*this);
77  }
78 }
79 
80 void vpPanda3DRendererSet::initFromParent(PointerTo<WindowFramework> window)
81 {
83  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
84  renderer->initFromParent(m_window);
85  }
86 }
87 
89 {
91  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
92  renderer->initFromParent(*this);
93  }
94 }
95 
97 {
98  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
99  if (renderer->isRendering3DScene()) {
100  renderer->setCameraPose(wTc);
101  }
102  }
103 }
104 
106 {
107  if (m_subRenderers.size() == 0) {
108  throw vpException(vpException::fatalError, "cannot get the pose of an object if no subrenderer is present");
109  }
110  if (!m_subRenderers[0]->isRendering3DScene()) {
111  throw vpException(vpException::fatalError, "This renderer set only contains a non-3D renderer.");
112  }
113  return m_subRenderers[0]->getCameraPose();
114 }
115 
116 void vpPanda3DRendererSet::setNodePose(const std::string &name, const vpHomogeneousMatrix &wTo)
117 {
118  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
119  if (renderer->isRendering3DScene()) {
120  renderer->setNodePose(name, wTo);
121  }
122  }
123 }
124 
126 {
127  throw vpException(vpException::badValue, "NodePath setNodePose is not supported in renderer set, prefer the string version");
128 }
129 
131 {
132  if (m_subRenderers.size() == 0) {
133  throw vpException(vpException::fatalError, "cannot get the pose of an object if no subrenderer is present");
134  }
135  if (!m_subRenderers[0]->isRendering3DScene()) {
136  throw vpException(vpException::fatalError, "This renderer set only contains a non-3D renderer.");
137  }
138  return m_subRenderers[0]->getNodePose(name);
139 }
140 
142 {
143  throw vpException(vpException::badValue, "NodePath getNodePose is not supported in renderer set, prefer the string version");
144 }
145 
146 void vpPanda3DRendererSet::addNodeToScene(const NodePath &object)
147 {
148  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
149  if (renderer->isRendering3DScene()) {
150  renderer->addNodeToScene(object);
151  }
152  }
153 }
154 
156 {
157  m_renderParameters = params;
158  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
159  renderer->setRenderParameters(m_renderParameters);
160  }
161 }
162 
164 {
165  for (std::shared_ptr<vpPanda3DBaseRenderer> &renderer: m_subRenderers) {
166  vpPanda3DLightable *lightable = dynamic_cast<vpPanda3DLightable *>(renderer.get());
167  if (lightable != nullptr) {
168  lightable->addLight(light);
169  }
170  }
171 }
172 
173 void vpPanda3DRendererSet::addSubRenderer(std::shared_ptr<vpPanda3DBaseRenderer> renderer)
174 {
175  for (std::shared_ptr<vpPanda3DBaseRenderer> &otherRenderer: m_subRenderers) {
176  if (renderer->getName() == otherRenderer->getName()) {
177  throw vpException(vpException::badValue, "Cannot have two subrenderers with the same name");
178  }
179  }
180  std::vector<std::shared_ptr<vpPanda3DBaseRenderer>>::const_iterator it = m_subRenderers.begin();
181  while (it != m_subRenderers.end()) {
182  if ((*it)->getRenderOrder() > renderer->getRenderOrder()) {
183  break;
184  }
185  ++it;
186  }
187  m_subRenderers.insert(it, renderer);
188  renderer->setRenderParameters(m_renderParameters);
189  if (m_window != nullptr) {
190  renderer->initFromParent(m_window);
191  renderer->setCameraPose(getCameraPose());
192  }
193 
194 }
195 
197 {
198  for (std::shared_ptr<vpPanda3DBaseRenderer> &subRenderer: m_subRenderers) {
199  if (subRenderer.get() != &sourceBuffer) {
200  subRenderer->enableSharedDepthBuffer(sourceBuffer);
201  }
202  }
203 }
204 
205 END_VISP_NAMESPACE
206 
207 #elif !defined(VISP_BUILD_SHARED_LIBS)
208 // Work around to avoid warning: libvisp_ar.a(vpPanda3DRendererSet.cpp.o) has no symbols
209 void dummy_vpPanda3DRendererSet() { };
210 
211 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:73
@ fatalError
Fatal error.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
Base class for a panda3D renderer. This class handles basic functionalities, such as loading object,...
static bool frameworkIsOpen
Panda Rendering framework.
virtual bool isRendering3DScene() const
Returns true if this renderer process 3D data and its scene root can be interacted with.
static PandaFramework framework
Inverse of VISP_T_PANDA.
bool m_isWindowOwner
Set of buffers that this renderer uses. This storage contains weak refs to those buffers and should n...
PointerTo< WindowFramework > m_window
Rendering priority for this renderer and its buffers. A lower value will be rendered first....
virtual void initFromParent(PointerTo< WindowFramework > window)
vpPanda3DRenderParameters m_renderParameters
Pointer to owning window, which can create buffers etc. It is not necessarily visible.
Base class for a Light that can be added to a Panda3D scene.
Interface for objects, scenes or other Panda3D related data that can be lit by a vpPanda3DLight.
virtual void addLight(const vpPanda3DLight &light)=0
Light this lightable object with a new light.
Rendering parameters for a panda3D simulation.
void initFromParent(PointerTo< WindowFramework > window) VP_OVERRIDE
void addNodeToScene(const NodePath &object) VP_OVERRIDE
void initFramework() VP_OVERRIDE
Initialize the framework and propagate the created panda3D framework to the subrenderers.
virtual void setRenderParameters(const vpPanda3DRenderParameters &params) VP_OVERRIDE
Set new rendering parameters. If the scene has already been initialized, the renderer camera is updat...
void addSubRenderer(std::shared_ptr< vpPanda3DBaseRenderer > renderer)
Add a new subrenderer: This subrenderer should have a unique name, not present in the set.
std::vector< std::shared_ptr< vpPanda3DBaseRenderer > > m_subRenderers
void enableSharedDepthBuffer(vpPanda3DBaseRenderer &sourceBuffer) VP_OVERRIDE
vpHomogeneousMatrix getCameraPose() VP_OVERRIDE
Retrieve the pose of the camera. As this renderer contains multiple other renderers.
vpHomogeneousMatrix getNodePose(const std::string &name) VP_OVERRIDE
Retrieve the pose of a scene node. The pose is in the world frame, using a ViSP convention.
void setNodePose(const std::string &name, const vpHomogeneousMatrix &wTo) VP_OVERRIDE
Set the pose of an object for all the subrenderers. The pose is specified using the ViSP convention T...
void addLight(const vpPanda3DLight &light) VP_OVERRIDE
Light this lightable object with a new light.
void setCameraPose(const vpHomogeneousMatrix &wTc) VP_OVERRIDE
Set the pose of the camera, using the ViSP convention. This change is propagated to all subrenderers.