Got a copy of 'OpenGL ES 2.0 Programming Guide' from Amazon (seems good so far) and I'm trying to get my head around GLES2 starting with reworking the 'MyFirstTriangle' tutorial. Should be easy eh?... but I'm not having much luck. All I get is a black screen...
- Code: Select all
package com.dustypixels.games;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.GdxRuntimeException;
public class MyFirstTriangle implements ApplicationListener {
private ShaderProgram shaderProgram;
private Mesh mesh;
@Override
public void create() {
Gdx.app.log("GDX", "create...");
//check we can use GLES2
if (!Gdx.app.getGraphics().isGL20Available()) {
throw new GdxRuntimeException("GLES2 Not Avialable!");
}
//create shader program
String fragmentShader =
"attribute vec4 vPosition; \n" +
"void main() \n" +
"{ \n" +
" gl_Position = vPosition; \n" +
"} \n";
String vertexShader =
"precision mediump float; \n"+
"void main() \n"+
"{ \n"+
" gl_FragColor = vec4(1.0,0.0,0.0,1.0); \n"+
"} \n";
shaderProgram = new ShaderProgram(vertexShader, fragmentShader);
mesh = new Mesh(true, 3, 3,
new VertexAttribute(VertexAttributes.Usage.Position, 3, "vPosition"));
mesh.setVertices(new float[] { -0.5f, -0.5f, 0,
0.5f, -0.5f, 0,
0, 0.5f, 0 });
mesh.setIndices(new short[] { 0, 1, 2 });
}
@Override
public void dispose() {
shaderProgram.dispose();
mesh.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.app.log("GDX", "render...");
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
//draw
shaderProgram.begin();
mesh.render(shaderProgram, GL20.GL_TRIANGLES);
shaderProgram.end();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
Mesh.invalidateAllMeshes();
}
}
I'm sure its something really stupid, but any help appreciated.
By the way, are there any tests or demos using GLES2? I couldn't find any in SVN... when I get this working I'll be happy to add it to the Wiki as a tutorial.
Cheers.

