以下代码有一个activity类 和一些 renderer的渲染类
在附件里有项目工程,这里共享给大家一起研究下
有什么好的提议,欢迎给我留言啊。
activity:
package com.tracy; import android.app.Activity; import android.graphics.Color; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.widget.LinearLayout; public class Test extends Activity { /** Called when the activity is first created. */ GLSurfaceView glSurfaceView; GLTexRender glRender; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); glSurfaceView = new GLSurfaceView(this); glRender = new GLTexRender(this); glSurfaceView.setRenderer(glRender); // Log.i("tracy", "glSurfaceView"+glSurfaceView.getTop()+" "+glSurfaceView.getWidth()+" "+glSurfaceView.getHeight()); LinearLayout ll = new LinearLayout(this); ll.addView(glSurfaceView); ll.setBackgroundColor(Color.WHITE); setContentView(ll); Log.i("tracy", "glSurfaceView"+glSurfaceView.getTop()+" "+glSurfaceView.getWidth()+" "+glSurfaceView.getHeight()); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); glSurfaceView.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); glSurfaceView.onResume(); } // @Override // public boolean onKeyDown(int keyCode, KeyEvent event) { // Log.i("tracy", "onKeyDown"); // switch(keyCode){ // // case KeyEvent.KEYCODE_DPAD_LEFT: // glRender.yflag=-1; // glRender.key=true; // glRender.leftOrRight=true; // glRender.upOrDown=false; // break; // case KeyEvent.KEYCODE_DPAD_RIGHT: // glRender.yflag=1; // glRender.key=true; // glRender.leftOrRight=true; // glRender.upOrDown=false; // break; // case KeyEvent.KEYCODE_DPAD_UP: // glRender.xflag=-1; // glRender.key=true; // glRender.leftOrRight=false; // glRender.upOrDown=true; // break; // case KeyEvent.KEYCODE_DPAD_DOWN: // glRender.xflag=1; // glRender.key=true; // glRender.leftOrRight=false; // glRender.upOrDown=true; // break; // case KeyEvent.KEYCODE_DPAD_CENTER: // Log.i("tracy", "KEYCODE_DPAD_CENTER"); // if(glRender.onOrOffLight){ // glRender.onOrOffLight=false; // }else{ // glRender.onOrOffLight=true; // } // glRender.fogMode(); // break; // } // return super.onKeyDown(keyCode, event); // } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log.i("tracy", "onKeyUp"); // glRender.key=false; return super.onKeyUp(keyCode, event); } }
GLRender.java
package com.tracy; 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.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer; import android.util.Log; public class GLRender implements Renderer { public GLRender(Context context) { bm[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); // TODO Auto-generated constructor stub } private static final String tag = "tracy"; private int one = 0x10000; // 三角形旋转角度 // private float rotateTri = 0.2f; private int[] a = { 0, 40000, 0, 0, 0, 0, 60000, 0, 0 }; // 三角形三个顶点 // private IntBuffer triggerBuffer = IntBuffer.wrap(a); // private IntBuffer texBuffer = IntBuffer.wrap(new int[] { 0, 40000, 0, 0, // 60000, 0 }); int[] tex={0, 0, 1, 0, 0, 1,1,1 }; private int[] b = { one, 0, 0, one, 0, one, 0, one, 0, 0, one, one }; private IntBuffer colorBuffer = IntBuffer.wrap(b); @Override public void onDrawFrame(GL10 gl) { // TODO Auto-generated method stub gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glColor4f(1, 0, 0, 0); gl.glLoadIdentity(); gl.glTranslatef(-0.5f, 0.0f, -1.0f); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // rotateTri+=0.5f; // gl.glRotatef(rotateTri, 0, 1, 0); gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer); gl.glVertexPointer(3, GL10.GL_FIXED, 0, getNativeIntBuffer(a)); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, getNativeIntBuffer(tex)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO Auto-generated method stub Log.i(tag, "onSurfaceChanged"); 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(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub Log.i(tag, "onSurfaceCreated"); // 启用阴影平滑 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); // 允许2D贴图,纹理 gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnable(GL10.GL_CULL_FACE); IntBuffer intBuffer = IntBuffer.allocate(1); gl.glGenTextures(1, intBuffer); texture = intBuffer.array(); // 设置要使用的纹理 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[0], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); } int[] texture; Bitmap[] bm = new Bitmap[5]; private IntBuffer getNativeIntBuffer(int[] buffer) { ByteBuffer ibb = ByteBuffer.allocateDirect(buffer.length * 4); ibb.order(ByteOrder.nativeOrder()); IntBuffer ibf = ibb.asIntBuffer(); ibf.put(buffer); ibf.position(0); return ibf; } }
GLTexRender.java
package com.tracy; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer; public class GLTexRender implements Renderer { Bitmap[] bm = new Bitmap[6]; float rotx=0.2f; float roty=0.2f; float rotz=0.2f; public GLTexRender(Context context) { super(); bm[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.a); bm[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.b); bm[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.c); bm[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.d); bm[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.e); bm[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.f); } int one = 0x10000; private IntBuffer quaterBuffer = IntBuffer.wrap(new int[]{ // FRONT -one, -one, one, one, -one, one, -one, one, one, one, one, one, // BACK -one, -one, -one, -one, one, -one, one, -one, -one, one, one, -one, // LEFT -one, -one, one, -one, one, one, -one, -one, -one, -one, one, -one, // RIGHT one, -one, -one, one, one, -one, one, -one, one, one, one, one, // TOP -one, one, one, one, one, one, -one, one, -one, one, one, -one, // BOTTOM -one, -one, one, -one, -one, -one, one, -one, one, one, -one, -one, }); IntBuffer texCoords=IntBuffer.wrap(new int[]{ 0,one,one,one,0,0,one,0, one,one,one,0,0,one,0,0, one,one,one,0,0,one,0,0, one,one,one,0,0,one,0,0, one,0,0,0,one,one,0,one, 0,0,0,one,one,0,one,one, }); @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -5.0f); gl.glRotatef(rotx, 1, 0, 0); gl.glRotatef(roty, 0, 1, 0); gl.glRotatef(rotz, 0, 0, 1); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords); this.bindTexturePic(gl); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); rotx+=0.5f; roty+=0.5f; rotz+=0.5f; } //指定纹理图片 private void bindTexturePic(GL10 gl){ gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); // gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); // gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); // gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); // gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); // gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); // gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { 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(); } @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); //允许2D贴图,纹理 gl.glEnable(GL10.GL_TEXTURE_2D); // gl.glEnable(GL10.GL_CULL_FACE); toTex(gl); } int[] texture= new int[6]; //纹理映射 private void toTex(GL10 gl){ IntBuffer intBuffer = IntBuffer.allocate(6); gl.glGenTextures(6, intBuffer); texture=intBuffer.array(); // 设置要使用的纹理 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[0], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[1], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[2], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[3], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[4], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[5], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); } }
GLBlendRender.java
package com.tracy; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer; public class GLBlendRender implements Renderer { Bitmap[] bm = new Bitmap[6]; float rotx=0.2f; float roty=0.2f; float rotz=0.2f; public boolean key=true; public GLBlendRender(Context context) { super(); bm[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.a); bm[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.b); bm[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.c); bm[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.d); bm[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.e); bm[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.f); } int one = 0x10000; private IntBuffer quaterBuffer = IntBuffer.wrap(new int[]{ // FRONT -one, -one, one, one, -one, one, -one, one, one, one, one, one, // BACK -one, -one, -one, -one, one, -one, one, -one, -one, one, one, -one, // LEFT -one, -one, one, -one, one, one, -one, -one, -one, -one, one, -one, // RIGHT one, -one, -one, one, one, -one, one, -one, one, one, one, one, // TOP -one, one, one, one, one, one, -one, one, -one, one, one, -one, // BOTTOM -one, -one, one, -one, -one, -one, one, -one, one, one, -one, -one, }); //纹理坐标 IntBuffer texCoords=IntBuffer.wrap(new int[]{ 0,one,one,one,0,0,one,0, one,one,one,0,0,one,0,0, one,one,one,0,0,one,0,0, one,one,one,0,0,one,0,0, one,0,0,0,one,one,0,one, 0,0,0,one,one,0,one,one, }); @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -5.0f); gl.glRotatef(rotx, 1, 0, 0); gl.glRotatef(roty, 0, 1, 0); gl.glRotatef(rotz, 0, 0, 1); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords); this.bindTexturePic(gl); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); rotx+=0.5f; roty+=0.5f; rotz+=0.5f; //混合开关 if (key) { gl.glEnable(GL10.GL_BLEND); // 打开混合 gl.glDisable(GL10.GL_DEPTH_TEST); // 关闭深度测试 } else { gl.glDisable(GL10.GL_BLEND); // 关闭混合 gl.glEnable(GL10.GL_DEPTH_TEST); // 打开深度测试 } } //指定纹理图片 private void bindTexturePic(GL10 gl){ gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { 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(); } @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); //设置光线,,1.0f为全光线,a=50% gl.glColor4f(1.0f,1.0f,1.0f,0.5f); // 基于源象素alpha通道值的半透明混合函数 gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE); //允许2D贴图,纹理 gl.glEnable(GL10.GL_TEXTURE_2D); // gl.glEnable(GL10.GL_CULL_FACE); toTex(gl); } int[] texture= new int[6]; //纹理映射 private void toTex(GL10 gl){ IntBuffer intBuffer = IntBuffer.allocate(6); gl.glGenTextures(6, intBuffer); texture=intBuffer.array(); // 设置要使用的纹理 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[0], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[1], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[2], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[3], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[4], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[5], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); } public void OnKeyDown(){ if(key){ key=false; }else{ key=true; } } }
GLFogRender.java
package com.tracy; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer; import android.util.Log; public class GLFogRender implements Renderer { Bitmap[] bm = new Bitmap[6]; float rotx = 0.2f; float roty = 0.2f; float rotz = 0.2f; public int xflag = 1; public int yflag = 1; public boolean leftOrRight = false; public boolean upOrDown = false; public boolean key = false; // public float pz; public GLFogRender(Context context) { super(); bm[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.a); bm[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.b); bm[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.c); bm[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.d); bm[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.e); bm[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.f); } int one = 0x10000; private IntBuffer quaterBuffer = IntBuffer .wrap(new int[] { // FRONT -one, -one, one, one, -one, one, -one, one, one, one, one, one, // BACK -one, -one, -one, -one, one, -one, one, -one, -one, one, one, -one, // LEFT -one, -one, one, -one, one, one, -one, -one, -one, -one, one, -one, // RIGHT one, -one, -one, one, one, -one, one, -one, one, one, one, one, // TOP -one, one, one, one, one, one, -one, one, -one, one, one, -one, // BOTTOM -one, -one, one, -one, -one, -one, one, -one, one, one, -one, -one, }); IntBuffer texCoords = IntBuffer.wrap(new int[] { 0, one, one, one, 0, 0, one, 0, one, one, one, 0, 0, one, 0, 0, one, one, one, 0, 0, one, 0, 0, one, one, one, 0, 0, one, 0, 0, one, 0, 0, 0, one, one, 0, one, 0, 0, 0, one, one, 0, one, one, }); // 定义环境光源数组 float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f }; // 定义漫射光源数组 float[] lightDiffuse = { 1.0f, 1.0f, 0.5f, 1.0f }; // 光源位置 float[] lightPosition = { 0.0f, 0.0f, 2.0f, 1.0f }; float fogColor[] = { 0.8f, 0.2f, 0.2f, 1.0f }; // 雾的颜色设为白色 int fogMode[] = { GL10.GL_EXP, GL10.GL_EXP2, GL10.GL_LINEAR }; // 雾气的模式 int fogfilter = 0; // 使用哪一种雾气 GL10 mGl10; public void fogMode() { Log.i("tracy", "fogfilter="+fogfilter); mGl10.glFogf(GL10.GL_FOG_MODE, fogMode[fogfilter++]); // 设置雾气的模式 fogfilter = fogfilter > 2 ? 0 : fogfilter; } @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -5.0f); // 如果不启用GL_LIGHTING光就什么都看不见 gl.glEnable(GL10.GL_LIGHTING); // gl.glRotatef(rotz, 0, 0, 1); gl.glRotatef(roty, 0, 1, 0); gl.glRotatef(rotx, 1, 0, 0); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords); this.bindTexturePic(gl); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); if (key) { if (upOrDown) { rotx += 0.5f * xflag; } if (leftOrRight) { roty += 0.5f * yflag; } // rotz+=0.5f; } if (onOrOffLight) { gl.glEnable(GL10.GL_LIGHT1); } else { gl.glDisable(GL10.GL_LIGHT1); } } // 指定纹理图片 private void bindTexturePic(GL10 gl) { gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { 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(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { mGl10 = gl; // 启用阴影平滑 gl.glShadeModel(GL10.GL_SMOOTH); // // 设置背景的颜色为雾气的颜色 gl.glClearColor(0.8f,0.2f,0.2f,1.0f); gl.glFogx(GL10.GL_FOG_MODE, fogMode[fogfilter]); // 设置雾气的模式 gl.glFogfv(GL10.GL_FOG_COLOR, fogColor,0); // 设置雾的颜色 gl.glFogf(GL10.GL_FOG_DENSITY, 0.35f); // 设置雾的密度 gl.glHint(GL10.GL_FOG_HINT, GL10.GL_DONT_CARE); // 设置系统如何计算雾气 gl.glFogf(GL10.GL_FOG_START, 1.0f); // 雾气的开始位置 gl.glFogf(GL10.GL_FOG_END, 5.0f); // 雾气的结束位置 gl.glEnable(GL10.GL_FOG); // 使用雾气 // 设置深度缓存 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); // 允许2D贴图,纹理 gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnable(GL10.GL_CULL_FACE); toTex(gl); initLight(gl); } // 初始化光源 private void initLight(GL10 gl) { // 设置环境光 gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, getNativeBuffer(lightAmbient)); // set diffuse light gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, getNativeBuffer(lightDiffuse)); // set light position gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, getNativeBuffer(lightPosition)); } public boolean onOrOffLight = true; int[] texture = new int[6]; // 纹理映射 private void toTex(GL10 gl) { IntBuffer intBuffer = IntBuffer.allocate(6); gl.glGenTextures(6, intBuffer); texture = intBuffer.array(); // 设置要使用的纹理 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[0], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[1], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[2], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[3], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[4], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[5], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); } private FloatBuffer getNativeBuffer(float[] buffer) { ByteBuffer ibb = ByteBuffer.allocateDirect(buffer.length * 8); ibb.order(ByteOrder.nativeOrder()); FloatBuffer fb = ibb.asFloatBuffer(); fb.put(buffer); fb.position(0); return fb; } }
GLLightRender.java
package com.tracy; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer; public class GLLightRender implements Renderer { Bitmap[] bm = new Bitmap[6]; float rotx=0.2f; float roty=0.2f; float rotz=0.2f; public int xflag=1; public int yflag=1; public boolean leftOrRight=false; public boolean upOrDown=false; public boolean key=false; // public float pz; public GLLightRender(Context context) { super(); bm[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.a); bm[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.b); bm[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.c); bm[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.d); bm[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.e); bm[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.f); } int one = 0x10000; private IntBuffer quaterBuffer = IntBuffer.wrap(new int[]{ // FRONT -one, -one, one, one, -one, one, -one, one, one, one, one, one, // BACK -one, -one, -one, -one, one, -one, one, -one, -one, one, one, -one, // LEFT -one, -one, one, -one, one, one, -one, -one, -one, -one, one, -one, // RIGHT one, -one, -one, one, one, -one, one, -one, one, one, one, one, // TOP -one, one, one, one, one, one, -one, one, -one, one, one, -one, // BOTTOM -one, -one, one, -one, -one, -one, one, -one, one, one, -one, -one, }); IntBuffer texCoords=IntBuffer.wrap(new int[]{ 0,one,one,one,0,0,one,0, one,one,one,0,0,one,0,0, one,one,one,0,0,one,0,0, one,one,one,0,0,one,0,0, one,0,0,0,one,one,0,one, 0,0,0,one,one,0,one,one, }); //定义环境光源数组 float[] lightAmbient={0.5f,0.5f,0.5f,1.0f}; //定义漫射光源数组 float[] lightDiffuse={1.0f,1.0f,0.5f,1.0f}; //光源位置 float[] lightPosition={0.0f,0.0f,2.0f,1.0f}; @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -5.0f); //如果不启用GL_LIGHTING光就什么都看不见 gl.glEnable(GL10.GL_LIGHTING); // gl.glRotatef(rotz, 0, 0, 1); gl.glRotatef(roty, 0, 1, 0); gl.glRotatef(rotx, 1, 0, 0); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords); this.bindTexturePic(gl); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); if(key){ if(upOrDown){ rotx+=0.5f*xflag; } if(leftOrRight){ roty+=0.5f*yflag; } // rotz+=0.5f; } if(onOrOffLight){ gl.glEnable(GL10.GL_LIGHT1); }else{ gl.glDisable(GL10.GL_LIGHT1); } } //指定纹理图片 private void bindTexturePic(GL10 gl){ gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { 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(); } @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); //允许2D贴图,纹理 gl.glEnable(GL10.GL_TEXTURE_2D); gl.glEnable(GL10.GL_CULL_FACE); toTex(gl); initLight(gl); } //初始化光源 private void initLight(GL10 gl){ //设置环境光 gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, getNativeBuffer(lightAmbient)); //set diffuse light gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, getNativeBuffer(lightDiffuse)); //set light position gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, getNativeBuffer(lightPosition)); } public boolean onOrOffLight=true; int[] texture= new int[6]; //纹理映射 private void toTex(GL10 gl){ IntBuffer intBuffer = IntBuffer.allocate(6); gl.glGenTextures(6, intBuffer); texture=intBuffer.array(); // 设置要使用的纹理 gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[0], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[1], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[2], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[3], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[4], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bm[5], 0); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); } private FloatBuffer getNativeBuffer(float[] buffer){ ByteBuffer ibb = ByteBuffer.allocateDirect(buffer.length * 8); ibb.order(ByteOrder.nativeOrder()); FloatBuffer fb =ibb.asFloatBuffer(); fb.put(buffer); fb.position(0); return fb; } }