Android 3D 旋转的三角形(三)
立体
package com.sunny;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView;
public class VortexRenderer implements GLSurfaceView.Renderer{
private static final String LOG_TAG=VortexRenderer.class.getSimpleName();
private ShortBuffer _indexBuffer;//保存索引
private FloatBuffer _vertexBuffer;//保存定点坐标
private FloatBuffer _colorBuffer;
//private short[] _indicesArray={0,1,2};
private int _nrOfVertices=0;//定义需要多少个顶点.对于一个三角形来说,一共需要三个顶点
private float _xAngle;
private float _yAngle;
public float getXAngle() {
return _xAngle;
}public void setXAngle(float angle) {
this._xAngle = angle;
}public float getYAngle() {
return _yAngle;
}public void setYAngle(float angle) {
this._yAngle = angle;
}@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {//surface创建以后调用
// TODO Auto-generated method stub
// enable the differentiation of which side may be visible
gl.glEnable(GL10.GL_CULL_FACE);//enable了culling面,以保证只有一面
// which is the front? the one which is drawn counter clockwise
gl.glFrontFace(GL10.GL_CCW);//GL_CCW表示逆时针
// which one should NOT be drawn
gl.glCullFace(GL10.GL_BACK);//GL_FRONT_AND_BACK
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
initTriangle();
}@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {//surface发生改变以后调用,例如从竖屏切换到横屏的时候
// TODO Auto-generated method stub
gl.glViewport(0,0,w,h);
}@Override
public void onDrawFrame(GL10 gl) {//当任何时候调用一个画图方法的时候
// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(0f, 0f, 0f, 1.0f);
// reset the matrix - good to fix the rotation to a static angle
gl.glLoadIdentity();
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// set rotation
gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
}
private void initTriangle(){
float[] coords={//坐标
-0.5f, -0.5f, 0.5f, // 0
0.5f, -0.5f, 0.5f, // 1
0f, -0.5f, -0.5f, // 2
0f, 0.5f, 0f, // 3
};
_nrOfVertices=coords.length;//更加的动态
float[] colors={//颜色
1f, 0f, 0f, 1f, // point 0 red
0f, 1f, 0f, 1f, // point 1 green
0f, 0f, 1f, 1f, // point 2 blue
1f, 1f, 1f, 1f, // point 3 white
};
short[] indices=new short[]{//定点数
0, 1, 3, // rwg
0, 2, 1, // rbg
0, 3, 2, // rbw
1, 2, 3, // bwg
};
//为这里两个buffer分配必须的内存
// float has 4 bytes
ByteBuffer vbb=ByteBuffer.allocateDirect(_nrOfVertices*3*4);
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer=vbb.asFloatBuffer();
// short has 2 bytes
ByteBuffer ibb=ByteBuffer.allocateDirect(_nrOfVertices*2);
ibb.order(ByteOrder.nativeOrder());
_indexBuffer=ibb.asShortBuffer();
ByteBuffer cbb=ByteBuffer.allocateDirect(4*_nrOfVertices*4);
cbb.order(ByteOrder.nativeOrder());
_colorBuffer=cbb.asFloatBuffer();
_vertexBuffer.put(coords);
_indexBuffer.put(indices);
_colorBuffer.put(colors);
_vertexBuffer.position(0);
_indexBuffer.position(0);
_colorBuffer.position(0);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南