Search code examples
javaandroidopengl-espoint-cloud-librarygoogle-project-tango

fill color inside triangle by three co-ordinates


I am using Point Cloud. I have 3D points with me .

Let's say : Point P(x,y,z), Point Q(x,y,z), Point R(x,y,z) assuming this points as Triangle PQR we proceed further .

Triangle Like this : Triangle created with 3 points.

How can it be possible to fill the area inside this points plotted , so that the triangle will be filled with colour.

Like this : image.

My study that might have helped :

Edit :

Some way to success :

public void make_polygon(float[] points_x,float[] points_y,float[] points_z)
{
    Material mSphereMaterial_z = new Material();
    //mSphereMaterial_z.setColor(Color.BLUE);
    Bitmap p_z_bitty = getTriangleBitmap(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.label_bg_sm),5,points_x,points_y,points_z);
    Texture t = new Texture("text",p_z_bitty);
    try {
        mSphereMaterial_z.addTexture(t);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    Object3D p_z =  new Plane();
    p_z.setPosition(points_x[0],points_y[1],points_z[2]);
    p_z.setMaterial(mSphereMaterial_z);
    p_z.setDoubleSided(true);
    getCurrentScene().addChild(p_z);
}

public static Bitmap getTriangleBitmap(Bitmap bitmap, int radius,float[] a,float[] b,float[] c) {
    Bitmap finalBitmap;
    if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
        finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                false);
    else
        finalBitmap = bitmap;
    Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
            finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
            finalBitmap.getHeight());

     //        Point point1_draw = new Point(75, 0);
     //        Point point2_draw = new Point(0, 180);
     //        Point point3_draw = new Point(180, 180);
    PointF point1_draw = new PointF(a[0],a[1]);
    PointF point2_draw = new PointF(b[0], b[1]);
    PointF point3_draw = new PointF(c[0],c[1] );

    Path path = new Path();
    path.moveTo(point1_draw.x, point1_draw.y);
    path.lineTo(point2_draw.x, point2_draw.y);
    path.lineTo(point3_draw.x, point3_draw.y);
    path.lineTo(point1_draw.x, point1_draw.y);
    path.close();
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawPath(path, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(finalBitmap, rect, rect, paint);

    return output;
}

Now result is : enter image description here

Can some one guide me or suggest some article about it for Android java ?

If any more information / code needed , it shall be provided.


Solution

  • You should be able to do this by simply passing the vertices from the triangle to Rajawali in order to draw the triangle as a primitive. That way you can avoid using Canvas or drawing a bitmap and simplify the rendering.

    // p, q, and r are triangle vertices and are of the form {x, y, z}.
    public void make_triangle(float[] p, float[] q, float[] r)
    {
        Object3D triangle = new Object3D();
    
        // Copy three points into a flat array as our vertices.
        float[] vertices = new float[3 * 3];
        for (int i = 0; i < 3; i++) {
            vertices[i] = p[i];
            vertices[3 + i] = q[i];
            vertices[6 + i] = r[i];
        }
    
        // Set up index buffer to point at our three vertices and thus draw one triangle.
        int[] indices = new int[3];
        for (int i = 0; i < 3; i++) {
            indices[i] = i;
        }
    
        // Set up the rendering data.
        triangle.setData(vertices, null, null, null, indices, true);
    
        // Render the triangle double sided.
        triangle.setDoubleSided(true);
    
        // Use a blue material.
        Material material = new Material();
        material.setColor(Color.BLUE);
        triangle.setMaterial(material);
    
        // Add the triangle to the current scene.
        getCurrentScene().addChild(triangle);
    }