{
1:创建一个没有数据的纹理
2:把屏幕上的某块拆下来,贴上去
3:三个注意
}
1:创建一个没有数据的纹理
//创建一个没有数据的纹理
virtual unsigned createTexture(int width,int height){
unsigned texture;
//生成纹理索引
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);//绑定纹理
//放大滤波/缩小滤波
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_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,0);
return texture;
}
//纹理id
unsigned kongbaiId=createTexture(_width,_height);
2:把屏幕上的某块拆下来,贴上去
x=0,y=0,z=0;
float w=200,h=200;
Vertex xt[]={
CELL::float3(x,y,0), CELL::float2(0,1), CELL::float4(1,1,1,1),
CELL::float3(x,y+h,0), CELL::float2(0,0), CELL::float4(1,1,1,1),
CELL::float3(x+w,y,0), CELL::float2(1,1), CELL::float4(1,1,1,1),
CELL::float3(x+w,y+h,0), CELL::float2(1,0), CELL::float4(1,1,1,1),
};
//绑定纹理
glBindTexture(GL_TEXTURE_2D,kongbaiId);
//拷贝局部纹理
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,_width,_height);
CELL::matrix4 mat=CELL::ortho<float>(0,(float)_width,(float)_height,0,-100,100);
glUniformMatrix4fv(shader._MVP,1,false,mat.data());
glVertexAttribPointer(shader._position,3,GL_FLOAT,false,sizeof(Vertex),&xt[0].pos);
glVertexAttribPointer(shader._tpos,2,GL_FLOAT,false,sizeof(Vertex),&xt[0].uvpos);
glVertexAttribPointer(shader._color,4,GL_FLOAT,false,sizeof(Vertex),&xt[0].rgba);
glDrawArrays(GL_TRIANGLE_STRIP,0,sizeof(xt)/sizeof(xt[0]));
3:三个注意
1:创建一张空纹理的大小问题,一定要获取你要显示整个页面的大小而不是放在什么地方的大小
例如:显示一个小窗口,显示的大小是整个屏幕的大小,放到什么位置的大小是当前纹理贴到小窗口位置上的大小
2:OpenGL-ES 状态机
纹理1
glActiveTexture(GL_TEXTURE0);//指定纹理级别(一张纹理不需要)
glBindTexture(GL_TEXTURE_2D,t0);//绑定纹理
glUniform1i(shader._tId0,0);//指定纹理阶段[0,31]
...
glVertexAttribPointer(shader._position,3,GL_FLOAT,false,sizeof(Vertex),&xt[0].pos);
glVertexAttribPointer(shader._tpos,2,GL_FLOAT,false,sizeof(Vertex),&xt[0].uvpos);
glVertexAttribPointer(shader._color,4,GL_FLOAT,false,sizeof(Vertex),&xt[0].rgba);
...
纹理2
glBindTexture(GL_TEXTURE_2D,kongbaiId);//绑定纹理
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,_width,_height);//拷贝局部纹理
glVertexAttribPointer(shader._position,3,GL_FLOAT,false,sizeof(Vertex),&xt[0].pos);
glVertexAttribPointer(shader._tpos,2,GL_FLOAT,false,sizeof(Vertex),&xt[0].uvpos);
glVertexAttribPointer(shader._color,4,GL_FLOAT,false,sizeof(Vertex),&xt[0].rgba);
...
类似这样glVertexAttribPointer,glUniformMatrix4f函数都是在当前状态机下设置属性
3:纹理翻转
正常纹理坐标和顶点坐标是一一对应的
CELL::float3(x,y,0), CELL::float2(0,0)
CELL::float3(x,y+h,0), CELL::float2(0,1)
CELL::float3(x+w,y,0), CELL::float2(1,0)
CELL::float3(x+w,y+h,0), CELL::float2(1,1)
垂直翻转的纹理坐标(把对应y轴翻转了一下)
CELL::float3(x,y,0), CELL::float2(0,1)
CELL::float3(x,y+h,0), CELL::float2(0,0)
CELL::float3(x+w,y,0), CELL::float2(1,1)
CELL::float3(x+w,y+h,0), CELL::float2(1,0)
源码地址
http://pan.baidu.com/s/1geVargZ OpenGL_ES(6.1).zip