Tuesday, 29 May 2012

Draw square in Android RenderScript

Modify MyRSSurfaceView.java from last exercise "Perform transform of Translate and Rotate on RenderScript", to draw square by combine two Triangle.

package com.exercise.AndroidRenderScript;

import android.content.Context;
import android.renderscript.Mesh;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScriptGL;
import android.renderscript.RenderScriptGL.SurfaceConfig;

public class MyRSSurfaceView extends RSSurfaceView {

private RenderScriptGL renderScriptGL;
private ScriptC_RenderScript myScriptC;

public MyRSSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
final RenderScriptGL.SurfaceConfig surfaceConfig
= new SurfaceConfig();
renderScriptGL = createRenderScriptGL(surfaceConfig);

myScriptC = new ScriptC_RenderScript(
renderScriptGL, getResources(), R.raw.renderscript);

myScriptC.set_my_rs_mesh(createMesh());

renderScriptGL.bindRootScript(myScriptC);
}

private Mesh createMesh(){
Mesh.TriangleMeshBuilder myMesh
= new Mesh.TriangleMeshBuilder(
renderScriptGL,
2,
Mesh.TriangleMeshBuilder.COLOR);
/* 0 2
* +---+
* |\ |
* | \ |
* | \|
* +---+
* 1 3
*/


myMesh.addVertex(0, 0); //- 0
myMesh.addVertex(0, 100); //- 1
myMesh.addVertex(100, 0); //- 2
myMesh.addVertex(100, 100); //- 3

myMesh.addTriangle(0, 1, 3);
myMesh.addTriangle(0, 3, 2);

return(myMesh.create(true));
};

}


Draw square in Android RenderScript


Download the files.

No comments:

Post a Comment