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;
    
    private float _width=320f;
    private float _height=480f;
   
    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
        gl.glMatrixMode(GL10.GL_PROJECTION);//设置投影
        float size=0.1f*(float)Math.tan(Math.toRadians(45.0)/2);
       
        float ratio=_width/_height;//计算下一样需要的屏幕比率
        //gl.glOrthof(-1, 1, -1/ratio, 1/ratio, 0.01f, 100.0f);//正交Orthographic: orthographic view
        //透视Perspective: glFrustumf()
        gl.glFrustumf(-size, size, -size/ratio, size/ratio, 0.01f, 100.0f);
       
        gl.glViewport(0, 0, (int)_width, (int)_height);//设置视点
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glEnable(GL10.GL_DEPTH_TEST);//即使这个物体本来应该被更近更大的物体遮盖,我们依然可以看到它
       
        gl.glClearColor(0f, 0f, 0f, 1.0f);
        // 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表示逆时针,CW顺时针
        // 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
        _width=w;
        _height=h;
        gl.glViewport(0,0,w,h);
    }

    @Override
    public void onDrawFrame(GL10 gl) {//当任何时候调用一个画图方法的时候
        gl.glLoadIdentity();
        // 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|GL10.GL_DEPTH_BUFFER_BIT);
   
       
       
       
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);
       
        for(int i=0;i<=10;i++){
            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, -1f, -1.0f+-1.5f*i);
            // set rotation
            gl.glRotatef(_xAngle, 1f, 0f, 0f);
            gl.glRotatef(_yAngle, 0f, 1f, 0f);
            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);
    }


}

 

image_thumb[2]

原文:http://blog.csdn.net/lixinso/article/details/5272495

posted @   朱旭东  阅读(417)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示