Loading obj model OK but not displayed

Started by Anton, May 22, 2015, 05:36:00 PM

Previous topic - Next topic

Anton

Hi all. I have some problem with displaying obj model.
Here code of my renderer class:

package com.amulet_it.openglexample;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.widget.Toast;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Interact2D;
import com.threed.jpct.Light;
import com.threed.jpct.Loader;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;

import java.io.InputStream;
import java.util.HashMap;

public class MyRenderer implements GLSurfaceView.Renderer {

    private Context mContext = null;

    private FrameBuffer fb = null;
    private World world = null;
    private Camera cam = null;

    public MyRenderer(Context context) {
        mContext = context;
    }

    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {

    }

    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height) {

        if ( fb != null ) {
            fb.dispose();
        }

        fb = new FrameBuffer(width, height);

        world = new World();
        cam = world.getCamera();

        InputStream objStream = mContext.getResources().openRawResource(R.raw.test_obj);
        Object3D[] loadRes = Loader.loadOBJ(objStream, null, 3);

        Object3D cube = loadRes[0];
        cube.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS | Object3D.COLLISION_CHECK_SELF);
        cube.build();

        world.addObject(cube);

        Light light = new Light(world);
        light.setPosition(new SimpleVector(0, -80, 0));
        light.setIntensity(140, 120, 120);
        light.setAttenuation(-1);

        world.setAmbientLight(20, 20, 20);

        cam.moveCamera(Camera.CAMERA_MOVEOUT, 200);
        cam.moveCamera(Camera.CAMERA_MOVEUP, 200);
        cam.lookAt(cube.getTransformedCenter());

    }

    @Override
    public void onDrawFrame(GL10 unused) {

        fb.clear(Color.RED);
        world.renderScene(fb);
        world.draw(fb);
        fb.display();

   }
}

Obj file in attachment as test.txt.
The main problem i'm don't see loaded object on the scene.
Can some help me please?

EgonOlsen

The most common reasons are: The object is too small, too large or far away from the origin in object space. Try to play around with scale and position. Look at the result of getCenter() after calling build() and the dimensions of the bounding box (can be obtained from the mesh).

Anton

Egon, thank you for reply.
I am trying to scale, translate the object but still I do not see it. I'm a newbie in 3D programming and probably not fully understand all the methods of working with 3D objects.
Can you help me to determine what kind of transformation I must do that the object became visible.

EgonOlsen

Even without trying the model itseld, you can spot the issue from the file itself:


v  39140.1797 1560.7319 -45251.5781
v  39140.1797 1560.7319 -45326.3555
v  39062.1797 1560.7319 -45326.3555
v  39124.3594 1560.7319 -45251.5781


These are examples from the file's vertex definition list. You are loading them with scale 3, which means that your coordinates are located somewhere at 120,000,...,-135.000...which basically means: This thing is huge and far away from the camera anyway. Try to load it with a scale of 0.01 or even 0.001 and see if that helps.

Anton

Egon, thank you very much.
By selecting I found the correct scale. When I load model it is 0.001f and after build I scale it with parameter 20.
But it is not clear if I load model with 0.02f scale and after build I do not scale I do not see it again.
What different between this approaches?

EgonOlsen

The center is different. The model scales around its rotation pivot (which is the calculated center by default). It might be a good idea to translate the object by the -1*center to place it at the origin. Then both approaches should deliver the same results.

Anton

If I understand correctly this code translate model to the origin and set rotation pivot and center to the origin. Is it right? And then I must scale model as I need?


InputStream objStream = mContext.getResources().openRawResource(R.raw.test_obj);
Object3D[] loadRes = Loader.loadOBJ(objStream, null, 1);

cube = loadRes[0];

float[] bb = cube.getMesh().getBoundingBox();
cube.translate(-(bb[0] + ((bb[1] - bb[0]) / 2)),
-(bb[2] + ((bb[3] - bb[2]) / 2)), -(bb[4] + ((bb[5] - bb[4]) / 2)));
cube.translateMesh();
cube.setTranslationMatrix(new Matrix());

cube.build();

cube.setCenter(SimpleVector.ORIGIN);
cube.setRotationPivot(SimpleVector.ORIGIN);

cube.scale(0.05f);

world.addObject(cube);

EgonOlsen

Yes, something like that. Personally, I would have used getCenter() instead of the center of the bounding box, but it highly depends on the model which will work better.

Anton

Thanks a lot.
I will continue to learn the basics.