We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › drawing a curved surface
Page Index Toggle Pages: 1
drawing a curved surface (Read 2670 times)
drawing a curved surface
Nov 25th, 2009, 2:44pm
 
Hi,

Trying to figure this out - basically I want to draw a tyre, so a torus/ donut shape, but the catch is drawing only the outer half.

Another way of putting it is that I want to lathe/ rotate an open semicircle or an arc around a point to create a mesh.

The end result I need is using something like quad_strip, that is curved around the X/Y axis 180 degrees and then also curved in the Z/X axis point 60 degrees.

I can curve the mesh I make in either axis but I can't seem to figure out curving in both. I can of course make a partial cylinder and I did look at a few torus and hemisphere tutorials but nothing seemed to really help me out.

Any help/ pointers appreciated.

Cheers,

Larry

Re: drawing a curved surface
Reply #1 - Nov 25th, 2009, 3:04pm
 
sorry another simpler way of saying this is that I want to lathe/rotate an arc around another arc.
Re: drawing a curved surface
Reply #2 - Nov 25th, 2009, 9:52pm
 
maybe this helps :

// MeshFrom2DPath.pde
// Marius Watz - http://workshop.evolutionzone.com
//
// Creates a 3D polygon mesh by rotating a 2D path.

import processing.opengl.*;

int numrot;
float px[],py[],mesh[][][];

void setup() {
 size(700,700, OPENGL);

 // To create a mesh, first create two arrays to hold the
 // X and Y coordinates of the path you will use to create it
 px=new float[20];
 py=new float[20];
 float t=0;
 for(int i=0; i< px.length; i++) {
   px[i]=bezierPoint(0,130, 130, 0, t);
   py[i]=bezierPoint(450,350,150,50, t);
   t+=(1.0/(float)(px.length-1));
 }

 // use createMesh to create the mesh. createMesh takes three parameters:
 // num == number of rotations
 // startDeg == start degree of rotation
 // endDeg == end degree of rotation

 mesh=createMesh(px,py,20, -60,60);
}

void draw() {

translate(width/2,height/2);
 pushMatrix();

 background(0);
 lights();

 rotateY(radians(frameCount));
 rotateX(radians(frameCount)/2);

 stroke(255);
 noStroke();
 fill(255,120,0);

 translate(0,0);
 for(int i=0; i< 6; i++) {
   rotateZ(radians(60));
   pushMatrix();
   rotateX(radians(-60));

   // to draw the mesh, use drawMesh and pass your mesh variable to it
   drawMesh(mesh);
   popMatrix();
 }

 popMatrix();
}

float [][][] createMesh(float px[],float py[],int numrot,
 float startDeg,float endDeg) {

 float deg,x,z;
 double cosval,sinval,tmp1,tmp2;

 float [][][] mesh=new float[numrot][px.length][3];
 endDeg-=startDeg;

 int meshindex=0;
 for(int i=0; i< numrot; i++) {
   deg=radians(startDeg+(endDeg/(float)(numrot-1))*(float)i);
   for(int j=0; j< px.length; j++) {
     x=px[j];
     z=0;
     cosval=Math.cos(deg);
     sinval=Math.sin(deg);
     tmp1=x*cosval - z*sinval;
     tmp2=x*sinval + z*cosval;
     mesh[i][j][0]=(float)tmp1;
     mesh[i][j][1]=py[j];
     mesh[i][j][2]=(float)tmp2;
   }
 }

 return mesh;
}

void drawMesh(float mesh[][][]) {
 println(mesh.length+" "+mesh[0].length+" "+mesh[0][0].length);
 for(int i=0; i< mesh.length-1; i++) {
   beginShape(QUAD_STRIP);
   for(int j=0; j< mesh[0].length; j++) {
     vertex(mesh[i][j][0],mesh[i][j][1],mesh[i][j][2]);
     vertex(mesh[i+1][j][0],mesh[i+1][j][1],mesh[i+1][j][2]);
   }
   endShape();
 }
}
Re: drawing a curved surface
Reply #3 - Nov 26th, 2009, 2:30pm
 
yep that does help Smiley

Basically thats pretty much exactly what I need to do except just got to avoid tapering to a point at either end, so the height is uniform but I'll have a stab at doing that first next chance I get. I'll post up again when I've got it working, or got myself stuck!

Cheers for the help Cedric, much appreciated.
Re: drawing a curved surface
Reply #4 - Jan 25th, 2010, 5:54am
 
What if I wanted the surface to be smooth? Do I need to go to the trouble of calculating the normal vectors for the object?
Re: drawing a curved surface
Reply #5 - Jan 25th, 2010, 12:17pm
 
The torus was created using the Shapes3D library.

...
This image was captured from the sketch below. It uses both the Shapes3D and PeasyCam libraries. PeasyCam is being used to orbit the shape.

Code:
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;

import shapes3d.utils.*;
import shapes3d.org.apache.commons.math.util.*;
import shapes3d.org.apache.commons.math.*;
import shapes3d.org.apache.commons.math.geometry.*;
import shapes3d.*;


PeasyCam pcam;
Toroid t;
MeshSection ms1, ms2;
int nSegs = 20;
int nSlices = 30;

void setup() {
 size(300,300,P3D);
 pcam = new PeasyCam(this,0,0,0, 300);
 t = new Toroid(this, nSegs, nSlices);
 // Set the radius of the torus (x,z and the ring y)
 t.setRadius(40,30,100);
 // Set the fill, stroke and strokeWeight values
 t.fill(color(0,200,0));
 t.stroke(color(0));
 t.strokeWeight(1.5f);
 // Get a mesh section that represents the full torus
 ms1 = t.getDrawSection();
 // Change it so it only shows the outer half
 ms1.setRange(0,1,0,0.5f);
 // Add this section for drawing (only one section needed)
 t.addDrawSection(ms1);
}

void draw() {
 background(32);
 ambientLight(200,200,200);
 directionalLight(128, 128, 128, -1, -0.5f, -1);
 // Normally you would use one or the other (wire or solid)
 // but for demo purposes use both
 // Draw the torus in wire mode
 t.drawMode(Shape3D.WIRE);
 t.draw();
 // redraw the torus in solid (colour) mode
 t.drawMode(Shape3D.SOLID);
 t.draw();
}

Although this is in colour you can also use an image as a texture for the tyre.

This might save you some time.
Smiley

PS All shapes created using Shapes3D create and use normals by default.
Page Index Toggle Pages: 1