Introduction to open_gl_in_android

  • 3,400 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
3,400
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
99
Comments
2
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

Transcript

  • 1. Introduction To OpenGL in Android Tamillharasan Chandran & Krishnaprasad
  • 2. Concepts Code!   Basic Shapes Animation
  • 3. Concepts GPU Cross-Platform
  • 4. Concepts      OpenGL OpenGL ES OpenGL ES Not just Android!
  • 5. Concepts Vector Graphics Raster Graphics Lines points, lines, polygons, Surface normals etc .. Grid of pixels
  • 6. OpenGL Rendering Pipeline Simplified
  • 7. One more thing! State Machine
  • 8. Code!
  • 9. Make sure to add this!
  • 10. Two Steps GLSurfaceView GLSurfaceView.Renderer
  • 11. GLSurfaceView
    • What is it?
    • Why is it needed?
  • 12. GLSurfaceView           GLSurfaceView view = new GLSurfaceView(this);      view.setEGLContextClientVersion(2);      view.setRenderer(new SquareRenderer());
  • 13. GLSurfaceView.Renderer
    • What is it?
    • Why is it needed?
      • Create a Subclass of GLSurfaceView.Renderer 
      • Setup the view port
  • 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. There you go!! Your First Android OpenGL App
  • 16. Drawing a Basic Shape
  • 17.
    • Define a Square
      • All the 2D/ 3D objects needs to be defined using Primitives.
      • Vertex -  A vertex is a point where two or more edges meet
      • 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
      • Face is a triangle. Face is a surface between three corner vertices and three surrounding edges. 
  • 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. 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.
    • Draw Methods 
        • glDrawArrays()
        • glDrawElements()  
    • Available Primitives in OpenGL ES
      • GL_POINTS
      • GL_LINE_STRIP
      • GL_LINE_LOOP
      • GL_LINES
      • GL_TRIANGLES
      • GL_TRIANGLE_STRIP
      • GL_TRIANGLE_FAN
    Draw a square 
  • 21. GL_POINTS GL_LINES GL_LINE_STRIP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
  • 22. OpenGL Rendering Pipeline Simplified
  • 23.
    • Shader Programs
      • Vertex Shader
      • Fragment Shader
      • Loading the shader objects
      • Attaching the Shader objects to Shader program
      • Linking the program to create executable shader program
  • 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); }
  • 26. Loading the Shader
    • GLES20.glCompileShader(vertexShader);
      • GLES20.GL_FRAGMENT_SHADER
    int vertexShader =           GLES20.glCreateShader(GLES20. GL_VERTEX_SHADER ); GLES20.glShaderSource(vertexShader, shaderCode);
  • 27. Compiling and Linking the Shader program
    • shaderProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(shaderProgram, vertexShader);  GLES20.glAttachShader(shaderProgram, fragmentShader); GLES20.glLinkProgram(shaderProgram);
  • 28.  
  • 29. attributePositionHandle =     GLES20. glGetAttribLocation (shaderProgram,"vertexPosition");
  • 30. Drawing the square
    • GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | 
    • GLES20.GL_DEPTH_BUFFER_BIT);
    • GLES20.glUseProgram(shaderProgram);
    • GLES20.glVertexAttribPointer(attributePositionHandle, 
    •                               3, GLES20.GL_FLOAT, 
    • false, 12, squareVB);
    • GLES20.glEnableVertexAttribArray(attributePositionHandle);
    • GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  • 31.  
  • 32. Apply Projection and Camera View
      • Square Coordinate system is a mapped to non-square screen
      • To display Objects in correct proportions on different device screens
  • 33. Setting up the projection
    • private float[] MVPMatrix = new float[16];
    • private float[] projectionMatrix = new float[16];
    • //--
    • float ratio = (float) width / height;
    • Matrix.frustumM(projectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
    • Matrix.setLookAtM(ViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
  • 34. Applying the MVP matrix
    • uniform mat4 MVPMatrix ;
    • attribute vec4 vertexPosition; 
    • void main(){
    •   gl_Position = MVPMatrix * vertexPosition;  
    • }
  • 35. Applying the MVP matrix(Contd)
    • ...
    • Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
    • GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, MVPMatrix, 0);
    • // Draw the square
    • GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    • ...
  • 36.  
  • 37. Giving life to the objects (Animation)
  • 38. Animation
    • Matrix.multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, modelMatrix, 0);
    • GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
    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);