-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for basic filters on output textures
- Loading branch information
Showing
12 changed files
with
331 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef vpPanda3DCommonFilters_h | ||
#define vpPanda3DCommonFilters_h | ||
|
||
#include <visp3/core/vpConfig.h> | ||
|
||
#if defined(VISP_HAVE_PANDA3D) | ||
|
||
#include <visp3/ar/vpPanda3DPostProcessFilter.h> | ||
|
||
class vpPanda3DRGBRenderer; | ||
|
||
class VISP_EXPORT vpPanda3DLuminanceFilter : public vpPanda3DPostProcessFilter | ||
{ | ||
public: | ||
vpPanda3DLuminanceFilter(const std::string &name, std::shared_ptr<vpPanda3DRGBRenderer> &inputRenderer, bool isOutput); | ||
FrameBufferProperties getBufferProperties() const vp_override; | ||
void getRender(vpImage<unsigned char> &I) const; | ||
|
||
private: | ||
static const char *FRAGMENT_SHADER; | ||
|
||
}; | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef vpPanda3DPostProcessFilter_h | ||
#define vpPanda3DPostProcessFilter_h | ||
|
||
#include <visp3/core/vpConfig.h> | ||
|
||
#if defined(VISP_HAVE_PANDA3D) | ||
#include <visp3/ar/vpPanda3DBaseRenderer.h> | ||
#include "cardMaker.h" | ||
#include "orthographicLens.h" | ||
|
||
|
||
class VISP_EXPORT vpPanda3DPostProcessFilter : public vpPanda3DBaseRenderer | ||
{ | ||
public: | ||
vpPanda3DPostProcessFilter(const std::string &name, const std::shared_ptr<vpPanda3DBaseRenderer> &inputRenderer, bool isOutput, std::string fragmentShader) | ||
: vpPanda3DBaseRenderer(name), m_inputRenderer(inputRenderer), m_isOutput(isOutput), m_fragmentShader(fragmentShader) | ||
{ | ||
m_renderOrder = m_inputRenderer->getRenderOrder() + 1; | ||
} | ||
bool isRendering3DScene() const vp_override | ||
{ | ||
return false; | ||
} | ||
|
||
protected: | ||
void setupScene() vp_override; | ||
|
||
void setupCamera() vp_override; | ||
|
||
void setupRenderTarget() vp_override; | ||
|
||
void setRenderParameters(const vpPanda3DRenderParameters ¶ms) vp_override; | ||
|
||
virtual FrameBufferProperties getBufferProperties() const = 0; | ||
|
||
std::shared_ptr<vpPanda3DBaseRenderer> m_inputRenderer; | ||
bool m_isOutput; //! Whether this filter is an output to be used and should be copied to ram | ||
std::string m_fragmentShader; | ||
PT(Shader) shader; | ||
Texture *m_texture; | ||
GraphicsOutput *m_buffer; | ||
|
||
static const char *FILTER_VERTEX_SHADER; | ||
}; | ||
|
||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <visp3/ar/vpPanda3DCommonFilters.h> | ||
#include <visp3/ar/vpPanda3DRGBRenderer.h> | ||
|
||
#if defined(VISP_HAVE_PANDA3D) | ||
|
||
const char *vpPanda3DLuminanceFilter::FRAGMENT_SHADER = R"shader( | ||
#version 330 | ||
in vec2 texcoords; | ||
out vec4 p3d_FragData; | ||
uniform sampler2D p3d_Texture0; | ||
void main() { | ||
vec4 v = texture(p3d_Texture0, texcoords); | ||
p3d_FragData.b = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b; | ||
} | ||
)shader"; | ||
|
||
|
||
vpPanda3DLuminanceFilter::vpPanda3DLuminanceFilter(const std::string &name, std::shared_ptr<vpPanda3DRGBRenderer> &inputRenderer, bool isOutput) | ||
: vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, std::string(vpPanda3DLuminanceFilter::FRAGMENT_SHADER)) | ||
{ | ||
|
||
} | ||
FrameBufferProperties vpPanda3DLuminanceFilter::getBufferProperties() const | ||
{ | ||
FrameBufferProperties fbp; | ||
fbp.set_depth_bits(0); | ||
fbp.set_rgba_bits(0, 0, 8, 0); | ||
fbp.set_float_color(false); | ||
return fbp; | ||
} | ||
void vpPanda3DLuminanceFilter::getRender(vpImage<unsigned char> &I) const | ||
{ | ||
if (!m_isOutput) { | ||
throw vpException(vpException::fatalError, "Tried to fetch output of a postprocessing filter that was configured as an intermediate output"); | ||
} | ||
unsigned indexMultiplier = m_texture->get_num_components(); // we ask for only 8 bits image, but we may get an rgb image | ||
I.resize(m_texture->get_y_size(), m_texture->get_x_size()); | ||
unsigned char *data = (unsigned char *)(&(m_texture->get_ram_image().front())); | ||
if (indexMultiplier != 1) { | ||
for (unsigned int i = 0; i < I.getSize(); ++i) { | ||
I.bitmap[i] = data[i * indexMultiplier]; | ||
} | ||
} | ||
else { | ||
memcpy(I.bitmap, &data[0], I.getSize() * sizeof(unsigned char)); | ||
} | ||
} | ||
|
||
|
||
|
||
#endif |
114 changes: 114 additions & 0 deletions
114
modules/ar/src/panda3d-simulator/vpPanda3DPostProcessFilter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <visp3/ar/vpPanda3DPostProcessFilter.h> | ||
|
||
|
||
#if defined(VISP_HAVE_PANDA3D) | ||
|
||
const char *vpPanda3DPostProcessFilter::FILTER_VERTEX_SHADER = R"shader( | ||
#version 330 | ||
in vec4 p3d_Vertex; | ||
uniform mat4 p3d_ModelViewProjectionMatrix; | ||
in vec2 p3d_MultiTexCoord0; | ||
out vec2 texcoords; | ||
void main() | ||
{ | ||
gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; | ||
texcoords = p3d_MultiTexCoord0; | ||
} | ||
)shader"; | ||
|
||
|
||
void vpPanda3DPostProcessFilter::setupScene() | ||
{ | ||
CardMaker cm("cm"); | ||
cm.set_frame_fullscreen_quad(); | ||
m_renderRoot = NodePath(cm.generate()); // Render root is a 2D rectangle | ||
m_renderRoot.set_depth_test(false); | ||
m_renderRoot.set_depth_write(false); | ||
GraphicsOutput *buffer = m_inputRenderer->getMainOutputBuffer(); | ||
if (buffer == nullptr) { | ||
throw vpException(vpException::fatalError, | ||
"Cannot add a postprocess filter to a renderer that does not define getMainOutputBuffer()"); | ||
} | ||
shader = Shader::make(Shader::ShaderLanguage::SL_GLSL, | ||
FILTER_VERTEX_SHADER, | ||
m_fragmentShader); | ||
m_renderRoot.set_shader(shader); | ||
std::cout << m_fragmentShader << std::endl; | ||
m_renderRoot.set_texture(buffer->get_texture()); | ||
m_renderRoot.set_attrib(LightRampAttrib::make_identity()); | ||
} | ||
|
||
void vpPanda3DPostProcessFilter::setupCamera() | ||
{ | ||
m_cameraPath = m_window->make_camera(); | ||
m_camera = (Camera *)m_cameraPath.node(); | ||
PT(OrthographicLens) lens = new OrthographicLens(); | ||
lens->set_film_size(2, 2); | ||
lens->set_film_offset(0, 0); | ||
lens->set_near_far(-1000, 1000); | ||
m_camera->set_lens(lens); | ||
m_cameraPath = m_renderRoot.attach_new_node(m_camera); | ||
m_camera->set_scene(m_renderRoot); | ||
} | ||
|
||
void vpPanda3DPostProcessFilter::setupRenderTarget() | ||
{ | ||
|
||
if (m_window == nullptr) { | ||
throw vpException(vpException::fatalError, "Cannot setup render target when window is null"); | ||
} | ||
FrameBufferProperties fbp = getBufferProperties(); | ||
WindowProperties win_prop; | ||
win_prop.set_size(m_renderParameters.getImageWidth(), m_renderParameters.getImageHeight()); | ||
|
||
// Don't open a window - force it to be an offscreen buffer. | ||
int flags = GraphicsPipe::BF_refuse_window | GraphicsPipe::BF_resizeable; | ||
GraphicsOutput *windowOutput = m_window->get_graphics_output(); | ||
GraphicsEngine *engine = windowOutput->get_engine(); | ||
GraphicsStateGuardian *gsg = windowOutput->get_gsg(); | ||
GraphicsPipe *pipe = windowOutput->get_pipe(); | ||
m_buffer = engine->make_output(pipe, m_name, m_renderOrder, | ||
fbp, win_prop, flags, | ||
gsg, windowOutput); | ||
if (m_buffer == nullptr) { | ||
throw vpException(vpException::fatalError, "Could not create buffer"); | ||
} | ||
m_buffers.push_back(m_buffer); | ||
m_buffer->set_inverted(gsg->get_copy_texture_inverted()); | ||
m_texture = new Texture(); | ||
//fbp.setup_color_texture(m_texture); | ||
m_buffer->add_render_texture(m_texture, m_isOutput ? GraphicsOutput::RenderTextureMode::RTM_copy_ram : GraphicsOutput::RenderTextureMode::RTM_copy_texture); | ||
m_buffer->set_clear_color(LColor(0.f)); | ||
m_buffer->set_clear_color_active(true); | ||
DisplayRegion *region = m_buffer->make_display_region(); | ||
if (region == nullptr) { | ||
throw vpException(vpException::fatalError, "Could not create display region"); | ||
} | ||
region->set_camera(m_cameraPath); | ||
region->set_clear_color(LColor(0.f)); | ||
} | ||
|
||
void vpPanda3DPostProcessFilter::setRenderParameters(const vpPanda3DRenderParameters ¶ms) | ||
{ | ||
m_renderParameters = params; | ||
unsigned int previousH = m_renderParameters.getImageHeight(), previousW = m_renderParameters.getImageWidth(); | ||
bool resize = previousH != params.getImageHeight() || previousW != params.getImageWidth(); | ||
|
||
m_renderParameters = params; | ||
|
||
if (resize) { | ||
for (GraphicsOutput *buffer: m_buffers) { | ||
//buffer->get_type().is_derived_from() | ||
GraphicsBuffer *buf = dynamic_cast<GraphicsBuffer *>(buffer); | ||
if (buf == nullptr) { | ||
throw vpException(vpException::fatalError, "Panda3D: could not cast to GraphicsBuffer when rendering."); | ||
} | ||
else { | ||
buf->set_size(m_renderParameters.getImageWidth(), m_renderParameters.getImageHeight()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.