VividRendering.cpp 9.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/**************************************************************************************/
/*                                                                                    */
/*  Visualization Library                                                             */
/*  http://visualizationlibrary.org                                                   */
/*                                                                                    */
/*  Copyright (c) 2005-2016, Michele Bosi                                             */
/*  All rights reserved.                                                              */
/*                                                                                    */
/*  Redistribution and use in source and binary forms, with or without modification,  */
/*  are permitted provided that the following conditions are met:                     */
/*                                                                                    */
/*  - Redistributions of source code must retain the above copyright notice, this     */
/*  list of conditions and the following disclaimer.                                  */
/*                                                                                    */
/*  - Redistributions in binary form must reproduce the above copyright notice, this  */
/*  list of conditions and the following disclaimer in the documentation and/or       */
/*  other materials provided with the distribution.                                   */
/*                                                                                    */
/*  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.                      */
/*                                                                                    */
/**************************************************************************************/

Michele Bosi's avatar
Michele Bosi committed
32
#include <vlVivid/VividRendering.hpp>
33 34 35 36

using namespace vl;

//-----------------------------------------------------------------------------
37
VividRendering::VividRendering() {
38 39
  VL_DEBUG_SET_OBJECT_NAME()

40
  setNearFarClippingPlanesOptimized( false );
41

42
  mVividRenderer = new VividRenderer( this );
43
  // mVividRenderer->setFramebuffer( framebuffer ); // set by the render() method.
44 45 46 47 48 49
  setRenderer( mVividRenderer.get() );

  mSceneManagerActorTree = new vl::SceneManagerActorTree;
  sceneManagers()->push_back( mSceneManagerActorTree.get() );

  mCalibratedCamera = new vl::CalibratedCamera;
50
  setCamera( mCalibratedCamera.get() );
51
  mCalibratedCamera->viewport()->setScissorEnabled( true );
52 53 54
  mCalibratedCamera->viewport()->setClearColor( vl::black );
  mCalibratedCamera->setNearPlane( 10 );
  mCalibratedCamera->setFarPlane( 50000 );
55 56 57

  // Settings

58
  mRenderingMode = Vivid::DepthPeeling;
59

60
  mOpacity = 1;
61

62 63
  mOutline3DEyeOffset  = 0; // 0.25mm works well with VTK
  mOutline3DClipOffset = 0; // 0.0005 is more general but can create artifacts
64

65
  mBackgroundImageEnabled = false;
66 67 68 69 70 71 72

  // Stencil Texture

  mStencilSmoothness = 10;
  mStencilBackground = vl::black;
  mStencilEnabled = false;

73 74 75 76
  // Depth Peeling Throttle

  m_DepthPeelingAutoThrottleEnabled = true;

Michele Bosi's avatar
Michele Bosi committed
77 78
  // Volume

79
  mVividVolume = new VividVolume( this );
80 81
}
//------------------------------------------------------------------------------
82
void VividRendering::setupVolume( Image* img, const AABB& bounds, Image* transfer_func) {
83
  // Add the Actor to the scene, once
84 85
  mSceneManagerActorTree->tree()->eraseActor( vividVolume()->volumeActor() );
  mSceneManagerActorTree->tree()->addActor( vividVolume()->volumeActor() );
86
  // Initialize the volume
87
  mVividVolume->setupVolume( img, bounds, transfer_func );
88
}
89 90 91 92 93 94
//-----------------------------------------------------------------------------
ref<Effect> VividRendering::makeVividEffect(Effect* effect) {

  // Here we set only the uniforms that are not marked as <automatic> in vivid-uniforms.glsl

  ref<Effect> fx = effect == NULL ? new Effect() : effect;
95

Michele Bosi's avatar
Michele Bosi committed
96
  fx->shader()->gocLineWidth();
97
  fx->shader()->gocPointSize();
Michele Bosi's avatar
Michele Bosi committed
98
  fx->shader()->gocPolygonMode();
99 100 101 102 103 104 105 106 107

  // Material
  fx->shader()->gocUniform( "vl_Vivid.material.diffuse" )->setUniform( fvec4(0.8f, 0.8f, 0.8f, 1) );
  fx->shader()->gocUniform( "vl_Vivid.material.specular" )->setUniform( fvec4(0, 0, 0, 1) );
  fx->shader()->gocUniform( "vl_Vivid.material.ambient" )->setUniform( fvec4(0.2f, 0.2f, 0.2f, 1) );
  fx->shader()->gocUniform( "vl_Vivid.material.emission" )->setUniform( fvec4(0, 0, 0, 1) );
  fx->shader()->gocUniform( "vl_Vivid.material.shininess" )->setUniformF( 128 );

  // Fog
108 109 110 111 112 113
  fx->shader()->gocUniform("vl_Vivid.fog.mode")->setUniformI( 0 );   // 0=Off, 1=Linear, 2=Exp, 3=Exp2
  fx->shader()->gocUniform("vl_Vivid.fog.target")->setUniformI( 0 ); // 0=Color, 1=Alpha, 2=Saturation
  fx->shader()->gocUniform("vl_Vivid.fog.start")->setUniformF( 0 );
  fx->shader()->gocUniform("vl_Vivid.fog.end")->setUniformF( 1 );
  fx->shader()->gocUniform("vl_Vivid.fog.density")->setUniformF( 1 );
  fx->shader()->gocUniform("vl_Vivid.fog.color")->setUniform( vl::black );
114 115 116 117 118 119 120

  // Light
  fx->shader()->gocUniform( "vl_Vivid.light.diffuse" )->setUniform( fvec4(1, 1, 1, 1) );
  fx->shader()->gocUniform( "vl_Vivid.light.specular" )->setUniform( fvec4(1, 1, 1, 1) );
  fx->shader()->gocUniform( "vl_Vivid.light.ambient" )->setUniform( fvec4(0, 0, 0, 1) );
  fx->shader()->gocUniform( "vl_Vivid.light.position" )->setUniform( fvec4(0, 0, 0, 1) );

121
  fx->shader()->gocUniform( "vl_Vivid.enableLighting" )->setUniformI( 1 );
Michele Bosi's avatar
Michele Bosi committed
122
  fx->shader()->gocUniform( "vl_Vivid.enablePointSprite" )->setUniformI( 0 );
123 124 125 126

  // Outline
  fx->shader()->gocUniform("vl_Vivid.renderMode")->setUniformI( 0 ); // 0=Polys, 1=Outline3D, 2=Polys+Outline3D, 3=Slice, 4=Outline2D, 5=Polys+Outline2D
  fx->shader()->gocUniform("vl_Vivid.outline.color")->setUniform( vl::white );
Michele Bosi's avatar
Michele Bosi committed
127
  fx->shader()->gocUniform("vl_Vivid.outline.width")->setUniformF( 2.0f );
128 129 130 131 132 133
  fx->shader()->gocUniform("vl_Vivid.outline.slicePlane")->setUniform( vl::vec4( 1, 0, 0, 0 ) );

  // Smart Clipping
  for( char i = '0'; i < '4'; ++i ) {
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].mode").c_str())->setUniformI( 0 ); // 0=OFF, 1=Sphere, 2=Box, 3=Plane
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].target").c_str())->setUniformI( 0 ); // 0=Color, 1=Alpha, 2=Saturation
Michele Bosi's avatar
Michele Bosi committed
134
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].fadeRange").c_str())->setUniformF( 0.0 ); // 0=Sharp, 0...X=Fuzzy
135 136 137 138 139 140 141 142
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].color").c_str())->setUniform( vl::black );
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].plane").c_str())->setUniform( vl::vec4( 0, 0, 0, 0 ) );  // Plane Nx, Ny, Nz, Pd (distance from origin) (World Coords)
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].sphere").c_str())->setUniform( vl::vec4( 0, 0, 0, 0 ) ); // Sphere X, Y, Z, Radius (World Coords)
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].boxMin").c_str())->setUniform( vl::vec3( 0, 0, 0 ) ); // AABB min edge (World Coords)
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].boxMax").c_str())->setUniform( vl::vec3( 0, 0, 0 ) ); // AABB max edge (World Coords)
    fx->shader()->gocUniform((std::string("vl_Vivid.smartClip[") + i + "].reverse").c_str())->setUniformI( 0 ); // 0=false, 1=true (Reverses what's in and what's out)
  }

Michele Bosi's avatar
Michele Bosi committed
143
  // User Texture Mapping
144

145
  ref<vl::Texture> texture = new Texture( 32, 32, vl::TF_RGBA );
Michele Bosi's avatar
Michele Bosi committed
146 147
  texture->getTexParameter()->setAnisotropy( 16 );
  texture->getTexParameter()->setMinFilter( vl::TPF_LINEAR );
148 149
  texture->getTexParameter()->setMagFilter( vl::TPF_LINEAR );
  texture->getTexParameter()->setWrap( vl::TPW_CLAMP_TO_EDGE );
150
  fx->shader()->gocTextureSampler( vl::Vivid::UserTexture )->setTexture( texture.get() );
151 152 153
  fx->shader()->gocUniform( "vl_UserTexture1D" )->setUniformI( vl::Vivid::UserTexture );
  fx->shader()->gocUniform( "vl_UserTexture2D" )->setUniformI( vl::Vivid::UserTexture );
  fx->shader()->gocUniform( "vl_UserTexture3D" )->setUniformI( vl::Vivid::UserTexture );
Michele Bosi's avatar
Michele Bosi committed
154
  fx->shader()->gocUniform( "vl_Vivid.enableTextureMapping" )->setUniformI( 0 );
155
  fx->shader()->gocUniform( "vl_Vivid.textureDimension" )->setUniformI( 0 );
156

157 158 159 160 161 162
  // Used by the fast renderer - ignored by depth peeling

  fx->shader()->enable( vl::EN_BLEND );
  fx->shader()->enable( vl::EN_DEPTH_TEST );
  fx->shader()->enable( vl::EN_LIGHTING );

163 164
  fx->shader()->setRenderState( new vl::Light, 0 );

165 166 167 168 169
  fx->shader()->gocMaterial()->setColorMaterialEnabled( false );
  fx->shader()->gocMaterial()->setColorMaterial( vl::PF_FRONT_AND_BACK, vl::CM_DIFFUSE );

  fx->shader()->gocLightModel()->setTwoSide( true );
  fx->shader()->gocLightModel()->setLocalViewer( true );
170

171 172 173 174 175
  fx->shader()->gocDepthMask()->set( true );

  return fx;
}
//-----------------------------------------------------------------------------