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

📄 mysurfaceview.java

📁 基于ANdroid平台的高欢堂书籍的对应源代码 非常有用
💻 JAVA
字号:
package com.misoo.ppxx;import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceView;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;class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {	     SurfaceHolder mHolder;	     private GLThread mGLThread;	//     private boolean mHasSurface;	     private MyCube mCube_cb, mCube_pr;	     private float mAngle;	     private boolean isCube;	     MySurfaceView(Context context) {	         super(context);	         init();	     }	     public void setCube(boolean cb) {				// TODO Auto-generated method stub	       	 isCube = cb;         }	     public MySurfaceView(Context context, AttributeSet attrs) {	         super(context, attrs);	         init();	     }	     	     private void init() {	         mHolder = getHolder();	         mHolder.addCallback(this);	         mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);	     }	     	     public void surfaceCreated(SurfaceHolder holder) {	         mGLThread = new GLThread();	         mGLThread.start();	     }	     public void surfaceDestroyed(SurfaceHolder holder) {	         mGLThread.requestExitAndWait();	         mGLThread = null;	     }	     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {	         mGLThread.onWindowResize(w, h);	     }	     // ----------------------------------------------------------------------	     class GLThread extends Thread {	         private boolean mDone;	         private boolean mSizeChanged = true;	         private int mWidth;	         private int mHeight;	         	         GLThread() {	             super();	             mDone = false;	             mWidth = 0;	             mHeight = 0;	             	        /*     byte indices_pr[]= {		        			6, 0, 1,   5, 1, 0,		            		1, 5, 6,   0, 6, 5,		             	    2, 3, 7,   6, 2, 7,		              };  		              */	             byte indices_cb[] = {	            			0, 7, 3,    7, 0, 4,	            		                4, 0, 5,    5, 0, 1,	            		                5, 1, 2,    5, 2, 6,	            		                6, 2, 7,    7, 2, 3,	            		                2, 1, 3,    3, 1, 0,	            		                7, 4, 5,    6, 7, 5,	            		        };	             byte indices_pr[] = {           			6, 0, 1,  5, 1, 0,	              		1, 5, 6,  0, 6, 5,	               };/*	             byte indices[] = {	                     0, 4, 5,    0, 5, 1,	                     1, 5, 6,    1, 6, 2,	                     2, 6, 7,    2, 7, 3,	                     3, 7, 4,    3, 4, 0,	                     4, 7, 6,    4, 6, 5,	                     3, 0, 1,    3, 1, 2	             };	*/           mCube_cb = new MyCube(indices_cb);	             mCube_pr = new MyCube(indices_pr); 	         }	     	         @Override	            public void run() {	               	                EGL10 egl = (EGL10)EGLContext.getEGL();	               	                EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);	                int[] version = new int[2];	                egl.eglInitialize(dpy, version);	                int[] configSpec = {	                        EGL10.EGL_RED_SIZE,      8,	                        EGL10.EGL_GREEN_SIZE,    8,	                        EGL10.EGL_BLUE_SIZE,     8,	                        EGL10.EGL_DEPTH_SIZE,   16,	                        EGL10.EGL_NONE	                };	                EGLConfig[] configs = new EGLConfig[1];	                int[] num_config = new int[1];	                egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);	                EGLConfig config = configs[0];	                EGLContext glc = egl.eglCreateContext(dpy, config,	                        EGL10.EGL_NO_CONTEXT, null);	                EGLSurface surface = null;	                GL10 gl = null;	                while (!mDone) {		                 		                 /*		                  *  Update the asynchronous state (window size, key events)		                  */		                 int w, h;		                 boolean changed;		                 synchronized(this) {		                     changed = mSizeChanged;		                     w = mWidth;		                     h = mHeight;		                     mSizeChanged = false;		                 }		     		                 if (changed) {		                     		                     surface = egl.eglCreateWindowSurface(dpy, config, mHolder,		                             null);		         		                     /*		                      * Before we can issue GL commands, we need to make sure 		                      * the context is current and bound to a surface.		                      */		                     egl.eglMakeCurrent(dpy, surface, surface, glc);		                     		                     /*		                      * Get to the appropriate GL interface.		                      * This is simply done by casting the GL context to either		                      * GL10 or GL11.		                      */		                     gl = (GL10)glc.getGL();		                    		                     /*		                      * By default, OpenGL enables features that improve quality		                      * but reduce performance. One might want to tweak that		                      * especially on software renderer.		                      */		                     gl.glDisable(GL10.GL_DITHER);		         		                     /*		                      * Some one-time OpenGL initialization can be made here		                      * probably based on features of this particular context		                      */		                      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,		                              GL10.GL_FASTEST);		         		                      gl.glClearColor(1,1,1,1);		                      gl.glEnable(GL10.GL_CULL_FACE);		                      gl.glShadeModel(GL10.GL_SMOOTH);		                      gl.glEnable(GL10.GL_DEPTH_TEST);		  		                      gl.glViewport(0, 0, w, h);		                      /*		                       * Set our projection matrix. This doesn't have to be done		                       * each time we draw, but usually a new projection needs to		                       * be set when the viewport is resized.		                       */		                      float ratio = (float)w / h;		                      gl.glMatrixMode(GL10.GL_PROJECTION);		                      gl.glLoadIdentity();		                      gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);		                 } 		               		                 drawFrame(gl);		                 		                 egl.eglSwapBuffers(dpy, surface);		                 if (egl.eglGetError() == EGL11.EGL_CONTEXT_LOST) {		                     // we lost the gpu, quit immediately		                     Context c = getContext();		                     if (c instanceof Activity) {		                         ((Activity)c).finish();		                     }		                 }		             }		                          		             		             egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,		                     EGL10.EGL_NO_CONTEXT);		             egl.eglDestroySurface(dpy, surface);		             egl.eglDestroyContext(dpy, glc);		             egl.eglTerminate(dpy);		         }		         	         	         private void drawFrame(GL10 gl) {	             gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);	             /*	              * Now we're ready to draw some 3D objects	             */                 //    while(true)                 //    {	             gl.glMatrixMode(GL10.GL_MODELVIEW);	             gl.glLoadIdentity();	             gl.glTranslatef(0, 0, -3.0f);	             gl.glRotatef(mAngle,        0, 1, 0);	             gl.glRotatef(mAngle*0.25f,  1, 0, 0);	             gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);	             gl.glEnableClientState(GL10.GL_COLOR_ARRAY);	             mCube_pr.draw(gl);	             gl.glRotatef(mAngle*2.0f, 0, 1, 1);	             gl.glTranslatef(0.5f, 0.5f, 0.5f);                 if(isCube)	                 mCube_cb.draw(gl);	             mAngle += 1.2f;             //    }	         }	         public void onWindowResize(int w, int h) {	             synchronized(this) {	                 mWidth = w;	                 mHeight = h;	                 mSizeChanged = true;	             }	         }	         	         public void requestExitAndWait() {	             // don't call this from GLThread thread or it is a guaranteed	             // deadlock!	             mDone = true;	             try {	                 join();	             } catch (InterruptedException ex) { }	         }	     }			 }

⌨️ 快捷键说明

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