Advertisement
Guest User

Cube class with invalid vertex shader.

a guest
Apr 1st, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.85 KB | None | 0 0
  1. package dd.ww;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.nio.ByteBuffer;
  6. import java.nio.ByteOrder;
  7. import java.nio.FloatBuffer;
  8. import java.nio.ShortBuffer;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import android.content.Context;
  12. import android.content.res.AssetManager;
  13. import android.opengl.GLES20;
  14. import android.opengl.Matrix;
  15. import android.util.Log;
  16.  
  17. public class Cube {
  18.     private Context context;
  19.     private FloatBuffer vertexBuffer;
  20.     private FloatBuffer normalBuffer;
  21.     private FloatBuffer colorBuffer;
  22.     private ShortBuffer vertexIndexBuffer;
  23.     private ShortBuffer normalIndexBuffer;
  24.    
  25.     private int shaderProgram;
  26.     private int lightProgram;
  27.    
  28.     //Go to Google Code, find OpenGL ES 2.0 Programming Guide source code, Android,
  29.     //check in the ESShapes.java, and study the FloatBuffers...
  30.    
  31.     public Cube(Context c) {
  32.         context = c;
  33.         loadCube("cube/cube.obj");
  34.     }
  35.    
  36.     private void loadCube(String filename) {
  37.        
  38.         ArrayList<Float> tempVertices = new ArrayList<Float>();
  39.         ArrayList<Float> tempNormals = new ArrayList<Float>();
  40.         ArrayList<Short> tempVertexIndices = new ArrayList<Short>();
  41.         ArrayList<Short> tempNormalIndices = new ArrayList<Short>();
  42.        
  43.         try {
  44.             AssetManager manager = context.getAssets();
  45.             BufferedReader reader = new BufferedReader(new InputStreamReader(manager.open(filename)));
  46.             String line;
  47.             while ((line = reader.readLine()) != null) {
  48.                 if (line.startsWith("v")) {
  49.                     tempVertices.add(Float.valueOf(line.split(" ")[1])); //vx
  50.                     tempVertices.add(Float.valueOf(line.split(" ")[2])); //vy
  51.                     tempVertices.add(Float.valueOf(line.split(" ")[3])); //vz
  52.                 }
  53.                 else if (line.startsWith("vn")) {
  54.                     tempNormals.add(Float.valueOf(line.split(" ")[1]));
  55.                     tempNormals.add(Float.valueOf(line.split(" ")[2]));
  56.                     tempNormals.add(Float.valueOf(line.split(" ")[3]));
  57.                 }
  58.                 else if (line.startsWith("f")) {
  59.                     String[] tokens = line.split(" ");
  60.                     for (int i= 1;i<tokens.length; i++){
  61.                         tempVertexIndices.add(Short.valueOf(tokens[i].split("/")[0]));
  62.                         tempNormalIndices.add(Short.valueOf(tokens[i].split("/")[2]));
  63.                     }
  64.                 }
  65.             }
  66.            
  67.             float[] vertices = new float[tempVertices.size()];
  68.             for (int i = 0; i < tempVertices.size(); i++) {
  69.                 Float f = tempVertices.get(i);
  70.                 vertices[i] = (f != null ? f : Float.NaN);
  71.             }
  72.            
  73.             float[] normals = new float[tempNormals.size()];
  74.             for (int i = 0; i < tempNormals.size(); i++) {
  75.                 Float f = tempNormals.get(i);
  76.                 normals[i] = (f != null ? f : Float.NaN);
  77.             }
  78.            
  79.             short[] vertexIndices = new short[tempVertexIndices.size()];
  80.             for (int i = 0; i < tempVertexIndices.size(); i++) {
  81.                 Short s = tempVertexIndices.get(i);
  82.                 s--;
  83.                 vertexIndices[i] = (s != null ? s : 0);
  84.             }
  85.            
  86.             short[] normalIndices = new short[tempNormalIndices.size()];
  87.             for (int i = 0; i < tempNormalIndices.size(); i++) {
  88.                 Short s = tempNormalIndices.get(i);
  89.                 s--;
  90.                 normalIndices[i] = (s != null ? s : 0);
  91.             }
  92.            
  93.             float[] colors = {0.2f, 0.2f, 0.8f};
  94.            
  95.             vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  96.             vertexBuffer.put(vertices).position(0);
  97.             normalBuffer = ByteBuffer.allocateDirect(normals.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  98.             normalBuffer.put(normals).position(0);
  99.             colorBuffer = ByteBuffer.allocateDirect(colors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  100.             colorBuffer.put(colors).position(0);
  101.             vertexIndexBuffer = ByteBuffer.allocateDirect(vertexIndices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
  102.             vertexIndexBuffer.put(vertexIndices).position(0);
  103.             normalIndexBuffer = ByteBuffer.allocateDirect(normalIndices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
  104.             normalIndexBuffer.put(normalIndices).position(0);
  105.            
  106.             int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
  107.             GLES20.glShaderSource(vertexShader, vertexCode);
  108.             GLES20.glCompileShader(vertexShader);
  109.            
  110.             int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
  111.             GLES20.glShaderSource(fragmentShader, fragmentCode);
  112.             GLES20.glCompileShader(fragmentShader);
  113.            
  114.             shaderProgram = GLES20.glCreateProgram();
  115.             GLES20.glAttachShader(shaderProgram, vertexShader);
  116.             GLES20.glAttachShader(shaderProgram, fragmentShader);
  117.             GLES20.glBindAttribLocation(shaderProgram, 0, "a_position");
  118.             GLES20.glBindAttribLocation(shaderProgram, 1, "a_color");
  119.             GLES20.glBindAttribLocation(shaderProgram, 2, "a_normal");
  120.             GLES20.glLinkProgram(shaderProgram);
  121.            
  122.             int[] linked = new int[1];
  123.             GLES20.glGetProgramiv(shaderProgram, GLES20.GL_LINK_STATUS, linked, 0);
  124.             if (linked[0] == 0) {
  125.                 Log.d("DEBUG", "Model: Shader code error.");
  126.                 Log.d("DEBUG", GLES20.glGetProgramInfoLog(shaderProgram));
  127.                 int[] params = new int[1];
  128.                 GLES20.glGetShaderiv(vertexShader, GLES20.GL_INFO_LOG_LENGTH, params, 0);
  129.                 Log.d("DEBUG", Integer.toString(params[0]));
  130.                 GLES20.glGetShaderiv(vertexShader, GLES20.GL_COMPILE_STATUS, params, 0);
  131.                 Log.d("DEBUG", Boolean.toString(params[0] == GLES20.GL_TRUE));
  132.                 String str = GLES20.glGetShaderInfoLog(vertexShader);
  133.                 Log.d("DEBUG", GLES20.glGetShaderInfoLog(vertexShader));
  134.                 Log.d("DEBUG", GLES20.glGetShaderInfoLog(fragmentShader));
  135.                 GLES20.glDeleteProgram(shaderProgram);
  136.                 return;
  137.             }
  138.            
  139.             int lightVertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
  140.             GLES20.glShaderSource(lightVertexShader, lightPointVertexCode);
  141.             GLES20.glCompileShader(lightVertexShader);
  142.            
  143.             int lightFragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
  144.             GLES20.glShaderSource(lightFragmentShader, lightPointFragmentCode);
  145.             GLES20.glCompileShader(lightFragmentShader);
  146.            
  147.             lightProgram = GLES20.glCreateProgram();
  148.             GLES20.glAttachShader(lightProgram, lightVertexShader);
  149.             GLES20.glAttachShader(lightProgram, lightFragmentShader);
  150.             GLES20.glBindAttribLocation(lightProgram, 3, "a_position");
  151.             GLES20.glLinkProgram(lightProgram);
  152.            
  153.             GLES20.glGetProgramiv(lightProgram, GLES20.GL_LINK_STATUS, linked, 0);
  154.             if (linked[0] == 0) {
  155.                 Log.d("DEBUG", "Lighting: Shader code error.");
  156.                 Log.d("DEBUG", GLES20.glGetProgramInfoLog(lightProgram));
  157.                 Log.d("DEBUG", GLES20.glGetShaderInfoLog(lightVertexShader));
  158.                 Log.d("DEBUG", GLES20.glGetShaderInfoLog(lightFragmentShader));
  159.                 GLES20.glDeleteProgram(lightProgram);
  160.                 return;
  161.             }
  162.            
  163.             GLES20.glDeleteShader(vertexShader);
  164.             GLES20.glDeleteShader(fragmentShader);
  165.             GLES20.glDeleteShader(lightVertexShader);
  166.             GLES20.glDeleteShader(lightFragmentShader);
  167.         }
  168.         catch (Exception e) {
  169.             Log.e("DEBUG", "Error.", e);
  170.         }
  171.     }
  172.    
  173.     private String vertexCode = "" +
  174.             "uniform mat4 u_mvpMatrix;                                              \n" +
  175.             "uniform mat4 u_mvMatrix;                                               \n" +
  176.             "uniform vec3 u_lightPosition;                                          \n" +
  177.             "attribute vec4 a_position;                                             \n" +
  178.             "attribute vec4 a_color;                                                \n" +
  179.             "attribute vec3 a_normal;                                               \n" +
  180.             "varying vec4 v_color;                                                  \n" +
  181.             "void main() {                                                          \n" +
  182.             "   vec3 modelViewVertex = vec3(u_mvMatrix * a_position);               \n" +
  183.             "   vec3 modelViewNormal = vec3(u_mvMatrix * vec4(a_normal, 0.0));      \n" +
  184.             "   float distance = length(u_lightPosition - modelViewVertex);         \n" +
  185.             "   vec3 lightVector = normalize(u_lightPosition - modelViewVertex);    \n" +
  186.             "   float diffuse = max(dot(modelViewNormal, lightVector), 0.1);        \n" +
  187.             "   diffuse = diffuse * (1.0 / (0.25 * distance * distance));           \n" +
  188.             "   v_color = a_color * diffuse;                                        \n" +
  189.             "   gl_Position = mvpMatrix * a_position;                               \n" +
  190.             "}                                                                      \n";
  191.    
  192.     private String fragmentCode = "" +
  193.             "precision mediump float;                   \n" +
  194.             "varying vec4 v_color;                      \n" +
  195.             "void main() {                              \n" +
  196.             "   gl_FragColor = v_color;                 \n" +
  197.             "}                                          \n";
  198.    
  199.     private int attribute_Position;
  200.     private int uniform_mvpMatrix;
  201.    
  202.     //public void draw(final float[] mMatrix, final float[] vMatrix, final float[] pMatrix, float[] mvpMatrix) {
  203.     public void draw(float[] modelViewProjectionMatrix) {
  204.         GLES20.glUseProgram(shaderProgram);
  205.         attribute_Position = GLES20.glGetAttribLocation(shaderProgram, "a_position");
  206.         uniform_mvpMatrix = GLES20.glGetUniformLocation(shaderProgram, "mvpMatrix");
  207.         GLES20.glVertexAttribPointer(attribute_Position, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
  208.         GLES20.glEnableVertexAttribArray(attribute_Position);
  209.        
  210.         GLES20.glUniformMatrix4fv(uniform_mvpMatrix, 1, false, modelViewProjectionMatrix, 0);
  211.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, vertexIndexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, vertexIndexBuffer);
  212.         //GLES20.glDisableVertexAttribArray(attribute_Position);
  213.     }
  214.    
  215.     private int attribute_Color;
  216.     private int attribute_Normal;
  217.     private int uniform_mvMatrix;
  218.     private int uniform_lightPosition;
  219.     private float lightingAngle;
  220.     private float[] lightVector = {0f, 0f, 0f, 1f};
  221.     private float[] lightWorldSpace;
  222.     private float[] lightEyeSpace;
  223.    
  224.    
  225.     public void draw(float[] mMatrix, float[] vMatrix, float[] pMatrix) {
  226.         float[] mvpMatrix = new float[16];
  227.         float[] mvMatrix = new float[16];
  228.        
  229.         attribute_Position = GLES20.glGetAttribLocation(shaderProgram, "a_position");
  230.         attribute_Color = GLES20.glGetAttribLocation(shaderProgram, "a_color");
  231.         attribute_Normal = GLES20.glGetAttribLocation(shaderProgram, "a_normal");
  232.        
  233.         uniform_mvpMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_mvpMatrix");
  234.         uniform_mvMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_mvMatrix");
  235.         uniform_lightPosition = GLES20.glGetUniformLocation(shaderProgram, "u_lightPosition");
  236.        
  237.         GLES20.glUseProgram(shaderProgram);
  238.         GLES20.glVertexAttribPointer(attribute_Position, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
  239.         GLES20.glEnableVertexAttribArray(attribute_Position);
  240.         GLES20.glVertexAttribPointer(attribute_Normal, 3, GLES20.GL_FLOAT, false, 3 * 4, normalBuffer);
  241.         GLES20.glEnableVertexAttribArray(attribute_Normal);
  242.         GLES20.glVertexAttribPointer(attribute_Color, 3, GLES20.GL_FLOAT, false, 3 * 4, colorBuffer);
  243.         GLES20.glEnableVertexAttribArray(attribute_Color);
  244.         //if (vertexAttribEnabled != true) {
  245.         //  GLES20.glEnableVertexAttribArray(attribute_Position);
  246.         //  vertexAttribEnabled = true;
  247.         //}
  248.        
  249.         Matrix.multiplyMM(mvMatrix, 0, vMatrix, 0, mMatrix, 0);
  250.         GLES20.glUniformMatrix4fv(uniform_mvMatrix, 1, false, mvMatrix, 0);
  251.         Matrix.multiplyMM(mvpMatrix, 0, pMatrix, 0, mvMatrix, 0);
  252.         GLES20.glUniformMatrix4fv(uniform_mvpMatrix, 1, false, mvpMatrix, 0);
  253.        
  254.         //--------------  LIGHT MODEL MATRIX --------------------
  255.         float[] lightModelMatrix = new float[16];
  256.         Matrix.setIdentityM(lightModelMatrix, 0);
  257.         Matrix.translateM(lightModelMatrix, 0, 0f, 0f, -5f);
  258.         Matrix.rotateM(lightModelMatrix, 0, lightingAngle, 1f, 0f, 0f);
  259.         Matrix.translateM(lightModelMatrix, 0, 0f, 0f, -5f);
  260.        
  261.         Matrix.multiplyMV(lightWorldSpace, 0, lightModelMatrix, 0, lightVector, 0);
  262.         Matrix.multiplyMV(lightEyeSpace, 0, vMatrix, 0, lightWorldSpace, 0);
  263.        
  264.         drawLight(lightModelMatrix, mvMatrix, mvpMatrix);
  265.        
  266.         lightingAngle += 0.3f;
  267.         if (lightingAngle > 360f)
  268.             lightingAngle = 0f;
  269.        
  270.         //GLES20.glUniform3f(uniform_lightPosition, )
  271.         //------------  END LIGHT MODEL MATRIX ------------------
  272.        
  273.         GLES20.glUniform3f(uniform_lightPosition, lightEyeSpace[0], lightEyeSpace[1], lightEyeSpace[2]);
  274.        
  275.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, vertexIndexBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, vertexIndexBuffer);
  276.        
  277.         //GLES20.glDisableVertexAttribArray(attribute_Position);
  278.     }
  279.    
  280.     private String lightPointVertexCode = "" +
  281.             "uniform mat4 u_mvpMatrix;                  \n" +
  282.             "attribute vec4 a_position;                 \n" +
  283.             "void main(){                               \n" +
  284.             "   gl_Position = u_mvpMatrix * a_position; \n" +
  285.             "   gl_PointSize = 5.0;                     \n" +
  286.             "}                                          \n";
  287.    
  288.     private String lightPointFragmentCode = "" +
  289.             "precision mediump float;                   \n" +
  290.             "void main(){                               \n" +
  291.             "   gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n" +
  292.             "}                                          \n";
  293.    
  294.    
  295.     public void drawLight(float[] mMatrix, float[] vMatrix, float[] pMatrix) {
  296.         GLES20.glUseProgram(lightProgram);
  297.         int uniform_pointMVPMatrix = GLES20.glGetUniformLocation(lightProgram, "u_mvpMatrix");
  298.         int attribute_lightPosition = GLES20.glGetAttribLocation(lightProgram, "a_position");
  299.         GLES20.glVertexAttrib3f(attribute_lightPosition, lightVector[0], lightVector[1], lightVector[2]);
  300.         GLES20.glDisableVertexAttribArray(attribute_lightPosition);
  301.         float[] mvpMatrix = new float[16];
  302.         Matrix.multiplyMM(mvpMatrix, 0, vMatrix, 0, mMatrix, 0);
  303.         Matrix.multiplyMM(mvpMatrix, 0, pMatrix, 0, Arrays.copyOf(mvpMatrix, 16), 0);
  304.         GLES20.glUniformMatrix4fv(uniform_pointMVPMatrix, 1, false, mvpMatrix, 0);
  305.         GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement