I would like to draw a 3D Superformula mesh but not sure how I should organize the faces(be them triangles or quads).
I've installed octave and tried the sample code. I have no clue how Gnuplot's mesh() function works, but I imagine I would need something similar.
The Wikipedia entry has a link to a Processing demo. I had a look at the source and noticed it only draws points. I tried to wrap that segment of code within beginShape()/endShape() calls but work the way I hoped.
I also tried to check if the number of points is divisible by 3 or 4 and used TRIANGLES or QUADS, but this is not the right way to do this, as you can see below:
How can I draw a SuperShape3D using triangles/quads ? I imagine the vertices are in the right positions, but they need to be sorted to calls that would draw the faces using the vertex indices.
I'm not fixed to a particular language at the moment, but my goal would be to have the vertices in an array, then push faces(3 or 4 points) using vertex indices.
Any hints ?
Update:
Here is the function used to get the points in the Processing sample code:
import toxi.geom.*;
import controlP5.*;
ControlP5 controlP5;
ArrayList points = new ArrayList();
ArrayList faces = new ArrayList();
float a1=1,a2=1,b=1,xx,step = 0.05,yy,zz,n1=4,n2=12,n3=15,n4=15,r,raux1,r1,raux2,r2;
int N_X = int(2*PI/step);
int N_Y = int(PI/step);
void setup() {
size(800,800,P3D);
//hint(ENABLE_DEPTH_SORT);
controlP5 = new ControlP5(this);
controlP5.addSlider("a1value",0,3,1,20,0,200,10);
controlP5.addSlider("a2value",0,3,1,20,20,200,10);
controlP5.addSlider("bvalue",0,3,1,20,40,200,10);
controlP5.addSlider("n1value",0,20,8,20,60,200,10);
controlP5.addSlider("n2value",0,5,0.5,20,80,200,10);
controlP5.addSlider("n3value",0,5,0.5,20,100,200,10);
controlP5.addSlider("n4value",0,20,8,20,120,200,10);
controlP5.addSlider("stepvalue",0.02,0.9,0.05,20,140,200,10);
controlP5.setAutoDraw(false);
draw_super_formula();
}
void draw() {
background(0);
fill(255);
controlP5.draw();
lights();
translate(width / 2, height / 2, 0);
rotateX(mouseY * 0.01f);
rotateY(mouseX * 0.01f);
// connect 4 points into quads:
Vec3D pt;
for(int x=0;x<N_X-1;x++)
{
for(int y=0;y<N_Y-1;y++)
{
beginShape(QUADS);
pt = (Vec3D)points.get( x*N_Y + y );
vertex(pt.x,pt.y,pt.z);
pt = (Vec3D)points.get( x*N_Y + y+1 );
vertex(pt.x,pt.y,pt.z);
pt = (Vec3D)points.get( (x+1)*N_Y + y+1 );
vertex(pt.x,pt.y,pt.z);
pt = (Vec3D)points.get( (x+1)*N_Y + y);
vertex(pt.x,pt.y,pt.z);
endShape();
}
}
}
void vertex(Vec3D v) {
vertex(v.x,v.y,v.z);
}
void draw_super_formula() {
for(int i = points.size()-1; i>0;i--){
points.remove(i);
}
for(int x=0;x<N_X;x++)
{
float i = -PI + x*step;
for(int y=0;y<N_Y;y++)
{
float j = -PI/2.0 + y*step;
raux1=pow(abs(1/a1*abs(cos(n1*i/4))),n3)+pow(abs(1/a2*abs(sin(n1*i/4))),n4);
r1=pow(abs(raux1),(-1/n2));
raux2=pow(abs(1/a1*abs(cos(n1*j/4))),n3)+pow(abs(1/a2*abs(sin(n1*j/4))),n4);
r2=pow(abs(raux2),(-1/n2));
xx=r1*cos(i)*r2*cos(j)*100;
yy=r1*sin(i)*r2*cos(j)*100;
zz=r2*sin(j)*100;
Vec3D test1 = new Vec3D(xx,yy,zz);
points.add(test1);
}
}
}
void bvalue(float new_value){
b = new_value;
draw_super_formula();
}
void a1value(float new_value){
a1 = new_value;
draw_super_formula();
}
void a2value(float new_value){
a2 = new_value;
draw_super_formula();
}
void n1value(float new_value){
n1 = new_value;
draw_super_formula();
}
void n2value(float new_value){
n2 = new_value;
draw_super_formula();
}
void n3value(float new_value){
n3 = new_value;
draw_super_formula();
}
void n4value(float new_value){
n4 = new_value;
draw_super_formula();
}
void stepvalue(float new_value){
step = new_value;
draw_super_formula();
println("% 3: "+(points.size()%3));
println("% 4: "+(points.size()%4));
}
class F4{
int a,b,c,d;
F4(int a,int b,int c,int d){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
@tim_hutton's solution is great, but it looks an index off, trying to figure out where that is.