Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Introduction To OpenGL in Android Tamillharasan Chandran & Krishnaprasad
Concepts Code!   Basic Shapes Animation
Concepts GPU Cross-Platform
Concepts      OpenGL OpenGL ES OpenGL ES Not just Android!
Concepts Vector Graphics Raster Graphics Lines points, lines, polygons, Surface normals etc .. Grid of pixels
OpenGL Rendering Pipeline Simplified
One more thing! State Machine
Code!
Make sure to add this!
Two Steps GLSurfaceView GLSurfaceView.Renderer
GLSurfaceView <ul><li>What is it? </li></ul><ul><li>Why is it needed? </li></ul>
GLSurfaceView           GLSurfaceView view = new GLSurfaceView(this);      view.setEGLContextClientVersion(2);      view.s...
GLSurfaceView.Renderer <ul><li>What is it? </li></ul><ul><li>Why is it needed? </li></ul><ul><ul><li>Create a Subclass of ...
GLSurfaceView.Renderer public class SquareRenderer implements GLSurfaceView.Renderer {      public void onSurfaceCreated(G...
There you go!! Your First Android OpenGL App
Drawing a Basic Shape
<ul><li>Define a Square </li></ul><ul><ul><li>All the 2D/ 3D objects needs to be defined using Primitives. </li></ul></ul>...
-0.5f, -0.5f, 0.0f,  // Bottom Left 0.5f, -0.5f, 0.0f,  // Bottom Right -0.5f, 0.5f, 0.0f,  // Top Left 0.5f, 0.5f, 0.0f, ...
private void initShapes(){   float squareCoords[] = {     //The coordinates       };    // initialize vertex Buffer for sq...
<ul><li>Draw Methods  </li></ul><ul><ul><ul><li>glDrawArrays() </li></ul></ul></ul><ul><ul><ul><li>glDrawElements()   </li...
GL_POINTS GL_LINES GL_LINE_STRIP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
OpenGL Rendering Pipeline Simplified
<ul><li>Shader Programs </li></ul><ul><ul><li>Vertex Shader </li></ul></ul><ul><ul><li>Fragment Shader </li></ul></ul><ul>...
 
Vertex Shader      Fragment Shader     attribute vec4 vertexPosition; void main(){    gl_Position = vertexPosition; } prec...
Loading the Shader <ul><li>GLES20.glCompileShader(vertexShader); </li></ul><ul><ul><li>GLES20.GL_FRAGMENT_SHADER </li></ul...
Compiling and Linking the Shader program <ul><li>shaderProgram = GLES20.glCreateProgram(); </li></ul>GLES20.glAttachShader...
 
attributePositionHandle =     GLES20. glGetAttribLocation (shaderProgram,&quot;vertexPosition&quot;);
Drawing the square <ul><li>GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |  </li></ul><ul><li>GLES20.GL_DEPTH_BUFFER_BIT); </l...
 
Apply Projection and Camera View <ul><ul><li>Square Coordinate system is a mapped to non-square screen </li></ul></ul><ul>...
Setting up the projection <ul><li>private float[] MVPMatrix = new float[16]; </li></ul><ul><li>private float[] projectionM...
Applying the MVP matrix <ul><li>uniform mat4 MVPMatrix ; </li></ul><ul><li>attribute vec4 vertexPosition;  </li></ul><ul><...
Applying the MVP matrix(Contd) <ul><li>... </li></ul><ul><li>Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, viewMatr...
 
Giving life to the objects (Animation)
Animation <ul><li>Matrix.multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, modelMatrix, 0); </li></ul><ul><li>...
Upcoming SlideShare
Loading in …5
×
Upcoming SlideShare
OpenGL ES Presentation
Next
Download to read offline and view in fullscreen.

2

Share

Download to read offline

Introduction to open_gl_in_android

Download to read offline

Introduction to open_gl_in_android

  1. 1. Introduction To OpenGL in Android Tamillharasan Chandran & Krishnaprasad
  2. 2. Concepts Code!   Basic Shapes Animation
  3. 3. Concepts GPU Cross-Platform
  4. 4. Concepts      OpenGL OpenGL ES OpenGL ES Not just Android!
  5. 5. Concepts Vector Graphics Raster Graphics Lines points, lines, polygons, Surface normals etc .. Grid of pixels
  6. 6. OpenGL Rendering Pipeline Simplified
  7. 7. One more thing! State Machine
  8. 8. Code!
  9. 9. Make sure to add this!
  10. 10. Two Steps GLSurfaceView GLSurfaceView.Renderer
  11. 11. GLSurfaceView <ul><li>What is it? </li></ul><ul><li>Why is it needed? </li></ul>
  12. 12. GLSurfaceView           GLSurfaceView view = new GLSurfaceView(this);      view.setEGLContextClientVersion(2);      view.setRenderer(new SquareRenderer());
  13. 13. GLSurfaceView.Renderer <ul><li>What is it? </li></ul><ul><li>Why is it needed? </li></ul><ul><ul><li>Create a Subclass of GLSurfaceView.Renderer  </li></ul></ul><ul><ul><li>Setup the view port </li></ul></ul>
  14. 14. GLSurfaceView.Renderer public class SquareRenderer implements GLSurfaceView.Renderer {      public void onSurfaceCreated(GL10 unused, EGLConfig config) {                  }     public void onDrawFrame(GL10 unused) {      }     public void onSurfaceChanged(GL10 unused, int width, int height)   {              } } GLES20.glViewport(0, 0, width, height); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);   GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
  15. 15. There you go!! Your First Android OpenGL App
  16. 16. Drawing a Basic Shape
  17. 17. <ul><li>Define a Square </li></ul><ul><ul><li>All the 2D/ 3D objects needs to be defined using Primitives. </li></ul></ul><ul><ul><li>Vertex -  A vertex is a point where two or more edges meet </li></ul></ul><ul><ul><li>Edges - A vertex (vertices in plural) is the smallest building block of 3D model. A vertex is a point where two or more edges meet </li></ul></ul><ul><ul><li>Face is a triangle. Face is a surface between three corner vertices and three surrounding edges.  </li></ul></ul>
  18. 18. -0.5f, -0.5f, 0.0f, // Bottom Left 0.5f, -0.5f, 0.0f, // Bottom Right -0.5f, 0.5f, 0.0f, // Top Left 0.5f, 0.5f, 0.0f, // Top Right
  19. 19. private void initShapes(){   float squareCoords[] = {     //The coordinates      };   // initialize vertex Buffer for square   ByteBuffer vbb = ByteBuffer.allocateDirect(squareCoords.length * 4);   vbb.order(ByteOrder.nativeOrder());   squareVB = vbb.asFloatBuffer();    squareVB.put(squareCoords);    squareVB.position(0); }
  20. 20. <ul><li>Draw Methods  </li></ul><ul><ul><ul><li>glDrawArrays() </li></ul></ul></ul><ul><ul><ul><li>glDrawElements()   </li></ul></ul></ul><ul><li>Available Primitives in OpenGL ES </li></ul><ul><ul><li>GL_POINTS </li></ul></ul><ul><ul><li>GL_LINE_STRIP </li></ul></ul><ul><ul><li>GL_LINE_LOOP </li></ul></ul><ul><ul><li>GL_LINES </li></ul></ul><ul><ul><li>GL_TRIANGLES </li></ul></ul><ul><ul><li>GL_TRIANGLE_STRIP </li></ul></ul><ul><ul><li>GL_TRIANGLE_FAN </li></ul></ul>Draw a square 
  21. 21. GL_POINTS GL_LINES GL_LINE_STRIP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
  22. 22. OpenGL Rendering Pipeline Simplified
  23. 23. <ul><li>Shader Programs </li></ul><ul><ul><li>Vertex Shader </li></ul></ul><ul><ul><li>Fragment Shader </li></ul></ul><ul><ul><li>Loading the shader objects </li></ul></ul><ul><ul><li>Attaching the Shader objects to Shader program </li></ul></ul><ul><ul><li>Linking the program to create executable shader program </li></ul></ul>
  24. 25. Vertex Shader      Fragment Shader     attribute vec4 vertexPosition; void main(){   gl_Position = vertexPosition; } precision mediump float; void main(){   gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0); }
  25. 26. Loading the Shader <ul><li>GLES20.glCompileShader(vertexShader); </li></ul><ul><ul><li>GLES20.GL_FRAGMENT_SHADER </li></ul></ul>int vertexShader =           GLES20.glCreateShader(GLES20. GL_VERTEX_SHADER ); GLES20.glShaderSource(vertexShader, shaderCode);
  26. 27. Compiling and Linking the Shader program <ul><li>shaderProgram = GLES20.glCreateProgram(); </li></ul>GLES20.glAttachShader(shaderProgram, vertexShader);  GLES20.glAttachShader(shaderProgram, fragmentShader); GLES20.glLinkProgram(shaderProgram);
  27. 29. attributePositionHandle =     GLES20. glGetAttribLocation (shaderProgram,&quot;vertexPosition&quot;);
  28. 30. Drawing the square <ul><li>GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT |  </li></ul><ul><li>GLES20.GL_DEPTH_BUFFER_BIT); </li></ul><ul><li>GLES20.glUseProgram(shaderProgram); </li></ul><ul><li>GLES20.glVertexAttribPointer(attributePositionHandle,  </li></ul><ul><li>                              3, GLES20.GL_FLOAT,  </li></ul><ul><li>false, 12, squareVB); </li></ul><ul><li>GLES20.glEnableVertexAttribArray(attributePositionHandle); </li></ul><ul><li>GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); </li></ul>
  29. 32. Apply Projection and Camera View <ul><ul><li>Square Coordinate system is a mapped to non-square screen </li></ul></ul><ul><ul><li>To display Objects in correct proportions on different device screens </li></ul></ul>
  30. 33. Setting up the projection <ul><li>private float[] MVPMatrix = new float[16]; </li></ul><ul><li>private float[] projectionMatrix = new float[16]; </li></ul><ul><li>//-- </li></ul><ul><li>float ratio = (float) width / height; </li></ul><ul><li>Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7); </li></ul><ul><li>Matrix.setLookAtM(ViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); </li></ul>
  31. 34. Applying the MVP matrix <ul><li>uniform mat4 MVPMatrix ; </li></ul><ul><li>attribute vec4 vertexPosition;  </li></ul><ul><li>void main(){ </li></ul><ul><li>  gl_Position = MVPMatrix * vertexPosition;   </li></ul><ul><li>} </li></ul>
  32. 35. Applying the MVP matrix(Contd) <ul><li>... </li></ul><ul><li>Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, viewMatrix, 0); </li></ul><ul><li>GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, MVPMatrix, 0); </li></ul><ul><li>// Draw the square </li></ul><ul><li>GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); </li></ul><ul><li>... </li></ul>
  33. 37. Giving life to the objects (Animation)
  34. 38. Animation <ul><li>Matrix.multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, modelMatrix, 0); </li></ul><ul><li>GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, modelViewProjectionMatrix, 0); </li></ul>long time = SystemClock.uptimeMillis() % 4000L; float angle = 0.090f * ((int) time); Matrix.setRotateM(modelMatrix, 0, angle, 0, 0, 1.0f); Matrix.multiplyMM(modelViewProjectionMatrix,0,projectionMatrix,0,viewMatrix,0); 
  • shybovycha

    Jan. 19, 2015
  • darvind

    Sep. 26, 2013

Views

Total views

4,869

On Slideshare

0

From embeds

0

Number of embeds

935

Actions

Downloads

140

Shares

0

Comments

0

Likes

2

×