Android OpenGL ES 顏色

來源:互聯網
上載者:User

一、基礎知識:1.平滑著色(Smooth coloring):  將多個頂點的不同顏色混合在一起,建立出漂亮的色彩混合。 2.單調著色:  給圖形塗上一種固定單一的顏色。 3.三角形定義的顏色數組(平滑著色):[java]  int one = 0x10000;  //三角形的頂點顏色值(r,g,b,a)  private IntBuffer colorBuffer = IntBuffer.wrap(new int[]{      one,0,0,one,      0,one,0,one,      0,0,one,one,  });   [java // 開啟顏色渲染功能.  gl.glEnableClientState(GL10.GL_COLOR_ARRAY);    // 設定三角形頂點的顏色  gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);    //關閉顏色渲染功能.  gl.glDisableClientState(GL10.GL_COLOR_ARRAY);   4.正方形定義的顏色數組(單調著色):[java] view plaincopy/* 渲染正方形 */  // 設定當前色為藍色  gl.glColor4f(1.0f, 0.5f, 1.0f, 1.0f);     二、代碼編輯:【Activity01.java】[java] mples_12_02;    import android.app.Activity;  import android.opengl.GLSurfaceView;  import android.opengl.GLSurfaceView.Renderer;  import android.os.Bundle;    public class Activity01 extends Activity  {      Renderer render = new GLRender();      /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);            GLSurfaceView glView = new GLSurfaceView(this);                    glView.setRenderer(render);          setContentView(glView);      }  }   【GLRender.java】[java] package com.yarin.android.Examples_12_02;    import java.nio.IntBuffer;    import javax.microedition.khronos.egl.EGLConfig;  import javax.microedition.khronos.opengles.GL10;    import android.opengl.GLSurfaceView.Renderer;    public class GLRender implements Renderer  {       int one = 0x10000;                 //三角形三個頂點       private IntBuffer triggerBuffer = IntBuffer.wrap(new int[]{              0,one,0,        //上頂點              -one,-one,0,    //坐下點              one,-one,0,});  //右下點       //正方形的4個頂點       private IntBuffer quaterBuffer = IntBuffer.wrap(new int[]{                  one,one,0,                  -one,one,0,                  one,-one,0,                  -one,-one,0});              //三角形的頂點顏色值(r,g,b,a)       private IntBuffer colorBuffer = IntBuffer.wrap(new int[]{               one,0,0,one,               0,one,0,one,               0,0,one,one,       });      @Override      public void onDrawFrame(GL10 gl)      {          // 清除螢幕和深度緩衝          gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);          // 重設當前的模型觀察矩陣          gl.glLoadIdentity();                    // 左移 1.5 單位,並移入螢幕 6.0          gl.glTranslatef(-1.5f, 0.0f, -6.0f);                    //設定定點數組          gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);          //設定顏色數組 -- 開啟顏色渲染功能.          gl.glEnableClientState(GL10.GL_COLOR_ARRAY);          // 設定三角形頂點的顏色          gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);          // 設定三角形頂點          gl.glVertexPointer(3, GL10.GL_FIXED, 0, triggerBuffer);          //繪製三角形          gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);          //關閉顏色數組 -- 關閉顏色渲染功能.          gl.glDisableClientState(GL10.GL_COLOR_ARRAY);                    /* 渲染正方形 */          // 設定當前色為藍色          gl.glColor4f(1.0f, 0.5f, 1.0f, 1.0f);                    // 重設當前的模型觀察矩陣          gl.glLoadIdentity();                    // 左移 1.5 單位,並移入螢幕 6.0          gl.glTranslatef(1.5f, 0.0f, -6.0f);          //設定和繪製正方形          gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer);          gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);            //取消頂點數組          gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);                }        @Override      public void onSurfaceChanged(GL10 gl, int width, int height)      {          // TODO Auto-generated method stub          float ratio = (float) width / height;          //設定OpenGL情境的大小          gl.glViewport(0, 0, width, height);          //設定投影矩陣          gl.glMatrixMode(GL10.GL_PROJECTION);          //重設投影矩陣          gl.glLoadIdentity();          // 設定視口的大小          gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);          // 選擇模型觀察矩陣          gl.glMatrixMode(GL10.GL_MODELVIEW);           // 重設模型觀察矩陣          gl.glLoadIdentity();          }  www.2cto.com      @Override      public void onSurfaceCreated(GL10 gl, EGLConfig config)      {          // 啟用陰影平滑          gl.glShadeModel(GL10.GL_SMOOTH);                    // 黑色背景          gl.glClearColor(0, 0, 0, 0);                    // 設定深度緩衝          gl.glClearDepthf(1.0f);                                   // 啟用深度測試          gl.glEnable(GL10.GL_DEPTH_TEST);                                  // 所作深度測試的類型          gl.glDepthFunc(GL10.GL_LEQUAL);                                             // 告訴系統對透視進行修正          gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);      }  }    三、運行效果:  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.