Back to project page mic-check.
The source code is released under:
GNU General Public License
If you think the Android project mic-check listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ruman.experiments.miccheck; //from w w w. j av a2 s.c o m import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer; import java.util.Random; import javax.microedition.khronos.opengles.GL10; import android.opengl.EGLConfig; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.opengl.Matrix; public class MyRenderer implements GLSurfaceView.Renderer { private Triangle mTriangle; private float[] mProjMatrix = new float[16]; private float[] mVMatrix = new float[16]; private float[] mMVPMatrix = new float[16]; public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) { GLES20.glClearColor(0.5f, 0.77f, .25f, 1.0f); mTriangle = new Triangle(); } public void onDrawFrame(GL10 unused) { Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 1.0f); Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); mTriangle.draw(mMVPMatrix); } public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); } public static int loadShader(int type, String shaderCode) { // Create a vertex shader type or fragment shader type int shader = GLES20.glCreateShader(type); // add source code to shader and compile! GLES20.glShaderSource(shader, shaderCode); GLES20.glCompileShader(shader); return shader; } } class Triangle { private int mProgram; private int mPositionHandle = 0; private int mMVPMatrixHandle = 0; private int mColorHandle = 0; private final String vertexShaderCode = "attribute vec4 vPosition;" + "void main() {" + " gl_Position = vPosition;" + "}"; private final String fragmentShaderCode = "precision mediump float;" + "uniform vec4 vColor;" + "void main() {" + " gl_FragColor = vColor;" + "}"; private Random r = new Random(); private FloatBuffer vertexBuffer; static final int COORDS_PER_VERTEX = 3; static float triangleCoords[] = { 0.0f, 0.622008f, 0.0f, // top -0.5f, -0.311004f, 0.0f, // bottom left 0.5f, -0.311004f, 0.0f // bottom right }; private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX; private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex float color[] = {r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0f}; public Triangle() { // Create byte buffer with 4 bytes of space per float ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4); // Use the device hardware's native byte ordering bb.order(ByteOrder.nativeOrder()); // Create a floating point buffer from the byte buffer vertexBuffer = bb.asFloatBuffer(); // add the coordinates to the floatbuffer vertexBuffer.put(triangleCoords); // set the buffer to read the first coordinate vertexBuffer.position(0); int vertexShader = MyRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = MyRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES20.glCreateProgram(); GLES20.glAttachShader(mProgram, vertexShader); GLES20.glAttachShader(mProgram, fragmentShader); GLES20.glLinkProgram(mProgram); } public void draw(float[] mvpMatrix) { // Add program to OpenGLES environment GLES20.glUseProgram(mProgram); // get handle to vertex shader's vPosition member mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // enable a handle to the triangle vertices GLES20.glEnableVertexAttribArray(mPositionHandle); // get handle to shape's transformation matrix mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // prepare triangle coordinate data GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // get handle to fragment shader's vColor member mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // set color for drawing the triangle GLES20.glUniform4fv(mColorHandle, 1, color, 0); // Apply the projection and view transformation GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle); } } class Square { private FloatBuffer vertexBuffer; private ShortBuffer drawListBuffer; // number of coordinates per vertex in this array static final int COORDS_PER_VERTEX = 3; // Given counterclockwise, provide x, y, and z coordinates static float squareCoords[] = { -0.5f, 0.5f, 0.0f, // top left -0.5f, -0.5f, 0.0f, // bottom left 0.5f, -0.5f, 0.0f, // bottom right 0.5f, 0.5f, 0.0f}; // top right private short drawOrder[] = {0, 1, 2, 0, 2, 3}; // order to draw vertices public Square() { // initialize vertex byte buffer for shape coordinates // number of coordinate values * 4 bytes per float ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4); bb.order(ByteOrder.nativeOrder()); vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(squareCoords); vertexBuffer.position(0); // initialize byte buffer for the draw list // number of coordinate values * 2 bytes per short ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2); dlb.order(ByteOrder.nativeOrder()); drawListBuffer = dlb.asShortBuffer(); drawListBuffer.put(drawOrder); drawListBuffer.position(0); } }