Drawing a tube

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Drawing a tube

Irene Tang
Hi, I found another way to draw a tube, which is drawing a lot of circles then connecting the adjacent circles to a segment. I've got a problem that the segment might get twisted like this: .
Here is the main code to calculate all the points on the circles:
//curvePoints are the center of the circles
//cn is the number of the center
//res is the number of dividing a circle into how many pieces
public static List<Circle> circlesFromCurve(List<Point3D> curvePoints, int cn, int res, double radius) {
        List<Circle> circles = new ArrayList<Circle>();
        Point3D pRandom = new Point3D();
        pRandom.x = 1;
        pRandom.y = 1;
        pRandom.z = 1;
       
        for(int i = 0; i < cn - 1; i++) {
            Point3D pDirection = new Point3D();
            Point3D axis1 = new Point3D();
            Point3D axis2 = new Point3D();
           
            pDirection.x = curvePoints.get(i+1).x - curvePoints.get(i).x;
            pDirection.y = curvePoints.get(i+1).y - curvePoints.get(i).y;
            pDirection.z = curvePoints.get(i+1).z - curvePoints.get(i).z;
           
            axis1.x=pDirection.y * pRandom.z - pDirection.z * pRandom.y;
            axis1.y=pDirection.z * pRandom.x - pDirection.x * pRandom.z;
            axis1.z=pDirection.x * pRandom.y - pDirection.y * pRandom.x;
            double l=axis1.x * axis1.x + axis1.y * axis1.y + axis1.z * axis1.z;
            axis1.x=axis1.x / Math.sqrt(l);
            axis1.y=axis1.y / Math.sqrt(l);
            axis1.z=axis1.z / Math.sqrt(l);
           
            axis2.x=axis1.y * pDirection.z - axis1.z * pDirection.y;
            axis2.y=axis1.z * pDirection.x - axis1.x * pDirection.z;
            axis2.z=axis1.x * pDirection.y - axis1.y * pDirection.x;
            l=axis2.x * axis2.x + axis2.y * axis2.y + axis2.z * axis2.z;
            axis2.x=axis2.x / Math.sqrt(l);
            axis2.y=axis2.y / Math.sqrt(l);
            axis2.z=axis2.z / Math.sqrt(l);
           
            Circle circle = new Circle();
            circle.n = res;
            circle.points = new Point3D[res];
            double step = 360 / res;
            for(int j = 0; j < res; j++) {
                double degree = j * step * Math.PI / 180;
                double vcos = Math.sin(degree) * radius;
                double vsin = Math.cos(degree) * radius;
                double x = curvePoints.get(i).x + axis1.x * vcos + axis2.x * vsin;
                double y = curvePoints.get(i).y + axis1.y * vcos + axis2.y * vsin;
                double z = curvePoints.get(i).z + axis1.z * vcos + axis2.z * vsin;
                Point3D point = new Point3D(x, y, z);
                circle.points[j] = point;
            }
            circles.add(circle);
           
            pRandom.x = pDirection.x;
            pRandom.y = pDirection.y;
            pRandom.z = pDirection.z;
        }
        return circles;
    }
Here is the code for drawing a tube:
List<Circle> circles = Tube.circlesFromCurve(curvePoints, 3, 8, 1, gl2);
for(int i = 0; i < circles.size() - 1; i++) {
            gl2.glBegin(GL2.GL_TRIANGLE_STRIP);
            for(int j = 0; j < 1; j++) {
                gl2.glVertex3d(circles.get(i).points[j].x, circles.get(i).points[j].y, circles.get(i).points[j].z);
                gl2.glVertex3d(circles.get(i).points[(j + 1) % 8].x, circles.get(i).points[(j + 1) % 8].y, circles.get(i).points[(j + 1) % 8].z);
                gl2.glVertex3d(circles.get(i + 1).points[j].x, circles.get(i + 1).points[j].y, circles.get(i + 1).points[j].z);
                gl2.glVertex3d(circles.get(i + 1).points[(j + 1) % 8].x, circles.get(i + 1).points[(j + 1) % 8].y, circles.get(i + 1).points[(j + 1) % 8].z);
               
            }
            gl2.glEnd();
}
This is a outputted picture when I applied this method to my project: . It has gaps that i think are caused by twisted segment. Do you have any ideas to avoid twist?
Reply | Threaded
Open this post in threaded view
|

Re: Drawing a tube

Irene Tang
I searched online and found this. http://stackoverflow.com/questions/11193524/create-tube-along-polyline-path-in-opengl. Some  guy named M-V mentioned the twist problem. But I don't understand him. Is it the same problem he and I are talking about?