⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glthread.java

📁 android 下学习openGL的一个不错demo
💻 JAVA
字号:
/*** * Excerpted from "Hello, Android!", * published by The Pragmatic Bookshelf. * Copyrights apply to this code. It may not be used to create training material,  * courses, books, articles, and the like. Contact us if you are in doubt. * We make no guarantees that this code is fit for any purpose.  * Visit http://www.pragmaticprogrammer.com/titles/eband for more book information.***/
package com.mot.opengl;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11ExtensionPack;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;import android.opengl.GLU;
import android.util.Log;import android.view.SurfaceHolder;
class GLThread extends Thread {
   private final GLView view;
   private boolean mRun = true;   private final String TAG = "GLThread";   private int type;
   private SurfaceHolder mSurfaceHolder;   private int[] cube_pictures = {		   		   R.drawable.android,		   R.drawable.wallpaper_dale_chihuly_small,		   R.drawable.wallpaper_lake_small,		   R.drawable.wallpaper_mountain_small,		   R.drawable.wallpaper_sunrise_small,		   R.drawable.wallpaper_darkwhirl_small   };      private int rec_picture = R.drawable.android;
   
   private GLCube cube;
   private GLRectangle rectangle;
   
   private long startTime;
   
   GLThread(GLView view,int type) {	   
      this.view = view;      this.type = type;      this.mSurfaceHolder= view.getHolder();
      switch (type) {      case OpenGL.TYPE_CUBE:    	  cube = new GLCube();    	  break;      case OpenGL.TYPE_RECTANGLE:    	  rectangle = new GLRectangle();    	  break;    	        }   }

   
   @Override
   public void run() {
      // Initialize OpenGL...
      
      EGL10 egl = (EGL10) EGLContext.getEGL();       EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); 
      int[] version = new int[2];
      egl.eglInitialize(display, version); 
      int[] configSpec = { EGL10.EGL_RED_SIZE, 5,             EGL10.EGL_GREEN_SIZE, 6, EGL10.EGL_BLUE_SIZE, 5,
            EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };

      EGLConfig[] configs = new EGLConfig[1];
      int[] numConfig = new int[1];
      egl.eglChooseConfig(display, configSpec, configs, 1,             numConfig);
      EGLConfig config = configs[0];

      EGLContext glc = egl.eglCreateContext(display, config,             EGL10.EGL_NO_CONTEXT, null);

      EGLSurface surface = egl.eglCreateWindowSurface(display,             config, view.getHolder(), null);
      egl.eglMakeCurrent(display, surface, surface, glc); 
      GL10 gl = (GL10) (glc.getGL());       init(gl); 
      
      // Loop until asked to quit
      while (mRun) {
         // Draw a single frame here...
    	/*  Canvas canvas = null;    	  canvas = mSurfaceHolder.lockCanvas(null);*/    	  Log.e(TAG,"mRUN");
         drawFrame(gl);
         egl.eglSwapBuffers(display, surface);
        /* if (canvas != null) {             mSurfaceHolder.unlockCanvasAndPost(canvas);}*/
         // Error handling
         if (egl.eglGetError() == EGL11.EGL_CONTEXT_LOST) {
            Context c = view.getContext();
            if (c instanceof Activity) {
               ((Activity) c).finish();
            }
         }
         
      }
      

      // Free OpenGL resources
      egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE,             EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
      egl.eglDestroySurface(display, surface);
      egl.eglDestroyContext(display, glc);
      egl.eglTerminate(display);
      
   }
   

   
   
   
   private void init(GL10 gl) {
      
      
      
      boolean SEE_THRU = true;
      
      
      startTime = System.currentTimeMillis();
      
      
      // Define the view frustrum
      gl.glViewport(0, 0, view.getWidth(), view.getHeight());
      gl.glMatrixMode(GL10.GL_PROJECTION);
      gl.glLoadIdentity();
      float ratio = (float) view.getWidth() / view.getHeight();
      GLU.gluPerspective(gl, 45.0f, ratio, 1, 100f);       

      
      // Define the lighting
      float lightAmbient[] = new float[] { 0.2f, 0.2f, 0.2f, 1 };
      float lightDiffuse[] = new float[] { 1, 1, 1, 1 };
      float[] lightPos = new float[] { 1, 1, 1, 1 };

      gl.glEnable(GL10.GL_LIGHTING);
      gl.glEnable(GL10.GL_LIGHT0);
      gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
      gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
      gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
      

      
      // What is the cube made of?
      float matAmbient[] = new float[] { 1, 1, 1, 1 };
      float matDiffuse[] = new float[] { 1, 1, 1, 1 };
      gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,
            matAmbient, 0);
      gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE,
            matDiffuse, 0);
      
      

      // Set up any other options we need
      gl.glEnable(GL10.GL_DEPTH_TEST);       gl.glDepthFunc(GL10.GL_LEQUAL);
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      
      
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
      gl.glEnable(GL11ExtensionPack.GL_TEXTURE_CUBE_MAP);      //gl.glEnable(GL11ExtensionPack.GL_TEXTURE_GEN_STR);      gl.glEnable(GL10.GL_TEXTURE_2D);
      
      
      // Optional: disable dither to boost performance
      // gl.glDisable(GL10.GL_DITHER);
      

      
      // ...
      if (SEE_THRU) {
         gl.glDisable(GL10.GL_DEPTH_TEST);
         gl.glEnable(GL10.GL_BLEND);
         gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
      }
            switch (type) {
      case OpenGL.TYPE_CUBE:
    	  GLCube.loadTexture(gl, view.getContext(), cube_pictures);    	  break;      case OpenGL.TYPE_RECTANGLE:    	  GLRectangle.loadTexture(gl, view.getContext(), rec_picture);      }      
   }
     
   private void drawFrame(GL10 gl) {
      
      
      // Clear the screen to black
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT
            | GL10.GL_DEPTH_BUFFER_BIT);

      // Position model so we can see it
      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();
      gl.glTranslatef(0, 0, -3.0f);

      // Other drawing commands go here...
      
      
      // ... Set rotation angle based on the time
      long elapsed = System.currentTimeMillis() - startTime;
      // ... Draw the model
      switch (type) {      case OpenGL.TYPE_CUBE:          gl.glRotatef(elapsed * (360f / 1000f)* 2.0f, 0, 1, 0);          gl.glRotatef(elapsed * (15f / 1000f), 1, 0, 0);    	  cube.draw(gl);    	  break;      case OpenGL.TYPE_RECTANGLE:          gl.glRotatef(elapsed * (180f / 1000f), 1, 0, 0);    	  rectangle.draw(gl);    	  break;    	        }   }
   
   public void setRunning(boolean b) {	   mRun = b;   }
   

   
   public void requestExitAndWait() {
      // Tell the thread to quit
	   
      try {
         join();
      } catch (InterruptedException ex) {
         // Ignore
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -