Skip to content

Commit

Permalink
Refactor shaders
Browse files Browse the repository at this point in the history
Fix warning: missing terminating " character [enabled by default]
  • Loading branch information
fspindle committed Sep 27, 2024
1 parent 46cdf54 commit d11db8e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 112 deletions.
6 changes: 3 additions & 3 deletions modules/ar/include/visp3/ar/vpPanda3DCommonFilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class VISP_EXPORT vpPanda3DLuminanceFilter : public vpPanda3DPostProcessFilter
void getRender(vpImage<unsigned char> &I) const;

private:
static const char *FRAGMENT_SHADER;
static const std::string FRAGMENT_SHADER;
};

/**
Expand All @@ -71,7 +71,7 @@ class VISP_EXPORT vpPanda3DGaussianBlur : public vpPanda3DPostProcessFilter
void getRender(vpImage<unsigned char> &I) const;

private:
static const char *FRAGMENT_SHADER;
static const std::string FRAGMENT_SHADER;
};

/**
Expand All @@ -96,7 +96,7 @@ class VISP_EXPORT vpPanda3DCanny : public vpPanda3DPostProcessFilter
void setupScene() VP_OVERRIDE;

private:
static const char *FRAGMENT_SHADER;
static const std::string FRAGMENT_SHADER;
float m_edgeThreshold;
};

Expand Down
193 changes: 84 additions & 109 deletions modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,15 @@
#if defined(VISP_HAVE_PANDA3D)

BEGIN_VISP_NAMESPACE
const char *vpPanda3DLuminanceFilter::FRAGMENT_SHADER = R"shader(
#version 330
in vec2 texcoords;
uniform sampler2D p3d_Texture0;
out vec4 p3d_FragData;
void main() {
vec4 v = texture(p3d_Texture0, texcoords);
p3d_FragData.b = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b;
}
)shader";
const std::string vpPanda3DLuminanceFilter::FRAGMENT_SHADER =
"#version 330\n"
"in vec2 texcoords;\n"
"uniform sampler2D p3d_Texture0;\n"
"out vec4 p3d_FragData;\n"
"void main() {\n"
" vec4 v = texture(p3d_Texture0, texcoords);\n"
" p3d_FragData.b = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b;\n"
"}\n";

vpPanda3DLuminanceFilter::vpPanda3DLuminanceFilter(const std::string &name, std::shared_ptr<vpPanda3DRGBRenderer> inputRenderer, bool isOutput)
: vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, std::string(vpPanda3DLuminanceFilter::FRAGMENT_SHADER))
Expand All @@ -65,42 +60,34 @@ void vpPanda3DLuminanceFilter::getRender(vpImage<unsigned char> &I) const
vpPanda3DPostProcessFilter::getRenderBasic(I);
}

const char *vpPanda3DGaussianBlur::FRAGMENT_SHADER = R"shader(
#version 330
in vec2 texcoords;
uniform sampler2D p3d_Texture0;
uniform vec2 dp; // 1 divided by number of pixels
const float kernel[25] = float[25](
2, 4, 5, 4, 2,
4, 9, 12, 9, 4,
5, 12, 15, 12, 5,
4, 9, 12, 9, 4,
2, 4, 5, 4, 2
);
const float normalize = 1 / 159.0;
vec2 offset[25] = vec2[25](
vec2(-2*dp.x,-2*dp.y), vec2(-dp.x,-2*dp.y), vec2(0,-2*dp.y), vec2(dp.x,-2*dp.y), vec2(2*dp.x,-2*dp.y),
vec2(-2*dp.x,-dp.y), vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y), vec2(2*dp.x,-dp.y),
vec2(-2*dp.x,0.0), vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0), vec2(2*dp.x,0.0),
vec2(-2*dp.x, dp.y), vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y), vec2(2*dp.x, dp.y),
vec2(-2*dp.x, 2*dp.y), vec2(-dp.x, 2*dp.y), vec2(0.0, 2*dp.y), vec2(dp.x, 2*dp.y), vec2(2*dp.x, 2*dp.y)
);
out vec4 p3d_FragData;
void main() {
float v = 0.f;
for(int i = 0; i < 25; ++i) {
v += kernel[i] * texture(p3d_Texture0, texcoords + offset[i]).b ;
}
p3d_FragData.b = v * normalize;
}
)shader";
const std::string vpPanda3DGaussianBlur::FRAGMENT_SHADER =
"#version 330\n"
"in vec2 texcoords;\n"
"uniform sampler2D p3d_Texture0;\n"
"uniform vec2 dp; // 1 divided by number of pixels\n"
"const float kernel[25] = float[25](\n"
" 2, 4, 5, 4, 2,\n"
" 4, 9, 12, 9, 4,\n"
" 5, 12, 15, 12, 5,\n"
" 4, 9, 12, 9, 4,\n"
" 2, 4, 5, 4, 2\n"
");\n"
"const float normalize = 1 / 159.0;\n"
"vec2 offset[25] = vec2[25](\n"
" vec2(-2*dp.x,-2*dp.y), vec2(-dp.x,-2*dp.y), vec2(0,-2*dp.y), vec2(dp.x,-2*dp.y), vec2(2*dp.x,-2*dp.y),\n"
" vec2(-2*dp.x,-dp.y), vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y), vec2(2*dp.x,-dp.y),\n"
" vec2(-2*dp.x,0.0), vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0), vec2(2*dp.x,0.0),\n"
" vec2(-2*dp.x, dp.y), vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y), vec2(2*dp.x, dp.y),\n"
" vec2(-2*dp.x, 2*dp.y), vec2(-dp.x, 2*dp.y), vec2(0.0, 2*dp.y), vec2(dp.x, 2*dp.y), vec2(2*dp.x, 2*dp.y)\n"
");\n"
"out vec4 p3d_FragData;\n"
"void main() {\n"
" float v = 0.f;\n"
" for(int i = 0; i < 25; ++i) {\n"
" v += kernel[i] * texture(p3d_Texture0, texcoords + offset[i]).b;\n"
" }\n"
" p3d_FragData.b = v * normalize;\n"
"}\n";

vpPanda3DGaussianBlur::vpPanda3DGaussianBlur(const std::string &name, std::shared_ptr<vpPanda3DBaseRenderer> inputRenderer, bool isOutput)
: vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, vpPanda3DGaussianBlur::FRAGMENT_SHADER)
Expand All @@ -120,64 +107,53 @@ void vpPanda3DGaussianBlur::getRender(vpImage<unsigned char> &I) const
vpPanda3DPostProcessFilter::getRenderBasic(I);
}

const char *vpPanda3DCanny::FRAGMENT_SHADER = R"shader(
#version 330
in vec2 texcoords;
uniform sampler2D p3d_Texture0;
uniform vec2 dp; // 1 divided by number of pixels
uniform float edgeThreshold;
const float kernel[9] = float[9](
0.0, 1.0, 0.0,
1.0,-4.0, 1.0,
0.0, 1.0, 0.0
);
const float kernel_h[9] = float[9](
-1.0, 0.0, 1.0,
-2.0, 0.0, 2.0,
-1.0, 0.0, 1.0
);
const float kernel_v[9] = float[9](
-1.0, -2.0, -1.0,
0.0, 0.0, 0.0,
1.0, 2.0, 1.0
);
vec2 offset[9] = vec2[9](
vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y),
vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0),
vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y)
);
out vec4 p3d_FragData;
void main() {
float sum = 0.f;
for(int i = 0; i < 9; ++i) {
float pix = texture(p3d_Texture0, texcoords + offset[i]).b;
sum += pix * kernel[i];
}
if(abs(sum * 255.f) > edgeThreshold) {
float sum_h = 0.f;
float sum_v = 0.f;
for(int i = 0; i < 9; ++i) {
float pix = texture(p3d_Texture0, texcoords + offset[i]).b;
sum_h += pix * kernel_h[i];
sum_v += pix * kernel_v[i];
}
vec2 orientationAndValid = sum_h * sum_h + sum_v * sum_v > 0 ? vec2(-atan(sum_v/sum_h), 1.f) : vec2(0.f, 0.f);
p3d_FragData = vec4(sum_h, sum_v, orientationAndValid.x, orientationAndValid.y);
} else {
p3d_FragData = vec4(0.f, 0.f, 0.f, 0.f);
}
}
)shader";
const std::string vpPanda3DCanny::FRAGMENT_SHADER =
"#version 330\n"
"in vec2 texcoords;\n"
"uniform sampler2D p3d_Texture0;\n"
"uniform vec2 dp; // 1 divided by number of pixels\n"
"uniform float edgeThreshold;\n"
"const float kernel[9] = float[9](\n"
" 0.0, 1.0, 0.0,\n"
" 1.0,-4.0, 1.0,\n"
" 0.0, 1.0, 0.0\n"
");\n"
"const float kernel_h[9] = float[9](\n"
" -1.0, 0.0, 1.0,\n"
" -2.0, 0.0, 2.0,\n"
" -1.0, 0.0, 1.0\n"
");\n"
"const float kernel_v[9] = float[9](\n"
" -1.0, -2.0, -1.0,\n"
" 0.0, 0.0, 0.0,\n"
" 1.0, 2.0, 1.0\n"
");\n"
"vec2 offset[9] = vec2[9](\n"
" vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y),\n"
" vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0),\n"
" vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y)\n"
");\n"
"out vec4 p3d_FragData;\n"
"void main() {\n"
" float sum = 0.f;\n"
" for(int i = 0; i < 9; ++i) {\n"
" float pix = texture(p3d_Texture0, texcoords + offset[i]).b;\n"
" sum += pix * kernel[i];\n"
" }\n"
" if(abs(sum * 255.f) > edgeThreshold) {\n"
" float sum_h = 0.f;\n"
" float sum_v = 0.f;\n"
" for(int i = 0; i < 9; ++i) {\n"
" float pix = texture(p3d_Texture0, texcoords + offset[i]).b;\n"
" sum_h += pix * kernel_h[i];\n"
" sum_v += pix * kernel_v[i];\n"
" }\n"
" vec2 orientationAndValid = sum_h * sum_h + sum_v * sum_v > 0 ? vec2(-atan(sum_v/sum_h), 1.f) : vec2(0.f, 0.f);\n"
" p3d_FragData = vec4(sum_h, sum_v, orientationAndValid.x, orientationAndValid.y);\n"
" } else {\n"
" p3d_FragData = vec4(0.f, 0.f, 0.f, 0.f);\n"
" }\n"
"}\n";

vpPanda3DCanny::vpPanda3DCanny(const std::string &name, std::shared_ptr<vpPanda3DBaseRenderer> inputRenderer, bool isOutput, float edgeThreshold)
: vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, vpPanda3DCanny::FRAGMENT_SHADER), m_edgeThreshold(edgeThreshold)
Expand All @@ -195,7 +171,6 @@ void vpPanda3DCanny::setEdgeThreshold(float edgeThreshold)
m_renderRoot.set_shader_input("edgeThreshold", LVector2f(m_edgeThreshold));
}


FrameBufferProperties vpPanda3DCanny::getBufferProperties() const
{
FrameBufferProperties fbp;
Expand Down

0 comments on commit d11db8e

Please sign in to comment.