Android 3D 旋轉的三角形(四)

來源:互聯網
上載者:User

透視

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);
    }

}

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.