之前学会了基本图形的绘制,本来打算立刻弄一个触摸->平移、旋转、缩放的,但是发现如果没有坐标轴的话单凭感觉画出来的东西跟自己的意图总是不太一致。还是得先搞好基础
public class Axis extends Shape {float[] axis = {0f, 0f, 0f,100, 0, 0,0, 100, 0,0, 0, 100,};//X轴、Y轴、Z轴private FloatBuffer axisBuffer;//XY平面的网格线private FloatBuffer gridBuffer;private FloatBuffer colorBuffer;private ShortBuffer drawListBuffer;private short[] indexOrder = {0, 1,0, 2,0, 3};private final String vertexShaderCode =// This matrix member variable provides a hook to manipulate// the coordinates of the objects that use this vertex shader"uniform mat4 uMVPMatrix;" +"attribute vec4 vPosition;" +"varying vec4 vColor;" +"attribute vec4 aColor;" +"void main() {" +// the matrix must be included as a modifier of gl_Position// Note that the uMVPMatrix factor *must be first* in order// for the matrix multiplication product to be correct." gl_Position = uMVPMatrix * vPosition;" +"vColor = aColor;" +"}";private final String fragmentShaderCode ="precision mediump float;" +"varying vec4 vColor;" +"void main() {" +" gl_FragColor = vColor;" +"}";//设置颜色float color[] = {0.0f, 1.0f, 0.0f, 1.0f,1.0f, 0.0f, 0.0f, 1.0f,0.0f, 0.0f, 1.0f, 1.0f};private int program;private int positionHandle;private int colorHandle;private int matrixHandle;public Axis() {ByteBuffer byteBuffer = ByteBuffer.allocateDirect(axis.length * 4);byteBuffer.order(ByteOrder.nativeOrder());axisBuffer = byteBuffer.asFloatBuffer();axisBuffer.put(axis);axisBuffer.position(0);ByteBuffer clByteBuffer = ByteBuffer.allocateDirect(color.length * 4);clByteBuffer.order(ByteOrder.nativeOrder());colorBuffer = clByteBuffer.asFloatBuffer();colorBuffer.put(color);colorBuffer.position(0);ByteBuffer orderbb = ByteBuffer.allocateDirect(indexOrder.length * 2);orderbb.order(ByteOrder.nativeOrder());drawListBuffer = orderbb.asShortBuffer();drawListBuffer.put(indexOrder);drawListBuffer.position(0);program = GLES20.glCreateProgram();int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);GLES20.glAttachShader(program, vertexShader);GLES20.glAttachShader(program, fragmentShader);GLES20.glLinkProgram(program);}public void draw(float[] mvpMatrix) {GLES20.glUseProgram(program);positionHandle = GLES20.glGetAttribLocation(program, "vPosition");GLES20.glEnableVertexAttribArray(positionHandle);GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, axisBuffer);colorHandle = GLES20.glGetAttribLocation(program, "aColor");GLES20.glEnableVertexAttribArray(colorHandle);GLES20.glVertexAttribPointer(colorHandle, 4, GLES20.GL_FLOAT, false, 0, colorBuffer);matrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");GLES20.glUniformMatrix4fv(matrixHandle, 1, false, mvpMatrix, 0);// GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, axis.length / 3);GLES20.glDrawElements(GLES20.GL_LINES, indexOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);GLES20.glDisableVertexAttribArray(positionHandle);GLES20.glDisableVertexAttribArray(colorHandle);}
}
public class GLRender_Touch implements GLSurfaceView.Renderer {private float[] mvPMatrix = new float[16];private float[] viewMatrix = new float[16];private float[] projectMatrix = new float[16];private float[] rotateMatrix = new float[16];private Axis axis;
// private Grid grid;@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {GLES20.glClearColor(0, 0, 0, 1);Matrix.setLookAtM(viewMatrix, 0, 300, 300, 300, 0, 0, 0, 0, 1, 0);axis = new Axis();
// grid = new Grid();}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height) {GLES20.glViewport(0, 0, width, height);float ratio = width * 1.0f / height;Matrix.frustumM(projectMatrix, 0, -ratio, ratio, -1, 1, 3, 1000);}@Overridepublic void onDrawFrame(GL10 gl) {GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);Matrix.multiplyMM(mvPMatrix, 0, projectMatrix, 0, viewMatrix, 0);/*float[] scrach = new float[16];long time = SystemClock.uptimeMillis() % 4000L;float angle = 0.090f * ((int) time);Matrix.setRotateM(rotateMatrix, 0, angle, 1, 0, 0);Matrix.multiplyMM(scrach, 0, mvPMatrix, 0, rotateMatrix, 0);*/axis.draw(mvPMatrix);
// grid.draw(mvPMatrix);}
}
为了节约时间,三个坐标轴一次性画了,一个一个画的话可以设置三个不同的颜色,后面做平移、旋转、缩放 有一个相对位置,比较直观