We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi !
I'm trying to make an icoSphere using the beginShape()
function.
I've managed to draw a sphere made of particles but don't know how to connect those particles using the TRIANGLE_FAN
parameter.
More specifically, there are 2 key parts that I can't figure out (from line 57):
vertex()
functions do I need to write within the double for
loop ?Any help would be much appreciated.
add_library('peasycam')
add_library('toxiclibscore')
add_library('verletphysics')
from toxi.physics import VerletPhysics ### using VerletPhysics engine
from particle import Particle ### simple class containing a particle object
def setup():
global particules, physics, total
size(1080, 800, P3D)
smooth(8)
cam = PeasyCam(this, 110)
physics = VerletPhysics()
r = 180 ### radius of the sphere
total = 10 ### number of particles/points
particules = [[[] for e in range(total +1)] for f in range(total+1)] ### 2D array list storing the particles locations
for e in range(0, total+1):
lon = map(e, 0, total, 0, PI)
for f in range(0, total+1):
lat = map(f, 0, total , 0, TWO_PI)
x = r * sin(lat) * cos(lon)
y = r * sin(lat) * sin (lon)
z = r * cos(lat)
particules[e][f] = Particle(x, y, z)
for e in range(total+1):
for f in range(total+1):
p = particules[e][f]
physics.addParticle(p) ### adding the particles to the physics engine
def draw():
global particules, physics
background(0)
for e in range(total+1): ### displaying the particles
for f in range(total+1):
p = particules[e][f]
p.display()
for e in range(total):
beginShape(TRIANGLE_FAN) ### Trying to connect the particles with triangle fans
for f in range(total+1):
noFill()
strokeWeight(1)
p = particules[e][f-1] ### PART TO CORRECT #### vertices and their coordinates ####
vertex(p.x(), p.y(), p.z())
p2 = particules[e+1][f]
vertex(p2.x(), p2.y(), p2.z())
endShape()
Answers
The code above gives me this shape:
https://imgur.com/IaV2d7A
As you can see the triangle fans and not completed and most of the points are connected to one pole.
Any idea of what I should change in the following snippet ?
Please share your ideas, suggestions, code (even in Java)... anything !
I tried and I failed... Shouldn't you be using TRIANGLE_STRIPS btw?
Check this post: https://www.openprocessing.org/sketch/92464
My other lame suggestion: https://www.google.ca/search?source=hp&ei=wxyWWsmdMMScjwO16Y2ABA&q=icosphere+using+triangles&oq=icosphere+using+triangles&gs_l=psy-ab.3...1416.7710.0.7920.26.25.0.0.0.0.142.2201.21j4.25.0....0...1c.1.64.psy-ab..1.17.1546.0..0j35i39k1j0i131k1j0i10k1j0i22i30k1j33i160k1j33i21k1.0.JLiDaHcOEWE
My attempt below but it has a bug.
Kf
You didn't need to start a new Question for this.
You don't use all triangle fans, just the top and bottom rows where you have a central point at the pole. The rest is triangle strips.
The sphere code in github does exactly this.
@kfrajer: Hey that's super nice of you to share your attempt ! I did use TRIANGLE_STRIPS at first but koogs suggested me to use TRIANGLE_FANS instead. I've now managed to draw a sphere made of TRIANGLE_STRIPS and where the top and bottom only are made of TRIANGLE_FANS.
And thanks for the links, the icosahedron sketch is really interesting. I'm now wondering what are the difference between an icosphere and an icosahedron. Anyway I'll probably use a part of the code for my next attempt.
@koogs: As I just mentionned, I could draw a sphere made of triangle strips, where tops and bottoms only are triangle fans. HOWEVER (and this has more to do with my other question about springs) when I connect the vertices by springs the whole sphere falls apart. This is partly because I've now different meshes:
I guess it'd be a better idea to draw a sphere based on a single mesh. Is that possible ? (with an icosahedron for instance ?)
Also what "sphere code" are you refering to ? Would you mind sharing a link ?
PS: This is what the same sphere looks like when I add springs to each vertices and tie the different meshes together:
processing sphere code. it's complicated but you can see from the comments it's three bits, the top, the middle and the bottom.
https://github.com/processing/processing/blob/master/core/src/processing/opengl/PGraphicsOpenGL.java#L8961
Do you think it's possible to draw a sphere with a single mesh ? (one single part, no top, middle or bottom)
Just brainstorming -- you might try basing your mesh algorithm on the points of a Fibonacci spiral. That walks a sphere from top to bottom as a spiral.
https://forum.processing.org/two/discussion/18779/move-a-single-point
@jeremydouglass: That's a very interesting suggestion. I'll definitely look into it after I'm finished with my current polyhedron experiment. Thanks a lot !
I just wanted to share a much less cumbersome approach based on @jeremydouglass last suggestion. It's a 2 step process:
triangulating those points with the Hemesh library