以下代码显示了一个缓慢旋转并开始显示的圆环:
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() {
}
}
它渲染一个保存在数据文件夹中的环面对象,通过在屏幕上单击和拖动,用户可以旋转相机。
这在桌面上运行良好,但是当我尝试在 android 上运行它时,我在以下位置收到 NullPointerException:
模型.render(GL10.GL_TRIANGLES);
我尝试将 torus.obj 放在资产内和资产/数据内。我正在使用 libgdx 0.9.2。