Commit 8c8155d3 authored by schultezub's avatar schultezub

* introducing ImageMappingInformation::_textureToWorldMatrix

 * introducing ProcessorDecoratorShading and shading GLSL include
 * added Shading to SimpleRaycaster

git-svn-id: https://camplinux.in.tum.de/svn/campvis/trunk@295 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent d81337c6
......@@ -36,7 +36,7 @@ namespace TUMVis {
, _dimensionality(dimensionality)
, _size(size)
, _numElements(tgt::hmul(size))
, _mappingInformation(tgt::vec3(0.f), tgt::vec3(1.f)) // TODO: get offset/voxel size as parameter or put default values into ImageMappingInformation ctor.
, _mappingInformation(size, tgt::vec3(0.f), tgt::vec3(1.f)) // TODO: get offset/voxel size as parameter or put default values into ImageMappingInformation ctor.
{
}
......
......@@ -121,6 +121,7 @@ namespace TUMVis {
shader->setUniform(texUniform + "._sizeRCP", tgt::vec3(1.f) / tgt::vec3(_size));
shader->setUniform(texUniform + "._voxelSize", _mappingInformation.getVoxelSize());
shader->setUniform(texUniform + "._voxelSizeRCP", tgt::vec3(1.f) / _mappingInformation.getVoxelSize());
shader->setUniform(texUniform + "._textureToWorldMatrix", _mappingInformation.getTextureToWorldMatrix());
break;
default:
......
......@@ -30,16 +30,15 @@
namespace TUMVis {
ImageMappingInformation::ImageMappingInformation(const tgt::vec3& offset, const tgt::vec3& voxelSize, const LinearMapping<float>& realWorldValueMapping /*= LinearMapping<float>::identity*/)
: _offset(offset)
ImageMappingInformation::ImageMappingInformation(const tgt::vec3& size, const tgt::vec3& offset, const tgt::vec3& voxelSize, const LinearMapping<float>& realWorldValueMapping /*= LinearMapping<float>::identity*/)
: _size(size)
, _offset(offset)
, _voxelSize(voxelSize)
, _realWorldValueMapping(realWorldValueMapping)
{
updateMatrixes();
}
const tgt::vec3& ImageMappingInformation::getOffset() const {
return _offset;
}
......@@ -56,4 +55,12 @@ namespace TUMVis {
_realWorldValueMapping = rwvm;
}
void ImageMappingInformation::updateMatrixes() {
_textureToWolrdTransformation = tgt::mat4::createTranslation(_offset) * tgt::mat4::createScale(_voxelSize * _size);
}
const tgt::mat4 ImageMappingInformation::getTextureToWorldMatrix() const {
return _textureToWolrdTransformation;
}
}
......@@ -45,11 +45,13 @@ namespace TUMVis {
public:
/**
* Creates a new default ImageMappingInformation.
* \param size Image size (number of elements, redundant...)
* \param offset Position of LLF corner in world coordinates (mm)
* \param voxelSize Voxel size in (mm)
* \param realWorldValueMapping Linear mapping for mapping element values to real world values, defaults to identity.
*/
ImageMappingInformation(
const tgt::vec3& size,
const tgt::vec3& offset,
const tgt::vec3& voxelSize,
const LinearMapping<float>& realWorldValueMapping = LinearMapping<float>::identity);
......@@ -66,6 +68,12 @@ namespace TUMVis {
*/
const tgt::vec3& getVoxelSize() const;
/**
* Gets the transformation matrix from texture to world coordinates.
* \return _textureToWolrdTransformation
*/
const tgt::mat4 getTextureToWorldMatrix() const;
/**
* Get the real world value mapping.
* \return Linear mapping for mapping element values to real world values.
......@@ -79,11 +87,17 @@ namespace TUMVis {
void setRealWorldMapping(const LinearMapping<float>& rwvm);
private:
/**
* Updates the X-to-Y matrices.
*/
void updateMatrixes();
tgt::vec3 _size; ///< Image size (number of elements, redundant...)
tgt::vec3 _offset; ///< Position of LLF corner in world coordinates (mm)
tgt::vec3 _voxelSize; ///< Voxel size in (mm)
LinearMapping<float> _realWorldValueMapping; ///< Linear mapping for mapping element values to real world values
//tgt::mat4 _voxelToWorldTransformation; ///< Transformation matrix from voxel to world coordinates
tgt::mat4 _textureToWolrdTransformation; ///< Transformation matrix from texture to world coordinates
static const std::string loggerCat_;
};
......
/// This struct contains all information about a light source.
struct LightSource {
vec3 _position; ///< light position in world space
vec3 _ambientColor; ///< ambient color (r,g,b)
vec3 _diffuseColor; ///< diffuse color (r,g,b)
vec3 _specularColor; ///< specular color (r,g,b)
vec3 _attenuation; ///< attenuation (constant, linear, quadratic)
float _shininess; ///< Shininess (usually a per-material parameter, but we put it here for convenience)
};
// uniforms needed for shading
uniform float shininess_; // material shininess parameter
uniform LightSource lightSource_;
/**
* Returns attenuation factor based on the given parameters.
*
* \param attenuation Attenuation parameter vector (constant, linear, quadratic)
* \param d Distance to the light source.
*/
float computeAttenuation(in vec3 attenuation, in float d) {
float att = 1.0 / (attenuation.x + (d * attenuation.y) + (d * d * attenuation.z));
return min(att, 1.0);
}
/**
* Computes the ambient term according to the given material color \a ka and ambient intensity \a ia.
*
* \param ka Material ambient color
* \param ia Ambient light intensity
*/
vec3 getAmbientTerm(in vec3 ka, in vec3 ia) {
return ka * ia;
}
/**
* Computes the diffuse term according to the given parameters.
*
* \param kd Material diffuse color
* \param id Diffuse light intensity
* \param N Surface normal
* \param L Normalized light vector
*/
vec3 getDiffuseTerm(in vec3 kd, in vec3 id, in vec3 N, in vec3 L) {
float NdotL = max(dot(N, L), 0.0);
return kd * id * NdotL;
}
/**
* Computes the specular term according to the given parameters.
*
* \param ks Material specular color
* \param is Specular light intensity
* \param N Surface normal
* \param L Normalized light vector
* \param V View vector
* \param shininess Shininess coefficient
*/
vec3 getSpecularTerm(in vec3 ks, in vec3 is, in vec3 N, in vec3 L, in vec3 V, in float shininess) {
vec3 H = normalize(V + L);
float NdotH = pow(max(dot(N, H), 0.0), shininess);
return ks * is * NdotH;
}
/**
* Computes the phong shading according to the given parameters.
*
* \param position sample position
* \param light LightSource
* \param camera Camera position
* \param normal Normal
* \param ka Material ambient color
* \param kd Material diffuse color
* \param ks Material specular color
*/
vec3 calculatePhongShading(in vec3 position, in LightSource light, in vec3 camera, in vec3 normal, in vec3 ka, in vec3 kd, in vec3 ks) {
vec3 N = normalize(normal);
vec3 V = normalize(camera - position);
// get light source distance for attenuation and normalize light vector
vec3 L = light._position - position;
float d = length(L);
L /= d;
vec3 toReturn = getAmbientTerm(ka, light._ambientColor);
toReturn += getDiffuseTerm(kd, light._diffuseColor, N, L);
toReturn += getSpecularTerm(ks, light._specularColor, N, L, V, light._shininess);
#ifdef PHONG_APPLY_ATTENUATION
toReturn *= computeAttenuation(light._attenuation, d);
#endif
return toReturn;
}
/**
* Computes the phong shading according to the given parameters.
*
* \param position sample position
* \param light LightSource
* \param camera Camera position
* \param normal Normal
* \param materialColor Material color (used for all shading coefficients)
*/
vec3 calculatePhongShading(in vec3 position, in LightSource light, in vec3 camera, in vec3 normal, in vec3 materialColor) {
vec3 N = normalize(normal);
vec3 V = normalize(camera - position);
// get light source distance for attenuation and normalize light vector
vec3 L = light._position - position;
float d = length(L);
L /= d;
vec3 toReturn = getAmbientTerm(materialColor, light._ambientColor);
toReturn += getDiffuseTerm(materialColor, light._diffuseColor, N, L);
toReturn += getSpecularTerm(materialColor, light._specularColor, N, L, V, light._shininess);
#ifdef PHONG_APPLY_ATTENUATION
toReturn *= computeAttenuation(light._attenuation, d);
#endif
return toReturn;
}
......@@ -39,6 +39,9 @@ struct Texture3D {
// Voxel spacing
vec3 _voxelSize;
vec3 _voxelSizeRCP;
// Transformation matrices
mat4 _textureToWorldMatrix;
};
/**
......@@ -63,3 +66,14 @@ vec4 getElement3DNormalized(in Texture3D tex, in vec3 texCoords) {
//vec2 texCoordsTransformed = (texParams.matrix_ * vec4(texCoords, 0.0, 1.0)).xy;
return texture(tex._texture, texCoords);
}
/**
* Transforms texture coordinates for texture \a tex to world coordinates using the texture's
* texture-to-world matrix.
* \param tex texture
* \param texCoords texture coordinates
* \return \a texCoords transformes to woorld coordinates.
*/
vec4 textureToWorld(in Texture3D tex, in vec3 texCoords) {
return tex._textureToWorldMatrix * vec4(texCoords, 1.0);
}
\ No newline at end of file
// ================================================================================================
//
// This file is part of the TUMVis Visualization Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
// Christian Schulte zu Berge (christian.szb@in.tum.de)
// Chair for Computer Aided Medical Procedures
// Technische Universitt Mnchen
// Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
//
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
//
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ================================================================================================
#include "processordecoratorshading.h"
#include "tgt/shadermanager.h"
#include "core/properties/propertycollection.h"
namespace TUMVis {
ProcessorDecoratorShading::ProcessorDecoratorShading(const std::string& lightUniformName /*= "_lightSource"*/)
: AbstractProcessorDecorator()
, _lightPosition("LightPosition", "Light Position", tgt::vec3(-8.f), tgt::vec3(-500.f), tgt::vec3(500.f))
, _ambientColor("AmbientColor", "Ambient Light Color", tgt::vec3(0.5f), tgt::vec3(0.f), tgt::vec3(1.f))
, _diffuseColor("DiffuseColor", "Diffuse Light Color", tgt::vec3(0.75f), tgt::vec3(0.f), tgt::vec3(1.f))
, _specularColor("SpecularColor", "Specular Light Color", tgt::vec3(0.5f), tgt::vec3(0.f), tgt::vec3(1.f))
, _shininess("Shininess", "Specular Shininess", 4.f, 0.f, 64.f)
, _attenuation("Attenuation", "Attenuation Factors", tgt::vec3(0.f), tgt::vec3(0.f), tgt::vec3(1.f))
, _lightUniformName(lightUniformName)
{
}
ProcessorDecoratorShading::~ProcessorDecoratorShading() {
}
void ProcessorDecoratorShading::addProperties(HasPropertyCollection* propCollection) {
propCollection->addProperty(&_lightPosition);
propCollection->addProperty(&_ambientColor);
propCollection->addProperty(&_diffuseColor);
propCollection->addProperty(&_specularColor);
propCollection->addProperty(&_shininess);
propCollection->addProperty(&_attenuation);
}
void ProcessorDecoratorShading::renderProlog(const DataContainer& dataContainer, tgt::Shader* shader) {
shader->setUniform(_lightUniformName + "._position", _lightPosition.getValue());
shader->setUniform(_lightUniformName + "._ambientColor", _ambientColor.getValue());
shader->setUniform(_lightUniformName + "._diffuseColor", _diffuseColor.getValue());
shader->setUniform(_lightUniformName + "._specularColor", _specularColor.getValue());
shader->setUniform(_lightUniformName + "._shininess", _shininess.getValue());
shader->setUniform(_lightUniformName + "._attenuation", _attenuation.getValue());
}
std::string ProcessorDecoratorShading::generateHeader() const {
return "";
}
}
// ================================================================================================
//
// This file is part of the TUMVis Visualization Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
// Christian Schulte zu Berge (christian.szb@in.tum.de)
// Chair for Computer Aided Medical Procedures
// Technische Universitt Mnchen
// Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
//
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
//
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ================================================================================================
#ifndef PROCESSORDECORATORSHADING_H__
#define PROCESSORDECORATORSHADING_H__
#include "tgt/textureunit.h"
#include "core/datastructures/datacontainer.h"
#include "core/datastructures/imagedatarendertarget.h"
#include "core/pipeline/abstractprocessordecorator.h"
#include "core/properties/datanameproperty.h"
#include "core/properties/genericproperty.h"
#include "core/properties/numericproperty.h"
namespace TUMVis {
class ProcessorDecoratorShading : public AbstractProcessorDecorator {
public:
ProcessorDecoratorShading(const std::string& lightUniformName = "_lightSource");
virtual ~ProcessorDecoratorShading();
protected:
void addProperties(HasPropertyCollection* propCollection);
void renderProlog(const DataContainer& dataContainer, tgt::Shader* shader);
std::string generateHeader() const;
Vec3Property _lightPosition; ///< Light position
Vec3Property _ambientColor; ///< Ambient light color
Vec3Property _diffuseColor; ///< Diffuse light color
Vec3Property _specularColor; ///< Specular light color
FloatProperty _shininess; ///< Specular shininess
Vec3Property _attenuation; ///< Attenuation factors
const std::string _lightUniformName; ///< Uniform name for light
};
}
#endif // PROCESSORDECORATORSHADING_H__
......@@ -73,6 +73,8 @@ namespace TUMVis {
void RaycastingProcessor::init() {
VisualizationProcessor::init();
_shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", _fragmentShaderFilename, "", false);
_shader->setAttributeLocation(0, "in_Position");
_shader->setAttributeLocation(1, "in_TexCoord");
_shader->setHeaders(generateHeader());
_shader->rebuild();
}
......@@ -99,10 +101,8 @@ namespace TUMVis {
glEnable(GL_DEPTH_TEST);
_shader->activate();
_shader->setAttributeLocation(0, "in_Position");
_shader->setAttributeLocation(1, "in_TexCoord");
_shader->setIgnoreUniformLocationError(true);
decorateRenderProlog(data, _shader);
_shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
_shader->setUniform("_jitterEntryPoints", _jitterEntryPoints.getValue());
_shader->setUniform("_jitterStepSizeMultiplier", _jitterStepSizeMultiplier.getValue());
......@@ -111,6 +111,7 @@ namespace TUMVis {
const tgt::Camera& cam = _camera.getValue();
float n = cam.getNearDist();
float f = cam.getFarDist();
_shader->setUniform("_cameraPosition", _camera.getValue().getPosition());
_shader->setUniform("const_to_z_e_1", 0.5f + 0.5f*((f+n)/(f-n)));
_shader->setUniform("const_to_z_e_2", ((f-n)/(f*n)));
_shader->setUniform("const_to_z_w_1", ((f*n)/(f-n)));
......@@ -134,6 +135,7 @@ namespace TUMVis {
processImpl(data);
}
decorateRenderEpilog(_shader);
_shader->deactivate();
tgt::TextureUnit::setZeroUnit();
glPopAttrib();
......@@ -151,7 +153,7 @@ namespace TUMVis {
}
std::string RaycastingProcessor::generateHeader() const {
std::string toReturn;
std::string toReturn = getDecoratedHeader();
return toReturn;
}
......
......@@ -32,6 +32,7 @@
#include <string>
#include "core/pipeline/visualizationprocessor.h"
#include "core/pipeline/abstractprocessordecorator.h"
#include "core/properties/cameraproperty.h"
#include "core/properties/datanameproperty.h"
#include "core/properties/genericproperty.h"
......@@ -53,7 +54,7 @@ namespace TUMVis {
* Of course you can also directly overwrite process() yourself, but then you will need to to the
* sanity checks yourself.
*/
class RaycastingProcessor : public VisualizationProcessor {
class RaycastingProcessor : public VisualizationProcessor, public HasProcessorDecorators {
public:
/**
* Creates a RaycastingProcessor.
......
......@@ -30,6 +30,7 @@
#include "core/tools/quadrenderer.h"
#include "core/datastructures/imagedatarendertarget.h"
#include "core/pipeline/processordecoratorshading.h"
namespace TUMVis {
const std::string SimpleRaycaster::loggerCat_ = "TUMVis.modules.vis.SimpleRaycaster";
......@@ -38,7 +39,10 @@ namespace TUMVis {
: RaycastingProcessor(canvasSize, "modules/vis/simpleraycaster.frag", true)
, _targetImageID("targetImageID", "Output Image", "", DataNameProperty::WRITE)
{
addDecorator(new ProcessorDecoratorShading());
addProperty(&_targetImageID);
decoratePropertyCollection(this);
// TODO: remove hack
_transferFunction.getTF()->setIntensityDomain(tgt::vec2(0.01f, 0.05f));
......
......@@ -34,6 +34,7 @@ layout(location = 2) out vec4 out_FHN; ///< outgoing fragment first hit no
#include "tools/gradient.frag"
#include "tools/raycasting.frag"
#include "tools/shading.frag"
#include "tools/texture2d.frag"
#include "tools/texture3d.frag"
#include "tools/transferfunction.frag"
......@@ -51,6 +52,9 @@ uniform Texture3D _volume; // texture lookup parameters for volume_
uniform sampler1D _tfTex;
uniform TFParameters _tfTextureParameters;
uniform LightSource _lightSource;
uniform vec3 _cameraPosition;
uniform float _samplingStepSize;
// TODO: copy+paste from Voreen - eliminate or improve.
......@@ -75,8 +79,11 @@ vec4 performRaycasting(in vec3 entryPoint, in vec3 exitPoint, in vec2 texCoords)
while (t < tend) {
vec3 samplePosition = entryPoint.rgb + t * direction;
float intensity = getElement3DNormalized(_volume, samplePosition).a;
vec3 gradient = computeGradientCentralDifferences(_volume, samplePosition);
vec4 color = lookupTF(_tfTextureParameters, _tfTex, intensity);
color.rgb = calculatePhongShading(textureToWorld(_volume, samplePosition).xyz, _lightSource, _cameraPosition, gradient, color.xyz, color.xyz, vec3(1.0, 1.0, 1.0));
// perform compositing
if (color.a > 0.0) {
// accomodate for variable sampling rates
......@@ -89,7 +96,7 @@ vec4 performRaycasting(in vec3 entryPoint, in vec3 exitPoint, in vec2 texCoords)
if (firstHitT < 0.0 && result.a > 0.0) {
firstHitT = t;
out_FHP = vec4(samplePosition, 1.0);
out_FHN = vec4(normalize(computeGradientCentralDifferences(_volume, samplePosition)), 1.0);
out_FHN = vec4(normalize(gradient), 1.0);
}
// early ray termination
......
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