实现三角体,正方体的3d效果及自动旋转思路如下:


其实要实现三角体,正方体自动旋转效果,只需绘出立方体的三角形与四边形。在加上gl.glRotatef(angle,x , y, z);就行了。

具体实现如下:

Renderer类代码

package sim.feel;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;

public class MyRender implements Renderer {
//google画图单位
private int one = 0x10000;

//三角体旋转角度

private float triangleAngle = 0.5f;

//正方体旋转角度
private float quaterAngle = 0.5f;

//三角体、正方体及其颜色缓冲
private IntBuffer triangleBuffer;
private IntBuffer quaterBuffer;
private IntBuffer triangleColorBuffer;
private IntBuffer quaterColorBuffer;
// 三角体四个面的顶点
private int[] triangle = {

0, one, 0,

-one, -one, one,

one, -one, one,

0,one, 0,

one, -one, one,

one, -one, -one,

0, one, 0,

one, -one,-one,

-one, -one, one,

0, one, 0,

-one, -one, -one,

-one, -one, one

};
// 正方体8个面得顶点
private int[] quater = new int[] {

one, one, -one,

-one, one, -one,

one,one, one,

-one, one, one,

one, -one, one,

-one, -one, one,

one,-one, -one,

-one, -one, -one,

one, one, one,

-one, one, one,

one,-one, one,

-one, -one, one,

one, -one, -one,

-one, -one, -one,

one,one, -one,

-one, one, -one,

-one, one, one,

-one, one, -one,

-one,-one, one,

-one, -one, -one,

one, one, -one,

one, one, one,

one,-one, -one,

one, -one, one,

};
// 三角体各顶点的颜色(r,g,b,a)
private int[] triangleColor = new int[] {
// tri 4 face
one, 0, 0, one,

0, one, 0, one,

0, 0, one, one,

one, 0, 0, one,

0,one, 0, one,

0, 0, one, one,

one, 0, 0, one,

0, one, 0, one,

0, 0,one, one,

one, 0, 0, one,

0, one, 0, one,

0, 0, one, one,

};
// 正方形各顶点颜色
private int[] quaterColor = new int[] {

0, one, 0, one,

0, one, 0, one,

0,one, 0, one,

0, one, 0, one,

one, one / 2, 0, one,

one, one / 2, 0,one,

one, one / 2, 0, one,

one, one / 2, 0, one,

one, 0, 0, one,

one, 0, 0, one,

one, 0, 0, one,

one, 0, 0, one,

one, one, 0, one,
one, one, 0, one,

one, one, 0, one,

one, one, 0, one,

0, 0, one,one,

0, 0, one, one,

0,0, one, one,

0,0, one, one,

one,0, one,one,

one,0, one, one,

one,0, one, one,

one,0, one, one

};
//初始化Buffer
public void init() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(triangle.length * 4);
ByteBuffer byteColorBuffer = ByteBuffer
.allocateDirect(triangleColor.length * 4);
ByteBuffer byteQuaterBuffer = ByteBuffer
.allocateDirect(quater.length * 4);
ByteBuffer byteQuterColorBuffer = ByteBuffer
.allocateDirect(quaterColor.length * 4);

byteBuffer.order(ByteOrder.nativeOrder());
byteColorBuffer.order(ByteOrder.nativeOrder());
byteQuaterBuffer.order(ByteOrder.nativeOrder());
byteQuterColorBuffer.order(ByteOrder.nativeOrder());

triangleBuffer = byteBuffer.asIntBuffer();
triangleColorBuffer = byteColorBuffer.asIntBuffer();
quaterBuffer = byteQuaterBuffer.asIntBuffer();
quaterColorBuffer = byteQuterColorBuffer.asIntBuffer();

triangleBuffer.put(triangle);
triangleColorBuffer.put(triangleColor);
quaterBuffer.put(quater);
quaterColorBuffer.put(quaterColor);

triangleBuffer.position(0);
triangleColorBuffer.position(0);
quaterBuffer.position(0);
quaterColorBuffer.position(0);
}

@Override
public void onDrawFrame(GL10 gl) {
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 重置
gl.glLoadIdentity();

// 初始化triangleBuffer与triangleColorBuffer
init();
// 左移1.5f并向里移6.0f
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
// 旋转
gl.glRotatef(triangleAngle, 0.0f, 1.0f, 0.0f);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

// 设置顶点及颜色
gl.glVertexPointer(3, GL10.GL_FIXED, 0, triangleBuffer);
gl.glColorPointer(4, GL10.GL_FIXED, 0, triangleColorBuffer);
// 绘制
for (int i = 0; i < 4; i++) {
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 3, 3);
}

gl.glFinish();
// 重置观察模型
gl.glLoadIdentity();
gl.glTranslatef(1.5f, 0.0f, -6.0f);

gl.glRotatef(quaterAngle, 1.0f, 0.0f, 0.0f);

gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer);
gl.glColorPointer(4, GL10.GL_FIXED, 0, quaterColorBuffer);

// 绘制
for (int i = 0; i < 6; i++) {
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
}

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// 旋转角度+0.5f
triangleAngle += 0.5f;
quaterAngle += 0.5f;
}


@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
float ratio = (float) width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
// 告诉系统对透视进行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
// 黑色背景
gl.glClearColor(0, 0, 0, 0);
// 启用阴影平滑
gl.glShadeModel(GL10.GL_SMOOTH);

// 清除深度缓存
gl.glClearDepthf(1.0f);
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
// 所做深度测试的类型
gl.glDepthFunc(GL10.GL_LEQUAL);
}
}

效果图:

更多相关文章

  1. android Path 和 PathMeasure 进阶
  2. Android实现传感器应用及位置服务
  3. Android(安卓)OpenGL ES从白痴到入门(二):App诞生
  4. Android绘制平面上的多边形
  5. OpenGL ES for Android(安卓)绘制矩形和正方形
  6. Android(安卓)Animation动画
  7. [置顶] 很实用的android压缩图片的算法
  8. Android补间动画
  9. 使用Android来画一个钟表

随机推荐

  1. 截取android正在播放音乐的audio音频流(后
  2. 使用Android Studio调试Android Framewor
  3. Android插件开发机制
  4. Java利用Tomcat作为服务器与Android的Htt
  5. Android自动化测试“Adb connection Erro
  6. Android VCard联系人备份恢复(导入/导出)详
  7. Android文字转语音
  8. Android(安卓)- 数据存储 -在SQL数据库中
  9. [置顶] 史上最全selector和shape使用方法
  10. android adb 工具