1#include "gl_transparent_renderer.h"
3#include <cgv_gl/gl/gl.h>
14 layer_tex(
"uint8[R,G,B,A]",TF_NEAREST,TF_NEAREST),
15 color_tex(
"uint8[R,G,B,A]",TF_NEAREST,TF_NEAREST),
16 depth_buffer(
"[D]", TF_NEAREST, TF_NEAREST)
25 if (!GLEW_EXT_framebuffer_object) {
26 last_error =
"gl_transparent_renderer::init -> framebuffer objects not supported";
55 glGetIntegerv(GL_VIEWPORT, vp);
74 std::cerr <<
"gl_transparent_renderer::init_frame -> framebuffer not complete" << std::endl;
84 glPushAttrib(GL_TEXTURE_BIT|GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
87 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
91 glBlendFuncSeparate(GL_DST_ALPHA, GL_ONE, GL_DST_ALPHA, GL_ZERO);
93 glBlendFuncSeparate(GL_ONE, GL_SRC_ALPHA, GL_DST_ALPHA, GL_ZERO);
95 glDisable(GL_DEPTH_TEST);
96 glDepthMask(GL_FALSE);
99 vec4 p_lb = inv_PV *
vec4(-1,-1, 0, 1);
100 vec4 p_rb = inv_PV *
vec4( 1,-1, 0, 1);
101 vec4 p_rt = inv_PV *
vec4( 1, 1, 0, 1);
102 vec4 p_lt = inv_PV *
vec4(-1, 1, 0, 1);
106 glVertex4fv(p_lb.
data());
108 glVertex4fv(p_rb.
data());
110 glVertex4fv(p_rt.
data());
112 glVertex4fv(p_lt.
data());
122 glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT|GL_SCISSOR_BIT);
123 glDisable(GL_SCISSOR_TEST);
125 int nr_fragments, nr_layers = 0;
127 glGetIntegerv(GL_VIEWPORT, vp);
128 GLint vp_off[4] = { 0,0, vp[2], vp[3] };
130 glDepthFunc(GL_LESS);
135 glClearColor(0, 0, 0, 1);
138 glClear(GL_COLOR_BUFFER_BIT);
149 glClear(GL_COLOR_BUFFER_BIT);
155 if (++nr_layers > max_nr_layers)
166 glClear(GL_COLOR_BUFFER_BIT);
168 glGenQueriesARB(1, &query);
169 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query);
172 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
174 GLuint nr_drawn_fragments;
175 glGetQueryObjectuivARB(query, GL_QUERY_RESULT_ARB, &nr_drawn_fragments);
176 glDeleteQueriesARB(1, &query);
178 nr_fragments = nr_drawn_fragments;
184 }
while (nr_fragments > 0);
186 front_to_back =
false;
188 front_to_back =
true;
192 glDepthFunc(GL_GREATER);
198 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
209 if (++nr_layers > max_nr_layers)
216 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
221 }
while (nr_fragments > 0);
T * data()
cast into array. This allows calls like glVertex<N><T>v(p.data()) instead of glVertex<N><T,...
base class for all drawables, which is independent of the used rendering API.
virtual dmat4 get_projection_matrix() const =0
return homogeneous 4x4 projection matrix, which transforms from eye to clip space
virtual dmat4 get_modelview_matrix() const =0
return homogeneous 4x4 viewing matrix, which transforms from world to eye space
bool create(const context &ctx, int _width=-1, int _height=-1)
create framebuffer if extension is supported, otherwise return false.
bool is_complete(const context &ctx) const
check for completeness, if not complete, get the reason in last_error
int get_width() const
return the width
void destruct(const context &ctx)
destruct the framebuffer objext
bool attach(const context &ctx, const render_buffer &rb, int i=0)
attach render buffer to depth buffer if it is a depth buffer, to stencil if it is a stencil buffer or...
bool enable(context &ctx, int i0=-1, int i1=-1, int i2=-1, int i3=-1, int i4=-1, int i5=-1, int i6=-1, int i7=-1, int i8=-1, int i9=-1, int i10=-1, int i11=-1, int i12=-1, int i13=-1, int i14=-1, int i15=-1)
enable the framebuffer either with all color attachments if no arguments are given or if arguments ar...
bool disable(context &ctx)
disable the framebuffer object
int get_height() const
return the height
OpenGL helper class to simplify depth peeling.
void copy_depth_buffer(context &ctx)
copies the current depth buffer to the second depth buffer, what is typically done before peeling a l...
bool is_front_to_back() const
return whether the mode is front to back
void begin_layer(context &ctx, int tex_unit=-1)
start to extract the next layer. Within begin_layer and end_layer the following OpenGL features are u...
virtual bool init(context &ctx)
checks for extensions and init depth peeler, return success
unsigned int end_layer(context &ctx)
finish the layer and return the number of drawn fragments in the layer. All OpenGL settings are resto...
virtual void destruct(context &ctx)
destruct the depth peeler
virtual void init_frame(context &ctx)
call this after the viewport has been set and before the first call to copy_depth_buffer or begin_lay...
cgv::signal::signal< context &, texture & > render_callback_2
signal called to render the transparent content for second and further layers with additional depth t...
int render_transparent(context &ctx, int max_nr_layers, int tex_unit=-1)
perform transparent rendering by considering a maximum of the specified number of depth layers
void init_frame(context &ctx)
configure frame buffer and textures
void blend_texture_over_viewport(context &ctx, texture &tex)
renders the given texture over the current viewport
cgv::signal::signal< context & > render_callback
signal called to render the transparent content for first layer without additional depth test
bool init(context &ctx)
checks for extensions and init depth peeler, return success
void create_and_attach_texture(context &ctx, texture &tex, int w, int h, int i=-1)
ensure that texture has the correct dimensions and is created
gl_transparent_renderer(bool front_to_back=true, float _depth_bias=0.001)
construct uninitialized depth peeler
texture color_tex
texture used as color buffer in front to back mode to blend together all transparent layers before bl...
void destruct(context &ctx)
destruct the transparent renderer
std::string last_error
a string that contains the last error, which is only set by the init method
virtual bool is_created() const
return whether component has been created
the texture class encapsulates all functionality independent of the rendering api.
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 replace_from_buffer(const context &ctx, int x, int y, int x_buffer, int y_buffer, int width, int height, int level=-1)
replace a block within a 2d texture from the current read buffer.
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.
bool destruct(const context &ctx)
destruct the texture and free texture memory and handle
cgv::math::fvec< float, 4 > vec4
declare type of 4d single precision floating point vectors (used for homogeneous coordinates)