开发者

Loading Obj files in Libgdx not working on android

开发者 https://www.devze.com 2023-04-06 22:15 出处:网络
The following code shows a torus slowly revolving and coming into display: package com.objloader.example;

The following code shows a torus slowly revolving and coming into display:

package com.objloader.example;

import ...
public class ObjLoaderProg implements ApplicationListener{
    String torus;
    Mesh model;
    private PerspectiveCamera 开发者_JAVA技巧camera;
    @Override
    public void create() {
        InputStream stream=null;
        try {
            stream = new FileInputStream(Gdx.files.internal("data/torus.obj").path());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        model = ObjLoader.loadObj(stream, true);
        Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);
        Gdx.gl10.glTranslatef(0.0f,0.0f,-3.0f);
    }

    @Override
    public void dispose() {
    }

    @Override
    public void pause() {
    }

    protected int lastTouchX;
    protected int lastTouchY;
    protected float rotateZ=0.01f;
    protected float increment=0.01f;

    @Override
    public void render() {
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        camera.update();
        camera.apply(Gdx.gl10);
        Gdx.gl10.glTranslatef(0.0f,0.0f,-3.0f);
        Gdx.gl10.glRotatef(rotateZ, rotateZ, 5.0f, rotateZ);
        model.render(GL10.GL_TRIANGLES);

         if (Gdx.input.justTouched()) {
              lastTouchX = Gdx.input.getX();
              lastTouchY = Gdx.input.getY();
            } else if (Gdx.input.isTouched()) {
              camera.rotate(0.2f * (lastTouchX - Gdx.input.getX()), 0, 1.0f, 0);
              camera.rotate(0.2f * (lastTouchY - Gdx.input.getY()), 1.0f, 0, 0);

              lastTouchX = Gdx.input.getX();
              lastTouchY = Gdx.input.getY();
            }
         rotateZ+=increment;
         System.out.println(""+rotateZ);
    }

    @Override
    public void resize(int arg0, int arg1) {
        float aspectRatio = (float) arg0 / (float) arg1;
        camera = new PerspectiveCamera(67, 2f * aspectRatio, 2f);
        camera.near=0.1f;
        camera.translate(0, 0, 0);
    }

    @Override
    public void resume() {
    }
}

It renders a torus obj that's saved in the data folder, and by clicking and dragging on the screen the user can rotate the camera.

This works fine on the desktop, but when I try to run it on android, I get a NullPointerException at:

model.render(GL10.GL_TRIANGLES);

I've tried placing torus.obj just inside assets, and within assets/data. I'm using libgdx 0.9.2.


I assume you are referring to model.render(GL10.GL_TRIANGLES);. I believe you have two problems. First, model is null because you are catching a FileNotFoundException and ignoring it. I suggest you don't catch the FileNotFoundException right now, let it crash, and look at the stack trace. That will give you a better indication of why this failing. Note that e.printStackTrace() is not useful for debugging on android, try using the gdx log.

The second problem is that I suspect the path/stream is actually going to the wrong place. Instead of creating a FileInputStream, use the FileHandle.read() function. It returns a java.io.InputStream that you can pass to ObjLoader.loadObj().

in = Gdx.files.internal("data/torus.obj").read();
model ObjLoader.loadObj(in);
in.close();

The difference between this and your code is that FileHandle.read() in libgdx's android backend uses the android AssetManager to open files that are bundled with the program.

Finally, copy the torus.obj file to <YourAndroidProject>/assets/data/torus.obj.

An aside: if you specify the line number, please provide the full file otherwise the line will be off. Line 52 of the code you have provided is: lastTouchY = Gdx.input.getY();. Note the "import ..." at the beginning of your code. Those imports affect the line number.

0

精彩评论

暂无评论...
验证码 换一张
取 消