opengl es2.0繪制球體

import android.app.Activity;
import android.graphics.Color;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.ViewGroup;

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;

/**
 * Created by Administrator on 2016/8/31 0031.
 */
public class Test2 extends Activity {
    GLSurfaceView glSurfaceView;
    //定點(diǎn)著色器
    public static String VL = "attribute vec4 vPosition;\n" +
            "uniform mat4 u_Matrix;"
            + "void main() {\n"
            + "  gl_Position = u_Matrix*vPosition;\n"
            + "}";
    //片段著色器
    public static String FL = "precision mediump float;\n" +
            "uniform vec4 u_Color;"
            + "void main() {\n"
            + "  gl_FragColor = u_Color;\n"
            + "}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glSurfaceView = new GLSurfaceView(this);
        glSurfaceView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        setContentView(glSurfaceView);
        //設(shè)置opengl es版本
        glSurfaceView.setEGLContextClientVersion(2);
        glSurfaceView.setRenderer(new RenderListener());
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

    }

    class RenderListener implements GLSurfaceView.Renderer {

        FloatBuffer verticalsBuffer;
        //多邊形定點(diǎn)
       /* float[] verticals = new float[]{
                -1, -1, 0.0f, // top
                -1f, 1, 0.0f, // bottom left
                1f, 1, 0.0f,  // bottom right
               // 1,-0.5f,0.0f,
        };*/
        float[] verticals = new float[20 * 40 * 6 * 3];
        float[] projectMatrix = new float[16];
        ShortBuffer verticalsIndexBuffer;
        //每個(gè)三角形的定點(diǎn)編號(hào)馏臭,多邊形有兩個(gè)三角形組成
        short[] verticalsIndex = new short[]{
                0, 1, 2,
                0, 2, 3
        };
        private int mProgram;
        private int mPositionHandle;
        private int mColorHandle;
        private int mMatricHandle;

        public RenderListener() {
            float x = 0;
            float y = 0;
            float z = -1;
            float r = 1;
            int index=0;
            double d = 9 * Math.PI / 180;
            for (int i = 0; i < 180; i += 9) {
                double d1 = i * Math.PI / 180;
                for (int j = 0; j < 360; j += 9) {
                    double d2 = j * Math.PI / 180;
                    verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2+d));
                    verticals[index++] = (float) (y+r*Math.cos(d1+d));
                    verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2+d));

                    verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2));
                    verticals[index++] = (float) (y+r*Math.cos(d1));
                    verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2));

                    verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2+d));
                    verticals[index++] = (float) (y+r*Math.cos(d1));
                    verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2+d));

                    verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2+d));
                    verticals[index++] = (float) (y+r*Math.cos(d1+d));
                    verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2+d));

                    verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2));
                    verticals[index++] = (float) (y+r*Math.cos(d1+d));
                    verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2));

                    verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2));
                    verticals[index++] = (float) (y+r*Math.cos(d1));
                    verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2));

                }
            }

            verticalsBuffer = ByteBuffer.allocateDirect(verticals.length * 4)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer()
                    .put(verticals);
            verticalsBuffer.position(0);

            ByteBuffer dlb = ByteBuffer.allocateDirect(
                    // (對(duì)應(yīng)順序的坐標(biāo)數(shù) * 2)short是2字節(jié)
                    verticalsIndex.length * 2);
            dlb.order(ByteOrder.nativeOrder());
            verticalsIndexBuffer = dlb.asShortBuffer();
            verticalsIndexBuffer.put(verticalsIndex);
            verticalsIndexBuffer.position(0);
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            mProgram = GLES20.glCreateProgram();

            int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
            GLES20.glShaderSource(vertexShader, VL);
            GLES20.glCompileShader(vertexShader);

            int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
            GLES20.glShaderSource(fragmentShader, FL);
            GLES20.glCompileShader(fragmentShader);

            GLES20.glAttachShader(mProgram, vertexShader);
            GLES20.glAttachShader(mProgram, fragmentShader);

            GLES20.glLinkProgram(mProgram);

            mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
            mColorHandle = GLES20.glGetUniformLocation(mProgram, "u_Color");
            mMatricHandle = GLES20.glGetUniformLocation(mProgram, "u_Matrix");

            final float aspectRatio = width > height ? width * 1f / height : height * 1f / width;
            if (width > height) {
                Matrix.orthoM(projectMatrix, 0, -aspectRatio, aspectRatio, -1f, 1f, -1f, 1f);
            } else {
                Matrix.orthoM(projectMatrix, 0, -1f, 1f, -aspectRatio, aspectRatio, -1f, 1f);
            }
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
            GLES20.glUseProgram(mProgram);
            GLES20.glEnableVertexAttribArray(mPositionHandle);
            GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false,
                    12, verticalsBuffer);
            GLES20.glUniform4fv(mColorHandle, 1, new float[]{0, 1, 1, 5}, 0);
            GLES20.glUniformMatrix4fv(mMatricHandle, 1, false, projectMatrix, 0);
            GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0,20 * 40 * 6 );
            //GLES20.glDrawElements(GLES20.GL_TRIANGLES, verticalsIndex.length, GLES20.GL_UNSIGNED_SHORT, verticalsIndexBuffer);
            GLES20.glDisableVertexAttribArray(mPositionHandle);

        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市瘪弓,隨后出現(xiàn)的幾起案子逢享,更是在濱河造成了極大的恐慌待榔,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異瀑罗,居然都是意外死亡闽颇,警方通過(guò)查閱死者的電腦和手機(jī)盾戴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)兵多,“玉大人尖啡,你說(shuō)我怎么就攤上這事橄仆。” “怎么了衅斩?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵盆顾,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我畏梆,道長(zhǎng)椎扬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任具温,我火速辦了婚禮蚕涤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铣猩。我一直安慰自己揖铜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布达皿。 她就那樣靜靜地躺著天吓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪峦椰。 梳的紋絲不亂的頭發(fā)上龄寞,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音汤功,去河邊找鬼物邑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛滔金,可吹牛的內(nèi)容都是我干的色解。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼餐茵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼科阎!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起忿族,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤锣笨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后道批,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體错英,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年屹徘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了走趋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡噪伊,死狀恐怖簿煌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鉴吹,我是刑警寧澤姨伟,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站豆励,受9級(jí)特大地震影響夺荒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜良蒸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一技扼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嫩痰,春花似錦剿吻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至纺棺,卻和暖如春榄笙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背祷蝌。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工茅撞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人巨朦。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓乡翅,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親罪郊。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蠕蚜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容