Load 3d Object in android

This example will show how you can use 3DObject model to your android application using the Rajawali library. You can move objects using the user touch.

3D Modeling:-

In 3D computer graphics3D modeling (or three-dimensional modeling) is the process of developing a mathematical representation of any three-dimensional surface of an object (either inanimate or living) via specialized software. The product is called a 3D model
 

Steps:-

1) Add the Gradle to your project:-
        compile 'org.rajawali3d:rajawali:1.0.325@aar'

2) Download any object file and copy that in your raw folder.

3) Create a rendering class and use that class to make 3DObject.


Note:- You can download 3DObject file from below link.
           
           Source : http://tf3dm.com/ 


1) Create activity_main.java.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.demoimage360view.MainActivity">

    <ImageView
        android:id="@+id/iv_imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>
   
</RelativeLayout>


2 Create MainActivity.java.


public class MainActivity extends AppCompatActivity {

    ImageView imageView360;
    Renderer renderer;
    int angle = 10;
    private int mStartX, mStartY, mEndX, mEndY, mImageIndex = 0;

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

        imageView360 = (ImageView) findViewById(R.id.iv_imageView);


        final RajawaliSurfaceView surface = new RajawaliSurfaceView(this);
        surface.setFrameRate(60.0);
        surface.setRenderMode(IRajawaliSurface.RENDERMODE_WHEN_DIRTY);

        // Add mSurface to your root view
        addContentView(surface, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT));

        renderer = new Renderer(this);
        surface.setSurfaceRenderer(renderer);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();

        switch (action) {
            case (MotionEvent.ACTION_DOWN):

                mStartX = (int) event.getX();
                mStartY = (int) event.getY();
                return true;

            case (MotionEvent.ACTION_MOVE):

                mEndX = (int) event.getX();
                mEndY = (int) event.getY();

                if ((mEndX - mStartX) > 3) {
                    angle = angle + 10;
                    if (mImageIndex > 100)
                        mImageIndex = 0;

                    imageView360.setImageLevel(mImageIndex);
                    Log.i("Level", mImageIndex + "");
                    renderer.getObject().rotate(Vector3.Axis.Y, -1.0);

                }
                if ((mEndX - mStartX) < -3) {
                    angle = angle - 10;
                    if (mImageIndex < 0)
                        mImageIndex = 10;

                    renderer.getObject().rotate(Vector3.Axis.Y, 1.0);

                }
                mStartX = (int) event.getX();
                mStartY = (int) event.getY();

                return true;

            case (MotionEvent.ACTION_UP):
                mEndX = (int) event.getX();
                mEndY = (int) event.getY();

                return true;

            case (MotionEvent.ACTION_CANCEL):
                return true;

            case (MotionEvent.ACTION_OUTSIDE):
                return true;

            default:
                return super.onTouchEvent(event);
        }
    }


}

3) Create Renderer.java.

package com.demoimage360view;

import android.content.Context;
import android.view.MotionEvent;

import org.rajawali3d.Object3D;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.loader.LoaderOBJ;
import org.rajawali3d.loader.ParsingException;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.renderer.RajawaliRenderer;

import javax.microedition.khronos.opengles.GL10;

public class Renderer extends RajawaliRenderer {

    private DirectionalLight directionalLight;
    private Object3D earthSphere;
    LoaderOBJ objParser ;

    public Renderer(Context context) {
        super(context);
        setFrameRate(60);
    }

    public void onTouchEvent(MotionEvent event){
    }

    public void onOffsetsChanged(float x, float y, float z, float w, int i, int j){
    }

    @Override
    protected void initScene() {
        directionalLight = new DirectionalLight(1f, .2f, -1.0f);
        directionalLight.setColor(1.0f, 1.0f, 1.0f);
        directionalLight.setPower(2);
        getCurrentScene().addLight(directionalLight);

        //your object file name instead of R.raw.***;

        objParser = new LoaderOBJ(mContext.getResources(),mTextureManager, R.raw.finalbasemesh);

        try {

            objParser.parse();
            earthSphere = objParser.getParsedObject();
            earthSphere.setX(0);
            earthSphere.setY(-1);
            earthSphere.setZ(0);
            earthSphere.setScale(0.1f);    //change as per you object height to fit in screen
            earthSphere.setDoubleSided(true);
            getCurrentScene().addChild(earthSphere);


        } catch (ParsingException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onRender(long ellapsedRealtime, double deltaTime) {
        super.onRender(ellapsedRealtime, deltaTime);
    }

    @Override
    public void onRenderFrame(GL10 gl) {
        super.onRenderFrame(gl);

    }

    public Object3D getObject(){
        return earthSphere;
    }
}


Output
 3d model        3d model
                       Model Source : http://tf3dm.com/

Note:- You can move objects automatically. For that, you need to add below line in renderer.java

         @Override
           protected void onRender(long ellapsedRealtime, double deltaTime) {
               super.onRender(ellapsedRealtime, deltaTime);
                 earthSphere.rotate(Vector3.Axis.Y, 1.0);
    }

                  



Comments

  1. How can I change color of the object ??

    ReplyDelete
    Replies
    1. You have add texture to your model. If you looking for the example, you can visit this link.

      Materials : https://github.com/Rajawali/Rajawali/wiki/Materials

      Delete
  2. I cannot change color of object

    ReplyDelete
    Replies
    1. You have add texture to your model. If you looking for the example, you can visit this link.

      Materials : https://github.com/Rajawali/Rajawali/wiki/Materials

      Delete

Post a Comment