先上第一个demo,关于物体cube在空间中的平移,(理解了这个,再学习接下来的旋转和缩放,就简单的多了···)
第一个文件:ShaderUtil.java————(主要作用是,生成渲染器,再生成渲染器程序)
package com.example.sample_5_3_mine; import java.io.ByteArrayOutputStream; import java.io.InputStream; import android.content.res.Resources; import android.opengl.GLES20; import android.util.Log; public class ShaderUtil { public static int loadShader( int shaderType, String source){ int shader = GLES20.glCreateShader(shaderType); if(shader!=0){ GLES20.glShaderSource(shader, source); GLES20.glCompileShader(shader); int[] compiled = new int[1]; GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); if(compiled[0] == 0){ Log.e("ES_ERROR","Could not compiled shader"); Log.e("ES_ERROR",GLES20.glGetShaderInfoLog(shader)); GLES20.glDeleteShader(shader); shader = 0; } } return shader; } public static int createProgram( String vertexSource, String fragmentSource){ int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,vertexSource); if(vertexShader == 0){ return 0; } int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentSource); if(pixelShader == 0){ return 0; } int program = GLES20.glCreateProgram(); if(program != 0){ GLES20.glAttachShader(program,vertexShader); checkGlError("glAttachShader"); GLES20.glAttachShader(program, pixelShader); checkGlError("glAttachShader"); GLES20.glLinkProgram(program); int[] linkStatus = new int[1]; GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus,0); if(linkStatus[0] != GLES20.GL_TRUE){ Log.e("ES20_ERROR","Could not link program"); Log.e("ES_ERROR",GLES20.glGetProgramInfoLog(program)); GLES20.glDeleteProgram(program); program = 0; } } return program; } public static void checkGlError(String op){ int error; while((error = GLES20.glGetError()) !=GLES20.GL_NO_ERROR){ Log.e("ES20_ERROR",op + "glError:" + error); throw new RuntimeException(op + ":glError"); } } public static String loadFromAssetsFile(String fname,Resources r){ String result = null; try{ InputStream in = r.getAssets().open(fname); int ch = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while((ch = in.read()) != -1){ baos.write(ch); } byte[] buff = baos.toByteArray(); baos.close(); in.close(); result = new String(buff,"UTF-8"); result = result.replaceAll("\\r\\n", "\n"); } catch(Exception e){ e.printStackTrace(); } return result; } }
ok、现在改轮到主类:MainActivity.java的编写了:
package com.example.sample_5_3_mine; //以后像这种多个demo.java这种shen me gui!! //应该先在LogCat中查看相应的包类,查看出来第一个出错的是哪一个.java类, //而不是盲目的一次“比较文件”,一比较就是6个!我勒个去!6个啊!! //相比VC++6.0来说,Eclipse的开发节省了很多细节问题,(之前出现的大小写,多一个少一个字符啊,什么的,没了) //哈哈、好开森~~ //错误检查报告: //1、屏幕设置方式出错 || 2、ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4); import android.os.Bundle; import android.app.Activity; import android.content.pm.ActivityInfo; import android.view.Menu; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { private MySurfaceView mGLSurfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //这儿是显示不正确的第二个错误:屏幕显示方式应该为:LANFSCAPE, setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); mGLSurfaceView = new MySurfaceView(this); setContentView(mGLSurfaceView); mGLSurfaceView.requestFocus(); mGLSurfaceView.setFocusableInTouchMode(true); } @Override protected void onPause() { // TODO 自动生成的方法存根 super.onPause(); mGLSurfaceView.onPause(); } @Override protected void onResume() { // TODO 自动生成的方法存根 super.onResume(); mGLSurfaceView.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
现在是MySurfaceView.java文件的编写:
这个类的作用主要用于定义摄像机的位置参数,以及镜头的显示(宽高比等等),
package com.example.sample_5_3_mine; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.opengl.GLES20; import android.opengl.GLSurfaceView; public class MySurfaceView extends GLSurfaceView{ private SceneRenderer mRenderer; public MySurfaceView(Context context) { super(context); // TODO 自动生成的构造函数存根 this.setEGLContextClientVersion(2); mRenderer = new SceneRenderer(); setRenderer(mRenderer); setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); } private class SceneRenderer implements GLSurfaceView.Renderer{ Cube cube; //下面由于三个函数:onDrawFrame()、onSurfaceChanged()、onSurfaceCreated() //调用的时候并非按照顺序进行,而应该是: //创建对象,打开开关->设置摄相机参数->开始绘画屏幕 //onSurfaceCreated()->onSurfaceChanged()->onDrawFrame() @Override public void onDrawFrame(GL10 gl) { // TODO 自动生成的方法存根 GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); MatrixState.pushMatrix(); cube.drawSelf(); MatrixState.popMatrix(); MatrixState.pushMatrix(); MatrixState.translate(4,0,0); cube.drawSelf(); MatrixState.popMatrix(); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // TODO 自动生成的方法存根 GLES20.glViewport(0, 0, width, height); Constant.ratio = (float) width/height; MatrixState.setProjectFrustum(-Constant.ratio*0.8f, Constant.ratio*1.2f, -1, 1, 20, 100); MatrixState.setCamera(-16f, 8f, 45, 0f, 0f, 0f, 0f, 1.0f, 0.0f); MatrixState.setInitStack(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO 自动生成的方法存根 GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); cube = new Cube(MySurfaceView.this); GLES20.glEnable(GLES20.GL_DEPTH_TEST); GLES20.glEnable(GLES20.GL_CULL_FACE); } } }
好了,现在编写MatrixState.java这个类:
这个类主要作用于:矩阵的变换,保存矩阵场景,以及如何通过举证实现物体的平移、旋转、缩放等等函数:
package com.example.sample_5_3_mine; import java.nio.ByteBuffer; import android.opengl.Matrix; public class MatrixState { private static float[] mProjMatrix = new float[16]; private static float[] mVMatrix = new float[16]; private static float[] currMatrix; static float[][] mStack = new float[10][16]; static int stackTop = -1; public static void setInitStack(){ currMatrix = new float[16]; Matrix.setRotateM(currMatrix, 0, 0, 1, 0, 0); } public static void pushMatrix(){ stackTop++; for(int i=0;i<16;i++){ mStack[stackTop][i] = currMatrix[i]; } } public static void popMatrix(){ for(int i=0;i<16;i++){ currMatrix[i] = mStack[stackTop][i]; } stackTop--; } public static void translate(float x,float y, float z){ Matrix.translateM(currMatrix, 0, x, y, z); } static ByteBuffer llbb = ByteBuffer.allocateDirect(3*4); static float[] cameraLocation = new float[3]; public static void setCamera( float cx,float cy, float cz, float tx, float ty, float tz, float upx,float upy, float upz){ Matrix.setLookAtM(mVMatrix, 0, cx, cy, cz, tx, ty, tz, upx, upy, upz); } public static void setProjectFrustum(float left,float right, float bottom, float top, float near, float far){ Matrix.frustumM(mProjMatrix, 0, left, right, bottom, top, near, far); } public static void setProjectOrtho(float left, float right, float bottom, float top, float near,float far){ Matrix.orthoM(mProjMatrix, 0, left, right, bottom, top, near, far); } static float[] mMVPMatrix = new float[16]; public static float[] getFinalMatrix(){ Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, currMatrix, 0); Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); return mMVPMatrix; } public static float[] getMMatrix(){ return currMatrix; } }
嗯、对了,还有一个常量类Constan.java,,它的作用主要是:
申请定义一些常用的变量,直接定义好,一次定义,多次使用~
package com.example.sample_5_3_mine; public class Constant { public static final float UNIT_SIZE = 1f; public static float ratio; }
ok、ok、现在就剩下最后一个类Cube.jaba的定义以及编写了:
它的作用主要是:定义一个正方体Cube,定义好它的大小,位置,各个顶点等数据,同时也包含有自身的drawSelf()方法,用于在屏幕上对自身进行绘画等等,
来啦~~:
package com.example.sample_5_3_mine; //自我总结:本次出错在Cube.java上,其中一个比较重要的原因是:不仔细,前面5个全神贯注敲打了, //这个给漏网之鱼,一不留神就溜走了,下次记住:一定要:仔仔细细敲全部! //还有就是!检查核对的是也没有仔细看! import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import android.opengl.GLES20; public class Cube { int mProgram; int muMVPMatrixHandle; int maPositionHandle; int maColorHandle; String mVertexShader; String mFragmentShader; FloatBuffer mVertexBuffer; FloatBuffer mColorBuffer; int vCount = 0; //构造函数 public Cube(MySurfaceView mv){ initVertexData(); initShader(mv); } public void initVertexData(){ vCount = 12*6; float vertices[] = new float[]{ //前面 0,0,Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, 0,0,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, 0,0,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, 0,0,Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, //后面 0,0,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,0,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,0,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,0,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, //左面 -Constant.UNIT_SIZE,0,0, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,0,0, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,0,0, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,0,0, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, //右面 Constant.UNIT_SIZE,0,0, Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,0,0, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,0,0, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,0,0, Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, //上面 0,Constant.UNIT_SIZE,0, Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,Constant.UNIT_SIZE,0, Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,Constant.UNIT_SIZE,0, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,-Constant.UNIT_SIZE, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, 0,Constant.UNIT_SIZE,0, -Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, Constant.UNIT_SIZE,Constant.UNIT_SIZE,Constant.UNIT_SIZE, //下面 0,-Constant.UNIT_SIZE,0, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, 0,-Constant.UNIT_SIZE,0, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,-Constant.UNIT_SIZE,0, -Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, 0,-Constant.UNIT_SIZE,0, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,-Constant.UNIT_SIZE, Constant.UNIT_SIZE,-Constant.UNIT_SIZE,Constant.UNIT_SIZE, }; //错在这里啦!!! ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4); vbb.order(ByteOrder.nativeOrder()); mVertexBuffer = vbb.asFloatBuffer(); mVertexBuffer.put(vertices); mVertexBuffer.position(0); float colors[] = new float[]{ //前面 1,1,1,0,//中间为白色 1,0,0,0, 1,0,0,0, 1,1,1,0,//中间为白色 1,0,0,0, 1,0,0,0, 1,1,1,0,//中间为白色 1,0,0,0, 1,0,0,0, 1,1,1,0,//中间为白色 1,0,0,0, 1,0,0,0, //后面 1,1,1,0,//中间为白色 0,0,1,0, 0,0,1,0, 1,1,1,0,//中间为白色 0,0,1,0, 0,0,1,0, 1,1,1,0,//中间为白色 0,0,1,0, 0,0,1,0, 1,1,1,0,//中间为白色 0,0,1,0, 0,0,1,0, //左面 1,1,1,0,//中间为白色 1,0,1,0, 1,0,1,0, 1,1,1,0,//中间为白色 1,0,1,0, 1,0,1,0, 1,1,1,0,//中间为白色 1,0,1,0, 1,0,1,0, 1,1,1,0,//中间为白色 1,0,1,0, 1,0,1,0, //右面 1,1,1,0,//中间为白色 1,1,0,0, 1,1,0,0, 1,1,1,0,//中间为白色 1,1,0,0, 1,1,0,0, 1,1,1,0,//中间为白色 1,1,0,0, 1,1,0,0, 1,1,1,0,//中间为白色 1,1,0,0, 1,1,0,0, //上面 1,1,1,0,//中间为白色 0,1,0,0, 0,1,0,0, 1,1,1,0,//中间为白色 0,1,0,0, 0,1,0,0, 1,1,1,0,//中间为白色 0,1,0,0, 0,1,0,0, 1,1,1,0,//中间为白色 0,1,0,0, 0,1,0,0, //下面 1,1,1,0,//中间为白色 0,1,1,0, 0,1,1,0, 1,1,1,0,//中间为白色 0,1,1,0, 0,1,1,0, 1,1,1,0,//中间为白色 0,1,1,0, 0,1,1,0, 1,1,1,0,//中间为白色 0,1,1,0, 0,1,1,0, }; ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4); cbb.order(ByteOrder.nativeOrder()); mColorBuffer = cbb.asFloatBuffer(); mColorBuffer.put(colors); mColorBuffer.position(0); } public void initShader(MySurfaceView mv){ mVertexShader = ShaderUtil.loadFromAssetsFile("vertex.sh", mv.getResources()); mFragmentShader = ShaderUtil.loadFromAssetsFile("frag.sh", mv.getResources()); mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader); maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); maColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor"); muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); } public void drawSelf(){ GLES20.glUseProgram(mProgram); GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(),0); //GLES20.glUniformMatrix4fv(muMV, arg1, arg2, arg3) GLES20.glVertexAttribPointer(maPositionHandle,3,GLES20.GL_FLOAT, false,3*4,mVertexBuffer); GLES20.glVertexAttribPointer(maColorHandle, 4, GLES20.GL_FLOAT, false, 4*4, mColorBuffer); GLES20.glEnableVertexAttribArray(maPositionHandle); GLES20.glEnableVertexAttribArray(maColorHandle); GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,vCount); } }
我了个去,差点忘记了一个重要的demo:
frag.sh文件:
precision mediump float; varying vec4 vColor; //接收从顶点着色器过来的参数 varying vec3 vPosition;//接收从顶点着色器过来的顶点位置 void main() { gl_FragColor = vColor;//给此片元颜色值 }
vertex.sh文件:
uniform mat4 uMVPMatrix; //总变换矩阵 attribute vec3 aPosition; //顶点位置 attribute vec4 aColor; //顶点颜色 varying vec4 vColor; //用于传递给片元着色器的变量 void main() { gl_Position = uMVPMatrix * vec4(aPosition,1); //根据总变换矩阵计算此次绘制此顶点位置 vColor = aColor;//将接收的颜色传递给片元着色器 }