private final String vertexShaderCode = "uniform mat4 uMVPMatrix;" + "uniform mat4 uMVMatrix;" + "uniform vec3 vLightPos;" + "attribute vec4 vPosition;" + "attribute vec4 vColor;" + "attribute vec3 vNormal;" + "varying vec4 finalColor;" + "void main() \n" // The entry point for our vertex shader. + "{ \n" // Transform the vertex into eye space. + " vec3 modelViewVertex = vec3(uMVPMatrix * vPosition); \n" // Transform the normal's orientation into eye space. + " vec3 modelViewNormal = vec3(uMVMatrix * vec4(vNormal, 0.0)); \n" // Will be used for attenuation. + " float distance = length(vLightPos - modelViewVertex); \n" // Get a lighting direction vector from the light to the vertex. + " vec3 lightVector = normalize(vLightPos - modelViewVertex); \n" // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are // pointing in the same direction then it will get max illumination. + " float diffuse = max(dot(modelViewNormal, lightVector), 0.1); \n" // Attenuate the light based on distance. + " diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance))); \n" // Multiply the color by the illumination level. It will be interpolated across the triangle. + " finalColor = vColor * diffuse; \n" // gl_Position is a special variable used to store the final position. // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. + " gl_Position = uMVPMatrix * vPosition; \n" + "}"; private final String fragmentShaderCode = "precision mediump float;" + "varying vec4 finalColor;" + "void main() {" + " gl_FragColor = finalColor;" + "}"; // Define a simple shader program for our point. final String pointVertexShaderCode = "uniform mat4 vMVPMatrix; \n" + "attribute vec4 vPosition; \n" + "void main() \n" + "{ \n" + " gl_Position = vMVPMatrix \n" + " * vPosition; \n" + " gl_PointSize = 5.0; \n" + "} \n"; final String pointFragmentShaderCode = "precision mediump float; \n" + "void main() \n" + "{ \n" + " gl_FragColor = vec4(1.0, \n" + " 1.0, 1.0, 1.0); \n" + "} \n"; private final int mProgram; private int mPositionHandle; private int mColorHandle; // Use to access and set the view transformation private int mMVPMatrixHandle; private final int vertexCount = drawOrder.length; private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex public Cube() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (# of coordinate values * 4 bytes per float) squareCoords.length * 4); bb.order(ByteOrder.nativeOrder()); vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(squareCoords); vertexBuffer.position(0); // initialize byte buffer for the draw list ByteBuffer dlb = ByteBuffer.allocateDirect( // (# of coordinate values * 2 bytes per short) drawOrder.length * 2); dlb.order(ByteOrder.nativeOrder()); drawListBuffer = dlb.asShortBuffer(); drawListBuffer.put(drawOrder); drawListBuffer.position(0); int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); int pointVertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, pointVertexShaderCode); int pointFragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, pointFragmentShaderCode); // create empty OpenGL ES Program mProgram = GLES20.glCreateProgram(); // add the vertex shader to program GLES20.glAttachShader(mProgram, vertexShader); // add the fragment shader to program GLES20.glAttachShader(mProgram, fragmentShader); GLES20.glAttachShader(mProgram, pointVertexShader); GLES20.glAttachShader(mProgram, pointFragmentShader); GLES20.glBindAttribLocation(mProgram, 0, "vPosition"); GLES20.glBindAttribLocation(mProgram, 0, "finalColor"); // creates OpenGL ES program executables GLES20.glLinkProgram(mProgram); }