我来自 FlashBuilder、Flex、Flash 开发领域,正在努力研究如何通过 LibGDX 进入 Android 开发。
我已经完成了环境设置,学习了一些基础知识,但是现在我在研究如何渲染带有纹理的平面时遇到了麻烦。
我发现的大多数演示都是 OpenGL ES2.0,但我需要我的应用程序在两者上运行,所以我尝试寻找有关创建纹理平面的 OpenGL ES1.1 教程,但最终徘徊了几个小时。所以如果有人有任何建议?代码示例?教程?请告诉我。会很棒。
这是到目前为止我得到的代码,但没有显示任何内容(libgdx 演示图像已被注释掉,因此我可以查看它是否呈现任何内容但没有):
public class MyGdxGame implements ApplicationListener {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private ShaderProgram sp;
private Mesh mesh;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
mesh = new Mesh(true, 3, 3, new VertexAttribute(Usage.Position, 3,"a_position"));
mesh.setVertices(new float[] { 100f, 100f, 0,
400f, 100f, 0,
250, 400f, 0 });
mesh.setIndices(new short[] { 0, 1, 2 });
tex = new Texture(Gdx.files.internal("data/libgdx.png"));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private Texture tex;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Override
public void render() {
// Set bg
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);
Gdx.gl10.glLoadIdentity();
Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);
Gdx.gl10.glLoadIdentity();
mesh.render(GL10.GL_TRIANGLES, 0, 3);
batch.begin();
//sprite.draw(batch);
//mesh.render(sp, GL10.GL_TRIANGLES);
batch.draw(tex, 10, 10);
batch.end();
// Results in white window output only...
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// From a previous tutorial attempt didn't show anything
public Mesh createFullScreenQuad() {
float[] verts = new float[20];
int i = 0;
verts[i++] = -1; // x1
verts[i++] = -1; // y1
verts[i++] = 0;
verts[i++] = 0f; // u1
verts[i++] = 0f; // v1
verts[i++] = 1f; // x2
verts[i++] = -1; // y2
verts[i++] = 0;
verts[i++] = 1f; // u2
verts[i++] = 0f; // v2
verts[i++] = 1f; // x3
verts[i++] = 1f; // y2
verts[i++] = 0;
verts[i++] = 1f; // u3
verts[i++] = 1f; // v3
verts[i++] = -1; // x4
verts[i++] = 1f; // y4
verts[i++] = 0;
verts[i++] = 0f; // u4
verts[i++] = 1f; // v4
Mesh mesh = new Mesh( true, 4, 0, // static mesh with 4 vertices and no indices
new VertexAttribute( Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE ),
new VertexAttribute( Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE+"0" ) );
mesh.setVertices( verts );
return mesh;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Override
public void resize(int width, int height) {
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Override
public void pause() {
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Override
public void resume() {
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}