Introduction to open_gl_in_android

38
Introduction To OpenGL in Android Tamillharasan Chandran & Krishnaprasad

description

 

Transcript of Introduction to open_gl_in_android

Page 1: Introduction to open_gl_in_android

Introduction To OpenGL in Android

Tamillharasan Chandran&

Krishnaprasad

Page 2: Introduction to open_gl_in_android

Concepts

Code! 

Basic Shapes

Animation

Page 3: Introduction to open_gl_in_android

Concepts

GPUCross-Platform

Page 4: Introduction to open_gl_in_android

Concepts

    OpenGL

OpenGL ES

OpenGL ES

Not just Android!

Page 5: Introduction to open_gl_in_android

Concepts

Vector Graphics

Raster Graphics

Lines points, lines, polygons, Surface normals etc ..

Grid of pixels

Page 6: Introduction to open_gl_in_android

OpenGL Rendering Pipeline Simplified

Page 7: Introduction to open_gl_in_android

One more thing!

State Machine

Page 8: Introduction to open_gl_in_android

Code!

Page 9: Introduction to open_gl_in_android

Make sure to add this!

Page 10: Introduction to open_gl_in_android

Two Steps

GLSurfaceViewGLSurfaceView.Renderer

Page 11: Introduction to open_gl_in_android

GLSurfaceView

What is it?

Why is it needed?

Page 12: Introduction to open_gl_in_android

GLSurfaceView    

    GLSurfaceView view = new GLSurfaceView(this);

    view.setEGLContextClientVersion(2);

    view.setRenderer(new SquareRenderer());

Page 13: Introduction to open_gl_in_android

GLSurfaceView.Renderer

What is it?

Why is it needed?

• Create a Subclass of GLSurfaceView.Renderer 

• Setup the view port

Page 14: Introduction to open_gl_in_android

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);

Page 15: Introduction to open_gl_in_android

There you go!! Your First Android OpenGL App

Page 16: Introduction to open_gl_in_android

Drawing a Basic Shape

Page 17: Introduction to open_gl_in_android

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. 

Page 18: Introduction to open_gl_in_android

-0.5f, -0.5f, 0.0f, // Bottom Left0.5f, -0.5f, 0.0f, // Bottom Right-0.5f, 0.5f, 0.0f, // Top Left0.5f, 0.5f, 0.0f, // Top Right

Page 19: Introduction to open_gl_in_android

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);}

Page 20: Introduction to open_gl_in_android

Draw Methods o glDrawArrays()o 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 

Page 21: Introduction to open_gl_in_android

GL_POINTS GL_LINES GL_LINE_STRIP

GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

Page 22: Introduction to open_gl_in_android

OpenGL Rendering Pipeline Simplified

Page 23: Introduction to open_gl_in_android

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

Page 24: Introduction to open_gl_in_android
Page 25: Introduction to open_gl_in_android

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);}

Page 26: Introduction to open_gl_in_android

Loading the Shader

GLES20.glCompileShader(vertexShader);

• GLES20.GL_FRAGMENT_SHADER

int vertexShader =           GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);

GLES20.glShaderSource(vertexShader, shaderCode);

Page 27: Introduction to open_gl_in_android

Compiling and Linking the Shader program

shaderProgram = GLES20.glCreateProgram();

GLES20.glAttachShader(shaderProgram, vertexShader); GLES20.glAttachShader(shaderProgram, fragmentShader);

GLES20.glLinkProgram(shaderProgram);

Page 28: Introduction to open_gl_in_android
Page 29: Introduction to open_gl_in_android

attributePositionHandle =     GLES20.glGetAttribLocation(shaderProgram,"vertexPosition");

Page 30: Introduction to open_gl_in_android

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);

Page 31: Introduction to open_gl_in_android
Page 32: Introduction to open_gl_in_android

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

Page 33: Introduction to open_gl_in_android

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);

Page 34: Introduction to open_gl_in_android

Applying the MVP matrix

uniform mat4 MVPMatrix;attribute vec4 vertexPosition; void main(){ gl_Position = MVPMatrix * vertexPosition;  }

Page 35: Introduction to open_gl_in_android

Applying the MVP matrix(Contd)

...

Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, viewMatrix, 0);GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, MVPMatrix, 0);

// Draw the squareGLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

...

Page 36: Introduction to open_gl_in_android
Page 37: Introduction to open_gl_in_android

Giving life to the objects (Animation)

Page 38: Introduction to open_gl_in_android

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);