Back to project page planets.
The source code is released under:
GNU General Public License
If you think the Android project planets 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 se.liu.lysator.dahlberg.planets; /*www.j a va 2 s . co m*/ import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLES20; import android.opengl.Matrix; import android.opengl.GLSurfaceView.Renderer; public abstract class GLRenderer implements Renderer { private boolean firstDraw; private boolean surfaceCreated; private int width; private int height; private int fps; /** * Store the model matrix. This matrix is used to move models from object * space (where each model can be thought of being located at the center of * the universe) to world space. */ private float[] modelMatrix = new float[16]; /** * Store the view matrix. This can be thought of as our camera. This matrix * transforms world space to eye space; it positions things relative to our * eye. */ private float[] viewMatrix = new float[16]; /** * Store the projection matrix. This is used to project the scene onto a 2D * viewport. */ private float[] projectionMatrix = new float[16]; public GLRenderer() { firstDraw = true; surfaceCreated = false; width = -1; height = -1; fps = 0; } @Override public void onSurfaceCreated(GL10 notUsed, EGLConfig config) { surfaceCreated = true; width = -1; height = -1; // Set the background clear color to gray. GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Position the eye behind the origin. final float eyeX = 0.0f; final float eyeY = 0.0f; final float eyeZ = 1.5f; // We are looking toward the distance final float lookX = 0.0f; final float lookY = 0.0f; final float lookZ = -5.0f; // Set our up vector. This is where our head would be pointing were we // holding the camera. final float upX = 0.0f; final float upY = 1.0f; final float upZ = 0.0f; // Set the view matrix. This matrix can be said to represent the camera // position. // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination // of a model and // view matrix. In OpenGL 2, we can keep track of these matrices // separately if we choose. Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); final String vertexShader = "uniform mat4 u_MVPMatrix; \n" // A constant // representing // the // combined // model/view/projection // matrix. + "attribute vec4 a_Position; \n" // Per-vertex position // information we will pass // in. + "attribute vec4 a_Color; \n" // Per-vertex color information // we will pass in. + "varying vec4 v_Color; \n" // This will be passed into the // fragment shader. + "void main() \n" // The entry point for our vertex shader. + "{ \n" + " v_Color = a_Color; \n" // Pass the color through to // the fragment shader. // It will be interpolated across the triangle. + " gl_Position = u_MVPMatrix \n" // gl_Position is a special // variable used to store // the final position. + " * a_Position; \n" // Multiply the vertex by the matrix to // get the final point in + "} \n"; // normalized screen coordinates. final String fragmentShader = "precision mediump float; \n" // Set the // default // precision // to // medium. // We don't // need as // high of a // precision in the fragment shader. + "varying vec4 v_Color; \n" // This is the color from the // vertex shader interpolated // across the // triangle per fragment. + "void main() \n" // The entry point for our fragment shader. + "{ \n" + " gl_FragColor = v_Color; \n" // Pass the color // directly through // the pipeline. + "} \n"; // Load in the vertex shader. int vertexShaderHandle = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER); if (vertexShaderHandle != 0) { // Pass in the shader source. GLES20.glShaderSource(vertexShaderHandle, vertexShader); // Compile the shader. GLES20.glCompileShader(vertexShaderHandle); // Get the compilation status. final int[] compileStatus = new int[1]; GLES20.glGetShaderiv(vertexShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.glDeleteShader(vertexShaderHandle); vertexShaderHandle = 0; } } if (vertexShaderHandle == 0) { throw new RuntimeException("Error creating vertex shader."); } // Load in the fragment shader shader. int fragmentShaderHandle = GLES20 .glCreateShader(GLES20.GL_FRAGMENT_SHADER); if (fragmentShaderHandle != 0) { // Pass in the shader source. GLES20.glShaderSource(fragmentShaderHandle, fragmentShader); // Compile the shader. GLES20.glCompileShader(fragmentShaderHandle); // Get the compilation status. final int[] compileStatus = new int[1]; GLES20.glGetShaderiv(fragmentShaderHandle, GLES20.GL_COMPILE_STATUS, compileStatus, 0); // If the compilation failed, delete the shader. if (compileStatus[0] == 0) { GLES20.glDeleteShader(fragmentShaderHandle); fragmentShaderHandle = 0; } } if (fragmentShaderHandle == 0) { throw new RuntimeException("Error creating fragment shader."); } // Create a program object and store the handle to it. int programHandle = GLES20.glCreateProgram(); if (programHandle != 0) { // Bind the vertex shader to the program. GLES20.glAttachShader(programHandle, vertexShaderHandle); // Bind the fragment shader to the program. GLES20.glAttachShader(programHandle, fragmentShaderHandle); // Bind attributes GLES20.glBindAttribLocation(programHandle, 0, "a_Position"); GLES20.glBindAttribLocation(programHandle, 1, "a_Color"); // Link the two shaders together into a program. GLES20.glLinkProgram(programHandle); // Get the link status. final int[] linkStatus = new int[1]; GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0); // If the link failed, delete the program. if (linkStatus[0] == 0) { GLES20.glDeleteProgram(programHandle); programHandle = 0; } } if (programHandle == 0) { throw new RuntimeException("Error creating program."); } onSurfaceCreated(programHandle); } @Override public void onSurfaceChanged(GL10 notUsed, int width, int height) { if (!surfaceCreated && width == this.width && height == this.height) { return; } this.width = width; this.height = height; onCreate(width, height, surfaceCreated); surfaceCreated = false; } @Override public void onDrawFrame(GL10 notUsed) { onDrawFrame(firstDraw); if (firstDraw) { firstDraw = false; } } public int getFPS() { return fps; } public abstract void onCreate(int width, int height, boolean contextLost); public abstract void onDrawFrame(boolean firstDraw); public abstract void onSurfaceCreated(int programHandle); public float[] getProjectionMatrix() { return projectionMatrix; } public float[] getModelMatrix() { return modelMatrix; } public float[] getViewMatrix() { return viewMatrix; } }