Skip to content

Commit

Permalink
Add proof of concept for depth test state stack
Browse files Browse the repository at this point in the history
  • Loading branch information
David Groß committed Jan 25, 2024
1 parent 1f7a519 commit 6760e53
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 102 deletions.
30 changes: 30 additions & 0 deletions cgv/render/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ class CGV_API context : public render_types, public context_config
std::stack<frame_buffer_base*> frame_buffer_stack;
/// stack of currently enabled shader programs
std::stack<shader_program_base*> shader_program_stack;







std::stack<bool> depth_test_stack;






public:
/// check for current program, prepare it for rendering and return pointer to it
shader_program_base* get_current_program() const;
Expand Down Expand Up @@ -1203,6 +1217,22 @@ class CGV_API context : public render_types, public context_config
virtual void recover_from_external_viewport_change(const ivec4& cgv_viewport_storage) = 0;
/// query the maximum number of supported window transformations, which is at least 1
virtual unsigned get_max_window_transformation_array_size() const = 0;





virtual void enable_depth_test() {}
virtual void disable_depth_test() {}

virtual void restore_depth_test_state() {
if(depth_test_stack.empty())
error("context::pop_depth_test_state() ... attempt to pop depth test state from empty stack.");
else
depth_test_stack.pop();
}


protected:
bool ensure_window_transformation_index(int& array_index);
public:
Expand Down
232 changes: 132 additions & 100 deletions libs/cgv_gl/gl/gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extern CGV_API GLuint map_to_gl(PrimitiveType pt);

extern CGV_API GLuint map_to_gl(MaterialSide ms);

/// set material in opengl state to given material
extern CGV_API void set_material(const cgv::media::illum::phong_material& mat, MaterialSide ms, float alpha);
/// set material in opengl state to given material
extern CGV_API void set_material(const cgv::media::illum::phong_material& mat, MaterialSide ms, float alpha);


/** implementation of the context API for the OpenGL API excluding methods for font selection, redraw and
Expand All @@ -48,14 +48,14 @@ class CGV_API gl_context : public render::context
void ensure_configured() const;
void destruct_render_objects();
void put_id(void* handle, void* ptr) const;
void draw_elements_void(GLenum mode, size_t count, GLenum type, size_t type_size, const void* indices) const;
template <typename T>
void draw_elements(GLenum mode, size_t count, const T* indices) const {
if (!gl_traits<T>::valid_index)
error("called draw_elements with invalid index type");
else
draw_elements_void(mode, count, gl_traits<T>::type, sizeof(T), indices);
}
void draw_elements_void(GLenum mode, size_t count, GLenum type, size_t type_size, const void* indices) const;
template <typename T>
void draw_elements(GLenum mode, size_t count, const T* indices) const {
if (!gl_traits<T>::valid_index)
error("called draw_elements with invalid index type");
else
draw_elements_void(mode, count, gl_traits<T>::type, sizeof(T), indices);
}

cgv::data::component_format texture_find_best_format(const cgv::data::component_format& cf, render_component& rc, const std::vector<cgv::data::data_view>* palettes = 0) const;
bool texture_create(texture_base& tb, cgv::data::data_format& df) const;
Expand All @@ -71,47 +71,47 @@ class CGV_API gl_context : public render::context
bool texture_disable(texture_base& tb, int tex_unit, unsigned int nr_dims) const;
bool texture_bind_as_image(texture_base& tb, int tex_unit, int level, bool bind_array, int layer, AccessType access) const;

bool render_buffer_create(render_buffer_base& rc, cgv::data::component_format& cf, int& _width, int& _height) const;
bool render_buffer_destruct(render_buffer_base& rc) const;

bool frame_buffer_create(frame_buffer_base& fbb) const;
bool render_buffer_create(render_buffer_base& rc, cgv::data::component_format& cf, int& _width, int& _height) const;
bool render_buffer_destruct(render_buffer_base& rc) const;

bool frame_buffer_create(frame_buffer_base& fbb) const;
bool frame_buffer_attach(frame_buffer_base& fbb, const render_buffer_base& rb, bool is_depth, int i) const;
bool frame_buffer_attach(frame_buffer_base& fbb, const texture_base& t, bool is_depth, int level, int i, int z) const;
bool frame_buffer_is_complete(const frame_buffer_base& fbb) const;
bool frame_buffer_enable(frame_buffer_base& fbb);
bool frame_buffer_disable(frame_buffer_base& fbb);
bool frame_buffer_destruct(frame_buffer_base& fbb) const;
bool frame_buffer_destruct(frame_buffer_base& fbb) const;
void frame_buffer_blit(const frame_buffer_base* src_fbb_ptr, const ivec4& S, frame_buffer_base* dst_fbb_ptr, const ivec4& _D, BufferTypeBits btbs, bool interpolate) const;
int frame_buffer_get_max_nr_color_attachments() const;
int frame_buffer_get_max_nr_draw_buffers() const;

bool shader_code_create(render_component& sc, ShaderType st, const std::string& source) const;
bool shader_code_compile(render_component& sc) const;
void shader_code_destruct(render_component& sc) const;

bool shader_program_create(shader_program_base& spb) const;
void shader_program_attach(shader_program_base& spb, const render_component& sc) const;
bool shader_program_link(shader_program_base& spb) const;
bool shader_program_set_state(shader_program_base& spb) const;
bool shader_program_enable(shader_program_base& spb);
bool shader_program_disable(shader_program_base& spb);
void shader_program_detach(shader_program_base& spb, const render_component& sc) const;
bool shader_program_destruct(shader_program_base& spb) const;

int get_uniform_location(const shader_program_base& spb, const std::string& name) const;
bool set_uniform_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const;
bool set_uniform_array_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr, size_t nr_elements) const;
int get_attribute_location(const shader_program_base& spb, const std::string& name) const;
bool set_attribute_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const;

bool attribute_array_binding_create(attribute_array_binding_base& aab) const;
bool attribute_array_binding_destruct(attribute_array_binding_base& aab);
bool attribute_array_binding_enable(attribute_array_binding_base& aab);
bool attribute_array_binding_disable(attribute_array_binding_base& aab);
bool set_attribute_array_void(attribute_array_binding_base* aab, int loc, type_descriptor value_type, const vertex_buffer_base* vbb, const void* ptr, size_t nr_elements, unsigned stride_in_bytes) const;
bool set_element_array(attribute_array_binding_base* aab, const vertex_buffer_base* vbb) const;
bool enable_attribute_array(attribute_array_binding_base* aab, int loc, bool do_enable) const;
bool is_attribute_array_enabled(const attribute_array_binding_base* aab, int loc) const;
bool shader_code_create(render_component& sc, ShaderType st, const std::string& source) const;
bool shader_code_compile(render_component& sc) const;
void shader_code_destruct(render_component& sc) const;

bool shader_program_create(shader_program_base& spb) const;
void shader_program_attach(shader_program_base& spb, const render_component& sc) const;
bool shader_program_link(shader_program_base& spb) const;
bool shader_program_set_state(shader_program_base& spb) const;
bool shader_program_enable(shader_program_base& spb);
bool shader_program_disable(shader_program_base& spb);
void shader_program_detach(shader_program_base& spb, const render_component& sc) const;
bool shader_program_destruct(shader_program_base& spb) const;

int get_uniform_location(const shader_program_base& spb, const std::string& name) const;
bool set_uniform_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const;
bool set_uniform_array_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr, size_t nr_elements) const;
int get_attribute_location(const shader_program_base& spb, const std::string& name) const;
bool set_attribute_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const;

bool attribute_array_binding_create(attribute_array_binding_base& aab) const;
bool attribute_array_binding_destruct(attribute_array_binding_base& aab);
bool attribute_array_binding_enable(attribute_array_binding_base& aab);
bool attribute_array_binding_disable(attribute_array_binding_base& aab);
bool set_attribute_array_void(attribute_array_binding_base* aab, int loc, type_descriptor value_type, const vertex_buffer_base* vbb, const void* ptr, size_t nr_elements, unsigned stride_in_bytes) const;
bool set_element_array(attribute_array_binding_base* aab, const vertex_buffer_base* vbb) const;
bool enable_attribute_array(attribute_array_binding_base* aab, int loc, bool do_enable) const;
bool is_attribute_array_enabled(const attribute_array_binding_base* aab, int loc) const;

bool vertex_buffer_bind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const;
bool vertex_buffer_unbind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const;
Expand All @@ -135,17 +135,17 @@ class CGV_API gl_context : public render::context
///
bool show_help;
bool show_stats;
/// helper function that multiplies a rotation to modelview matrix such that vector is rotated onto target
/// helper function that multiplies a rotation to modelview matrix such that vector is rotated onto target
void rotate_vector_to_target(const dvec3& vector, const dvec3& target);
public:
/// construct context and attach signals
gl_context();
/// ensure that glew is initialized, define lighting mode, viewing pyramid and the rendering mode and return whether gl configuration was successful
bool configure_gl();
void resize_gl();
/// overwrite function to return info font size in case no font is currently selected
float get_current_font_size() const;
/// overwrite function to return info font face in case no font is currently selected
/// overwrite function to return info font size in case no font is currently selected
float get_current_font_size() const;
/// overwrite function to return info font face in case no font is currently selected
media::font::font_face_ptr get_current_font_face() const;
///
void perform_screen_shot();
Expand All @@ -156,25 +156,25 @@ class CGV_API gl_context : public render::context
///
void finish_render_pass();
/**@name light and materials management*/
//@{
/// set the current color
void set_color(const rgba& clr);
/// set the current material
void set_material(const cgv::media::illum::surface_material& mat);
/// enable a material with textures
void enable_material(textured_material& mat);
/// disable a material with textures
void disable_material(textured_material& mat);
/// return a reference to a shader program used to render without illumination
shader_program& ref_default_shader_program(bool texture_support = false);
/// return a reference to the default shader program used to render surfaces
shader_program& ref_surface_shader_program(bool texture_support = false);
/// get list of program uniforms
void enumerate_program_uniforms(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr = 0, std::vector<int>* sizes_ptr = 0, std::vector<int>* types_ptr = 0, bool show = false) const;
/// get list of program attributes
void enumerate_program_attributes(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr = 0, std::vector<int>* sizes_ptr = 0, std::vector<int>* types_ptr = 0, bool show = false) const;
/// helper function to send light update events
void on_lights_changed();
//@{
/// set the current color
void set_color(const rgba& clr);
/// set the current material
void set_material(const cgv::media::illum::surface_material& mat);
/// enable a material with textures
void enable_material(textured_material& mat);
/// disable a material with textures
void disable_material(textured_material& mat);
/// return a reference to a shader program used to render without illumination
shader_program& ref_default_shader_program(bool texture_support = false);
/// return a reference to the default shader program used to render surfaces
shader_program& ref_surface_shader_program(bool texture_support = false);
/// get list of program uniforms
void enumerate_program_uniforms(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr = 0, std::vector<int>* sizes_ptr = 0, std::vector<int>* types_ptr = 0, bool show = false) const;
/// get list of program attributes
void enumerate_program_attributes(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr = 0, std::vector<int>* sizes_ptr = 0, std::vector<int>* types_ptr = 0, bool show = false) const;
/// helper function to send light update events
void on_lights_changed();
///
void tesselate_arrow(double length = 1, double aspect = 0.1, double rel_tip_radius = 2.0, double tip_aspect = 0.3, int res = 25, bool edges = false);
///
Expand Down Expand Up @@ -210,21 +210,21 @@ class CGV_API gl_context : public render::context
/**@name tesselation*/
//@{
/// helper function to prepare attribute arrays
bool prepare_attributes(std::vector<vec3>& P, std::vector<vec3>& N, std::vector<vec2>& T, unsigned nr_vertices,
const float* vertices, const float* normals, const float* tex_coords,
bool prepare_attributes(std::vector<vec3>& P, std::vector<vec3>& N, std::vector<vec2>& T, unsigned nr_vertices,
const float* vertices, const float* normals, const float* tex_coords,
const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals) const;
/// helper function to disable attribute arrays
bool release_attributes(const float* normals, const float* tex_coords, const int* normal_indices, const int* tex_coord_indices) const;
/// pass geometry of given faces to current shader program and generate draw calls to render lines for the edges
void draw_edges_of_faces(
const float* vertices, const float* normals, const float* tex_coords,
const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
int nr_faces, int face_degree, bool flip_normals = false) const;
/// pass geometry of given strip or fan to current shader program and generate draw calls to render lines for the edges
void draw_edges_of_strip_or_fan(
const float* vertices, const float* normals, const float* tex_coords,
const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
int nr_faces, int face_degree, bool is_fan, bool flip_normals = false) const;
/// pass geometry of given faces to current shader program and generate draw calls to render lines for the edges
void draw_edges_of_faces(
const float* vertices, const float* normals, const float* tex_coords,
const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
int nr_faces, int face_degree, bool flip_normals = false) const;
/// pass geometry of given strip or fan to current shader program and generate draw calls to render lines for the edges
void draw_edges_of_strip_or_fan(
const float* vertices, const float* normals, const float* tex_coords,
const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
int nr_faces, int face_degree, bool is_fan, bool flip_normals = false) const;
/// tesselate with independent faces
void draw_faces(
const float* vertices, const float* normals, const float* tex_coords,
Expand All @@ -239,29 +239,61 @@ class CGV_API gl_context : public render::context

/**@name transformations*/
//@{
/// return homogeneous 4x4 viewing matrix, which transforms from world to eye space
dmat4 get_modelview_matrix() const;
/// set the current modelview matrix, which transforms from world to eye space
void set_modelview_matrix(const dmat4& V);
/// return homogeneous 4x4 projection matrix, which transforms from eye to clip space
dmat4 get_projection_matrix() const;
/// set the current projection matrix, which transforms from eye to clip space
void set_projection_matrix(const dmat4& P);

/// restore previous viewport and depth range arrays defining the window transformations
void pop_window_transformation_array();
/// query the maximum number of supported window transformations, which is at least 1
unsigned get_max_window_transformation_array_size() const;
protected:
void update_window_transformation_array();
public:
/// set the current viewport or one of the viewports in the window transformation array
void set_viewport(const ivec4& viewport, int array_index = -1);
/// set the current depth range or one of the depth ranges in the window transformation array
void set_depth_range(const dvec2& depth_range = dvec2(0, 1), int array_index = -1);

// return homogeneous 4x4 projection matrix, which transforms from clip to device space
// dmat4 get_device_matrix() const;
/// return homogeneous 4x4 viewing matrix, which transforms from world to eye space
dmat4 get_modelview_matrix() const;
/// set the current modelview matrix, which transforms from world to eye space
void set_modelview_matrix(const dmat4& V);
/// return homogeneous 4x4 projection matrix, which transforms from eye to clip space
dmat4 get_projection_matrix() const;
/// set the current projection matrix, which transforms from eye to clip space
void set_projection_matrix(const dmat4& P);

/// restore previous viewport and depth range arrays defining the window transformations
void pop_window_transformation_array();
/// query the maximum number of supported window transformations, which is at least 1
unsigned get_max_window_transformation_array_size() const;




void push_previous_depth_test_state() {
GLboolean state;
glGetBooleanv(GL_DEPTH_TEST, &state);
depth_test_stack.push(static_cast<bool>(state));
}

virtual void enable_depth_test() {
push_previous_depth_test_state();
glEnable(GL_DEPTH_TEST);
}

virtual void disable_depth_test() {
push_previous_depth_test_state();
glDisable(GL_DEPTH_TEST);
}

virtual void restore_depth_test_state() {
bool state = depth_test_stack.top();

if(state)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);

context::restore_depth_test_state();
}


protected:
void update_window_transformation_array();
public:
/// set the current viewport or one of the viewports in the window transformation array
void set_viewport(const ivec4& viewport, int array_index = -1);
/// set the current depth range or one of the depth ranges in the window transformation array
void set_depth_range(const dvec2& depth_range = dvec2(0, 1), int array_index = -1);

// return homogeneous 4x4 projection matrix, which transforms from clip to device space
// dmat4 get_device_matrix() const;
/// read the device z-coordinate from the z-buffer for the given device x- and y-coordinates
double get_window_z(int x_window, int y_window) const;
//@}
Expand Down
6 changes: 4 additions & 2 deletions libs/cgv_gl/volume_renderer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ namespace cgv {
ref_prog().set_uniform(ctx, "clip_box_min", vrs.clip_box.get_min_pnt());
ref_prog().set_uniform(ctx, "clip_box_max", vrs.clip_box.get_max_pnt());

glDisable(GL_DEPTH_TEST);
//glDisable(GL_DEPTH_TEST);
ctx.disable_depth_test();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_FRONT);
Expand All @@ -257,7 +258,8 @@ namespace cgv {
glCullFace(GL_BACK);
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_DEPTH_TEST);
ctx.restore_depth_test_state();

return renderer::disable(ctx);
}
Expand Down
Loading

0 comments on commit 6760e53

Please sign in to comment.