加载中Libgdx OBJ文件不工作的机器人 [英] Loading Obj files in Libgdx not working on android

查看:120
本文介绍了加载中Libgdx OBJ文件不工作的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code显示了一个圆环缓缓旋转,并进入显示:

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 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() {
    }
}

据,并通过点击和拖动屏幕上的用户可以旋转​​视角呈现了所保存的数据文件夹中的圆环OBJ。

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.

这工作正常在桌面上,但是当我尝试在Android上运行它,我得到一个NullPointerException异常为:

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);

model.render(GL10.GL_TRIANGLES);

我试过把torus.obj刚内资产和资产内/数据。我使用libgdx 0.9.2。

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

推荐答案

我想你指的是 model.render(GL10.GL_TRIANGLES); 。我相信你有两个问题。首先,模式是空,因为你正在追赶一个FileNotFoundException异常,而忽略它。我建议你​​不要赶FileNotFoundException异常,现在,让它崩溃,并期待在堆栈跟踪。这会给你一个更好的指示为什么这个失败的。需要注意的是e.printStackTrace()是没有用的调试Android上,尝试使用GDX日志。

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.

第二个问题是,我怀疑路径/流实际上是将错误的地方。而不是创建一个FileInputStream,请使用FileHandle.read()函数。它返回一个java.io.InputStream对象,你可以传递给ObjLoader.loadObj()。

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();

这和你的code之间的不同之处在于FileHandle.read()在libgdx的Andr​​oid后端采用的是Android <一href="http://developer.android.com/reference/android/content/res/AssetManager.html%20AssestManager">AssetManager打开捆绑在一起的程序文件。

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.

最后,复制torus.obj文件到&lt; YourAndroidProject&GT;。/assets/data/torus.obj

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

旁白:如果指定的行号,请提供完整的文件,否则该行将熄灭。你所提供的code 52行是: lastTouchY = Gdx.input.getY(); 。请注意导入......在你的code开头。这些进口影响的行号。

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.

这篇关于加载中Libgdx OBJ文件不工作的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆