Hello I am learning now OpenGL ES and the use of shaders. I have
written a test class that should only paint the application icon into
the screen. So far no luck. Can anyone see what I am doing wrong or
maybe point me to the right direction?

public class GLTester
{
  final int FLOAT_SIZE_BYTES = 4;
  int glProgramHandle = 0;
  int glVertexShaderHandle = 0;
  int glFragmentShaderHandle = 0;
  int glPositionHandle = 0;
  int glTextCoordHandle = 0;
  int glTextureHandle  = 0;
  int frameTextureHandle = 0;

  FloatBuffer vertexBuffer = null;
  final float squareVertices[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f,
0.0f,
                                                  -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f };

  FloatBuffer textureBuffer = null;
  final float textureVertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f,
                                                 1.0f, 1.0f };

  final String vertexShaderCode    = "attribute vec4 a_position;
  attribute vec2 a_texcoord; varying vec2 v_texcoord; void main()
  {v_texcoord = a_texcoord; gl_Position = a_position;}";

  final String fragmentShaderCode = "precision mediump float; varying
  vec2 v_texcoord; uniform sampler2D u_texture; void main()
  {gl_FragColor = texture2D(u_texture, v_texcoord.st);}";

  Bitmap bitmap = null;


void test(final Context context)
{
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inScaled = false;
  bitmap = BitmapFactory.decodeResource(context.getResources(),
 
R.drawable.icon, options);

  setupGLES();
  createProgram();
  setupTexture();
  draw();
}

void setupGLES()
{
  GLES20.glEnable(GLES20.GL_TEXTURE_2D);
  GLES20.glEnable(GLES20.GL_CULL_FACE);
  GLES20.glCullFace(GLES20.GL_BACK);
  GLES20.glDisable(GLES20.GL_DEPTH_TEST);
  GLES20.glViewport(0, 0, 400, 800);
  GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

void createProgram()
{
  glProgramHandle = GLES20.glCreateProgram();

  glVertexShaderHandle = setupShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);

  glFragmentShaderHandle = setupShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);

  GLES20.glLinkProgram(glProgramHandle);

  glPositionHandle = GLES20.glGetAttribLocation(glProgramHandle,
"a_position");

  glTextCoordHandle = GLES20.glGetAttribLocation(glProgramHandle,
"a_texcoord");

  glTextureHandle = GLES20.glGetUniformLocation(glProgramHandle,
"u_texture");

  vertexBuffer = ByteBuffer.allocateDirect(squareVertices.length *
  FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
  vertexBuffer.put(squareVertices).position(0);

  textureBuffer = ByteBuffer.allocateDirect(textureVertices.length *
  FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
  textureBuffer.put(textureVertices).position(0);
}

int setupShader(final int type, final String code)
{
  int shaderHandle = GLES20.glCreateShader(type);
  GLES20.glShaderSource(shaderHandle, code);
  GLES20.glCompileShader(shaderHandle);
  GLES20.glAttachShader(glProgramHandle, shaderHandle);
  return shaderHandle;
}

void setupTexture()
{
  int texture[] = new int[1];
  GLES20.glGenTextures(1, texture, 0);
  frameTextureHandle = texture[0];
  texture = null;
  GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  GLES20.glBindTexture(GL10.GL_TEXTURE_2D, frameTextureHandle);
  GLES20.glUniform1i(glTextureHandle, 0);
  GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
  GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
  GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
  GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
}

void draw()
{
  GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  glUseProgram(glProgramHandle);

  vertexBuffer.position(0);
  GLES20.glVertexAttribPointer(glPositionHandle, 3, GLES20.GL_FLOAT,
                                                   false, 0,
vertexBuffer);
  GLES20.glEnableVertexAttribArray(glPositionHandle);

  textureBuffer.position(0);
  GLES20.glEnableVertexAttribArray(glTextCoordHandle);
  GLES20.glVertexAttribPointer(glTextCoordHandle, 2, GLES20.GL_FLOAT,
                                                   false, 0,
textureBuffer);

  GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameTextureHandle);
  GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
  GLES20.glUniform1i(glTextureHandle, 0);

  GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0,
                                        squareVertices.length / 3);

  GLES20.glDisableVertexAttribArray(glPositionHandle);
  GLES20.glDisableVertexAttribArray(glTextCoordHandle);
  GLES20.glDisableVertexAttribArray(glTextureHandle);
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to