OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ComputeParticles.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.computeparticles;
22 
23 import java.io.File;
24 import java.io.InputStream;
25 import java.io.RandomAccessFile;
26 import android.os.Bundle;
27 import android.app.Activity;
28 import android.content.res.AssetManager;
29 import android.util.Log;
30 import javax.microedition.khronos.egl.EGLConfig;
31 import javax.microedition.khronos.opengles.GL10;
32 import android.content.Context;
33 import android.opengl.GLSurfaceView;
34 import android.os.Bundle;
35 import android.view.Window;
36 import android.view.WindowManager;
37 import android.view.MotionEvent;
38 import android.view.Display;
39 import com.arm.malideveloper.openglessdk.*;
40 
41 class ComputeView extends MaliSamplesView
42 {
43  public ComputeView(Context context)
44  {
45  super(context, GlesVersion.GLES3);
46  }
47 
48  @Override protected void setRendererCallback()
49  {
50  setRenderer(new Renderer());
51  }
52 
53  @Override protected void destroyContextCallback()
54  {
55  ComputeParticles.uninit();
56  }
57 
58  public boolean onTouchEvent(MotionEvent event)
59  {
60  // Resize to fit framebuffer size
61  // float x = 1280.0f * (event.getRawX() / 2560.0f);
62  // float y = 800.0f * (event.getRawY() / 1600.0f);
63  // Since we render into half-resolution buffer
64  float x = event.getRawX() / 2.0f;
65  float y = event.getRawY() / 2.0f;
66 
67  if (event.getAction() == MotionEvent.ACTION_UP)
69  else if (event.getAction() == MotionEvent.ACTION_MOVE)
71  return true;
72  }
73 
74  protected class Renderer implements GLSurfaceView.Renderer
75  {
76  public void onDrawFrame(GL10 gl)
77  {
78  ComputeParticles.step();
79  }
80 
81  public void onSurfaceChanged(GL10 gl, int width, int height)
82  {
83  ComputeParticles.init(width, height);
84  }
85 
86  public void onSurfaceCreated(GL10 gl, EGLConfig config)
87  {
88  }
89  }
90 };
91 
93 {
94  ComputeView mView;
95  private static android.content.Context applicationContext = null;
96  private static String assetsDirectory = null;
97  private static String LOGTAG = "ComputeParticles";
98 
99  public static native void init(int width, int height);
100  public static native void step();
101  public static native void uninit();
102  public static native void onpointerup(float x, float y);
103  public static native void onpointerdown(float x, float y);
104 
105  @Override protected void onCreate(Bundle savedInstanceState)
106  {
107  super.onCreate(savedInstanceState);
108  applicationContext = getApplicationContext();
109  assetsDirectory = applicationContext.getFilesDir().getPath() + "/";
110 
111  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
112  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
113  WindowManager.LayoutParams.FLAG_FULLSCREEN);
114 
115  extractAsset("sphere.vs");
116  extractAsset("sphere.fs");
117 
118  extractAsset("plane.vs");
119  extractAsset("plane.fs");
120 
121  extractAsset("shadowmap.vs");
122  extractAsset("shadowmap.fs");
123 
124  extractAsset("particle.vs");
125  extractAsset("particle.fs");
126 
127  extractAsset("spawn.cs");
128  extractAsset("update.cs");
129 
130  extractAsset("scan.cs");
131  extractAsset("scan_first.cs");
132  extractAsset("scan_resolve.cs");
133  extractAsset("scan_reorder.cs");
134 
135  mView = new ComputeView(getApplication());
136 
137  // Change resolution to half
138  Display display = getWindowManager().getDefaultDisplay();
139  int width = display.getWidth();
140  int height = display.getHeight();
141  mView.getHolder().setFixedSize(width / 2, height / 2);
142  setContentView(mView);
143  }
144 
145  @Override protected void onPause()
146  {
147  super.onPause();
148  mView.onPause();
149  }
150 
151  @Override protected void onResume()
152  {
153  super.onResume();
154  mView.onResume();
155  }
156 
157  @Override protected void onDestroy()
158  {
159  super.onDestroy();
160  }
161 
162  private void extractAsset(String assetName)
163  {
164  File file = new File(assetsDirectory + assetName);
165 
166  if(file.exists()) {
167  Log.d(LOGTAG, assetName + " already exists. No extraction needed.\n");
168  } else {
169  Log.d(LOGTAG, assetName + " doesn't exist. Extraction needed. \n");
170 
171  try {
172  RandomAccessFile randomAccessFile = new RandomAccessFile(assetsDirectory + assetName,"rw");
173  AssetManager assetManager = applicationContext.getResources().getAssets();
174  InputStream inputStream = assetManager.open(assetName);
175 
176  byte buffer[] = new byte[1024];
177  int count = inputStream.read(buffer, 0, 1024);
178 
179  while (count > 0) {
180  randomAccessFile.write(buffer, 0, count);
181 
182  count = inputStream.read(buffer, 0, 1024);
183  }
184 
185  randomAccessFile.close();
186  inputStream.close();
187  } catch(Exception e) {
188  Log.e(LOGTAG, "Failure in extractAssets(): " + e.toString() + " " + assetsDirectory + assetName);
189  }
190 
191  if(file.exists()) {
192  Log.d(LOGTAG,"File extracted successfully");
193  }
194  }
195  }
196 
197  static
198  {
199  // Load the NDK library for this example, built with ndk-build
200  System.loadLibrary("Native");
201  }
202 }
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
GLenum GLenum GLsizei count
Definition: gl2ext.h:133
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
GLenum GLuint buffer
Definition: gl2ext.h:628
GLint GLsizei width
Definition: gl2ext.h:179
GLint y
Definition: gl2ext.h:179