使用OpenGL显示一张图片。
工程源码:https://github.com/sunxiaolin2016/OpenglOne
GLImageHandler
GLImageHandler中封装了图片显示的方法.
- NO_FILTER_VERTEX_SHADER和NO_FILTER_FRAGMENT_SHADER 分别是顶点着色器程式和片段着色器程式。
- init(),OpenGL初始化操作,创建着色器程式。初始化顶点坐标,图片纹理,纹理坐标变量。
- onDraw(),根据顶点坐标,图片纹理和纹理坐标,使用GL_TRIANGLE_STRIP,顶点法,绘制图片。
/**
* 负责显示一张图片
*/
public class GLImageHandler {
// 数据中有多少个顶点,管线就调用多少次顶点着色器
public static final String NO_FILTER_VERTEX_SHADER = "" +
"attribute vec4 position;\n" + // 顶点着色器的顶点坐标,由外部程序传入
"attribute vec4 inputTextureCoordinate;\n" + // 传入的纹理坐标
" \n" +
"varying vec2 textureCoordinate;\n" +
" \n" +
"void main()\n" +
"{\n" +
" gl_Position = position;\n" +
" textureCoordinate = inputTextureCoordinate.xy;\n" + // 最终顶点位置
"}";
// 光栅化后产生了多少个片段,就会插值计算出多少个varying变量,同时渲染管线就会调用多少次片段着色器
public static final String NO_FILTER_FRAGMENT_SHADER = "" +
"varying highp vec2 textureCoordinate;\n" + // 最终顶点位置,上面顶点着色器的varying变量会传递到这里
" \n" +
"uniform sampler2D inputImageTexture;\n" + // 外部传入的图片纹理 即代表整张图片的数据
" \n" +
"void main()\n" +
"{\n" +
" gl_FragColor = texture2D(inputImageTexture, textureCoordinate);\n" + // 调用函数 进行纹理贴图
"}";
private final LinkedList<Runnable> mRunOnDraw;
private final String mVertexShader;
private final String mFragmentShader;
protected int mGLProgId;
protected int mGLAttribPosition;
protected int mGLUniformTexture;
protected int mGLAttribTextureCoordinate;
public GLImageHandler() {
this(NO_FILTER_VERTEX_SHADER, NO_FILTER_FRAGMENT_SHADER);
}
public GLImageHandler(final String vertexShader, final String fragmentShader) {
mRunOnDraw = new LinkedList<Runnable>();
mVertexShader = vertexShader;
mFragmentShader = fragmentShader;
}
public final void init() {
mGLProgId = OpenGLUtils.loadProgram(mVertexShader, mFragmentShader); // 编译链接着色器,创建着色器程序
mGLAttribPosition = GLES20.glGetAttribLocation(mGLProgId, "position"); // 顶点着色器的顶点坐标
mGLUniformTexture = GLES20.glGetUniformLocation(mGLProgId, "inputImageTexture"); // 传入的图片纹理
mGLAttribTextureCoordinate = GLES20.glGetAttribLocation(mGLProgId, "inputTextureCoordinate"); // 顶点着色器的纹理坐标
}
public void onDraw(final int textureId, final FloatBuffer cubeBuffer,
final FloatBuffer textureBuffer) {
GLES20.glUseProgram(mGLProgId);
// 顶点着色器的顶点坐标
cubeBuffer.position(0);
GLES20.glVertexAttribPointer(mGLAttribPosition, 2, GLES20.GL_FLOAT, false, 0, cubeBuffer);
GLES20.glEnableVertexAttribArray(mGLAttribPosition);
// 顶点着色器的纹理坐标
textureBuffer.position(0);
GLES20.glVertexAttribPointer(mGLAttribTextureCoordinate, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
GLES20.glEnableVertexAttribArray(mGLAttribTextureCoordinate);
// 传入的图片纹理
if (textureId != OpenGLUtils.NO_TEXTURE) {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
GLES20.glUniform1i(mGLUniformTexture, 0);
}
// 绘制顶点 ,方式有顶点法和索引法
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); // 顶点法,按照传入渲染管线的顶点顺序及采用的绘制方式将顶点组成图元进行绘制
GLES20.glDisableVertexAttribArray(mGLAttribPosition);
GLES20.glDisableVertexAttribArray(mGLAttribTextureCoordinate);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
}
}
GLShowImageActivity
- CUBE[]定义了原始的顶点坐标,顶点坐标一般是固定(相当于一张画布)。
- TEXTURE_NO_ROTATION[]定义了纹理坐标,纹理坐标决定了图片显示的区域和角度(在画布中显示)。
- MyRender()类中实现OpenGL的绘制。
- onSurfaceCreated()中,loadTexture()加载图片纹理,初始化顶点坐标缓冲区和纹理坐标缓冲区。
- onSurfaceChanged()中,初始化图片大小,和画布大小。
- onDrawFrame()中,调用GLImageHandler中的onDraw进行图片绘制。
public class GLShowImageActivity extends Activity {
// 绘制图片的原理:定义一组矩形区域的顶点,然后根据纹理坐标把图片作为纹理贴在该矩形区域内。
// 原始的矩形区域的顶点坐标,因为后面使用了顶点法绘制顶点,所以不用定义绘制顶点的索引。无论窗口的大小为多少,在OpenGL二维坐标系中都是为下面表示的矩形区域
static final float CUBE[] = { // 窗口中心为OpenGL二维坐标系的原点(0,0)
-1.0f, -1.0f, // v1
1.0f, -1.0f, // v2
-1.0f, 1.0f, // v3
1.0f, 1.0f, // v4
};
// 纹理也有坐标系,称UV坐标,或者ST坐标。UV坐标定义为左上角(0,0),右下角(1,1),一张图片无论大小为多少,在UV坐标系中都是图片左上角为(0,0),右下角(1,1)
// 纹理坐标,每个坐标的纹理采样对应上面顶点坐标。
public static final float TEXTURE_NO_ROTATION[] = {
0.0f, 1.0f, // v1
1.0f, 1.0f, // v2
0.0f, 0.0f, // v3
1.0f, 0.0f, // v4
};
private GLSurfaceView mGLSurfaceView;
private int mGLTextureId = OpenGLUtils.NO_TEXTURE; // 纹理id
private GLImageHandler mGLImageHandler = new GLImageHandler();
private FloatBuffer mGLCubeBuffer;
private FloatBuffer mGLTextureBuffer;
private int mOutputWidth, mOutputHeight; // 窗口大小
private int mImageWidth, mImageHeight; // bitmap图片实际大小
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glshow_image);
mGLSurfaceView = findViewById(R.id.gl_surfaceview);
mGLSurfaceView.setEGLContextClientVersion(2); // 创建OpenGL ES 2.0 的上下文环境
mGLSurfaceView.setRenderer(new MyRender());
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // 手动刷新
}
private class MyRender implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0, 0, 0, 1);
GLES20.glDisable(GLES20.GL_DEPTH_TEST); // 当我们需要绘制透明图片时,就需要关闭它
mGLImageHandler.init();
// 需要显示的图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.o);
mImageWidth = bitmap.getWidth();
mImageHeight = bitmap.getHeight();
// 把图片数据加载进GPU,生成对应的纹理id
mGLTextureId = OpenGLUtils.loadTexture(bitmap, mGLTextureId, true); // 加载纹理
// 顶点数组缓冲器
mGLCubeBuffer = ByteBuffer.allocateDirect(CUBE.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
mGLCubeBuffer.put(CUBE).position(0);
// 纹理数组缓冲器
mGLTextureBuffer = ByteBuffer.allocateDirect(TEXTURE_NO_ROTATION.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
mGLTextureBuffer.put(TEXTURE_NO_ROTATION).position(0);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
mOutputWidth = width;
mOutputHeight = height;
GLES20.glViewport(0, 0, width, height); // 设置窗口大小
adjustImageScaling(); // 调整图片显示大小。如果不调用该方法,则会导致图片整个拉伸到填充窗口显示区域
}
@Override
public void onDrawFrame(GL10 gl) { // 绘制
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// 根据纹理id,顶点和纹理坐标数据绘制图片
mGLImageHandler.onDraw(mGLTextureId, mGLCubeBuffer, mGLTextureBuffer);
}
// 调整图片显示大小为居中显示
private void adjustImageScaling() {
float outputWidth = mOutputWidth;
float outputHeight = mOutputHeight;
float ratio1 = outputWidth / mImageWidth;
float ratio2 = outputHeight / mImageHeight;
float ratioMax = Math.min(ratio1, ratio2);
// 居中后图片显示的大小
int imageWidthNew = Math.round(mImageWidth * ratioMax);
int imageHeightNew = Math.round(mImageHeight * ratioMax);
// 图片被拉伸的比例
float ratioWidth = outputWidth / imageWidthNew;
float ratioHeight = outputHeight / imageHeightNew;
// 根据拉伸比例还原顶点
float[] cube = new float[]{
CUBE[0] / ratioWidth, CUBE[1] / ratioHeight,
CUBE[2] / ratioWidth, CUBE[3] / ratioHeight,
CUBE[4] / ratioWidth, CUBE[5] / ratioHeight,
CUBE[6] / ratioWidth, CUBE[7] / ratioHeight,
};
mGLCubeBuffer.clear();
mGLCubeBuffer.put(cube).position(0);
}
}
}
OpenGLUtils
public class OpenGLUtils {
public static final int NO_TEXTURE = -1;
public static void deleteTexture(int textureID) {
GLES20.glDeleteTextures(1, new int[]{
textureID
}, 0);
}
public static int loadTexture(final Bitmap img, final int usedTexId) {
return loadTexture(img, usedTexId, true);
}
public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) {
int textures[] = new int[]{-1};
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
//纹理也有坐标系,称UV坐标,或者ST坐标
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT); // S轴的拉伸方式为重复,决定采样值的坐标超出图片范围时的采样方式
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT); // T轴的拉伸方式为重复
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
textures[0] = usedTexId;
}
if (recycle) {
img.recycle();
}
return textures[0];
}
public static int loadTexture(final IntBuffer data, final Size size, final int usedTexId) {
int textures[] = new int[1];
if (usedTexId == NO_TEXTURE) {
GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, size.width, size.height,
0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data);
} else {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, size.width,
size.height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data);
textures[0] = usedTexId;
}
return textures[0];
}
public static int loadTextureAsBitmap(final IntBuffer data, final Size size, final int usedTexId) {
Bitmap bitmap = Bitmap
.createBitmap(data.array(), size.width, size.height, Config.ARGB_8888);
return loadTexture(bitmap, usedTexId);
}
/**
* 加载着色器
* @param strSource
* @param iType
* @return
*/
public static int loadShader(final String strSource, final int iType) {
int[] compiled = new int[1];
int iShader = GLES20.glCreateShader(iType);
GLES20.glShaderSource(iShader, strSource);
GLES20.glCompileShader(iShader);
GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
Log.d("Load Shader Failed", "Compilation\n" + GLES20.glGetShaderInfoLog(iShader));
return 0;
}
return iShader;
}
/**
* 加载着色器程序
* @param strVSource
* @param strFSource
* @return
*/
public static int loadProgram(final String strVSource, final String strFSource) {
int iVShader;
int iFShader;
int iProgId;
iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER); // 顶点着色器
if (iVShader == 0) {
Log.d("Load Program", "Vertex Shader Failed");
return 0;
}
iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER); // 片元着色器
if (iFShader == 0) {
Log.d("Load Program", "Fragment Shader Failed");
return 0;
}
iProgId = GLES20.glCreateProgram();
GLES20.glAttachShader(iProgId, iVShader);
GLES20.glAttachShader(iProgId, iFShader);
GLES20.glLinkProgram(iProgId);
// 获取program的链接情况
int[] link = new int[1];
GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0);
if (link[0] <= 0) {
Log.d("Load Program", "Linking Failed");
return 0;
}
GLES20.glDeleteShader(iVShader);
GLES20.glDeleteShader(iFShader);
return iProgId;
}
public static float rnd(final float min, final float max) {
float fRandNum = (float) Math.random();
return min + (max - min) * fRandNum;
}
}
本文深入解析OpenGL中如何显示一张图片,从着色器程序的编写到纹理映射,再到顶点坐标与纹理坐标的匹配,全面阐述图片显示的原理与实践。
5324

被折叠的 条评论
为什么被折叠?



