31 #include <visp3/core/vpConfig.h>
33 #if defined(VISP_HAVE_PANDA3D)
35 #include <visp3/ar/vpPanda3DGeometryRenderer.h>
36 #include "windowFramework.h"
37 #include "graphicsOutput.h"
38 #include "graphicsEngine.h"
39 #include "graphicsBuffer.h"
43 const std::string SHADER_VERT_NORMAL_AND_DEPTH_CAMERA =
45 "in vec3 p3d_Normal;\n"
46 "in vec4 p3d_Vertex;\n"
47 "uniform mat3 p3d_NormalMatrix;\n"
48 "uniform mat4 p3d_ModelViewMatrix;\n"
49 "uniform mat4 p3d_ModelViewProjectionMatrix;\n"
51 "out float distToCamera;\n"
54 " gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;\n"
55 " oNormal = p3d_NormalMatrix * normalize(p3d_Normal);\n"
56 " vec4 cs_position = p3d_ModelViewMatrix * p3d_Vertex;\n"
57 " distToCamera = -cs_position.z;\n"
60 const std::string SHADER_VERT_NORMAL_AND_DEPTH_OBJECT =
62 "in vec3 p3d_Normal;\n"
63 "in vec4 p3d_Vertex;\n"
64 "uniform mat4 p3d_ModelViewMatrix;\n"
65 "uniform mat4 p3d_ModelViewProjectionMatrix;\n"
67 "out float distToCamera;\n"
70 " gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;\n"
71 " oNormal = vec3(p3d_Normal.x, -p3d_Normal.z, p3d_Normal.y);\n"
72 " vec4 cs_position = p3d_ModelViewMatrix * p3d_Vertex;\n"
73 " distToCamera = -cs_position.z;\n"
76 const std::string SHADER_FRAG_NORMAL_AND_DEPTH =
79 "in float distToCamera;\n"
80 "out vec4 p3d_FragData;\n"
83 " p3d_FragData.bgra = vec4(normalize(oNormal), distToCamera);\n"
89 case vpPanda3DGeometryRenderer::vpRenderType::OBJECT_NORMALS:
90 return "normals-world";
91 case vpPanda3DGeometryRenderer::vpRenderType::CAMERA_NORMALS:
92 return "normals-camera";
105 shader = Shader::make(Shader::ShaderLanguage::SL_GLSL,
106 SHADER_VERT_NORMAL_AND_DEPTH_OBJECT,
107 SHADER_FRAG_NORMAL_AND_DEPTH);
110 shader = Shader::make(Shader::ShaderLanguage::SL_GLSL,
111 SHADER_VERT_NORMAL_AND_DEPTH_CAMERA,
112 SHADER_FRAG_NORMAL_AND_DEPTH);
122 FrameBufferProperties fbp;
123 fbp.set_rgb_color(
true);
124 fbp.set_float_depth(
false);
125 fbp.set_float_color(
true);
126 fbp.set_depth_bits(16);
127 fbp.set_rgba_bits(32, 32, 32, 32);
129 WindowProperties win_prop;
132 int flags = GraphicsPipe::BF_refuse_window | GraphicsPipe::BF_resizeable;
133 GraphicsOutput *windowOutput =
m_window->get_graphics_output();
134 GraphicsEngine *engine = windowOutput->get_engine();
135 GraphicsPipe *pipe = windowOutput->get_pipe();
138 m_normalDepthBuffer = engine->make_output(pipe, renderTypeToName(m_renderType) + std::to_string(
id),
m_renderOrder, fbp, win_prop, flags,
139 windowOutput->get_gsg(), windowOutput);
140 m_normalDepthTexture =
new Texture(
"geometry texture " + std::to_string(
id));
142 if (m_normalDepthBuffer ==
nullptr) {
148 m_buffers.push_back(m_normalDepthBuffer);
149 m_normalDepthBuffer->set_inverted(windowOutput->get_gsg()->get_copy_texture_inverted());
150 fbp.setup_color_texture(m_normalDepthTexture);
151 m_normalDepthTexture->set_format(Texture::F_rgba32);
152 m_normalDepthBuffer->add_render_texture(m_normalDepthTexture, GraphicsOutput::RenderTextureMode::RTM_copy_texture, GraphicsOutput::RenderTexturePlane::RTP_color);
153 m_normalDepthBuffer->set_clear_color(LColor(0.f));
154 m_normalDepthBuffer->set_clear_color_active(
true);
156 DisplayRegion *region = m_normalDepthBuffer->make_display_region();
157 if (region ==
nullptr) {
161 region->set_clear_color(LColor(0.f));
166 normals.
resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
167 depth.
resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
168 if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
172 int rowIncrement = normals.
getWidth() * 4;
173 float *data = (
float *)(&(m_normalDepthTexture->get_ram_image().front()));
174 data = data + rowIncrement * (normals.
getHeight() - 1);
175 rowIncrement = -rowIncrement;
177 for (
unsigned int i = 0; i < normals.
getHeight(); ++i) {
178 vpRGBf *normalRow = normals[i];
179 float *depthRow = depth[i];
180 for (
unsigned int j = 0; j < normals.
getWidth(); ++j) {
181 normalRow[j].
R = (data[j * 4]);
182 normalRow[j].
G = (data[j * 4 + 1]);
183 normalRow[j].
B = (data[j * 4 + 2]);
184 depthRow[j] = (data[j * 4 + 3]);
186 data += rowIncrement;
197 const unsigned top =
static_cast<unsigned int>(std::max(0.0, bb.
getTop()));
198 const unsigned left =
static_cast<unsigned int>(std::max(0.0, bb.
getLeft()));
199 const unsigned numComponents = m_normalDepthTexture->get_num_components();
201 const float *data = (
float *)(&(m_normalDepthTexture->get_ram_image().front()));
204 if (numComponents != 4) {
207 if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
213 const float *
const rowData = data - i * rowIncrement;
214 vpRGBf *normalRow = normals[top + i];
215 float *depthRow = depth[top + i];
216 #if defined(VISP_HAVE_OPENMP)
217 #pragma omp parallel for
219 for (
int j = 0; j < image_width; ++j) {
220 int left_j = left + j;
222 normalRow[left_j].
R = (rowData[j_4]);
223 normalRow[left_j].
G = (rowData[j_4 + 1]);
224 normalRow[left_j].
B = (rowData[j_4 + 2]);
225 depthRow[left_j] = (rowData[j_4 + 3]);
232 normals.
resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
233 if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
237 int rowIncrement = normals.
getWidth() * 4;
238 float *data = (
float *)(&(m_normalDepthTexture->get_ram_image().front()));
239 data = data + rowIncrement * (normals.
getHeight() - 1);
240 rowIncrement = -rowIncrement;
241 for (
unsigned int i = 0; i < normals.
getHeight(); ++i) {
242 vpRGBf *normalRow = normals[i];
243 for (
unsigned int j = 0; j < normals.
getWidth(); ++j) {
244 normalRow[j].
R = (data[j * 4]);
245 normalRow[j].
G = (data[j * 4 + 1]);
246 normalRow[j].
B = (data[j * 4 + 2]);
248 data += rowIncrement;
254 depth.
resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
256 if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
260 int rowIncrement = depth.
getWidth() * 4;
261 float *data = (
float *)(&(m_normalDepthTexture->get_ram_image().front()));
262 data = data + rowIncrement * (depth.
getHeight() - 1);
263 rowIncrement = -rowIncrement;
265 for (
unsigned int i = 0; i < depth.
getHeight(); ++i) {
266 float *depthRow = depth[i];
267 for (
unsigned int j = 0; j < depth.
getWidth(); ++j) {
268 depthRow[j] = (data[j * 4 + 3]);
270 data += rowIncrement;
276 #elif !defined(VISP_BUILD_SHARED_LIBS)
278 void dummy_vpPanda3DGeometryRenderer() { };
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
@ dimensionError
Bad dimension.
unsigned int getWidth() const
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
unsigned int getHeight() const
Base class for a panda3D renderer. This class handles basic functionalities, such as loading object,...
NodePath m_renderRoot
Rendering parameters.
std::string m_name
Inverse of VISP_T_PANDA.
std::vector< PointerTo< GraphicsOutput > > m_buffers
NodePath of the camera.
PointerTo< WindowFramework > m_window
Rendering priority for this renderer and its buffers. A lower value will be rendered first....
int m_renderOrder
name of the renderer
vpPanda3DRenderParameters m_renderParameters
Pointer to owning window, which can create buffers etc. It is not necessarily visible.
void getRender(vpImage< vpRGBf > &colorData, vpImage< float > &depth) const
Get render results into ViSP readable structures.
void setupScene() VP_OVERRIDE
Initialize the scene for this specific renderer.
void setupRenderTarget() VP_OVERRIDE
Initialize buffers and other objects that are required to save the render.
vpPanda3DGeometryRenderer(vpRenderType renderType)
@ CAMERA_NORMALS
Surface normals in the object frame.
unsigned int getImageWidth() const
unsigned int getImageHeight() const
Defines a rectangle in the plane.