LIBGDX 3D无法正常工作

时间:2013-07-18 14:06:55

标签: java libgdx

所以我刚刚开始使用LIBGDX的3D部分。我下载了最新的每晚构建,当我按照教程时,我在一行上得到了一个nullPointerException(将在代码中标记)。在行上有一个nullPointer就是这段代码

modelBatch.render(instance);

此行的所有值都在那里。该实例具有其所有属性,而且我的代码中的其他所有内容都是如此。任何想法为什么我得到nullPointerException?提前谢谢。

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.materials.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.materials.Material;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;


public class threeDTest implements ApplicationListener {
public PerspectiveCamera camera;
public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;

    @Override
    public void create() {
        camera =  new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.position.set(10f, 10f, 10f);
        camera.lookAt(0, 0, 0);
        camera.near = 0.1f;
        camera.far = 300f;
        camera.update();

    ModelBuilder modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(5f, 5f, 5f, 
        new Material(ColorAttribute.createDiffuse(Color.GREEN)),
        Usage.Position | Usage.Normal);
    instance = new ModelInstance(model);
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void render() {  
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        modelBatch.begin(camera);//Begin Rendering
        modelBatch.render(instance);<--Null Pointer on this line
        modelBatch.end();//End Rendering
    }

@Override
public void pause() {

}

@Override
    public void resume() {

    }

    @Override
    public void dispose() {
        model.dispose();
    }

}

1 个答案:

答案 0 :(得分:3)

查看代码,您永远不会构造(并处置)modelBatch实例。因此,modelBatch将为null,从而导致您获得NPE。

在create方法中添加以下行:

modelBatch = new ModelBatch();

以及您的配置方法中的以下行:

modelBatch.dispose();