Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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<Float> tempVertices = new ArrayList<Float>();
- ArrayList<Float> tempNormals = new ArrayList<Float>();
- ArrayList<Short> tempVertexIndices = new ArrayList<Short>();
- ArrayList<Short> tempNormalIndices = new ArrayList<Short>();
- 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<tokens.length; i++){
- tempVertexIndices.add(Short.valueOf(tokens[i].split("/")[0]));
- tempNormalIndices.add(Short.valueOf(tokens[i].split("/")[2]));
- }
- }
- }
- float[] vertices = new float[tempVertices.size()];
- for (int i = 0; i < tempVertices.size(); i++) {
- Float f = tempVertices.get(i);
- vertices[i] = (f != null ? f : Float.NaN);
- }
- float[] normals = new float[tempNormals.size()];
- for (int i = 0; i < tempNormals.size(); i++) {
- Float f = tempNormals.get(i);
- normals[i] = (f != null ? f : Float.NaN);
- }
- short[] vertexIndices = new short[tempVertexIndices.size()];
- for (int i = 0; i < tempVertexIndices.size(); i++) {
- Short s = tempVertexIndices.get(i);
- s--;
- vertexIndices[i] = (s != null ? s : 0);
- }
- short[] normalIndices = new short[tempNormalIndices.size()];
- for (int i = 0; i < tempNormalIndices.size(); i++) {
- Short s = tempNormalIndices.get(i);
- s--;
- normalIndices[i] = (s != null ? s : 0);
- }
- float[] colors = {0.2f, 0.2f, 0.8f};
- vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- vertexBuffer.put(vertices).position(0);
- normalBuffer = ByteBuffer.allocateDirect(normals.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- normalBuffer.put(normals).position(0);
- colorBuffer = ByteBuffer.allocateDirect(colors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- colorBuffer.put(colors).position(0);
- vertexIndexBuffer = ByteBuffer.allocateDirect(vertexIndices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
- vertexIndexBuffer.put(vertexIndices).position(0);
- normalIndexBuffer = ByteBuffer.allocateDirect(normalIndices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
- normalIndexBuffer.put(normalIndices).position(0);
- int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
- GLES20.glShaderSource(vertexShader, vertexCode);
- GLES20.glCompileShader(vertexShader);
- int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
- GLES20.glShaderSource(fragmentShader, fragmentCode);
- GLES20.glCompileShader(fragmentShader);
- shaderProgram = GLES20.glCreateProgram();
- GLES20.glAttachShader(shaderProgram, vertexShader);
- GLES20.glAttachShader(shaderProgram, fragmentShader);
- GLES20.glBindAttribLocation(shaderProgram, 0, "a_position");
- GLES20.glBindAttribLocation(shaderProgram, 1, "a_color");
- GLES20.glBindAttribLocation(shaderProgram, 2, "a_normal");
- GLES20.glLinkProgram(shaderProgram);
- int[] linked = new int[1];
- GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, linked, 0);
- if (linked[0] == 0) {
- Log.d("DEBUG", "Model: Shader code error.");
- Log.d("DEBUG", GLES20.glGetProgramInfoLog(shaderProgram));
- int[] params = new int[1];
- GLES20.glGetShaderiv(vertexShader, GLES20.GL_INFO_LOG_LENGTH, params, 0);
- Log.d("DEBUG", Integer.toString(params[0]));
- GLES20.glGetShaderiv(vertexShader, GLES20.GL_COMPILE_STATUS, params, 0);
- Log.d("DEBUG", Boolean.toString(params[0] == GLES20.GL_TRUE));
- String str = GLES20.glGetShaderInfoLog(vertexShader);
- Log.d("DEBUG", GLES20.glGetShaderInfoLog(vertexShader));
- Log.d("DEBUG", GLES20.glGetShaderInfoLog(fragmentShader));
- GLES20.glDeleteProgram(shaderProgram);
- return;
- }
- int lightVertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
- GLES20.glShaderSource(lightVertexShader, lightPointVertexCode);
- GLES20.glCompileShader(lightVertexShader);
- int lightFragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
- GLES20.glShaderSource(lightFragmentShader, lightPointFragmentCode);
- GLES20.glCompileShader(lightFragmentShader);
- lightProgram = GLES20.glCreateProgram();
- GLES20.glAttachShader(lightProgram, lightVertexShader);
- GLES20.glAttachShader(lightProgram, lightFragmentShader);
- GLES20.glBindAttribLocation(lightProgram, 3, "a_position");
- GLES20.glLinkProgram(lightProgram);
- GLES20.glGetProgramiv(lightProgram, GLES20.GL_LINK_STATUS, linked, 0);
- if (linked[0] == 0) {
- Log.d("DEBUG", "Lighting: Shader code error.");
- Log.d("DEBUG", GLES20.glGetProgramInfoLog(lightProgram));
- Log.d("DEBUG", GLES20.glGetShaderInfoLog(lightVertexShader));
- Log.d("DEBUG", GLES20.glGetShaderInfoLog(lightFragmentShader));
- GLES20.glDeleteProgram(lightProgram);
- return;
- }
- GLES20.glDeleteShader(vertexShader);
- GLES20.glDeleteShader(fragmentShader);
- GLES20.glDeleteShader(lightVertexShader);
- GLES20.glDeleteShader(lightFragmentShader);
- }
- catch (Exception e) {
- Log.e("DEBUG", "Error.", e);
- }
- }
- private String vertexCode = "" +
- "uniform mat4 u_mvpMatrix; \n" +
- "uniform mat4 u_mvMatrix; \n" +
- "uniform vec3 u_lightPosition; \n" +
- "attribute vec4 a_position; \n" +
- "attribute vec4 a_color; \n" +
- "attribute vec3 a_normal; \n" +
- "varying vec4 v_color; \n" +
- "void main() { \n" +
- " vec3 modelViewVertex = vec3(u_mvMatrix * a_position); \n" +
- " vec3 modelViewNormal = vec3(u_mvMatrix * vec4(a_normal, 0.0)); \n" +
- " float distance = length(u_lightPosition - modelViewVertex); \n" +
- " vec3 lightVector = normalize(u_lightPosition - modelViewVertex); \n" +
- " float diffuse = max(dot(modelViewNormal, lightVector), 0.1); \n" +
- " diffuse = diffuse * (1.0 / (0.25 * distance * distance)); \n" +
- " v_color = a_color * diffuse; \n" +
- " gl_Position = mvpMatrix * a_position; \n" +
- "} \n";
- private String fragmentCode = "" +
- "precision mediump float; \n" +
- "varying vec4 v_color; \n" +
- "void main() { \n" +
- " gl_FragColor = v_color; \n" +
- "} \n";
- private int attribute_Position;
- private int uniform_mvpMatrix;
- //public void draw(final float[] mMatrix, final float[] vMatrix, final float[] pMatrix, float[] mvpMatrix) {
- public void draw(float[] modelViewProjectionMatrix) {
- GLES20.glUseProgram(shaderProgram);
- attribute_Position = GLES20.glGetAttribLocation(shaderProgram, "a_position");
- uniform_mvpMatrix = GLES20.glGetUniformLocation(shaderProgram, "mvpMatrix");
- GLES20.glVertexAttribPointer(attribute_Position, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
- GLES20.glEnableVertexAttribArray(attribute_Position);
- GLES20.glUniformMatrix4fv(uniform_mvpMatrix, 1, false, modelViewProjectionMatrix, 0);
- GLES20.glDrawElements(GLES20.GL_TRIANGLES, vertexIndexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, vertexIndexBuffer);
- //GLES20.glDisableVertexAttribArray(attribute_Position);
- }
- private int attribute_Color;
- private int attribute_Normal;
- private int uniform_mvMatrix;
- private int uniform_lightPosition;
- private float lightingAngle;
- private float[] lightVector = {0f, 0f, 0f, 1f};
- private float[] lightWorldSpace;
- private float[] lightEyeSpace;
- public void draw(float[] mMatrix, float[] vMatrix, float[] pMatrix) {
- float[] mvpMatrix = new float[16];
- float[] mvMatrix = new float[16];
- attribute_Position = GLES20.glGetAttribLocation(shaderProgram, "a_position");
- attribute_Color = GLES20.glGetAttribLocation(shaderProgram, "a_color");
- attribute_Normal = GLES20.glGetAttribLocation(shaderProgram, "a_normal");
- uniform_mvpMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_mvpMatrix");
- uniform_mvMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_mvMatrix");
- uniform_lightPosition = GLES20.glGetUniformLocation(shaderProgram, "u_lightPosition");
- GLES20.glUseProgram(shaderProgram);
- GLES20.glVertexAttribPointer(attribute_Position, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
- GLES20.glEnableVertexAttribArray(attribute_Position);
- GLES20.glVertexAttribPointer(attribute_Normal, 3, GLES20.GL_FLOAT, false, 3 * 4, normalBuffer);
- GLES20.glEnableVertexAttribArray(attribute_Normal);
- GLES20.glVertexAttribPointer(attribute_Color, 3, GLES20.GL_FLOAT, false, 3 * 4, colorBuffer);
- GLES20.glEnableVertexAttribArray(attribute_Color);
- //if (vertexAttribEnabled != true) {
- // GLES20.glEnableVertexAttribArray(attribute_Position);
- // vertexAttribEnabled = true;
- //}
- Matrix.multiplyMM(mvMatrix, 0, vMatrix, 0, mMatrix, 0);
- GLES20.glUniformMatrix4fv(uniform_mvMatrix, 1, false, mvMatrix, 0);
- Matrix.multiplyMM(mvpMatrix, 0, pMatrix, 0, mvMatrix, 0);
- GLES20.glUniformMatrix4fv(uniform_mvpMatrix, 1, false, mvpMatrix, 0);
- //-------------- LIGHT MODEL MATRIX --------------------
- float[] lightModelMatrix = new float[16];
- Matrix.setIdentityM(lightModelMatrix, 0);
- Matrix.translateM(lightModelMatrix, 0, 0f, 0f, -5f);
- Matrix.rotateM(lightModelMatrix, 0, lightingAngle, 1f, 0f, 0f);
- Matrix.translateM(lightModelMatrix, 0, 0f, 0f, -5f);
- Matrix.multiplyMV(lightWorldSpace, 0, lightModelMatrix, 0, lightVector, 0);
- Matrix.multiplyMV(lightEyeSpace, 0, vMatrix, 0, lightWorldSpace, 0);
- drawLight(lightModelMatrix, mvMatrix, mvpMatrix);
- lightingAngle += 0.3f;
- if (lightingAngle > 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement