Skip to content

Commit 04d9825

Browse files
committed
Geometry Grid Pixel Flow
1 parent edf25a4 commit 04d9825

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com
2+
#
3+
# A Processing/Java library for high performance GPU-Computing (GLSL).
4+
# MIT License: https://opensource.org/licenses/MIT
5+
#
6+
load_libraries :PixelFlow, :peasycam
7+
8+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwHalfEdge'
9+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwIFSGrid'
10+
java_import 'com.thomasdiewald.pixelflow.java.geometry.DwIndexedFaceSetAble'
11+
java_import 'peasy.PeasyCam'
12+
# A demo to create a Subdivision Cube, and either render it by as usual,
13+
# or convert it to a HalfEdge representation and use that for rendering and
14+
# more complex mesh operations/iterations.
15+
#
16+
# Controls:
17+
# key '1'-'7' ... subdivisions
18+
# key 's ... toggle stroke display
19+
20+
attr_reader :cam, :grid, :mesh, :shp_gizmo, :display_stroke
21+
RADIUS = 200
22+
23+
def settings
24+
size(800, 800, P3D)
25+
smooth(8)
26+
end
27+
28+
def setup
29+
@cam = PeasyCam.new(self, 1000)
30+
create_mesh(4)
31+
@display_stroke = true
32+
@shp_gizmo = nil
33+
end
34+
35+
def create_mesh(subdivisions)
36+
@grid = DwIFSGrid.new(1<<subdivisions, 2<<subdivisions)
37+
@mesh = DwHalfEdge::Mesh.new(grid)
38+
end
39+
40+
def draw
41+
lights
42+
directionalLight(128, 96, 64, -500, -500, 1_000)
43+
background(64)
44+
display_gizmo(300)
45+
46+
scale(RADIUS)
47+
if(display_stroke)
48+
stroke_weight(0.5 / RADIUS)
49+
stroke(0)
50+
else
51+
no_stroke
52+
end
53+
fill(255)
54+
# display the IFS-mesh (Indexed Face set)
55+
pushMatrix
56+
translate(-1.5, 0)
57+
display_mesh(grid)
58+
popMatrix
59+
# display the HalfEdge mesh
60+
pushMatrix
61+
translate(1.5, 0)
62+
mesh.display(g)
63+
popMatrix
64+
# info
65+
num_faces = mesh.ifs.getFacesCount
66+
num_verts = mesh.ifs.getVertsCount
67+
num_edges = mesh.edges.length
68+
#String txt_fps = String.format(getClass().getName()+ " [Verts %d] [Faces %d] [HalfEdges %d] [fps %6.2f]", num_verts, num_faces, num_edges, frameRate)
69+
#surface.setTitle(txt_fps)
70+
format_string = 'Geometry Grid [Verts %d] [Faces %d] [Half Edges %d] [fps: (%6.2f)]'
71+
surface.set_title(format(format_string, num_verts, num_faces, num_edges, frame_rate))
72+
end
73+
74+
# this method can of course be optimized if we know in advance the number of
75+
# vertices per face
76+
def display_mesh(ifs)
77+
faces_count = ifs.getFacesCount
78+
faces = ifs.getFaces
79+
verts = ifs.getVerts
80+
v = Array.new(3)
81+
faces.each do |face|
82+
case(face.length)
83+
when 3
84+
begin_shape(TRIANGLE)
85+
v = verts[face[2]]
86+
vertex(v[0], v[1], v[2])
87+
v = verts[face[1]]
88+
vertex(v[0], v[1], v[2])
89+
v = verts[face[0]]
90+
vertex(v[0], v[1], v[2])
91+
end_shape
92+
when 4
93+
begin_shape(QUAD)
94+
v = verts[face[3]]
95+
vertex(v[0], v[1], v[2])
96+
v = verts[face[2]]
97+
vertex(v[0], v[1], v[2])
98+
v = verts[face[1]]
99+
vertex(v[0], v[1], v[2])
100+
v = verts[face[0]]
101+
vertex(v[0], v[1], v[2])
102+
end_shape
103+
else
104+
begin_shape # POLYGON
105+
face.each do |coord|
106+
v = verts[face[coord]]
107+
vertex(v[0], v[1], v[2])
108+
end
109+
end_shape(CLOSE)
110+
end
111+
end
112+
end
113+
114+
def display_gizmo(s)
115+
if shp_gizmo.nil?
116+
strokeWeight(1)
117+
@shp_gizmo = create_shape
118+
shp_gizmo.begin_shape(LINES)
119+
shp_gizmo.stroke(255,0,0)
120+
shp_gizmo.vertex(0,0,0)
121+
shp_gizmo.vertex(s,0,0)
122+
shp_gizmo.stroke(0,255,0)
123+
shp_gizmo.vertex(0,0,0)
124+
shp_gizmo.vertex(0,s,0)
125+
shp_gizmo.stroke(0,0,255)
126+
shp_gizmo.vertex(0,0,0)
127+
shp_gizmo.vertex(0,0,s)
128+
shp_gizmo.end_shape
129+
end
130+
shape(shp_gizmo)
131+
end
132+
133+
def key_released
134+
case key
135+
when '1'..'7'
136+
create_mesh(key.to_i)
137+
when 's', 'S'
138+
@display_stroke = !display_stroke
139+
end
140+
end

0 commit comments

Comments
 (0)