本文主要演示OpenGL ES 3.0 纹理演示。接口大部分和2.0没什么区别,脚本稍微有了点变化而已。

扩展GLSurfaceView

package com.example.gles300;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log; /**
*
* @author gaofeng
*
*/
public class MyGLSurfaceView extends GLSurfaceView { private GLRenderer renderer; /**
* @param context
*/
public MyGLSurfaceView(Context context) {
super(context);
init();
} /**
* @param context
* @param attrs
*/
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} private void init() {
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo info = am.getDeviceConfigurationInfo();
String v = info.getGlEsVersion(); //判断是否为3.0 ,一般4.4就开始支持3.0版本了。
if (v.equalsIgnoreCase("3.0")) {
setEGLContextClientVersion(3);
} else {
setEGLContextClientVersion(2);
}
renderer = new GLRenderer();
renderer.setContext(getContext());
setRenderer(renderer);
setRenderMode(RENDERMODE_CONTINUOUSLY);
}
}

  

package com.example.gles300;

import java.io.IOException;
import java.io.InputStream; import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.util.Log; /**
* @author gaofeng
*
*/
public class GLRenderer implements Renderer { public static float[] projMatrix = new float[16];// 投影
public static float[] viewMatrix = new float[16];// 相机
public static float[] mViewPjMatrix;// 总变换矩阵
public static float[] matrixs = new float[16];
public static int textureId = -1;
Context context;
MyDrawModel drawModel; public void setContext(Context context) {
this.context = context;
} public GLRenderer() {
} @Override
public void onDrawFrame(GL10 arg0) {
GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
Log.e("", "textureId:" + textureId);
drawModel.drawFrame(textureId);
} @Override
public void onSurfaceChanged(GL10 arg0, int w, int h) {
GLES30.glViewport(0, 0, w, h);
float ratio = (float) w / h;
Matrix.frustumM(projMatrix, 0, -ratio, ratio, -1, 1, 1, 10);//投影矩阵设置
Matrix.setLookAtM(viewMatrix, 0, 0, 0, 3, 0, 0, 0, 0.0f, 1.0f, 0.0f);//摄像机坐标设置
} @Override
public void onSurfaceCreated(GL10 g, EGLConfig eglConfig) {
GLES30.glClearColor(0.5f,0.5f,0.5f, 1.0f);
GLES30.glEnable(GLES30.GL_DEPTH_TEST);
InputStream ins = null;
drawModel = new MyDrawModel();
drawModel.init();
try {
ins = context.getAssets().open("girl4.jpg");
textureId = createTexture(ins);
Log.e("", "textureId:" + textureId);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ins != null) {
ins.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
GLES30.glDisable(GLES30.GL_CULL_FACE);
} public static int createTexture(InputStream ins) {
int[] textures = new int[1];
GLES30.glGenTextures(1, textures, 0);
int textureId = textures[0];
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER,GLES30.GL_NEAREST);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D,GLES30.GL_TEXTURE_MAG_FILTER,GLES30.GL_LINEAR);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S,GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T,GLES30.GL_CLAMP_TO_EDGE);
//上面是纹理贴图的取样方式,包括拉伸方式,取临近值和线性值
Bitmap bitmap = BitmapFactory.decodeStream(ins);
GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bitmap, 0);//让图片和纹理关联起来,加载到OpenGl空间中
Log.d("OPENGL","bitmap:" + bitmap);
bitmap.recycle();//不需要,可以释放
return textureId;
}
}

  

package com.example.gles300;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer; import android.opengl.GLES30;
import android.opengl.Matrix;
import android.util.Log; /**
* @author gaofeng
*
*/
public class MyDrawModel {

private int programId;
private int mVPMatrixHandle;
private FloatBuffer vertexBuffer;
private FloatBuffer texCoorBuffer; public MyDrawModel() {
} public void init() {
initData();
int vertexsharder = GLHelper.compileScript(GLES30.GL_VERTEX_SHADER, GLScript.vertex3);
int fragmentsharder = GLHelper.compileScript(GLES30.GL_FRAGMENT_SHADER, GLScript.fragment3);
programId = GLHelper.linkAttach(vertexsharder, fragmentsharder);
boolean isOK = GLHelper.checkProgram(programId);
mVPMatrixHandle = GLES30.glGetUniformLocation(programId, "uMVPMatrix");
} private void initData() {
//X,Y,Z 顶点
float vertices[] = new float[] {
0, 0, 0,
-1.8f, -1f, 0,
1.8f, -1f, 0,
1.8f, 1f, 0,
-1.8f, 1f, 0,
-1.8f, -1f, 0
}; ByteBuffer vb = ByteBuffer.allocateDirect(vertices.length * 4);
vb.order(ByteOrder.nativeOrder());
vertexBuffer = vb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0); //纹理空间坐标UV
float texCoor[] = new float[] {
0.5f, 0.5f,
0f, 1f,
1f, 1f,
1f, 0f,
0f, 0f,
0f, 1f
}; ByteBuffer cb = ByteBuffer.allocateDirect(texCoor.length * 4);
cb.order(ByteOrder.nativeOrder());
texCoorBuffer = cb.asFloatBuffer();
texCoorBuffer.put(texCoor);
texCoorBuffer.position(0);
} public void drawFrame(int textureId) { GLES30.glUseProgram(programId); // // 初始化矩阵
Matrix.setRotateM(GLRenderer.matrixs, 0, 0, 1, 0, 0);
Matrix.translateM(GLRenderer.matrixs, 0, 0, 0, 1); //矩阵转换 ,投影矩阵,摄像机矩阵,模型矩阵
GLRenderer.mViewPjMatrix = new float[16];
Matrix.multiplyMM(GLRenderer.mViewPjMatrix, 0, GLRenderer.viewMatrix,0, GLRenderer.matrixs, 0);
Matrix.multiplyMM(GLRenderer.mViewPjMatrix, 0, GLRenderer.projMatrix,0, GLRenderer.mViewPjMatrix, 0);
GLES30.glUniformMatrix4fv(mVPMatrixHandle, 1, false, GLRenderer.mViewPjMatrix, 0); GLES30.glVertexAttribPointer(0, 3, GLES30.GL_FLOAT, false, 3 * 4, vertexBuffer);
GLES30.glVertexAttribPointer(1, 2, GLES30.GL_FLOAT, false, 2 * 4, texCoorBuffer); GLES30.glEnableVertexAttribArray(0);
GLES30.glEnableVertexAttribArray(1); GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId);
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_FAN, 0, 6);//六个定点,绘制三角形 } }

  

package com.example.gles300;

/**
* @author gaofeng
*
*/
public class GLScript { public GLScript() {
} public static final String vertex3 =
"#version 300 es \n" +
"uniform mat4 uMVPMatrix;\n"
+ "layout(location = 0) in vec3 aPosition;\n"
+ "layout(location = 1) in vec2 aTexCoor;\n"
+ "out vec2 vTextureCoord;\n"
+ "void main() { \n"
+ "gl_Position = uMVPMatrix * vec4(aPosition,1);\n"
+ "vTextureCoord = aTexCoor;\n"
+ "}\n"
; public static final String fragment3 = "#version 300 es \n" +
"precision mediump float;\n"
+ "in vec2 vTextureCoord;\n"
+ "uniform sampler2D sTexture;\n"
+ "out vec4 v_color;\n"
+ "void main() { \n"
+ "vec2 coord = vTextureCoord;\n"
+ "coord.s = coord.s * 0.5;\n"
+ "v_color = texture(sTexture, coord); \n"
+ "}\n"
; }

  layout(location=0) in vec3 aPosition;定义个输入,location定义了这个数据的位置,相对于2.0 不需要进行attribute查找。

3.0中内置变量gl_FragColor 也被 out 颜色输出修改了。

3.0脚本前面需要添加 #version 300 es 版本声明

最后是脚本编译和链接工具类.

package com.example.gles300;

import android.opengl.GLES30;
import android.util.Log; /**
* @author gaofeng
*
*/
public class GLHelper { /**
*
*/
public GLHelper() {
// TODO Auto-generated constructor stub
} public static int linkGL(){
int programId = GLES30.glCreateProgram();//创建一个程序
if (programId == 0) {
Log.e("OPENGL", "Error Create Link Program");
return 0;
}
return programId;
} public static int linkAttach(int vertexsharder,int fragmentsharder){
int programId = linkGL();
GLES30.glAttachShader(programId, vertexsharder); //和着色器进行关联
GLES30.glAttachShader(programId, fragmentsharder);//和着色器进行关联
GLES30.glLinkProgram(programId); //把program链接起来
int status[] = new int[1];
GLES30.glGetProgramiv(programId, GLES30.GL_LINK_STATUS, status, 0); //这地方一样是检查是否有错误发生。
Log.d("OPENGL","linkAttach link status is " + GLES30.glGetProgramInfoLog(programId));
if (status[0] == 0) {
Log.e("OPENGL","link status is error.");
GLES30.glDeleteProgram(programId);
return 0;
}
return programId;
} public static boolean checkProgram(int programId){
GLES30.glValidateProgram(programId);
int status[] = new int[1];
GLES30.glGetProgramiv(programId,GLES30.GL_VALIDATE_STATUS, status,0);
if (status[0] == 0) {
Log.e("OPENGL","program is error");
return false;
}
return true;
} public static int compileScript(int type, String script){
int objID = GLES30.glCreateShader(type); //创建一个着色器对象,TYPE表示顶点着色器和片段着色器
if (objID == 0) { //0表示有错误
return 0;
}
GLES30.glShaderSource(objID, script); //把脚本代码传给OpenGL 引擎
GLES30.glCompileShader(objID); //开始编译
int[] status = new int[1];
GLES30.glGetShaderiv(objID, GLES30.GL_COMPILE_STATUS, status, 0); //看看编译结果是否有错误。
Log.d("OPENGL","compileScript status info:" + GLES30.glGetShaderInfoLog(objID));
if (status[0] == 0) {
GLES30.glDeleteShader(objID);//有错误我们删除这个对象。
Log.e("OPENGL", "Error Compile Script:" + script);
return 0;
}
return objID;
} }

  

Android OpenGL ES 3.0 纹理应用的更多相关文章

  1. [置顶] 使用Android OpenGL ES 2.0绘图之五:添加运动

    传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...

  2. Android OpenGL ES(七)----理解纹理与纹理过滤

    1.理解纹理 OpenGL中的纹理能够用来表示图像.照片,甚至由一个数学算法生成的分形数据.每一个二维的纹理都由很多小的纹理元素组成.它们是小块的数据,类似于我们前面讨论过的片段和像素.要使用纹理,最 ...

  3. [置顶] 使用Android OpenGL ES 2.0绘图之六:响应触摸事件

    传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...

  4. Android OpenGL ES 应用(二) 纹理

    上一篇讲了基础入门 OpenGL (一) ,这一次主要学习OpenGL 纹理基本学习总结 要是做复杂的OpenGL应用程序,一定会用到纹理技术.纹理说白了就是把图片或者视频图像绘制到OpenGL空间中 ...

  5. [工作记录] Android OpenGL ES 2.0: square texture not supported on some device

    npot texture: non-power-of-two texture.rectangle texture: non-square (height != wdith) 在测试Samsumg Ga ...

  6. Android openGL ES 2.0里Surfaceview背景透明

    surfaceview的黑色背景会挡住其父的背景,现在把surfaceview的背景设为透明,既可以看到所绘的3D物体,又可以看到背景. 在onSurfaceCreated里,调用GLES20.glC ...

  7. [工作记录] Android OpenGL ES: non-square texture - continue

    previous: [工作记录] Android OpenGL ES 2.0: square texture not supported on some device recently I found ...

  8. 基于Cocos2d-x学习OpenGL ES 2.0系列——你的第一个三角形(1)

    前言 在本系列教程中,我会以当下最流行的2D引擎Cocos2d-x为基础,介绍OpenGL ES 2.0的一些基本用法.本系列教程的宗旨是OpenGL扫盲,让大家在使用Cocos2d-x过程中,知其然 ...

  9. 利用JNI技术在Android中调用C++形式的OpenGL ES 2.0函数

    1.                 打开Eclipse,File-->New-->Project…-->Android-->AndroidApplication Projec ...

随机推荐

  1. android 上传图片到服务器Tomcat(Struts2)

    在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...

  2. Bluetooth LE(低功耗蓝牙) - 第二部分

    回顾 在前面的文章中我们介绍了Bluetooth LE的背景也说明了我们在本系列文章中将要开发什么,但是还没有实际的代码.我们将在这篇文章中纠正这一点,我们将通过定义 Service/Activity ...

  3. 使用 Chef 管理 Azure 资源

     发布于 2014-07-08 作者 陈 忠岳 Chef 是一款开源的开发运营(DevOps)工具,用以应对 IT 基础设施方面的挑战.微软开放技术有限公司(简称"微软开放技术" ...

  4. MessageFormat.format处理单引号和大括号

    在MessageFormat.format方法中组装jason数据字符串:{code:"w1",des:"w2"},起止分别有左大括号和右大括号.方法是将单引号 ...

  5. C#针对DataTable进行分页方法

    以下的分页方法是针对数据量不是非常大的数据进行的,是在内存中进行的分页操作. /// <summary> /// DataTable分页 /// </summary> /// ...

  6. Hibernate(十一)多对多双向关联映射

    上次我们在中Hibernate从入门到精通(十)多对多单向关联映射讲解了一下多对多单向关联映射,这次我 们讲解一下七种映射中的最后一种多对多双向关联映射. 多对多双向关联映射 按照我们之前的惯例,先看 ...

  7. hdu 4411 arrest 最小费用流

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  8. Milk Patterns - poj 3261 (求重复k次的最长子串)

    题目大意:给你一个数组,求这个数组里面至少重复k次的子串.   分析:后缀数组的练手题目...不过给的数字比较大,可以先离散化处理一下即可.   代码如下: ===================== ...

  9. 什么要缓存curl资源

    在看公司的代码框架底层时,发现了一个问题,如下: 代码中调用接口时,使用的是curl,框架将curl资源以IP :端口的形式缓存了下来,例如: 10.10.10.10:80     curl1 10. ...

  10. 在Tomcat下部署web项目

    每个web项目可以以两种方式存在,如联合风控项目,Urc.war,另一中是Urc解压后的目录结构.而tomcat目录下的\webapps\下则均是要部署的web项目解压后的文件夹,启动tomcat后, ...