2

我一直在互联网上拼命寻找解决方案......但我没有比功能更进一步glDrawArrays。我不知道如何使用它,或者它为什么不工作......我想渲染我使用 ObjLoader 加载的模型的多边形线(或顶点线)。相反,它一直向我显示白色边框。

我正在使用 libgdx 1.0.0。

这是我的代码:

public class Teapot extends ApplicationAdapter {
    ModelBatch batch;
    ModelInstance m;
    Camera cam;
    CameraInputController camController;
    Environment environment;

    @Override
    public void create () {
        batch = 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));


        cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.position.set(0, 0, 5f);
        cam.lookAt(0,0,0);
        cam.position.add(0, 2, 0);
        cam.near = 1f;
        cam.far = 100f;
        cam.update();

        camController = new CameraInputController(cam);
        Gdx.input.setInputProcessor(camController);


        Model model = new ObjLoader().loadModel(Gdx.files.internal("teapot.obj"), true);
        m = new ModelInstance(model);
    }

  @Override
  public void render () {
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        Gdx.gl.glDrawArrays(GL20.GL_TRIANGLES, 0, 3);
    batch.begin(cam);
    batch.render(m);
    batch.end();
  }
}

teapot.obj正在使用:http ://groups.csail.mit.edu/graphics/classes/6.837/F03/models/teapot.obj

4

2 回答 2

5

有几种不同的方法可以做到这一点。基本上,问题在于加载的模型带有关于它是哪种模型的信息(三角形,而不是线),并且 ModelBatch 会查看该信息以决定如何绘制它。

最直接的方法是以指定其线条的格式导出模型,然后使用fbx-conv. 我认为不可能将.obj文件指定为行,因此您需要 FBX 或 Collada 之类的东西。

这是我能想到的最简单的方法,可以在不与模型文件混淆的情况下获得快速而肮脏的结果:子类ModelInstance化并覆盖以下方法,如下所示:

    public Renderable getRenderable(final Renderable out, final Node node, 
            final NodePart nodePart) {
        super.getRenderable(out, node, nodePart);
        out.primitiveType = GL20.GL_LINE_STRIP;
        return out;
    }

然后使用这个子类而不是 ModelInstance。这将告诉 ModelBatch 网格是线带网格以使其绘制线条。缺点是它将按照它们在文件中出现的顺序连接所有顶点,因此您会在模型的某些地方出现一些意想不到的额外线条。你可以GL_LINES改用,但是你会有一堆缺失的线段。

于 2014-05-05T02:42:00.523 回答
2

您还可以使用着色器设置原始类型,然后将其提供给 modelBatch 实例:

    modelBatch = new ModelBatch(new DefaultShaderProvider() {
        @Override
        protected Shader createShader(Renderable renderable) {
            return new WireframeShader(renderable, config);
        }
    });

线框着色器.java:

public class WireframeShader extends DefaultShader {

    public static final int PRIMITIVE_TYPE = GL20.GL_LINE_STRIP;
    private int mSavedPrimitiveType;

    public WireframeShader(Renderable renderable) {
        super(renderable);
    }

    public WireframeShader(Renderable renderable, Config config) {
        super(renderable, config);
    }

    public WireframeShader(Renderable renderable, Config config, String prefix) {
        super(renderable, config, prefix);
    }

    public WireframeShader(Renderable renderable, Config config, String prefix, String vertexShader, String fragmentShader) {
        super(renderable, config, prefix, vertexShader, fragmentShader);
    }

    public WireframeShader(Renderable renderable, Config config, ShaderProgram shaderProgram) {
        super(renderable, config, shaderProgram);
    }

    @Override
    public void render(Renderable renderable) {
        setPrimitiveType(renderable);
        super.render(renderable);
        restorePrimitiveType(renderable);
    }

    @Override
    public void render(Renderable renderable, Attributes combinedAttributes) {
        setPrimitiveType(renderable);
        super.render(renderable, combinedAttributes);
        restorePrimitiveType(renderable);
    }

    private void restorePrimitiveType(Renderable renderable) {
        renderable.primitiveType = mSavedPrimitiveType; // gdxVersion = '1.6.4'

        //consider using the following for newer gdx versions instead as per kami comment:
        //renderable.meshPart.primitiveType = mSavedPrimitiveType

    }

    private void setPrimitiveType(Renderable renderable) {
        mSavedPrimitiveType = renderable.primitiveType; //gdxVersion = '1.6.4'
        renderable.primitiveType = PRIMITIVE_TYPE; //gdxVersion = '1.6.4'


        //consider using the following for newer gdx versions instead as per kami comment:
        //mSavedPrimitiveType = renderable.meshPart.primitiveType;
        //renderable.meshPart.primitiveType = PRIMITIVE_TYPE;

    }
}

这将使用线条而不是填充三角形来渲染模型

        modelBatch.begin(EnvironmentWrapper.single().getCamera());
        modelBatch.render(renderables);
        modelBatch.end();
于 2015-09-21T14:15:19.717 回答