浮动应用+android,Android的强大 浮动效果

package eoe.demo;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;

import android.opengl.GLUtils;

public class MyRenderer implements Renderer {

// Points 网格顶点数组(45,45,3)

float vertex[][][] = new float[45][45][3];

// 指定旗形波浪的运动速度

int wiggle_count = 0;

// 临时变量

float hold;

// x,y,z转角

float xrot, yrot, zrot;

// vertex

private float[] texVertex = new float[12];

// vetexBuffer

private FloatBuffer vertexBuffer;

// coord

private float[] coord = new float[8];

// coordBuffer

private FloatBuffer coordBuffer;

// textures

private int[] textures = new int[1];

// init()

public void init() {

ByteBuffer texByteBuffer = ByteBuffer

.allocateDirect(texVertex.length * 4);

texByteBuffer.order(ByteOrder.nativeOrder());

vertexBuffer = texByteBuffer.asFloatBuffer();

vertexBuffer.put(texVertex);

vertexBuffer.position(0);

ByteBuffer coordByteBuffer = ByteBuffer

.allocateDirect(coord.length * 4);

coordByteBuffer.order(ByteOrder.nativeOrder());

coordBuffer = coordByteBuffer.asFloatBuffer();

coordBuffer.put(coord);

coordBuffer.position(0);

}

@Override

public void onDrawFrame(GL10 gl) {

init();

int x, y; // 循环变量

float _x, _y, _xb, _yb; // 用来将旗形的波浪分割成很小的四边形

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓冲

gl.glLoadIdentity(); // 重置当前的模型观察矩阵

gl.glTranslatef(0.0f, 0.0f, -12.0f); // 移入屏幕12个单位

gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); // 绕 X 轴旋转

gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); // 绕 Y 轴旋转

gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f); // 绕 Z 轴旋转

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, coordBuffer);

for (x = 0; x < 44; x++) {

for (y = 0; y < 44; y++) {

_x = (float) (x) / 44.0f; // 生成X浮点值

_y = (float) (y) / 44.0f; // 生成Y浮点值

_xb = (float) (x + 1) / 44.0f; // X浮点值+0.0227f

_yb = (float) (y + 1) / 44.0f; // Y浮点值+0.0227f

coordBuffer.clear();

// bottom left

coordBuffer.put(_x);

coordBuffer.put(_y);

// bottom right

coordBuffer.put(_x);

coordBuffer.put(_yb);

// top right

coordBuffer.put(_xb);

coordBuffer.put(_yb);

// top left

coordBuffer.put(_xb);

coordBuffer.put(_y);

vertexBuffer.clear();

// bottom left

vertexBuffer.put(vertex[x][y][0]);

vertexBuffer.put(vertex[x][y][1]);

vertexBuffer.put(vertex[x][y][2]);

// bottom right

vertexBuffer.put(vertex[x][y + 1][0]);

vertexBuffer.put(vertex[x][y + 1][1]);

vertexBuffer.put(vertex[x][y + 1][2]);

// top right

vertexBuffer.put(vertex[x + 1][y + 1][0]);

vertexBuffer.put(vertex[x + 1][y + 1][1]);

vertexBuffer.put(vertex[x + 1][y + 1][2]);

// top left

vertexBuffer.put(vertex[x + 1][y][0]);

vertexBuffer.put(vertex[x + 1][y][1]);

vertexBuffer.put(vertex[x + 1][y][2]);

gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);

}

}

gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

if (wiggle_count == 2) { // 用来降低波浪速度(每隔2帧一次)

for (int j = 0; j < 45; j++) {

hold = vertex[0][j][2]; // 存储当前左侧波浪值

for (int i = 0; i < 44; i++) {

// 当前波浪值等于其右侧的波浪值

vertex[i][j][2] = vertex[i + 1][j][2];

}

vertex[44][j][2] = hold;

}

wiggle_count=0;

}

wiggle_count++;

xrot += 0.3f; // X 轴旋转

yrot += 0.2f; // Y 轴旋转

zrot += 0.4f; // Z 轴旋转

}

@Override

public void onSurfaceChanged(GL10 gl, int width, int height) {

float ratio = (float) width / (float) height;

// 设置OpenGL场景的大小

gl.glViewport(0, 0, width, height);

// 设置投影矩阵

gl.glMatrixMode(GL10.GL_PROJECTION);

// 重置投影矩阵

gl.glLoadIdentity();

// 设置视口的大小

gl.glFrustumf(-ratio, ratio, -1, 1, 1, 15);

// 选择模型观察矩阵

gl.glMatrixMode(GL10.GL_MODELVIEW);

// 重置模型观察矩阵

gl.glLoadIdentity();

}

@Override

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

// 黑色背景

gl.glClearColor(0, 0, 0, 0);

// 启用阴影平滑

gl.glShadeModel(GL10.GL_SMOOTH);

// 启用深度测试

gl.glEnable(GL10.GL_DEPTH_TEST);

// 启用纹理映射

gl.glClearDepthf(1.0f);

// 深度测试的类型

gl.glDepthFunc(GL10.GL_LEQUAL);

// 精细的透视修正

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

// 允许2D贴图,纹理

gl.glEnable(GL10.GL_TEXTURE_2D);

gl.glEnable(GL10.GL_LEQUAL);

// 创建纹理

gl.glGenTextures(1, textures, 0);

// 设置要使用的纹理

gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

// 生成纹理

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, LoadImage.bitmap, 0);

// 线形滤波

gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,

GL10.GL_LINEAR);

gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,

GL10.GL_LINEAR);

//NEW

// 沿X平面循环

for (int x = 0; x < 45; x++) {

// 沿Y平面循环

for (int y = 0; y < 45; y++) {

// 向表面添加波浪效果

// -4.5f~4.5f

vertex[x][y][0] = ((float) x / 5.0f) - 4.5f;

vertex[x][y][1] = (((float) y / 5.0f) - 4.5f);

// 0-2*pai

vertex[x][y][2] = (float) (Math

.sin(((((float) x / 5.0f) * 40.0f) / 360.0f) * 3.141592654 * 2.0f));

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值