OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Translucency.java
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 package com.arm.malideveloper.openglessdk.translucency;
22 
23 import java.io.File;
24 import java.io.InputStream;
25 import java.io.RandomAccessFile;
26 import android.os.Bundle;
27 import android.os.Build;
28 import android.app.Activity;
29 import android.app.ActivityManager;
30 import android.content.res.AssetManager;
31 import android.util.Log;
32 import android.view.MotionEvent;
33 import android.content.Context;
34 import android.content.pm.ConfigurationInfo;
35 import android.opengl.GLSurfaceView;
36 import android.widget.Toast;
37 import android.content.res.AssetManager;
38 import android.view.Window;
39 import android.view.WindowManager;
40 import android.opengl.GLSurfaceView.EGLContextFactory;
41 import javax.microedition.khronos.egl.EGLContext;
42 import javax.microedition.khronos.egl.EGLDisplay;
43 import javax.microedition.khronos.egl.EGL10;
44 
45 import javax.microedition.khronos.egl.EGLConfig;
46 import javax.microedition.khronos.opengles.GL10;
47 
48 public class Translucency extends Activity
49 {
50  private static android.content.Context applicationContext = null;
51  private static String assetsDirectory = null;
52  private static String LOGTAG = "libNative";
53  private GLSurfaceView view;
54 
55  @Override protected void onCreate(Bundle savedInstanceState)
56  {
57  super.onCreate(savedInstanceState);
58 
59  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
60  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
61  WindowManager.LayoutParams.FLAG_FULLSCREEN);
62 
63  view = new GLSurfaceView(this);
64  view.setEGLContextClientVersion(2);
65  view.setEGLConfigChooser(8, 8, 8, 8, 16, 8); // OBS! Nonzero stencil buffer size!
66  view.setRenderer(new Renderer());
67  view.getHolder().setFixedSize(1280, 720);
68  setContentView(view);
69 
70  applicationContext = getApplicationContext();
71  assetsDirectory = applicationContext.getFilesDir().getPath() + "/";
72 
73  extractAsset("thickness.vs");
74  extractAsset("thickness.fs");
75 
76  extractAsset("resolve.vs");
77  extractAsset("resolve.fs");
78 
79  extractAsset("prepass.vs");
80  extractAsset("prepass.fs");
81 
82  extractAsset("scattering.vs");
83  extractAsset("scattering.fs");
84 
85  extractAsset("opaque.vs");
86  extractAsset("opaque.fs");
87 
88  extractAsset("teapot.bin");
89  }
90 
91  @Override protected void onPause()
92  {
93  super.onPause();
94  view.onPause();
95  NativeLibrary.uninit();
96  }
97 
98  @Override protected void onResume()
99  {
100  super.onResume();
101  view.onResume();
102  }
103 
104  @Override public boolean onTouchEvent(MotionEvent event)
105  {
106  float x = event.getRawX();
107  float y = event.getRawY();
108  if (event.getAction() == MotionEvent.ACTION_UP)
110  else if (event.getAction() == MotionEvent.ACTION_MOVE)
112  return true;
113  }
114 
115  private void extractAsset(String assetName)
116  {
117  File file = new File(assetsDirectory + assetName);
118 
119  if(file.exists()) {
120  Log.d(LOGTAG, assetName + " already exists. No extraction needed.\n");
121  } else {
122  Log.d(LOGTAG, assetName + " doesn't exist. Extraction needed. \n");
123 
124  try {
125  RandomAccessFile randomAccessFile = new RandomAccessFile(assetsDirectory + assetName,"rw");
126  AssetManager assetManager = applicationContext.getResources().getAssets();
127  InputStream inputStream = assetManager.open(assetName);
128 
129  byte buffer[] = new byte[1024];
130  int count = inputStream.read(buffer, 0, 1024);
131 
132  while (count > 0) {
133  randomAccessFile.write(buffer, 0, count);
134 
135  count = inputStream.read(buffer, 0, 1024);
136  }
137 
138  randomAccessFile.close();
139  inputStream.close();
140  } catch(Exception e) {
141  Log.e(LOGTAG, "Failure in extractAssets(): " + e.toString() + " " + assetsDirectory + assetName);
142  }
143 
144  if(file.exists()) {
145  Log.d(LOGTAG,"File extracted successfully");
146  }
147  }
148  }
149 
150  private static class Renderer implements GLSurfaceView.Renderer
151  {
152  public void onDrawFrame(GL10 gl)
153  {
154  NativeLibrary.step();
155  }
156 
157  public void onSurfaceChanged(GL10 gl, int width, int height)
158  {
159  NativeLibrary.init(width, height);
160  }
161 
162  public void onSurfaceCreated(GL10 gl, EGLConfig config)
163  {
164 
165  }
166  }
167 }
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
GLenum GLenum GLsizei count
Definition: gl2ext.h:133
static native void onpointerup(float x, float y)
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
GLenum GLuint buffer
Definition: gl2ext.h:628
static native void onpointerdown(float x, float y)
GLint GLsizei width
Definition: gl2ext.h:179
GLint y
Definition: gl2ext.h:179