From a63ada1a932670ca63f4909abe44b72e9b9fa5cb Mon Sep 17 00:00:00 2001 From: gumhold Date: Sat, 23 Mar 2019 00:03:56 +0100 Subject: [PATCH] removed several mesh and textured material problems --- cgv/media/mesh/simple_mesh.cxx | 33 +++++++++++++++++ cgv/media/mesh/simple_mesh.h | 3 +- cgv/render/clipped_view.cxx | 2 +- cgv/render/texture.cxx | 55 ++++++++++++++++++----------- cgv/render/textured_material.cxx | 1 + libs/cgv_gl/gl/mesh_render_info.cxx | 2 +- libs/cgv_gl/gl/mesh_render_info.h | 3 +- libs/cgv_gl/glsl/surface.glsl | 16 ++++++--- 8 files changed, 85 insertions(+), 30 deletions(-) diff --git a/cgv/media/mesh/simple_mesh.cxx b/cgv/media/mesh/simple_mesh.cxx index d992534bf..3e54c1eb4 100644 --- a/cgv/media/mesh/simple_mesh.cxx +++ b/cgv/media/mesh/simple_mesh.cxx @@ -194,6 +194,39 @@ typename simple_mesh::box_type simple_mesh::compute_box() const box.add_point(p); return box; } +/// compute vertex normals by averaging triangle normals +template +void simple_mesh::compute_vertex_normals() +{ + // clear previous normal info + if (has_normals()) + normals.clear(); + // copy position indices to normals + normal_indices = position_indices; + // initialize normals to null vectors + normals.resize(positions.size(), vec3(0.0f)); + // iterate faces + for (idx_type fi = 0; fi < get_nr_faces(); ++fi) { + idx_type c0 = begin_corner(fi); + idx_type ce = end_corner(fi); + vec3 p0 = position(position_indices[c0]); + vec3 dj = position(position_indices[c0 + 1]) - p0; + vec3 nml(0.0f); + for (idx_type ci = c0+2; ci < ce; ++ci) { + vec3 di = position(position_indices[ci]) - p0; + nml += cross(dj, di); + dj = di; + } + T nl = nml.length(); + if (nl > 1e-8f) { + nml *= 1.0f/nl; + for (idx_type ci = c0; ci < ce; ++ci) + normal(normal_indices[ci]) += nml; + } + } + for (auto& n : normals) + n.normalize(); +} /// extract vertex attribute array and element array buffers for triangulation and edges in wireframe template diff --git a/cgv/media/mesh/simple_mesh.h b/cgv/media/mesh/simple_mesh.h index ec620edbe..ef2b36679 100644 --- a/cgv/media/mesh/simple_mesh.h +++ b/cgv/media/mesh/simple_mesh.h @@ -89,7 +89,8 @@ class CGV_API simple_mesh : public simple_mesh_base bool has_normals() const { return get_nr_normals() > 0; } /// compute the axis aligned bounding box box_type compute_box() const; - + /// compute vertex normals by averaging triangle normals + void compute_vertex_normals(); /// clear simple mesh void clear(); /// read simple mesh from file diff --git a/cgv/render/clipped_view.cxx b/cgv/render/clipped_view.cxx index 282c94a0b..208a844ee 100644 --- a/cgv/render/clipped_view.cxx +++ b/cgv/render/clipped_view.cxx @@ -49,7 +49,7 @@ void clipped_view::set_default_view() set_view_dir(0, 0, -1); set_view_up_dir(0, 1, 0); set_focus(get_scene_extent().get_center()); - set_y_extent_at_focus(1.5*get_scene_extent().get_extent()(1)); + set_y_extent_at_focus(get_scene_extent().get_extent().length()); } /// transform a z value in eye-coordinates (should be negative!) to device coordinate diff --git a/cgv/render/texture.cxx b/cgv/render/texture.cxx index fac803184..bd8eb7ac2 100644 --- a/cgv/render/texture.cxx +++ b/cgv/render/texture.cxx @@ -278,29 +278,42 @@ bool texture::create_from_image(cgv::data::data_format& df, cgv::data::data_view } if (cube_side < 1) destruct(ctx); - unsigned int w = df.get_width(); - unsigned int h = df.get_height(); - if (ensure_power_of_two && ( - !is_power_of_two(w) || - !is_power_of_two(h))) { - unsigned int W = power_of_two_ub(df.get_width()); - unsigned int H = power_of_two_ub(df.get_height()); - float ext[2] = { (float)w/W, (float)h/H }; - data_format df1(df); + unsigned w = df.get_width(), h = df.get_height(); + unsigned W = w, H = h; + data_format df1(df); + if (ensure_power_of_two && (!is_power_of_two(w) || !is_power_of_two(h))) { + W = power_of_two_ub(df.get_width()); + H = power_of_two_ub(df.get_height()); df1.set_width(W); df1.set_height(H); - data_view dv1(&df1); - unsigned int es = df1.get_entry_size(); - unsigned char* dest_ptr = dv1.get_ptr(); - const unsigned char* src_ptr = dv.get_ptr(); - unsigned char* dest_ptr_end = dest_ptr+es*W*H; - std::fill(dest_ptr, dest_ptr_end, clear_color_ptr?*clear_color_ptr:0); - for (unsigned int y=0; y(); + unsigned char* dest_ptr = dv1.get_ptr(); + unsigned char* dest_ptr_end = dest_ptr + entry_size * W*H; + for (unsigned y = 0; y < h; ++y) { + dest_ptr_end -= W * entry_size; + memcpy(dest_ptr_end, src_ptr, w*entry_size); + if (clear_color_ptr) { + for (unsigned x = w; x < W; ++x) + memcpy(dest_ptr_end + x * entry_size, clear_color_ptr, entry_size); + } + else + std::fill(dest_ptr_end + w * entry_size, dest_ptr_end + W*entry_size, 0); + src_ptr += w * entry_size; + } + if (H > h) { + unsigned N = (H - h)*W; + if (clear_color_ptr) { + for (unsigned i = 0; i < N; ++i) + memcpy(dest_ptr + i * entry_size, clear_color_ptr, entry_size); + } + else + std::fill(dest_ptr, dest_ptr + N*entry_size, 0); + } + return create(ctx, dv1, level, cube_side, &palettes); } diff --git a/cgv/render/textured_material.cxx b/cgv/render/textured_material.cxx index a96273e3e..d31f2c500 100644 --- a/cgv/render/textured_material.cxx +++ b/cgv/render/textured_material.cxx @@ -19,6 +19,7 @@ textured_material::textured_material(const media::illum::textured_surface_materi { alpha_test_func = AT_GREATER; alpha_threshold = 0.0f; + ctx_ptr = 0; } /// destruct textures diff --git a/libs/cgv_gl/gl/mesh_render_info.cxx b/libs/cgv_gl/gl/mesh_render_info.cxx index 3af3b261f..c04cd28f4 100644 --- a/libs/cgv_gl/gl/mesh_render_info.cxx +++ b/libs/cgv_gl/gl/mesh_render_info.cxx @@ -135,7 +135,7 @@ namespace cgv { glAlphaFunc(GL_GREATER, 0.1f); } prog.enable(c); - prog.set_uniform(c, "illumination_mode", 2); + //prog.set_uniform(c, "illumination_mode", 2); c.enable_material(mat); aab.enable(c); glDrawElements(GL_TRIANGLES, GLsizei(count), GL_UNSIGNED_INT, (void*)(offset * sizeof(idx_type))); diff --git a/libs/cgv_gl/gl/mesh_render_info.h b/libs/cgv_gl/gl/mesh_render_info.h index 6dcd29846..7ae191995 100644 --- a/libs/cgv_gl/gl/mesh_render_info.h +++ b/libs/cgv_gl/gl/mesh_render_info.h @@ -61,9 +61,8 @@ namespace cgv { vbo.create(ctx, attrib_buffer); finish_construct_base(ctx, sizeof(T), include_tex_coords, include_normals, triangle_element_buffer, edge_element_buffer, cgv::render::element_descriptor_traits::vec3>::get_type_descriptor(mesh.position(0)), - cgv::render::element_descriptor_traits::vec2>::get_type_descriptor(mesh.tex_coord(0)), unique_triples.size(), + cgv::render::element_descriptor_traits::vec2>::get_type_descriptor(typename cgv::media::mesh::simple_mesh::vec2()), unique_triples.size(), color_increment, mesh.get_color_storage_type()); - } /// void render_mesh(cgv::render::context& c); diff --git a/libs/cgv_gl/glsl/surface.glsl b/libs/cgv_gl/glsl/surface.glsl index b6d0b615b..ac5e87f67 100644 --- a/libs/cgv_gl/glsl/surface.glsl +++ b/libs/cgv_gl/glsl/surface.glsl @@ -125,8 +125,12 @@ vec4 compute_reflected_radiance(in Material M, vec3 position_eye, vec3 normal_ey vec4 compute_reflected_appearance(vec3 position_eye, vec3 normal_eye, vec4 color, int side) { - if (illumination_mode == 0) - return color; + if (illumination_mode == 0) { + vec3 col = material.diffuse_reflectance; + float tra = material.transparency; + update_material_color_and_transparency(col, tra, side, color); + return vec4(col, 1.0-tra); + } Material M = material; update_material_color_and_transparency(M.diffuse_reflectance, M.transparency, side, color); update_normal(normal_eye, side); @@ -243,8 +247,12 @@ void update_material_from_texture(inout Material M, in vec2 texcoords) vec4 compute_reflected_appearance_texture(vec3 position_eye, vec3 normal_eye, vec2 texcoords, vec4 color, int side) { - if (illumination_mode == 0) - return color; + if (illumination_mode == 0) { + vec3 col = material.diffuse_reflectance; + float tra = material.transparency; + update_material_color_and_transparency(col, tra, side, color); + return vec4(col, 1.0 - tra); + } Material M = material; update_material_color_and_transparency(M.diffuse_reflectance, M.transparency, side, color); update_material_from_texture(M, texcoords);