将文本放在3D平面上

时间:2016-12-02 07:22:11

标签: android google-project-tango rajawali

我正在尝试在其上放置带有文字的3D平面。平面在3D中正确渲染。但是带有文本的纹理不会渲染。

以下是我创建3D对象的代码:

float destPoints[]={1.23f,1.2f,-3.0f};
Object3D destPointObject3D = makePoint(destPoints,"Laptop");
getCurrentScene().addChild(destPointObject3D);

/**
 * Render the new correspondence destination point measurements 
 * white background with plain text
 */
private Object3D makePoint(float[] openGLPpoint, String text) {
    Material mSphereMaterial = new Material();
    mSphereMaterial.enableLighting(true);
    mSphereMaterial.setColorInfluence(0);
    mSphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    Bitmap bitmap= ImageText.setTextInImage(text);
    try {
        Texture t = new Texture("text",bitmap);
        mSphereMaterial.addTexture(t);
    } catch (ATexture.TextureException e) {
        Log.e(TAG, "Exception generating earth texture", e);
    }
    Object3D object3D = new Plane(0.3f,0.1f,100,100);
    object3D.setMaterial(mSphereMaterial);

    object3D.setPosition(openGLPpoint[0],
                         openGLPpoint[1],
                         openGLPpoint[2]-3);
    return object3D;
}

//imageText method to write text
public static Bitmap setTextInImage(String text){
    // the original file yourimage.jpg i added in resources
    Bitmap src = 
            BitmapFactory.decodeResource(App.getContext().getResources(),
                                         R.drawable.white);    
    Bitmap dest = 
            Bitmap.createBitmap(src.getWidth(),
                                src.getHeight(),
                                Bitmap.Config.ARGB_8888);
    Canvas cs = new Canvas(dest);
    Paint tPaint = new Paint();
    tPaint.setTextSize(35);
    tPaint.setColor(Color.BLUE);
    tPaint.setStyle(Paint.Style.FILL);
    cs.drawBitmap(src, 0f, 0f, null);
    float height = tPaint.measureText("yY");
    float width = tPaint.measureText(text);
    float x_coord = (src.getWidth() - width)/2;

    // 15f is to put space between top edge and the text, if you want to 
    // change it, you can
    cs.drawText(text, x_coord, height+15f, tPaint); 

    return dest;
}

0 个答案:

没有答案