Skip to content

Commit

Permalink
removed several mesh and textured material problems
Browse files Browse the repository at this point in the history
  • Loading branch information
sgumhold committed Mar 22, 2019
1 parent 83ed589 commit a63ada1
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 30 deletions.
33 changes: 33 additions & 0 deletions cgv/media/mesh/simple_mesh.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,39 @@ typename simple_mesh<T>::box_type simple_mesh<T>::compute_box() const
box.add_point(p);
return box;
}
/// compute vertex normals by averaging triangle normals
template <typename T>
void simple_mesh<T>::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 <typename T>
Expand Down
3 changes: 2 additions & 1 deletion cgv/media/mesh/simple_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cgv/render/clipped_view.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 34 additions & 21 deletions cgv/render/texture.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>();
const unsigned char* src_ptr = dv.get_ptr<unsigned char>();
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<h; ++y, dest_ptr += es*W, src_ptr += es*w)
memcpy(dest_ptr, src_ptr, es*w);
return create(ctx, dv1, level, cube_side, &palettes);
}
else
return create(ctx, dv, level, cube_side, &palettes);
}
//float ext[2] = { (float)w/W, (float)h/H };
data_view dv1(&df1);
unsigned entry_size = df.get_entry_size();
const unsigned char* src_ptr = dv.get_ptr<unsigned char>();
unsigned char* dest_ptr = dv1.get_ptr<unsigned char>();
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);
}


Expand Down
1 change: 1 addition & 0 deletions cgv/render/textured_material.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/cgv_gl/gl/mesh_render_info.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
3 changes: 1 addition & 2 deletions libs/cgv_gl/gl/mesh_render_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename cgv::media::mesh::simple_mesh<T>::vec3>::get_type_descriptor(mesh.position(0)),
cgv::render::element_descriptor_traits<typename cgv::media::mesh::simple_mesh<T>::vec2>::get_type_descriptor(mesh.tex_coord(0)), unique_triples.size(),
cgv::render::element_descriptor_traits<typename cgv::media::mesh::simple_mesh<T>::vec2>::get_type_descriptor(typename cgv::media::mesh::simple_mesh<T>::vec2()), unique_triples.size(),
color_increment, mesh.get_color_storage_type());

}
///
void render_mesh(cgv::render::context& c);
Expand Down
16 changes: 12 additions & 4 deletions libs/cgv_gl/glsl/surface.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a63ada1

Please sign in to comment.