Use precomputed normals in 3D preview

This commit is contained in:
Alessandro Ranellucci 2013-09-11 20:15:42 +02:00
parent d4512a12df
commit bc48e17dff
3 changed files with 26 additions and 4 deletions

View File

@ -49,14 +49,14 @@ sub new {
color => COLORS->[ $color_idx % scalar(@{&COLORS}) ], color => COLORS->[ $color_idx % scalar(@{&COLORS}) ],
}; };
my ($vertices, $facets) = ($mesh->vertices, $mesh->facets);
{ {
my @verts = map @{ $vertices->[$_] }, map @$_, @$facets; my $vertices = $mesh->vertices;
my @verts = map @{ $vertices->[$_] }, map @$_, @{$mesh->facets};
$v->{verts} = OpenGL::Array->new_list(GL_FLOAT, @verts); $v->{verts} = OpenGL::Array->new_list(GL_FLOAT, @verts);
} }
{ {
my @norms = map { @$_, @$_, @$_ } map normalize(triangle_normal(map $vertices->[$_], @$_)), @$facets; my @norms = map @$_, @{$mesh->normals};
$v->{norms} = OpenGL::Array->new_list(GL_FLOAT, @norms); $v->{norms} = OpenGL::Array->new_list(GL_FLOAT, @norms);
} }
} }

View File

@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 50; use Test::More tests => 51;
is Slic3r::TriangleMesh::hello_world(), 'Hello world!', is Slic3r::TriangleMesh::hello_world(), 'Hello world!',
'hello world'; 'hello world';
@ -22,6 +22,7 @@ my $cube = {
is_deeply $vertices, $cube->{vertices}, 'vertices arrayref roundtrip'; is_deeply $vertices, $cube->{vertices}, 'vertices arrayref roundtrip';
is_deeply $facets, $cube->{facets}, 'facets arrayref roundtrip'; is_deeply $facets, $cube->{facets}, 'facets arrayref roundtrip';
is scalar(@{$m->normals}), scalar(@$facets), 'normals returns the right number of items';
{ {
my $m2 = $m->clone; my $m2 = $m->clone;

View File

@ -92,6 +92,27 @@ TriangleMesh::facets()
OUTPUT: OUTPUT:
RETVAL RETVAL
SV*
TriangleMesh::normals()
CODE:
if (!THIS->repaired) CONFESS("normals() requires repair()");
// normals
AV* normals = newAV();
av_extend(normals, THIS->stl.stats.number_of_facets);
for (int i = 0; i < THIS->stl.stats.number_of_facets; i++) {
AV* facet = newAV();
av_store(normals, i, newRV_noinc((SV*)facet));
av_extend(facet, 2);
av_store(facet, 0, newSVnv(THIS->stl.facet_start[i].normal.x));
av_store(facet, 1, newSVnv(THIS->stl.facet_start[i].normal.y));
av_store(facet, 2, newSVnv(THIS->stl.facet_start[i].normal.z));
}
RETVAL = newRV_noinc((SV*)normals);
OUTPUT:
RETVAL
SV* SV*
TriangleMesh::size() TriangleMesh::size()
CODE: CODE: