我一直在尝试加载我在LibGDX游戏中制作的3D模型。我已经遵循了许多不同的教程,但我一直收到同样的错误:Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.IndexOutOfBoundsException
这似乎源于if(loading && assets.update())
这是我的代码:
public class Game extends ApplicationAdapter {
private PerspectiveCamera camera;
private ModelBatch modelBatch;
private Environment environment;
private AssetManager assets;
private boolean loading;
private Array<ModelInstance> instances = new Array<ModelInstance>();
@Override
public void create () {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(1f, 1f, 1f);
camera.lookAt(0,0,0);
camera.near = 1f;
camera.far = 300f;
camera.update();
assets = new AssetManager();
assets.load("assets/obj/tablegame.g3db", Model.class);
loading = true;
}
private void doneLoading(){
Model table = assets.get("assets/obj/tablegame.g3db", Model.class);
ModelInstance tableInst = new ModelInstance(table);
instances.add(tableInst);
loading = false;
}
@Override
public void render () {
if (loading && assets.update())
doneLoading();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(camera);
modelBatch.render(instances, environment);
modelBatch.end();
}
@Override
public void dispose () {
modelBatch.dispose();
instances.clear();
assets.dispose();;
}
}
这是完整的错误字符串:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.IndexOutOfBoundsException
at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:579)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:380)
at csgo.casino.game.Game.render(Game.java:65)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Caused by: java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:540)
at java.nio.DirectFloatBufferU.get(DirectFloatBufferU.java:253)
at com.badlogic.gdx.graphics.Mesh.extendBoundingBox(Mesh.java:668)
at com.badlogic.gdx.graphics.Mesh.extendBoundingBox(Mesh.java:605)
at com.badlogic.gdx.graphics.Mesh.calculateBoundingBox(Mesh.java:587)
at com.badlogic.gdx.graphics.g3d.model.MeshPart.update(MeshPart.java:124)
at com.badlogic.gdx.graphics.g3d.Model.convertMesh(Model.java:263)
at com.badlogic.gdx.graphics.g3d.Model.loadMeshes(Model.java:231)
at com.badlogic.gdx.graphics.g3d.Model.load(Model.java:106)
at com.badlogic.gdx.graphics.g3d.Model.<init>(Model.java:102)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadSync(ModelLoader.java:114)
at com.badlogic.gdx.assets.loaders.ModelLoader.loadSync(ModelLoader.java:35)
at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:141)
at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:90)
at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:504)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:378)
... 3 more
有什么想法吗?