最近迫于生存压力,不得不给人兼职打工。故在博文中加了个求点击的链接。麻烦有时间的博友们帮我点击一下。没时间的不用勉强啊。不过请放心,我是做技术的,肯定链接没病毒,就是我打工的淘宝店铺。嘻嘻。http://shop108130013.taobao.com。谢谢捧场。以后就每周写篇原创的技术博客回报大家,实在是迫于生计,无所不用其极。请谅解。
做项目就是一个阶段一个专注点。最近被安排做OpenGL ES相关的开发。从头学起。先一点一点啃啃现有书籍吧。今天按照<Pro OpenGL ES for Android >一书第一章做成一个上下跳动的正方形。
完整的代码上传到资源页了:http://download.csdn.net/download/fener10289/5243696
现将主要的java处理代码贴上。
package book.BouncySquare; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; /** * A vertex shaded square. */ class Square { public Square() { float vertices[] = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f }; byte maxColor=(byte)255; byte colors[] = { maxColor,maxColor, 0,maxColor, 0, maxColor,maxColor,maxColor, 0, 0, 0,maxColor, maxColor, 0,maxColor,maxColor }; byte indices[] = { 0, 3, 1, 0, 2, 3 }; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); mFVertexBuffer = vbb.asFloatBuffer(); mFVertexBuffer.put(vertices); mFVertexBuffer.position(0); mColorBuffer = ByteBuffer.allocateDirect(colors.length); mColorBuffer.put(colors); mColorBuffer.position(0); mIndexBuffer = ByteBuffer.allocateDirect(indices.length); mIndexBuffer.put(indices); mIndexBuffer.position(0); } public void draw(GL10 gl) { gl.glFrontFace(GL11.GL_CW); gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer); gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer); gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer); gl.glFrontFace(GL11.GL_CCW); } private FloatBuffer mFVertexBuffer; private ByteBuffer mColorBuffer; private ByteBuffer mIndexBuffer; }
程序的BouncySquareActivity相对于系统默认的Activity有了些改变。即GLSurfaceView实际上使用SquareRenderer来定位和圈定边界的渲染。代码如下:
package book.BouncySquare; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.WindowManager; import book.BouncySquare.*; public class BouncySquareActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); GLSurfaceView view = new GLSurfaceView(this); view.setRenderer(new SquareRenderer(true)); setContentView(view); } }
SquareRenderer.java,opengl es程序中最重要的渲染。所有的效果都在这个类中实现。
package book.BouncySquare; //在此工程中EGL库隐藏于GLSurfaceview中绑定OpenGL画surface给系统。 //注:EGL主要用来定位和管理surface,是OpenGL ES的扩展,其关于平台独立。 import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import android.opengl.GLSurfaceView; import java.lang.Math; class SquareRenderer implements GLSurfaceView.Renderer { public SquareRenderer(boolean useTranslucentBackground) { mTranslucentBackground = useTranslucentBackground; mSquare = new Square(); //square 对象被创建和储存 } //onDrawFrame是最root的刷新方法 public void onDrawFrame(GL10 gl) { //第一件事典型的清屏动作 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //以下两行是设置具体值确使实例几何体可见 gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); //使box上下移动;为了获得漂亮的,平滑的动作,实际上的移动基于sine曲线 gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f); //以下两行告诉OpenGL希望vertex和color数据 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); //调用Square的draw方法,绘制路径 mSquare.draw(gl); mTransY += .075f; } //此函数在第一次创建或是屏幕改变时都会调用。其也用于建立视锥体,你可以实际看到的空间体积。 public void onSurfaceChanged(GL10 gl, int width, int height) { //只是允许你定义实际维数和OpenGL窗体的位置 //典型的,当location为0时,其将是主屏幕的大小 gl.glViewport(0, 0, width, height); float ratio = (float) width / height; //设置矩形模式为GL_PROJECTION式,投影3D影像至2D屏幕 gl.glMatrixMode(GL11.GL_PROJECTION); //重新设置矩形模式,擦除之前的设置 gl.glLoadIdentity(); //现在可以使用6个参数设置椎体实际的大小 //near/far,left/right,top/bottom gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); } //一些初始化操作在这个函数中执行 public void onSurfaceCreated(GL10 gl, EGLConfig config) { //确认任何抖动效果被关闭,因为默认其是被被打开的 //OpenGl的抖动会使屏幕在有限的色块中看起来更好些,但是同时会消耗性能 gl.glDisable(GL11.GL_DITHER); //glHint推进 gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,GL11.GL_FASTEST); if (mTranslucentBackground) { gl.glClearColor(0,0,0,0); } else { gl.glClearColor(1,1,1,1); } gl.glEnable(GL11.GL_CULL_FACE); gl.glShadeModel(GL11.GL_SMOOTH); gl.glEnable(GL11.GL_DEPTH_TEST); } private boolean mTranslucentBackground; private Square mSquare; private float mTransY; private float mAngle; }