本文主要演示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. GConf 错误:联系配置服务器失败;某些可能原因是需要为 ORBit 启用 TCP/IP 联网

    http://blog.csdn.net/heirenheiren/article/details/8107294 http://blog.sina.com.cn/s/blog_8097f8aa010 ...

  2. codeforces C. No to Palindromes!

    http://codeforces.com/contest/465/problem/C 题意:给你一个字符串,然后按照字典序找出下一个字符串,这个字符串中不能含有长度大于等于2的子串为回文串,如果含有 ...

  3. [LeetCode#265] Paint House II

    Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...

  4. Delphi WebService 需要注意 转

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://gang4415.blog.51cto.com/225775/251997 Web ...

  5. Atmospheric Scattering in Unity5

    本次实践效果仅有Atmospheric Scattering和AA,并无其他post-processing,看到类似depth of field等的效果全部是Atmospheric Scatterin ...

  6. PHP函数:PHP_SELF

    php_self是php的内置变量,记作$php_self,其作用是实现“页内跳转”.这里的页内跳转不同等于html的书签之类的跳转,而是php程序通过URL的尾参数的改变在同一个程序里提供不同的We ...

  7. Android 2D绘图初步

    Android是通过graphics类来显示2D图形的.其中graphics中包括了Canvas.Paint.Color.Bitmap等类.graphics具有绘制点.线.颜色.2D几何图形.图像处理 ...

  8. 数据库事务的ACID和BASE

    ACID versus BASE for database transactions解释了ACID和BASE的区别.如下: ACID: (关系数据库) Atomic: 原子性,一个事务要么全部成功,要 ...

  9. 使用redis来实现分布式锁

    在实际的工作中,有部分的特定场景需要使用到分布式锁来进行跨服务器资源的统一调配.之前在一家医疗互联网公司,因为黄牛抢号等原因,造成同一个患者同一时段在同一个医生处,挂到了两个及以上的号,我对之前我司实 ...

  10. cocos2d&cocos2dx学习资源

    汇总一下自己学习Cocos2d和cocos2dx认为比較好的一些资源: 书籍: <iPhone&iPad cocos2d游戏开发实战> Steffen Itterheim < ...