我已经尝试了下面的代码,但是我在下面突出显示的行中得到了NullPointerException。
班级名称:地形
在以下行中获取例外
modelInstance = new ModelInstance(modelBuilder.end(),0,0,0);
请找到以下代码
代码:
private PerspectiveCamera camera;
private ModelBatch modelBatch;
private ModelBuilder modelBuilder;
private Model box;
private ModelInstance modelInstance;
private Environment environment;
@Override
public void create () {
camera = new PerspectiveCamera(75,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.position.set(0f, 0f, 3f);
camera.lookAt(0f,0f,0f);
camera.near =0.1f;
camera.far = 300f;
modelBatch = new ModelBatch();
modelBuilder = new ModelBuilder();
modelBuilder.begin();
MeshPartBuilder part=modelBuilder.part("Box1", GL20.GL_TRIANGLES, Usage.Position, null);
part.rect(new Vector3(0,0,0),new Vector3(100,0,0),new Vector3(100,100,0),new Vector3(0,100,0),new Vector3(0,0,0));
modelInstance = new ModelInstance(modelBuilder.end(),0,0,0);
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight,0.8f,0.8f,0.8f,1f));
Gdx.input.setInputProcessor(this);
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT|GL20.GL_DEPTH_BUFFER_BIT);
camera.update();
modelBatch.begin(camera);
modelBatch.render(modelInstance,environment);
modelBatch.end();
}
@Override
public boolean keyDown(int keycode) {
// In the real world, do not create NEW variables over and over, create
// a temporary static member instead
if(keycode == Input.Keys.LEFT)
camera.rotateAround(new Vector3(0f, 0f, 0f), new Vector3(0f, 1f, 0f), 1f);
if(keycode == Input.Keys.RIGHT)
camera.rotateAround(new Vector3(0f,0f,0f),new Vector3(0f,1f,0f), -1f);
return true;
}
当我尝试执行上面的代码时,我收到以下错误。
线程“LWJGL Application”中的异常java.lang.NullPointerException 在com.badlogic.gdx.graphics.g3d.ModelInstance.invalidate(ModelInstance.java:269) 在com.badlogic.gdx.graphics.g3d.ModelInstance.invalidate(ModelInstance.java:283) 在com.badlogic.gdx.graphics.g3d.ModelInstance.copyNodes(ModelInstance.java:226) 在com.badlogic.gdx.graphics.g3d.ModelInstance。(ModelInstance.java:154) 在com.badlogic.gdx.graphics.g3d.ModelInstance。(ModelInstance.java:146) 在com.badlogic.gdx.graphics.g3d.ModelInstance。(ModelInstance.java:62) 在com.badlogic.gdx.graphics.g3d.ModelInstance。(ModelInstance.java:188) 在com.mygdx.game.desktop.Terrain.create(Terrain.java:63) 在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147) 在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:124)
有人可以提供解决方案以使代码有效。