纹理贴图的原理
1.作用:可以用来渲染视频。
2.纹理坐标
生成OpenGL中的纹理对象
1.像素数据想要绘制出来需要先变成纹理
2.创建纹理放在GPU上
GLuint CreateTexture2D(unsigned char *pixelData,int width,int height,GLenum type) {GLuint texture;glGenTextures(1,&texture);//创建一个纹理对象glBindTexture(GL_TEXTURE_2D,texture);//设置成当前要操作的2D纹理glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//线性过滤glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);//GL_CLAMP,U方向glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);//V方向glTexImage2D(GL_TEXTURE_2D,0,type,width,height,0,type,GL_UNSIGNED_BYTE,pixelData);//GL_RGBA,像素数据发送到显卡上去glBindTexture(GL_TEXTURE_2D,0);//伴读return texture;
}
纹理贴图在Shader中的修改
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_Texture; //纹理图片
varying vec4 V_Texcoord;
void main(){gl_FragColor = texture2D(U_Texture,V_Texcoord.xy);
}
3.插入纹理
glBindTexture(GL_TEXTURE_2D,texture);//设置纹理