Reputation: 1253
How can I create a reticle using the Cardboard SDK for Android Studio and RajawaliVR 3D Renderer?
I search through out many websites and the Rajawali wiki on github to try and find a solution that would keep a 3D object in the center of the users view with perfect orientation. I did stumble upon a link on the WIKI but it did not offer a solution that worked with RajawaliVR.
After some trial and error I came up with this solution
First create a 3D Object in the renderer
Sphere RETICLE = new Sphere(1, 50, 32);
Material sphereMaterial = new Material();
sphereMaterial.enableLighting(true);
sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
RETICLE.setMaterial(sphereMaterial);
try{
sphereMaterial.addTexture(tExture);
} catch (ATexture.TextureException error){
Log.d("DEBUG", "TEXTURE ERROR");
}
Next you will need the following code placed in your renderer class
public void centerObject(Object3D obj){
float[] newPosition4 = new float[4];
float[] posVec4 = {0, 0, -3, 1.0f};
float[] HeadViewMatrix_inv = new float[16];
Matrix4 HeadViewMatrix4 = new Matrix4();
HeadViewMatrix4.setAll(mHeadViewMatrix);
HeadViewMatrix4 = HeadViewMatrix4.inverse();
HeadViewMatrix4.toFloatArray(HeadViewMatrix_inv);
Matrix.multiplyMV(newPosition4, 0, HeadViewMatrix_inv, 0, posVec4, 0);
obj.setPosition(newPosition4[0], newPosition4[1], newPosition4[2]);
obj.setLookAt(getCurrentCamera().getPosition());
}
You can then call the centerObject method from the onRender method or fram a handler. (The on render method gets called everytime a new frame is drawn.)
@Override
public void onRender(long elapsedTime, double deltaTime) {
super.onRender(elapsedTime, deltaTime);
if(RETICLE != null)centerObject(RETICLE);
}
Placing a null check before calling centerObject() is important because the scene may render before the object is created.
The Rajawali WIKI link above offered some great clues but the issue has been closed. I did comment there as well with my solution as to help provide a turn key solution for the next person facing the same problem.
I hope the helps some people. Please post better, Improved or other solutions so others may benefit.
Upvotes: 2
Views: 513