Android Open Source - glvideoplayer-android G L Shader Program






From Project

Back to project page glvideoplayer-android.

License

The source code is released under:

Apache License

If you think the Android project glvideoplayer-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.binzume.android.glvideoplayer;
//from   w  ww.  j av a  2s.  c  o  m
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.res.Resources;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;

/**
 * ???????????????
 * 
 * color????
 * 
 * @author kosuke_kawahira
 */
public class GLShaderProgram {

  public int program;

  // ?????
  public int modelMatrixHandle; // ????????
  public int texMatrixHandle;// ????????
  public int positionHandle; // ??
  public int colorHandle; // ?
  public int uvHandle; // UV
  public int texHandle; // ??????
  public int texExtHandle; // ??????

  public float[] projectionMatrix = new float[16];
  public float[] modelViewProjectionMatrix = new float[16];

  public GLShaderProgram(Context context, int vResId, int fResId) {
    makeProgram(loadString(context, vResId), loadString(context, fResId));
  }

  private String loadString(Context context, int resId) {
    try {
      Resources r = context.getResources();
      BufferedReader reader = new BufferedReader(new InputStreamReader(r.openRawResource(resId), "UTF-8"));
      StringBuffer buf = new StringBuffer();
      String str;
      while ((str = reader.readLine()) != null) {
        buf.append(str);
        buf.append("\n");
      }
      return buf.toString();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  // ???????????
  private void makeProgram(String vs, String fs) {
    // ?????????????????
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vs);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fs);

    // ?????????????????
    program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vertexShader);
    GLES20.glAttachShader(program, fragmentShader);
    GLES20.glLinkProgram(program);

    Log.d("", GLES20.glGetShaderInfoLog(vertexShader));
    Log.d("", GLES20.glGetShaderInfoLog(fragmentShader));

    // ?????????
    modelMatrixHandle = GLES20.glGetUniformLocation(program, "u_MMatrix");
    texMatrixHandle = GLES20.glGetUniformLocation(program, "u_TexMatrix");
    positionHandle = GLES20.glGetAttribLocation(program, "a_Position");
    uvHandle = GLES20.glGetAttribLocation(program, "a_UV");
    texHandle = GLES20.glGetUniformLocation(program, "u_Texture");
    texExtHandle = GLES20.glGetUniformLocation(program, "u_TextureEXT");
    colorHandle = GLES20.glGetUniformLocation(program, "u_Color");

    GLES20.glUseProgram(program);
  }

  // ?????????????
  private static int loadShader(int type, String shaderCode) {
    int shader = GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);
    return shader;
  }

  // GL10 compat
  public void glTexCoordPointer(int size, int type, int stride, FloatBuffer pointer) {
    GLES20.glVertexAttribPointer(uvHandle, size, type, false, stride, pointer);
  }

  public void glColor4f(float r, float g, float b, float a) {
    GLES20.glEnableVertexAttribArray(colorHandle);
    GLES20.glUniform4f(colorHandle, r, g, b, a);
  }

  public void glVertexPointer(int size, int type, int stride, FloatBuffer pointer) {
    GLES20.glVertexAttribPointer(positionHandle, size, type, false, stride, pointer);
  }

  public void glEnableClientState(int array) {
    GLES20.glEnableVertexAttribArray(array == GL10.GL_TEXTURE_COORD_ARRAY ? uvHandle : positionHandle);
  }

  public void setProjectionMatrix(float[] mat) {
    System.arraycopy(mat, 0, projectionMatrix, 0, 16);
    System.arraycopy(mat, 0, modelViewProjectionMatrix, 0, 16);
    GLES20.glUniformMatrix4fv(modelMatrixHandle, 1, false, mat, 0);
  }

  public void scale(float scale) {
    Matrix.scaleM(modelViewProjectionMatrix, 0, scale, scale, 1.0f);
    GLES20.glUniformMatrix4fv(modelMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
  }

  public void scale(float scaleX, float scaleY) {
    Matrix.scaleM(modelViewProjectionMatrix, 0, scaleX, scaleY, 1.0f);
    GLES20.glUniformMatrix4fv(modelMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
  }

  public void rotate(float a) {
    Matrix.rotateM(modelViewProjectionMatrix, 0, a, 0, 0, 1.0f);
    GLES20.glUniformMatrix4fv(modelMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
  }

  public void translate(float x, float y) {
    Matrix.translateM(modelViewProjectionMatrix, 0, x, y, 0);
    GLES20.glUniformMatrix4fv(modelMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
  }

  public void resetMatrix() {
    System.arraycopy(projectionMatrix, 0, modelViewProjectionMatrix, 0, 16);
    GLES20.glUniformMatrix4fv(modelMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
  }

}




Java Source Code List

net.binzume.android.glvideoplayer.DeviceCameraPlane.java
net.binzume.android.glvideoplayer.FBOTexture.java
net.binzume.android.glvideoplayer.GLShaderProgram.java
net.binzume.android.glvideoplayer.GLVideoRenderer.java
net.binzume.android.glvideoplayer.MainActivity.java
net.binzume.android.glvideoplayer.MediaPlayerPlane.java
net.binzume.android.glvideoplayer.NicoCommentPlane.java
net.binzume.android.glvideoplayer.OESTexture.java
net.binzume.android.glvideoplayer.TextureFactory.java
net.binzume.android.glvideoplayer.Texture.java
net.binzume.android.glvideoplayer.VideoSurfaceView.java
net.binzume.android.nicoplayer.player.CommentController.java
net.binzume.android.nicoplayer.player.CommentLoadTask.java
net.binzume.android.nicoplayer.player.CommentSlot.java
net.binzume.android.nicovideo.ChannelCategoryInfo.java
net.binzume.android.nicovideo.ChannelInfo.java
net.binzume.android.nicovideo.Comment.java
net.binzume.android.nicovideo.MyList.java
net.binzume.android.nicovideo.NicoSession.java
net.binzume.android.nicovideo.ThreadInfo.java
net.binzume.android.nicovideo.VideoFileInfo.java
net.binzume.android.nicovideo.VideoInfo.java
net.binzume.android.nicovideo.util.HtmlUtil.java
net.binzume.android.nicovideo.util.HttpClient.java
net.binzume.android.nicovideo.webapi.ChannelAPI.java
net.binzume.android.nicovideo.webapi.CommentAPI.java
net.binzume.android.nicovideo.webapi.Constants.java
net.binzume.android.nicovideo.webapi.FriendListAPI.java
net.binzume.android.nicovideo.webapi.LoginAPI.java
net.binzume.android.nicovideo.webapi.MyListAPI.java
net.binzume.android.nicovideo.webapi.NotLoginException.java
net.binzume.android.nicovideo.webapi.PaymentRequiredException.java
net.binzume.android.nicovideo.webapi.VideoAPI.java
net.binzume.android.nicovideo.webapi.VideoRssParser.java
net.binzume.android.nicovideo.webapi.VideoSearchAPI.java
net.binzume.android.nicovideo.webapi.WatchItemAPI.java
net.binzume.android.nicovideo.webapi.WebApiException.java