alegen

Untitled

Jun 29th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1.     // My GLSurfaceView.Renderer class
  2.  
  3.     @Override
  4.     public void onDrawFrame(GL10 unused) {
  5.  
  6.         // draw background color
  7.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  8.  
  9.         Matrix.setLookAtM(this.modelViewMatrix, 0, 0, 0, this.eyeZ, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
  10.         Matrix.multiplyMM(this.mvpMatrix, 0, this.projectionMatrix, 0, this.modelViewMatrix, 0);
  11.  
  12.         Square s = new Square();
  13.         s.draw(this.mvpMatrix);
  14.     }
  15.  
  16.     @Override
  17.     public void onSurfaceChanged(GL10 unused, int width, int height) {
  18.         GLES20.glViewport(0, 0, width, height);
  19.         ratio = (float) width / height;
  20.         Matrix.frustumM(this.projectionMatrix, 0, -ratio, ratio, -1, 1, 50, 70);
  21.     }
  22.  
  23.  
  24.     // My shaders
  25.  
  26.     public static final String vertexShaderCode =
  27.                     "uniform mat4 mvpMatrix;" +
  28.                     "attribute vec4 positionVector;" +
  29.                     "void main() {" +
  30.                     "   gl_Position = positionVector * mvpMatrix;" +
  31.                     "}";
  32.  
  33.     public static final String fragmentShaderCode =
  34.             "precision mediump float;" +
  35.                     "uniform vec4 colorVector;" +
  36.                     "void main() {" +
  37.                     "   gl_FragColor = colorVector;" +
  38.                     "}";
  39.  
  40.  
  41.     //  Draw method for the square that I see
  42.  
  43.     public void draw(float[] mvpMatrix) {
  44.         FloatBuffer vertexBuffer = this.calculateVertexBuffer();
  45.  
  46.         GLES20.glUseProgram(program);
  47.         this.positionHandle = GLES20.glGetAttribLocation(program, "positionVector");
  48.         GLES20.glEnableVertexAttribArray(this.positionHandle);
  49.  
  50.         GLES20.glVertexAttribPointer(
  51.                 this.positionHandle,
  52.                 this.coordsPerVertex,
  53.                 GLES20.GL_FLOAT,
  54.                 false,
  55.                 this.vertexStride,
  56.                 vertexBuffer
  57.         );
  58.  
  59.         this.coloHandle = GLES20.glGetUniformLocation(this.program, "colorVector");
  60.         GLES20.glUniform4fv(this.coloHandle, 1, this.color, 0);
  61.  
  62.         this.mvpMatrixHandle = GLES20.glGetUniformLocation(this.program, "mvpMatrix");
  63.         UtilOpenGL.checkGlError("glGetUniformLocation");
  64.  
  65.         GLES20.glUniformMatrix4fv(this.mvpMatrixHandle, 1, false, mvpMatrix, 0);
  66.         UtilOpenGL.checkGlError("glUniformMatrix4fv");
  67.  
  68.         GLES20.glDrawElements(
  69.                 GLES20.GL_TRIANGLES,
  70.                 drawOrder.length,
  71.                 GLES20.GL_UNSIGNED_SHORT,
  72.                 drawListBuffer
  73.         );
  74.  
  75.         GLES20.glDisableVertexAttribArray(this.positionHandle);
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment