Skip to content

Commit

Permalink
fix texture inversion
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed May 3, 2024
1 parent 4f84919 commit 2971eb2
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 117 deletions.
2 changes: 0 additions & 2 deletions modules/ar/include/visp3/ar/vpPanda3DBaseRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,11 @@ class VISP_EXPORT vpPanda3DBaseRenderer
* @param abort whether to abort (true) or display a message when an assertion fails.
*/
void setAbortOnPandaError(bool abort);
void setForcedInvertTextures(bool invert);
void enableDebugLog();

static vpColVector vispPointToPanda(const vpColVector &point);
static vpColVector vispVectorToPanda(const vpColVector &vec);


void printStructure();

virtual GraphicsOutput *getMainOutputBuffer() { return nullptr; }
Expand Down
2 changes: 1 addition & 1 deletion modules/ar/include/visp3/ar/vpPanda3DGeometryRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <visp3/ar/vpPanda3DBaseRenderer.h>

/**
* \ingroup group_ar_renderer_panda3d
* \ingroup group_ar_renderer_panda3d_3d
*
* @brief Renderer that outputs object geometric information.
*
Expand Down
4 changes: 4 additions & 0 deletions modules/ar/include/visp3/ar/vpPanda3DPostProcessFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class VISP_EXPORT vpPanda3DPostProcessFilter : public vpPanda3DBaseRenderer

void setRenderParameters(const vpPanda3DRenderParameters &params) vp_override;

void getRenderBasic(vpImage<unsigned char> &I) const;
void getRenderBasic(vpImage<vpRGBf> &I) const;


virtual FrameBufferProperties getBufferProperties() const = 0;

std::shared_ptr<vpPanda3DBaseRenderer> m_inputRenderer;
Expand Down
10 changes: 0 additions & 10 deletions modules/ar/src/panda3d-simulator/vpPanda3DBaseRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,6 @@ void vpPanda3DBaseRenderer::setAbortOnPandaError(bool abort)
}
}

void vpPanda3DBaseRenderer::setForcedInvertTextures(bool invert)
{
if (invert) {
load_prc_file_data("", "copy-texture-inverted 1");
}
else {
load_prc_file_data("", "copy-texture-inverted 0");
}
}

void vpPanda3DBaseRenderer::enableDebugLog()
{
load_prc_file_data("", "gl-debug 1");
Expand Down
42 changes: 3 additions & 39 deletions modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,7 @@ FrameBufferProperties vpPanda3DLuminanceFilter::getBufferProperties() const
}
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_renderParameters.getImageHeight(), m_renderParameters.getImageWidth());
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));
}
vpPanda3DPostProcessFilter::getRenderBasic(I);
}


Expand Down Expand Up @@ -101,20 +88,7 @@ FrameBufferProperties vpPanda3DGaussianBlur::getBufferProperties() const

void vpPanda3DGaussianBlur::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_renderParameters.getImageHeight(), m_renderParameters.getImageWidth());
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));
}
vpPanda3DPostProcessFilter::getRenderBasic(I);
}

const char *vpPanda3DCanny::FRAGMENT_SHADER = R"shader(
Expand Down Expand Up @@ -204,17 +178,7 @@ FrameBufferProperties vpPanda3DCanny::getBufferProperties() const

void vpPanda3DCanny::getRender(vpImage<vpRGBf> &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_renderParameters.getImageHeight(), m_renderParameters.getImageWidth());
float *data = (float *)(&(m_texture->get_ram_image().front()));
for (unsigned int i = 0; i < I.getSize(); ++i) {
I.bitmap[i].B = (data[i * 4]);
I.bitmap[i].G = (data[i * 4 + 1]);
I.bitmap[i].R = (data[i * 4 + 2]);
}
vpPanda3DPostProcessFilter::getRenderBasic(I);
}

#endif
96 changes: 47 additions & 49 deletions modules/ar/src/panda3d-simulator/vpPanda3DGeometryRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,73 +187,71 @@ void vpPanda3DGeometryRenderer::getRender(vpImage<vpRGBf> &normals, vpImage<floa
{
normals.resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
depth.resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
// unsigned expectedSize = m_normalDepthTexture->get_y_size() * m_normalDepthTexture->get_x_size() * sizeof(float) * 4;
// unsigned actualSize = m_normalDepthTexture->get_ram_image_size();
// if (expectedSize != actualSize) {
// throw vpException(vpException::fatalError, "Expected %d bytes, but got %d bytes", expectedSize, actualSize);
// }
// std::stringstream ss;
// ss << "Texture info:" << std::endl;
// ss << "Has ram image: " << m_normalDepthTexture->has_ram_image() << std::endl;
// ss << "Size (H x W): " << m_normalDepthTexture->get_y_size() << " x " << m_normalDepthTexture->get_x_size() << std::endl;
// ss << "Number of channels: " << m_normalDepthTexture->get_num_components() << std::endl;
// ss << "Bytes per channel: " << m_normalDepthTexture->get_component_width() << std::endl;
// ss << "Channel type: " << m_normalDepthTexture->get_component_type() << std::endl;
// std::cout << ss.str() << std::endl;

if (m_normalDepthTexture->get_component_type() == Texture::T_float) {
float *data = (float *)(&(m_normalDepthTexture->get_ram_image().front()));
for (unsigned int i = 0; i < normals.getSize(); ++i) {
normals.bitmap[i].B = (data[i * 4]);
normals.bitmap[i].G = (data[i * 4 + 1]);
normals.bitmap[i].R = (data[i * 4 + 2]);
depth.bitmap[i] = (data[i * 4 + 3]);
}

if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
throw vpException(vpException::badValue, "Unexpected data type in normals texture");
}
else if (m_normalDepthTexture->get_component_type() == Texture::T_unsigned_byte) {
unsigned char *data = (unsigned char *)(&(m_normalDepthTexture->get_ram_image().front()));
std::cout << "AAAAAAA" << std::endl;
for (unsigned int i = 0; i < normals.getSize(); ++i) {
normals.bitmap[i].B = (static_cast<float>(data[i * 4]) / 127.5f - 1.0);
normals.bitmap[i].G = (static_cast<float>(data[i * 4 + 1]) / 127.5f - 1.0);
normals.bitmap[i].R = (static_cast<float>(data[i * 4 + 2]) / 127.5f - 1.0);
depth.bitmap[i] = ((static_cast<float>(data[i * 4 + 3])));

int rowIncrement = normals.getWidth() * 4;
float *data = (float *)(&(m_normalDepthTexture->get_ram_image().front()));
data = data + rowIncrement * (normals.getHeight() - 1);
rowIncrement = -rowIncrement;

for (unsigned int i = 0; i < normals.getHeight(); ++i) {
data += rowIncrement;
vpRGBf *normalRow = normals[i];
float *depthRow = depth[i];
for (unsigned int j = 0; j < normals.getWidth(); ++j) {
normalRow[j].B = (data[j * 4]);
normalRow[j].G = (data[j * 4 + 1]);
normalRow[j].R = (data[j * 4 + 2]);
depthRow[j] = (data[j * 4 + 3]);
}
}

//#pragma omp parallel for simd
}

void vpPanda3DGeometryRenderer::getRender(vpImage<vpRGBf> &normals) const
{
normals.resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());

//#pragma omp parallel for simd
if (m_normalDepthTexture->get_component_type() == Texture::T_float) {
float *data = (float *)(&(m_normalDepthTexture->get_ram_image().front()));
for (unsigned int i = 0; i < normals.getSize(); ++i) {
normals.bitmap[i].B = (data[i * 4]);
normals.bitmap[i].G = (data[i * 4 + 1]);
normals.bitmap[i].R = (data[i * 4 + 2]);
}
if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
throw vpException(vpException::badValue, "Unexpected data type in normals texture");
}
else if (m_normalDepthTexture->get_component_type() == Texture::T_unsigned_byte) {
unsigned char *data = (unsigned char *)(&(m_normalDepthTexture->get_ram_image().front()));
for (unsigned int i = 0; i < normals.getSize(); ++i) {
normals.bitmap[i].B = (static_cast<float>(data[i * 4]) / 127.5f - 1.0);
normals.bitmap[i].G = (static_cast<float>(data[i * 4 + 1]) / 127.5f - 1.0);
normals.bitmap[i].R = (static_cast<float>(data[i * 4 + 2]) / 127.5f - 1.0);

int rowIncrement = normals.getWidth() * 4;
float *data = (float *)(&(m_normalDepthTexture->get_ram_image().front()));
data = data + rowIncrement * (normals.getHeight() - 1);
rowIncrement = -rowIncrement;

for (unsigned int i = 0; i < normals.getHeight(); ++i) {
data += rowIncrement;
vpRGBf *normalRow = normals[i];
for (unsigned int j = 0; j < normals.getWidth(); ++j) {
normalRow[j].B = (data[j * 4]);
normalRow[j].G = (data[j * 4 + 1]);
normalRow[j].R = (data[j * 4 + 2]);
}
}
}

void vpPanda3DGeometryRenderer::getRender(vpImage<float> &depth) const
{
depth.resize(m_normalDepthTexture->get_y_size(), m_normalDepthTexture->get_x_size());
if (m_normalDepthTexture->get_component_type() != Texture::T_float) {
throw vpException(vpException::badValue, "Unexpected data type in normals texture");
}

int rowIncrement = depth.getWidth() * 4;
float *data = (float *)(&(m_normalDepthTexture->get_ram_image().front()));
//#pragma omp parallel for simd
for (unsigned int i = 0; i < depth.getSize(); ++i) {
depth.bitmap[i] = (data[i * 4 + 3]);
data = data + rowIncrement * (depth.getHeight() - 1);
rowIncrement = -rowIncrement;

for (unsigned int i = 0; i < depth.getHeight(); ++i) {
data += rowIncrement;
float *depthRow = depth[i];
for (unsigned int j = 0; j < depth.getWidth(); ++j) {
depthRow[j] = (data[j * 4 + 3]);
}
}
}

Expand Down
52 changes: 51 additions & 1 deletion modules/ar/src/panda3d-simulator/vpPanda3DPostProcessFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void vpPanda3DPostProcessFilter::setupRenderTarget()
throw vpException(vpException::fatalError, "Could not create buffer");
}
m_buffers.push_back(m_buffer);
m_buffer->set_inverted(gsg->get_copy_texture_inverted());
//m_buffer->set_inverted(true);
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);
Expand Down Expand Up @@ -115,4 +115,54 @@ void vpPanda3DPostProcessFilter::setRenderParameters(const vpPanda3DRenderParame
}
}

void vpPanda3DPostProcessFilter::getRenderBasic(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");
}

I.resize(m_renderParameters.getImageHeight(), m_renderParameters.getImageWidth());
const unsigned numComponents = m_texture->get_num_components();
int rowIncrement = I.getWidth() * numComponents; // we ask for only 8 bits image, but we may get an rgb image
unsigned char *data = (unsigned char *)(&(m_texture->get_ram_image().front()));
// Panda3D stores data upside down
data += rowIncrement * (I.getHeight() - 1);
rowIncrement = -rowIncrement;

for (unsigned int i = 0; i < I.getHeight(); ++i) {
data += rowIncrement;
unsigned char *colorRow = I[i];
for (unsigned int j = 0; j < I.getWidth(); ++j) {
colorRow[j] = data[j * numComponents];
}
}
}

void vpPanda3DPostProcessFilter::getRenderBasic(vpImage<vpRGBf> &I) const
{
if (!m_isOutput) {
throw vpException(vpException::fatalError, "Tried to fetch output of a postprocessing filter that was configured as an intermediate output");
}

I.resize(m_renderParameters.getImageHeight(), m_renderParameters.getImageWidth());
const unsigned numComponents = m_texture->get_num_components();
int rowIncrement = I.getWidth() * numComponents; // we ask for only 8 bits image, but we may get an rgb image
float *data = (float *)(&(m_texture->get_ram_image().front()));
// Panda3D stores data upside down
data += rowIncrement * (I.getHeight() - 1);
rowIncrement = -rowIncrement;

for (unsigned int i = 0; i < I.getHeight(); ++i) {
data += rowIncrement;
vpRGBf *colorRow = I[i];
for (unsigned int j = 0; j < I.getWidth(); ++j) {
colorRow[j].B = data[j * numComponents];
colorRow[j].G = data[j * numComponents + 1];
colorRow[j].R = data[j * numComponents + 2];

}
}
}


#endif
27 changes: 19 additions & 8 deletions modules/ar/src/panda3d-simulator/vpPanda3DRGBRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,25 @@ void vpPanda3DRGBRenderer::getRender(vpImage<vpRGBa> &I) const
{
I.resize(m_colorTexture->get_y_size(), m_colorTexture->get_x_size());
unsigned char *data = (unsigned char *)(&(m_colorTexture->get_ram_image().front()));
// BGRA order in panda3d
for (unsigned int i = 0; i < I.getSize(); ++i) {
I.bitmap[i].B = data[i * 4];
I.bitmap[i].G = data[i * 4 + 1];
I.bitmap[i].R = data[i * 4 + 2];
I.bitmap[i].A = data[i * 4 + 3];
int rowIncrement = I.getWidth() * 4;
// Panda3D stores the image using the OpenGL convention (origin is bottom left),
// while we store data with origin as upper left. We copy with a flip
data = data + rowIncrement * (I.getHeight() - 1);
rowIncrement = -rowIncrement;

for (unsigned int i = 0; i < I.getHeight(); ++i) {
data += rowIncrement;
vpRGBa *colorRow = I[i];
for (unsigned int j = 0; j < I.getWidth(); ++j) {
// BGRA order in panda3d
colorRow[j].B = data[j * 4];
colorRow[j].G = data[j * 4 + 1];
colorRow[j].R = data[j * 4 + 2];
colorRow[j].A = data[j * 4 + 3];
}
}
// memcpy(I.bitmap, data, sizeof(unsigned char) * I.getSize() * 4);


}

void vpPanda3DRGBRenderer::setupScene()
Expand Down Expand Up @@ -288,7 +299,7 @@ void vpPanda3DRGBRenderer::setupRenderTarget()
throw vpException(vpException::fatalError, "Could not create color buffer");
}
m_buffers.push_back(m_colorBuffer);
m_colorBuffer->set_inverted(gsg->get_copy_texture_inverted());
//m_colorBuffer->set_inverted(gsg->get_copy_texture_inverted());
m_colorTexture = new Texture();
fbp.setup_color_texture(m_colorTexture);
//m_colorTexture->set_format(Texture::Format::F_srgb_alpha);
Expand Down
9 changes: 2 additions & 7 deletions tutorial/ar/tutorial-panda3d-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ void displayCanny(const vpImage<vpRGBf> &cannyRawData,

int main(int argc, const char **argv)
{
bool invertTexture = false;
bool stepByStep = false;
bool debug = false;
bool showLightContrib = false;
bool showCanny = false;
char *modelPathCstr = nullptr;
vpParseArgv::vpArgvInfo argTable[] =
{
{"-invert", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *)&invertTexture,
"Whether to force Texture inversion. Use this if the model is upside down."},
{"-model", vpParseArgv::ARGV_STRING, (char *) nullptr, (char *)&modelPathCstr,
"Path to the model to load."},
{"-step", vpParseArgv::ARGV_CONSTANT_BOOL, (char *) nullptr, (char *)&stepByStep,
Expand Down Expand Up @@ -167,7 +164,7 @@ int main(int argc, const char **argv)
std::shared_ptr<vpPanda3DRGBRenderer> rgbDiffuseRenderer = std::make_shared<vpPanda3DRGBRenderer>(false);
std::shared_ptr<vpPanda3DLuminanceFilter> grayscaleFilter = std::make_shared<vpPanda3DLuminanceFilter>("toGrayscale", rgbRenderer, false);
std::shared_ptr<vpPanda3DGaussianBlur> blurFilter = std::make_shared<vpPanda3DGaussianBlur>("blur", grayscaleFilter, false);
std::shared_ptr<vpPanda3DCanny> cannyFilter = std::make_shared<vpPanda3DCanny>("canny", blurFilter, true, 8.f);
std::shared_ptr<vpPanda3DCanny> cannyFilter = std::make_shared<vpPanda3DCanny>("canny", blurFilter, true, 10.f);


renderer.addSubRenderer(geometryRenderer);
Expand All @@ -188,9 +185,7 @@ int main(int argc, const char **argv)
if (debug) {
renderer.enableDebugLog();
}
if (invertTexture) {
renderer.setForcedInvertTextures(true);
}


std::cout << "Initializing Panda3D rendering framework" << std::endl;
renderer.initFramework();
Expand Down

0 comments on commit 2971eb2

Please sign in to comment.