SlideShare a Scribd company logo
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 What is it? Why is it needed?
GLSurfaceView           GLSurfaceView view = new GLSurfaceView(this);      view.setEGLContextClientVersion(2);      view.setRenderer(new SquareRenderer());
GLSurfaceView.Renderer What is it? Why is it needed? Create a Subclass of GLSurfaceView.Renderer  Setup the view port
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);
There you go!! Your First Android OpenGL App
Drawing a Basic Shape
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. 
-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
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); }
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 
GL_POINTS GL_LINES GL_LINE_STRIP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
OpenGL Rendering Pipeline Simplified
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
 
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); }
Loading the Shader GLES20.glCompileShader(vertexShader); GLES20.GL_FRAGMENT_SHADER int vertexShader =           GLES20.glCreateShader(GLES20. GL_VERTEX_SHADER ); GLES20.glShaderSource(vertexShader, shaderCode);
Compiling and Linking the Shader program shaderProgram = GLES20.glCreateProgram(); GLES20.glAttachShader(shaderProgram, vertexShader);  GLES20.glAttachShader(shaderProgram, fragmentShader); GLES20.glLinkProgram(shaderProgram);
 
attributePositionHandle =     GLES20. glGetAttribLocation (shaderProgram,"vertexPosition");
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);
 
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
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);
Applying the MVP matrix uniform mat4 MVPMatrix ; attribute vec4 vertexPosition;  void main(){   gl_Position =  MVPMatrix  * vertexPosition;   }
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); ...
 
Giving life to the objects (Animation)
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); 

More Related Content

PPTX
OpenGL Introduction
PPTX
Introduction to open gl in android droidcon - slides
PDF
OpenGLES - Graphics Programming in Android
PDF
OpenGLES Android Graphics
PPTX
Opengl presentation
PDF
Graphics programming in open gl
PDF
Introduction of openGL
PDF
OpenGL Introduction
OpenGL Introduction
Introduction to open gl in android droidcon - slides
OpenGLES - Graphics Programming in Android
OpenGLES Android Graphics
Opengl presentation
Graphics programming in open gl
Introduction of openGL
OpenGL Introduction

What's hot (20)

PDF
Open gl
PDF
Getting Started with OpenGL ES
PDF
OpenGL L01-Primitives
PDF
Opengl basics
PPTX
Lecture 6 introduction to open gl and glut
PPTX
OpenGL ES Presentation
PPTX
Baiscs of OpenGL
PPT
Open gl
PDF
OpenGL L03-Utilities
PDF
OpenGL L07-Skybox and Terrian
PDF
OpenGL L02-Transformations
PDF
OpenGL Introduction.
PDF
OpenGL L06-Performance
PDF
Android High performance in GPU using opengles and renderscript
PPT
Programming with OpenGL
PPTX
Chapter02 graphics-programming
PDF
OpenGL Starter L02
PDF
Open gl basics
PPTX
OpenGL Shading Language
PPT
NV_path rendering Functional Improvements
Open gl
Getting Started with OpenGL ES
OpenGL L01-Primitives
Opengl basics
Lecture 6 introduction to open gl and glut
OpenGL ES Presentation
Baiscs of OpenGL
Open gl
OpenGL L03-Utilities
OpenGL L07-Skybox and Terrian
OpenGL L02-Transformations
OpenGL Introduction.
OpenGL L06-Performance
Android High performance in GPU using opengles and renderscript
Programming with OpenGL
Chapter02 graphics-programming
OpenGL Starter L02
Open gl basics
OpenGL Shading Language
NV_path rendering Functional Improvements
Ad

Viewers also liked (20)

DOC
Socialmedia connecting the world with businesses
PPT
routing Protocols and Virtual private network
PDF
LTE Physical layer aspects
KEY
jQTouch at jQuery Conference 2010
PDF
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
PPTX
Introduction to LESS
PDF
jQTouch and Titanium
PDF
OpenGL NVIDIA Command-List: Approaching Zero Driver Overhead
PDF
OpenGL ES 2.x Programming Introduction
KEY
HTML5 로 iPhone App 만들기
KEY
jQTouch – Mobile Web Apps with HTML, CSS and JavaScript
PPTX
Apache web server
PDF
jQuery in 15 minutes
PPTX
jQuery PPT
PDF
Modern JavaScript Applications: Design Patterns
PDF
jQuery for beginners
PPT
Comparison and Contrast between OSI and TCP/IP Model
PPT
OpenGL Basics
PPTX
Routing Information Protocol
ODP
Apache ppt
Socialmedia connecting the world with businesses
routing Protocols and Virtual private network
LTE Physical layer aspects
jQTouch at jQuery Conference 2010
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
Introduction to LESS
jQTouch and Titanium
OpenGL NVIDIA Command-List: Approaching Zero Driver Overhead
OpenGL ES 2.x Programming Introduction
HTML5 로 iPhone App 만들기
jQTouch – Mobile Web Apps with HTML, CSS and JavaScript
Apache web server
jQuery in 15 minutes
jQuery PPT
Modern JavaScript Applications: Design Patterns
jQuery for beginners
Comparison and Contrast between OSI and TCP/IP Model
OpenGL Basics
Routing Information Protocol
Apache ppt
Ad

Similar to Introduction to open_gl_in_android (20)

PDF
Intro to OpenGL ES 2.0
PDF
GL Shading Language Document by OpenGL.pdf
PPT
Open gles
PDF
Android open gl2_droidcon_2014
PDF
The Ring programming language version 1.5.4 book - Part 110 of 185
PDF
The Ring programming language version 1.8 book - Part 179 of 202
PDF
The Ring programming language version 1.7 book - Part 169 of 196
PDF
The Ring programming language version 1.5.2 book - Part 135 of 181
PDF
The Ring programming language version 1.5.4 book - Part 119 of 185
PDF
The Ring programming language version 1.6 book - Part 151 of 189
PDF
OpenGL ES 3.0 2013
PDF
The Ring programming language version 1.9 book - Part 177 of 210
PDF
The Ring programming language version 1.5.3 book - Part 143 of 184
PDF
A Novice's Guide to WebGL
PDF
The Ring programming language version 1.5.1 book - Part 134 of 180
PDF
OpenGL Starter L01
PDF
The Ring programming language version 1.7 book - Part 141 of 196
PPT
CS 354 Viewing Stuff
Intro to OpenGL ES 2.0
GL Shading Language Document by OpenGL.pdf
Open gles
Android open gl2_droidcon_2014
The Ring programming language version 1.5.4 book - Part 110 of 185
The Ring programming language version 1.8 book - Part 179 of 202
The Ring programming language version 1.7 book - Part 169 of 196
The Ring programming language version 1.5.2 book - Part 135 of 181
The Ring programming language version 1.5.4 book - Part 119 of 185
The Ring programming language version 1.6 book - Part 151 of 189
OpenGL ES 3.0 2013
The Ring programming language version 1.9 book - Part 177 of 210
The Ring programming language version 1.5.3 book - Part 143 of 184
A Novice's Guide to WebGL
The Ring programming language version 1.5.1 book - Part 134 of 180
OpenGL Starter L01
The Ring programming language version 1.7 book - Part 141 of 196
CS 354 Viewing Stuff

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Diabetes mellitus diagnosis method based random forest with bat algorithm
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.

Introduction to open_gl_in_android

  • 1. Introduction To OpenGL in Android Tamillharasan Chandran & Krishnaprasad
  • 2. Concepts Code!   Basic Shapes Animation
  • 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
  • 7. One more thing! State Machine
  • 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
  • 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
  • 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); 

Editor's Notes

  • #4: 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”
  • #5: A Brief explanation about the latest Programmable pipleline. What is a shader ?  
  • #7: Explain about different stages in opengl pipeline. questions from audience
  • #23: Explain about different stages in opengl pipeline. questions from audience