public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyGLSurfaceView view = new MyGLSurfaceView(this); view.setRenderer(new Myrenderer()); setContentView(view); } class MyGLSurfaceView extends GLSurfaceView { public MyGLSurfaceView(Context context) { super(context); } public MyGLSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); } } //渲染器 class Myrenderer implements GLSurfaceView.Renderer{ //表层创建 @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { //设置清屏色 gl.glClearColor(1,1,1,1); //启用顶点缓冲区 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); } //表层size时候 @Override public void onSurfaceChanged(GL10 gl, int width, int height) { //设置视口 gl.glViewport(0,0,width,height); //视口比例 float tatio=(float)width/(float)height; //矩形模式 投影矩阵, gl.glMatrixMode(GL10.GL_PROJECTION); //加载单位矩阵 gl.glLoadIdentity(); //平截投体 gl.glFrustumf(-1f,1f,-tatio,tatio,3,7); } //绘图 @Override public void onDrawFrame(GL10 gl) { //004眼球的位置 //000眼球的观察点 //020眼球向上的向量 //模型视图矩阵 gl.glClear(GL10.GL_COLOR_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); GLU.gluLookAt(gl,0,0,4,0,0,0,0,2,0); float[] coords={ 0f,0f,0f, 0f,0.5f,0f, 0.5f,0.5f,0f, }; //分配字节缓冲空间,存放顶点坐标 ByteBuffer byteBuffer=ByteBuffer.allocateDirect(coords.length*4); //设置本地顺序 byteBuffer.order(ByteOrder.nativeOrder()); FloatBuffer fbyebuffer = byteBuffer.asFloatBuffer(); fbyebuffer.put(coords); byteBuffer.position(0);//设置指针位置 gl.glColor4f(0f,1f,0f,1);//颜色 //3三维点 gl.glVertexPointer(3,GL10.GL_FLOAT,0,byteBuffer); //三角形 gl.glDrawArrays(GL10.GL_TRIANGLES,0,4); } } }
Android OpenGL 画简单的三角形
最新推荐文章于 2022-07-05 09:32:41 发布