git.s-ol.nu ~forks/DiligentTools / ee177d3
Moved Native App from master repository Egor Yusov 1 year, 11 months ago
119 changed file(s) with 12819 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
4141 )
4242
4343 set_target_properties(Diligent-AssetLoader PROPERTIES
44 FOLDER Tools
44 FOLDER DiligentTools
4545 )
55 add_subdirectory(TextureLoader)
66 add_subdirectory(AssetLoader)
77 add_subdirectory(Imgui)
8 add_subdirectory(NativeApp)
89
910 if(${DILIGENT_BUILD_RENDER_SCRIPT})
1011 add_subdirectory(RenderScript)
3232 source_group("source" FILES ${SOURCE})
3333
3434 set_target_properties(HLSL2GLSLConverter PROPERTIES
35 FOLDER Tools
35 FOLDER DiligentTools
3636 )
9898 )
9999
100100 set_target_properties(Diligent-Imgui PROPERTIES
101 FOLDER Tools
101 FOLDER DiligentTools
102102 )
0 /build
1 /.externalNativeBuild
2 *.iml
3 .cxx
0 cmake_minimum_required (VERSION 3.6)
1
2 add_library(native_app_glue ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
3 target_link_libraries(native_app_glue log)
4 target_include_directories(native_app_glue INTERFACE ${ANDROID_NDK}/sources/android/native_app_glue)
5 set_common_target_properties(native_app_glue)
6
7 add_subdirectory(ndk_helper)
0 android {
1 compileSdkVersion=28
2
3 defaultConfig {
4 minSdkVersion 21
5 targetSdkVersion 28
6 versionCode 1
7 versionName "1.0"
8
9 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
10 }
11 }
0 apply plugin: 'com.android.library'
1 apply from: "android_common.gradle"
2
3 android {
4 defaultConfig {
5
6 ndk {
7 abiFilters 'armeabi-v7a'//, 'armeabi', 'arm64-v8a','x86', 'x86_64'
8 }
9 externalNativeBuild {
10 cmake {
11 arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static', '-DENABLE_TESTS=TRUE'
12 }
13 }
14 }
15
16 externalNativeBuild {
17 cmake {
18 version '3.10.2'
19 path '../../../CMakeLists.txt'
20 }
21 }
22
23 sourceSets {
24 main {
25 java.srcDirs = ['src/main/java', 'ndk_helper/src/java']
26 }
27 }
28
29 buildTypes {
30 release {
31 minifyEnabled false
32 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
33 }
34 }
35 }
36
37 buildDir './build'
38
39 dependencies {
40 implementation fileTree(dir: 'libs', include: ['*.jar'])
41 implementation 'com.android.support:appcompat-v7:28.0.0'
42 }
0 cmake_minimum_required (VERSION 3.6)
1
2 project(NDKHelper CXX)
3
4 set(SOURCE
5 src/gestureDetector.cpp
6 src/JNIHelper.cpp
7 src/perfMonitor.cpp
8 src/sensorManager.cpp
9 src/tapCamera.cpp
10 src/vecmath.cpp
11 )
12
13 set(INCLUDE
14 include/gestureDetector.h
15 include/JNIHelper.h
16 include/perfMonitor.h
17 include/sensorManager.h
18 include/tapCamera.h
19 )
20
21 add_library(NDKHelper STATIC ${SOURCE} ${INCLUDE})
22 set_common_target_properties(NDKHelper)
23
24 target_include_directories(NDKHelper
25 PUBLIC
26 include
27 )
28
29 target_link_libraries(NDKHelper
30 PRIVATE
31 Diligent-BuildSettings
32 PUBLIC
33 native_app_glue
34 )
35
36 source_group("src" FILES ${SOURCE})
37 source_group("include" FILES ${INCLUDE})
38
39 set_target_properties(NDKHelper PROPERTIES
40 FOLDER Core/External
41 )
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //--------------------------------------------------------------------------------
17 // GLContext.h
18 //--------------------------------------------------------------------------------
19 #ifndef GLCONTEXT_H_
20 #define GLCONTEXT_H_
21
22 #include <EGL/egl.h>
23 #include <GLES2/gl2.h>
24 #include <android/log.h>
25
26 #include "JNIHelper.h"
27
28 namespace ndk_helper {
29
30 //--------------------------------------------------------------------------------
31 // Constants
32 //--------------------------------------------------------------------------------
33
34 //--------------------------------------------------------------------------------
35 // Class
36 //--------------------------------------------------------------------------------
37
38 /******************************************************************
39 * OpenGL context handler
40 * The class handles OpenGL and EGL context based on Android activity life cycle
41 * The caller needs to call corresponding methods for each activity life cycle
42 *events as it's done in sample codes.
43 *
44 * Also the class initializes OpenGL ES3 when the compatible driver is installed
45 *in the device.
46 * getGLVersion() returns 3.0~ when the device supports OpenGLES3.0
47 *
48 * Thread safety: OpenGL context is expecting used within dedicated single
49 *thread,
50 * thus GLContext class is not designed as a thread-safe
51 */
52 class GLContext {
53 private:
54 // EGL configurations
55 ANativeWindow* window_;
56 EGLDisplay display_;
57 EGLSurface surface_;
58 EGLContext context_;
59 EGLConfig config_;
60
61 // Screen parameters
62 int32_t screen_width_;
63 int32_t screen_height_;
64 int32_t color_size_;
65 int32_t depth_size_;
66
67 // Flags
68 bool gles_initialized_;
69 bool egl_context_initialized_;
70 bool es3_supported_;
71 float gl_version_;
72 bool context_valid_;
73
74 void InitGLES();
75 void Terminate();
76 bool InitEGLSurface();
77 bool InitEGLContext();
78
79 GLContext(GLContext const&);
80 void operator=(GLContext const&);
81 GLContext();
82 virtual ~GLContext();
83
84 public:
85 static GLContext* GetInstance() {
86 // Singleton
87 static GLContext instance;
88
89 return &instance;
90 }
91
92 bool Init(ANativeWindow* window);
93 EGLint Swap();
94 bool Invalidate();
95
96 void Suspend();
97 EGLint Resume(ANativeWindow* window);
98
99 ANativeWindow* GetANativeWindow(void) const { return window_; };
100 int32_t GetScreenWidth() const { return screen_width_; }
101 int32_t GetScreenHeight() const { return screen_height_; }
102
103 int32_t GetBufferColorSize() const { return color_size_; }
104 int32_t GetBufferDepthSize() const { return depth_size_; }
105 float GetGLVersion() const { return gl_version_; }
106 bool CheckExtension(const char* extension);
107
108 EGLDisplay GetDisplay() const { return display_; }
109 EGLSurface GetSurface() const { return surface_; }
110 };
111
112 } // namespace ndkHelper
113
114 #endif /* GLCONTEXT_H_ */
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #pragma once
17
18 #include <jni.h>
19 #include <vector>
20 #include <string>
21 #include <functional>
22 #include <assert.h>
23 #include <mutex>
24 #include <pthread.h>
25
26 #include <android/log.h>
27 #include <android_native_app_glue.h>
28
29 #define LOGI(...) \
30 ((void)__android_log_print( \
31 ANDROID_LOG_INFO, ndk_helper::JNIHelper::GetInstance()->GetAppName(), \
32 __VA_ARGS__))
33 #define LOGW(...) \
34 ((void)__android_log_print( \
35 ANDROID_LOG_WARN, ndk_helper::JNIHelper::GetInstance()->GetAppName(), \
36 __VA_ARGS__))
37 #define LOGE(...) \
38 ((void)__android_log_print( \
39 ANDROID_LOG_ERROR, ndk_helper::JNIHelper::GetInstance()->GetAppName(), \
40 __VA_ARGS__))
41
42 namespace ndk_helper {
43
44 class JUIView;
45
46 /******************************************************************
47 * Helper functions for JNI calls
48 * This class wraps JNI calls and provides handy interface calling commonly used
49 *features
50 * in Java SDK.
51 * Such as
52 * - loading graphics files (e.g. PNG, JPG)
53 * - character code conversion
54 * - retrieving system properties which only supported in Java SDK
55 *
56 * NOTE: To use this class, add NDKHelper.java as a corresponding helpers in
57 *Java code
58 */
59 class JNIHelper {
60 private:
61 std::string app_name_;
62
63 ANativeActivity* activity_;
64 jobject jni_helper_java_ref_;
65 jclass jni_helper_java_class_;
66
67 jstring GetExternalFilesDirJString(JNIEnv* env);
68 jclass RetrieveClass(JNIEnv* jni, const char* class_name);
69
70 JNIHelper();
71 ~JNIHelper();
72 JNIHelper(const JNIHelper& rhs);
73 JNIHelper& operator=(const JNIHelper& rhs);
74
75 std::string app_label_;
76
77 // mutex for synchronization
78 // This class uses singleton pattern and can be invoked from multiple threads,
79 // each methods locks the mutex for a thread safety
80 mutable std::mutex mutex_;
81
82 /*
83 * Call method in JNIHelper class
84 */
85 jobject CallObjectMethod(const char* strMethodName, const char* strSignature,
86 ...);
87 void CallVoidMethod(const char* strMethodName, const char* strSignature, ...);
88
89 /*
90 * Unregister this thread from the VM
91 */
92 static void DetachCurrentThreadDtor(void* p) {
93 LOGI("detached current thread");
94 ANativeActivity* activity = (ANativeActivity*)p;
95 activity->vm->DetachCurrentThread();
96 }
97
98 public:
99 /*
100 * To load your own Java classes, JNIHelper requires to be initialized with a
101 *ANativeActivity handle.
102 * This methods need to be called before any call to the helper class.
103 * Static member of the class
104 *
105 * arguments:
106 * in: activity, pointer to ANativeActivity. Used internally to set up JNI
107 *environment
108 * in: helper_class_name, pointer to Java side helper class name. (e.g.
109 *"com/sample/helper/NDKHelper" in samples )
110 */
111 static void Init(ANativeActivity* activity, const char* helper_class_name);
112
113 /*
114 * Init() that accept so name.
115 * When using a JUI helper class, Java side requires SO name to initialize JNI
116 * calls to invoke native callbacks.
117 * Use this version when using JUI helper.
118 *
119 * arguments:
120 * in: activity, pointer to ANativeActivity. Used internally to set up JNI
121 * environment
122 * in: helper_class_name, pointer to Java side helper class name. (e.g.
123 * "com/sample/helper/NDKHelper" in samples )
124 * in: native_soname, pointer to soname of native library. (e.g.
125 * "NativeActivity" for "libNativeActivity.so" )
126 */
127 static void Init(ANativeActivity* activity, const char* helper_class_name,
128 const char* native_soname);
129
130 /*
131 * Retrieve the singleton object of the helper.
132 * Static member of the class
133
134 * Methods in the class are designed as thread safe.
135 */
136 static JNIHelper* GetInstance();
137
138 /*
139 * Read a file from a strorage.
140 * First, the method tries to read the file from an external storage.
141 * If it fails to read, it falls back to use assset manager and try to read
142 * the file from APK asset.
143 *
144 * arguments:
145 * in: file_name, file name to read
146 * out: buffer_ref, pointer to a vector buffer to read a file.
147 * when the call succeeded, the buffer includes contents of specified
148 *file
149 * when the call failed, contents of the buffer remains same
150 * return:
151 * true when file read succeeded
152 * false when it failed to read the file
153 */
154 bool ReadFile(const char* file_name, std::vector<uint8_t>* buffer_ref);
155
156 /*
157 * Load and create OpenGL texture from given file name.
158 * The method invokes BitmapFactory in Java so it can read jpeg/png formatted
159 * files
160 *
161 * The methods creates mip-map and set texture parameters like this,
162 * glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
163 * GL_LINEAR_MIPMAP_NEAREST );
164 * glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
165 * glGenerateMipmap( GL_TEXTURE_2D );
166 *
167 * arguments:
168 * in: file_name, file name to read, PNG&JPG is supported
169 * outWidth(Optional) pointer to retrieve original bitmap width
170 * outHeight(Optional) pointer to retrieve original bitmap height
171 * return:
172 * OpenGL texture name when the call succeeded
173 * When it failed to load the texture, it returns -1
174 */
175 uint32_t LoadTexture(const char* file_name, int32_t* outWidth = nullptr,
176 int32_t* outHeight = nullptr, bool* hasAlpha = nullptr);
177
178 /*
179 * Load and create OpenGL cubemap texture from given file name
180 * into specified cubemap face & miplevel
181 * The method invokes BitmapFactory in Java so it can read jpeg/png formatted
182 * files
183 *
184 * arguments:
185 * in: file_name, file name to read, PNG&JPG is supported
186 * outWidth(Optional) pointer to retrieve original bitmap width
187 * outHeight(Optional) pointer to retrieve original bitmap height
188 * return:
189 * OpenGL texture name when the call succeeded
190 * When it failed to load the texture, it returns -1
191 */
192 uint32_t LoadCubemapTexture(const char* file_name, const int32_t face,
193 const int32_t miplevel, const bool sRGB,
194 int32_t* outWidth = nullptr,
195 int32_t* outHeight = nullptr,
196 bool* hasAlpha = nullptr);
197
198 /*
199 * Load and create image and return it as a byte array.
200 * The method invokes BitmapFactory in Java so it can read jpeg/png formatted
201 * files
202 *
203 * arguments:
204 * in: file_name, file name to read, PNG&JPG is supported
205 * outWidth(Optional) pointer to retrieve original bitmap width
206 * outHeight(Optional) pointer to retrieve original bitmap height
207 * return:
208 * Java ByteBuffer object containing image data converted to RGBA values.
209 */
210 jobject LoadImage(const char* file_name, int32_t* outWidth = nullptr,
211 int32_t* outHeight = nullptr, bool* hasAlpha = nullptr);
212
213 /*
214 * Convert string from character code other than UTF-8
215 *
216 * arguments:
217 * in: str, pointer to a string which is encoded other than UTF-8
218 * in: encoding, pointer to a character encoding string.
219 * The encoding string can be any valid java.nio.charset.Charset name
220 * e.g. "UTF-16", "Shift_JIS"
221 * return: converted input string as an UTF-8 std::string
222 */
223 std::string ConvertString(const char* str, const char* encode);
224 /*
225 * Retrieve external file directory through JNI call
226 *
227 * return: std::string containing external file diretory
228 */
229 std::string GetExternalFilesDir();
230
231 /*
232 * Retrieve string resource with a given name
233 * arguments:
234 * in: resourceName, name of string resource to retrieve
235 * return: string resource value, returns "" when there is no string resource
236 * with given name
237 */
238 std::string GetStringResource(const std::string& resourceName);
239
240 /*
241 * Audio helper
242 * Retrieves native audio buffer size which is required to achieve low latency
243 * audio
244 *
245 * return: Native audio buffer size which is a hint to achieve low latency
246 * audio
247 * If the API is not supported (API level < 17), it returns 0
248 */
249 int32_t GetNativeAudioBufferSize();
250
251 /*
252 * Audio helper
253 * Retrieves native audio sample rate which is required to achieve low latency
254 * audio
255 *
256 * return: Native audio sample rate which is a hint to achieve low latency
257 * audio
258 */
259 int32_t GetNativeAudioSampleRate();
260
261 /*
262 * Retrieves application bundle name
263 *
264 * return: pointer to an app name string
265 *
266 */
267 const char* GetAppName() const { return app_name_.c_str(); }
268
269 /*
270 * Retrieves application label
271 *
272 * return: pointer to an app label string
273 *
274 */
275 const char* GetAppLabel() const { return app_label_.c_str(); }
276
277 /*
278 * Execute given function in Java UIThread.
279 *
280 * arguments:
281 * in: pFunction, a pointer to a function to be executed in Java UI Thread.
282 * Note that the helper function returns immediately without synchronizing a
283 * function completion.
284 */
285 void RunOnUiThread(std::function<void()> callback);
286
287 /*
288 * Attach current thread
289 * In Android, the thread doesn't have to be 'Detach' current thread
290 * as application process is only killed and VM does not shut down
291 */
292 JNIEnv* AttachCurrentThread() {
293 JNIEnv* env;
294 if (activity_->vm->GetEnv((void**)&env, JNI_VERSION_1_4) == JNI_OK)
295 return env;
296 activity_->vm->AttachCurrentThread(&env, NULL);
297 pthread_key_create((int32_t*)activity_, DetachCurrentThreadDtor);
298 return env;
299 }
300
301 void DetachCurrentThread() {
302 activity_->vm->DetachCurrentThread();
303 return;
304 }
305
306 /*
307 * Decrement a global reference to the object
308 * arguments:
309 * in: obj, obj to decrement a global reference
310 */
311 void DeleteObject(jobject obj);
312
313 /*
314 * Helper methods to call a method in given object
315 */
316 jobject CreateObject(const char* class_name);
317 jobject CallObjectMethod(jobject object, const char* strMethodName,
318 const char* strSignature, ...);
319 void CallVoidMethod(jobject object, const char* strMethodName,
320 const char* strSignature, ...);
321 float CallFloatMethod(jobject object, const char* strMethodName,
322 const char* strSignature, ...);
323 int32_t CallIntMethod(jobject object, const char* strMethodName,
324 const char* strSignature, ...);
325 bool CallBooleanMethod(jobject object, const char* strMethodName,
326 const char* strSignature, ...);
327 };
328
329 } // namespace ndkHelper
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef _NDKSUPPORT_H
17 #define _NDKSUPPORT_H
18
19 #define NDK_HELPER_VERSION "0.90"
20
21 /******************************************************************
22 * NDK support helpers
23 * Utility module to provide misc functionalities that is used widely in native
24 * applications,
25 * such as gesture detection, jni bridge, openGL context etc.
26 *
27 * The purpose of this module is,
28 * - Provide best practices using NDK
29 * - Provide handy utility functions for NDK development
30 * - Make NDK samples more simpler and readable
31 */
32 #include "gl3stub.h" // GLES3 stubs
33 #include "GLContext.h" // EGL & OpenGL manager
34 #include "shader.h" // Shader compiler support
35 #include "vecmath.h" // Vector math support, C++ implementation n current version
36 #include "tapCamera.h" // Tap/Pinch camera control
37 #include "JNIHelper.h" // JNI support
38 #include "gestureDetector.h" // Tap/Doubletap/Pinch detector
39 #include "perfMonitor.h" // FPS counter
40 #include "sensorManager.h" // SensorManager
41 #include "interpolator.h" // Interpolator
42 #endif
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //--------------------------------------------------------------------------------
17 // gestureDetector.h
18 //--------------------------------------------------------------------------------
19 #ifndef GESTUREDETECTOR_H_
20 #define GESTUREDETECTOR_H_
21
22 #include <vector>
23
24 #include <android/sensor.h>
25 #include <android/log.h>
26 #include <android_native_app_glue.h>
27 #include <android/native_window_jni.h>
28 #include "JNIHelper.h"
29 #include "vecmath.h"
30
31 namespace ndk_helper {
32 //--------------------------------------------------------------------------------
33 // Constants
34 //--------------------------------------------------------------------------------
35 const int32_t DOUBLE_TAP_TIMEOUT = 300 * 1000000;
36 const int32_t TAP_TIMEOUT = 180 * 1000000;
37 const int32_t DOUBLE_TAP_SLOP = 100;
38 const int32_t TOUCH_SLOP = 8;
39
40 enum {
41 GESTURE_STATE_NONE = 0,
42 GESTURE_STATE_START = 1,
43 GESTURE_STATE_MOVE = 2,
44 GESTURE_STATE_END = 4,
45 GESTURE_STATE_ACTION = (GESTURE_STATE_START | GESTURE_STATE_END),
46 };
47 typedef int32_t GESTURE_STATE;
48
49 /******************************************************************
50 * Base class of Gesture Detectors
51 * GestureDetectors handles input events and detect gestures
52 * Note that different detectors may detect gestures with an event at
53 * same time. The caller needs to manage gesture priority accordingly
54 *
55 */
56 class GestureDetector {
57 protected:
58 float dp_factor_;
59
60 public:
61 GestureDetector();
62 virtual ~GestureDetector() {}
63 virtual void SetConfiguration(AConfiguration* config);
64
65 virtual GESTURE_STATE Detect(const AInputEvent* motion_event) = 0;
66 };
67
68 /******************************************************************
69 * Tap gesture detector
70 * Returns GESTURE_STATE_ACTION when a tap gesture is detected
71 *
72 */
73 class TapDetector : public GestureDetector {
74 private:
75 int32_t down_pointer_id_;
76 float down_x_;
77 float down_y_;
78
79 public:
80 TapDetector();
81 virtual ~TapDetector() {}
82 virtual GESTURE_STATE Detect(const AInputEvent* motion_event);
83 };
84
85 /******************************************************************
86 * Pinch gesture detector
87 * Returns GESTURE_STATE_ACTION when a double-tap gesture is detected
88 *
89 */
90 class DoubletapDetector : public GestureDetector {
91 private:
92 TapDetector tap_detector_;
93 int64_t last_tap_time_;
94 float last_tap_x_;
95 float last_tap_y_;
96
97 public:
98 DoubletapDetector();
99 virtual ~DoubletapDetector() {}
100 virtual GESTURE_STATE Detect(const AInputEvent* motion_event);
101 virtual void SetConfiguration(AConfiguration* config);
102 };
103
104 /******************************************************************
105 * Double gesture detector
106 * Returns pinch gesture state when a pinch gesture is detected
107 * The class handles multiple touches more than 2
108 * When the finger 1,2,3 are tapped and then finger 1 is released,
109 * the detector start new pinch gesture with finger 2 & 3.
110 */
111 class PinchDetector : public GestureDetector {
112 private:
113 int32_t FindIndex(const AInputEvent* event, int32_t id);
114 const AInputEvent* event_;
115 std::vector<int32_t> vec_pointers_;
116
117 public:
118 PinchDetector() {}
119 virtual ~PinchDetector() {}
120 virtual GESTURE_STATE Detect(const AInputEvent* event);
121 bool GetPointers(Vec2& v1, Vec2& v2);
122 };
123
124 /******************************************************************
125 * Drag gesture detector
126 * Returns drag gesture state when a drag-tap gesture is detected
127 *
128 */
129 class DragDetector : public GestureDetector {
130 private:
131 int32_t FindIndex(const AInputEvent* event, int32_t id);
132 const AInputEvent* event_;
133 std::vector<int32_t> vec_pointers_;
134
135 public:
136 DragDetector() : event_(nullptr) {}
137 virtual ~DragDetector() {}
138 virtual GESTURE_STATE Detect(const AInputEvent* event);
139 bool GetPointer(Vec2& v);
140 };
141
142 } // namespace ndkHelper
143 #endif /* GESTUREDETECTOR_H_ */
0 #ifndef __gl3_h_
1 #define __gl3_h_
2
3 /*
4 * stub gl3.h for dynamic loading, based on:
5 * gl3.h last updated on $Date: 2013-02-12 14:37:24 -0800 (Tue, 12 Feb 2013) $
6 *
7 * Changes:
8 * - Added #include <GLES2/gl2.h>
9 * - Removed duplicate OpenGL ES 2.0 declarations
10 * - Converted OpenGL ES 3.0 function prototypes to function pointer
11 * declarations
12 * - Added gl3stubInit() declaration
13 */
14
15 #include <GLES2/gl2.h>
16 #include <android/api-level.h>
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /*
23 ** Copyright (c) 2007-2013 The Khronos Group Inc.
24 **
25 ** Permission is hereby granted, free of charge, to any person obtaining a
26 ** copy of this software and/or associated documentation files (the
27 ** "Materials"), to deal in the Materials without restriction, including
28 ** without limitation the rights to use, copy, modify, merge, publish,
29 ** distribute, sublicense, and/or sell copies of the Materials, and to
30 ** permit persons to whom the Materials are furnished to do so, subject to
31 ** the following conditions:
32 **
33 ** The above copyright notice and this permission notice shall be included
34 ** in all copies or substantial portions of the Materials.
35 **
36 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
39 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
40 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
41 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
43 */
44
45 /*
46 * This files is for apps that want to use ES3 if present,
47 * but continue to work on pre-API-18 devices. They can't just link to -lGLESv3
48 *since
49 * that library doesn't exist on pre-API-18 devices.
50 * The function dynamically check if OpenGLES3.0 APIs are present and fill in if
51 *there are.
52 * Also the header defines some extra variables for OpenGLES3.0.
53 *
54 */
55
56 /* Call this function before calling any OpenGL ES 3.0 functions. It will
57 * return GL_TRUE if the OpenGL ES 3.0 was successfully initialized, GL_FALSE
58 * otherwise. */
59 GLboolean gl3stubInit();
60
61 /*-------------------------------------------------------------------------
62 * Data type definitions
63 *-----------------------------------------------------------------------*/
64
65 /* OpenGL ES 3.0 */
66
67 typedef unsigned short GLhalf;
68 #if __ANDROID_API__ <= 19
69 typedef khronos_int64_t GLint64;
70 typedef khronos_uint64_t GLuint64;
71 typedef struct __GLsync* GLsync;
72 #endif
73
74 /*-------------------------------------------------------------------------
75 * Token definitions
76 *-----------------------------------------------------------------------*/
77
78 /* OpenGL ES core versions */
79 #define GL_ES_VERSION_3_0 1
80
81 /* OpenGL ES 3.0 */
82
83 #define GL_READ_BUFFER 0x0C02
84 #define GL_UNPACK_ROW_LENGTH 0x0CF2
85 #define GL_UNPACK_SKIP_ROWS 0x0CF3
86 #define GL_UNPACK_SKIP_PIXELS 0x0CF4
87 #define GL_PACK_ROW_LENGTH 0x0D02
88 #define GL_PACK_SKIP_ROWS 0x0D03
89 #define GL_PACK_SKIP_PIXELS 0x0D04
90 #define GL_COLOR 0x1800
91 #define GL_DEPTH 0x1801
92 #define GL_STENCIL 0x1802
93 #define GL_RED 0x1903
94 #define GL_RGB8 0x8051
95 #define GL_RGBA8 0x8058
96 #define GL_RGB10_A2 0x8059
97 #define GL_TEXTURE_BINDING_3D 0x806A
98 #define GL_UNPACK_SKIP_IMAGES 0x806D
99 #define GL_UNPACK_IMAGE_HEIGHT 0x806E
100 #define GL_TEXTURE_3D 0x806F
101 #define GL_TEXTURE_WRAP_R 0x8072
102 #define GL_MAX_3D_TEXTURE_SIZE 0x8073
103 #define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368
104 #define GL_MAX_ELEMENTS_VERTICES 0x80E8
105 #define GL_MAX_ELEMENTS_INDICES 0x80E9
106 #define GL_TEXTURE_MIN_LOD 0x813A
107 #define GL_TEXTURE_MAX_LOD 0x813B
108 #define GL_TEXTURE_BASE_LEVEL 0x813C
109 #define GL_TEXTURE_MAX_LEVEL 0x813D
110 #define GL_MIN 0x8007
111 #define GL_MAX 0x8008
112 #define GL_DEPTH_COMPONENT24 0x81A6
113 #define GL_MAX_TEXTURE_LOD_BIAS 0x84FD
114 #define GL_TEXTURE_COMPARE_MODE 0x884C
115 #define GL_TEXTURE_COMPARE_FUNC 0x884D
116 #define GL_CURRENT_QUERY 0x8865
117 #define GL_QUERY_RESULT 0x8866
118 #define GL_QUERY_RESULT_AVAILABLE 0x8867
119 #define GL_BUFFER_MAPPED 0x88BC
120 #define GL_BUFFER_MAP_POINTER 0x88BD
121 #define GL_STREAM_READ 0x88E1
122 #define GL_STREAM_COPY 0x88E2
123 #define GL_STATIC_READ 0x88E5
124 #define GL_STATIC_COPY 0x88E6
125 #define GL_DYNAMIC_READ 0x88E9
126 #define GL_DYNAMIC_COPY 0x88EA
127 #define GL_MAX_DRAW_BUFFERS 0x8824
128 #define GL_DRAW_BUFFER0 0x8825
129 #define GL_DRAW_BUFFER1 0x8826
130 #define GL_DRAW_BUFFER2 0x8827
131 #define GL_DRAW_BUFFER3 0x8828
132 #define GL_DRAW_BUFFER4 0x8829
133 #define GL_DRAW_BUFFER5 0x882A
134 #define GL_DRAW_BUFFER6 0x882B
135 #define GL_DRAW_BUFFER7 0x882C
136 #define GL_DRAW_BUFFER8 0x882D
137 #define GL_DRAW_BUFFER9 0x882E
138 #define GL_DRAW_BUFFER10 0x882F
139 #define GL_DRAW_BUFFER11 0x8830
140 #define GL_DRAW_BUFFER12 0x8831
141 #define GL_DRAW_BUFFER13 0x8832
142 #define GL_DRAW_BUFFER14 0x8833
143 #define GL_DRAW_BUFFER15 0x8834
144 #define GL_MAX_FRAGMENT_UNIFORM_COMPONENTS 0x8B49
145 #define GL_MAX_VERTEX_UNIFORM_COMPONENTS 0x8B4A
146 #define GL_SAMPLER_3D 0x8B5F
147 #define GL_SAMPLER_2D_SHADOW 0x8B62
148 #define GL_FRAGMENT_SHADER_DERIVATIVE_HINT 0x8B8B
149 #define GL_PIXEL_PACK_BUFFER 0x88EB
150 #define GL_PIXEL_UNPACK_BUFFER 0x88EC
151 #define GL_PIXEL_PACK_BUFFER_BINDING 0x88ED
152 #define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF
153 #define GL_FLOAT_MAT2x3 0x8B65
154 #define GL_FLOAT_MAT2x4 0x8B66
155 #define GL_FLOAT_MAT3x2 0x8B67
156 #define GL_FLOAT_MAT3x4 0x8B68
157 #define GL_FLOAT_MAT4x2 0x8B69
158 #define GL_FLOAT_MAT4x3 0x8B6A
159 #define GL_SRGB 0x8C40
160 #define GL_SRGB8 0x8C41
161 #define GL_SRGB8_ALPHA8 0x8C43
162 #define GL_COMPARE_REF_TO_TEXTURE 0x884E
163 #define GL_MAJOR_VERSION 0x821B
164 #define GL_MINOR_VERSION 0x821C
165 #define GL_NUM_EXTENSIONS 0x821D
166 #define GL_RGBA32F 0x8814
167 #define GL_RGB32F 0x8815
168 #define GL_RGBA16F 0x881A
169 #define GL_RGB16F 0x881B
170 #define GL_VERTEX_ATTRIB_ARRAY_INTEGER 0x88FD
171 #define GL_MAX_ARRAY_TEXTURE_LAYERS 0x88FF
172 #define GL_MIN_PROGRAM_TEXEL_OFFSET 0x8904
173 #define GL_MAX_PROGRAM_TEXEL_OFFSET 0x8905
174 #define GL_MAX_VARYING_COMPONENTS 0x8B4B
175 #define GL_TEXTURE_2D_ARRAY 0x8C1A
176 #define GL_TEXTURE_BINDING_2D_ARRAY 0x8C1D
177 #define GL_R11F_G11F_B10F 0x8C3A
178 #define GL_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
179 #define GL_RGB9_E5 0x8C3D
180 #define GL_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
181 #define GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH 0x8C76
182 #define GL_TRANSFORM_FEEDBACK_BUFFER_MODE 0x8C7F
183 #define GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS 0x8C80
184 #define GL_TRANSFORM_FEEDBACK_VARYINGS 0x8C83
185 #define GL_TRANSFORM_FEEDBACK_BUFFER_START 0x8C84
186 #define GL_TRANSFORM_FEEDBACK_BUFFER_SIZE 0x8C85
187 #define GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN 0x8C88
188 #define GL_RASTERIZER_DISCARD 0x8C89
189 #define GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS 0x8C8A
190 #define GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS 0x8C8B
191 #define GL_INTERLEAVED_ATTRIBS 0x8C8C
192 #define GL_SEPARATE_ATTRIBS 0x8C8D
193 #define GL_TRANSFORM_FEEDBACK_BUFFER 0x8C8E
194 #define GL_TRANSFORM_FEEDBACK_BUFFER_BINDING 0x8C8F
195 #define GL_RGBA32UI 0x8D70
196 #define GL_RGB32UI 0x8D71
197 #define GL_RGBA16UI 0x8D76
198 #define GL_RGB16UI 0x8D77
199 #define GL_RGBA8UI 0x8D7C
200 #define GL_RGB8UI 0x8D7D
201 #define GL_RGBA32I 0x8D82
202 #define GL_RGB32I 0x8D83
203 #define GL_RGBA16I 0x8D88
204 #define GL_RGB16I 0x8D89
205 #define GL_RGBA8I 0x8D8E
206 #define GL_RGB8I 0x8D8F
207 #define GL_RED_INTEGER 0x8D94
208 #define GL_RGB_INTEGER 0x8D98
209 #define GL_RGBA_INTEGER 0x8D99
210 #define GL_SAMPLER_2D_ARRAY 0x8DC1
211 #define GL_SAMPLER_2D_ARRAY_SHADOW 0x8DC4
212 #define GL_SAMPLER_CUBE_SHADOW 0x8DC5
213 #define GL_UNSIGNED_INT_VEC2 0x8DC6
214 #define GL_UNSIGNED_INT_VEC3 0x8DC7
215 #define GL_UNSIGNED_INT_VEC4 0x8DC8
216 #define GL_INT_SAMPLER_2D 0x8DCA
217 #define GL_INT_SAMPLER_3D 0x8DCB
218 #define GL_INT_SAMPLER_CUBE 0x8DCC
219 #define GL_INT_SAMPLER_2D_ARRAY 0x8DCF
220 #define GL_UNSIGNED_INT_SAMPLER_2D 0x8DD2
221 #define GL_UNSIGNED_INT_SAMPLER_3D 0x8DD3
222 #define GL_UNSIGNED_INT_SAMPLER_CUBE 0x8DD4
223 #define GL_UNSIGNED_INT_SAMPLER_2D_ARRAY 0x8DD7
224 #define GL_BUFFER_ACCESS_FLAGS 0x911F
225 #define GL_BUFFER_MAP_LENGTH 0x9120
226 #define GL_BUFFER_MAP_OFFSET 0x9121
227 #define GL_DEPTH_COMPONENT32F 0x8CAC
228 #define GL_DEPTH32F_STENCIL8 0x8CAD
229 #define GL_FLOAT_32_UNSIGNED_INT_24_8_REV 0x8DAD
230 #define GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING 0x8210
231 #define GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE 0x8211
232 #define GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE 0x8212
233 #define GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE 0x8213
234 #define GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE 0x8214
235 #define GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE 0x8215
236 #define GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE 0x8216
237 #define GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE 0x8217
238 #define GL_FRAMEBUFFER_DEFAULT 0x8218
239 #define GL_FRAMEBUFFER_UNDEFINED 0x8219
240 #define GL_DEPTH_STENCIL_ATTACHMENT 0x821A
241 #define GL_DEPTH_STENCIL 0x84F9
242 #define GL_UNSIGNED_INT_24_8 0x84FA
243 #define GL_DEPTH24_STENCIL8 0x88F0
244 #define GL_UNSIGNED_NORMALIZED 0x8C17
245 #define GL_DRAW_FRAMEBUFFER_BINDING GL_FRAMEBUFFER_BINDING
246 #define GL_READ_FRAMEBUFFER 0x8CA8
247 #define GL_DRAW_FRAMEBUFFER 0x8CA9
248 #define GL_READ_FRAMEBUFFER_BINDING 0x8CAA
249 #define GL_RENDERBUFFER_SAMPLES 0x8CAB
250 #define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LAYER 0x8CD4
251 #define GL_MAX_COLOR_ATTACHMENTS 0x8CDF
252 #define GL_COLOR_ATTACHMENT1 0x8CE1
253 #define GL_COLOR_ATTACHMENT2 0x8CE2
254 #define GL_COLOR_ATTACHMENT3 0x8CE3
255 #define GL_COLOR_ATTACHMENT4 0x8CE4
256 #define GL_COLOR_ATTACHMENT5 0x8CE5
257 #define GL_COLOR_ATTACHMENT6 0x8CE6
258 #define GL_COLOR_ATTACHMENT7 0x8CE7
259 #define GL_COLOR_ATTACHMENT8 0x8CE8
260 #define GL_COLOR_ATTACHMENT9 0x8CE9
261 #define GL_COLOR_ATTACHMENT10 0x8CEA
262 #define GL_COLOR_ATTACHMENT11 0x8CEB
263 #define GL_COLOR_ATTACHMENT12 0x8CEC
264 #define GL_COLOR_ATTACHMENT13 0x8CED
265 #define GL_COLOR_ATTACHMENT14 0x8CEE
266 #define GL_COLOR_ATTACHMENT15 0x8CEF
267 #define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE 0x8D56
268 #define GL_MAX_SAMPLES 0x8D57
269 #define GL_HALF_FLOAT 0x140B
270 #define GL_MAP_READ_BIT 0x0001
271 #define GL_MAP_WRITE_BIT 0x0002
272 #define GL_MAP_INVALIDATE_RANGE_BIT 0x0004
273 #define GL_MAP_INVALIDATE_BUFFER_BIT 0x0008
274 #define GL_MAP_FLUSH_EXPLICIT_BIT 0x0010
275 #define GL_MAP_UNSYNCHRONIZED_BIT 0x0020
276 #define GL_RG 0x8227
277 #define GL_RG_INTEGER 0x8228
278 #define GL_R8 0x8229
279 #define GL_RG8 0x822B
280 #define GL_R16F 0x822D
281 #define GL_R32F 0x822E
282 #define GL_RG16F 0x822F
283 #define GL_RG32F 0x8230
284 #define GL_R8I 0x8231
285 #define GL_R8UI 0x8232
286 #define GL_R16I 0x8233
287 #define GL_R16UI 0x8234
288 #define GL_R32I 0x8235
289 #define GL_R32UI 0x8236
290 #define GL_RG8I 0x8237
291 #define GL_RG8UI 0x8238
292 #define GL_RG16I 0x8239
293 #define GL_RG16UI 0x823A
294 #define GL_RG32I 0x823B
295 #define GL_RG32UI 0x823C
296 #define GL_VERTEX_ARRAY_BINDING 0x85B5
297 #define GL_R8_SNORM 0x8F94
298 #define GL_RG8_SNORM 0x8F95
299 #define GL_RGB8_SNORM 0x8F96
300 #define GL_RGBA8_SNORM 0x8F97
301 #define GL_SIGNED_NORMALIZED 0x8F9C
302 #define GL_PRIMITIVE_RESTART_FIXED_INDEX 0x8D69
303 #define GL_COPY_READ_BUFFER 0x8F36
304 #define GL_COPY_WRITE_BUFFER 0x8F37
305 #define GL_COPY_READ_BUFFER_BINDING GL_COPY_READ_BUFFER
306 #define GL_COPY_WRITE_BUFFER_BINDING GL_COPY_WRITE_BUFFER
307 #define GL_UNIFORM_BUFFER 0x8A11
308 #define GL_UNIFORM_BUFFER_BINDING 0x8A28
309 #define GL_UNIFORM_BUFFER_START 0x8A29
310 #define GL_UNIFORM_BUFFER_SIZE 0x8A2A
311 #define GL_MAX_VERTEX_UNIFORM_BLOCKS 0x8A2B
312 #define GL_MAX_FRAGMENT_UNIFORM_BLOCKS 0x8A2D
313 #define GL_MAX_COMBINED_UNIFORM_BLOCKS 0x8A2E
314 #define GL_MAX_UNIFORM_BUFFER_BINDINGS 0x8A2F
315 #define GL_MAX_UNIFORM_BLOCK_SIZE 0x8A30
316 #define GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS 0x8A31
317 #define GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS 0x8A33
318 #define GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT 0x8A34
319 #define GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH 0x8A35
320 #define GL_ACTIVE_UNIFORM_BLOCKS 0x8A36
321 #define GL_UNIFORM_TYPE 0x8A37
322 #define GL_UNIFORM_SIZE 0x8A38
323 #define GL_UNIFORM_NAME_LENGTH 0x8A39
324 #define GL_UNIFORM_BLOCK_INDEX 0x8A3A
325 #define GL_UNIFORM_OFFSET 0x8A3B
326 #define GL_UNIFORM_ARRAY_STRIDE 0x8A3C
327 #define GL_UNIFORM_MATRIX_STRIDE 0x8A3D
328 #define GL_UNIFORM_IS_ROW_MAJOR 0x8A3E
329 #define GL_UNIFORM_BLOCK_BINDING 0x8A3F
330 #define GL_UNIFORM_BLOCK_DATA_SIZE 0x8A40
331 #define GL_UNIFORM_BLOCK_NAME_LENGTH 0x8A41
332 #define GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS 0x8A42
333 #define GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES 0x8A43
334 #define GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER 0x8A44
335 #define GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER 0x8A46
336 #define GL_INVALID_INDEX 0xFFFFFFFFu
337 #define GL_MAX_VERTEX_OUTPUT_COMPONENTS 0x9122
338 #define GL_MAX_FRAGMENT_INPUT_COMPONENTS 0x9125
339 #define GL_MAX_SERVER_WAIT_TIMEOUT 0x9111
340 #define GL_OBJECT_TYPE 0x9112
341 #define GL_SYNC_CONDITION 0x9113
342 #define GL_SYNC_STATUS 0x9114
343 #define GL_SYNC_FLAGS 0x9115
344 #define GL_SYNC_FENCE 0x9116
345 #define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117
346 #define GL_UNSIGNALED 0x9118
347 #define GL_SIGNALED 0x9119
348 #define GL_ALREADY_SIGNALED 0x911A
349 #define GL_TIMEOUT_EXPIRED 0x911B
350 #define GL_CONDITION_SATISFIED 0x911C
351 #define GL_WAIT_FAILED 0x911D
352 #define GL_SYNC_FLUSH_COMMANDS_BIT 0x00000001
353 #define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
354 #define GL_VERTEX_ATTRIB_ARRAY_DIVISOR 0x88FE
355 #define GL_ANY_SAMPLES_PASSED 0x8C2F
356 #define GL_ANY_SAMPLES_PASSED_CONSERVATIVE 0x8D6A
357 #define GL_SAMPLER_BINDING 0x8919
358 #define GL_RGB10_A2UI 0x906F
359 #define GL_TEXTURE_SWIZZLE_R 0x8E42
360 #define GL_TEXTURE_SWIZZLE_G 0x8E43
361 #define GL_TEXTURE_SWIZZLE_B 0x8E44
362 #define GL_TEXTURE_SWIZZLE_A 0x8E45
363 #define GL_GREEN 0x1904
364 #define GL_BLUE 0x1905
365 #define GL_INT_2_10_10_10_REV 0x8D9F
366 #define GL_TRANSFORM_FEEDBACK 0x8E22
367 #define GL_TRANSFORM_FEEDBACK_PAUSED 0x8E23
368 #define GL_TRANSFORM_FEEDBACK_ACTIVE 0x8E24
369 #define GL_TRANSFORM_FEEDBACK_BINDING 0x8E25
370 #define GL_PROGRAM_BINARY_RETRIEVABLE_HINT 0x8257
371 #define GL_PROGRAM_BINARY_LENGTH 0x8741
372 #define GL_NUM_PROGRAM_BINARY_FORMATS 0x87FE
373 #define GL_PROGRAM_BINARY_FORMATS 0x87FF
374 #define GL_COMPRESSED_R11_EAC 0x9270
375 #define GL_COMPRESSED_SIGNED_R11_EAC 0x9271
376 #define GL_COMPRESSED_RG11_EAC 0x9272
377 #define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
378 #define GL_COMPRESSED_RGB8_ETC2 0x9274
379 #define GL_COMPRESSED_SRGB8_ETC2 0x9275
380 #define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
381 #define GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
382 #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
383 #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
384 #define GL_TEXTURE_IMMUTABLE_FORMAT 0x912F
385 #define GL_MAX_ELEMENT_INDEX 0x8D6B
386 #define GL_NUM_SAMPLE_COUNTS 0x9380
387 #define GL_TEXTURE_IMMUTABLE_LEVELS 0x82DF
388
389 /*-------------------------------------------------------------------------
390 * Entrypoint definitions
391 *-----------------------------------------------------------------------*/
392
393 /* OpenGL ES 3.0 */
394
395 extern GL_APICALL void (*GL_APIENTRY glReadBuffer)(GLenum mode);
396 extern GL_APICALL void (*GL_APIENTRY glDrawRangeElements)(
397 GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type,
398 const GLvoid* indices);
399 extern GL_APICALL void (*GL_APIENTRY glTexImage3D)(
400 GLenum target, GLint level, GLint internalformat, GLsizei width,
401 GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type,
402 const GLvoid* pixels);
403 extern GL_APICALL void (*GL_APIENTRY glTexSubImage3D)(
404 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
405 GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type,
406 const GLvoid* pixels);
407 extern GL_APICALL void (*GL_APIENTRY glCopyTexSubImage3D)(
408 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
409 GLint x, GLint y, GLsizei width, GLsizei height);
410 extern GL_APICALL void (*GL_APIENTRY glCompressedTexImage3D)(
411 GLenum target, GLint level, GLenum internalformat, GLsizei width,
412 GLsizei height, GLsizei depth, GLint border, GLsizei imageSize,
413 const GLvoid* data);
414 extern GL_APICALL void (*GL_APIENTRY glCompressedTexSubImage3D)(
415 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
416 GLsizei width, GLsizei height, GLsizei depth, GLenum format,
417 GLsizei imageSize, const GLvoid* data);
418 extern GL_APICALL void (*GL_APIENTRY glGenQueries)(GLsizei n, GLuint* ids);
419 extern GL_APICALL void (*GL_APIENTRY glDeleteQueries)(GLsizei n,
420 const GLuint* ids);
421 extern GL_APICALL GLboolean (*GL_APIENTRY glIsQuery)(GLuint id);
422 extern GL_APICALL void (*GL_APIENTRY glBeginQuery)(GLenum target, GLuint id);
423 extern GL_APICALL void (*GL_APIENTRY glEndQuery)(GLenum target);
424 extern GL_APICALL void (*GL_APIENTRY glGetQueryiv)(GLenum target, GLenum pname,
425 GLint* params);
426 extern GL_APICALL void (*GL_APIENTRY glGetQueryObjectuiv)(GLuint id,
427 GLenum pname,
428 GLuint* params);
429 extern GL_APICALL GLboolean (*GL_APIENTRY glUnmapBuffer)(GLenum target);
430 extern GL_APICALL void (*GL_APIENTRY glGetBufferPointerv)(GLenum target,
431 GLenum pname,
432 GLvoid** params);
433 extern GL_APICALL void (*GL_APIENTRY glDrawBuffers)(GLsizei n,
434 const GLenum* bufs);
435 extern GL_APICALL void (*GL_APIENTRY glUniformMatrix2x3fv)(
436 GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
437 extern GL_APICALL void (*GL_APIENTRY glUniformMatrix3x2fv)(
438 GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
439 extern GL_APICALL void (*GL_APIENTRY glUniformMatrix2x4fv)(
440 GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
441 extern GL_APICALL void (*GL_APIENTRY glUniformMatrix4x2fv)(
442 GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
443 extern GL_APICALL void (*GL_APIENTRY glUniformMatrix3x4fv)(
444 GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
445 extern GL_APICALL void (*GL_APIENTRY glUniformMatrix4x3fv)(
446 GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
447 extern GL_APICALL void (*GL_APIENTRY glBlitFramebuffer)(
448 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0,
449 GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter);
450 extern GL_APICALL void (*GL_APIENTRY glRenderbufferStorageMultisample)(
451 GLenum target, GLsizei samples, GLenum internalformat, GLsizei width,
452 GLsizei height);
453 extern GL_APICALL void (*GL_APIENTRY glFramebufferTextureLayer)(
454 GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer);
455 extern GL_APICALL GLvoid* (*GL_APIENTRY glMapBufferRange)(GLenum target,
456 GLintptr offset,
457 GLsizeiptr length,
458 GLbitfield access);
459 extern GL_APICALL void (*GL_APIENTRY glFlushMappedBufferRange)(
460 GLenum target, GLintptr offset, GLsizeiptr length);
461 extern GL_APICALL void (*GL_APIENTRY glBindVertexArray)(GLuint array);
462 extern GL_APICALL void (*GL_APIENTRY glDeleteVertexArrays)(
463 GLsizei n, const GLuint* arrays);
464 extern GL_APICALL void (*GL_APIENTRY glGenVertexArrays)(GLsizei n,
465 GLuint* arrays);
466 extern GL_APICALL GLboolean (*GL_APIENTRY glIsVertexArray)(GLuint array);
467 extern GL_APICALL void (*GL_APIENTRY glGetIntegeri_v)(GLenum target,
468 GLuint index,
469 GLint* data);
470 extern GL_APICALL void (*GL_APIENTRY glBeginTransformFeedback)(
471 GLenum primitiveMode);
472 extern GL_APICALL void (*GL_APIENTRY glEndTransformFeedback)(void);
473 extern GL_APICALL void (*GL_APIENTRY glBindBufferRange)(GLenum target,
474 GLuint index,
475 GLuint buffer,
476 GLintptr offset,
477 GLsizeiptr size);
478 extern GL_APICALL void (*GL_APIENTRY glBindBufferBase)(GLenum target,
479 GLuint index,
480 GLuint buffer);
481 extern GL_APICALL void (*GL_APIENTRY glTransformFeedbackVaryings)(
482 GLuint program, GLsizei count, const GLchar* const* varyings,
483 GLenum bufferMode);
484 extern GL_APICALL void (*GL_APIENTRY glGetTransformFeedbackVarying)(
485 GLuint program, GLuint index, GLsizei bufSize, GLsizei* length,
486 GLsizei* size, GLenum* type, GLchar* name);
487 extern GL_APICALL void (*GL_APIENTRY glVertexAttribIPointer)(
488 GLuint index, GLint size, GLenum type, GLsizei stride,
489 const GLvoid* pointer);
490 extern GL_APICALL void (*GL_APIENTRY glGetVertexAttribIiv)(GLuint index,
491 GLenum pname,
492 GLint* params);
493 extern GL_APICALL void (*GL_APIENTRY glGetVertexAttribIuiv)(GLuint index,
494 GLenum pname,
495 GLuint* params);
496 extern GL_APICALL void (*GL_APIENTRY glVertexAttribI4i)(GLuint index, GLint x,
497 GLint y, GLint z,
498 GLint w);
499 extern GL_APICALL void (*GL_APIENTRY glVertexAttribI4ui)(GLuint index, GLuint x,
500 GLuint y, GLuint z,
501 GLuint w);
502 extern GL_APICALL void (*GL_APIENTRY glVertexAttribI4iv)(GLuint index,
503 const GLint* v);
504 extern GL_APICALL void (*GL_APIENTRY glVertexAttribI4uiv)(GLuint index,
505 const GLuint* v);
506 extern GL_APICALL void (*GL_APIENTRY glGetUniformuiv)(GLuint program,
507 GLint location,
508 GLuint* params);
509 extern GL_APICALL GLint (*GL_APIENTRY glGetFragDataLocation)(
510 GLuint program, const GLchar* name);
511 extern GL_APICALL void (*GL_APIENTRY glUniform1ui)(GLint location, GLuint v0);
512 extern GL_APICALL void (*GL_APIENTRY glUniform2ui)(GLint location, GLuint v0,
513 GLuint v1);
514 extern GL_APICALL void (*GL_APIENTRY glUniform3ui)(GLint location, GLuint v0,
515 GLuint v1, GLuint v2);
516 extern GL_APICALL void (*GL_APIENTRY glUniform4ui)(GLint location, GLuint v0,
517 GLuint v1, GLuint v2,
518 GLuint v3);
519 extern GL_APICALL void (*GL_APIENTRY glUniform1uiv)(GLint location,
520 GLsizei count,
521 const GLuint* value);
522 extern GL_APICALL void (*GL_APIENTRY glUniform2uiv)(GLint location,
523 GLsizei count,
524 const GLuint* value);
525 extern GL_APICALL void (*GL_APIENTRY glUniform3uiv)(GLint location,
526 GLsizei count,
527 const GLuint* value);
528 extern GL_APICALL void (*GL_APIENTRY glUniform4uiv)(GLint location,
529 GLsizei count,
530 const GLuint* value);
531 extern GL_APICALL void (*GL_APIENTRY glClearBufferiv)(GLenum buffer,
532 GLint drawbuffer,
533 const GLint* value);
534 extern GL_APICALL void (*GL_APIENTRY glClearBufferuiv)(GLenum buffer,
535 GLint drawbuffer,
536 const GLuint* value);
537 extern GL_APICALL void (*GL_APIENTRY glClearBufferfv)(GLenum buffer,
538 GLint drawbuffer,
539 const GLfloat* value);
540 extern GL_APICALL void (*GL_APIENTRY glClearBufferfi)(GLenum buffer,
541 GLint drawbuffer,
542 GLfloat depth,
543 GLint stencil);
544 extern GL_APICALL const GLubyte* (*GL_APIENTRY glGetStringi)(GLenum name,
545 GLuint index);
546 extern GL_APICALL void (*GL_APIENTRY glCopyBufferSubData)(GLenum readTarget,
547 GLenum writeTarget,
548 GLintptr readOffset,
549 GLintptr writeOffset,
550 GLsizeiptr size);
551 extern GL_APICALL void (*GL_APIENTRY glGetUniformIndices)(
552 GLuint program, GLsizei uniformCount, const GLchar* const* uniformNames,
553 GLuint* uniformIndices);
554 extern GL_APICALL void (*GL_APIENTRY glGetActiveUniformsiv)(
555 GLuint program, GLsizei uniformCount, const GLuint* uniformIndices,
556 GLenum pname, GLint* params);
557 extern GL_APICALL GLuint (*GL_APIENTRY glGetUniformBlockIndex)(
558 GLuint program, const GLchar* uniformBlockName);
559 extern GL_APICALL void (*GL_APIENTRY glGetActiveUniformBlockiv)(
560 GLuint program, GLuint uniformBlockIndex, GLenum pname, GLint* params);
561 extern GL_APICALL void (*GL_APIENTRY glGetActiveUniformBlockName)(
562 GLuint program, GLuint uniformBlockIndex, GLsizei bufSize, GLsizei* length,
563 GLchar* uniformBlockName);
564 extern GL_APICALL void (*GL_APIENTRY glUniformBlockBinding)(
565 GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding);
566 extern GL_APICALL void (*GL_APIENTRY glDrawArraysInstanced)(
567 GLenum mode, GLint first, GLsizei count, GLsizei instanceCount);
568 extern GL_APICALL void (*GL_APIENTRY glDrawElementsInstanced)(
569 GLenum mode, GLsizei count, GLenum type, const GLvoid* indices,
570 GLsizei instanceCount);
571 extern GL_APICALL GLsync (*GL_APIENTRY glFenceSync)(GLenum condition,
572 GLbitfield flags);
573 extern GL_APICALL GLboolean (*GL_APIENTRY glIsSync)(GLsync sync);
574 extern GL_APICALL void (*GL_APIENTRY glDeleteSync)(GLsync sync);
575 extern GL_APICALL GLenum (*GL_APIENTRY glClientWaitSync)(GLsync sync,
576 GLbitfield flags,
577 GLuint64 timeout);
578 extern GL_APICALL void (*GL_APIENTRY glWaitSync)(GLsync sync, GLbitfield flags,
579 GLuint64 timeout);
580 extern GL_APICALL void (*GL_APIENTRY glGetInteger64v)(GLenum pname,
581 GLint64* params);
582 extern GL_APICALL void (*GL_APIENTRY glGetSynciv)(GLsync sync, GLenum pname,
583 GLsizei bufSize,
584 GLsizei* length,
585 GLint* values);
586 extern GL_APICALL void (*GL_APIENTRY glGetInteger64i_v)(GLenum target,
587 GLuint index,
588 GLint64* data);
589 extern GL_APICALL void (*GL_APIENTRY glGetBufferParameteri64v)(GLenum target,
590 GLenum pname,
591 GLint64* params);
592 extern GL_APICALL void (*GL_APIENTRY glGenSamplers)(GLsizei count,
593 GLuint* samplers);
594 extern GL_APICALL void (*GL_APIENTRY glDeleteSamplers)(GLsizei count,
595 const GLuint* samplers);
596 extern GL_APICALL GLboolean (*GL_APIENTRY glIsSampler)(GLuint sampler);
597 extern GL_APICALL void (*GL_APIENTRY glBindSampler)(GLuint unit,
598 GLuint sampler);
599 extern GL_APICALL void (*GL_APIENTRY glSamplerParameteri)(GLuint sampler,
600 GLenum pname,
601 GLint param);
602 extern GL_APICALL void (*GL_APIENTRY glSamplerParameteriv)(GLuint sampler,
603 GLenum pname,
604 const GLint* param);
605 extern GL_APICALL void (*GL_APIENTRY glSamplerParameterf)(GLuint sampler,
606 GLenum pname,
607 GLfloat param);
608 extern GL_APICALL void (*GL_APIENTRY glSamplerParameterfv)(
609 GLuint sampler, GLenum pname, const GLfloat* param);
610 extern GL_APICALL void (*GL_APIENTRY glGetSamplerParameteriv)(GLuint sampler,
611 GLenum pname,
612 GLint* params);
613 extern GL_APICALL void (*GL_APIENTRY glGetSamplerParameterfv)(GLuint sampler,
614 GLenum pname,
615 GLfloat* params);
616 extern GL_APICALL void (*GL_APIENTRY glVertexAttribDivisor)(GLuint index,
617 GLuint divisor);
618 extern GL_APICALL void (*GL_APIENTRY glBindTransformFeedback)(GLenum target,
619 GLuint id);
620 extern GL_APICALL void (*GL_APIENTRY glDeleteTransformFeedbacks)(
621 GLsizei n, const GLuint* ids);
622 extern GL_APICALL void (*GL_APIENTRY glGenTransformFeedbacks)(GLsizei n,
623 GLuint* ids);
624 extern GL_APICALL GLboolean (*GL_APIENTRY glIsTransformFeedback)(GLuint id);
625 extern GL_APICALL void (*GL_APIENTRY glPauseTransformFeedback)(void);
626 extern GL_APICALL void (*GL_APIENTRY glResumeTransformFeedback)(void);
627 extern GL_APICALL void (*GL_APIENTRY glGetProgramBinary)(GLuint program,
628 GLsizei bufSize,
629 GLsizei* length,
630 GLenum* binaryFormat,
631 GLvoid* binary);
632 extern GL_APICALL void (*GL_APIENTRY glProgramBinary)(GLuint program,
633 GLenum binaryFormat,
634 const GLvoid* binary,
635 GLsizei length);
636 extern GL_APICALL void (*GL_APIENTRY glProgramParameteri)(GLuint program,
637 GLenum pname,
638 GLint value);
639 extern GL_APICALL void (*GL_APIENTRY glInvalidateFramebuffer)(
640 GLenum target, GLsizei numAttachments, const GLenum* attachments);
641 extern GL_APICALL void (*GL_APIENTRY glInvalidateSubFramebuffer)(
642 GLenum target, GLsizei numAttachments, const GLenum* attachments, GLint x,
643 GLint y, GLsizei width, GLsizei height);
644 extern GL_APICALL void (*GL_APIENTRY glTexStorage2D)(GLenum target,
645 GLsizei levels,
646 GLenum internalformat,
647 GLsizei width,
648 GLsizei height);
649 extern GL_APICALL void (*GL_APIENTRY glTexStorage3D)(
650 GLenum target, GLsizei levels, GLenum internalformat, GLsizei width,
651 GLsizei height, GLsizei depth);
652 extern GL_APICALL void (*GL_APIENTRY glGetInternalformativ)(
653 GLenum target, GLenum internalformat, GLenum pname, GLsizei bufSize,
654 GLint* params);
655
656 #ifdef __cplusplus
657 }
658 #endif
659
660 #endif
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef INTERPOLATOR_H_
17 #define INTERPOLATOR_H_
18
19 #include <jni.h>
20 #include <errno.h>
21 #include <time.h>
22 #include "JNIHelper.h"
23 #include "perfMonitor.h"
24 #include <list>
25
26 namespace ndk_helper {
27
28 enum INTERPOLATOR_TYPE {
29 INTERPOLATOR_TYPE_LINEAR,
30 INTERPOLATOR_TYPE_EASEINQUAD,
31 INTERPOLATOR_TYPE_EASEOUTQUAD,
32 INTERPOLATOR_TYPE_EASEINOUTQUAD,
33 INTERPOLATOR_TYPE_EASEINCUBIC,
34 INTERPOLATOR_TYPE_EASEOUTCUBIC,
35 INTERPOLATOR_TYPE_EASEINOUTCUBIC,
36 INTERPOLATOR_TYPE_EASEINQUART,
37 INTERPOLATOR_TYPE_EASEINEXPO,
38 INTERPOLATOR_TYPE_EASEOUTEXPO,
39 };
40
41 struct InterpolatorParams {
42 float dest_value_;
43 INTERPOLATOR_TYPE type_;
44 double duration_;
45 };
46
47 /******************************************************************
48 * Interpolates values with several interpolation methods
49 */
50 class Interpolator {
51 private:
52 double start_time_;
53 double dest_time_;
54 INTERPOLATOR_TYPE type_;
55
56 float start_value_;
57 float dest_value_;
58 std::list<InterpolatorParams> list_params_;
59
60 float GetFormula(const INTERPOLATOR_TYPE type, const float t, const float b,
61 const float d, const float c);
62
63 public:
64 Interpolator();
65 ~Interpolator();
66
67 Interpolator& Set(const float start, const float dest,
68 const INTERPOLATOR_TYPE type, double duration);
69
70 Interpolator& Add(const float dest, const INTERPOLATOR_TYPE type,
71 const double duration);
72
73 bool Update(const double currentTime, float& p);
74
75 void Clear();
76 };
77
78 } // namespace ndkHelper
79 #endif /* INTERPOLATOR_H_ */
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PERFMONITOR_H_
17 #define PERFMONITOR_H_
18
19 #include <jni.h>
20 #include <errno.h>
21 #include <time.h>
22 #include "JNIHelper.h"
23
24 namespace ndk_helper {
25
26 const int32_t kNumSamples = 100;
27
28 /******************************************************************
29 * Helper class for a performance monitoring and get current tick time
30 */
31 class PerfMonitor {
32 private:
33 float current_FPS_;
34 time_t tv_last_sec_;
35
36 double last_tick_;
37 int32_t tickindex_;
38 double ticksum_;
39 double ticklist_[kNumSamples];
40
41 double UpdateTick(double current_tick);
42
43 public:
44 PerfMonitor();
45 virtual ~PerfMonitor();
46
47 bool Update(float &fFPS);
48
49 static double GetCurrentTime() {
50 struct timeval time;
51 gettimeofday(&time, NULL);
52 double ret = time.tv_sec + time.tv_usec * 1.0 / 1000000.0;
53 return ret;
54 }
55 };
56
57 } // namespace ndkHelper
58 #endif /* PERFMONITOR_H_ */
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //--------------------------------------------------------------------------------
17 // sensorManager.h
18 //--------------------------------------------------------------------------------
19 #ifndef SENSORMANAGER_H_
20 #define SENSORMANAGER_H_
21
22 #include <android/sensor.h>
23 #include "JNIHelper.h"
24
25 namespace ndk_helper {
26 //--------------------------------------------------------------------------------
27 // Constants
28 //--------------------------------------------------------------------------------
29 enum ORIENTATION {
30 ORIENTATION_UNKNOWN = -1,
31 ORIENTATION_PORTRAIT = 0,
32 ORIENTATION_LANDSCAPE = 1,
33 ORIENTATION_REVERSE_PORTRAIT = 2,
34 ORIENTATION_REVERSE_LANDSCAPE = 3,
35 };
36
37 /*
38 * Helper to handle sensor inputs such as accelerometer.
39 * The helper also check for screen rotation
40 *
41 */
42 class SensorManager {
43 ASensorManager *sensorManager_;
44 const ASensor *accelerometerSensor_;
45 ASensorEventQueue *sensorEventQueue_;
46
47 protected:
48 public:
49 SensorManager();
50 ~SensorManager();
51 void Init(android_app *state);
52 void Suspend();
53 void Resume();
54 };
55
56 /*
57 * AcquireASensorManagerInstance(android_app* app)
58 * Workaround ASensorManager_getInstance() deprecation false alarm
59 * for Android-N and before, when compiling with NDK-r15
60 */
61 ASensorManager* AcquireASensorManagerInstance(android_app* app);
62 } // namespace ndkHelper
63 #endif /* SENSORMANAGER_H_ */
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SHADER_H_
17 #define SHADER_H_
18
19 #include <jni.h>
20
21 #include <vector>
22 #include <map>
23 #include <string>
24
25 #include <EGL/egl.h>
26 #include <GLES/gl.h>
27
28 #include <android/log.h>
29
30 #include "JNIHelper.h"
31
32 namespace ndk_helper {
33
34 namespace shader {
35
36 /******************************************************************
37 * Shader compiler helper
38 * namespace: ndkHelper::shader
39 *
40 */
41
42 /******************************************************************
43 * CompileShader() with vector
44 *
45 * arguments:
46 * out: shader, shader variable
47 * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER)
48 * in: data, source vector
49 * return: true if a shader compilation succeeded, false if it failed
50 *
51 */
52 bool CompileShader(GLuint *shader, const GLenum type,
53 std::vector<uint8_t> &data);
54
55 /******************************************************************
56 * CompileShader() with buffer
57 *
58 * arguments:
59 * out: shader, shader variable
60 * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER)
61 * in: source, source buffer
62 * in: iSize, buffer size
63 * return: true if a shader compilation succeeded, false if it failed
64 *
65 */
66 bool CompileShader(GLuint *shader, const GLenum type, const GLchar *source,
67 const int32_t iSize);
68
69 /******************************************************************
70 * CompileShader() with filename
71 *
72 * arguments:
73 * out: shader, shader variable
74 * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER)
75 * in: strFilename, filename
76 * return: true if a shader compilation succeeded, false if it failed
77 *
78 */
79 bool CompileShader(GLuint *shader, const GLenum type, const char *strFileName);
80
81 /******************************************************************
82 * CompileShader() with std::map helps patching on a shader on the fly.
83 *
84 * arguments:
85 * out: shader, shader variable
86 * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER)
87 * in: mapParameters
88 * For a example,
89 * map : %KEY% -> %VALUE% replaces all %KEY% entries in the given shader
90 *code to %VALUE"
91 * return: true if a shader compilation succeeded, false if it failed
92 *
93 */
94 bool CompileShader(GLuint *shader, const GLenum type, const char *str_file_name,
95 const std::map<std::string, std::string> &map_parameters);
96
97 /******************************************************************
98 * LinkProgram()
99 *
100 * arguments:
101 * in: program, program
102 * return: true if a shader linkage succeeded, false if it failed
103 *
104 */
105 bool LinkProgram(const GLuint prog);
106
107 /******************************************************************
108 * validateProgram()
109 *
110 * arguments:
111 * in: program, program
112 * return: true if a shader validation succeeded, false if it failed
113 *
114 */
115 bool ValidateProgram(const GLuint prog);
116 } // namespace shader
117
118 } // namespace ndkHelper
119 #endif /* SHADER_H_ */
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #pragma once
17 #include <vector>
18 #include <string>
19 #include <GLES2/gl2.h>
20
21 #include "JNIHelper.h"
22 #include "vecmath.h"
23 #include "interpolator.h"
24
25 namespace ndk_helper {
26
27 /******************************************************************
28 * Camera control helper class with a tap gesture
29 * This class is mainly used for 3D space camera control in samples.
30 *
31 */
32 class TapCamera {
33 private:
34 // Trackball
35 Vec2 vec_ball_center_;
36 float ball_radius_;
37 Quaternion quat_ball_now_;
38 Quaternion quat_ball_down_;
39 Vec2 vec_ball_now_;
40 Vec2 vec_ball_down_;
41 Quaternion quat_ball_rot_;
42
43 bool dragging_;
44 bool pinching_;
45
46 // Pinch related info
47 Vec2 vec_pinch_start_;
48 Vec2 vec_pinch_start_center_;
49 float pinch_start_distance_SQ_;
50
51 // Camera shift
52 Vec3 vec_offset_;
53 Vec3 vec_offset_now_;
54
55 // Camera Rotation
56 float camera_rotation_;
57 float camera_rotation_start_;
58 float camera_rotation_now_;
59
60 // Momentum support
61 bool momentum_;
62 double time_stamp_;
63 Vec2 vec_drag_delta_;
64 Vec2 vec_last_input_;
65 Vec3 vec_offset_last_;
66 Vec3 vec_offset_delta_;
67 float momemtum_steps_;
68
69 Vec2 vec_flip_;
70 float flip_z_;
71
72 Mat4 mat_rotation_;
73 Mat4 mat_transform_;
74
75 Vec3 vec_pinch_transform_factor_;
76
77 Vec3 PointOnSphere(Vec2& point);
78 void BallUpdate();
79 void InitParameters();
80
81 public:
82 TapCamera();
83 virtual ~TapCamera();
84 void BeginDrag(const Vec2& vec);
85 void EndDrag();
86 void Drag(const Vec2& vec);
87 void Update();
88 void Update(const double time);
89
90 Mat4& GetRotationMatrix();
91 Mat4& GetTransformMatrix();
92
93 void BeginPinch(const Vec2& v1, const Vec2& v2);
94 void EndPinch();
95 void Pinch(const Vec2& v1, const Vec2& v2);
96
97 void SetFlip(const float x, const float y, const float z) {
98 vec_flip_ = Vec2(x, y);
99 flip_z_ = z;
100 }
101
102 void SetPinchTransformFactor(const float x, const float y, const float z) {
103 vec_pinch_transform_factor_ = Vec3(x, y, z);
104 }
105
106 void Reset(const bool bAnimate);
107 };
108
109 } // namespace ndkHelper
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef VECMATH_H_
17 #define VECMATH_H_
18
19 #include <cmath>
20 #include "JNIHelper.h"
21
22 namespace ndk_helper {
23
24 /******************************************************************
25 * Helper class for vector math operations
26 * Currently all implementations are in pure C++.
27 * Each class is an opaque class so caller does not have a direct access
28 * to each element. This is for an ease of future optimization to use vector
29 *operations.
30 *
31 */
32
33 class Vec2;
34 class Vec3;
35 class Vec4;
36 class Mat4;
37
38 /******************************************************************
39 * 2 elements vector class
40 *
41 */
42 class Vec2 {
43 private:
44 float x_;
45 float y_;
46
47 public:
48 friend class Vec3;
49 friend class Vec4;
50 friend class Mat4;
51 friend class Quaternion;
52
53 Vec2() { x_ = y_ = 0.f; }
54
55 Vec2(const float fX, const float fY) {
56 x_ = fX;
57 y_ = fY;
58 }
59
60 Vec2(const Vec2& vec) {
61 x_ = vec.x_;
62 y_ = vec.y_;
63 }
64
65 Vec2(const float* pVec) {
66 x_ = (*pVec++);
67 y_ = (*pVec++);
68 }
69
70 // Operators
71 Vec2 operator*(const Vec2& rhs) const {
72 Vec2 ret;
73 ret.x_ = x_ * rhs.x_;
74 ret.y_ = y_ * rhs.y_;
75 return ret;
76 }
77
78 Vec2 operator/(const Vec2& rhs) const {
79 Vec2 ret;
80 ret.x_ = x_ / rhs.x_;
81 ret.y_ = y_ / rhs.y_;
82 return ret;
83 }
84
85 Vec2 operator+(const Vec2& rhs) const {
86 Vec2 ret;
87 ret.x_ = x_ + rhs.x_;
88 ret.y_ = y_ + rhs.y_;
89 return ret;
90 }
91
92 Vec2 operator-(const Vec2& rhs) const {
93 Vec2 ret;
94 ret.x_ = x_ - rhs.x_;
95 ret.y_ = y_ - rhs.y_;
96 return ret;
97 }
98
99 Vec2& operator+=(const Vec2& rhs) {
100 x_ += rhs.x_;
101 y_ += rhs.y_;
102 return *this;
103 }
104
105 Vec2& operator-=(const Vec2& rhs) {
106 x_ -= rhs.x_;
107 y_ -= rhs.y_;
108 return *this;
109 }
110
111 Vec2& operator*=(const Vec2& rhs) {
112 x_ *= rhs.x_;
113 y_ *= rhs.y_;
114 return *this;
115 }
116
117 Vec2& operator/=(const Vec2& rhs) {
118 x_ /= rhs.x_;
119 y_ /= rhs.y_;
120 return *this;
121 }
122
123 // External operators
124 friend Vec2 operator-(const Vec2& rhs) { return Vec2(rhs) *= -1; }
125
126 friend Vec2 operator*(const float lhs, const Vec2& rhs) {
127 Vec2 ret;
128 ret.x_ = lhs * rhs.x_;
129 ret.y_ = lhs * rhs.y_;
130 return ret;
131 }
132
133 friend Vec2 operator/(const float lhs, const Vec2& rhs) {
134 Vec2 ret;
135 ret.x_ = lhs / rhs.x_;
136 ret.y_ = lhs / rhs.y_;
137 return ret;
138 }
139
140 // Operators with float
141 Vec2 operator*(const float& rhs) const {
142 Vec2 ret;
143 ret.x_ = x_ * rhs;
144 ret.y_ = y_ * rhs;
145 return ret;
146 }
147
148 Vec2& operator*=(const float& rhs) {
149 x_ = x_ * rhs;
150 y_ = y_ * rhs;
151 return *this;
152 }
153
154 Vec2 operator/(const float& rhs) const {
155 Vec2 ret;
156 ret.x_ = x_ / rhs;
157 ret.y_ = y_ / rhs;
158 return ret;
159 }
160
161 Vec2& operator/=(const float& rhs) {
162 x_ = x_ / rhs;
163 y_ = y_ / rhs;
164 return *this;
165 }
166
167 // Compare
168 bool operator==(const Vec2& rhs) const {
169 if (x_ != rhs.x_ || y_ != rhs.y_) return false;
170 return true;
171 }
172
173 bool operator!=(const Vec2& rhs) const {
174 if (x_ == rhs.x_) return false;
175
176 return true;
177 }
178
179 float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
180
181 Vec2 Normalize() {
182 float len = Length();
183 x_ = x_ / len;
184 y_ = y_ / len;
185 return *this;
186 }
187
188 float Dot(const Vec2& rhs) { return x_ * rhs.x_ + y_ * rhs.y_; }
189
190 bool Validate() {
191 if (std::isnan(x_) || std::isnan(y_)) return false;
192 return true;
193 }
194
195 void Value(float& fX, float& fY) {
196 fX = x_;
197 fY = y_;
198 }
199
200 void Dump() { LOGI("Vec2 %f %f", x_, y_); }
201 };
202
203 /******************************************************************
204 * 3 elements vector class
205 *
206 */
207 class Vec3 {
208 private:
209 float x_, y_, z_;
210
211 public:
212 friend class Vec4;
213 friend class Mat4;
214 friend class Quaternion;
215
216 Vec3() { x_ = y_ = z_ = 0.f; }
217
218 Vec3(const float fX, const float fY, const float fZ) {
219 x_ = fX;
220 y_ = fY;
221 z_ = fZ;
222 }
223
224 Vec3(const Vec3& vec) {
225 x_ = vec.x_;
226 y_ = vec.y_;
227 z_ = vec.z_;
228 }
229
230 Vec3(const float* pVec) {
231 x_ = (*pVec++);
232 y_ = (*pVec++);
233 z_ = *pVec;
234 }
235
236 Vec3(const Vec2& vec, float f) {
237 x_ = vec.x_;
238 y_ = vec.y_;
239 z_ = f;
240 }
241
242 Vec3(const Vec4& vec);
243
244 // Operators
245 Vec3 operator*(const Vec3& rhs) const {
246 Vec3 ret;
247 ret.x_ = x_ * rhs.x_;
248 ret.y_ = y_ * rhs.y_;
249 ret.z_ = z_ * rhs.z_;
250 return ret;
251 }
252
253 Vec3 operator/(const Vec3& rhs) const {
254 Vec3 ret;
255 ret.x_ = x_ / rhs.x_;
256 ret.y_ = y_ / rhs.y_;
257 ret.z_ = z_ / rhs.z_;
258 return ret;
259 }
260
261 Vec3 operator+(const Vec3& rhs) const {
262 Vec3 ret;
263 ret.x_ = x_ + rhs.x_;
264 ret.y_ = y_ + rhs.y_;
265 ret.z_ = z_ + rhs.z_;
266 return ret;
267 }
268
269 Vec3 operator-(const Vec3& rhs) const {
270 Vec3 ret;
271 ret.x_ = x_ - rhs.x_;
272 ret.y_ = y_ - rhs.y_;
273 ret.z_ = z_ - rhs.z_;
274 return ret;
275 }
276
277 Vec3& operator+=(const Vec3& rhs) {
278 x_ += rhs.x_;
279 y_ += rhs.y_;
280 z_ += rhs.z_;
281 return *this;
282 }
283
284 Vec3& operator-=(const Vec3& rhs) {
285 x_ -= rhs.x_;
286 y_ -= rhs.y_;
287 z_ -= rhs.z_;
288 return *this;
289 }
290
291 Vec3& operator*=(const Vec3& rhs) {
292 x_ *= rhs.x_;
293 y_ *= rhs.y_;
294 z_ *= rhs.z_;
295 return *this;
296 }
297
298 Vec3& operator/=(const Vec3& rhs) {
299 x_ /= rhs.x_;
300 y_ /= rhs.y_;
301 z_ /= rhs.z_;
302 return *this;
303 }
304
305 // External operators
306 friend Vec3 operator-(const Vec3& rhs) { return Vec3(rhs) *= -1; }
307
308 friend Vec3 operator*(const float lhs, const Vec3& rhs) {
309 Vec3 ret;
310 ret.x_ = lhs * rhs.x_;
311 ret.y_ = lhs * rhs.y_;
312 ret.z_ = lhs * rhs.z_;
313 return ret;
314 }
315
316 friend Vec3 operator/(const float lhs, const Vec3& rhs) {
317 Vec3 ret;
318 ret.x_ = lhs / rhs.x_;
319 ret.y_ = lhs / rhs.y_;
320 ret.z_ = lhs / rhs.z_;
321 return ret;
322 }
323
324 // Operators with float
325 Vec3 operator*(const float& rhs) const {
326 Vec3 ret;
327 ret.x_ = x_ * rhs;
328 ret.y_ = y_ * rhs;
329 ret.z_ = z_ * rhs;
330 return ret;
331 }
332
333 Vec3& operator*=(const float& rhs) {
334 x_ = x_ * rhs;
335 y_ = y_ * rhs;
336 z_ = z_ * rhs;
337 return *this;
338 }
339
340 Vec3 operator/(const float& rhs) const {
341 Vec3 ret;
342 ret.x_ = x_ / rhs;
343 ret.y_ = y_ / rhs;
344 ret.z_ = z_ / rhs;
345 return ret;
346 }
347
348 Vec3& operator/=(const float& rhs) {
349 x_ = x_ / rhs;
350 y_ = y_ / rhs;
351 z_ = z_ / rhs;
352 return *this;
353 }
354
355 // Compare
356 bool operator==(const Vec3& rhs) const {
357 if (x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_) return false;
358 return true;
359 }
360
361 bool operator!=(const Vec3& rhs) const {
362 if (x_ == rhs.x_) return false;
363
364 return true;
365 }
366
367 float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
368
369 Vec3 Normalize() {
370 float len = Length();
371 x_ = x_ / len;
372 y_ = y_ / len;
373 z_ = z_ / len;
374 return *this;
375 }
376
377 float Dot(const Vec3& rhs) { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
378
379 Vec3 Cross(const Vec3& rhs) {
380 Vec3 ret;
381 ret.x_ = y_ * rhs.z_ - z_ * rhs.y_;
382 ret.y_ = z_ * rhs.x_ - x_ * rhs.z_;
383 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_;
384 return ret;
385 }
386
387 bool Validate() {
388 if (std::isnan(x_) || std::isnan(y_) || std::isnan(z_)) return false;
389 return true;
390 }
391
392 void Value(float& fX, float& fY, float& fZ) {
393 fX = x_;
394 fY = y_;
395 fZ = z_;
396 }
397
398 void Dump() { LOGI("Vec3 %f %f %f", x_, y_, z_); }
399 };
400
401 /******************************************************************
402 * 4 elements vector class
403 *
404 */
405 class Vec4 {
406 private:
407 float x_, y_, z_, w_;
408
409 public:
410 friend class Vec3;
411 friend class Mat4;
412 friend class Quaternion;
413
414 Vec4() { x_ = y_ = z_ = w_ = 0.f; }
415
416 Vec4(const float fX, const float fY, const float fZ, const float fW) {
417 x_ = fX;
418 y_ = fY;
419 z_ = fZ;
420 w_ = fW;
421 }
422
423 Vec4(const Vec4& vec) {
424 x_ = vec.x_;
425 y_ = vec.y_;
426 z_ = vec.z_;
427 w_ = vec.w_;
428 }
429
430 Vec4(const Vec3& vec, const float fW) {
431 x_ = vec.x_;
432 y_ = vec.y_;
433 z_ = vec.z_;
434 w_ = fW;
435 }
436
437 Vec4(const float* pVec) {
438 x_ = (*pVec++);
439 y_ = (*pVec++);
440 z_ = *pVec;
441 w_ = *pVec;
442 }
443
444 // Operators
445 Vec4 operator*(const Vec4& rhs) const {
446 Vec4 ret;
447 ret.x_ = x_ * rhs.x_;
448 ret.y_ = y_ * rhs.y_;
449 ret.z_ = z_ * rhs.z_;
450 ret.w_ = z_ * rhs.w_;
451 return ret;
452 }
453
454 Vec4 operator/(const Vec4& rhs) const {
455 Vec4 ret;
456 ret.x_ = x_ / rhs.x_;
457 ret.y_ = y_ / rhs.y_;
458 ret.z_ = z_ / rhs.z_;
459 ret.w_ = z_ / rhs.w_;
460 return ret;
461 }
462
463 Vec4 operator+(const Vec4& rhs) const {
464 Vec4 ret;
465 ret.x_ = x_ + rhs.x_;
466 ret.y_ = y_ + rhs.y_;
467 ret.z_ = z_ + rhs.z_;
468 ret.w_ = z_ + rhs.w_;
469 return ret;
470 }
471
472 Vec4 operator-(const Vec4& rhs) const {
473 Vec4 ret;
474 ret.x_ = x_ - rhs.x_;
475 ret.y_ = y_ - rhs.y_;
476 ret.z_ = z_ - rhs.z_;
477 ret.w_ = z_ - rhs.w_;
478 return ret;
479 }
480
481 Vec4& operator+=(const Vec4& rhs) {
482 x_ += rhs.x_;
483 y_ += rhs.y_;
484 z_ += rhs.z_;
485 w_ += rhs.w_;
486 return *this;
487 }
488
489 Vec4& operator-=(const Vec4& rhs) {
490 x_ -= rhs.x_;
491 y_ -= rhs.y_;
492 z_ -= rhs.z_;
493 w_ -= rhs.w_;
494 return *this;
495 }
496
497 Vec4& operator*=(const Vec4& rhs) {
498 x_ *= rhs.x_;
499 y_ *= rhs.y_;
500 z_ *= rhs.z_;
501 w_ *= rhs.w_;
502 return *this;
503 }
504
505 Vec4& operator/=(const Vec4& rhs) {
506 x_ /= rhs.x_;
507 y_ /= rhs.y_;
508 z_ /= rhs.z_;
509 w_ /= rhs.w_;
510 return *this;
511 }
512
513 // External operators
514 friend Vec4 operator-(const Vec4& rhs) { return Vec4(rhs) *= -1; }
515
516 friend Vec4 operator*(const float lhs, const Vec4& rhs) {
517 Vec4 ret;
518 ret.x_ = lhs * rhs.x_;
519 ret.y_ = lhs * rhs.y_;
520 ret.z_ = lhs * rhs.z_;
521 ret.w_ = lhs * rhs.w_;
522 return ret;
523 }
524
525 friend Vec4 operator/(const float lhs, const Vec4& rhs) {
526 Vec4 ret;
527 ret.x_ = lhs / rhs.x_;
528 ret.y_ = lhs / rhs.y_;
529 ret.z_ = lhs / rhs.z_;
530 ret.w_ = lhs / rhs.w_;
531 return ret;
532 }
533
534 // Operators with float
535 Vec4 operator*(const float& rhs) const {
536 Vec4 ret;
537 ret.x_ = x_ * rhs;
538 ret.y_ = y_ * rhs;
539 ret.z_ = z_ * rhs;
540 ret.w_ = w_ * rhs;
541 return ret;
542 }
543
544 Vec4& operator*=(const float& rhs) {
545 x_ = x_ * rhs;
546 y_ = y_ * rhs;
547 z_ = z_ * rhs;
548 w_ = w_ * rhs;
549 return *this;
550 }
551
552 Vec4 operator/(const float& rhs) const {
553 Vec4 ret;
554 ret.x_ = x_ / rhs;
555 ret.y_ = y_ / rhs;
556 ret.z_ = z_ / rhs;
557 ret.w_ = w_ / rhs;
558 return ret;
559 }
560
561 Vec4& operator/=(const float& rhs) {
562 x_ = x_ / rhs;
563 y_ = y_ / rhs;
564 z_ = z_ / rhs;
565 w_ = w_ / rhs;
566 return *this;
567 }
568
569 // Compare
570 bool operator==(const Vec4& rhs) const {
571 if (x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_)
572 return false;
573 return true;
574 }
575
576 bool operator!=(const Vec4& rhs) const {
577 if (x_ == rhs.x_) return false;
578
579 return true;
580 }
581
582 Vec4 operator*(const Mat4& rhs) const;
583
584 float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_); }
585
586 Vec4 Normalize() {
587 float len = Length();
588 x_ = x_ / len;
589 y_ = y_ / len;
590 z_ = z_ / len;
591 w_ = w_ / len;
592 return *this;
593 }
594
595 float Dot(const Vec3& rhs) { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
596
597 Vec3 Cross(const Vec3& rhs) {
598 Vec3 ret;
599 ret.x_ = y_ * rhs.z_ - z_ * rhs.y_;
600 ret.y_ = z_ * rhs.x_ - x_ * rhs.z_;
601 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_;
602 return ret;
603 }
604
605 bool Validate() {
606 if (std::isnan(x_) || std::isnan(y_) ||
607 std::isnan(z_) || std::isnan(w_))
608 return false;
609
610 return true;
611 }
612
613 void Value(float& fX, float& fY, float& fZ, float& fW) {
614 fX = x_;
615 fY = y_;
616 fZ = z_;
617 fW = w_;
618 }
619 };
620
621 /******************************************************************
622 * 4x4 matrix
623 *
624 */
625 class Mat4 {
626 private:
627 float f_[16];
628
629 public:
630 friend class Vec3;
631 friend class Vec4;
632 friend class Quaternion;
633
634 Mat4();
635 Mat4(const float*);
636
637 Mat4 operator*(const Mat4& rhs) const;
638 Vec4 operator*(const Vec4& rhs) const;
639
640 Mat4 operator+(const Mat4& rhs) const {
641 Mat4 ret;
642 for (int32_t i = 0; i < 16; ++i) {
643 ret.f_[i] = f_[i] + rhs.f_[i];
644 }
645 return ret;
646 }
647
648 Mat4 operator-(const Mat4& rhs) const {
649 Mat4 ret;
650 for (int32_t i = 0; i < 16; ++i) {
651 ret.f_[i] = f_[i] - rhs.f_[i];
652 }
653 return ret;
654 }
655
656 Mat4& operator+=(const Mat4& rhs) {
657 for (int32_t i = 0; i < 16; ++i) {
658 f_[i] += rhs.f_[i];
659 }
660 return *this;
661 }
662
663 Mat4& operator-=(const Mat4& rhs) {
664 for (int32_t i = 0; i < 16; ++i) {
665 f_[i] -= rhs.f_[i];
666 }
667 return *this;
668 }
669
670 Mat4& operator*=(const Mat4& rhs) {
671 Mat4 ret;
672 ret.f_[0] = f_[0] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[8] * rhs.f_[2] +
673 f_[12] * rhs.f_[3];
674 ret.f_[1] = f_[1] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[9] * rhs.f_[2] +
675 f_[13] * rhs.f_[3];
676 ret.f_[2] = f_[2] * rhs.f_[0] + f_[6] * rhs.f_[1] + f_[10] * rhs.f_[2] +
677 f_[14] * rhs.f_[3];
678 ret.f_[3] = f_[3] * rhs.f_[0] + f_[7] * rhs.f_[1] + f_[11] * rhs.f_[2] +
679 f_[15] * rhs.f_[3];
680
681 ret.f_[4] = f_[0] * rhs.f_[4] + f_[4] * rhs.f_[5] + f_[8] * rhs.f_[6] +
682 f_[12] * rhs.f_[7];
683 ret.f_[5] = f_[1] * rhs.f_[4] + f_[5] * rhs.f_[5] + f_[9] * rhs.f_[6] +
684 f_[13] * rhs.f_[7];
685 ret.f_[6] = f_[2] * rhs.f_[4] + f_[6] * rhs.f_[5] + f_[10] * rhs.f_[6] +
686 f_[14] * rhs.f_[7];
687 ret.f_[7] = f_[3] * rhs.f_[4] + f_[7] * rhs.f_[5] + f_[11] * rhs.f_[6] +
688 f_[15] * rhs.f_[7];
689
690 ret.f_[8] = f_[0] * rhs.f_[8] + f_[4] * rhs.f_[9] + f_[8] * rhs.f_[10] +
691 f_[12] * rhs.f_[11];
692 ret.f_[9] = f_[1] * rhs.f_[8] + f_[5] * rhs.f_[9] + f_[9] * rhs.f_[10] +
693 f_[13] * rhs.f_[11];
694 ret.f_[10] = f_[2] * rhs.f_[8] + f_[6] * rhs.f_[9] + f_[10] * rhs.f_[10] +
695 f_[14] * rhs.f_[11];
696 ret.f_[11] = f_[3] * rhs.f_[8] + f_[7] * rhs.f_[9] + f_[11] * rhs.f_[10] +
697 f_[15] * rhs.f_[11];
698
699 ret.f_[12] = f_[0] * rhs.f_[12] + f_[4] * rhs.f_[13] + f_[8] * rhs.f_[14] +
700 f_[12] * rhs.f_[15];
701 ret.f_[13] = f_[1] * rhs.f_[12] + f_[5] * rhs.f_[13] + f_[9] * rhs.f_[14] +
702 f_[13] * rhs.f_[15];
703 ret.f_[14] = f_[2] * rhs.f_[12] + f_[6] * rhs.f_[13] + f_[10] * rhs.f_[14] +
704 f_[14] * rhs.f_[15];
705 ret.f_[15] = f_[3] * rhs.f_[12] + f_[7] * rhs.f_[13] + f_[11] * rhs.f_[14] +
706 f_[15] * rhs.f_[15];
707
708 *this = ret;
709 return *this;
710 }
711
712 Mat4 operator*(const float rhs) {
713 Mat4 ret;
714 for (int32_t i = 0; i < 16; ++i) {
715 ret.f_[i] = f_[i] * rhs;
716 }
717 return ret;
718 }
719
720 Mat4& operator*=(const float rhs) {
721 for (int32_t i = 0; i < 16; ++i) {
722 f_[i] *= rhs;
723 }
724 return *this;
725 }
726
727 Mat4& operator=(const Mat4& rhs) {
728 for (int32_t i = 0; i < 16; ++i) {
729 f_[i] = rhs.f_[i];
730 }
731 return *this;
732 }
733
734 Mat4 Inverse();
735
736 Mat4 Transpose() {
737 Mat4 ret;
738 ret.f_[0] = f_[0];
739 ret.f_[1] = f_[4];
740 ret.f_[2] = f_[8];
741 ret.f_[3] = f_[12];
742 ret.f_[4] = f_[1];
743 ret.f_[5] = f_[5];
744 ret.f_[6] = f_[9];
745 ret.f_[7] = f_[13];
746 ret.f_[8] = f_[2];
747 ret.f_[9] = f_[6];
748 ret.f_[10] = f_[10];
749 ret.f_[11] = f_[14];
750 ret.f_[12] = f_[3];
751 ret.f_[13] = f_[7];
752 ret.f_[14] = f_[11];
753 ret.f_[15] = f_[15];
754 *this = ret;
755 return *this;
756 }
757
758 Mat4& PostTranslate(float tx, float ty, float tz) {
759 f_[12] += (tx * f_[0]) + (ty * f_[4]) + (tz * f_[8]);
760 f_[13] += (tx * f_[1]) + (ty * f_[5]) + (tz * f_[9]);
761 f_[14] += (tx * f_[2]) + (ty * f_[6]) + (tz * f_[10]);
762 f_[15] += (tx * f_[3]) + (ty * f_[7]) + (tz * f_[11]);
763 return *this;
764 }
765
766 float* Ptr() { return f_; }
767
768 //--------------------------------------------------------------------------------
769 // Misc
770 //--------------------------------------------------------------------------------
771 static Mat4 Perspective(float width, float height, float nearPlane,
772 float farPlane);
773 static Mat4 Ortho2D(float left, float top, float right, float bottom);
774
775 static Mat4 LookAt(const Vec3& vEye, const Vec3& vAt, const Vec3& vUp);
776
777 static Mat4 Translation(const float fX, const float fY, const float fZ);
778 static Mat4 Translation(const Vec3 vec);
779
780 static Mat4 RotationX(const float angle);
781
782 static Mat4 RotationY(const float angle);
783
784 static Mat4 RotationZ(const float angle);
785
786 static Mat4 Scale(const float scaleX, const float scaleY, const float scaleZ);
787
788 static Mat4 Identity() {
789 Mat4 ret;
790 ret.f_[0] = 1.f;
791 ret.f_[1] = 0;
792 ret.f_[2] = 0;
793 ret.f_[3] = 0;
794 ret.f_[4] = 0;
795 ret.f_[5] = 1.f;
796 ret.f_[6] = 0;
797 ret.f_[7] = 0;
798 ret.f_[8] = 0;
799 ret.f_[9] = 0;
800 ret.f_[10] = 1.f;
801 ret.f_[11] = 0;
802 ret.f_[12] = 0;
803 ret.f_[13] = 0;
804 ret.f_[14] = 0;
805 ret.f_[15] = 1.f;
806 return ret;
807 }
808
809 void Dump() {
810 LOGI("%f %f %f %f", f_[0], f_[1], f_[2], f_[3]);
811 LOGI("%f %f %f %f", f_[4], f_[5], f_[6], f_[7]);
812 LOGI("%f %f %f %f", f_[8], f_[9], f_[10], f_[11]);
813 LOGI("%f %f %f %f", f_[12], f_[13], f_[14], f_[15]);
814 }
815 };
816
817 /******************************************************************
818 * Quaternion class
819 *
820 */
821 class Quaternion {
822 private:
823 float x_, y_, z_, w_;
824
825 public:
826 friend class Vec3;
827 friend class Vec4;
828 friend class Mat4;
829
830 Quaternion() {
831 x_ = 0.f;
832 y_ = 0.f;
833 z_ = 0.f;
834 w_ = 1.f;
835 }
836
837 Quaternion(const float fX, const float fY, const float fZ, const float fW) {
838 x_ = fX;
839 y_ = fY;
840 z_ = fZ;
841 w_ = fW;
842 }
843
844 Quaternion(const Vec3 vec, const float fW) {
845 x_ = vec.x_;
846 y_ = vec.y_;
847 z_ = vec.z_;
848 w_ = fW;
849 }
850
851 Quaternion(const float* p) {
852 x_ = *p++;
853 y_ = *p++;
854 z_ = *p++;
855 w_ = *p++;
856 }
857
858 Quaternion operator*(const Quaternion rhs) {
859 Quaternion ret;
860 ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_;
861 ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_;
862 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_;
863 ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_;
864 return ret;
865 }
866
867 Quaternion& operator*=(const Quaternion rhs) {
868 Quaternion ret;
869 ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_;
870 ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_;
871 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_;
872 ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_;
873 *this = ret;
874 return *this;
875 }
876
877 Quaternion Conjugate() {
878 x_ = -x_;
879 y_ = -y_;
880 z_ = -z_;
881 return *this;
882 }
883
884 // Non destuctive version
885 Quaternion Conjugated() {
886 Quaternion ret;
887 ret.x_ = -x_;
888 ret.y_ = -y_;
889 ret.z_ = -z_;
890 ret.w_ = w_;
891 return ret;
892 }
893
894 void ToMatrix(Mat4& mat) {
895 float x2 = x_ * x_ * 2.0f;
896 float y2 = y_ * y_ * 2.0f;
897 float z2 = z_ * z_ * 2.0f;
898 float xy = x_ * y_ * 2.0f;
899 float yz = y_ * z_ * 2.0f;
900 float zx = z_ * x_ * 2.0f;
901 float xw = x_ * w_ * 2.0f;
902 float yw = y_ * w_ * 2.0f;
903 float zw = z_ * w_ * 2.0f;
904
905 mat.f_[0] = 1.0f - y2 - z2;
906 mat.f_[1] = xy + zw;
907 mat.f_[2] = zx - yw;
908 mat.f_[4] = xy - zw;
909 mat.f_[5] = 1.0f - z2 - x2;
910 mat.f_[6] = yz + xw;
911 mat.f_[8] = zx + yw;
912 mat.f_[9] = yz - xw;
913 mat.f_[10] = 1.0f - x2 - y2;
914
915 mat.f_[3] = mat.f_[7] = mat.f_[11] = mat.f_[12] = mat.f_[13] = mat.f_[14] =
916 0.0f;
917 mat.f_[15] = 1.0f;
918 }
919
920 void ToMatrixPreserveTranslate(Mat4& mat) {
921 float x2 = x_ * x_ * 2.0f;
922 float y2 = y_ * y_ * 2.0f;
923 float z2 = z_ * z_ * 2.0f;
924 float xy = x_ * y_ * 2.0f;
925 float yz = y_ * z_ * 2.0f;
926 float zx = z_ * x_ * 2.0f;
927 float xw = x_ * w_ * 2.0f;
928 float yw = y_ * w_ * 2.0f;
929 float zw = z_ * w_ * 2.0f;
930
931 mat.f_[0] = 1.0f - y2 - z2;
932 mat.f_[1] = xy + zw;
933 mat.f_[2] = zx - yw;
934 mat.f_[4] = xy - zw;
935 mat.f_[5] = 1.0f - z2 - x2;
936 mat.f_[6] = yz + xw;
937 mat.f_[8] = zx + yw;
938 mat.f_[9] = yz - xw;
939 mat.f_[10] = 1.0f - x2 - y2;
940
941 mat.f_[3] = mat.f_[7] = mat.f_[11] = 0.0f;
942 mat.f_[15] = 1.0f;
943 }
944
945 static Quaternion RotationAxis(const Vec3 axis, const float angle) {
946 Quaternion ret;
947 float s = sinf(angle / 2);
948 ret.x_ = s * axis.x_;
949 ret.y_ = s * axis.y_;
950 ret.z_ = s * axis.z_;
951 ret.w_ = cosf(angle / 2);
952 return ret;
953 }
954
955 void Value(float& fX, float& fY, float& fZ, float& fW) {
956 fX = x_;
957 fY = y_;
958 fZ = z_;
959 fW = w_;
960 }
961 };
962
963 } // namespace ndk_helper
964 #endif /* VECMATH_H_ */
0 /*
1 * Copyright 2013 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //--------------------------------------------------------------------------------
17 // GLContext.cpp
18 //--------------------------------------------------------------------------------
19 //--------------------------------------------------------------------------------
20 // includes
21 //--------------------------------------------------------------------------------
22 #include "GLContext.h"
23
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "gl3stub.h"
28
29 namespace ndk_helper {
30
31 //--------------------------------------------------------------------------------
32 // eGLContext
33 //--------------------------------------------------------------------------------
34
35 //--------------------------------------------------------------------------------
36 // Ctor
37 //--------------------------------------------------------------------------------
38 GLContext::GLContext()
39 : window_(nullptr),
40 display_(EGL_NO_DISPLAY),
41 surface_(EGL_NO_SURFACE),
42 context_(EGL_NO_CONTEXT),
43 screen_width_(0),
44 screen_height_(0),
45 gles_initialized_(false),
46 egl_context_initialized_(false),
47 es3_supported_(false) {}
48
49 void GLContext::InitGLES() {
50 if (gles_initialized_) return;
51 //
52 // Initialize OpenGL ES 3 if available
53 //
54 const char* versionStr = (const char*)glGetString(GL_VERSION);
55 if (strstr(versionStr, "OpenGL ES 3.") && gl3stubInit()) {
56 es3_supported_ = true;
57 gl_version_ = 3.0f;
58 } else {
59 gl_version_ = 2.0f;
60 }
61
62 gles_initialized_ = true;
63 }
64
65 //--------------------------------------------------------------------------------
66 // Dtor
67 //--------------------------------------------------------------------------------
68 GLContext::~GLContext() { Terminate(); }
69
70 bool GLContext::Init(ANativeWindow* window) {
71 if (egl_context_initialized_) return true;
72
73 //
74 // Initialize EGL
75 //
76 window_ = window;
77 InitEGLSurface();
78 InitEGLContext();
79 InitGLES();
80
81 egl_context_initialized_ = true;
82
83 return true;
84 }
85
86 bool GLContext::InitEGLSurface() {
87 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY);
88 eglInitialize(display_, 0, 0);
89
90 /*
91 * Here specify the attributes of the desired configuration.
92 * Below, we select an EGLConfig with at least 8 bits per color
93 * component compatible with on-screen windows
94 */
95 const EGLint attribs[] = {EGL_RENDERABLE_TYPE,
96 EGL_OPENGL_ES2_BIT, // Request opengl ES2.0
97 EGL_SURFACE_TYPE,
98 EGL_WINDOW_BIT,
99 EGL_BLUE_SIZE,
100 8,
101 EGL_GREEN_SIZE,
102 8,
103 EGL_RED_SIZE,
104 8,
105 EGL_DEPTH_SIZE,
106 24,
107 EGL_NONE};
108 color_size_ = 8;
109 depth_size_ = 24;
110
111 EGLint num_configs;
112 eglChooseConfig(display_, attribs, &config_, 1, &num_configs);
113
114 if (!num_configs) {
115 // Fall back to 16bit depth buffer
116 const EGLint attribs[] = {EGL_RENDERABLE_TYPE,
117 EGL_OPENGL_ES2_BIT, // Request opengl ES2.0
118 EGL_SURFACE_TYPE,
119 EGL_WINDOW_BIT,
120 EGL_BLUE_SIZE,
121 8,
122 EGL_GREEN_SIZE,
123 8,
124 EGL_RED_SIZE,
125 8,
126 EGL_DEPTH_SIZE,
127 16,
128 EGL_NONE};
129 eglChooseConfig(display_, attribs, &config_, 1, &num_configs);
130 depth_size_ = 16;
131 }
132
133 if (!num_configs) {
134 LOGW("Unable to retrieve EGL config");
135 return false;
136 }
137
138 surface_ = eglCreateWindowSurface(display_, config_, window_, NULL);
139 eglQuerySurface(display_, surface_, EGL_WIDTH, &screen_width_);
140 eglQuerySurface(display_, surface_, EGL_HEIGHT, &screen_height_);
141
142 return true;
143 }
144
145 bool GLContext::InitEGLContext() {
146 const EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION,
147 2, // Request opengl ES2.0
148 EGL_NONE};
149 context_ = eglCreateContext(display_, config_, NULL, context_attribs);
150
151 if (eglMakeCurrent(display_, surface_, surface_, context_) == EGL_FALSE) {
152 LOGW("Unable to eglMakeCurrent");
153 return false;
154 }
155
156 context_valid_ = true;
157 return true;
158 }
159
160 EGLint GLContext::Swap() {
161 bool b = eglSwapBuffers(display_, surface_);
162 if (!b) {
163 EGLint err = eglGetError();
164 if (err == EGL_BAD_SURFACE) {
165 // Recreate surface
166 InitEGLSurface();
167 return EGL_SUCCESS; // Still consider glContext is valid
168 } else if (err == EGL_CONTEXT_LOST || err == EGL_BAD_CONTEXT) {
169 // Context has been lost!!
170 context_valid_ = false;
171 Terminate();
172 InitEGLContext();
173 }
174 return err;
175 }
176 return EGL_SUCCESS;
177 }
178
179 void GLContext::Terminate() {
180 if (display_ != EGL_NO_DISPLAY) {
181 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
182 if (context_ != EGL_NO_CONTEXT) {
183 eglDestroyContext(display_, context_);
184 }
185
186 if (surface_ != EGL_NO_SURFACE) {
187 eglDestroySurface(display_, surface_);
188 }
189 eglTerminate(display_);
190 }
191
192 display_ = EGL_NO_DISPLAY;
193 context_ = EGL_NO_CONTEXT;
194 surface_ = EGL_NO_SURFACE;
195 window_ = nullptr;
196 context_valid_ = false;
197 }
198
199 EGLint GLContext::Resume(ANativeWindow* window) {
200 if (egl_context_initialized_ == false) {
201 Init(window);
202 return EGL_SUCCESS;
203 }
204
205 int32_t original_widhth = screen_width_;
206 int32_t original_height = screen_height_;
207
208 // Create surface
209 window_ = window;
210 surface_ = eglCreateWindowSurface(display_, config_, window_, NULL);
211 eglQuerySurface(display_, surface_, EGL_WIDTH, &screen_width_);
212 eglQuerySurface(display_, surface_, EGL_HEIGHT, &screen_height_);
213
214 if (screen_width_ != original_widhth || screen_height_ != original_height) {
215 // Screen resized
216 LOGI("Screen resized");
217 }
218
219 if (eglMakeCurrent(display_, surface_, surface_, context_) == EGL_TRUE)
220 return EGL_SUCCESS;
221
222 EGLint err = eglGetError();
223 LOGW("Unable to eglMakeCurrent %d", err);
224
225 if (err == EGL_CONTEXT_LOST) {
226 // Recreate context
227 LOGI("Re-creating egl context");
228 InitEGLContext();
229 } else {
230 // Recreate surface
231 Terminate();
232 InitEGLSurface();
233 InitEGLContext();
234 }
235
236 return err;
237 }
238
239 void GLContext::Suspend() {
240 if (surface_ != EGL_NO_SURFACE) {
241 eglDestroySurface(display_, surface_);
242 surface_ = EGL_NO_SURFACE;
243 }
244 }
245
246 bool GLContext::Invalidate() {
247 Terminate();
248
249 egl_context_initialized_ = false;
250 return true;
251 }
252
253 bool GLContext::CheckExtension(const char* extension) {
254 if (extension == NULL) return false;
255
256 std::string extensions = std::string((char*)glGetString(GL_EXTENSIONS));
257 std::string str = std::string(extension);
258 str.append(" ");
259
260 size_t pos = 0;
261 if (extensions.find(extension, pos) != std::string::npos) {
262 return true;
263 }
264
265 return false;