load a .obj file in JPCT-AE demo

Started by nico_r_a, July 21, 2011, 09:50:18 AM

Previous topic - Next topic

nico_r_a

Hello!!
I would like to load a .obj file in JPCT-AE.
I have read all the topics in relation with my question but i haven't found an answer.
So, how should i do to load .obj file in the demo JPCT-AE.

thank you very much

sorry for the mistakes, but i am french

EgonOlsen

There's a Loader-class for loading OBJ-files. Copy your obj-file (and the mtl-file if needed) into the res/raw-folder of your project and use the Loade.loadOBJ(...) method to load it. Keep in mind that Android limits the file size based on the extension. So if your obj-file is too large (>1mb), you might want to zip it and load it via a ZipInputStream or rename it to .mp3 (no restrictions apply for that extension).
In addition, it may be required to scale and/or translate the model to make it appear correctly in the demo. For loading textured models, please refer to the wiki: http://www.jpct.net/wiki/index.php/Loading_models

nico_r_a

#2
thank you for the reply
actually my mtl et obj are in differrent folders.
Should i change the way to texture in the obj et mtl if i add the mtl et obj in (res/raw)??
if yes, what should the software be used to open the mtl and obj??

and my .obj do 3.5Mo so i must change this extension in .mp3?
and where should i copy the command Loade.loadOBJ(...)??
and i put the way in the (...)?

EgonOlsen


nico_r_a

#4
i have seen your link,
but there aren't information on the large of my obj file.
what should i do with my obj file if his large is 3.5Mo?

and actually i have a folder with files
.obj
.mtl
.mb
.ma
.fbx
.dxf
.dae
what file should i use to create a 3d wih android please?
thank you

EgonOlsen

You need obj and mtl. If the file is too large, rename to .mp3 as a quick and dirty solution. However, loading a 3.5Mb OBJ file into Android might not be the best idea ever. What kind of model is that? Isn't it possible to reduce the polygon count?

sienioslaw

Hello! I'm new one to this forum, and I also have problem with loading obj models...
Here is some info: I'm using Eclipse and Android SDK 2.1
I'm following this tutorial: http://www.jpct.net/wiki/index.php/Loading_3ds_Models_from_Blender - only changed loading function from 3ds into OBJ(as far I understood from doc's, I don't need mtl, so left it null),
I put my model, a example file from http://people.sc.fsu.edu/~jburkardt/data/obj/obj.html in 'assets' folder

Tried to test it on emulator but crashing like hell  :-\ and looks like it's OutOfMemory problem, but how if I only load simple cube file? 

Here is code and log:
source:

package com.threed.jpct.example;

import java.io.*;
import java.lang.reflect.Field;

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

import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;


import com.threed.jpct.*;
import com.threed.jpct.util.BitmapHelper;
import com.threed.jpct.util.MemoryHelper;
/**
* A simple demo. This shows more how to use jPCT-AE than it shows how to write
* a proper application for Android.
* It includes basic activity management to handle pause and resume...
*
* @author EgonOlsen
*
*/
public class HelloWorld extends Activity {
    private String thingName = "temp";
    private int thingScale = 1;//end
private static HelloWorld master = null;

private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer buffer = null;

private World world = null;
private RGBColor back = new RGBColor(50, 50, 100);

private float touchTurn = 0;
private float touchTurnUp = 0;

private float xpos = -1;
private float ypos = -1;

private Object3D thing = null;
private int fps = 0;

private Light sun = null;
private Camera cam = null;

AssetManager assMan;
InputStream is;
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(getApplication());
renderer = new MyRenderer();
mGLView.setRenderer(renderer);
setContentView(mGLView);
}

@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}

@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
}

protected void onStop() {
super.onStop();
}

public boolean onTouchEvent(MotionEvent me) {

if (me.getAction() == MotionEvent.ACTION_DOWN) {
xpos = me.getX();
ypos = me.getY();
return true;
}

if (me.getAction() == MotionEvent.ACTION_UP) {
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
return true;
}

if (me.getAction() == MotionEvent.ACTION_MOVE) {
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;

xpos = me.getX();
ypos = me.getY();

touchTurn = xd / -100f;
touchTurnUp = yd / -100f;
return true;
}

try {
Thread.sleep(15);
} catch (Exception e) {
// No need for this...
}

return super.onTouchEvent(me);
}

protected boolean isFullscreenOpaque() {
return true;
}

class MyRenderer implements GLSurfaceView.Renderer {

private long time = System.currentTimeMillis();
private boolean stop = false;

public MyRenderer() {
}

public void stop() {
stop = true;
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
if (buffer != null) {
buffer.dispose();
}
buffer = new FrameBuffer(gl, w, h);

if (master == null) {

world = new World();
        world.setAmbientLight(150, 150, 150);

sun = new Light(world);
sun.setIntensity(250, 250, 250);

thing = loadModel("assets/model.obj", thingScale);
        thing.build();

world.addObject(thing);

cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(thing.getTransformedCenter());

SimpleVector sv = new SimpleVector();
sv.set(thing.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
//MemoryHelper.compact();

if (master == null) {
Logger.log("Saving master Activity!");
master = HelloWorld.this;
}
}
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

public void onDrawFrame(GL10 gl) {

try {
if (!stop) {
if (touchTurn != 0) {
thing.rotateY(touchTurn);
touchTurn = 0;
}

if (touchTurnUp != 0) {
thing.rotateX(touchTurnUp);
touchTurnUp = 0;
}

buffer.clear(back);
world.renderScene(buffer);
world.draw(buffer);
buffer.display();

if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
}
fps++;
} else {
if (buffer != null) {
buffer.dispose();
buffer = null;
}
}
} catch (Exception e) {
Logger.log(e, Logger.MESSAGE);
}
}

    private Object3D loadModel(String filename, float scale) {
assMan = getResources().getAssets();
is = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
};
        Object3D[] model = Loader.loadOBJ(is, null, scale);
        Object3D o3d = new Object3D(0);
        Object3D temp = null;
        for (int i = 0; i < model.length; i++) {
            temp = model[i];
            temp.setCenter(SimpleVector.ORIGIN);
            temp.rotateX((float)( -.5*Math.PI));
            temp.rotateMesh();
            temp.setRotationMatrix(new Matrix());
            o3d = Object3D.mergeObjects(o3d, temp);
            o3d.build();
        }
        return o3d;
    }
}
}





log:
07-25 22:33:48.269: WARN/ActivityManager(65): Activity idle timeout for HistoryRecord{44ffd098 com.threed.jpct.example/.HelloWorld}
07-25 22:33:48.549: INFO/dalvikvm-heap(466): Grow heap (frag case) to 13.402MB for 6378722-byte allocation
07-25 22:33:51.678: INFO/dalvikvm-heap(466): Forcing collection of SoftReferences for 9568078-byte allocation
07-25 22:33:51.798: ERROR/dalvikvm-heap(466): Out of memory on a 9568078-byte allocation.
07-25 22:33:51.798: INFO/dalvikvm(466): "GLThread 8" prio=5 tid=7 RUNNABLE
07-25 22:33:51.809: INFO/dalvikvm(466):   | group="main" sCount=0 dsCount=0 s=N obj=0x44e85270 self=0x11f848
07-25 22:33:51.809: INFO/dalvikvm(466):   | sysTid=472 nice=0 sched=0/0 cgrp=default handle=1283576
07-25 22:33:51.809: INFO/dalvikvm(466):   | schedstat=( 10442697916 807643615 403 )
07-25 22:33:51.828: INFO/dalvikvm(466):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~97)
07-25 22:33:51.828: INFO/dalvikvm(466):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:157)
07-25 22:33:51.838: INFO/dalvikvm(466):   at java.lang.StringBuffer.append(StringBuffer.java:215)
07-25 22:33:51.838: INFO/dalvikvm(466):   at com.threed.jpct.Loader.loadBinaryFile(Loader.java:1076)
07-25 22:33:51.850: INFO/dalvikvm(466):   at com.threed.jpct.Loader.loadTextFile(Loader.java:71)
07-25 22:33:51.850: INFO/dalvikvm(466):   at com.threed.jpct.Loader.loadOBJ(Loader.java:302)
07-25 22:33:51.850: INFO/dalvikvm(466):   at com.threed.jpct.Loader.loadOBJ(Loader.java:207)
07-25 22:33:51.858: INFO/dalvikvm(466):   at com.threed.jpct.example.HelloWorld$MyRenderer.loadModel(HelloWorld.java:218)
07-25 22:33:51.858: INFO/dalvikvm(466):   at com.threed.jpct.example.HelloWorld$MyRenderer.onSurfaceChanged(HelloWorld.java:148)
07-25 22:33:51.878: INFO/dalvikvm(466):   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1325)
07-25 22:33:51.878: INFO/dalvikvm(466):   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
07-25 22:33:51.928: INFO/ActivityManager(65): Displayed activity com.threed.jpct.example/.HelloWorld: 14353 ms (total 418702 ms)
07-25 22:33:51.958: WARN/dalvikvm(466): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
07-25 22:33:52.078: ERROR/AndroidRuntime(466): FATAL EXCEPTION: GLThread 8
07-25 22:33:52.078: ERROR/AndroidRuntime(466): java.lang.OutOfMemoryError
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:157)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at java.lang.StringBuffer.append(StringBuffer.java:215)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at com.threed.jpct.Loader.loadBinaryFile(Loader.java:1076)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at com.threed.jpct.Loader.loadTextFile(Loader.java:71)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at com.threed.jpct.Loader.loadOBJ(Loader.java:302)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at com.threed.jpct.Loader.loadOBJ(Loader.java:207)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at com.threed.jpct.example.HelloWorld$MyRenderer.loadModel(HelloWorld.java:218)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at com.threed.jpct.example.HelloWorld$MyRenderer.onSurfaceChanged(HelloWorld.java:148)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1325)
07-25 22:33:52.078: ERROR/AndroidRuntime(466):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
07-25 22:33:52.168: WARN/ActivityManager(65):   Force finishing activity com.threed.jpct.example/.HelloWorld


Any advices?
Btw. Sorry for my english...

EgonOlsen

#7
You've coded that OutOfMemory-Exception all by yourself. Why are you creating an InputStream that does nothing but constantly writes 0 into the stream? This can't lead to anywhere else but an OutOfMemory. Simply load it like this:


cube=Object3D.mergeAll(Loader.loadOBJ(getResources().getAssets().open("cube.obj"), null, 20));

nico_r_a

ok, but when i put the file .mp3 in my raw, le file R.java doesn't update.

EgonOlsen

Press f5 on the project in Eclipse.

nico_r_a

i have already tried and don't update

EgonOlsen

It's an Eclipse issue. I have this problem myself from time to time. Try to erstattet Eclipse or do a clean on the project.

nico_r_a

#12
i have another problem now.
in my .obj i don't have the link with my texture.
how can i do to create a link between the texture and the object?


nico_r_a

#14
but i don't understand, i have downloaded deep exploration 5.06. But what should i do with the texture??
i don't understant the link between the code java in eclipse and the deep exploration

and can i do a effect transparency on a object??

I know that my questions are bad level but i am beginner.
thank you