package com.zhou.android.opengl;
import android.opengl.GLES20;
import android.util.Log;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* step to use:<br/>
* 1. new GLProgram()<br/>
* 2. buildProgram()<br/>
* 3. buildTextures()<br/>
* 4. drawFrame()<br/>
*/
public class GLProgram {
private String TAG = "open_gl";
// program id
private int _program;
// window position
public final int mWinPosition;
// texture id
private int _textureI;
private int _textureII;
private int _textureIII;
// texture index in gles
private int _tIindex;
private int _tIIindex;
private int _tIIIindex;
// vertices on screen
private float[] _vertices;
// handles
private int _positionHandle = -1, _coordHandle = -1;
private int _yhandle = -1, _uhandle = -1, _vhandle = -1;
private int _ytid = -1, _utid = -1, _vtid = -1;
// vertices buffer
private ByteBuffer _vertice_buffer;
private ByteBuffer _coord_buffer;
// video width and height
private int _video_width = -1;
private int _video_height = -1;
// flow control
private boolean isProgBuilt = false;
/**
* position can only be 0~4:<br/>
* fullscreen => 0<br/>
* left-top => 1<br/>
* right-top => 2<br/>
* left-bottom => 3<br/>
* right-bottom => 4
*/
public GLProgram(int position) {
if (position < 0 || position > 4) {
throw new RuntimeException("Index can only be 0 to 4");
}
mWinPosition = position;
setup(mWinPosition);
}
/**
* prepared for later use
*/
public void setup(int position) {
switch (mWinPosition) {
case 1:
_vertices = squareVertices1;
_textureI = GLES20.GL_TEXTURE0;
_textureII = GLES20.GL_TEXTURE1;
_textureIII = GLES20.GL_TEXTURE2;
_tIindex = 0;
_tIIindex = 1;
_tIIIindex = 2;
break;
case 2:
_vertices = squareVertices2;
_textureI = GLES20.GL_TEXTURE3;
_textureII = GLES20.GL_TEXTURE4;
_textureIII = GLES20.GL_TEXTURE5;
_tIindex = 3;
_tIIindex = 4;
_tIIIindex = 5;
break;
case 3:
_vertices = squareVertices3;
_textureI = GLES20.GL_TEXTURE6;
_textureII = GLES20.GL_TEXTURE7;
_textureIII = GLES20.GL_TEXTURE8;
_tIindex = 6;
_tIIindex = 7;
_tIIIindex = 8;
break;
case 4:
_vertices = squareVertices4;
_textureI = GLES20.GL_TEXTURE9;
_textureII = GLES20.GL_TEXTURE10;
_textureIII = GLES20.GL_TEXTURE11;
_tIindex = 9;
_tIIindex = 10;
_tIIIindex = 11;
break;
case 0:
default:
_vertices = squareVertices;
_textureI = GLES20.GL_TEXTURE0;
_textureII = GLES20.GL_TEXTURE1;
_textureIII = GLES20.GL_TEXTURE2;
_tIindex = 0;
_tIIindex = 1;
_tIIIindex = 2;
break;
}
}
public boolean isProgramBuilt() {
return isProgBuilt;
}
public void buildProgram() {
// TODO createBuffers(_vertices, coordVertices);
if (_program <= 0) {
_program = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
}
Log.d(TAG, "_program = " + _program);
/*
* get handle for "vPosition" and "a_texCoord"
*/
_positionHandle = GLES20.glGetAttribLocation(_program, "vPosition");
Log.d(TAG, "_positionHandle = " + _positionHandle);
checkGlError("glGetAttribLocation vPosition");
if (_positionHandle == -1) {
throw new RuntimeException("Could not get attribute location for vPosition");
}
_coordHandle = GLES20.glGetAttribLocation(_program, "a_texCoord");
Log.d(TAG, "_coordHandle = " + _coordHandle);
checkGlError("glGetAttribLocation a_texCoord");
if (_coordHandle == -1) {
throw new RuntimeException("Could not get attribute location for a_texCoord");
}
/*
* get uniform location for y/u/v, we pass data through these uniforms
*/
_yhandle = GLES20.glGetUniformLocation(_program, "tex_y");
Log.d(TAG, "_yhandle = " + _yhandle);
checkGlError("glGetUniformLocation tex_y");
if (_yhandle == -1) {
throw new RuntimeException("Could not get uniform location for tex_y");
}
_uhandle = GLES20.glGetUniformLocation(_program, "tex_u");
Log.d(TAG, "_uhandle = " + _uhandle);
checkGlError("glGetUniformLocation tex_u");
if (_uhandle == -1) {
throw new RuntimeException("Could not get uniform location for tex_u");
}
_vhandle = GLES20.glGetUniformLocation(_program, "tex_v");
Log.d(TAG, "_vhandle = " + _vhandle);
checkGlError("glGetUniformLocation tex_v");
if (_vhandle == -1) {
throw new RuntimeException("Could not get uniform location for tex_v");
}
isProgBuilt = true;
}
/**
* build a set of textures, one for R, one for G, and one for B.
*/
public void buildTextures(Buffer y, Buffer u, Buffer v, int width, int height) {
boolean videoSizeChanged = (width != _video_width || height != _video_height);
if (videoSizeChanged) {
_video_width = width;
_video_height = height;
Log.d(TAG, "buildTextures videoSizeChanged: w=" + _video_width + " h=" + _video_height);
}
// building texture for Y data
if (_ytid < 0 || videoSizeChanged) {
if (_ytid >= 0) {
Log.d(TAG, "glDeleteTextures Y");
GLES20.glDeleteTextures(1, new int[]{_ytid}, 0);
checkGlError("glDeleteTextures");
}
// GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
checkGlError("glGenTextures");
_ytid = textures[0];
Log.d(TAG, "glGenTextures Y = " + _ytid);
}
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, _ytid);
checkGlError("glBindTexture");
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, _video_width, _video_height, 0,
GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, y);
checkGlError("glTexImage2D");
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
// building texture for U data
if (_utid < 0 || videoSizeChanged) {
if (_utid >= 0) {
Log.d(TAG, "glDeleteTextures U");
GLES20.glDeleteTextures(1, new int[]{_utid}, 0);
checkGlError("glDeleteTextures");
}
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
checkGlError("glGenTextures");
_utid = textures[0];
Log.d(TAG, "glGenTextures U = " + _utid);
}
GLES20.glBindT
com.zhou.android.opengl
需积分: 50 112 浏览量
2018-11-04
22:44:09
上传
评论
收藏 5KB ZIP 举报
__xiaozhou
- 粉丝: 44
- 资源: 6
最新资源
- 奥迪A8D4仪表开机报文
- apache-skywalking-java-agent 8.8.0-9.2.0,一共12个版本
- DTS7001-VB一款SOT23封装P-Channel场效应MOS管
- ZTE-C300 OLT常用命令
- 6666666666666666
- DTS6410-VB一款SOT23封装N-Channel场效应MOS管
- SpyGlass-PowerVerifyMethodology-GuideWare2.0-UserGuide
- DTS6401-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 基于树莓派、OpenCV及Python语言的人脸识别-i
- 游戏开发 UE蓝图 分支(Branch)节点和源码.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈