纹理放大和缩小的过滤参数

1.使用线性插值效果最佳

2.通过修改

float maxscale = 4.0f  //放大

float  minscale = 0.5f //缩小

vertices[0].mPosition[0]=0.5f * maxscale; //x
vertices[0].mPosition[1]=0.5f * maxscale; //y

实现模糊效果

1.利用显卡的并行计算的强大功能对图像进行处理

2.FS中的代码实现

#ifdef GL_ES
prection mediump float;
#endif
uniform sampler2D U_Textture;
uniform vec4 U_ImageSize;
varying vec4 V_Texcoord;
void main(){vec3 color = vec3(0.0);float radius_in_pixle = 1.0;//半径//一个像素的偏移换算成纹理坐标在s,t方向偏移多少float radius_in_texcoord_s =  radius_in_pixle/U_ImageSize.x;float radius_in_texcoord_t =  radius_in_pixle/U_ImageSize.y;for(int y = -1; y<=1;y++){for(int x = -1; x<=1;x++){float texcoord_x= V_Texcoord.x + float(x) * radius_in_texcoord_s;float texcoord_y= V_Texcoord.y + float(y) * radius_in_texcoord_t;color += texture2D(U_Texture,vec2(texcoord_x,texcoord_y)).rgb;}}color/=9.0;gl_FragColor = vec4(color,texture2D(U_Texture,V_Texcoord.xy).a);
}

加载多张纹理

#include "Sence.h"
#include "Utils.h"
static AAssetManager *sAssetManager= nullptr;
GLuint vbo;//vertex buffer object
GLuint ibo;//index buffer object ,element array buffer object
GLuint program;
GLint modelMatrixLocation,viewMatrixLocation,projectionMatrixLocation,textlocation,text2location;
GLint attrPositionLocation,attrTexCoordLocation;
GLuint texture , texture2 , texture3;
glm::mat4 modelMatrix,viewMatrix,projectionMatrix,modelMatrix2;unsigned char * LoadFileContent(const char *path,int&filesize){unsigned char *filecontent=nullptr;filesize=0;AAsset* asset = AAssetManager_open(sAssetManager,path,AASSET_MODE_UNKNOWN);if(asset!= nullptr){filesize=AAsset_getLength(asset);filecontent=new unsigned char[filesize+1];AAsset_read(asset,filecontent,filesize);filecontent[filesize] = 0;AAsset_close(asset);}return filecontent;
}
extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Init(JNIEnv*env,jobject MainActivity,jobject am
){sAssetManager = AAssetManager_fromJava(env,am);__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"Init");//设置擦除颜色glClearColor(0.6f,0.4f,0.1f,1.0f);Vertice vertices[4];//cpu -> gpu//图元左下角点vertices[0].mPosition[0]=-500.0f;//xvertices[0].mPosition[1]=-500.0f;//yvertices[0].mPosition[2]=0.0f;//zvertices[0].mPosition[3]=1.0f;//wvertices[0].mTexcoord[0]=0.0f;//uvertices[0].mTexcoord[1]=0.0f;//vvertices[0].mTexcoord[2]=0.0f;//vertices[0].mTexcoord[3]=0.0f;////图元右下角点vertices[1].mPosition[0]=500.0f;//xvertices[1].mPosition[1]=-500.0f;//yvertices[1].mPosition[2]=0.0f;//zvertices[1].mPosition[3]=1.0f;//wvertices[1].mTexcoord[0]=1.0f;//uvertices[1].mTexcoord[1]=0.0f;//vvertices[1].mTexcoord[2]=0.0f;//vertices[1].mTexcoord[3]=0.0f;////图元左上角点vertices[2].mPosition[0]=-500.0f;//xvertices[2].mPosition[1]=500.0f;//yvertices[2].mPosition[2]=0.0f;//zvertices[2].mPosition[3]=1.0f;//wvertices[2].mTexcoord[0]=0.0f;//uvertices[2].mTexcoord[1]=1.0f;//vvertices[2].mTexcoord[2]=0.0f;//vertices[2].mTexcoord[3]=0.0f;////图元右上角点vertices[3].mPosition[0]=500.0f;//xvertices[3].mPosition[1]=500.0f;//yvertices[3].mPosition[2]=0.0f;//zvertices[3].mPosition[3]=1.0f;//wvertices[3].mTexcoord[0]=1.0f;//uvertices[3].mTexcoord[1]=1.0f;//vvertices[3].mTexcoord[2]=0.0f;//vertices[3].mTexcoord[3]=0.0f;////沿X轴正方向平移x个单位(x是有符号数)modelMatrix = glm::translate(0.0f,0.0f,-1.0f);modelMatrix2 = glm::translate(50.0f,0.0f,-2.0f);unsigned short indexes[]={ 0,1,2,1,3,2};ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER,indexes,sizeof(unsigned short)*6,GL_STATIC_DRAW);vbo = CreateBufferObject(GL_ARRAY_BUFFER,vertices,sizeof(Vertice) * 4,GL_STATIC_DRAW);program = CreateStandardProgram("test.vs","yuvtest.fs");attrPositionLocation = glGetAttribLocation(program,"position");attrTexCoordLocation = glGetAttribLocation(program,"texcoord");modelMatrixLocation = glGetUniformLocation(program,"U_ModelMatrix");viewMatrixLocation = glGetUniformLocation(program,"U_ViewMatrix");projectionMatrixLocation = glGetUniformLocation(program,"U_ProjectionMatrix");textlocation =  glGetUniformLocation(program,"U_Texture");text2location = glGetUniformLocation(program,"U_Texture2");;texture = CreateTextureFromFile("front.bmp");texture2 = CreateTextureFromFile("lenna.bmp");__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"%d,%d,%d,%d",attrPositionLocation,modelMatrixLocation,viewMatrixLocation,projectionMatrixLocation);
}
extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_OnViewportChanged(JNIEnv*env,jobject MainActivity,jint width,jint height
){__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"OnViewportChanged %dx%d",width,height);glViewport(0,0,width,height);viewMatrix=glm::lookAt(glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0,0.0,-1.0f),glm::vec3(0.0,1.0f,0.0f));float half_width=float(width)/2.0f;float half_height=float(height)/2.0f;projectionMatrix=glm::ortho(-half_width,half_width,-half_height,half_height,0.1f,100.0f);
}
extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Render(JNIEnv*env,jobject MainActivity
){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);glBindBuffer(GL_ARRAY_BUFFER,vbo);//使用程序对象作为当前渲染状态的一部分glUseProgram(program);glEnable(GL_DEPTH_TEST);glActiveTexture(GL_TEXTURE0);//激活0号纹理单元(初始化为GL_TEXTURE0)glBindTexture(GL_TEXTURE_2D,texture);//绑定纹理glUniform1i(textlocation,0);//把0号纹理单元内容植入卡槽中glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D,texture2);glUniform1i(text2location,1);//mvp矩阵glUniformMatrix4fv(modelMatrixLocation,1,GL_FALSE,glm::value_ptr(modelMatrix));glUniformMatrix4fv(viewMatrixLocation,1,GL_FALSE,glm::value_ptr(viewMatrix));glUniformMatrix4fv(projectionMatrixLocation,1,GL_FALSE,glm::value_ptr(projectionMatrix));//set attribute//启用或禁用通用顶点属性数组glEnableVertexAttribArray(attrPositionLocation);glVertexAttribPointer(attrPositionLocation,4,GL_FLOAT,GL_FALSE,sizeof(Vertice),0);glEnableVertexAttribArray(attrTexCoordLocation);glVertexAttribPointer(attrTexCoordLocation,4,GL_FLOAT,GL_FALSE,sizeof(Vertice),(void*)(sizeof(float)*4));glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_SHORT,0);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);glBindBuffer(GL_ARRAY_BUFFER,0);glUseProgram(0);
}

新建Fragment shader

#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D U_Texture;
uniform sampler2D U_Texture2;
varying vec4 V_Texcoord;
void main(){gl_FragColor = texture2D(U_Texture,V_Texcoord.xy) * texture2D(U_Texture2,V_Texcoord.xy);
}

渲染YUV420格式图片

#include "Sence.h"
#include "Utils.h"
static AAssetManager *sAssetManager= nullptr;
GLuint vbo;//vertex buffer object
GLuint ibo;//index buffer object ,element array buffer object
GLuint program;
GLint modelMatrixLocation,viewMatrixLocation,projectionMatrixLocation,textYlocation,textUlocation,textVlocation;
GLint attrPositionLocation,attrTexCoordLocation;
GLuint texture_y , texture_u , texture_v;
glm::mat4 modelMatrix,viewMatrix,projectionMatrix,modelMatrix2;unsigned char * LoadFileContent(const char *path,int&filesize){unsigned char *filecontent = nullptr;filesize = 0;AAsset* asset = AAssetManager_open(sAssetManager,path,AASSET_MODE_UNKNOWN);if(asset!= nullptr){filesize = AAsset_getLength(asset);filecontent=new unsigned char[filesize+1];AAsset_read(asset,filecontent,filesize);filecontent[filesize] = 0;AAsset_close(asset);}return filecontent;
}void InitYUV420()
{int filesize = 0;unsigned char * filecontent = LoadFileContent("test.yuv",filesize);unsigned char * YData = filecontent;int YDataSize = 1280 * 720;unsigned char * UData = filecontent + YDataSize;int UDataSize = 640 * 360;unsigned char * VData = filecontent + (YDataSize + UDataSize);delete [] filecontent;texture_y = CreateTexture2D(YData,1280,720,GL_ALPHA,GL_ALPHA);texture_u = CreateTexture2D(UData,640,360,GL_ALPHA,GL_ALPHA);texture_v = CreateTexture2D(VData,640,360,GL_ALPHA,GL_ALPHA);}
extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Init(JNIEnv*env,jobject MainActivity,jobject am
){sAssetManager = AAssetManager_fromJava(env,am);__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"Init");//设置擦除颜色glClearColor(0.6f,0.4f,0.1f,1.0f);Vertice vertices[4];//cpu -> gpu//图元左下角点vertices[0].mPosition[0]=-500.0f;//xvertices[0].mPosition[1]=-500.0f;//yvertices[0].mPosition[2]=0.0f;//zvertices[0].mPosition[3]=1.0f;//wvertices[0].mTexcoord[0]=0.0f;//uvertices[0].mTexcoord[1]=0.0f;//vvertices[0].mTexcoord[2]=0.0f;//vertices[0].mTexcoord[3]=0.0f;////图元右下角点vertices[1].mPosition[0]=500.0f;//xvertices[1].mPosition[1]=-500.0f;//yvertices[1].mPosition[2]=0.0f;//zvertices[1].mPosition[3]=1.0f;//wvertices[1].mTexcoord[0]=1.0f;//uvertices[1].mTexcoord[1]=0.0f;//vvertices[1].mTexcoord[2]=0.0f;//vertices[1].mTexcoord[3]=0.0f;////图元左上角点vertices[2].mPosition[0]=-500.0f;//xvertices[2].mPosition[1]=500.0f;//yvertices[2].mPosition[2]=0.0f;//zvertices[2].mPosition[3]=1.0f;//wvertices[2].mTexcoord[0]=0.0f;//uvertices[2].mTexcoord[1]=1.0f;//vvertices[2].mTexcoord[2]=0.0f;//vertices[2].mTexcoord[3]=0.0f;////图元右上角点vertices[3].mPosition[0]=500.0f;//xvertices[3].mPosition[1]=500.0f;//yvertices[3].mPosition[2]=0.0f;//zvertices[3].mPosition[3]=1.0f;//wvertices[3].mTexcoord[0]=1.0f;//uvertices[3].mTexcoord[1]=1.0f;//vvertices[3].mTexcoord[2]=0.0f;//vertices[3].mTexcoord[3]=0.0f;////沿X轴正方向平移x个单位(x是有符号数)modelMatrix = glm::translate(0.0f,0.0f,-1.0f);modelMatrix2 = glm::translate(50.0f,0.0f,-2.0f);unsigned short indexes[]={ 0,1,2,1,3,2};ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER,indexes,sizeof(unsigned short)*6,GL_STATIC_DRAW);vbo = CreateBufferObject(GL_ARRAY_BUFFER,vertices,sizeof(Vertice) * 4,GL_STATIC_DRAW);program = CreateStandardProgram("yuv420.vs","yuv420.fs");attrPositionLocation = glGetAttribLocation(program,"position");attrTexCoordLocation = glGetAttribLocation(program,"texcoord");modelMatrixLocation = glGetUniformLocation(program,"U_ModelMatrix");viewMatrixLocation = glGetUniformLocation(program,"U_ViewMatrix");projectionMatrixLocation = glGetUniformLocation(program,"U_ProjectionMatrix");textYlocation = glGetUniformLocation(program,"U_TextureY");textUlocation = glGetUniformLocation(program,"U_TextureU");textVlocation = glGetUniformLocation(program,"U_TextureV");InitYUV420();__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"%d,%d,%d,%d",attrPositionLocation,modelMatrixLocation,viewMatrixLocation,projectionMatrixLocation);
}
extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_OnViewportChanged(JNIEnv*env,jobject MainActivity,jint width,jint height
){__android_log_print(ANDROID_LOG_INFO,HANGYU_LOG_TAG,"OnViewportChanged %dx%d",width,height);glViewport(0,0,width,height);viewMatrix=glm::lookAt(glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0,0.0,-1.0f),glm::vec3(0.0,1.0f,0.0f));float half_width=float(width)/2.0f;float half_height=float(height)/2.0f;projectionMatrix=glm::ortho(-half_width,half_width,-half_height,half_height,0.1f,100.0f);
}
extern "C" JNIEXPORT void JNICALL Java_com_example_learnogles_MainActivity_Render(JNIEnv*env,jobject MainActivity
){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);glBindBuffer(GL_ARRAY_BUFFER,vbo);//使用程序对象作为当前渲染状态的一部分glUseProgram(program);glEnable(GL_DEPTH_TEST);glActiveTexture(GL_TEXTURE0);//激活0号纹理单元(初始化为GL_TEXTURE0)glBindTexture(GL_TEXTURE_2D,texture_y);//绑定纹理glUniform1i(textYlocation,0);//把0号纹理单元内容植入卡槽中glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D,texture_u);glUniform1i(textUlocation,1);glActiveTexture(GL_TEXTURE2);glBindTexture(GL_TEXTURE_2D,texture_v);glUniform1i(textVlocation,2);//mvp矩阵glUniformMatrix4fv(modelMatrixLocation,1,GL_FALSE,glm::value_ptr(modelMatrix));glUniformMatrix4fv(viewMatrixLocation,1,GL_FALSE,glm::value_ptr(viewMatrix));glUniformMatrix4fv(projectionMatrixLocation,1,GL_FALSE,glm::value_ptr(projectionMatrix));//set attribute//启用或禁用通用顶点属性数组glEnableVertexAttribArray(attrPositionLocation);glVertexAttribPointer(attrPositionLocation,4,GL_FLOAT,GL_FALSE,sizeof(Vertice),0);glEnableVertexAttribArray(attrTexCoordLocation);glVertexAttribPointer(attrTexCoordLocation,4,GL_FLOAT,GL_FALSE,sizeof(Vertice),(void*)(sizeof(float)*4));glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_SHORT,0);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);glBindBuffer(GL_ARRAY_BUFFER,0);glUseProgram(0);
}

VS

attribute vec4 position;
attribute vec4 texcoord;
uniform mat4 U_ModelMatrix;
uniform mat4 U_ViewMatrix;
uniform mat4 U_ProjectionMatrix;
varying vec4 V_Texcoord;
void main(){V_Texcoord=texcoord;gl_Position=U_ProjectionMatrix*U_ViewMatrix*U_ModelMatrix*position;
}

FS

#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D U_TextureY;
uniform sampler2D U_TextureU;
uniform sampler2D U_TextureV;
varying vec4 V_Texcoord;
vec3 YUV420ToRGB(float y,float u,float v){return vec3(y+1.402*v,y-0.34413*u-0.71414*v,y+1.772*u);
}
vec3 YUV420ToRGBViaMatrix(float y,float u,float v){mat3 transform_matrix=mat3(1.0,1.0,1.0,0.0,-0.34413,1.772,1.402,-0.71414,0.0);return transform_matrix*vec3(y,u,v);
}
void main(){vec2 texcoord=vec2(V_Texcoord.x,1.0-V_Texcoord.y);float Y=texture2D(U_TextureY,texcoord).a;float U=texture2D(U_TextureU,texcoord).a-0.5;float V=texture2D(U_TextureV,texcoord).a-0.5;gl_FragColor=vec4(YUV420ToRGBViaMatrix(Y,U,V),1.0);
}

YUV纹理刷新

Utils类新增

void SubmitTexture2D(GLuint texture,void*pixel,int x, int y,int width,int height,GLenum cpu_format,GLenum dataType);
void SubmitTexture2D(GLuint texture,void*pixel,int x, int y,int width,int height,GLenum cpu_format,GLenum dataType)
{glBindTexture(GL_TEXTURE_2D,texture);glTexSubImage2D(GL_TEXTURE_2D,0,x,y,width,height,cpu_format,dataType,pixel);glBindTexture(GL_TEXTURE_2D,0);}

【Android OpenGL ES 开发 (五)】纹理相关(二)相关推荐

  1. Android OpenGL ES 学习(五) -- 渐变色

    OpenGL 学习教程 Android OpenGL ES 学习(一) – 基本概念 Android OpenGL ES 学习(二) – 图形渲染管线和GLSL Android OpenGL ES 学 ...

  2. Android OpenGL ES 开发教程(20):颜色Color

    OpenGL ES 支持的颜色格式为RGBA模式(红,绿,蓝,透明度).颜色的定义通常使用Hex格式0xFF00FF 或十进制格式(255,0,255), 在OpenGL 中却是使用0-1之间的浮点数 ...

  3. 使用Android OpenGL ES 2.0绘图之二:定义形状

    传送门☞Android兵器谱☞转载请注明☞http://blog.csdn.net/leverage_1229 传送门☞系统架构设计☞转载请注明☞http://blog.csdn.net/levera ...

  4. Android OpenGL ES 开发教程(16):Viewing和Modeling(MODELVIEW) 变换

    Viewing和Modeling 变换关系紧密,对应到相机拍照为放置三角架和调整被拍物体位置及角度,通常将这两个变换使用一个modelview 变换矩阵来定义.对于同一个坐标变换,可以使用不同的方法来 ...

  5. android纹理坐标,Android OpenGL ES 7.1纹理绘制

    基本原理 与渐变色接近,但有些区别: 渐变色:光栅化过程中,计算出颜色值,然后在片段着色器的时候可以直接赋值 纹理:光栅化过程中,计算出当前片段在纹理上的坐标位置,然后在片段着色器的中,根据这个纹理上 ...

  6. 【Android OpenGL ES 开发 (一)】使用c++开发opengles 与 日志功能 及 加载assets

    创建OpenGLES视口 1.App窗口改成OpenGL窗口,是通过java调用C++,在以下位置修改如下内容 package com.example.learnogles;import androi ...

  7. android opengl es 纹理 不同设备 白色,android – OpenGL ES 2.0纹理没有在某些设备上显示...

    早上好,这是2个纹理非幂的典型例子. 由于多种原因,纹理在分辨率上需要2的幂,这是一个非常常见的错误,每个人都碰巧陷入这个陷阱:)我也是. 2个纹理的非功率在某些设备/ GPU上运行平稳的事实,仅仅取 ...

  8. Android OpenGL ES 开发教程(24):Depth Buffer

    OpenGL ES 中Depth Buffer 保存了像素与观测点之间的距离信息,在绘制3D图形时,将只绘制可见的面而不去绘制隐藏的面,这个过程叫"Hidden surface remova ...

  9. 【Android OpenGL ES 开发 (四)】纹理相关(一)

    纹理贴图的原理 1.作用:可以用来渲染视频. 2.纹理坐标 生成OpenGL中的纹理对象 1.像素数据想要绘制出来需要先变成纹理 2.创建纹理放在GPU上 GLuint CreateTexture2D ...

最新文章

  1. python正则表达regex_Python 正则表达式(RegEx)
  2. 组态王6.55连接MySql数据库(笔记)
  3. 1-1、作用域深入和面向对象
  4. 利用反射机制创建新类的两种方式及比较
  5. K8S的HelloWorld之旅
  6. java中socket类_Java中的Socket的用法
  7. CentOS Linux 7编译安装Redis
  8. R 语言与简单的回归分析
  9. 【一天一个C++小知识】015:c++11线程创建的三种方法
  10. 9102,用原生js造一个轮播组件
  11. 阿里云DataV基础平面地图使用笔记(2)
  12. UML建模工具 Visual Paradigm敏捷开发教程(一):如何确立项目愿景
  13. 基于张正友平面标定法的摄像机标定及GUI实现
  14. Vocabulary and Phrase in Paper of Computer Vision (Updating)
  15. 前端效果之“拉开窗帘”
  16. 修改软件的系统默认(C盘)安装路径方法
  17. 盘点 2012 年没落科技巨头
  18. 盘点 8 款好用的 API 接口文档管理工具
  19. 手动部署java jar包
  20. 织梦网站巧用标签实现图片自动Alt功能,强化织梦seo效果

热门文章

  1. junit测试报告生成_这是东西:jUnit:动态测试生成
  2. 自动化测试在CI CD管道中的作用
  3. jooq 分页排序_将jOOQ与Spring结合使用:排序和分页
  4. 什么是javax.ws.rs.core.context? [ 第1部分 ]
  5. 使用Hystrix DSL创建弹性骆驼应用程序
  6. 为Java应用程序编写数据驱动的测试
  7. 使用Camel从WildFly 8向WebLogic 12发送JMS消息
  8. 在Hibernate中使用存储过程
  9. 关于Servlet和异步Servlet
  10. 为什么NULL是错误的?