Skip to content

v0.9 Tutorial 18 Drawing Lines

contriteobserver edited this page Jul 16, 2017 · 1 revision

A line in Rajawali consists of one or more line segments. First you need to specify all the points that make up the line. Like so:

Stack points = new Stack();
points.add(new Number3D(2, 0, 0));
points.add(new Number3D(4, 0, 0));
points.add(new Number3D(2, 2, 0));
points.add(new Number3D(2, 0, 6));
// ... etc ...

This Stack can then be passed into the constructor of the Line3D class along with a thickness and a color:

Line3D line = new Line3D(points, 1, 0xffffff00);
Material material = new Material();
material.setUseColor(true);
whirl.setMaterial(material);
addChild(whirl);

It’s easy as that. Using line segments like this you can create nice looking shapes like this:

It’s also relatively easy to draw bezier curves. You first need to create an instance of BezierPath3D and then pass in the points and control points:

Cubic Bezier Curve

            ICurve3D curve = new CubicBezierCurve3D(new Vector3(-1, 0, 0),
                    new Vector3(-1, 1.3f, 0), new Vector3(1, -1.9f, 0),
                    new Vector3(1, 0, 0));

Linear Bezier Curve


            ICurve3D curve = new LinearBezierCurve3D(new Vector3(-1, 0, 0), new Vector3(
                    1, 0, 0));

Quadratic Bezier Curve

            ICurve3D curve = new QuadraticBezierCurve3D(new Vector3(-1, 0, 0),
                    new Vector3(.3f, 1, 0), new Vector3(1, 0, 0));

Now we can interpolate through these points and create line segments. The more segments, the smoother the line.

        private void drawCurve(ICurve3D curve, int color, Vector3 position) {
            Material lineMaterial = new Material();

            Stack<Vector3> points = new Stack<Vector3>();
            for (int i = 0; i <= NUM_POINTS; i++) {
                Vector3 point = new Vector3();
                curve.calculatePoint(point, (float) i / (float) NUM_POINTS);
                points.add(point);
            }

            Line3D line = new Line3D(points, 1, color);
            line.setMaterial(lineMaterial);
            line.setPosition(position);
            getCurrentScene().addChild(line);
        }

… and here’s the result of this:

The shape in the first image can be seen in the examples app.

Anti aliasing/multi sampling can be enabled in the Activity. Just call surface.setAntiAliasingMode() method with ISurface.ANTI_ALIASING_CONFIG.MULTISAMPLING just before surface.setSurfaceRenderer() is called:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rajawali);

        surface = (SurfaceView) findViewById(R.id.glsl_content);
        renderer = new WhirlRenderer(getApplicationContext());

        surface.setFrameRate(30);
        surface.setRenderMode(ISurface.RENDERMODE_WHEN_DIRTY);
        surface.setAntiAliasingMode(ISurface.ANTI_ALIASING_CONFIG.MULTISAMPLING);
        surface.setSurfaceRenderer(renderer);
	...
Clone this wiki locally