Skip to content

Commit 7ebdb83

Browse files
author
monkstone
committed
2 parents 8abd223 + 3cdf56a commit 7ebdb83

File tree

5 files changed

+177
-62
lines changed

5 files changed

+177
-62
lines changed

examples/povmesh/ftest.rb

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
11
require 'toxiclibs'
22

3-
attr_reader :gfx, :mesh0, :mesh1, :mesh2
3+
attr_reader :gfx, :mesh0, :mesh1, :mesh2, :fshape
44

55
def settings
66
size(200, 200, P3D)
7-
smooth 4
7+
smooth 8
88
end
99

1010
def setup
1111
sketch_title('FTest')
12-
@gfx = Gfx::ToxiclibsSupport.new(self)
12+
ArcBall.init(self)
13+
@gfx = Gfx::MeshToVBO.new(self)
1314
# define a rounded cube using the SuperEllipsoid surface function
1415
vert = AABB.fromMinMax(TVec3D.new(-1.0, -3.5, -1.0), TVec3D.new(1.0, 3.5, 1.0))
1516
box = AABB.fromMinMax(TVec3D.new(1.0, -1.5, -1.0), TVec3D.new(3.0, -3.5, 1.0))
1617
box2 = AABB.fromMinMax(TVec3D.new(1.0, 2.0, -1.0), TVec3D.new(3.0, 0.0, 1.0))
1718
@mesh0 = box.to_mesh
1819
@mesh1 = vert.to_mesh
19-
@mesh2 = box2.to_mesh
20+
@mesh2 = box2.to_mesh # build a composite mesh
2021
mesh0.add_mesh(mesh1)
2122
mesh0.add_mesh(mesh2)
2223
mesh0.compute_face_normals
23-
mesh0.compute_vertex_normals
24-
fileID = 'FTest'
25-
pm = Gfx::POVMesh.new(self)
26-
file = java.io.File.new(sketchPath(fileID + '.inc'))
27-
pm.begin_save(file)
28-
pm.set_texture(Gfx::Textures::CHROME)
29-
pm.saveAsPOV(mesh0.faceOutwards, false)
30-
# pm.set_texture(Textures::RED)
31-
# pm.saveAsPOV(mesh1, false)
32-
# pm.set_texture(Textures::WHITE)
33-
# pm.saveAsPOV(mesh2, false)
34-
pm.end_save
35-
# exit
24+
fill(color('#c0c0c0')) # silver
25+
specular(20, 20, 20)
26+
ambient(100)
27+
no_stroke
28+
@fshape = gfx.mesh_to_shape(mesh0, false)
3629
end
3730

3831
def draw
39-
background 50, 50, 200
40-
lights
41-
translate(width / 2, height / 2)
32+
background 80, 80, 160
33+
setup_lights
4234
scale(10)
43-
rotateY(20.radians)
44-
gfx.choose_stroke_fill(false, Toxi::TColor::WHITE, Toxi::TColor::RED)
45-
gfx.mesh(mesh0)
35+
shape(fshape)
36+
end
37+
38+
def setup_lights
39+
lights
40+
ambient_light(150, 150, 150)
41+
directional_light(100, 100, 100, -1, 0, 0)
42+
directional_light(100, 100, 100, 1, 0, -1)
4643
end
44+
45+
46+
def key_pressed
47+
case key
48+
when 'p', 'P'
49+
fileID = 'FTest'
50+
pm = Gfx::POVMesh.new(self)
51+
file = java.io.File.new(sketchPath(fileID + '.inc'))
52+
pm.begin_save(file)
53+
pm.set_texture(Gfx::Textures::CHROME)
54+
pm.saveAsPOV(mesh0.faceOutwards, false)
55+
pm.end_save
56+
when 's', 'S'
57+
save_frame('FTest.png')
58+
end
59+
end

examples/povmesh/mesh_align.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'toxiclibs'
2+
3+
attr_reader :gfx, :vbo, :meshes
4+
SCALE = 200
5+
BOX_SIZE = TVec3D.new(5, 5, 50)
6+
7+
def settings
8+
size(600, 600, P3D)
9+
end
10+
11+
def setup
12+
sketch_title('Mesh Align')
13+
ArcBall.init(self)
14+
@vbo = Gfx::MeshToVBO.new(self)
15+
no_stroke
16+
@meshes = create_shape(GROUP)
17+
600.times do |i|
18+
# create a new direction vector for each box
19+
dir = TVec3D.new(cos(i * TWO_PI / 75), sin(i * TWO_PI / 50), sin(i * TWO_PI / 25)).normalize
20+
# create a position on a sphere, using the direction vector
21+
pos = dir.scale(SCALE)
22+
# create a box mesh at the origin
23+
b = AABB.new(TVec3D.new, BOX_SIZE).to_mesh
24+
# align the Z axis of the box with the direction vector
25+
b.point_towards(dir)
26+
# move the box to the correct position
27+
b.transform(Toxi::Matrix4x4.new.translate_self(pos.x, pos.y, pos.z))
28+
b.compute_face_normals
29+
temp = vbo.mesh_to_shape(b, false)
30+
temp.disable_style
31+
temp.set_fill(color(rand(255), rand(255), rand(255)))
32+
meshes.add_child(temp)
33+
end
34+
end
35+
36+
def draw
37+
background 50, 50, 200
38+
define_lights
39+
shape(meshes)
40+
end
41+
42+
def define_lights
43+
lights
44+
shininess(16)
45+
directionalLight(255, 255, 255, 0, -1, 1)
46+
specular(255)
47+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'toxiclibs'
2+
3+
#######
4+
# After Paul Bourke see http://paulbourke.net/geometry/sphericalh/
5+
# radius =
6+
# sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
7+
# where phi = (0..PI) and theta = (0..TWO_PI)
8+
# As implemented by Karsten Schmidt aka toxi/postspectacular
9+
#######
10+
11+
attr_reader :gfx, :mesh, :spherical, :param
12+
13+
def setup
14+
sketch_title 'Spherical Harmonics Mesh Builder'
15+
ArcBall.init(self)
16+
@param = [8, 4, 1, 5, 1, 4, 0, 0] # default function parameters (m0..m7)
17+
@mesh = spherical_mesh(param)
18+
@gfx = Gfx::MeshToVBO.new(self) # Mesh to vertex buffer object converter
19+
no_stroke
20+
@spherical = gfx.mesh_to_shape(mesh, true) # white
21+
end
22+
23+
def draw
24+
background(0)
25+
lights
26+
shininess(16)
27+
directional_light(255, 255, 255, 0, -1, 1)
28+
specular(255)
29+
shape(spherical)
30+
end
31+
32+
def key_pressed
33+
return unless (key == 'r')
34+
@mesh = spherical_mesh(random_parameters)
35+
no_stroke
36+
@spherical = gfx.mesh_to_colored_shape(mesh, true) # harmonic colors
37+
end
38+
39+
def random_parameters
40+
(0..8).map { rand(0..8) }
41+
end
42+
43+
def spherical_mesh(param)
44+
b = SurfaceMeshBuilder.new(SphericalHarmonics.new(param.to_java(:float)))
45+
b.create_mesh(nil, 80, 60)
46+
end
47+
48+
def settings
49+
size(1024, 576, P3D)
50+
end

lib/toxiclibs/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Toxiclibs
2-
VERSION = '0.5.1'
2+
VERSION = '0.5.1'.freeze
33
end

src/toxi/processing/MeshToVBO.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
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-
*/
211
package toxi.processing;
222

233
import java.util.Collection;
244
import processing.core.PApplet;
255
import processing.core.PConstants;
266
import processing.core.PShape;
7+
import toxi.geom.Matrix4x4;
8+
import toxi.geom.Vec3D;
279
import toxi.geom.mesh.Mesh3D;
2810
import toxi.geom.mesh.Face;
11+
import toxi.geom.mesh.TriangleMesh;
2912

3013
/**
3114
*
@@ -34,6 +17,8 @@
3417
public 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

Comments
 (0)