SlideShare a Scribd company logo
Intro to OpenGL ES
ES = Embedded (and mobile) Systems
OpenGL ES versions
● 1.x - deprecated, fixed function pipeline
● 2.0 - programmable pipeline, the most widespread API
● 3.0 - enhanced graphics (multiple rendering targets etc.)
○ 3.1 - general purpose compute (compute shaders, independent vertex and fragment shaders)
○ 3.2 - extensions to v.3.1 (based on Android Extension Pack)
Compatible devices
iOS Android
OpenGL ES Version Device Distribution
2.0 37.2%
3.0 45.1%
3.1 17.7%
Device
OpenGL ES Version
2.0 3.X
iPhone X
iPhone 8/8 Plus
iPhone 7/7 Plus
iPhone 6s/6s Plus
iPhone SE
iPhone 6/6 Plus
iPhone 5s
iPhone 5/5c
iPhone 4/4s
iPhone 3GS
Fixed vs Programmable
Fixed function pipeline (OpenGL ES 1.x)
Vertices Transform and
Lighting
Primitive
Assembly
Rasterizer
Color
Sum
Texture
Environment
Fog
Alpha
Test
Depth
Stencil
Color
Buffer
Blend
Dither Frame Buffer
Programmable pipeline (OpenGL ES 2.0)
Vertices Vertex
Shader
Primitive
Assembly
Rasterizer
Color
Sum
Fragment
Shader
Fog
Alpha
Test
Depth
Stencil
Color
Buffer
Blend
Dither Frame Buffer
Simplest Vertex Shader Code
attribute vec4 vPosition;
void main() {
gl_Position = vPosition;
gl_PointSize = 10.0; // Makes sense only when drawing points
}
Simplest Fragment Shader Code
precision mediump float;
void main() {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); //Everything will be green...
}
Let’s draw!
But first we need a surface...
● GLKit framework (GLKView and GLKViewController) on iOS
● GLSurfaceView or TextureView on Android
GLSurfaceView (Android)
● Implement and set rendering callback!
● OpenGL context will be created and attached to surface for you
(still possible to create on your own)
● The rendering context stores the appropriate OpenGL ES state
(state machine)
Renderer callback (pseudocode)
glView.setRenderer(new GLSurfaceView.Renderer() {
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
createNative();
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
initNative(width, height);
}
@Override
public void onDrawFrame(GL10 unused) {
drawNative();
}
});
createNative()
Create vertex/fragment shader
GLuint createShader(GLenum shaderType, const char *pSource) {
GLuint shader = glCreateShader(shaderType);
if (shader) {
glShaderSource(shader, 1, &pSource, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
// Log error, clear memory...
...
}
}
return shader;
}
Create and link program
GLuint createProgram(GLuint vertexShader, GLuint fragmentShader) {
GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
// Handle error
...
}
}
return program;
}
initNative(width, height)
Viewport
void initNative(int w, int h) {
// Specifies the affine transformation of x and y
// from normalized device coordinates
// to window coordinates.
glViewport(0, 0, w, h);
}
Image from https://developer.android.com/guide/topics/graphics/opengl.html#coordinate-mapping
drawNative()
Draw with shader
void drawSomething(GLuint shaderId) {
glClearColor(.0f, .0f, .0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderId);
// draw with program...
...
glUseProgram(0);
}
glDrawElements
● GL_POINTS
● GL_LINE_STRIP
● GL_LINE_LOOP
● GL_LINES
● GL_TRIANGLE_STRIP
● GL_TRIANGLE_FAN
● GL_TRIANGLES
Image from https://stackoverflow.com/questions/13789269/draw-a-ring-with-different-color-sector
Draw square
GLuint vPositionHandle = glGetAttribLocation(programId, "vPosition");
static const GLfloat triangleVertices[] = {
-0.5, 0.5, 0.0f, // square left top vertex, 0 index
-0.5, -0.5, 0.0f, // square left bottom vertex, 1 index
0.5, -0.5, 0.0f, // square right bottom vertex, 2 index
0.5, 0.5, 0.0f, // square right top vertex, 3 index
};
const GLubyte indices[] = {0, 1, 2, 0, 2, 3};
glVertexAttribPointer(vPositionHandle, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices);
glEnableVertexAttribArray(vPositionHandle);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
Texturing
Texture Vertex Shader
attribute vec4 vPosition;
attribute vec2 vTexCoord;
varying vec2 v_TexCoordinate;
void main() {
v_TexCoordinate = vTexCoord;
gl_Position = vPosition;
}
Texture Fragment Shader
precision mediump float;
uniform sampler2D tex;
varying vec2 v_TexCoordinate;
void main() {
gl_FragColor = texture2D(tex, v_TexCoordinate);
}
http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/texture-coordinates/
Set texture coordinates
GLfloat texCoords[] = {
0, 0,
0, 1,
1, 1,
1, 0
};
GLuint vTexCoordHandle;
vTexCoordHandle = glGetAttribLocation(textureShader->getId(), "vTexCoord");
glVertexAttribPointer(vTexCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, texCoords);
glEnableVertexAttribArray(vTexCoordHandle);
Bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(glGetUniformLocation(programId, "tex"), 0);
Transformations
Vertex Shader
attribute vec4 vPosition;
attribute vec4 vColor;
varying vec4 v_Color;
uniform mat4 uMVPMatrix;
void main() {
v_Color = vColor;
gl_Position = uMVPMatrix * vPosition;
}
Fragment Shader
precision mediump float;
varying vec4 v_Color;
varying vec2 v_TexCoordinate;
void main() {
gl_FragColor = v_Color;
}
Cube vertices and indices
float d = 0.5f;
const GLfloat vertices[] = {
// near cube face
-d, d, d, // left top vertex, 0 index
-d, -d, d, // left bottom vertex, 1 index
d, -d, d, // right bottom vertex, 2 index
d, d, d, // right top vertex, 3 index
// far cube face
-d, d, -d, // left top vertex, 4 index
-d, -d, -d, // left bottom vertex, 5 index
d, -d, -d, // right bottom vertex, 6 index
d, d, -d, // right top vertex, 7 index
};
const GLubyte indices[] = {
0, 1, 2, 2, 3, 0, // front face
6, 5, 4, 4, 7, 6, // rear face
4, 0, 3, 3, 7, 4, // top face
1, 5, 6, 6, 2, 1, // bottom face
4, 5, 1, 1, 0, 4, // left face
3, 2, 6, 6, 7, 3, // right face
};
Set colors per vertex
const float colors[] = {
0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
};
const int componentsPerColor = 3;
GLuint colorHandle = glGetAttribLocation(programId, "vColor");
glEnableVertexAttribArray(colorHandle);
glVertexAttribPointer(colorHandle, 4, GL_FLOAT, false, componentsPerColor * 4, colors);
Set matrices
//on init...
projMatr = matr4::frustum(-aspectRatio, aspectRatio, -1, 1, 3, 7);
viewMatr = matr4::lookAt(0, 0, -4, 0, 0, 0, 0, 1, 0);
//on draw...
modelMatr = matr4::rotateX(angleX) * matr4::rotateY(angleY) * matr4::rotateZ(angleZ);
matr4 mvp = projMatr * viewMatr * modelMatr;
GLuint modelViewProjectionHandle = glGetUniformLocation(programId, "uMVPMatrix");
GL2::uniformMatrix4fv(modelViewProjectionHandle, 1, GL_FALSE, mvp.ptr());
References
1. https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf
2. https://www.amazon.com/OpenGL-ES-2-0-Programming-Guide-ebook/dp/B004Z6EW5O
3. https://developer.apple.com/library/content/documentation/3DDrawing/Conceptual/OpenGLES_Prog
rammingGuide/Introduction/Introduction.html
4. https://developer.android.com/training/graphics/opengl/index.html
Android sample on GitHub
https://github.com/AlexanderKozubets/opengl-sample-android
Thank you for attention!

More Related Content

What's hot

2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
vandna123
 
Proyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital IProyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital I
Daniel A. Lopez Ch.
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
mohamed sikander
 
Digital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 unitsDigital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 units
Lekashri Subramanian
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
Chris Ohk
 
Gerbang Logika Dasar
Gerbang Logika DasarGerbang Logika Dasar
Gerbang Logika Dasar
Arif Hakim
 
7segment scetch
7segment scetch7segment scetch
7segment scetch
Bang Igo
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
Ch shampi Ch shampi
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
Palak Sanghani
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
Syed Umair
 
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT SolversAn Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
Tobias Philipp
 
C questions
C questionsC questions
C questions
MD Rashid
 
1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional
Isabella789
 

What's hot (14)

2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Proyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital IProyecto final curso – Electrónica Digital I
Proyecto final curso – Electrónica Digital I
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Digital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 unitsDigital logic circuits important question and answers for 5 units
Digital logic circuits important question and answers for 5 units
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Gerbang Logika Dasar
Gerbang Logika DasarGerbang Logika Dasar
Gerbang Logika Dasar
 
7segment scetch
7segment scetch7segment scetch
7segment scetch
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
 
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT SolversAn Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
 
C questions
C questionsC questions
C questions
 
1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional
 

Similar to Intro to OpenGL ES 2.0

Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
Rup Chowdhury
 
GL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdfGL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdf
shaikhshehzad024
 
Open GL ES Android
Open GL ES AndroidOpen GL ES Android
Open GL ES Android
Mindos Cheng
 
Bai 1
Bai 1Bai 1
Opengl basics
Opengl basicsOpengl basics
Opengl basics
pushpa latha
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
Susan Gold
 
Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School
Flink Forward
 
Shadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics HardwareShadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics Hardware
stefan_b
 
Open gles
Open glesOpen gles
Open gles
sarmisthadas
 
Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5
Takao Wada
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)
Alex Larcheveque
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
amitsarda3
 
Tao Fayan_Iso and Full_volume rendering
Tao Fayan_Iso and Full_volume renderingTao Fayan_Iso and Full_volume rendering
Tao Fayan_Iso and Full_volume rendering
Fayan TAO
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slides
tamillarasan
 
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdflectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
SanjeevSaharan5
 
OpenGL Introduction
OpenGL IntroductionOpenGL Introduction
OpenGL Introduction
Jayant Mukherjee
 
OpenGL SC 2.0 Quick Reference
OpenGL SC 2.0 Quick ReferenceOpenGL SC 2.0 Quick Reference
OpenGL SC 2.0 Quick Reference
The Khronos Group Inc.
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
ICS
 
OpenGL ES 2.x Programming Introduction
OpenGL ES 2.x Programming IntroductionOpenGL ES 2.x Programming Introduction
OpenGL ES 2.x Programming Introduction
Champ Yen
 
Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
Samsung Open Source Group
 

Similar to Intro to OpenGL ES 2.0 (20)

Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
 
GL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdfGL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdf
 
Open GL ES Android
Open GL ES AndroidOpen GL ES Android
Open GL ES Android
 
Bai 1
Bai 1Bai 1
Bai 1
 
Opengl basics
Opengl basicsOpengl basics
Opengl basics
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School
 
Shadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics HardwareShadow Volumes on Programmable Graphics Hardware
Shadow Volumes on Programmable Graphics Hardware
 
Open gles
Open glesOpen gles
Open gles
 
Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5
 
Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)Final Design Project - Memo (with GUI)
Final Design Project - Memo (with GUI)
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Tao Fayan_Iso and Full_volume rendering
Tao Fayan_Iso and Full_volume renderingTao Fayan_Iso and Full_volume rendering
Tao Fayan_Iso and Full_volume rendering
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slides
 
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdflectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
 
OpenGL Introduction
OpenGL IntroductionOpenGL Introduction
OpenGL Introduction
 
OpenGL SC 2.0 Quick Reference
OpenGL SC 2.0 Quick ReferenceOpenGL SC 2.0 Quick Reference
OpenGL SC 2.0 Quick Reference
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
OpenGL ES 2.x Programming Introduction
OpenGL ES 2.x Programming IntroductionOpenGL ES 2.x Programming Introduction
OpenGL ES 2.x Programming Introduction
 
Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
 

Recently uploaded

What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 

Recently uploaded (20)

What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 

Intro to OpenGL ES 2.0

  • 2. ES = Embedded (and mobile) Systems
  • 3. OpenGL ES versions ● 1.x - deprecated, fixed function pipeline ● 2.0 - programmable pipeline, the most widespread API ● 3.0 - enhanced graphics (multiple rendering targets etc.) ○ 3.1 - general purpose compute (compute shaders, independent vertex and fragment shaders) ○ 3.2 - extensions to v.3.1 (based on Android Extension Pack)
  • 4. Compatible devices iOS Android OpenGL ES Version Device Distribution 2.0 37.2% 3.0 45.1% 3.1 17.7% Device OpenGL ES Version 2.0 3.X iPhone X iPhone 8/8 Plus iPhone 7/7 Plus iPhone 6s/6s Plus iPhone SE iPhone 6/6 Plus iPhone 5s iPhone 5/5c iPhone 4/4s iPhone 3GS
  • 6. Fixed function pipeline (OpenGL ES 1.x) Vertices Transform and Lighting Primitive Assembly Rasterizer Color Sum Texture Environment Fog Alpha Test Depth Stencil Color Buffer Blend Dither Frame Buffer
  • 7. Programmable pipeline (OpenGL ES 2.0) Vertices Vertex Shader Primitive Assembly Rasterizer Color Sum Fragment Shader Fog Alpha Test Depth Stencil Color Buffer Blend Dither Frame Buffer
  • 8.
  • 9. Simplest Vertex Shader Code attribute vec4 vPosition; void main() { gl_Position = vPosition; gl_PointSize = 10.0; // Makes sense only when drawing points }
  • 10. Simplest Fragment Shader Code precision mediump float; void main() { gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); //Everything will be green... }
  • 12. But first we need a surface... ● GLKit framework (GLKView and GLKViewController) on iOS ● GLSurfaceView or TextureView on Android
  • 13. GLSurfaceView (Android) ● Implement and set rendering callback! ● OpenGL context will be created and attached to surface for you (still possible to create on your own) ● The rendering context stores the appropriate OpenGL ES state (state machine)
  • 14. Renderer callback (pseudocode) glView.setRenderer(new GLSurfaceView.Renderer() { @Override public void onSurfaceCreated(GL10 unused, EGLConfig config) { createNative(); } @Override public void onSurfaceChanged(GL10 unused, int width, int height) { initNative(width, height); } @Override public void onDrawFrame(GL10 unused) { drawNative(); } });
  • 16. Create vertex/fragment shader GLuint createShader(GLenum shaderType, const char *pSource) { GLuint shader = glCreateShader(shaderType); if (shader) { glShaderSource(shader, 1, &pSource, NULL); glCompileShader(shader); GLint compiled = 0; glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); if (!compiled) { // Log error, clear memory... ... } } return shader; }
  • 17. Create and link program GLuint createProgram(GLuint vertexShader, GLuint fragmentShader) { GLuint program = glCreateProgram(); if (program) { glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); glLinkProgram(program); GLint linkStatus = GL_FALSE; glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); if (linkStatus != GL_TRUE) { // Handle error ... } } return program; }
  • 19. Viewport void initNative(int w, int h) { // Specifies the affine transformation of x and y // from normalized device coordinates // to window coordinates. glViewport(0, 0, w, h); } Image from https://developer.android.com/guide/topics/graphics/opengl.html#coordinate-mapping
  • 21. Draw with shader void drawSomething(GLuint shaderId) { glClearColor(.0f, .0f, .0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shaderId); // draw with program... ... glUseProgram(0); }
  • 22. glDrawElements ● GL_POINTS ● GL_LINE_STRIP ● GL_LINE_LOOP ● GL_LINES ● GL_TRIANGLE_STRIP ● GL_TRIANGLE_FAN ● GL_TRIANGLES Image from https://stackoverflow.com/questions/13789269/draw-a-ring-with-different-color-sector
  • 23. Draw square GLuint vPositionHandle = glGetAttribLocation(programId, "vPosition"); static const GLfloat triangleVertices[] = { -0.5, 0.5, 0.0f, // square left top vertex, 0 index -0.5, -0.5, 0.0f, // square left bottom vertex, 1 index 0.5, -0.5, 0.0f, // square right bottom vertex, 2 index 0.5, 0.5, 0.0f, // square right top vertex, 3 index }; const GLubyte indices[] = {0, 1, 2, 0, 2, 3}; glVertexAttribPointer(vPositionHandle, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices); glEnableVertexAttribArray(vPositionHandle); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
  • 25. Texture Vertex Shader attribute vec4 vPosition; attribute vec2 vTexCoord; varying vec2 v_TexCoordinate; void main() { v_TexCoordinate = vTexCoord; gl_Position = vPosition; }
  • 26. Texture Fragment Shader precision mediump float; uniform sampler2D tex; varying vec2 v_TexCoordinate; void main() { gl_FragColor = texture2D(tex, v_TexCoordinate); } http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/texture-coordinates/
  • 27. Set texture coordinates GLfloat texCoords[] = { 0, 0, 0, 1, 1, 1, 1, 0 }; GLuint vTexCoordHandle; vTexCoordHandle = glGetAttribLocation(textureShader->getId(), "vTexCoord"); glVertexAttribPointer(vTexCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, texCoords); glEnableVertexAttribArray(vTexCoordHandle);
  • 30. Vertex Shader attribute vec4 vPosition; attribute vec4 vColor; varying vec4 v_Color; uniform mat4 uMVPMatrix; void main() { v_Color = vColor; gl_Position = uMVPMatrix * vPosition; }
  • 31. Fragment Shader precision mediump float; varying vec4 v_Color; varying vec2 v_TexCoordinate; void main() { gl_FragColor = v_Color; }
  • 32. Cube vertices and indices float d = 0.5f; const GLfloat vertices[] = { // near cube face -d, d, d, // left top vertex, 0 index -d, -d, d, // left bottom vertex, 1 index d, -d, d, // right bottom vertex, 2 index d, d, d, // right top vertex, 3 index // far cube face -d, d, -d, // left top vertex, 4 index -d, -d, -d, // left bottom vertex, 5 index d, -d, -d, // right bottom vertex, 6 index d, d, -d, // right top vertex, 7 index }; const GLubyte indices[] = { 0, 1, 2, 2, 3, 0, // front face 6, 5, 4, 4, 7, 6, // rear face 4, 0, 3, 3, 7, 4, // top face 1, 5, 6, 6, 2, 1, // bottom face 4, 5, 1, 1, 0, 4, // left face 3, 2, 6, 6, 7, 3, // right face };
  • 33. Set colors per vertex const float colors[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, }; const int componentsPerColor = 3; GLuint colorHandle = glGetAttribLocation(programId, "vColor"); glEnableVertexAttribArray(colorHandle); glVertexAttribPointer(colorHandle, 4, GL_FLOAT, false, componentsPerColor * 4, colors);
  • 34. Set matrices //on init... projMatr = matr4::frustum(-aspectRatio, aspectRatio, -1, 1, 3, 7); viewMatr = matr4::lookAt(0, 0, -4, 0, 0, 0, 0, 1, 0); //on draw... modelMatr = matr4::rotateX(angleX) * matr4::rotateY(angleY) * matr4::rotateZ(angleZ); matr4 mvp = projMatr * viewMatr * modelMatr; GLuint modelViewProjectionHandle = glGetUniformLocation(programId, "uMVPMatrix"); GL2::uniformMatrix4fv(modelViewProjectionHandle, 1, GL_FALSE, mvp.ptr());
  • 35.
  • 36. References 1. https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf 2. https://www.amazon.com/OpenGL-ES-2-0-Programming-Guide-ebook/dp/B004Z6EW5O 3. https://developer.apple.com/library/content/documentation/3DDrawing/Conceptual/OpenGLES_Prog rammingGuide/Introduction/Introduction.html 4. https://developer.android.com/training/graphics/opengl/index.html
  • 37. Android sample on GitHub https://github.com/AlexanderKozubets/opengl-sample-android
  • 38. Thank you for attention!