package dd.ww; import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer; import java.util.ArrayList; import java.util.Arrays; import android.content.Context; import android.content.res.AssetManager; import android.opengl.GLES20; import android.opengl.Matrix; import android.util.Log; public class Cube { private Context context; private FloatBuffer vertexBuffer; private FloatBuffer normalBuffer; private FloatBuffer colorBuffer; private ShortBuffer vertexIndexBuffer; private ShortBuffer normalIndexBuffer; private int shaderProgram; private int lightProgram; //Go to Google Code, find OpenGL ES 2.0 Programming Guide source code, Android, //check in the ESShapes.java, and study the FloatBuffers... public Cube(Context c) { context = c; loadCube("cube/cube.obj"); } private void loadCube(String filename) { ArrayList tempVertices = new ArrayList(); ArrayList tempNormals = new ArrayList(); ArrayList tempVertexIndices = new ArrayList(); ArrayList tempNormalIndices = new ArrayList(); try { AssetManager manager = context.getAssets(); BufferedReader reader = new BufferedReader(new InputStreamReader(manager.open(filename))); String line; while ((line = reader.readLine()) != null) { if (line.startsWith("v")) { tempVertices.add(Float.valueOf(line.split(" ")[1])); //vx tempVertices.add(Float.valueOf(line.split(" ")[2])); //vy tempVertices.add(Float.valueOf(line.split(" ")[3])); //vz } else if (line.startsWith("vn")) { tempNormals.add(Float.valueOf(line.split(" ")[1])); tempNormals.add(Float.valueOf(line.split(" ")[2])); tempNormals.add(Float.valueOf(line.split(" ")[3])); } else if (line.startsWith("f")) { String[] tokens = line.split(" "); for (int i= 1;i 360f) lightingAngle = 0f; //GLES20.glUniform3f(uniform_lightPosition, ) //------------ END LIGHT MODEL MATRIX ------------------ GLES20.glUniform3f(uniform_lightPosition, lightEyeSpace[0], lightEyeSpace[1], lightEyeSpace[2]); GLES20.glDrawElements(GLES20.GL_TRIANGLES, vertexIndexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, vertexIndexBuffer); //GLES20.glDisableVertexAttribArray(attribute_Position); } private String lightPointVertexCode = "" + "uniform mat4 u_mvpMatrix; \n" + "attribute vec4 a_position; \n" + "void main(){ \n" + " gl_Position = u_mvpMatrix * a_position; \n" + " gl_PointSize = 5.0; \n" + "} \n"; private String lightPointFragmentCode = "" + "precision mediump float; \n" + "void main(){ \n" + " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n" + "} \n"; public void drawLight(float[] mMatrix, float[] vMatrix, float[] pMatrix) { GLES20.glUseProgram(lightProgram); int uniform_pointMVPMatrix = GLES20.glGetUniformLocation(lightProgram, "u_mvpMatrix"); int attribute_lightPosition = GLES20.glGetAttribLocation(lightProgram, "a_position"); GLES20.glVertexAttrib3f(attribute_lightPosition, lightVector[0], lightVector[1], lightVector[2]); GLES20.glDisableVertexAttribArray(attribute_lightPosition); float[] mvpMatrix = new float[16]; Matrix.multiplyMM(mvpMatrix, 0, vMatrix, 0, mMatrix, 0); Matrix.multiplyMM(mvpMatrix, 0, pMatrix, 0, Arrays.copyOf(mvpMatrix, 16), 0); GLES20.glUniformMatrix4fv(uniform_pointMVPMatrix, 1, false, mvpMatrix, 0); GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1); } }