最新廣商小助手 專案進展 OpenGL ES 3D在我專案中引用 程式碼太多隻好選重要部分出來

範銘祥發表於2015-12-25
package com.example.home;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.view.MotionEvent;
import android.view.SurfaceHolder;



public class MyGLView extends GLSurfaceView{
    private final float TOUCH_SCALE_FACTOR = 180.0f/1000;//角度縮放比例
    private SceneRenderer renderer;//場景渲染器
    private float previousX;//上次的觸控位置
    private float previousY; 
    private float cameraX=0;//攝像機的位置
    private float cameraY=0;
    private float cameraZ=0;
    private float targetX=0;//看點
    private float targetY=0;
    private float targetZ=-4;
    private float sightDis=40;//攝像機和目標的距離
    private float angdegElevation=45;//仰角
    private float angdegAzimuth=90;//方位角
    //關於燈光旋轉的量
    float angdegLight=0;//燈光旋轉的角度
    float angdegZ=45;//燈在xz面上的投影與z軸的夾角
    float rLight=10;
    LightRatateThread lightRatateThread;//旋轉燈的執行緒
    public MyGLView(Context context) {
        super(context);
        renderer=new SceneRenderer();//建立渲染器
        this.setRenderer(renderer);//設定渲染器
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//設定渲染模式為主動渲染   
    }
    //觸控事件回撥方法
    @Override 
    public boolean onTouchEvent(MotionEvent e) {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float dy = y - previousY;//計算觸控筆Y位移
            float dx = x - previousX;//計算觸控筆Y位移
            angdegAzimuth += dx * TOUCH_SCALE_FACTOR;//設定沿y軸旋轉角度
            angdegElevation+= dy * TOUCH_SCALE_FACTOR;//設定沿x軸旋轉角度
            requestRender();//重繪畫面
        }
        previousY = y;//記錄觸控筆位置
        previousX = x;//記錄觸控筆位置
        return true;
    }
    private class SceneRenderer implements GLSurfaceView.Renderer {



        Dadi dimian;//地面
        longCube longcube;//左教學樓
        longCube longcube2;//右教學樓
        longCube northcube;//北教學樓
        longCube southcube;//南教學樓
        longCube westcube;//西教學樓
        longCube eastcube;//東教學樓
        
        longCube zoulang1;//左前走廊下
        longCube zoulang2;//左後走廊下
        longCube zoulang3;//右前走廊下
        longCube zoulang4;//右後走廊下
        
        longCube zoulang1_1;//左前走廊中
        longCube zoulang2_1;//左後走廊中
        longCube zoulang3_1;//右前走廊中
        longCube zoulang4_1;//右後走廊中
        
        Cylinder cylinder1;//1右圓柱
        Cylinder cylinder1_1;//1左圓柱
        
        Cylinder cylinder2;//2右圓柱
        Cylinder cylinder2_1;//2左圓柱
        
        Cylinder cylinder3;//3右圓柱
        Cylinder cylinder3_1;//3左圓柱
        
        Cylinder cylinder4;//4右圓柱
        Cylinder cylinder4_1;//4左圓柱
        
        sanlingzui louti;//梯臺
        
        Cylinder cylinderAA;//1號樓梯
        Cylinder cylinderBB;//2號樓梯
        Cylinder cylinderCC;//3號樓梯
        Cylinder cylinderDD;//4號樓梯
        
        longCube caocong1;//左草叢
        longCube caocong2;//右草叢
        
        longCube paifang;//牌坊
        
        Cylinder p1;//牌坊1柱
        Cylinder p2;//牌坊2柱
        
        Cylinder p3;//牌坊3柱
        Cylinder p4;//牌坊4柱
        
        Ball tree1;//
        Ball tree2;//
        
        Cylinder gan1;//樹幹1
        Cylinder gan2;//樹幹2
        private int[] textureIds;
        
        private int topTexIdPlat;
        private int sideTexIdPlat;
        private int bottomTexIdPlat;
        private int AAding;
        private int ok;
        private int kk;
        private int[] huacong;
        private int cylinderSideTexId;
        private int loucheng;
        private int[] face;
        private int greenye;
        private int shugan;
        public SceneRenderer(){}
        @Override
        public void onDrawFrame(GL10 gl) {
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//清除顏色與深度快取
            gl.glMatrixMode(GL10.GL_MODELVIEW);//設定當前矩陣為模式矩陣
            gl.glLoadIdentity();//設定當前矩陣為單位矩陣  
            double angradElevation=Math.toRadians(angdegElevation);//仰角(弧度)
            double angradAzimuth=Math.toRadians(angdegAzimuth);//方位角
            cameraX=(float) (targetX+sightDis*Math.cos(angradElevation)*Math.cos(angradAzimuth));
            cameraY=(float) (targetY+sightDis*Math.sin(angradElevation));
            cameraZ=(float) (targetZ+sightDis*Math.cos(angradElevation)*Math.sin(angradAzimuth));
            GLU.gluLookAt(//設定camera位置
                    gl, 
                    cameraX, //人眼位置的X
                    cameraY, //人眼位置的Y
                    cameraZ, //人眼位置的Z
                    targetX, //人眼球看的點X
                    targetY, //人眼球看的點Y
                    targetZ, //人眼球看的點Z
                    0,  //頭的朝向
                    1, 
                    0
            );
            //設定定位光
            double angradLight=Math.toRadians(angdegLight);
            double angradZ=Math.toRadians(angdegZ);
            float[] positionParams={
                    (float) (-rLight*Math.cos(angradLight)*Math.sin(angradZ)),
                    (float) (rLight*Math.sin(angradLight)),
                    (float) (rLight*Math.cos(angradLight)*Math.cos(angradZ)),
                    1};//設定定位光
            gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, positionParams,0);
            gl.glTranslatef(0, 0f, -6f);
  
 
 
           
            //調整與原點的位置 左右 ,上下,前後
            //地面
            gl.glPushMatrix();
            gl.glTranslatef(0,-dimian.h-2,0);
            dimian.drawSelf(gl);
            gl.glPopMatrix();
            //左教學樓
            gl.glPushMatrix();
            gl.glTranslatef(-3f, longcube.b/2-2, -1.0f);
            longcube.drawSelf(gl);
            gl.glPopMatrix();
            //右教學樓
            gl.glPushMatrix();
            gl.glTranslatef(3f, longcube2.b/2-2, -1.0f);
            longcube2.drawSelf(gl);
            gl.glPopMatrix();       
            //北教學樓
            gl.glPushMatrix();
            gl.glTranslatef(0f, northcube.b/2-2, -2.0f);
            northcube.drawSelf(gl);
            gl.glPopMatrix();
            //南教學樓
            gl.glPushMatrix();
            gl.glTranslatef(0f, southcube.b/2-2, 0.0f);
            southcube.drawSelf(gl);
            gl.glPopMatrix();
            //西教學樓
            gl.glPushMatrix();
            gl.glTranslatef(-1f, westcube.b/2-2, -1.0f);
            westcube.drawSelf(gl);
            gl.glPopMatrix();
            //東教學樓
            gl.glPushMatrix();
            gl.glTranslatef(1f, eastcube.b/2-2, -1.0f);
            eastcube.drawSelf(gl);
            gl.glPopMatrix();
            //調整與原點的位置 左右 ,上下,前後
            //左前走廊下
            gl.glPushMatrix();
            gl.glTranslatef(-2f, zoulang1.b/2-2+0.2f, 0.0f);
            zoulang1.drawSelf(gl);
            gl.glPopMatrix();
            //左後走廊下
            gl.glPushMatrix();
            gl.glTranslatef(-2f, zoulang2.b/2-2+0.2f, -2.0f);
            zoulang2.drawSelf(gl);
            gl.glPopMatrix();
            //右前走廊下
            gl.glPushMatrix();
            gl.glTranslatef(2f, zoulang3.b/2-2+0.2f, 0.0f);
            zoulang3.drawSelf(gl);
            gl.glPopMatrix();
            //右後走廊下
            gl.glPushMatrix();
            gl.glTranslatef(2f, zoulang4.b/2-2+0.2f, -2.0f);
            zoulang4.drawSelf(gl);
            gl.glPopMatrix();
            //調整與原點的位置 左右 ,上下,前後
            //左前走廊中
            gl.glPushMatrix();
            gl.glTranslatef(-2f, zoulang1.b/2-2+0.8f, 0.0f);
            zoulang1_1.drawSelf(gl);
            gl.glPopMatrix();
            //左後走廊中
            gl.glPushMatrix();
            gl.glTranslatef(-2f, zoulang2.b/2-2+0.8f, -2.0f);
            zoulang2_1.drawSelf(gl);
            gl.glPopMatrix();
            //右前走廊中
            gl.glPushMatrix();
            gl.glTranslatef(2f, zoulang3.b/2-2+0.8f, 0.0f);
            zoulang3_1.drawSelf(gl);
            gl.glPopMatrix();
            //右後走廊中
            gl.glPushMatrix();
            gl.glTranslatef(2f, zoulang4.b/2-2+0.8f, -2.0f);
            zoulang4_1.drawSelf(gl);
            gl.glPopMatrix();
          //調整與原點的位置 左右 ,上下,前後
            //1右圓柱
            gl.glPushMatrix();
            gl.glTranslatef(-1.7f,zoulang1.b/2-2+0.2f, 0.2f);
            cylinder1.drawSelf(gl);
            gl.glPopMatrix();
            
            //1左圓柱
            gl.glPushMatrix();
            gl.glTranslatef(-2.2f,zoulang1.b/2-2+0.2f, 0.2f);
            cylinder1_1.drawSelf(gl);
            gl.glPopMatrix();
         
            //2右圓柱
            gl.glPushMatrix();
            gl.glTranslatef(-1.7f,zoulang2.b/2-2+0.2f, -2.0f);
            cylinder2.drawSelf(gl);
            gl.glPopMatrix();
            
          //2左圓柱
            gl.glPushMatrix();
            gl.glTranslatef(-2.2f,zoulang2.b/2-2+0.2f,-2.0f);
            cylinder2_1.drawSelf(gl);
            gl.glPopMatrix();
            
          //3右圓柱
            gl.glPushMatrix();
            gl.glTranslatef(1.7f,zoulang1.b/2-2+0.2f, 0.2f);
            cylinder3.drawSelf(gl);
            gl.glPopMatrix();
            
          //3左圓柱
            gl.glPushMatrix();
            gl.glTranslatef(2.2f,zoulang1.b/2-2+0.2f, 0.2f);
            cylinder3_1.drawSelf(gl);
            gl.glPopMatrix();
            
          //4右圓柱
            gl.glPushMatrix();
            gl.glTranslatef(1.7f,zoulang1.b/2-2+0.2f, -2.0f);
            cylinder4.drawSelf(gl);
            gl.glPopMatrix();
            
          //4左圓柱
            gl.glPushMatrix();
            gl.glTranslatef(2.2f,zoulang1.b/2-2+0.2f, -2.0f);
            cylinder4_1.drawSelf(gl);
            gl.glPopMatrix();
            
          //調整與原點的位置 左右 ,上下,前後 
          //樓梯
            gl.glPushMatrix();
            gl.glTranslatef(0,-dimian.h-2+0.5f,1.3f);
            louti.drawSelf(gl);
            gl.glPopMatrix();
            
            
          //教學樓內部樓梯
          //cylinderAA號圓柱
            gl.glPushMatrix();
            gl.glTranslatef(-3.6f,zoulang1.b/2-2, -2.0f);
            cylinderAA.drawSelf(gl);
            gl.glPopMatrix();
            
            
            //cylinderBB號圓柱
            gl.glPushMatrix();
            gl.glTranslatef(-3.6f,zoulang1.b/2-2, 0f);
            cylinderBB.drawSelf(gl);
            gl.glPopMatrix();
            
            //cylinderCC號圓柱
            gl.glPushMatrix();
            gl.glTranslatef(3.6f,zoulang1.b/2-2, -2.0f);
            cylinderCC.drawSelf(gl);
            gl.glPopMatrix();
            
            //cylinderDD號圓柱
            gl.glPushMatrix();
            gl.glTranslatef(3.6f,zoulang1.b/2-2, 0f);
            cylinderDD.drawSelf(gl);
            gl.glPopMatrix();
          //調整與原點的位置 左右 ,上下,前後
            //左花叢
            gl.glPushMatrix();
            gl.glTranslatef(-1.2f, -dimian.h-2+0.8f, 1.2f);
            caocong1.drawSelf(gl);
            gl.glPopMatrix();
            //右花叢
            gl.glPushMatrix();
            gl.glTranslatef(1.2f, -dimian.h-2+0.8f, 1.2f);
            caocong2.drawSelf(gl);
            gl.glPopMatrix();
          //調整與原點的位置 左右 ,上下,前後
            //牌坊
            gl.glPushMatrix();
            gl.glTranslatef(0f, 0.7f, 1.2f);
            paifang.drawSelf(gl);
            gl.glPopMatrix();
            
          //p1柱
            gl.glPushMatrix();
            gl.glTranslatef(-2.7f,zoulang1.b/2-2f,1.2f );
            p1.drawSelf(gl);
            gl.glPopMatrix();
            
          //p2柱
            gl.glPushMatrix();
            gl.glTranslatef(-2f,zoulang1.b/2-2f,1.2f );
            p2.drawSelf(gl);
            gl.glPopMatrix();
            
          //p3柱
            gl.glPushMatrix();
            gl.glTranslatef(2f,zoulang1.b/2-2f,1.2f );
            p3.drawSelf(gl);
            gl.glPopMatrix();
            
          //p4柱
            gl.glPushMatrix();
            gl.glTranslatef(2.7f,zoulang1.b/2-2f,1.2f );
            p4.drawSelf(gl);
            gl.glPopMatrix();
          //調整與原點的位置 左右 ,上下,前後
          //
            gl.glPushMatrix();
            gl.glTranslatef(-1.5f,-0.6f, 2.5f);
            tree1.drawSelf(gl);
            gl.glPopMatrix();
            
          //
            gl.glPushMatrix();
            gl.glTranslatef(1.5f,-0.6f, 2.5f);
            tree2.drawSelf(gl);
            gl.glPopMatrix();
            
          //樹幹1
            gl.glPushMatrix();
            gl.glTranslatef(-1.5f,zoulang1.b/2-2f,2.5f );
            gan1.drawSelf(gl);
            gl.glPopMatrix();
            
          //樹幹2
            gl.glPushMatrix();
            gl.glTranslatef(1.5f,zoulang1.b/2-2f,2.5f );
            gan2.drawSelf(gl);
            gl.glPopMatrix();
        }
        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {           
            gl.glViewport(0, 0, width, height); //設定視窗大小及位置             
            gl.glMatrixMode(GL10.GL_PROJECTION);//設定當前矩陣為投影矩陣            
            gl.glLoadIdentity();//設定當前矩陣為單位矩陣            
            float ratio = (float) width / height;//計算透視投影的比例            
            gl.glFrustumf(-ratio, ratio, -1, 1, 6f, 100);//呼叫此方法計算產生透視投影矩陣
            
            lightRatateThread=new LightRatateThread(MyGLView.this);//建立並開啟執行緒
            lightRatateThread.start();
        }
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {            
            gl.glDisable(GL10.GL_DITHER);//關閉抗抖動             
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);//設定特定Hint專案的模式          
            gl.glClearColor(0,0,0,0); //設定螢幕背景色黑色RGBA    
            gl.glEnable(GL10.GL_CULL_FACE);//設定為開啟背面剪裁
            gl.glEnable(GL10.GL_DEPTH_TEST); //啟用深度測試       
            
            gl.glEnable(GL10.GL_LIGHTING); //允許光照
            initGreenLight(gl);//初始化燈
            initMaterial(gl);//初始化材質
            //初始化紋理
            textureIds=new int[]{
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang)
                };
            huacong=new int[]{
                    initTexture(gl,R.drawable.huacong),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.huacong)
                };
            face=new int[]{
                    initTexture(gl,R.drawable.men),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang),
                    initTexture(gl,R.drawable.qiang)
                };
            //紋理id初始化  
            AAding=initTexture(gl,R.drawable.qiang);
            sideTexIdPlat=initTexture(gl,R.drawable.side);    
            bottomTexIdPlat=initTexture(gl,R.drawable.side);
            topTexIdPlat=initTexture(gl,R.drawable.top);
            loucheng=initTexture(gl,R.drawable.loucheng);
            ok=initTexture(gl,R.drawable.fff);
            kk=initTexture(gl,R.drawable.loutiche);
            cylinderSideTexId=initTexture(gl,R.drawable.cylinder);
            greenye=initTexture(gl,R.drawable.shuye);
            shugan=initTexture(gl,R.drawable.shugan);
            //建立各個幾何體
          
            dimian=new Dadi(//梯臺
                    2f,
                    4,4,
                    4,4,
                    0.2f,
                    topTexIdPlat,bottomTexIdPlat,sideTexIdPlat,
                    2,2
                    );
                                                //左右距  上下距  前後距
            longcube=new longCube(0.8f,new float[]{0.8f,4.0f,3.0f},textureIds);//左教學樓
            longcube2=new longCube(0.8f,new float[]{0.8f,4.0f,3.0f},textureIds);//右教學樓
            northcube=new longCube(0.8f,new float[]{2.5f,3.0f,0.6f},textureIds);//北教學樓
            southcube=new longCube(0.8f,new float[]{2.5f,3.0f,0.6f},face);//南教學樓
            westcube=new longCube(0.8f,new float[]{0.6f,3.0f,1.6f},textureIds);//西教學樓
            eastcube=new longCube(0.8f,new float[]{0.6f,3.0f,1.6f},textureIds);//東教學樓
                                            
            zoulang1=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//左前走廊下
            zoulang2=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//左後走廊下
            zoulang3=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//右前走廊下
            zoulang4=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//右後走廊下
            
            zoulang1_1=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//左前走廊下
            zoulang2_1=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//左後走廊下
            zoulang3_1=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//右前走廊下
            zoulang4_1=new longCube(0.8f,new float[]{1.5f,0.1f,0.7f},textureIds);//右後走廊下
                                    //尺寸  半徑  高
            cylinder1=new Cylinder(0.8f,0.1f,0.5f,40,//1右圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            cylinder1_1=new Cylinder(0.8f,0.1f,0.5f,40,//1左圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            
            cylinder2=new Cylinder(0.8f,0.1f,0.5f,40,//2右圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            cylinder2_1=new Cylinder(0.8f,0.1f,0.5f,40,//2左圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            
            cylinder3=new Cylinder(0.8f,0.1f,0.5f,40,//3右圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            cylinder3_1=new Cylinder(0.8f,0.1f,0.5f,40,//3左圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            
            cylinder4=new Cylinder(0.8f,0.1f,0.5f,40,//3右圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            cylinder4_1=new Cylinder(0.8f,0.1f,0.5f,40,//3左圓柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            //尺寸 底面  寬 長     頂面  寬 長   高度
            louti=new sanlingzui(//梯臺
                    0.8f,
                    2,2,
                    2,2,
                    0.5f,
                    kk,
                    ok,
                    2,2
                    );
                                //尺寸  半徑  高
            cylinderAA=new Cylinder(0.8f,0.3f,4f,40,//AA圓柱
                    AAding,AAding,loucheng);
            cylinderBB=new Cylinder(0.8f,0.3f,4f,40,//AA圓柱
                    AAding,AAding,loucheng);
            cylinderCC=new Cylinder(0.8f,0.3f,4f,40,//AA圓柱
                    AAding,AAding,loucheng);
            cylinderDD=new Cylinder(0.8f,0.3f,4f,40,//AA圓柱
                    AAding,AAding,loucheng);
                                                    //左右距  上下距  前後距
            caocong1=new longCube(0.8f,new float[]{0.4f,0.4f,1.6f},huacong);//左草叢
            caocong2=new longCube(0.8f,new float[]{0.4f,0.4f,1.6f},huacong);//右花叢
            
            paifang=new longCube(0.8f,new float[]{6f,0.4f,0.2f},textureIds);//牌坊
                            //尺寸  半徑  高
            p1=new Cylinder(0.8f,0.1f,2.3f,40,//p1柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            p2=new Cylinder(0.8f,0.1f,2.3f,40,//p1柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            p3=new Cylinder(0.8f,0.1f,2.3f,40,//p1柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
            p4=new Cylinder(0.8f,0.1f,2.3f,40,//p1柱
                    cylinderSideTexId,cylinderSideTexId,cylinderSideTexId);
                            //大小,半徑
            tree1=new Ball(0.8f,0.4f,30,30,greenye);//
            tree2=new Ball(0.8f,0.4f,30,30,greenye);////尺寸  半徑  高
            gan1=new Cylinder(0.8f,0.04f,1f,40,//p1柱
                    shugan,shugan,shugan);
            gan2=new Cylinder(0.8f,0.04f,1f,40,//p1柱
                    shugan,shugan,shugan);
        }
    }//SceneRenderer
    //初始化紋理
    public int initTexture(GL10 gl,int drawableId){
        //生成紋理ID
        int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);//生成一個紋理id放在textures陣列中的0位置
        int currTextureId=textures[0];   //獲取生成的紋理id 
        gl.glBindTexture(GL10.GL_TEXTURE_2D, currTextureId);//繫結該紋理id,後面的操作都是針對該id
        //設定MIN_FILTER與MAG_FILTER為MIPMAP紋理過濾方式
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR_MIPMAP_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR);
        //生成MIPMAP紋理
        ((GL11)gl).glTexParameterf(GL10.GL_TEXTURE_2D,GL11.GL_GENERATE_MIPMAP, GL10.GL_TRUE);
        //設定紋理拉伸方式為REPEAT 
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT);
        InputStream is = this.getResources().openRawResource(drawableId);//獲取圖片資源的輸入流
        Bitmap bitmapTmp; 
        try{
            bitmapTmp = BitmapFactory.decodeStream(is);//通過輸入流生成點陣圖
        } 
        finally{
            try {
                is.close();//關閉流
            }catch(IOException e){
                e.printStackTrace();//列印異常
            }
        }
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmapTmp, 0);//自動設定圖片的格式和型別
        bitmapTmp.recycle(); //回收圖片資源
        return currTextureId;
    }
    private void initGreenLight(GL10 gl){//初始化燈的方法
        gl.glEnable(GL10.GL_LIGHT0);//開啟0號燈 
        float[] ambientParams={0.6f,0.6f,0.6f,1.0f};//光引數 RGBA
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientParams,0); //環境光設定   
        float[] diffuseParams={1.0f,1.0f,1.0f,1.0f};//光引數 RGBA
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffuseParams,0); //散射光設定 
        float[] specularParams={1.0f,1.0f,1.0f,1.0f};//光引數 RGBA
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, specularParams,0);//反射光設定    
    }
    private void initMaterial(GL10 gl){//初始化材質的方法
        float ambientMaterial[] = {0.4f, 0.4f, 0.4f, 1.0f};//環境光為白色材質
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, ambientMaterial,0);
        float diffuseMaterial[] = {1.0f, 1.0f, 1.0f, 1.0f};
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, diffuseMaterial,0);//散射光為白色材質
        float specularMaterial[] = {1.0f, 1.0f, 1.0f, 1.0f};
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, specularMaterial,0);//高光材質為白色
        float shininessMaterial[] = {1.5f}; //高光反射區域
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, shininessMaterial,0);
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        lightRatateThread.setFlag(false);
    }
}

以上是我專案中最重要的3D程式碼 展示的是我們校 第一教學樓





 

相關文章