Android OpenGL ES 3.0 纹理应用
本文主要演示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 纹理应用的更多相关文章
- [置顶] 使用Android OpenGL ES 2.0绘图之五:添加运动
传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...
- Android OpenGL ES(七)----理解纹理与纹理过滤
1.理解纹理 OpenGL中的纹理能够用来表示图像.照片,甚至由一个数学算法生成的分形数据.每一个二维的纹理都由很多小的纹理元素组成.它们是小块的数据,类似于我们前面讨论过的片段和像素.要使用纹理,最 ...
- [置顶] 使用Android OpenGL ES 2.0绘图之六:响应触摸事件
传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...
- Android OpenGL ES 应用(二) 纹理
上一篇讲了基础入门 OpenGL (一) ,这一次主要学习OpenGL 纹理基本学习总结 要是做复杂的OpenGL应用程序,一定会用到纹理技术.纹理说白了就是把图片或者视频图像绘制到OpenGL空间中 ...
- [工作记录] 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 ...
- Android openGL ES 2.0里Surfaceview背景透明
surfaceview的黑色背景会挡住其父的背景,现在把surfaceview的背景设为透明,既可以看到所绘的3D物体,又可以看到背景. 在onSurfaceCreated里,调用GLES20.glC ...
- [工作记录] Android OpenGL ES: non-square texture - continue
previous: [工作记录] Android OpenGL ES 2.0: square texture not supported on some device recently I found ...
- 基于Cocos2d-x学习OpenGL ES 2.0系列——你的第一个三角形(1)
前言 在本系列教程中,我会以当下最流行的2D引擎Cocos2d-x为基础,介绍OpenGL ES 2.0的一些基本用法.本系列教程的宗旨是OpenGL扫盲,让大家在使用Cocos2d-x过程中,知其然 ...
- 利用JNI技术在Android中调用C++形式的OpenGL ES 2.0函数
1. 打开Eclipse,File-->New-->Project…-->Android-->AndroidApplication Projec ...
随机推荐
- python模块与包加载机制
模块的搜索路径: When a module named spam is imported, the interpreter searches for a file named spam.py in ...
- linux 设置命令行属性,背景色,前景色等
我的博客:www.while0.com 主要是命令setterm.
- Light OJ 1031 - Easy Game(区间DP)
题目大意: 给你一个n,代表n个数字,现在有两个选手,选手A,B轮流有有一次机会,每个选手一次可以得到一个或者多个数字,从左侧或者右侧,但是不能同时从两边取数字,当所有的数字被取完,那么游戏结束.然后 ...
- js或者cs代码拼接html
一:使用到的jQuery 文档操作方法 html() 设置或返回匹配的元素集合中的 HTML 内容. append() 向匹配元素集合中的每个元素结尾插入由参数指定的内容. empty() 删除匹配的 ...
- Linux学习笔记14——使用fcntl实现文件锁定
期末考试快要来了,Linux学习进度一下拉下来许多.今天学习的是文件锁定,在Linux中,实现文件锁定的方法很多,例如fcntl和lockf.下面主要是fcntl的调用. fcntl函数的原型是:in ...
- 【狼】unity3d point light 个数限制更改
unity默认的pointlight个数为2个,多加的话先加的就没有效果, 具体更改方式如下: Edit -> Project Settings -> Quality-> Ren ...
- VS 2010不显示头文件源文件和所有以前分类的文件夹,*.h 和*.cpp都显示在同一个文件
打开VS后不显示头文件源文件和所有以前分类的文件夹,*.h 和*.cpp都显示在同一个文件 点击右图红色指示显示所有文件夹按钮,就能恢复.
- HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- (转)java多线程的一篇好文
云转型基石ThinkServer特性解析 2013-05-29 10:47 佚名 importnew 字号:T | T 本文只是一些针对初学者或者新手的问题,如果你已经具备良好的基础,那么你可以跳过本 ...
- 【Objective-C基础教程-读书笔记】第1章 启程
在第1章里面,作者主要以一种站在世界中心呼唤爱的姿态,给读者们打打鸡血洗洗脑,鼓励大家,投入时间学习Objective-C,值得啊! 首先,Objective-C既能用来开发OS X平台上的APP,又 ...