1- /*
2- * This library adds translate from toxiclibs Mesh to processing PShape (vbo)
3- * Copyright (c) 2015 Martin Prout
4- *
5- * This library is free software; you can redistribute it and/or modify it under
6- * the terms of the GNU Lesser General Public License as published by the Free
7- * Software Foundation; either version 2.1 of the License, or (at your option)
8- * any later version.
9- *
10- * http://creativecommons.org/licenses/LGPL/2.1/
11- *
12- * This library is distributed in the hope that it will be useful, but WITHOUT
13- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14- * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15- * details.
16- *
17- * You should have received a copy of the GNU Lesser General Public License
18- * along with this library; if not, write to the Free Software Foundation, Inc.,
19- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20- */
211package toxi .processing ;
222
233import java .util .Collection ;
244import processing .core .PApplet ;
255import processing .core .PConstants ;
266import processing .core .PShape ;
7+ import toxi .geom .Matrix4x4 ;
8+ import toxi .geom .Vec3D ;
279import toxi .geom .mesh .Mesh3D ;
2810import toxi .geom .mesh .Face ;
11+ import toxi .geom .mesh .TriangleMesh ;
2912
3013/**
3114 *
3417public class MeshToVBO {
3518
3619 private final PApplet app ;
20+ private final Matrix4x4 normalMap =
21+ new Matrix4x4 ().translateSelf (128 , 128 , 128 ).scaleSelf (127 );
3722
3823 /**
3924 *
@@ -49,39 +34,24 @@ public MeshToVBO(PApplet app) {
4934 * @param smooth boolean
5035 * @return
5136 */
52- public PShape meshToShape (Mesh3D mesh , boolean smooth ) {
37+ public PShape meshToShape (TriangleMesh mesh , boolean smooth ) {
5338 PShape retained = app .createShape ();
5439 retained .beginShape (PConstants .TRIANGLE );
5540 if (smooth ) {
56- mesh .computeVertexNormals ();
57- Collection <Face > faces = mesh .getFaces ();
58- faces .stream ().map ((f ) -> {
41+ mesh .faces .stream ().map ((f ) -> {
5942 retained .normal (f .a .normal .x , f .a .normal .y , f .a .normal .z );
60- return f ;
61- }).map ((f ) -> {
6243 retained .vertex (f .a .x , f .a .y , f .a .z );
63- return f ;
64- }).map ((f ) -> {
6544 retained .normal (f .b .normal .x , f .b .normal .y , f .b .normal .z );
66- return f ;
67- }).map ((f ) -> {
6845 retained .vertex (f .b .x , f .b .y , f .b .z );
69- return f ;
70- }).map ((f ) -> {
7146 retained .normal (f .c .normal .x , f .c .normal .y , f .c .normal .z );
7247 return f ;
7348 }).forEach ((f ) -> {
7449 retained .vertex (f .c .x , f .c .y , f .c .z );
7550 });
7651 } else {
77- Collection <Face > faces = mesh .getFaces ();
78- faces .stream ().map ((f ) -> {
52+ mesh .faces .stream ().map ((f ) -> {
7953 retained .normal (f .normal .x , f .normal .y , f .normal .z );
80- return f ;
81- }).map ((f ) -> {
8254 retained .vertex (f .a .x , f .a .y , f .a .z );
83- return f ;
84- }).map ((f ) -> {
8555 retained .vertex (f .b .x , f .b .y , f .b .z );
8656 return f ;
8757 }).forEach ((f ) -> {
@@ -91,4 +61,39 @@ public PShape meshToShape(Mesh3D mesh, boolean smooth) {
9161 retained .endShape ();
9262 return retained ;
9363 }
64+
65+ public PShape meshToColoredShape (TriangleMesh mesh ,
66+ boolean vertexNormals ) {
67+ PShape gfx = app .createShape ();
68+ gfx .beginShape (PConstants .TRIANGLES );
69+ if (vertexNormals ) {
70+ mesh .faces .stream ().map ((f ) -> {
71+ Vec3D n = normalMap .applyTo (f .a .normal );
72+ gfx .fill (n .x , n .y , n .z );
73+ gfx .normal (f .a .normal .x , f .a .normal .y , f .a .normal .z );
74+ gfx .vertex (f .a .x , f .a .y , f .a .z );
75+ n = normalMap .applyTo (f .b .normal );
76+ gfx .fill (n .x , n .y , n .z );
77+ gfx .normal (f .b .normal .x , f .b .normal .y , f .b .normal .z );
78+ gfx .vertex (f .b .x , f .b .y , f .b .z );
79+ n = normalMap .applyTo (f .c .normal );
80+ gfx .fill (n .x , n .y , n .z );
81+ gfx .normal (f .c .normal .x , f .c .normal .y , f .c .normal .z );
82+ return f ;
83+ }).forEach ((f ) -> {
84+ gfx .vertex (f .c .x , f .c .y , f .c .z );
85+ });
86+ } else {
87+ mesh .faces .stream ().map ((f ) -> {
88+ gfx .normal (f .normal .x , f .normal .y , f .normal .z );
89+ gfx .vertex (f .a .x , f .a .y , f .a .z );
90+ gfx .vertex (f .b .x , f .b .y , f .b .z );
91+ return f ;
92+ }).forEach ((f ) -> {
93+ gfx .vertex (f .c .x , f .c .y , f .c .z );
94+ });
95+ }
96+ gfx .endShape ();
97+ return gfx ;
98+ }
9499}
0 commit comments