android opengl es 控制 贴图旋转
摘要:控制贴图旋转有很多种方式,通过片段着色语言或者顶点着色语言可以达到控制贴图旋转的目的,本文讲述一种更简单的控制方式,通过控制顶多坐标控制贴图旋转。
1. 顶点坐标
正常情况下顶点坐标如下所示
private float[] mVerticesData =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
通过上述可以正常显示一个正的图片,然后我们通过控制角度去改变顶点坐标的值对应的就可以更改旋转角度。
2. 设置角度
public void setAngle(float angle)
{
float map_width = 480f;
float map_height = 480f;//贴图宽高
float play_width = 1080f;//glviewport 设置的宽高
float play_height = 1080f;
float input_center_x = 540;
float input_center_y = 540;
//移动位置,中心点再正中间
float dst_x = (input_center_x - play_width / 2) ;
float dst_y = (play_height / 2 - input_center_y);
float x1 = 0,y1 = 0;
float x2 = 0,y2 = 0;
float x3 = 0,y3 = 0;
float x4 = 0,y4 = 0;
float cos_angle = (float) Math.cos((double)angle);
float sin_angle = (float) Math.sin((double)angle);
x1 = ((-map_width/2) * cos_angle) + ((-map_height/2) * sin_angle);
y1 = ((-map_height/2) * cos_angle) - (-(map_width/2) * sin_angle);
x2 = ((map_width/2) * cos_angle) + ((-map_height/2) * sin_angle);
y2 = ((-map_height/2) * cos_angle) - ((map_width/2) * sin_angle);
x3 = ((-map_width/2) * cos_angle) + ((map_height/2) * sin_angle);
y3 = ((map_height/2) * cos_angle) - ((-map_width/2) * sin_angle);
x4 = ((map_width/2) * cos_angle) + ((map_height/2) * sin_angle);
y4 = ((map_height/2) * cos_angle) - ((map_width/2) * sin_angle);
mVerticesData[0] = ((dst_x + x1) / (play_width/2));mVerticesData[1] = ((dst_y + y1) / (play_height/2));
mVerticesData[3] = ((dst_x + x2) / (play_width/2));mVerticesData[4] = ((dst_y+ y2) / (play_height/2));
mVerticesData[6] = ((dst_x + x3) / (play_width/2));mVerticesData[7] = ((dst_y + y3) / (play_height/2));
mVerticesData[9] = ((dst_x + x4) / (play_width/2));mVerticesData[10] = ((dst_y+ y4) / (play_height/2));
mVertices = ByteBuffer.allocateDirect ( mVerticesData.length * 4 )
.order ( ByteOrder.nativeOrder() ).asFloatBuffer();
mVertices.put ( mVerticesData ).position ( 0 );
}算出对应坐标,即可完成旋转贴图