16.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 9436df53 authored by Declara Denis's avatar Declara Denis Committed by Christian Schulte zu Berge
Browse files

Initial effort to support 2D and 3D textures in GlImageResampler

parent 063265f4
...@@ -25,14 +25,27 @@ ...@@ -25,14 +25,27 @@
in vec3 ex_TexCoord; in vec3 ex_TexCoord;
out vec4 out_Color; out vec4 out_Color;
#include "tools/gradient.frag" #ifdef GLRESAMPLER_3D
#include "tools/texture3d.frag" #include "tools/texture3d.frag"
uniform sampler3D _texture; uniform sampler3D _texture;
uniform TextureParameters3D _textureParams; uniform TextureParameters3D _textureParams;
uniform float _zTexCoord; uniform float _zTexCoord;
#endif
#ifdef GLRESAMPLER_2D
#include "tools/texture2d.frag"
uniform sampler2D _texture;
uniform TextureParameters2D _textureParams;
#endif
void main() { void main() {
out_Color = texture(_texture, vec3(ex_TexCoord.xy, _zTexCoord)); #ifdef GLRESAMPLER_3D
vec4 sample = texture(_texture, vec3(ex_TexCoord.xy, _zTexCoord));
#endif
#ifdef GLRESAMPLER_2D
vec4 sample = texture(_texture, ex_TexCoord.xy);
#endif
out_Color = sample;
} }
...@@ -45,7 +45,8 @@ namespace campvis { ...@@ -45,7 +45,8 @@ namespace campvis {
, p_outputImage("OutputImage", "Output Image", "GlImageResampler.out", DataNameProperty::WRITE) , p_outputImage("OutputImage", "Output Image", "GlImageResampler.out", DataNameProperty::WRITE)
, p_resampleScale("ResampleScale", "Resampling Scale", .5f, .01f, 10.f) , p_resampleScale("ResampleScale", "Resampling Scale", .5f, .01f, 10.f)
, p_targetSize("TargetSize", "Size of Resampled Image", cgt::ivec3(128), cgt::ivec3(1), cgt::ivec3(1024)) , p_targetSize("TargetSize", "Size of Resampled Image", cgt::ivec3(128), cgt::ivec3(1), cgt::ivec3(1024))
, _shader(0) , _shader2D(0)
, _shader3D(0)
{ {
addProperty(p_inputImage, INVALID_RESULT | INVALID_PROPERTIES); addProperty(p_inputImage, INVALID_RESULT | INVALID_PROPERTIES);
addProperty(p_outputImage); addProperty(p_outputImage);
...@@ -60,13 +61,18 @@ namespace campvis { ...@@ -60,13 +61,18 @@ namespace campvis {
void GlImageResampler::init() { void GlImageResampler::init() {
VisualizationProcessor::init(); VisualizationProcessor::init();
_shader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimageresampler.frag", ""); _shader2D = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimageresampler.frag", "#define GLRESAMPLER_3D\n");
_shader->setAttributeLocation(0, "in_Position"); _shader2D->setAttributeLocation(0, "in_Position");
_shader->setAttributeLocation(1, "in_TexCoord"); _shader2D->setAttributeLocation(1, "in_TexCoord");
_shader3D = ShdrMgr.load("core/glsl/passthrough.vert", "modules/preprocessing/glsl/glimageresampler.frag", "#define GLRESAMPLER_2D\n");
_shader3D->setAttributeLocation(0, "in_Position");
_shader3D->setAttributeLocation(1, "in_TexCoord");
} }
void GlImageResampler::deinit() { void GlImageResampler::deinit() {
ShdrMgr.dispose(_shader); ShdrMgr.dispose(_shader2D);
ShdrMgr.dispose(_shader3D);
VisualizationProcessor::deinit(); VisualizationProcessor::deinit();
} }
...@@ -76,34 +82,46 @@ namespace campvis { ...@@ -76,34 +82,46 @@ namespace campvis {
if (img != 0) { if (img != 0) {
cgt::vec3 originalSize(img->getSize()); cgt::vec3 originalSize(img->getSize());
const cgt::ivec3& resampledSize = p_targetSize.getValue(); const cgt::ivec3& resampledSize = p_targetSize.getValue();
bool isTexture2D = img->getParent()->getDimensionality() == 2;
// 2D textures should not be scaled along the z axis
if (isTexture2D) {
resampledSize.z = 1;
}
cgt::TextureUnit inputUnit; cgt::TextureUnit inputUnit;
inputUnit.activate(); inputUnit.activate();
// create texture for result // create texture for result
cgt::Texture* resultTexture = new cgt::Texture(GL_TEXTURE_3D, resampledSize, img->getTexture()->getInternalFormat(), cgt::Texture::LINEAR); cgt::Texture* resultTexture = new cgt::Texture(isTexture2D ? GL_TEXTURE_2D : GL_TEXTURE_3D, resampledSize, img->getTexture()->getInternalFormat(), cgt::Texture::LINEAR);
// activate shader and bind textures // Select the right shader for the 2D and 3D case
_shader->activate(); cgt::Shader *shader = isTexture2D ? _shader2D : _shader3D;
img->bind(_shader, inputUnit);
// activate shader and bind textures
shader->activate();
img->bind(shader, inputUnit);
// activate FBO and attach texture // activate FBO and attach texture
_fbo->activate(); _fbo->activate();
glViewport(0, 0, static_cast<GLsizei>(resampledSize.x), static_cast<GLsizei>(resampledSize.y)); glViewport(0, 0, static_cast<GLsizei>(resampledSize.x), static_cast<GLsizei>(resampledSize.y));
// render quad to compute difference measure by shader // render quad to compute difference measure by shader
for (int z = 0; z < resampledSize.z; ++z) { for (int z = 0; z < resampledSize.z; ++z) {
float zTexCoord = static_cast<float>(z)/static_cast<float>(resampledSize.z) + .5f/static_cast<float>(resampledSize.z); if (!isTexture2D) {
_shader->setUniform("_zTexCoord", zTexCoord); float zTexCoord = static_cast<float>(z)/static_cast<float>(resampledSize.z) + .5f/static_cast<float>(resampledSize.z);
shader->setUniform("_zTexCoord", zTexCoord);
}
_fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z); _fbo->attachTexture(resultTexture, GL_COLOR_ATTACHMENT0, 0, z);
LGL_ERROR;
QuadRdr.renderQuad(); QuadRdr.renderQuad();
} }
_fbo->detachAll(); _fbo->detachAll();
_fbo->deactivate(); _fbo->deactivate();
_shader->deactivate(); shader->deactivate();
// put resulting image into DataContainer // put resulting image into DataContainer
ImageData* id = new ImageData(3, resampledSize, 1); ImageData* id = new ImageData(img->getParent()->getDimensionality(), resampledSize, img->getParent()->getNumChannels());
ImageRepresentationGL::create(id, resultTexture); ImageRepresentationGL::create(id, resultTexture);
const ImageMappingInformation& imi = img->getParent()->getMappingInformation(); const ImageMappingInformation& imi = img->getParent()->getMappingInformation();
id->setMappingInformation(ImageMappingInformation(img->getSize(), imi.getOffset(), imi.getVoxelSize() / p_resampleScale.getValue(), imi.getCustomTransformation())); id->setMappingInformation(ImageMappingInformation(img->getSize(), imi.getOffset(), imi.getVoxelSize() / p_resampleScale.getValue(), imi.getCustomTransformation()));
......
...@@ -84,7 +84,8 @@ namespace campvis { ...@@ -84,7 +84,8 @@ namespace campvis {
virtual void updateResult(DataContainer& dataContainer); virtual void updateResult(DataContainer& dataContainer);
virtual void updateProperties(DataContainer& dataContainer); virtual void updateProperties(DataContainer& dataContainer);
cgt::Shader* _shader; ///< Shader for resampling cgt::Shader* _shader2D; ///< Shader for resampling 2D textures
cgt::Shader* _shader3D; ///< Shader for resampling 3D textures
static const std::string loggerCat_; static const std::string loggerCat_;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment