我一直在拼命寻找互联网上的解决方案...但我没有比函数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
答案 0 :(得分:5)
有几种不同的方法可以做到这一点。基本上,问题是加载的模型带有关于它是什么类型的模型的信息(三角形,而不是线条),而ModelBatch会查看该信息以决定如何绘制它。
最直接的方法是以指定其行的格式导出模型,然后使用{{3将其转换为libgdx的原生格式(G3D) }}。我不认为可以将.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
,但是你会有一堆缺失的线段。
答案 1 :(得分:1)
您还可以使用着色器设置基本类型,然后将其提供给modelBatch实例:
modelBatch = new ModelBatch(new DefaultShaderProvider() {
@Override
protected Shader createShader(Renderable renderable) {
return new WireframeShader(renderable, config);
}
});
WireframeShader.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();