Introduction to open_gl_in_android

4,029 views
3,837 views

Published on

Published in: Technology, Art & Photos
2 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
4,029
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
114
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide
  • The OpenGL API itself is not a programming language like C or C++. It is more like the C runtime library, which provides some prepackaged functionality. On the other hand, the OpenGL specification includes GLSL, the OpenGL Shading Language, which actually is a very C-like programming language. GLSL, however, does not control your application’s flow and logic, but rather it is intended for rendering operations. At a high level, application programs are not written in OpenGL, as much as they use OpenGL. There really is no such thing as an “OpenGL program”
  • A Brief explanation about the latest Programmable pipleline. What is a shader ?  
  • Explain about different stages in opengl pipeline. questions from audience
  • Explain about different stages in opengl pipeline. questions from audience
  • 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); 

    ×