openGL es实现小实例
生活随笔
收集整理的這篇文章主要介紹了
openGL es实现小实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在模擬器中要做一個彩色的三角形和正方形,代碼里面我寫了一些注解,新學opengl,不正確的地方望指正!
1、SqureOpenGlActivity類為Activity類
import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.WindowManager;public class SqureOpenGlActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);MyRenderer glRender = new MyRenderer();GLSurfaceView gView = new GLSurfaceView(this);gView.setRenderer(glRender);setContentView(gView);} }2、渲染類MyRenderer import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView.Renderer;public class MyRenderer implements Renderer {static int one = 0x10000;//int one =1//緩沖區:三角形、正方形、三角形顏色、正方形顏色private static IntBuffer trangelBuffer;private static IntBuffer squreBuffer;private static IntBuffer mTraColorBuffer;private static IntBuffer mSquColorbBuffer;//三角形頂點private int[] vertices = new int[]{0,one,0,-one,-one,0,one,-one,0};//正方形坐標private int[] squres = new int[] {one,one,0,-one,one,0,one,-one,0,-one,-one,0};//三角形各個頂點的顏色private int[] colorTra = new int[]{one,0,0,one,0,one,0,one,0,0,one,one};//正方形各個頂點的顏色private int[] colorSqu = new int[] {one,0,0,0,one,one,0,0,one,one,one,0,0,one,one,0};@Overridepublic void onDrawFrame(GL10 gl) {// 準備緩沖區//trangleByteBuffer vbb1 = ByteBuffer.allocateDirect(vertices.length * 4);vbb1.order(ByteOrder.nativeOrder());trangelBuffer = vbb1.asIntBuffer();trangelBuffer.put(vertices);trangelBuffer.position(0);//squreByteBuffer vbb2 = ByteBuffer.allocateDirect(squres.length * 4);vbb2.order(ByteOrder.nativeOrder());squreBuffer = vbb2.asIntBuffer();squreBuffer.put(squres);squreBuffer.position(0);//trangle colorByteBuffer color1 = ByteBuffer.allocateDirect(colorTra.length * 4);color1.order(ByteOrder.nativeOrder());mTraColorBuffer = color1.asIntBuffer();mTraColorBuffer.put(colorTra);mTraColorBuffer.position(0);//squre colorByteBuffer color2 = ByteBuffer.allocateDirect(colorSqu.length * 4);color2.order(ByteOrder.nativeOrder());mSquColorbBuffer = color2.asIntBuffer();mSquColorbBuffer.put(colorSqu);mSquColorbBuffer.position(0);//開始繪制三角形gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glLoadIdentity();//允許頂點和顏色緩沖gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//三角形處理gl.glTranslatef(-1.5f, 0.0f, -6.0f);gl.glVertexPointer(3, GL10.GL_FIXED, 0, trangelBuffer);gl.glColorPointer(4, GL10.GL_FIXED, 0, mTraColorBuffer);gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);gl.glLoadIdentity();//正方形處理gl.glTranslatef(1.5f, 0.0f, -6.0f);gl.glVertexPointer(3, GL10.GL_FIXED, 0, squreBuffer);gl.glColorPointer(4, GL10.GL_FIXED, 0, mSquColorbBuffer);gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);//取消緩沖gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisableClientState(GL10.GL_COLOR_ARRAY);}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {float ratio = (float) width / height;// 窗體變化時調用//場景大小gl.glViewport(0, 0, width, height);//投影矩陣gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();//設置視圖的大小gl.glFrustumf( -ratio, ratio, -1, 1, 1f, 10);//選擇觀察矩陣模型gl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();}@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {// 關閉圖形抖動,否則燈光照射后沒有光斑效果gl.glDisable(GL10.GL_DITHER);//透視效果gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);//啟用平滑陰影gl.glShadeModel(GL10.GL_SMOOTH);//設置深度緩存gl.glClearDepthf(1.0f);//啟用深度緩存gl.glEnable(GL10.GL_DEPTH_TEST);//設置深度測試的類型gl.glDepthFunc(GL10.GL_LEQUAL);// gl.glLoadIdentity();//清屏為黑色gl.glClearColor(0, 0, 0, 0);}}3、運行效果如下:
總結
以上是生活随笔為你收集整理的openGL es实现小实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ 方块游戏小更新
- 下一篇: IMEI码