Android and OpenGL. Android Smartphone Programming. Matthias Keil. University of Freiburg

Similar documents
Precept 2 Aleksey Boyko February 18, 2011

CIS 636 Interactive Computer Graphics CIS 736 Computer Graphics Spring 2011

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL II)

Advanced Computer Graphics (CS & SE )

Getting Started. Overview (1): Getting Started (1): Getting Started (2): Getting Started (3): COSC 4431/5331 Computer Graphics.

Spring 2013, CS 112 Programming Assignment 2 Submission Due: April 26, 2013

CS452/552; EE465/505. Review & Examples

1 (Practice 1) Introduction to OpenGL

1 Transformations. Chapter 1. Transformations. Department of Computer Science and Engineering 1-1

Geometry: Outline. Projections. Orthographic Perspective

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

COMS 4160: Problems on Transformations and OpenGL

GATAV 9. Goodby flatland Welcome to 3D!

Computer Viewing Computer Graphics I, Fall 2008

CS380: Computer Graphics Viewing Transformation. Sung-Eui Yoon ( 윤성의 ) Course URL:

Wire-Frame 3D Graphics Accelerator IP Core. C Library Specification

Viewing Transformation

Computer Graphics. Chapter 10 Three-Dimensional Viewing

Order of Transformations

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Graphics and Visualization

3D Graphics Pipeline II Clipping. Instructor Stephen J. Guy

The Viewing Pipeline adaptation of Paul Bunn & Kerryn Hugo s notes

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

CS354 Computer Graphics Viewing and Modeling

MORE OPENGL. Pramook Khungurn CS 4621, Fall 2011

7. 3D Viewing. Projection: why is projection necessary? CS Dept, Univ of Kentucky

Lecture 4 of 41. Lab 1a: OpenGL Basics

COMP3421. Introduction to 3D Graphics

Lecture 9 Sections 2.6, 5.1, 5.2. Wed, Sep 16, 2009

OpenGL on Android. Lecture 7. Android and Low-level Optimizations Summer School. 27 July 2015

Mobile Application Programing: Android. OpenGL Operation

CS 591B Lecture 9: The OpenGL Rendering Pipeline

COMP Computer Graphics and Image Processing. 5: Viewing 1: The camera. In part 1 of our study of Viewing, we ll look at ˆʹ U ˆ ʹ F ˆʹ S

Introduction to OpenGL. CS 4620 Balazs Kovacs, 2014 Daniel Schroeder, 2013 Pramook Khungurn, 2012

Fachhochschule Regensburg, Germany, February 15, 2017

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

COMP3421. Introduction to 3D Graphics

Transformation Pipeline

CS 4204 Computer Graphics

COMP3421. Introduction to 3D Graphics

Chapter 3 - Basic Mathematics for 3D Computer Graphics

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

Mobile Application Programing: Android. OpenGL Operation

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

part 3 Martin Samuelčík Room I4

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

3D Viewing. With acknowledge to: Ed Angel. Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

CS1950U Setup Spring 2018

The Projection Matrix

Matrix-Rechnung I ' z =... Universität Frankfurt

Visualizing Molecular Dynamics

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Profiling and Debugging Games on Mobile Platforms

Overview. Viewing and perspectives. Planar Geometric Projections. Classical Viewing. Classical views Computer viewing Perspective normalization

Overview of Projections: From a 3D world to a 2D screen.

OpenGL Transformations

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Mobile Application Programming: Android. OpenGL Operation

CS 4620 Program 3: Pipeline

Computer Graphics. Chapter 7 2D Geometric Transformations

Motivation. Sampling and Reconstruction of Visual Appearance. Effects needed for Realism. Ray Tracing. Outline

Introduction to OpenGL

Computer Graphics. Basic 3D Programming. Contents

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

Today s class. Viewing transformation Menus Mandelbrot set and pixel drawing. Informationsteknologi

Shaders. Slide credit to Prof. Zwicker

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Hands-On Workshop: 3D Automotive Graphics on Connected Radios Using Rayleigh and OpenGL ES 2.0

OpenGL. 1 OpenGL OpenGL 1.2 3D. (euske) 1. Client-Server Model OpenGL

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

CSC 470 Computer Graphics. Three Dimensional Viewing

Overview. By end of the week:

CSC 470 Computer Graphics

CS451Real-time Rendering Pipeline

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

Graphics Programming

Computer Graphics. Bing-Yu Chen National Taiwan University

CITSTUDENTS.IN VIEWING. Computer Graphics and Visualization. Classical and computer viewing. Viewing with a computer. Positioning of the camera

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Viewing and Projection

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

CS 130 Final. Fall 2015

Three-Dimensional Graphics III. Guoying Zhao 1 / 67

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Computer graphics MN1

I think this assignment should have 8 objectives but we should still mark it out of 10. The Mean TA.

Rendering Objects. Need to transform all geometry then

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

For each question, indicate whether the statement is true or false by circling T or F, respectively.

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Computer Viewing. Prof. George Wolberg Dept. of Computer Science City College of New York

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

OPENGL AND GLSL. Computer Graphics

Basic Graphics Programming

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

E.Order of Operations

Transcription:

Android and OpenGL Android Smartphone Programming Matthias Keil Institute for Computer Science Faculty of Engineering 1. Februar 2016

Outline 1 OpenGL Introduction 2 Displaying Graphics 3 Interaction 4 Notes 5 Summary Matthias Keil Android and OpenGL 1. Februar 2016 2 / 24

OpenGL Introduction Short for: Open Graphics Library[4]. Enables creation of 2D and 3D graphics. Special API for embedded systems available on Android: OpenGL ES API. Two important classes: GLSurfaceView and GLSurfaceView.Renderer. Matthias Keil Android and OpenGL 1. Februar 2016 3 / 24

OpenGL Introduction Important Classes GLSurfaceView View to draw and manipulate objects using OpenGL. GLSurfaceView.Renderer Interface defining methods to draw (render) graphics. Add renderer to GLSurfaceView using GLSurfaceView.setRenderer(). Extend GLSurfaceView to capture touch screen events. Extend Android manifest when using OpenGL ES 2.0: 1 <! -- Tell the system this app requires OpenGL ES 2.0. --> 2 < uses - feature android: glesversion =" 0 x00020000 " android:required =" true " /> Matthias Keil Android and OpenGL 1. Februar 2016 4 / 24

OpenGL Introduction Example 1 class MyGLSurfaceView extends GLSurfaceView { 2 public MyGLSurfaceView ( Context context ){ 3 super ( context ); 4 setrenderer ( new MyRenderer ()); 5 // Called when using OpenGL ES 2.0 6 seteglcontextclientversion (2) ; 7 } 8 } Matthias Keil Android and OpenGL 1. Februar 2016 5 / 24

OpenGL Introduction GLSurfaceView.Renderer Includes three methods to be implemented to draw graphics. onsurfacecreated() Called once when creating the GLSurfaceView. Should include all actions to do only once. ondrawframe() Called on each redraw of GLSurfaceView. Do all drawing and redrawing of graphic objects here. onsurfacechanged() Called when the geometry of GLSurfaceView changes, for example size screen or orientation. Add code to respond to those changes. Matthias Keil Android and OpenGL 1. Februar 2016 6 / 24

OpenGL Introduction Versions Two different OpenGL ES API versions available: 1.0 (together with version 1.1 extensions) and 2.0. Both usable to create high performance graphics for 3D games and visualizations. Grapic programming for one of the versions differs significantly to programming for the other version. Version 1.0/1.1 is easier to use as there are more convenience methods available. Version 2.0 provides higher degree of control, enabling creating of effects that are hard to realize in version 1.0/1.1. Matthias Keil Android and OpenGL 1. Februar 2016 7 / 24

Defining Shapes Shapes are graphic objects to be drawn in OpenGL. Shapes are defined using three-dimensional coordinates. Coordinates get written into ByteBuffer that is passed into the graphics pipeline for processing. Coordinate format: [X, Y, Z] Examples: Center of view: [0,0,0], top right corner: [1,1,0], bottom left corner: [-1,-1,0]. Matthias Keil Android and OpenGL 1. Februar 2016 8 / 24

Example: Defining Triangle 1 class Triangle { 2 private FloatBuffer vertexbuffer ;... 3 public Triangle () { 4 // initialize vertex byte buffer for shape coordinates (4 bytes per coordinate ) 5 ByteBuffer bb = ByteBuffer. allocatedirect ( trianglecoords. length * 4); 6 // use the device hardware s native byte order 7 bb. order ( ByteOrder. nativeorder ()); 8 // create a floating point buffer 9 vertexbuffer = bb. asfloatbuffer (); 10 // add the coordinates to the FloatBuffer 11 vertexbuffer. put ( trianglecoords ); 12 // set the buffer to read the first coordinate 13 vertexbuffer. position (0) ; 14 }} Matthias Keil Android and OpenGL 1. Februar 2016 9 / 24

Drawing Shapes Vertex Shader Contains code for rendering the vertices of a shape. Fragment Shader Contains code for rendering the face (visible front) of shape with colors or textures. Program OpenGL ES object containing shaders used. At least one vertex shader and one fragment shader needed to draw a shape. Both shaders must be compiled and then added to the program. Matthias Keil Android and OpenGL 1. Februar 2016 10 / 24

Mapping Coordinates for Drawn Objects Problem: Device screen is no square, but OpenGL assumes that[1]. The picture shows what happens. Left: How it should look. Right: How it looks in horizontal orientation. Solution: Use projection modes and camera views to transform coordinates. Matthias Keil Android and OpenGL 1. Februar 2016 11 / 24

Mapping Coordinates for Drawn Objects Create projection matrix and camera view matrix. Apply both to the OpenGL rendering pipeline. Projection matrix recalculates coordinates of the graphic objects to adjust the screen size. Camera view matrix creates transformation that shows object from specific eye position. Matthias Keil Android and OpenGL 1. Februar 2016 12 / 24

Example in OpenGL ES 1.0: Projection Matrix Create and use projection matrix in onsurfacechanged() of the GLSurfaceView.Renderer implementation. Use geometry of device seen to recalculate coordinates. 1 public void onsurfacechanged ( GL10 gl, int width, int height ) { 2 gl. glviewport (0, 0, width, height ); 3 float ratio = ( float ) width / height ; 4 // set matrix to projection mode 5 gl. glmatrixmode ( GL10. GL_PROJECTION ); 6 // reset the matrix to its default state 7 gl. glloadidentity (); 8 // Define and apply the projection matrix 9 gl. glfrustumf (- ratio, ratio, -1, 1, 3, 7); 10 } Matthias Keil Android and OpenGL 1. Februar 2016 13 / 24

Example in OpenGL ES 1.0: Methods Define a projection matrix in terms of six planes. 1 public static void frustumm ( float [] m, int offset, float left, float right, float bottom, float top, float near, float far ) Matthias Keil Android and OpenGL 1. Februar 2016 14 / 24

Example in OpenGL ES 1.0: Camera Transformation Matrix Apply camera view in ondrawframe() of the GLSurfaceView.Renderer implementation. Use GLU.gluLookAt() to create a transformation simulating the camera position. 1 public void ondrawframe ( GL10 gl) { 2... 3 // Set GL_ MODELVIEW transformation mode 4 gl. glmatrixmode ( GL10. GL_MODELVIEW ); 5 // reset the matrix to its default state 6 gl. glloadidentity (); 7 // When using GL_ MODELVIEW, you must set the camera view 8 GLU. glulookat (gl, 0, 0, -5, 0f, 0f, 0f, 0f, 9... 10 } 1.0f, 0.0 f); Matthias Keil Android and OpenGL 1. Februar 2016 15 / 24

Example in OpenGL ES 1.0: Methods Define a transformation in terms of an eye point, a center of view, and an up vector. 1 glulookat ( GL10 gl, float eyex, float eyey, float eyez, float centerx, float centery, float centerz, float upx, float upy, float upz ) Matthias Keil Android and OpenGL 1. Februar 2016 16 / 24

Example in OpenGL ES 2.0: Steps overview 1 Define a Projection[5]. 2 Define a Camera View. 3 Apply Projection and Camera Transformations on all objects to draw. Step 1 and 2 very similar to OpenGL ES 1.0. Matthias Keil Android and OpenGL 1. Februar 2016 17 / 24

Example in OpenGL ES 2.0: Step 3 Apply Projection and Camera Transformations on all objects to draw. Edit draw method of a shape: 1 public void draw ( float [] mvpmatrix ) {... 2 // get shape s transformation matrix 3 matrix = GLES20. glgetuniformlocation ( mprogram, " umvpmatrix "); 4 // Apply projection and view transformation 5 GLES20. gluniformmatrix4fv ( matrix, 1, false, mvpmatrix, 0); 6 // Draw the shape 7 GLES20. gldrawarrays ( GLES20. GL_TRIANGLES, 0, vertexcount ); 8... 9 } Matthias Keil Android and OpenGL 1. Februar 2016 18 / 24

Adding Motion Rotation can be simply added using OpenGL ES 2.0 Create rotation matrix and combine it with projection and camera view transformation matrices. Extend ondrawframe method. Matthias Keil Android and OpenGL 1. Februar 2016 19 / 24

Adding Motion Example 1 float [] mrotationmatrix = new float [ 16]; 2 // Create a rotation transformation for the triangle 3 long time = SystemClock. uptimemillis () % 4000 L; 4 float angle = 0. 090 f * (( int ) time ); 5 Matrix. setrotatem ( mrotationmatrix, 0, mangle, 0, 0, -1.0f); 6 // Combine the rotation matrix with the projection and camera view 7 Matrix. multiplymm ( mmvpmatrix, 0, mrotationmatrix, 0, mmvpmatrix, 0); 8 // Draw shape 9 mtriangle. draw ( mmvpmatrix ); Matthias Keil Android and OpenGL 1. Februar 2016 20 / 24

Touch Screen Interaction Can be implemented by overriding the method ontouchevent(motionevent) of the class View. MotionEvent gives you various information about where the event happened and how. Example: long MotionEvent.getDownTime() returns the time in ms when user started to press down. Also possible to recover historical/old coordinates of the event[3]. Easy simulation in the emulator possible: Click, hold and move the mouse. Matthias Keil Android and OpenGL 1. Februar 2016 21 / 24

Notes Class Random can produce a random number[6]. Class Sensor is used to access sensors of the cellphone, e.g. the gyroscope[8]. Class MediaPlayer enables playing of sounds[2]. Usage: Put a sound file into folder res/raw/. Supported file formats include ogg vorbis, wav, mp3 and more. 1 MediaPlayer mediaplayer = MediaPlayer. create ( context, R. raw. soundfile ); 2 mediaplayer. start (); Matthias Keil Android and OpenGL 1. Februar 2016 22 / 24

Summary Drawing with OpenGL takes place on GLSurfaceView. GLSurfaceView.Renderer is responsile to draw the shapes. Important to decide which OpenGL ES version to take. Shapes are defined using three-dimensional coordinates. Different shaders needed to draw a shape. Projection matrix is used to adjust graphics to the device screen. Camera transformation matrix is used to simulate a camera position. Rotation motion can be added using an additional matrix. Touch screen interaction can be implemented overriding method ontouchevent. Matthias Keil Android and OpenGL 1. Februar 2016 23 / 24

Bibliography Android Developers. Mapping Coordinates for Drawn Objects. http://developer.android.com/guide/topics/graphics/opengl.html#coordinate-mapping. Android Developers. Media Playback. http://developer.android.com/guide/topics/media/mediaplayer.html. Android Developers. MotionEvent. http://developer.android.com/reference/android/view/motionevent.html. Android Developers. OpenGL. http://developer.android.com/guide/topics/graphics/opengl.html. Android Developers. OpenGL ES 2.0: Applying Projection and Camera Views. http://developer.android.com/training/graphics/opengl/projection.html#projection. Android Developers. Random. http://developer.android.com/reference/java/util/random.html. Android Developers. Tutorial: Displaying Graphics with OpenGL ES. http://developer.android.com/training/graphics/opengl/index.html. Android Developers. Using the Gyroscope. http: Matthias Keil Android and OpenGL 1. Februar 2016 24 / 24