1#include "volume_renderer.h"
3#include <cgv/math/ftransform.h>
4#include <cgv_gl/gl/gl.h>
5#include <cgv_gl/gl/gl_tools.h>
11 static int ref_count = 0;
29 integration_quality = IQ_128;
34 compositing_mode = CM_BLEND;
51 isosurface_mode = IM_NONE;
56 slice_mode = SM_DISABLED;
88 std::vector<uint8_t> noise_data(size*size);
90 std::default_random_engine rng(42);
91 std::uniform_int_distribution<unsigned> dist(0u, 255u);
93 for(
size_t i = 0; i < noise_data.size(); ++i)
94 noise_data[i] =
static_cast<uint8_t
>(dist(rng));
110 return prog.
build_program(ctx,
"volume.glpr",
true, defines);
116 shader_code::set_define(defines,
"NUM_STEPS", vrs.integration_quality, volume_render_style::IQ_128);
119 shader_code::set_define(defines,
"ENABLE_LIGHTING", vrs.
enable_lighting,
false);
122 shader_code::set_define(defines,
"ENABLE_DEPTH_TEST", vrs.
enable_depth_test,
false);
124 shader_code::set_define(defines,
"ISOSURFACE_MODE", vrs.isosurface_mode, volume_render_style::IM_NONE);
127 shader_code::set_define(defines,
"SLICE_MODE", vrs.slice_mode, volume_render_style::SM_DISABLED);
129 shader_code::set_define(defines,
"COMPOSITING_MODE", vrs.compositing_mode, volume_render_style::CM_BLEND);
136 res = res && position_aam.init(ctx);
140 std::vector<vec3> positions = {
141 vec3(-0.5f, +0.5f, -0.5f),
142 vec3(+0.5f, +0.5f, -0.5f),
143 vec3(-0.5f, -0.5f, -0.5f),
144 vec3(+0.5f, -0.5f, -0.5f),
145 vec3(+0.5f, -0.5f, +0.5f),
146 vec3(+0.5f, +0.5f, -0.5f),
147 vec3(+0.5f, +0.5f, +0.5f),
148 vec3(-0.5f, +0.5f, -0.5f),
149 vec3(-0.5f, +0.5f, +0.5f),
150 vec3(-0.5f, -0.5f, -0.5f),
151 vec3(-0.5f, -0.5f, +0.5f),
152 vec3(+0.5f, -0.5f, +0.5f),
153 vec3(-0.5f, +0.5f, +0.5f),
154 vec3(+0.5f, +0.5f, +0.5f)
165 position_aam.destruct(ctx);
198 void volume_renderer::set_bounding_box(
const box3& bbox)
203 void volume_renderer::transform_to_bounding_box(
bool flag)
208 void volume_renderer::set_noise_offset(
const vec2& offset)
218 glGetIntegerv(GL_VIEWPORT, vp);
226 float specular_exponent = cgv::math::lerp(128.0f, 1.0f, cgv::math::clamp(vrs.
roughness, 0.0f, 1.0f));
291 glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)14);
298#include <cgv/gui/provider.h>
306 void* value_ptr,
const std::string& value_type,
307 const std::string& gui_type,
const std::string& options,
bool*) {
313 p->
add_member_control(b,
"Quality", vrs_ptr->integration_quality,
"dropdown",
"w=114;enums='8=8,16=16,32=32,64=64,128=128,256=256,512=512,1024=1024,2048=2048,4096=4096'",
" ");
315 p->
add_member_control(b,
"Interpolation", vrs_ptr->interpolation_mode,
"dropdown",
"enums=Nearest,Smoothed,Linear,Cubic");
319 p->
add_member_control(b,
"Compositing Mode", vrs_ptr->compositing_mode,
"dropdown",
"enums='Maximum Intensity Projection, Average, Blend'");
352 if (p->
begin_tree_node(
"Orthogonal Slicing", vrs_ptr->slice_mode,
false)) {
354 p->
add_member_control(b,
"Mode", vrs_ptr->slice_mode,
"dropdown",
"enums='Disabled,Opaque,Transparent'");
362 if(p->
begin_tree_node(
"Isosurface (Blend only)", vrs_ptr->isosurface_mode,
false)) {
364 p->
add_member_control(b,
"", vrs_ptr->isosurface_mode,
"dropdown",
"enums='Disabled,Isovalue,Alpha Threshold'");
376 "options='w=100;min=0;max=1;step=0.01;ticks=true;align=\"BL\"';align_col=' '");
385#include "gl/lib_begin.h"
base class for all classes that can be registered with support for dynamic properties (see also secti...
the data view gives access to a data array of one, two, three or four dimensions.
helper template for registration of gui creators
derive from this class to provide a gui to the current viewer
bool add_gui(const std::string &label, T &value, const std::string &gui_type="", const std::string &options="")
Add a composed gui of the given gui_type for the given value.
cgv::base::base_ptr add_decorator(const std::string &label, const std::string &decorator_type, const std::string &options="", const std::string &align="\n")
add a newly created decorator to the group
void align(const std::string &_align)
send pure alignment information
bool begin_tree_node(const std::string &label, const T &value, bool initial_visibility=false, const std::string &options="", gui_group_ptr ggp=gui_group_ptr())
Begin a sub tree of a tree structured gui.
data::ref_ptr< control< T > > add_member_control(cgv::base::base *base_ptr, const std::string &label, T &value, const std::string &gui_type="", const std::string &options="", const std::string &align="\n")
add control with callback to cgv::base::on_set method on cgv::gui::control::value_change
void end_tree_node(const T &value)
template specialization that allows to specify value reference plus node_instance by using the result...
base class for all drawables, which is independent of the used rendering API.
void set_blend_func_back_to_front()
set the default blend function for back to front blending
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
void push_depth_test_state()
push a copy of the current depth test state onto the stack saved attributes: depth test enablement,...
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
virtual void disable_depth_test()
disable the depth test
void pop_blend_state()
pop the top of the current culling state from the stack
void pop_depth_test_state()
pop the top of the current depth test state from the stack
void push_cull_state()
push a copy of the current culling state onto the stack saved attributes: cull face enablement,...
virtual void set_cull_state(CullingMode culling_mode)
set the culling state
virtual void enable_blending()
enable blending
void pop_cull_state()
pop the top of the current culling state from the stack
void pop_modelview_matrix()
see push_V for an explanation
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
virtual bool is_created() const
return whether component has been created
void manage_singleton(context &ctx, const std::string &renderer_name, int &ref_count, int ref_count_change)
used by derived classes to manage singletons
virtual bool enable(context &ctx)
enables renderer
virtual bool init(context &ctx)
call init() once before using renderer
shader_program & ref_prog()
derived renderer classes have access to shader program
virtual void enable_attribute_array_manager(const context &ctx, attribute_array_manager &aam)
call this before setting attribute arrays to manage attribute array in given manager
virtual void clear(const context &ctx)
the clear function destructs the shader program
virtual void disable_attribute_array_manager(const context &ctx, attribute_array_manager &aam)
call this after last render/draw call to ensure that no other users of renderer change attribute arra...
void set_position_array(const context &ctx, const std::vector< T > &positions)
templated method to set the position attribute from a vector of positions of type T
virtual bool validate_attributes(const context &ctx) const
call to validate, whether essential position attribute is defined
virtual bool disable(context &ctx)
disable renderer
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
bool build_program(const context &ctx, const std::string &file_name, bool show_error=false, const shader_define_map &defines=shader_define_map())
successively calls create, attach_program and link.
the texture class encapsulates all functionality independent of the rendering api.
void set_mag_filter(TextureFilter _mag_filter)
set the magnification filter
bool create(const context &ctx, TextureType _tt=TT_UNDEF, unsigned width=-1, unsigned height=-1, unsigned depth=-1)
create the texture of dimension and resolution specified in the data format base class.
bool disable(const context &ctx)
disable texture and restore state from before last enable call
bool enable(const context &ctx, int tex_unit=-1)
enable this texture in the given texture unit, -1 corresponds to the current unit.
void set_wrap_t(TextureWrap _wrap_t)
set the texture wrap behaviour in t direction
bool destruct(const context &ctx)
destruct the texture and free texture memory and handle
void set_wrap_s(TextureWrap _wrap_s)
set the texture wrap behaviour in s direction
void set_min_filter(TextureFilter _min_filter, float _anisotropy=2.0f)
set the minification filters, if minification is set to TF_ANISOTROP, the second floating point param...
renderer that supports point splatting
texture * gradient_texture
the 3D texture containing vector gradients used for lighting normal calculation
bool build_shader_program(context &ctx, shader_program &prog, const shader_define_map &defines)
build volume program
bool validate_attributes(const context &ctx) const
call to validate, whether essential position attribute is defined
void draw(context &ctx, size_t start, size_t count, bool use_strips=false, bool use_adjacency=false, uint32_t strip_restart_index=-1)
Draw a range of vertices or indexed elements.
void update_defines(shader_define_map &defines)
update shader defines based on render style
bool set_gradient_texture(texture *tex)
sets the gradient texture used for lighting
bool enable(context &ctx)
enables renderer
texture * depth_texture
a 2D texture from a frame buffer depth buffer used to combine volume rendering with opaque geometry
texture * transfer_function_texture
the 2D transfer function texture used for classification of the volume values
void init_noise_texture(context &ctx)
initializes the noise texture with random values
bool apply_bounding_box_transformation
whether to translate and scale the volume to the given bounding box during rendering
bool set_transfer_function_texture(texture *tex)
sets the transfer function used for classification; must be 1D or 2D (as loaded from an image)
virtual void clear(const context &ctx)
clean up
vec2 noise_offset
offset applied to the noise texture (can be used in conjunction with temporal anti aliasing)
bool set_volume_texture(texture *tex)
sets the 3D volume texture containing scalar values (density or other measured quantities)
render_style * create_render_style() const
overload to allow instantiation of volume_renderer
bool set_depth_texture(texture *tex)
sets the depth texture needed for rendering with additional opaque geometry
texture * volume_texture
the 3D texture used for rendering
bool init(context &ctx)
construct shader programs and return whether this was successful, call inside of init method of drawa...
bool disable(context &ctx)
disable renderer
texture noise_texture
a 2D texture containing random noise used to offset ray start positions in order to reduce ring artif...
box3 bounding_box
the bounding box of the volume in scene units
volume_renderer()
initializes position_is_center to true
@ CF_R
undefinded format with no component
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
volume_renderer & ref_volume_renderer(context &ctx, int ref_count_change)
reference to a singleton volume renderer that is shared among drawables
@ TI_UINT8
signed integer stored in 64 bits
cgv::media::axis_aligned_box< float, 3 > box3
declare type of 3d single precision floating point axis-aligned boxes
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
interface for gui creators
bool create(provider *p, const std::string &label, void *value_ptr, const std::string &value_type, const std::string &gui_type, const std::string &options, bool *)
attempt to create a gui and return whether this was successful
base class for all render styles
bool light_local_to_eye
whether the light is local to the eye position (moves with the eye) or is static to the scene
bool enable_noise_offset
whether to use the noise texture to offset ray start positions in order to reduce sampling artifacts
bool enable_lighting
whether to enable lighting
float specular_strength
material specular component strength
float ambient_strength
light ambient component strength
rgb isosurface_color
the default constant isosurface color
bool enable_depth_test
whether to enable depth testing by reading depth from a texture to allow geometry intersecting the vo...
float isovalue
the value used to check for an isosurface
float gradient_lambda
influence scale for gradient-based opacity modulation
bool isosurface_color_from_transfer_function
whether to color the isosurface based on the transfer function
float scale_adjustment_factor
the coefficient used to adjust sample opacity based on volume scaling (useful range between 50 and 50...
float slice_opacity
in case of transparent mode, slice opacity
float specular_color_mix
material specular color mix factor (0 = color from transfer function, 1 = pure white)
vec3 light_direction
the direction of the directional light
volume_render_style()
construct with default values
float roughness
material roughness (inversely proportional to specular shininess)
bool use_gradient_texture
whether to use a supplied gradient texture or compute gradients on the fly via central differences (d...
int slice_axis
coordinate axis orthogonal to which slice is rendered
@ IP_LINEAR
modification of the built-in trilinear interpolation to prevent triangular artifacts (results look bl...
float diffuse_strength
material diffuse component strength
bool enable_gradient_modulation
whether to enable modulating the volume opacity by the gradient magnitude
box3 clip_box
a bounding box used to define a subspace of the volume to be visualized
float slice_coordinate
coordinate value along axis defining slice in range [0,1]
traits class with a static function get_name() of type const char* that returns the type name of the ...