Android:OpenGL ES现实3D动画

Android:OpenGL ES现实3D动画_小安空间_新浪博客,小安空间,

</blockquote>
<blockquote>public class GLRender implements Renderer
{
<wbr />public static final int <wbr />num <wbr />= 50; <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 星星数目
<wbr />boolean <wbr /> <wbr /> <wbr /> <wbr /> <wbr />twinkle = true; <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 闪烁的星星
<wbr />boolean <wbr /> <wbr /> <wbr /> <wbr /> <wbr />key;
<wbr />public Star[] <wbr /> <wbr /> <wbr />star <wbr /> <wbr />= new Star[num]; <wbr />// 存放星星的数组
<wbr />float <wbr /> <wbr /> <wbr /> <wbr /> <wbr />zoom <wbr /> <wbr />= -10.0f; <wbr /> <wbr /> <wbr /> <wbr />// 星星离观察者的距离
<wbr />float <wbr /> <wbr /> <wbr /> <wbr /> <wbr />tilt <wbr /> <wbr />= 90.0f; <wbr /> <wbr /> <wbr /> <wbr />// 星星的倾角
<wbr />float <wbr /> <wbr /> <wbr /> <wbr /> <wbr />spin; <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 闪烁星星的自转 <wbr />
<wbr />int one = 0x10000;
<wbr />Random random = new Random();
<wbr />int <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />texture; <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 纹理
<wbr />
<wbr />IntBuffer coord = IntBuffer.wrap(new int[]{
<wbr /> <wbr /> <wbr />0,0,
<wbr /> <wbr /> <wbr />one,0,
<wbr /> <wbr /> <wbr />one,one,
<wbr /> <wbr /> <wbr />0,one,
<wbr />});
<wbr />IntBuffer vertexs = IntBuffer.wrap(new int[]{
<wbr /> <wbr /> <wbr />-one,-one,0,
<wbr /> <wbr /> <wbr />one,-one,0,
<wbr /> <wbr /> <wbr />one,one,0,
<wbr /> <wbr /> <wbr />-one,one,0,
<wbr />});
<wbr />ByteBuffer indices = ByteBuffer.wrap(new byte[]{
<wbr /> <wbr /> <wbr />1, 0, 2, 3
<wbr />});
<wbr />
<wbr />@Override
<wbr />public void onDrawFrame(GL10 gl)
<wbr />{
<wbr /> <wbr />
<wbr /> <wbr />gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// 清除屏幕和深度缓存
<wbr /> <wbr />gl.glBindTexture(GL10.GL_TEXTURE_2D, texture); <wbr /> <wbr />// 选择纹理

<wbr /> <wbr />for (int i=0; i&lt;num; i++) <wbr /> <wbr /> <wbr /> <wbr />// 循环设置所有的星星
<wbr /> <wbr />{
<wbr /> <wbr /> <wbr />gl.glLoadIdentity(); <wbr /> <wbr /> <wbr /> <wbr />// 绘制每颗星星之前,重置模型观察矩阵
<wbr /> <wbr /> <wbr />gl.glTranslatef(0.0f,0.0f,zoom); <wbr /> <wbr /> <wbr />// 深入屏幕里面
<wbr /> <wbr /> <wbr />gl.glRotatef(tilt,1.0f,0.0f,0.0f); <wbr /> <wbr /> <wbr />// 倾斜视角
<wbr /> <wbr /> <wbr />gl.glRotatef(star[i].angle,0.0f,1.0f,0.0f); <wbr />// 旋转至当前所画星星的角度
<wbr /> <wbr /> <wbr />gl.glTranslatef(star[i].dist,0.0f,0.0f); <wbr />// 沿X轴正向移动
<wbr /> <wbr /> <wbr />gl.glRotatef(-star[i].angle,0.0f,1.0f,0.0f); <wbr />// 取消当前星星的角度
<wbr /> <wbr /> <wbr />gl.glRotatef(-tilt,1.0f,0.0f,0.0f); <wbr /> <wbr />// 取消屏幕倾斜

<wbr /> <wbr /> <wbr />//设置定点数组
<wbr /> <wbr /> <wbr />gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
<wbr /> <wbr /> <wbr />//设置颜色数组
<wbr /> <wbr /> <wbr />gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
<wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr />gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
<wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr />if (twinkle) <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 启用闪烁效果
<wbr /> <wbr /> <wbr />{
<wbr /> <wbr /> <wbr /> <wbr />// 使用byte型数值指定一个颜色
<wbr /> <wbr /> <wbr /> <wbr />gl.glColor4f((float)star[(num-i)-1].r/255.0f,(float)star[(num-i)-1].g/255.0f,(float)star[(num-i)-1].b/255.0f,1.0f);
<wbr /> <wbr /> <wbr /> <wbr />gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexs);
<wbr /> <wbr /> <wbr /> <wbr />gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coord);
<wbr /> <wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr /> <wbr />{
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />coord.position(0);
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />vertexs.position(0);
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />indices.position(0);
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices);
<wbr /> <wbr /> <wbr /> <wbr />}
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> //绘制结束
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> gl.glFinish();
<wbr /> <wbr /> <wbr />}

<wbr /> <wbr /> <wbr />gl.glRotatef(spin,0.0f,0.0f,1.0f); <wbr /> <wbr /> <wbr />// 绕z轴旋转星星
<wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr />// 使用byte型数值指定一个颜色
<wbr /> <wbr /> <wbr />gl.glColor4f((float)star[(num-i)-1].r/255.0f,(float)star[(num-i)-1].g/255.0f,(float)star[(num-i)-1].b/255.0f,1.0f);
<wbr /> <wbr /> <wbr />gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexs);
<wbr /> <wbr /> <wbr />gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coord);
<wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr />{
<wbr /> <wbr /> <wbr /> <wbr />coord.position(0);
<wbr /> <wbr /> <wbr /> <wbr />vertexs.position(0);
<wbr /> <wbr /> <wbr /> <wbr />indices.position(0);
<wbr /> <wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr /> <wbr />gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices);
<wbr /> <wbr /> <wbr />}
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> //绘制正方形结束
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> gl.glFinish();
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> //取消顶点数组
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
<wbr /> <wbr /> <wbr /> <wbr /> <wbr /> gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
<wbr /> <wbr /> <wbr /> <wbr /> <wbr />
<wbr /> <wbr /> <wbr />spin+=0.01f; <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 星星的公转
<wbr /> <wbr /> <wbr />star[i].angle+=(float)(i)/(float)num; <wbr /> <wbr />// 改变星星的自转角度
<wbr /> <wbr /> <wbr />star[i].dist-=0.01f; <wbr /> <wbr /> <wbr /> <wbr />// 改变星星离中心的距离

<wbr /> <wbr /> <wbr />if (star[i].dist&lt;0.0f) <wbr /> <wbr /> <wbr />// 星星到达中心了么
<wbr /> <wbr /> <wbr />{
<wbr /> <wbr /> <wbr /> <wbr />star[i].dist+=5.0f; <wbr /> <wbr /> <wbr />// 往外移5个单位
<wbr /> <wbr /> <wbr /> <wbr />star[i].r=random.nextInt(256); <wbr /> <wbr />// 赋一个新红色分量
<wbr /> <wbr /> <wbr /> <wbr />star[i].g=random.nextInt(256); <wbr /> <wbr />// 赋一个新绿色分量
<wbr /> <wbr /> <wbr /> <wbr />star[i].b=random.nextInt(256); <wbr /> <wbr />// 赋一个新蓝色分量
<wbr /> <wbr /> <wbr />}

<wbr /> <wbr />}
<wbr />}

<wbr />@Override
<wbr />public void onSurfaceChanged(GL10 gl, int width, int height)
<wbr />{
<wbr /> <wbr />float ratio = (float) width / height;
<wbr /> <wbr />//设置OpenGL场景的大小
<wbr /> <wbr />gl.glViewport(0, 0, width, height);
<wbr /> <wbr />//设置投影矩阵
<wbr /> <wbr />gl.glMatrixMode(GL10.GL_PROJECTION);
<wbr /> <wbr />//重置投影矩阵
<wbr /> <wbr />gl.glLoadIdentity();
<wbr /> <wbr />// 设置视口的大小
<wbr /> <wbr />gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
<wbr /> <wbr />// 选择模型观察矩阵
<wbr /> <wbr />gl.glMatrixMode(GL10.GL_MODELVIEW); <wbr />
<wbr /> <wbr />// 重置模型观察矩阵
<wbr /> <wbr />gl.glLoadIdentity();
<wbr />}

<wbr />@Override
<wbr />public void onSurfaceCreated(GL10 gl, EGLConfig config)
<wbr />{
<wbr /> <wbr />gl.glShadeModel(GL10.GL_SMOOTH); <wbr /> <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 启用阴影平滑
<wbr /> <wbr />gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 黑色背景
<wbr /> <wbr />gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); <wbr /> <wbr /> <wbr />// 告诉系统对透视进行修正
<wbr /> <wbr />
<wbr /> <wbr />IntBuffer buffer = IntBuffer.allocate(1);
<wbr /> <wbr />// 创建一个纹理
<wbr /> <wbr />gl.glGenTextures(1, buffer);
<wbr /> <wbr />texture = buffer.get();
<wbr /> <wbr />// 创建一个线性滤波纹理
<wbr /> <wbr />gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
<wbr /> <wbr />gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
<wbr /> <wbr />gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
<wbr /> <wbr />GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);
<wbr /> <wbr />
<wbr /> <wbr />
<wbr /> <wbr />gl.glEnable(GL10.GL_TEXTURE_2D); <wbr /> <wbr /> <wbr /> <wbr />// 启用纹理映射
<wbr /> <wbr />gl.glShadeModel(GL10.GL_SMOOTH); <wbr /> <wbr /> <wbr /> <wbr />// 启用阴影平滑
<wbr /> <wbr />gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); <wbr /> <wbr /> <wbr />// 黑色背景
<wbr /> <wbr />gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); <wbr />// 真正精细的透视修正
<wbr /> <wbr />gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE); <wbr /> <wbr /> <wbr />// 设置混色函数取得半透明效果
<wbr /> <wbr />gl.glEnable(GL10.GL_BLEND); <wbr /> <wbr /> <wbr /> <wbr /> <wbr />// 启用混色

<wbr /> <wbr />for (int i=0; i&lt;num; i++) <wbr /> <wbr /> <wbr /> <wbr />// 创建循环设置全部星星
<wbr /> <wbr />{
<wbr /> <wbr /> <wbr />Star starTMP = new Star();
<wbr /> <wbr /> <wbr />starTMP.angle=0.0f; <wbr /> <wbr /> <wbr /> <wbr />// 所有星星都从零角度开始
<wbr /> <wbr /> <wbr />starTMP.dist=((float)(i)/(float)num)*5.0f; <wbr /> <wbr />// 计算星星离中心的距离
<wbr /> <wbr /> <wbr />starTMP.r=random.nextInt(256); <wbr /> <wbr /> <wbr />// 为star[loop]设置随机红色分量
<wbr /> <wbr /> <wbr />starTMP.g=random.nextInt(256); <wbr /> <wbr /> <wbr />// 为star[loop]设置随机红色分量
<wbr /> <wbr /> <wbr />starTMP.b=random.nextInt(256); <wbr /> <wbr /> <wbr />// 为star[loop]设置随机红色分量
<wbr /> <wbr /> <wbr />star[i] = starTMP;
<wbr /> <wbr />}
<wbr />}

<wbr />public boolean onKeyUp(int keyCode, KeyEvent event)
<wbr />{
<wbr /> <wbr />twinkle=!twinkle;
<wbr /> <wbr />return false;
<wbr />}
}
class Star
{
<wbr />int <wbr /> <wbr />r, g, b; <wbr />// 星星的颜色
<wbr />float <wbr />dist; <wbr /> <wbr />// 星星距离中心的距离
<wbr />float <wbr />angle <wbr />= 0.0f;// 当前星星所处的角度
}

<wbr />

public class MainActivity extends Activity {
<wbr />GLRender render = new GLRender();
<wbr />@Override
<wbr />public void onCreate(Bundle savedInstanceState)
<wbr />{
<wbr /> <wbr />super.onCreate(savedInstanceState);
<wbr /> <wbr />GLImage.load(this.getResources());
<wbr /> <wbr />GLSurfaceView glView = new GLSurfaceView(this);
<wbr /> <wbr />
<wbr /> <wbr />glView.setRenderer(render);
<wbr /> <wbr />setContentView(glView);
<wbr />}
}
class GLImage
{
<wbr />public static Bitmap mBitmap;
<wbr />public static void load(Resources resources)
<wbr />{
<wbr /> <wbr />mBitmap = BitmapFactory.decodeResource(resources, R.drawable.star);
<wbr />}
}

 

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏