Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Nov 16, 2015
2 parents 8abd223 + 3cdf56a commit 7ebdb83
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 62 deletions.
59 changes: 36 additions & 23 deletions examples/povmesh/ftest.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
require 'toxiclibs'

attr_reader :gfx, :mesh0, :mesh1, :mesh2
attr_reader :gfx, :mesh0, :mesh1, :mesh2, :fshape

def settings
size(200, 200, P3D)
smooth 4
smooth 8
end

def setup
sketch_title('FTest')
@gfx = Gfx::ToxiclibsSupport.new(self)
ArcBall.init(self)
@gfx = Gfx::MeshToVBO.new(self)
# define a rounded cube using the SuperEllipsoid surface function
vert = AABB.fromMinMax(TVec3D.new(-1.0, -3.5, -1.0), TVec3D.new(1.0, 3.5, 1.0))
box = AABB.fromMinMax(TVec3D.new(1.0, -1.5, -1.0), TVec3D.new(3.0, -3.5, 1.0))
box2 = AABB.fromMinMax(TVec3D.new(1.0, 2.0, -1.0), TVec3D.new(3.0, 0.0, 1.0))
@mesh0 = box.to_mesh
@mesh1 = vert.to_mesh
@mesh2 = box2.to_mesh
@mesh2 = box2.to_mesh # build a composite mesh
mesh0.add_mesh(mesh1)
mesh0.add_mesh(mesh2)
mesh0.compute_face_normals
mesh0.compute_vertex_normals
fileID = 'FTest'
pm = Gfx::POVMesh.new(self)
file = java.io.File.new(sketchPath(fileID + '.inc'))
pm.begin_save(file)
pm.set_texture(Gfx::Textures::CHROME)
pm.saveAsPOV(mesh0.faceOutwards, false)
# pm.set_texture(Textures::RED)
# pm.saveAsPOV(mesh1, false)
# pm.set_texture(Textures::WHITE)
# pm.saveAsPOV(mesh2, false)
pm.end_save
# exit
fill(color('#c0c0c0')) # silver
specular(20, 20, 20)
ambient(100)
no_stroke
@fshape = gfx.mesh_to_shape(mesh0, false)
end

def draw
background 50, 50, 200
lights
translate(width / 2, height / 2)
background 80, 80, 160
setup_lights
scale(10)
rotateY(20.radians)
gfx.choose_stroke_fill(false, Toxi::TColor::WHITE, Toxi::TColor::RED)
gfx.mesh(mesh0)
shape(fshape)
end

def setup_lights
lights
ambient_light(150, 150, 150)
directional_light(100, 100, 100, -1, 0, 0)
directional_light(100, 100, 100, 1, 0, -1)
end


def key_pressed
case key
when 'p', 'P'
fileID = 'FTest'
pm = Gfx::POVMesh.new(self)
file = java.io.File.new(sketchPath(fileID + '.inc'))
pm.begin_save(file)
pm.set_texture(Gfx::Textures::CHROME)
pm.saveAsPOV(mesh0.faceOutwards, false)
pm.end_save
when 's', 'S'
save_frame('FTest.png')
end
end
47 changes: 47 additions & 0 deletions examples/povmesh/mesh_align.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'toxiclibs'

attr_reader :gfx, :vbo, :meshes
SCALE = 200
BOX_SIZE = TVec3D.new(5, 5, 50)

def settings
size(600, 600, P3D)
end

def setup
sketch_title('Mesh Align')
ArcBall.init(self)
@vbo = Gfx::MeshToVBO.new(self)
no_stroke
@meshes = create_shape(GROUP)
600.times do |i|
# create a new direction vector for each box
dir = TVec3D.new(cos(i * TWO_PI / 75), sin(i * TWO_PI / 50), sin(i * TWO_PI / 25)).normalize
# create a position on a sphere, using the direction vector
pos = dir.scale(SCALE)
# create a box mesh at the origin
b = AABB.new(TVec3D.new, BOX_SIZE).to_mesh
# align the Z axis of the box with the direction vector
b.point_towards(dir)
# move the box to the correct position
b.transform(Toxi::Matrix4x4.new.translate_self(pos.x, pos.y, pos.z))
b.compute_face_normals
temp = vbo.mesh_to_shape(b, false)
temp.disable_style
temp.set_fill(color(rand(255), rand(255), rand(255)))
meshes.add_child(temp)
end
end

def draw
background 50, 50, 200
define_lights
shape(meshes)
end

def define_lights
lights
shininess(16)
directionalLight(255, 255, 255, 0, -1, 1)
specular(255)
end
50 changes: 50 additions & 0 deletions examples/spherical_harmonics_mesh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'toxiclibs'

#######
# After Paul Bourke see http://paulbourke.net/geometry/sphericalh/
# radius =
# sin(m0*phi)**m1 + cos(m2*phi)**m3 + sin(m4*theta)**m5 + cos(m6*theta)**m7
# where phi = (0..PI) and theta = (0..TWO_PI)
# As implemented by Karsten Schmidt aka toxi/postspectacular
#######

attr_reader :gfx, :mesh, :spherical, :param

def setup
sketch_title 'Spherical Harmonics Mesh Builder'
ArcBall.init(self)
@param = [8, 4, 1, 5, 1, 4, 0, 0] # default function parameters (m0..m7)
@mesh = spherical_mesh(param)
@gfx = Gfx::MeshToVBO.new(self) # Mesh to vertex buffer object converter
no_stroke
@spherical = gfx.mesh_to_shape(mesh, true) # white
end

def draw
background(0)
lights
shininess(16)
directional_light(255, 255, 255, 0, -1, 1)
specular(255)
shape(spherical)
end

def key_pressed
return unless (key == 'r')
@mesh = spherical_mesh(random_parameters)
no_stroke
@spherical = gfx.mesh_to_colored_shape(mesh, true) # harmonic colors
end

def random_parameters
(0..8).map { rand(0..8) }
end

def spherical_mesh(param)
b = SurfaceMeshBuilder.new(SphericalHarmonics.new(param.to_java(:float)))
b.create_mesh(nil, 80, 60)
end

def settings
size(1024, 576, P3D)
end
2 changes: 1 addition & 1 deletion lib/toxiclibs/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Toxiclibs
VERSION = '0.5.1'
VERSION = '0.5.1'.freeze
end
81 changes: 43 additions & 38 deletions src/toxi/processing/MeshToVBO.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
/*
* This library adds translate from toxiclibs Mesh to processing PShape (vbo)
* Copyright (c) 2015 Martin Prout
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
package toxi.processing;

import java.util.Collection;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PShape;
import toxi.geom.Matrix4x4;
import toxi.geom.Vec3D;
import toxi.geom.mesh.Mesh3D;
import toxi.geom.mesh.Face;
import toxi.geom.mesh.TriangleMesh;

/**
*
Expand All @@ -34,6 +17,8 @@
public class MeshToVBO {

private final PApplet app;
private final Matrix4x4 normalMap =
new Matrix4x4().translateSelf(128, 128, 128).scaleSelf(127);

/**
*
Expand All @@ -49,39 +34,24 @@ public MeshToVBO(PApplet app) {
* @param smooth boolean
* @return
*/
public PShape meshToShape(Mesh3D mesh, boolean smooth) {
public PShape meshToShape(TriangleMesh mesh, boolean smooth) {
PShape retained = app.createShape();
retained.beginShape(PConstants.TRIANGLE);
if (smooth) {
mesh.computeVertexNormals();
Collection<Face> faces = mesh.getFaces();
faces.stream().map((f) -> {
mesh.faces.stream().map((f) -> {
retained.normal(f.a.normal.x, f.a.normal.y, f.a.normal.z);
return f;
}).map((f) -> {
retained.vertex(f.a.x, f.a.y, f.a.z);
return f;
}).map((f) -> {
retained.normal(f.b.normal.x, f.b.normal.y, f.b.normal.z);
return f;
}).map((f) -> {
retained.vertex(f.b.x, f.b.y, f.b.z);
return f;
}).map((f) -> {
retained.normal(f.c.normal.x, f.c.normal.y, f.c.normal.z);
return f;
}).forEach((f) -> {
retained.vertex(f.c.x, f.c.y, f.c.z);
});
} else {
Collection<Face> faces = mesh.getFaces();
faces.stream().map((f) -> {
mesh.faces.stream().map((f) -> {
retained.normal(f.normal.x, f.normal.y, f.normal.z);
return f;
}).map((f) -> {
retained.vertex(f.a.x, f.a.y, f.a.z);
return f;
}).map((f) -> {
retained.vertex(f.b.x, f.b.y, f.b.z);
return f;
}).forEach((f) -> {
Expand All @@ -91,4 +61,39 @@ public PShape meshToShape(Mesh3D mesh, boolean smooth) {
retained.endShape();
return retained;
}

public PShape meshToColoredShape(TriangleMesh mesh,
boolean vertexNormals) {
PShape gfx = app.createShape();
gfx.beginShape(PConstants.TRIANGLES);
if (vertexNormals) {
mesh.faces.stream().map((f) -> {
Vec3D n = normalMap.applyTo(f.a.normal);
gfx.fill(n.x, n.y, n.z);
gfx.normal(f.a.normal.x, f.a.normal.y, f.a.normal.z);
gfx.vertex(f.a.x, f.a.y, f.a.z);
n = normalMap.applyTo(f.b.normal);
gfx.fill(n.x, n.y, n.z);
gfx.normal(f.b.normal.x, f.b.normal.y, f.b.normal.z);
gfx.vertex(f.b.x, f.b.y, f.b.z);
n = normalMap.applyTo(f.c.normal);
gfx.fill(n.x, n.y, n.z);
gfx.normal(f.c.normal.x, f.c.normal.y, f.c.normal.z);
return f;
}).forEach((f) -> {
gfx.vertex(f.c.x, f.c.y, f.c.z);
});
} else {
mesh.faces.stream().map((f) -> {
gfx.normal(f.normal.x, f.normal.y, f.normal.z);
gfx.vertex(f.a.x, f.a.y, f.a.z);
gfx.vertex(f.b.x, f.b.y, f.b.z);
return f;
}).forEach((f) -> {
gfx.vertex(f.c.x, f.c.y, f.c.z);
});
}
gfx.endShape();
return gfx;
}
}

0 comments on commit 7ebdb83

Please sign in to comment.