cleaned code, added pinch gesture detector, better opengl context handling, fixed issues
Change-Id: I26a28374ae74391204586b4584d03cf0c58772c2
This commit is contained in:
committed by
Andrew Hsieh
parent
122d194b36
commit
5d1c91fb75
@@ -21,7 +21,7 @@ void main()
|
||||
{
|
||||
#if USE_PHONG
|
||||
mediump vec3 halfVector = normalize(-vLight0 + position);
|
||||
mediump float NdotH = max(dot(normal, halfVector), 0.0);
|
||||
mediump float NdotH = max(dot(normalize(normal), halfVector), 0.0);
|
||||
mediump float fPower = vMaterialSpecular.w;
|
||||
mediump float specular = pow(NdotH, fPower);
|
||||
|
||||
|
||||
@@ -5,21 +5,16 @@ include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := NativeActivity
|
||||
LOCAL_SRC_FILES := TeapotNativeActivity.cpp \
|
||||
TeapotRenderer.cpp \
|
||||
NDKSupport/JNIHelper.cpp \
|
||||
NDKSupport/shader.cpp \
|
||||
NDKSupport/vecmath.cpp \
|
||||
NDKSupport/gestureDetector.cpp \
|
||||
NDKSupport/perfMonitor.cpp \
|
||||
NDKSupport/tapCamera.cpp \
|
||||
|
||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)/NDKSupport
|
||||
LOCAL_C_INCLUDES :=
|
||||
|
||||
LOCAL_CFLAGS :=
|
||||
|
||||
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2
|
||||
LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue
|
||||
LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue helper
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
$(call import-module,android/helper)
|
||||
$(call import-module,android/native_app_glue)
|
||||
$(call import-module,android/cpufeatures)
|
||||
$(call import-module,android/cpufeatures)
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "JNIHelper.h"
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
//JNI Helper functions
|
||||
//---------------------------------------------------------------------------
|
||||
//Static variable
|
||||
ANativeActivity* JNIHelper::_activity;
|
||||
jobject JNIHelper::_objJNIHelper;
|
||||
jclass JNIHelper::_clsJNIHelper;
|
||||
|
||||
jclass retrieveClass(JNIEnv *jni, ANativeActivity* activity,
|
||||
const char* className) {
|
||||
jclass activityClass = jni->FindClass(CLASS_NAME);
|
||||
jmethodID getClassLoader = jni->GetMethodID(activityClass, "getClassLoader",
|
||||
"()Ljava/lang/ClassLoader;");
|
||||
jobject cls = jni->CallObjectMethod(activity->clazz, getClassLoader);
|
||||
jclass classLoader = jni->FindClass("java/lang/ClassLoader");
|
||||
jmethodID findClass = jni->GetMethodID(classLoader, "loadClass",
|
||||
"(Ljava/lang/String;)Ljava/lang/Class;");
|
||||
|
||||
jstring strClassName = jni->NewStringUTF(className);
|
||||
jclass classRetrieved = (jclass) jni->CallObjectMethod(cls, findClass,
|
||||
strClassName);
|
||||
jni->DeleteLocalRef(strClassName);
|
||||
return classRetrieved;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
//readFile
|
||||
//---------------------------------------------------------------------------
|
||||
bool JNIHelper::readFile(const char* fileName, std::vector<uint8_t>& buffer) {
|
||||
if (_activity == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//First, try reading from externalFileDir;
|
||||
JNIEnv *env;
|
||||
jmethodID mid;
|
||||
|
||||
_activity->vm->AttachCurrentThread(&env, NULL);
|
||||
|
||||
jstring strPath = getExternalFilesDir(env);
|
||||
const char* path = env->GetStringUTFChars(strPath, NULL);
|
||||
std::string s(path);
|
||||
|
||||
if (fileName[0] != '/') {
|
||||
s.append("/");
|
||||
}
|
||||
s.append(fileName);
|
||||
std::ifstream f(s.c_str(), std::ios::binary);
|
||||
|
||||
env->ReleaseStringUTFChars(strPath, path);
|
||||
_activity->vm->DetachCurrentThread();
|
||||
|
||||
if (f) {
|
||||
LOGI("reading:%s", s.c_str());
|
||||
f.seekg(0, std::ifstream::end);
|
||||
int32_t fileSize = f.tellg();
|
||||
f.seekg(0, std::ifstream::beg);
|
||||
buffer.reserve(fileSize);
|
||||
buffer.assign(std::istreambuf_iterator<char>(f),
|
||||
std::istreambuf_iterator<char>());
|
||||
return true;
|
||||
} else {
|
||||
//Fallback to assetManager
|
||||
AAssetManager* assetManager = _activity->assetManager;
|
||||
AAsset* assetFile = AAssetManager_open(assetManager, fileName,
|
||||
AASSET_MODE_BUFFER);
|
||||
if (!assetFile) {
|
||||
return false;
|
||||
}
|
||||
uint8_t* data = (uint8_t*) AAsset_getBuffer(assetFile);
|
||||
int32_t iSize = AAsset_getLength(assetFile);
|
||||
if (data == NULL) {
|
||||
AAsset_close(assetFile);
|
||||
|
||||
LOGI("Failed to load:%s", fileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer.assign(data, data + iSize);
|
||||
|
||||
AAsset_close(assetFile);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
jstring JNIHelper::getExternalFilesDir(JNIEnv *env) {
|
||||
if (_activity == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// getExternalFilesDir() - java
|
||||
jclass cls_Env = env->FindClass("android/app/NativeActivity");
|
||||
jmethodID mid = env->GetMethodID(cls_Env, "getExternalFilesDir",
|
||||
"(Ljava/lang/String;)Ljava/io/File;");
|
||||
jobject obj_File = env->CallObjectMethod(_activity->clazz, mid, NULL);
|
||||
jclass cls_File = env->FindClass("java/io/File");
|
||||
jmethodID mid_getPath = env->GetMethodID(cls_File, "getPath",
|
||||
"()Ljava/lang/String;");
|
||||
jstring obj_Path = (jstring) env->CallObjectMethod(obj_File, mid_getPath);
|
||||
return obj_Path;
|
||||
}
|
||||
|
||||
uint32_t JNIHelper::loadTexture(const char* fileName) {
|
||||
if (_activity == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
JNIEnv *env;
|
||||
jmethodID mid;
|
||||
|
||||
_activity->vm->AttachCurrentThread(&env, NULL);
|
||||
|
||||
if (_clsJNIHelper == 0) {
|
||||
jclass cls = retrieveClass(env, _activity, APPLICATION_CLASS_NAME);
|
||||
_clsJNIHelper = (jclass) env->NewGlobalRef(cls);
|
||||
|
||||
jmethodID constructor = env->GetMethodID(_clsJNIHelper, "<init>",
|
||||
"()V");
|
||||
_objJNIHelper = env->NewObject(_clsJNIHelper, constructor);
|
||||
_objJNIHelper = env->NewGlobalRef(_objJNIHelper);
|
||||
}
|
||||
|
||||
/* Ask the PNG manager for a bitmap */
|
||||
mid = env->GetMethodID(_clsJNIHelper, "openBitmap",
|
||||
"(Ljava/lang/String;Z)Landroid/graphics/Bitmap;");
|
||||
|
||||
jstring name = env->NewStringUTF(fileName);
|
||||
#if 0
|
||||
jobject png = env->CallObjectMethod(_objJNIHelper, mid, name, true);
|
||||
env->DeleteLocalRef(name);
|
||||
env->NewGlobalRef(png);
|
||||
|
||||
/* Get image dimensions */
|
||||
mid = env->GetMethodID(_clsJNIHelper, "getBitmapWidth", "(Landroid/graphics/Bitmap;)I");
|
||||
int width = env->CallIntMethod(_objJNIHelper, mid, png);
|
||||
mid = env->GetMethodID(_clsJNIHelper, "getBitmapHeight", "(Landroid/graphics/Bitmap;)I");
|
||||
int height = env->CallIntMethod(_objJNIHelper, mid, png);
|
||||
|
||||
/* Get pixels */
|
||||
jintArray array = env->NewIntArray(width * height);
|
||||
env->NewGlobalRef(array);
|
||||
mid = env->GetMethodID(_clsJNIHelper, "getBitmapPixels", "(Landroid/graphics/Bitmap;[I)V");
|
||||
env->CallVoidMethod(_objJNIHelper, mid, png, array);
|
||||
|
||||
jint *pixels = env->GetIntArrayElements(array, 0);
|
||||
|
||||
LOGI( "Loaded bitmap %s, width %d height %d",fileName, width, height);
|
||||
|
||||
GLuint tex;
|
||||
glGenTextures( 1, &tex );
|
||||
glBindTexture( GL_TEXTURE_2D, tex );
|
||||
|
||||
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_NEAREST );
|
||||
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
//Generate mipmap
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
env->ReleaseIntArrayElements(array, pixels, 0);
|
||||
env->DeleteGlobalRef(array);
|
||||
|
||||
/* Free image */
|
||||
mid = env->GetMethodID(_clsJNIHelper, "closeBitmap", "(Landroid/graphics/Bitmap;)V");
|
||||
env->CallVoidMethod(_objJNIHelper, mid, png);
|
||||
env->DeleteGlobalRef(png);
|
||||
#else
|
||||
GLuint tex;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_NEAREST);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
mid = env->GetMethodID(_clsJNIHelper, "loadTexture",
|
||||
"(Ljava/lang/String;)V");
|
||||
env->CallVoidMethod(_objJNIHelper, mid, name);
|
||||
|
||||
//Generate mipmap
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
#endif
|
||||
|
||||
_activity->vm->DetachCurrentThread();
|
||||
|
||||
return tex;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
#include <errno.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <GLES2/gl2.h>
|
||||
|
||||
#include <android/sensor.h>
|
||||
#include <android/log.h>
|
||||
#include <android_native_app_glue.h>
|
||||
|
||||
|
||||
#define APP_NAME "native-activity"
|
||||
#define CLASS_NAME "android/app/NativeActivity"
|
||||
#define APPLICATION_CLASS_NAME "com/sample/mmdPlay/NDKSupport"
|
||||
|
||||
|
||||
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, APP_NAME, __VA_ARGS__))
|
||||
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, APP_NAME, __VA_ARGS__))
|
||||
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, APP_NAME, __VA_ARGS__))
|
||||
|
||||
jclass retrieveClass(JNIEnv *jni, ANativeActivity* activity, const char* className);
|
||||
|
||||
class JNIHelper
|
||||
{
|
||||
static ANativeActivity* _activity;
|
||||
static jobject _objJNIHelper;
|
||||
static jclass _clsJNIHelper;
|
||||
|
||||
static jstring getExternalFilesDir( JNIEnv *env );
|
||||
public:
|
||||
JNIHelper() {
|
||||
};
|
||||
~JNIHelper() {
|
||||
JNIEnv *env;
|
||||
_activity->vm->AttachCurrentThread(&env, NULL);
|
||||
|
||||
env->DeleteGlobalRef(_objJNIHelper);
|
||||
env->DeleteGlobalRef(_clsJNIHelper);
|
||||
|
||||
_activity->vm->DetachCurrentThread();
|
||||
|
||||
};
|
||||
static void init( ANativeActivity* activity )
|
||||
{
|
||||
_activity = activity;
|
||||
|
||||
};
|
||||
|
||||
static bool readFile( const char* fileName, std::vector<uint8_t>& buffer );
|
||||
static uint32_t loadTexture(const char* fileName );
|
||||
|
||||
|
||||
};
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _NDKSUPPORT_H
|
||||
#define _NDKSUPPORT_H
|
||||
|
||||
#include "shader.h" //Shader compiler support
|
||||
#include "vecmath.h" //Vector math support, C++ implementation n current version
|
||||
#include "tapCamera.h" //Tap/Pinch camera control
|
||||
#include "JNIHelper.h" //JNI support
|
||||
#include "gestureDetector.h" //Tap/Doubletap detector
|
||||
#include "perfMonitor.h" //FPS counter
|
||||
|
||||
#endif
|
||||
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// gestureDetector.cpp
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// includes
|
||||
//--------------------------------------------------------------------------------
|
||||
#include "gestureDetector.h"
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// GestureDetector
|
||||
//--------------------------------------------------------------------------------
|
||||
GestureDetector::GestureDetector()
|
||||
{
|
||||
_fDpFactor = 1.f;
|
||||
}
|
||||
|
||||
void GestureDetector::setConfiguration(AConfiguration* config)
|
||||
{
|
||||
_fDpFactor = 160.f / AConfiguration_getDensity(config);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// TapDetector
|
||||
//--------------------------------------------------------------------------------
|
||||
bool TapDetector::detect(const AInputEvent* motion_event)
|
||||
{
|
||||
if( AMotionEvent_getPointerCount(motion_event) > 1 )
|
||||
{
|
||||
//Only support single touch
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t iAction = AMotionEvent_getAction(motion_event);
|
||||
unsigned int flags = iAction & AMOTION_EVENT_ACTION_MASK;
|
||||
switch( flags )
|
||||
{
|
||||
case AMOTION_EVENT_ACTION_DOWN:
|
||||
_iDownPointerID = AMotionEvent_getPointerId(motion_event, 0);
|
||||
_fDownX = AMotionEvent_getX(motion_event, 0);
|
||||
_fDownY = AMotionEvent_getY(motion_event, 0);
|
||||
break;
|
||||
case AMOTION_EVENT_ACTION_UP:
|
||||
{
|
||||
int64_t eventTime = AMotionEvent_getEventTime(motion_event);
|
||||
int64_t downTime = AMotionEvent_getDownTime(motion_event);
|
||||
if( eventTime - downTime <= TAP_TIMEOUT )
|
||||
{
|
||||
if( _iDownPointerID == AMotionEvent_getPointerId(motion_event, 0) )
|
||||
{
|
||||
float fX = AMotionEvent_getX(motion_event, 0) - _fDownX;
|
||||
float fY = AMotionEvent_getY(motion_event, 0) - _fDownY;
|
||||
if( fX * fX + fY * fY < TOUCH_SLOP * TOUCH_SLOP * _fDpFactor )
|
||||
{
|
||||
LOGI("TapDetector: Tap detected");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// DoubletapDetector
|
||||
//--------------------------------------------------------------------------------
|
||||
bool DoubletapDetector::detect(const AInputEvent* motion_event)
|
||||
{
|
||||
if( AMotionEvent_getPointerCount(motion_event) > 1 )
|
||||
{
|
||||
//Only support single touch
|
||||
return false;
|
||||
}
|
||||
|
||||
bool bDetectedTap = _tapDetector.detect(motion_event);
|
||||
|
||||
int32_t iAction = AMotionEvent_getAction(motion_event);
|
||||
unsigned int flags = iAction & AMOTION_EVENT_ACTION_MASK;
|
||||
switch( flags )
|
||||
{
|
||||
case AMOTION_EVENT_ACTION_DOWN:
|
||||
{
|
||||
int64_t eventTime = AMotionEvent_getEventTime(motion_event);
|
||||
if( eventTime - _lastTapTime <= DOUBLE_TAP_TIMEOUT )
|
||||
{
|
||||
float fX = AMotionEvent_getX(motion_event, 0) - _fLastTapX;
|
||||
float fY = AMotionEvent_getY(motion_event, 0) - _fLastTapY;
|
||||
if( fX * fX + fY * fY < DOUBLE_TAP_SLOP * DOUBLE_TAP_SLOP * _fDpFactor )
|
||||
{
|
||||
LOGI("DoubletapDetector: Doubletap detected");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AMOTION_EVENT_ACTION_UP:
|
||||
if( bDetectedTap )
|
||||
{
|
||||
_lastTapTime = AMotionEvent_getEventTime(motion_event);
|
||||
_fLastTapX = AMotionEvent_getX(motion_event, 0);
|
||||
_fLastTapY = AMotionEvent_getY(motion_event, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DoubletapDetector::setConfiguration(AConfiguration* config)
|
||||
{
|
||||
_fDpFactor = 160.f / AConfiguration_getDensity(config);
|
||||
_tapDetector.setConfiguration(config);
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// gestureDetector.h
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
#ifndef GESTUREDETECTOR_H_
|
||||
#define GESTUREDETECTOR_H_
|
||||
|
||||
#include <android/sensor.h>
|
||||
#include <android/log.h>
|
||||
#include <android_native_app_glue.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include "JNIHelper.h"
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Constants
|
||||
//--------------------------------------------------------------------------------
|
||||
const int32_t DOUBLE_TAP_TIMEOUT = 300 * 1000000;
|
||||
const int32_t TAP_TIMEOUT = 180 * 1000000;
|
||||
const int32_t DOUBLE_TAP_SLOP = 100;
|
||||
const int32_t TOUCH_SLOP = 8;
|
||||
|
||||
class GestureDetector
|
||||
{
|
||||
protected:
|
||||
float _fDpFactor;
|
||||
public:
|
||||
GestureDetector();
|
||||
virtual ~GestureDetector() {}
|
||||
virtual void setConfiguration(AConfiguration* config);
|
||||
|
||||
virtual bool detect(const AInputEvent* motion_event) = 0;
|
||||
};
|
||||
|
||||
class TapDetector:public GestureDetector
|
||||
{
|
||||
int32_t _iDownPointerID;
|
||||
float _fDownX;
|
||||
float _fDownY;
|
||||
public:
|
||||
TapDetector() {}
|
||||
virtual ~TapDetector() {}
|
||||
virtual bool detect(const AInputEvent* motion_event);
|
||||
};
|
||||
|
||||
class DoubletapDetector:public GestureDetector
|
||||
{
|
||||
TapDetector _tapDetector;
|
||||
int64_t _lastTapTime;
|
||||
float _fLastTapX;
|
||||
float _fLastTapY;
|
||||
|
||||
public:
|
||||
DoubletapDetector() {}
|
||||
virtual ~DoubletapDetector() {}
|
||||
virtual bool detect(const AInputEvent* motion_event);
|
||||
virtual void setConfiguration(AConfiguration* config);
|
||||
};
|
||||
|
||||
|
||||
#endif /* GESTUREDETECTOR_H_ */
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "perfMonitor.h"
|
||||
|
||||
perfMonitor::perfMonitor():_dLastTick(0.f), _tvLastSec(0), _tickindex(0), _ticksum(0)
|
||||
{
|
||||
for(int32_t i = 0; i < NUM_SAMPLES; ++i )
|
||||
_ticklist[i] = 0;
|
||||
|
||||
}
|
||||
|
||||
perfMonitor::~perfMonitor() {
|
||||
}
|
||||
|
||||
double perfMonitor::updateTick(double currentTick)
|
||||
{
|
||||
_ticksum -= _ticklist[_tickindex];
|
||||
_ticksum += currentTick;
|
||||
_ticklist[_tickindex] = currentTick;
|
||||
_tickindex = (_tickindex+1)%NUM_SAMPLES;
|
||||
|
||||
return((double)_ticksum/NUM_SAMPLES);
|
||||
}
|
||||
|
||||
bool perfMonitor::update(float &fFPS)
|
||||
{
|
||||
struct timeval Time;
|
||||
gettimeofday( &Time, NULL );
|
||||
|
||||
double time = Time.tv_sec + Time.tv_usec * 1.0/1000000.0;
|
||||
double dTick = time - _dLastTick;
|
||||
double d = updateTick( dTick );
|
||||
_dLastTick = time;
|
||||
|
||||
if( Time.tv_sec - _tvLastSec >= 1 )
|
||||
{
|
||||
double time = Time.tv_sec + Time.tv_usec * 1.0/1000000.0;
|
||||
_fCurrentFPS = 1.f / d;
|
||||
_tvLastSec = Time.tv_sec;
|
||||
fFPS = _fCurrentFPS;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
fFPS = _fCurrentFPS;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef PERFMONITOR_H_
|
||||
#define PERFMONITOR_H_
|
||||
|
||||
#include <jni.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include "JNIHelper.h"
|
||||
|
||||
const int32_t NUM_SAMPLES = 100;
|
||||
|
||||
class perfMonitor {
|
||||
float _fCurrentFPS;
|
||||
time_t _tvLastSec;
|
||||
|
||||
double _dLastTick;
|
||||
int32_t _tickindex;
|
||||
double _ticksum;
|
||||
double _ticklist[ NUM_SAMPLES ];
|
||||
|
||||
double updateTick(double currentTick);
|
||||
public:
|
||||
perfMonitor();
|
||||
virtual ~perfMonitor();
|
||||
|
||||
bool update(float &fFPS);
|
||||
};
|
||||
|
||||
#endif /* PERFMONITOR_H_ */
|
||||
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "shader.h"
|
||||
#include "JNIHelper.h"
|
||||
|
||||
#define DEBUG (1)
|
||||
|
||||
bool shader::compileShader(GLuint *shader, const GLenum type,
|
||||
const char *strFileName) {
|
||||
GLint status;
|
||||
const GLchar *source;
|
||||
|
||||
std::vector<uint8_t> data;
|
||||
bool b = JNIHelper::readFile(strFileName, data);
|
||||
if (!b)
|
||||
{
|
||||
LOGI("Can not open a file:%s", strFileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
source = (GLchar *) &data[0];
|
||||
if (!source) {
|
||||
LOGI("Failed to load vertex shader:%s", strFileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t iSize = data.size();
|
||||
|
||||
*shader = glCreateShader(type);
|
||||
glShaderSource(*shader, 1, &source, &iSize); //Not specifying 3rd parameter (size) could be troublesome..
|
||||
|
||||
glCompileShader(*shader);
|
||||
|
||||
#if defined(DEBUG)
|
||||
GLint logLength;
|
||||
glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength);
|
||||
if (logLength > 0) {
|
||||
GLchar *log = (GLchar *) malloc(logLength);
|
||||
glGetShaderInfoLog(*shader, logLength, &logLength, log);
|
||||
LOGI("Shader compile log:\n%s", log);
|
||||
free(log);
|
||||
}
|
||||
#endif
|
||||
|
||||
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
|
||||
if (status == 0) {
|
||||
glDeleteShader(*shader);
|
||||
return false;
|
||||
}
|
||||
|
||||
data.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool shader::linkProgram(const GLuint prog) {
|
||||
GLint status;
|
||||
|
||||
glLinkProgram(prog);
|
||||
|
||||
#if defined(DEBUG)
|
||||
// GLint logLength;
|
||||
// glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
|
||||
// if (logLength > 0) {
|
||||
// GLchar *log = (GLchar *) malloc(logLength);
|
||||
// glGetProgramInfoLog(prog, logLength, &logLength, log);
|
||||
// LOGI("Program link log:\n%s", log);
|
||||
// free(log);
|
||||
// }
|
||||
#endif
|
||||
|
||||
glGetProgramiv(prog, GL_LINK_STATUS, &status);
|
||||
if (status == 0) {
|
||||
LOGI("Program link failed\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool shader::validateProgram(const GLuint prog) {
|
||||
GLint logLength, status;
|
||||
|
||||
glValidateProgram(prog);
|
||||
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
|
||||
if (logLength > 0) {
|
||||
GLchar *log = (GLchar *) malloc(logLength);
|
||||
glGetProgramInfoLog(prog, logLength, &logLength, log);
|
||||
LOGI("Program validate log:\n%s", log);
|
||||
free(log);
|
||||
}
|
||||
|
||||
glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
|
||||
if (status == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef SHADER_H_
|
||||
#define SHADER_H_
|
||||
|
||||
#include <jni.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <GLES/gl.h>
|
||||
|
||||
#include <android/sensor.h>
|
||||
#include <android/log.h>
|
||||
#include <android_native_app_glue.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <cpu-features.h>
|
||||
|
||||
#include "JNIHelper.h"
|
||||
|
||||
class shader {
|
||||
public:
|
||||
static bool compileShader(GLuint *shader, const GLenum type,
|
||||
const char *strFileName);
|
||||
static bool linkProgram(const GLuint prog);
|
||||
static bool validateProgram(const GLuint prog);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* SHADER_H_ */
|
||||
@@ -1,277 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------
|
||||
// tapCamera.cpp
|
||||
// Camera control with tap
|
||||
//
|
||||
//----------------------------------------------------------
|
||||
|
||||
#include <fstream>
|
||||
#include "tapCamera.h"
|
||||
|
||||
const float TRANSFORM_FACTOR = 15.f;
|
||||
const float TRANSFORM_FACTORZ = 10.f;
|
||||
|
||||
const float MOMENTUM_FACTOR_DECREASE = 0.85f;
|
||||
const float MOMENTUM_FACTOR_DECREASE_SHIFT = 0.9f;
|
||||
const float MOMENTUM_FACTOR = 0.8f;
|
||||
const float MOMENTUM_FACTOR_THRESHOLD = 0.001f;
|
||||
|
||||
//----------------------------------------------------------
|
||||
// Ctor
|
||||
//----------------------------------------------------------
|
||||
tapCamera::tapCamera():_bDragging(false), _bMomentum(false), _fBallRadius( 0.75f )
|
||||
{
|
||||
//Init offset
|
||||
initParameters();
|
||||
|
||||
_vFlip = vec2( 1.f, -1.f );
|
||||
_fFlipZ = -1.f;
|
||||
}
|
||||
|
||||
void tapCamera::initParameters()
|
||||
{
|
||||
//Init parameters
|
||||
_vecOffset = vec3();
|
||||
_vecOffsetNow = vec3();
|
||||
|
||||
_qBallNow = quaternion();
|
||||
_qBallRot = quaternion();
|
||||
_bMomentum = false;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
// Dtor
|
||||
//----------------------------------------------------------
|
||||
tapCamera::~tapCamera()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void tapCamera::update()
|
||||
{
|
||||
if( _bMomentum )
|
||||
{
|
||||
float fMomenttumSteps = _fMomentumSteps;
|
||||
|
||||
//Momentum rotation
|
||||
vec2 v = _vDragDelta;
|
||||
beginDrag(vec2() ); //NOTE:This call reset _VDragDelta
|
||||
drag(v * _vFlip);
|
||||
|
||||
//Momentum shift
|
||||
_vecOffset += _vecOffsetDelta;
|
||||
|
||||
ballUpdate();
|
||||
endDrag();
|
||||
|
||||
//Decrease deltas
|
||||
_vDragDelta = v * MOMENTUM_FACTOR_DECREASE;
|
||||
_vecOffsetDelta = _vecOffsetDelta * MOMENTUM_FACTOR_DECREASE_SHIFT;
|
||||
|
||||
//Count steps
|
||||
_fMomentumSteps = fMomenttumSteps * MOMENTUM_FACTOR_DECREASE;
|
||||
if( _fMomentumSteps < MOMENTUM_FACTOR_THRESHOLD )
|
||||
{
|
||||
_bMomentum = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_vDragDelta *= MOMENTUM_FACTOR;
|
||||
_vecOffsetDelta = _vecOffsetDelta * MOMENTUM_FACTOR;
|
||||
ballUpdate();
|
||||
}
|
||||
|
||||
vec3 vec = _vecOffset + _vecOffsetNow;
|
||||
vec3 vecTmp(TRANSFORM_FACTOR, -TRANSFORM_FACTOR, TRANSFORM_FACTORZ);
|
||||
vec *= vecTmp;
|
||||
|
||||
_mTransform = mat4::translation(vec);
|
||||
}
|
||||
|
||||
mat4& tapCamera::getRotationMatrix()
|
||||
{
|
||||
return _mRotation;
|
||||
}
|
||||
|
||||
mat4& tapCamera::getTransformMatrix()
|
||||
{
|
||||
return _mTransform;
|
||||
}
|
||||
|
||||
void tapCamera::reset(const bool bAnimate)
|
||||
{
|
||||
initParameters();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
//Drag control
|
||||
//----------------------------------------------------------
|
||||
void tapCamera::beginDrag(const vec2& v)
|
||||
{
|
||||
if( _bDragging )
|
||||
endDrag();
|
||||
|
||||
if( _bPinching )
|
||||
endPinch();
|
||||
|
||||
vec2 vec = v * _vFlip;
|
||||
_vBallNow = vec;
|
||||
_vBallDown = _vBallNow;
|
||||
|
||||
_bDragging = true;
|
||||
_bMomentum = false;
|
||||
_vLastInput = vec;
|
||||
_vDragDelta = vec2();
|
||||
}
|
||||
|
||||
void tapCamera::endDrag()
|
||||
{
|
||||
int i;
|
||||
_qBallDown = _qBallNow;
|
||||
_qBallRot = quaternion();
|
||||
|
||||
_bDragging = false;
|
||||
_bMomentum = true;
|
||||
_fMomentumSteps = 1.0f;
|
||||
}
|
||||
|
||||
void tapCamera::drag(const vec2& v )
|
||||
{
|
||||
if( !_bDragging )
|
||||
return;
|
||||
|
||||
vec2 vec = v * _vFlip;
|
||||
_vBallNow = vec;
|
||||
|
||||
_vDragDelta = _vDragDelta * MOMENTUM_FACTOR + (vec - _vLastInput);
|
||||
_vLastInput = vec;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
//Pinch controll
|
||||
//----------------------------------------------------------
|
||||
void tapCamera::beginPinch(const vec2& v1, const vec2& v2)
|
||||
{
|
||||
if( _bDragging )
|
||||
endDrag();
|
||||
|
||||
if( _bPinching )
|
||||
endPinch();
|
||||
|
||||
beginDrag( vec2() );
|
||||
|
||||
_vecPinchStartCenter = (v1 + v2) / 2.f;
|
||||
|
||||
vec2 vec = v1 - v2;
|
||||
float fXDiff;
|
||||
float fYDiff;
|
||||
vec.value( fXDiff, fYDiff );
|
||||
|
||||
_fPinchStartDistanceSQ = fXDiff*fXDiff + fYDiff*fYDiff;
|
||||
_fRotationStart = atan2f( fYDiff, fXDiff );
|
||||
_fRotationNow = 0;
|
||||
|
||||
_bPinching = true;
|
||||
_bMomentum = false;
|
||||
|
||||
//Init momentum factors
|
||||
_vecOffsetDelta = vec3();
|
||||
}
|
||||
|
||||
|
||||
void tapCamera::endPinch()
|
||||
{
|
||||
_bPinching = false;
|
||||
_bMomentum = true;
|
||||
_fMomentumSteps = 1.f;
|
||||
_vecOffset += _vecOffsetNow;
|
||||
_fRotation += _fRotationNow;
|
||||
_vecOffsetNow = vec3();
|
||||
|
||||
_fRotationNow = 0;
|
||||
|
||||
endDrag();
|
||||
|
||||
}
|
||||
|
||||
void tapCamera::pinch(const vec2& v1, const vec2& v2)
|
||||
{
|
||||
if( !_bPinching )
|
||||
return;
|
||||
|
||||
//Update momentum factor
|
||||
_vecOffsetLast = _vecOffsetNow;
|
||||
|
||||
float fXDiff, fYDiff;
|
||||
vec2 vec = v1 - v2;
|
||||
vec.value(fXDiff, fYDiff);
|
||||
|
||||
float fDistanceSQ = fXDiff * fXDiff + fYDiff * fYDiff;
|
||||
|
||||
vec = (v1 + v2) / 2.f - _vecPinchStartCenter;
|
||||
_vecOffsetNow = vec3( vec,
|
||||
_fFlipZ * (_fPinchStartDistanceSQ / fDistanceSQ - 1.f) );
|
||||
|
||||
//Update momentum factor
|
||||
_vecOffsetDelta = _vecOffsetDelta * MOMENTUM_FACTOR + (_vecOffsetNow - _vecOffsetLast);
|
||||
|
||||
//
|
||||
//Update ration quaternion
|
||||
float fRotation = atan2f( fYDiff, fXDiff );
|
||||
_fRotationNow = fRotation - _fRotationStart;
|
||||
|
||||
//Trackball rotation
|
||||
_qBallRot = quaternion( 0.f, 0.f, sinf(-_fRotationNow*0.5f), cosf(-_fRotationNow*0.5f) );
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
//Trackball controll
|
||||
//----------------------------------------------------------
|
||||
void tapCamera::ballUpdate()
|
||||
{
|
||||
if (_bDragging) {
|
||||
vec3 vFrom = pointOnSphere(_vBallDown);
|
||||
vec3 vTo = pointOnSphere(_vBallNow);
|
||||
|
||||
vec3 vec = vFrom.cross(vTo);
|
||||
float w = vFrom.dot( vTo );
|
||||
|
||||
quaternion qDrag = quaternion(vec, w);
|
||||
qDrag = qDrag * _qBallDown;
|
||||
_qBallNow = _qBallRot * qDrag;
|
||||
}
|
||||
_qBallNow.toMatrix(_mRotation);
|
||||
}
|
||||
|
||||
vec3 tapCamera::pointOnSphere(vec2& point)
|
||||
{
|
||||
vec3 ballMouse;
|
||||
float mag;
|
||||
vec2 vec = (point - _vBallCenter) / _fBallRadius;
|
||||
mag = vec.dot( vec );
|
||||
if (mag > 1.f)
|
||||
{
|
||||
float scale = 1.f / sqrtf(mag);
|
||||
vec *= scale;
|
||||
ballMouse = vec3( vec, 0.f );
|
||||
} else {
|
||||
ballMouse = vec3( vec, sqrtf(1.f - mag) );
|
||||
}
|
||||
return (ballMouse);
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//----------------------------------------------------------
|
||||
// tapCamera.h
|
||||
// Camera control with tap
|
||||
//----------------------------------------------------------
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <GLES2/gl2.h>
|
||||
|
||||
#include "JNIHelper.h"
|
||||
#include "vecmath.h"
|
||||
|
||||
class tapCamera
|
||||
{
|
||||
//Trackball
|
||||
vec2 _vBallCenter;
|
||||
float _fBallRadius;
|
||||
quaternion _qBallNow, _qBallDown;
|
||||
vec2 _vBallNow, _vBallDown;
|
||||
quaternion _qBallRot;
|
||||
|
||||
bool _bDragging;
|
||||
bool _bPinching;
|
||||
|
||||
//Pinch related info
|
||||
vec2 _vecPinchStart;
|
||||
vec2 _vecPinchStartCenter;
|
||||
float _fPinchStartDistanceSQ;
|
||||
|
||||
//Camera shift
|
||||
vec3 _vecOffset;
|
||||
vec3 _vecOffsetNow;
|
||||
|
||||
//Camera Rotation
|
||||
float _fRotation;
|
||||
float _fRotationStart;
|
||||
float _fRotationNow;
|
||||
|
||||
//Momentum support
|
||||
bool _bMomentum;
|
||||
vec2 _vDragDelta;
|
||||
vec2 _vLastInput;
|
||||
vec3 _vecOffsetLast;
|
||||
vec3 _vecOffsetDelta;
|
||||
float _fMomentumSteps;
|
||||
|
||||
vec2 _vFlip;
|
||||
float _fFlipZ;
|
||||
|
||||
mat4 _mRotation;
|
||||
mat4 _mTransform;
|
||||
|
||||
vec3 pointOnSphere(vec2& point);
|
||||
void ballUpdate();
|
||||
void initParameters();
|
||||
public:
|
||||
tapCamera();
|
||||
virtual ~tapCamera();
|
||||
void beginDrag(const vec2& vec);
|
||||
void endDrag();
|
||||
void drag(const vec2& vec);
|
||||
void update();
|
||||
|
||||
mat4& getRotationMatrix();
|
||||
mat4& getTransformMatrix();
|
||||
|
||||
void beginPinch(const vec2& v1, const vec2& v2);
|
||||
void endPinch();
|
||||
void pinch(const vec2& v1, const vec2& v2);
|
||||
|
||||
void setFlip(const float fX, const float fY, const float fZ)
|
||||
{
|
||||
_vFlip = vec2( fX, fY );
|
||||
_fFlipZ = fZ;
|
||||
}
|
||||
void reset(const bool bAnimate);
|
||||
|
||||
};
|
||||
@@ -1,267 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// vecmath.cpp
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
#include "vecmath.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// vec3
|
||||
//--------------------------------------------------------------------------------
|
||||
vec3::vec3(const vec4& vec)
|
||||
{
|
||||
x = vec.x; y = vec.y; z = vec.z;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// vec4
|
||||
//--------------------------------------------------------------------------------
|
||||
vec4 vec4::operator*(const mat4& rhs) const
|
||||
{
|
||||
vec4 out;
|
||||
out.x = x * rhs.f[0] + y * rhs.f[1] + z * rhs.f[2] + w * rhs.f[3];
|
||||
out.y = x * rhs.f[4] + y * rhs.f[5] + z * rhs.f[6] + w * rhs.f[7];
|
||||
out.z = x * rhs.f[8] + y * rhs.f[9] + z * rhs.f[10] + w * rhs.f[11];
|
||||
out.w = x * rhs.f[12] + y * rhs.f[13] + z * rhs.f[14] + w * rhs.f[15];
|
||||
return out;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// mat4
|
||||
//--------------------------------------------------------------------------------
|
||||
mat4::mat4()
|
||||
{
|
||||
for(int32_t i = 0; i < 16; ++i)
|
||||
f[i] = 0.f;
|
||||
}
|
||||
|
||||
mat4::mat4(const float* mIn )
|
||||
{
|
||||
for(int32_t i = 0; i < 16; ++i)
|
||||
f[i] = mIn[i];
|
||||
}
|
||||
|
||||
mat4 mat4::operator*(const mat4& rhs) const
|
||||
{
|
||||
mat4 ret;
|
||||
ret.f[0] = f[0]*rhs.f[0] + f[4]*rhs.f[1] + f[8]*rhs.f[2] + f[12]*rhs.f[3];
|
||||
ret.f[1] = f[1]*rhs.f[0] + f[5]*rhs.f[1] + f[9]*rhs.f[2] + f[13]*rhs.f[3];
|
||||
ret.f[2] = f[2]*rhs.f[0] + f[6]*rhs.f[1] + f[10]*rhs.f[2] + f[14]*rhs.f[3];
|
||||
ret.f[3] = f[3]*rhs.f[0] + f[7]*rhs.f[1] + f[11]*rhs.f[2] + f[15]*rhs.f[3];
|
||||
|
||||
ret.f[4] = f[0]*rhs.f[4] + f[4]*rhs.f[5] + f[8]*rhs.f[6] + f[12]*rhs.f[7];
|
||||
ret.f[5] = f[1]*rhs.f[4] + f[5]*rhs.f[5] + f[9]*rhs.f[6] + f[13]*rhs.f[7];
|
||||
ret.f[6] = f[2]*rhs.f[4] + f[6]*rhs.f[5] + f[10]*rhs.f[6] + f[14]*rhs.f[7];
|
||||
ret.f[7] = f[3]*rhs.f[4] + f[7]*rhs.f[5] + f[11]*rhs.f[6] + f[15]*rhs.f[7];
|
||||
|
||||
ret.f[8] = f[0]*rhs.f[8] + f[4]*rhs.f[9] + f[8]*rhs.f[10] + f[12]*rhs.f[11];
|
||||
ret.f[9] = f[1]*rhs.f[8] + f[5]*rhs.f[9] + f[9]*rhs.f[10] + f[13]*rhs.f[11];
|
||||
ret.f[10] = f[2]*rhs.f[8] + f[6]*rhs.f[9] + f[10]*rhs.f[10] + f[14]*rhs.f[11];
|
||||
ret.f[11] = f[3]*rhs.f[8] + f[7]*rhs.f[9] + f[11]*rhs.f[10] + f[15]*rhs.f[11];
|
||||
|
||||
ret.f[12] = f[0]*rhs.f[12] + f[4]*rhs.f[13] + f[8]*rhs.f[14] + f[12]*rhs.f[15];
|
||||
ret.f[13] = f[1]*rhs.f[12] + f[5]*rhs.f[13] + f[9]*rhs.f[14] + f[13]*rhs.f[15];
|
||||
ret.f[14] = f[2]*rhs.f[12] + f[6]*rhs.f[13] + f[10]*rhs.f[14] + f[14]*rhs.f[15];
|
||||
ret.f[15] = f[3]*rhs.f[12] + f[7]*rhs.f[13] + f[11]*rhs.f[14] + f[15]*rhs.f[15];
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
vec4 mat4::operator*(const vec4& rhs) const
|
||||
{
|
||||
vec4 ret;
|
||||
ret.x = rhs.x*f[0] + rhs.y*f[4] + rhs.z*f[8] + rhs.w*f[12];
|
||||
ret.y = rhs.x*f[1] + rhs.y*f[5] + rhs.z*f[9] + rhs.w*f[13];
|
||||
ret.z = rhs.x*f[2] + rhs.y*f[6] + rhs.z*f[10] + rhs.w*f[14];
|
||||
ret.w = rhs.x*f[3] + rhs.y*f[7] + rhs.z*f[11] + rhs.w*f[15];
|
||||
return ret;
|
||||
}
|
||||
|
||||
mat4 mat4::inverse()
|
||||
{
|
||||
mat4 ret;
|
||||
float det_1;
|
||||
float pos = 0;
|
||||
float neg = 0;
|
||||
float temp;
|
||||
|
||||
temp = f[0] * f[5] * f[10];
|
||||
if (temp >= 0) pos += temp; else neg += temp;
|
||||
temp = f[4] * f[9] * f[2];
|
||||
if (temp >= 0) pos += temp; else neg += temp;
|
||||
temp = f[8] * f[1] * f[6];
|
||||
if (temp >= 0) pos += temp; else neg += temp;
|
||||
temp = -f[8] * f[5] * f[2];
|
||||
if (temp >= 0) pos += temp; else neg += temp;
|
||||
temp = -f[4] * f[1] * f[10];
|
||||
if (temp >= 0) pos += temp; else neg += temp;
|
||||
temp = -f[0] * f[9] * f[6];
|
||||
if (temp >= 0) pos += temp; else neg += temp;
|
||||
det_1 = pos + neg;
|
||||
|
||||
if (det_1 == 0.0)
|
||||
{
|
||||
//Error
|
||||
}
|
||||
else
|
||||
{
|
||||
det_1 = 1.0f / det_1;
|
||||
ret.f[0] = ( f[ 5] * f[10] - f[ 9] * f[ 6] ) * det_1;
|
||||
ret.f[1] = -( f[ 1] * f[10] - f[ 9] * f[ 2] ) * det_1;
|
||||
ret.f[2] = ( f[ 1] * f[ 6] - f[ 5] * f[ 2] ) * det_1;
|
||||
ret.f[4] = -( f[ 4] * f[10] - f[ 8] * f[ 6] ) * det_1;
|
||||
ret.f[5] = ( f[ 0] * f[10] - f[ 8] * f[ 2] ) * det_1;
|
||||
ret.f[6] = -( f[ 0] * f[ 6] - f[ 4] * f[ 2] ) * det_1;
|
||||
ret.f[8] = ( f[ 4] * f[ 9] - f[ 8] * f[ 5] ) * det_1;
|
||||
ret.f[9] = -( f[ 0] * f[ 9] - f[ 8] * f[ 1] ) * det_1;
|
||||
ret.f[10] = ( f[ 0] * f[ 5] - f[ 4] * f[ 1] ) * det_1;
|
||||
|
||||
/* Calculate -C * inverse(A) */
|
||||
ret.f[12] = - ( f[12] *ret.f[0] + f[13] * ret.f[4] + f[14] *ret.f[8] );
|
||||
ret.f[13] = - ( f[12] * ret.f[1] + f[13] * ret.f[5] + f[14] * ret.f[9] );
|
||||
ret.f[14] = - ( f[12] * ret.f[2] + f[13] * ret.f[6] + f[14] * ret.f[10] );
|
||||
|
||||
ret.f[ 3] = 0.0f;
|
||||
ret.f[ 7] = 0.0f;
|
||||
ret.f[11] = 0.0f;
|
||||
ret.f[15] = 1.0f;
|
||||
}
|
||||
|
||||
*this = ret;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Misc
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
mat4 mat4::rotationX(
|
||||
const float fAngle)
|
||||
{
|
||||
mat4 ret;
|
||||
float fCosine, fSine;
|
||||
|
||||
fCosine = cosf(fAngle);
|
||||
fSine = sinf(fAngle);
|
||||
|
||||
ret.f[ 0]=1.0f; ret.f[ 4]=0.0f; ret.f[ 8]=0.0f; ret.f[12]=0.0f;
|
||||
ret.f[ 1]=0.0f; ret.f[ 5]=fCosine; ret.f[ 9]=fSine; ret.f[13]=0.0f;
|
||||
ret.f[ 2]=0.0f; ret.f[ 6]=-fSine; ret.f[10]=fCosine; ret.f[14]=0.0f;
|
||||
ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f;
|
||||
return ret;
|
||||
}
|
||||
|
||||
mat4 mat4::rotationY(
|
||||
const float fAngle)
|
||||
{
|
||||
mat4 ret;
|
||||
float fCosine, fSine;
|
||||
|
||||
fCosine = cosf(fAngle);
|
||||
fSine = sinf(fAngle);
|
||||
|
||||
ret.f[ 0]=fCosine; ret.f[ 4]=0.0f; ret.f[ 8]=-fSine; ret.f[12]=0.0f;
|
||||
ret.f[ 1]=0.0f; ret.f[ 5]=1.0f; ret.f[ 9]=0.0f; ret.f[13]=0.0f;
|
||||
ret.f[ 2]=fSine; ret.f[ 6]=0.0f; ret.f[10]=fCosine; ret.f[14]=0.0f;
|
||||
ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f;
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
mat4 mat4::rotationZ(
|
||||
const float fAngle)
|
||||
{
|
||||
mat4 ret;
|
||||
float fCosine, fSine;
|
||||
|
||||
fCosine = cosf(fAngle);
|
||||
fSine = sinf(fAngle);
|
||||
|
||||
ret.f[ 0]=fCosine; ret.f[ 4]=fSine; ret.f[ 8]=0.0f; ret.f[12]=0.0f;
|
||||
ret.f[ 1]=-fSine; ret.f[ 5]=fCosine; ret.f[ 9]=0.0f; ret.f[13]=0.0f;
|
||||
ret.f[ 2]=0.0f; ret.f[ 6]=0.0f; ret.f[10]=1.0f; ret.f[14]=0.0f;
|
||||
ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f;
|
||||
return ret;
|
||||
}
|
||||
|
||||
mat4 mat4::translation(
|
||||
const float fX,
|
||||
const float fY,
|
||||
const float fZ)
|
||||
{
|
||||
mat4 ret;
|
||||
ret.f[ 0]=1.0f; ret.f[ 4]=0.0f; ret.f[ 8]=0.0f; ret.f[12]=fX;
|
||||
ret.f[ 1]=0.0f; ret.f[ 5]=1.0f; ret.f[ 9]=0.0f; ret.f[13]=fY;
|
||||
ret.f[ 2]=0.0f; ret.f[ 6]=0.0f; ret.f[10]=1.0f; ret.f[14]=fZ;
|
||||
ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f;
|
||||
return ret;
|
||||
}
|
||||
|
||||
mat4 mat4::translation(
|
||||
const vec3 vec)
|
||||
{
|
||||
mat4 ret;
|
||||
ret.f[ 0]=1.0f; ret.f[ 4]=0.0f; ret.f[ 8]=0.0f; ret.f[12]=vec.x;
|
||||
ret.f[ 1]=0.0f; ret.f[ 5]=1.0f; ret.f[ 9]=0.0f; ret.f[13]=vec.y;
|
||||
ret.f[ 2]=0.0f; ret.f[ 6]=0.0f; ret.f[10]=1.0f; ret.f[14]=vec.z;
|
||||
ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f;
|
||||
return ret;
|
||||
}
|
||||
|
||||
mat4 mat4::perspective(
|
||||
float width,
|
||||
float height,
|
||||
float nearPlane, float farPlane)
|
||||
{
|
||||
float n2 = 2.0f * nearPlane;
|
||||
float rcpnmf = 1.f/(nearPlane - farPlane);
|
||||
|
||||
mat4 result;
|
||||
result.f[0] = n2 / width; result.f[4] = 0; result.f[8] = 0; result.f[12] = 0;
|
||||
result.f[1] = 0; result.f[5] = n2 / height; result.f[9] = 0; result.f[13] = 0;
|
||||
result.f[2] = 0; result.f[6] = 0; result.f[10] = (farPlane+nearPlane)*rcpnmf; result.f[14] = farPlane*rcpnmf*n2;
|
||||
result.f[3] = 0; result.f[7] = 0; result.f[11] = -1.0; result.f[15]=0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
mat4 mat4::lookAt(const vec3& vEye, const vec3& vAt, const vec3& vUp)
|
||||
{
|
||||
vec3 vForward, vUpNorm, vSide;
|
||||
mat4 result;
|
||||
|
||||
vForward.x = vEye.x - vAt.x;
|
||||
vForward.y = vEye.y - vAt.y;
|
||||
vForward.z = vEye.z - vAt.z;
|
||||
|
||||
vForward.normalize();
|
||||
vUpNorm = vUp;
|
||||
vUpNorm.normalize();
|
||||
vSide = vUpNorm.cross( vForward);
|
||||
vUpNorm = vForward.cross(vSide);
|
||||
|
||||
result.f[0]=vSide.x; result.f[4]=vSide.y; result.f[8]=vSide.z; result.f[12]=0;
|
||||
result.f[1]=vUpNorm.x; result.f[5]=vUpNorm.y; result.f[9]=vUpNorm.z; result.f[13]=0;
|
||||
result.f[2]=vForward.x; result.f[6]=vForward.y; result.f[10]=vForward.z; result.f[14]=0;
|
||||
result.f[3]=0; result.f[7]=0; result.f[11]=0; result.f[15]=1.0;
|
||||
|
||||
result.postTranslate(-vEye.x, -vEye.y, -vEye.z);
|
||||
return result;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -21,6 +21,7 @@
|
||||
#include <errno.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#include <GLES/gl.h>
|
||||
|
||||
@@ -30,173 +31,136 @@
|
||||
#include <android/native_window_jni.h>
|
||||
#include <cpu-features.h>
|
||||
|
||||
#include "NDKSupport/NDKSupport.h"
|
||||
#include "TeapotRenderer.h"
|
||||
#include "NDKHelper.h"
|
||||
|
||||
/**
|
||||
* Our saved state data.
|
||||
*/
|
||||
struct saved_state {
|
||||
float angle;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
};
|
||||
|
||||
/**
|
||||
* Shared state for our app.
|
||||
*/
|
||||
//-------------------------------------------------------------------------
|
||||
//Shared state for our app.
|
||||
//-------------------------------------------------------------------------
|
||||
struct android_app;
|
||||
class engine {
|
||||
public:
|
||||
struct android_app* app;
|
||||
|
||||
ASensorManager* sensorManager;
|
||||
const ASensor* accelerometerSensor;
|
||||
ASensorEventQueue* sensorEventQueue;
|
||||
|
||||
int animating;
|
||||
EGLDisplay display;
|
||||
EGLSurface surface;
|
||||
EGLContext context;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
struct saved_state state;
|
||||
|
||||
std::vector<int32_t> _vecPointers;
|
||||
tapCamera _tapCamera;
|
||||
TeapotRenderer _renderer;
|
||||
|
||||
GLContext* _glContext;
|
||||
|
||||
bool _bInitializedResources;
|
||||
bool _bHasFocus;
|
||||
|
||||
DoubletapDetector _doubletapDetector;
|
||||
PinchDetector _pinchDetector;
|
||||
DragDetector _dragDetector;
|
||||
perfMonitor _monitor;
|
||||
|
||||
tapCamera _tapCamera;
|
||||
|
||||
android_app* _app;
|
||||
|
||||
ASensorManager* _sensorManager;
|
||||
const ASensor* _accelerometerSensor;
|
||||
ASensorEventQueue* _sensorEventQueue;
|
||||
|
||||
void updateFPS(float fFPS);
|
||||
void showUI();
|
||||
void transformPosition( vec2& vec );
|
||||
|
||||
public:
|
||||
static void handleCmd(struct android_app* app, int32_t cmd);
|
||||
static int32_t handleInput( android_app* app, AInputEvent* event );
|
||||
|
||||
engine();
|
||||
~engine();
|
||||
void setState(android_app* state);
|
||||
int initDisplay();
|
||||
void loadResources();
|
||||
void unloadResources();
|
||||
void drawFrame();
|
||||
void termDisplay();
|
||||
void trimMemory();
|
||||
bool isReady();
|
||||
|
||||
void updatePosition( AInputEvent* event, int32_t iIndex, float& fX, float& fY);
|
||||
|
||||
void initSensors();
|
||||
void processSensors( int32_t id );
|
||||
void suspendSensors();
|
||||
void resumeSensors();
|
||||
};
|
||||
|
||||
|
||||
void showUI(android_app* app)
|
||||
//-------------------------------------------------------------------------
|
||||
//Ctor
|
||||
//-------------------------------------------------------------------------
|
||||
engine::engine() :
|
||||
_bInitializedResources( false ),
|
||||
_bHasFocus( false ),
|
||||
_app( NULL ),
|
||||
_sensorManager( NULL ),
|
||||
_accelerometerSensor( NULL ),
|
||||
_sensorEventQueue( NULL )
|
||||
{
|
||||
JNIEnv *jni;
|
||||
app->activity->vm->AttachCurrentThread(&jni, NULL);
|
||||
|
||||
//Default class retrieval
|
||||
jclass clazz = jni->GetObjectClass(app->activity->clazz);
|
||||
jmethodID methodID = jni->GetMethodID(clazz, "showUI", "()V");
|
||||
jni->CallVoidMethod(app->activity->clazz, methodID);
|
||||
|
||||
app->activity->vm->DetachCurrentThread();
|
||||
|
||||
return;
|
||||
_glContext = GLContext::getInstance();
|
||||
}
|
||||
|
||||
|
||||
void updateFPS(android_app* app, float fFPS)
|
||||
//-------------------------------------------------------------------------
|
||||
//Dtor
|
||||
//-------------------------------------------------------------------------
|
||||
engine::~engine()
|
||||
{
|
||||
JNIEnv *jni;
|
||||
app->activity->vm->AttachCurrentThread(&jni, NULL);
|
||||
}
|
||||
|
||||
//Default class retrieval
|
||||
jclass clazz = jni->GetObjectClass(app->activity->clazz);
|
||||
jmethodID methodID = jni->GetMethodID(clazz, "updateFPS", "(F)V");
|
||||
jni->CallVoidMethod(app->activity->clazz, methodID, fFPS);
|
||||
/**
|
||||
* Load resources
|
||||
*/
|
||||
void engine::loadResources()
|
||||
{
|
||||
_renderer.init();
|
||||
_renderer.bind(&_tapCamera);
|
||||
}
|
||||
|
||||
app->activity->vm->DetachCurrentThread();
|
||||
|
||||
return;
|
||||
/**
|
||||
* Unload resources
|
||||
*/
|
||||
void engine::unloadResources()
|
||||
{
|
||||
_renderer.unload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize an EGL context for the current display.
|
||||
*/
|
||||
static int engine_init_display(struct engine* engine) {
|
||||
// initialize OpenGL ES and EGL
|
||||
|
||||
showUI(engine->app);
|
||||
|
||||
const EGLint contextAttribs[] = {
|
||||
EGL_CONTEXT_CLIENT_VERSION, 2, //Request opengl ES2.0
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
EGLint w, h, dummy, format;
|
||||
EGLint numConfigs;
|
||||
EGLConfig config;
|
||||
EGLSurface surface;
|
||||
EGLContext context;
|
||||
|
||||
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
|
||||
eglInitialize(display, 0, 0);
|
||||
|
||||
/*
|
||||
* Here specify the attributes of the desired configuration.
|
||||
* Below, we select an EGLConfig with at least 8 bits per color
|
||||
* component compatible with on-screen windows
|
||||
*/
|
||||
const EGLint attribs[] = {
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Request opengl ES2.0
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_RED_SIZE, 8,
|
||||
EGL_DEPTH_SIZE, 24,
|
||||
EGL_NONE
|
||||
};
|
||||
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
|
||||
if( !numConfigs )
|
||||
int engine::initDisplay()
|
||||
{
|
||||
if( !_bInitializedResources )
|
||||
{
|
||||
//Fall back to 16bit depth buffer
|
||||
const EGLint attribs[] = {
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Request opengl ES2.0
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_RED_SIZE, 8,
|
||||
EGL_DEPTH_SIZE, 16,
|
||||
EGL_NONE
|
||||
};
|
||||
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
|
||||
_glContext->init( _app->window );
|
||||
loadResources();
|
||||
_bInitializedResources = true;
|
||||
}
|
||||
if ( !numConfigs )
|
||||
else
|
||||
{
|
||||
LOGW("Unable to retrieve EGL config");
|
||||
return -1;
|
||||
// initialize OpenGL ES and EGL
|
||||
if( EGL_SUCCESS != _glContext->resume( _app->window ) )
|
||||
{
|
||||
unloadResources();
|
||||
loadResources();
|
||||
}
|
||||
}
|
||||
|
||||
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
|
||||
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
|
||||
* As soon as we picked a EGLConfig, we can safely reconfigure the
|
||||
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
|
||||
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
|
||||
|
||||
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
|
||||
|
||||
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
|
||||
context = eglCreateContext(display, config, NULL, contextAttribs);
|
||||
|
||||
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
|
||||
LOGW("Unable to eglMakeCurrent");
|
||||
return -1;
|
||||
}
|
||||
|
||||
eglQuerySurface(display, surface, EGL_WIDTH, &w);
|
||||
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
|
||||
|
||||
engine->display = display;
|
||||
engine->context = context;
|
||||
engine->surface = surface;
|
||||
engine->width = w;
|
||||
engine->height = h;
|
||||
engine->state.angle = 0;
|
||||
showUI();
|
||||
|
||||
// Initialize GL state.
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
glFrontFace(GL_CW);
|
||||
glViewport(0, 0, engine->width, engine->height);
|
||||
|
||||
engine->_tapCamera.setFlip(1.f, -1.f, -8.f);
|
||||
//Note that screen size might have been changed
|
||||
glViewport(0, 0,
|
||||
_glContext->getScreenWidth(),
|
||||
_glContext->getScreenHeight() );
|
||||
_renderer.updateViewport();
|
||||
|
||||
engine->_renderer.init();
|
||||
engine->_renderer.bind(&engine->_tapCamera);
|
||||
_tapCamera.setFlip(1.f, -1.f, -1.f);
|
||||
_tapCamera.setPinchTransformFactor(2.f, 2.f, 8.f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -204,201 +168,108 @@ static int engine_init_display(struct engine* engine) {
|
||||
/**
|
||||
* Just the current frame in the display.
|
||||
*/
|
||||
static void engine_draw_frame(struct engine* engine) {
|
||||
if (engine->display == NULL) {
|
||||
// No display.
|
||||
return;
|
||||
}
|
||||
|
||||
void engine::drawFrame()
|
||||
{
|
||||
float fFPS;
|
||||
bool b = engine->_monitor.update(fFPS);
|
||||
if( b )
|
||||
if( _monitor.update(fFPS) )
|
||||
{
|
||||
updateFPS( engine->app, fFPS );
|
||||
updateFPS( fFPS );
|
||||
}
|
||||
|
||||
struct timeval Time;
|
||||
gettimeofday( &Time, NULL );
|
||||
double dTime = Time.tv_sec + Time.tv_usec * 1.0/1000000.0 ;
|
||||
|
||||
engine->_renderer.update(dTime);
|
||||
double dTime = _monitor.getCurrentTime();
|
||||
_renderer.update(dTime);
|
||||
|
||||
// Just fill the screen with a color.
|
||||
glViewport(0, 0, engine->width, engine->height);
|
||||
glClearColor(0.5f, 0.5f, 0.5f, 1.f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
engine->_renderer.render();
|
||||
_renderer.render();
|
||||
|
||||
eglSwapBuffers(engine->display, engine->surface);
|
||||
// Swap
|
||||
if( EGL_SUCCESS != _glContext->swap() )
|
||||
{
|
||||
unloadResources();
|
||||
loadResources();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down the EGL context currently associated with the display.
|
||||
*/
|
||||
static void engine_term_display(struct engine* engine) {
|
||||
|
||||
engine->_renderer.unload();
|
||||
|
||||
if (engine->display != EGL_NO_DISPLAY) {
|
||||
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
if (engine->context != EGL_NO_CONTEXT) {
|
||||
eglDestroyContext(engine->display, engine->context);
|
||||
}
|
||||
if (engine->surface != EGL_NO_SURFACE) {
|
||||
eglDestroySurface(engine->display, engine->surface);
|
||||
}
|
||||
eglTerminate(engine->display);
|
||||
}
|
||||
engine->animating = 0;
|
||||
engine->display = EGL_NO_DISPLAY;
|
||||
engine->context = EGL_NO_CONTEXT;
|
||||
engine->surface = EGL_NO_SURFACE;
|
||||
}
|
||||
|
||||
int32_t findIndex( AInputEvent* event, int32_t iID )
|
||||
void engine::termDisplay()
|
||||
{
|
||||
int32_t iCount = AMotionEvent_getPointerCount(event);
|
||||
for( int32_t i = 0; i < iCount; ++i )
|
||||
{
|
||||
if( iID == AMotionEvent_getPointerId(event, i) )
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
_glContext->suspend();
|
||||
|
||||
}
|
||||
|
||||
void updatePosition( engine* engine, AInputEvent* event, int32_t iIndex, float& fX, float& fY)
|
||||
void engine::trimMemory()
|
||||
{
|
||||
engine->state.x = AMotionEvent_getX(event, iIndex);
|
||||
engine->state.y = AMotionEvent_getY(event, iIndex);
|
||||
|
||||
fX = 2.0f * engine->state.x / engine->width -1.f;
|
||||
fY = 2.0f * engine->state.y / engine->height -1.f;
|
||||
LOGI( "Trimming memory" );
|
||||
_glContext->invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the next input event.
|
||||
*/
|
||||
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
|
||||
struct engine* engine = (struct engine*)app->userData;
|
||||
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
|
||||
engine->animating = 1;
|
||||
int32_t engine::handleInput( android_app* app, AInputEvent* event )
|
||||
{
|
||||
engine* eng = (engine*)app->userData;
|
||||
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION)
|
||||
{
|
||||
GESTURE_STATE doubleTapState = eng->_doubletapDetector.detect(event);
|
||||
GESTURE_STATE dragState = eng->_dragDetector.detect(event);
|
||||
GESTURE_STATE pinchState = eng->_pinchDetector.detect(event);
|
||||
|
||||
int32_t iCount = AMotionEvent_getPointerCount(event);
|
||||
int32_t iAction = AMotionEvent_getAction(event);
|
||||
unsigned int flags = iAction & AMOTION_EVENT_ACTION_MASK;
|
||||
float fX;
|
||||
float fY;
|
||||
float fX2;
|
||||
float fY2;
|
||||
switch( flags )
|
||||
{
|
||||
case AMOTION_EVENT_ACTION_DOWN:
|
||||
engine->_vecPointers.push_back(AMotionEvent_getPointerId(event, 0));
|
||||
//Double tap detector has a priority over other detectors
|
||||
if( doubleTapState == GESTURE_STATE_ACTION )
|
||||
{
|
||||
//Detect double tap
|
||||
eng->_tapCamera.reset(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Handle drag state
|
||||
if( dragState & GESTURE_STATE_START )
|
||||
{
|
||||
//Otherwise, start dragging
|
||||
vec2 v;
|
||||
eng->_dragDetector.getPointer( v );
|
||||
eng->transformPosition( v );
|
||||
eng->_tapCamera.beginDrag( v );
|
||||
}
|
||||
else if( dragState & GESTURE_STATE_MOVE )
|
||||
{
|
||||
vec2 v;
|
||||
eng->_dragDetector.getPointer( v );
|
||||
eng->transformPosition( v );
|
||||
eng->_tapCamera.drag( v );
|
||||
}
|
||||
else if( dragState & GESTURE_STATE_END )
|
||||
{
|
||||
eng->_tapCamera.endDrag();
|
||||
}
|
||||
|
||||
//Single touch
|
||||
if( engine->_doubletapDetector.detect(event) )
|
||||
{
|
||||
//Detect double tap
|
||||
engine->_tapCamera.reset(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Otherwise, start dragging
|
||||
updatePosition(engine, event, 0, fX, fY);
|
||||
engine->_tapCamera.beginDrag( vec2( fX, fY ) );
|
||||
}
|
||||
break;
|
||||
case AMOTION_EVENT_ACTION_POINTER_DOWN:
|
||||
{
|
||||
int32_t iIndex = (iAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
|
||||
engine->_vecPointers.push_back(AMotionEvent_getPointerId(event, iIndex));
|
||||
if( iCount == 2 )
|
||||
{
|
||||
//Start pinch
|
||||
//Start new pinch
|
||||
int32_t iIndex = findIndex( event, engine->_vecPointers[0] );
|
||||
updatePosition(engine, event, iIndex, fX, fY);
|
||||
iIndex = findIndex( event, engine->_vecPointers[1] );
|
||||
updatePosition(engine, event, iIndex, fX2, fY2);
|
||||
engine->_tapCamera.beginPinch( vec2( fX, fY ), vec2( fX2, fY2 ) );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case AMOTION_EVENT_ACTION_UP:
|
||||
//Update doubletap detector
|
||||
engine->_doubletapDetector.detect(event);
|
||||
|
||||
engine->_vecPointers.pop_back();
|
||||
engine->_tapCamera.endDrag();
|
||||
break;
|
||||
case AMOTION_EVENT_ACTION_POINTER_UP:
|
||||
{
|
||||
int32_t iIndex = (iAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
|
||||
int32_t iReleasedPointerID = AMotionEvent_getPointerId(event, iIndex);
|
||||
|
||||
std::vector<int32_t>::iterator it = engine->_vecPointers.begin();
|
||||
std::vector<int32_t>::iterator itEnd = engine->_vecPointers.end();
|
||||
int32_t i = 0;
|
||||
for(;it!=itEnd;++it, ++i)
|
||||
{
|
||||
if( *it == iReleasedPointerID )
|
||||
{
|
||||
engine->_vecPointers.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( i <= 1 )
|
||||
{
|
||||
//Reset pinch or drag
|
||||
if( iCount == 2 )
|
||||
{
|
||||
//Start new drag
|
||||
int32_t iIndex = findIndex( event, engine->_vecPointers.front() );
|
||||
updatePosition(engine, event, iIndex, fX, fY);
|
||||
engine->_tapCamera.beginDrag( vec2( fX, fY ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
//Start new pinch
|
||||
int32_t iIndex = findIndex( event, engine->_vecPointers[0] );
|
||||
updatePosition(engine, event, iIndex, fX, fY);
|
||||
iIndex = findIndex( event, engine->_vecPointers[1] );
|
||||
updatePosition(engine, event, iIndex, fX2, fY2);
|
||||
engine->_tapCamera.beginPinch( vec2( fX, fY ), vec2( fX2, fY2 ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case AMOTION_EVENT_ACTION_MOVE:
|
||||
{
|
||||
switch(iCount)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
//Single touch
|
||||
int32_t iIndex = findIndex( event, engine->_vecPointers.front() );
|
||||
updatePosition(engine, event, iIndex, fX, fY);
|
||||
engine->_tapCamera.drag( vec2( fX, fY ) );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
//Multi touch
|
||||
int32_t iIndex = findIndex( event, engine->_vecPointers[0] );
|
||||
updatePosition(engine, event, iIndex, fX, fY);
|
||||
iIndex = findIndex( event, engine->_vecPointers[1] );
|
||||
updatePosition(engine, event, iIndex, fX2, fY2);
|
||||
engine->_tapCamera.pinch( vec2( fX, fY ), vec2( fX2, fY2 ) );
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AMOTION_EVENT_ACTION_CANCEL:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
//Handle pinch state
|
||||
if( pinchState & GESTURE_STATE_START )
|
||||
{
|
||||
//Start new pinch
|
||||
vec2 v1;
|
||||
vec2 v2;
|
||||
eng->_pinchDetector.getPointers( v1, v2 );
|
||||
eng->transformPosition( v1 );
|
||||
eng->transformPosition( v2 );
|
||||
eng->_tapCamera.beginPinch( v1, v2 );
|
||||
}
|
||||
else if( pinchState & GESTURE_STATE_MOVE )
|
||||
{
|
||||
//Multi touch
|
||||
//Start new pinch
|
||||
vec2 v1;
|
||||
vec2 v2;
|
||||
eng->_pinchDetector.getPointers( v1, v2 );
|
||||
eng->transformPosition( v1 );
|
||||
eng->transformPosition( v2 );
|
||||
eng->_tapCamera.pinch( v1, v2 );
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -406,130 +277,206 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
|
||||
/**
|
||||
* Process the next main command.
|
||||
*/
|
||||
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
|
||||
struct engine* engine = (struct engine*)app->userData;
|
||||
void engine::handleCmd(struct android_app* app, int32_t cmd)
|
||||
{
|
||||
engine* eng = (engine*)app->userData;
|
||||
switch (cmd) {
|
||||
case APP_CMD_SAVE_STATE:
|
||||
// The system has asked us to save our current state. Do so.
|
||||
engine->app->savedState = malloc(sizeof(struct saved_state));
|
||||
*((struct saved_state*)engine->app->savedState) = engine->state;
|
||||
engine->app->savedStateSize = sizeof(struct saved_state);
|
||||
break;
|
||||
case APP_CMD_INIT_WINDOW:
|
||||
// The window is being shown, get it ready.
|
||||
if (engine->app->window != NULL) {
|
||||
engine_init_display(engine);
|
||||
engine_draw_frame(engine);
|
||||
engine->animating = 1;
|
||||
if (app->window != NULL) {
|
||||
eng->initDisplay();
|
||||
eng->drawFrame();
|
||||
}
|
||||
break;
|
||||
case APP_CMD_TERM_WINDOW:
|
||||
// The window is being hidden or closed, clean it up.
|
||||
engine_term_display(engine);
|
||||
eng->termDisplay();
|
||||
eng->_bHasFocus = false;
|
||||
break;
|
||||
case APP_CMD_STOP:
|
||||
break;
|
||||
case APP_CMD_GAINED_FOCUS:
|
||||
// When our app gains focus, we start monitoring the accelerometer.
|
||||
if (engine->accelerometerSensor != NULL) {
|
||||
ASensorEventQueue_enableSensor(engine->sensorEventQueue,
|
||||
engine->accelerometerSensor);
|
||||
// We'd like to get 60 events per second (in us).
|
||||
ASensorEventQueue_setEventRate(engine->sensorEventQueue,
|
||||
engine->accelerometerSensor, (1000L/60)*1000);
|
||||
}
|
||||
eng->resumeSensors();
|
||||
//Start animation
|
||||
eng->_bHasFocus = true;
|
||||
break;
|
||||
case APP_CMD_LOST_FOCUS:
|
||||
// When our app loses focus, we stop monitoring the accelerometer.
|
||||
// This is to avoid consuming battery while not being used.
|
||||
if (engine->accelerometerSensor != NULL) {
|
||||
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
|
||||
engine->accelerometerSensor);
|
||||
}
|
||||
eng->suspendSensors();
|
||||
// Also stop animating.
|
||||
engine->animating = 0;
|
||||
engine_draw_frame(engine);
|
||||
eng->_bHasFocus = false;
|
||||
eng->drawFrame();
|
||||
break;
|
||||
case APP_CMD_LOW_MEMORY:
|
||||
//Free up GL resources
|
||||
eng->trimMemory();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//Sensor handlers
|
||||
//-------------------------------------------------------------------------
|
||||
void engine::initSensors()
|
||||
{
|
||||
_sensorManager = ASensorManager_getInstance();
|
||||
_accelerometerSensor = ASensorManager_getDefaultSensor( _sensorManager,
|
||||
ASENSOR_TYPE_ACCELEROMETER);
|
||||
_sensorEventQueue = ASensorManager_createEventQueue( _sensorManager,
|
||||
_app->looper, LOOPER_ID_USER, NULL, NULL);
|
||||
}
|
||||
|
||||
void engine::processSensors( int32_t id )
|
||||
{
|
||||
// If a sensor has data, process it now.
|
||||
if( id == LOOPER_ID_USER )
|
||||
{
|
||||
if (_accelerometerSensor != NULL)
|
||||
{
|
||||
ASensorEvent event;
|
||||
while (ASensorEventQueue_getEvents(_sensorEventQueue,
|
||||
&event, 1) > 0)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void engine::resumeSensors()
|
||||
{
|
||||
// When our app gains focus, we start monitoring the accelerometer.
|
||||
if (_accelerometerSensor != NULL) {
|
||||
ASensorEventQueue_enableSensor(_sensorEventQueue,
|
||||
_accelerometerSensor);
|
||||
// We'd like to get 60 events per second (in us).
|
||||
ASensorEventQueue_setEventRate(_sensorEventQueue,
|
||||
_accelerometerSensor, (1000L/60)*1000);
|
||||
}
|
||||
}
|
||||
|
||||
void engine::suspendSensors()
|
||||
{
|
||||
// When our app loses focus, we stop monitoring the accelerometer.
|
||||
// This is to avoid consuming battery while not being used.
|
||||
if (_accelerometerSensor != NULL) {
|
||||
ASensorEventQueue_disableSensor(_sensorEventQueue,
|
||||
_accelerometerSensor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//Misc
|
||||
//-------------------------------------------------------------------------
|
||||
void engine::setState(android_app* state)
|
||||
{
|
||||
_app = state;
|
||||
_doubletapDetector.setConfiguration( _app->config );
|
||||
_dragDetector.setConfiguration( _app->config );
|
||||
_pinchDetector.setConfiguration( _app->config );
|
||||
}
|
||||
|
||||
bool engine::isReady()
|
||||
{
|
||||
if( _bHasFocus )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void engine::transformPosition( vec2& vec )
|
||||
{
|
||||
vec = vec2( 2.0f, 2.0f ) * vec / vec2( _glContext->getScreenWidth(), _glContext->getScreenHeight() ) - vec2( 1.f, 1.f );
|
||||
}
|
||||
|
||||
void engine::showUI()
|
||||
{
|
||||
JNIEnv *jni;
|
||||
_app->activity->vm->AttachCurrentThread(&jni, NULL);
|
||||
|
||||
//Default class retrieval
|
||||
jclass clazz = jni->GetObjectClass( _app->activity->clazz );
|
||||
jmethodID methodID = jni->GetMethodID(clazz, "showUI", "()V" );
|
||||
jni->CallVoidMethod( _app->activity->clazz, methodID );
|
||||
|
||||
_app->activity->vm->DetachCurrentThread();
|
||||
return;
|
||||
}
|
||||
|
||||
void engine::updateFPS(float fFPS)
|
||||
{
|
||||
JNIEnv *jni;
|
||||
_app->activity->vm->AttachCurrentThread(&jni, NULL);
|
||||
|
||||
//Default class retrieval
|
||||
jclass clazz = jni->GetObjectClass(_app->activity->clazz);
|
||||
jmethodID methodID = jni->GetMethodID(clazz, "updateFPS", "(F)V");
|
||||
jni->CallVoidMethod(_app->activity->clazz, methodID, fFPS);
|
||||
|
||||
_app->activity->vm->DetachCurrentThread();
|
||||
return;
|
||||
}
|
||||
|
||||
engine g_engine;
|
||||
|
||||
/**
|
||||
* This is the main entry point of a native application that is using
|
||||
* android_native_app_glue. It runs in its own thread, with its own
|
||||
* event loop for receiving input events and doing other things.
|
||||
*/
|
||||
void android_main(struct android_app* state) {
|
||||
engine engine;
|
||||
|
||||
// Make sure glue isn't stripped.
|
||||
void android_main(android_app* state)
|
||||
{
|
||||
app_dummy();
|
||||
|
||||
state->userData = &engine;
|
||||
state->onAppCmd = engine_handle_cmd;
|
||||
state->onInputEvent = engine_handle_input;
|
||||
engine.app = state;
|
||||
engine._doubletapDetector.setConfiguration(state->config);
|
||||
g_engine.setState( state );
|
||||
|
||||
//Init helper functions
|
||||
JNIHelper::init( state->activity );
|
||||
|
||||
state->userData = &g_engine;
|
||||
state->onAppCmd = engine::handleCmd;
|
||||
state->onInputEvent = engine::handleInput;
|
||||
|
||||
#ifdef USE_NDK_PROFILER
|
||||
monstartup("libNativeActivity.so");
|
||||
#endif
|
||||
|
||||
// Prepare to monitor accelerometer
|
||||
engine.sensorManager = ASensorManager_getInstance();
|
||||
engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
|
||||
ASENSOR_TYPE_ACCELEROMETER);
|
||||
engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager,
|
||||
state->looper, LOOPER_ID_USER, NULL, NULL);
|
||||
|
||||
if (state->savedState != NULL) {
|
||||
// We are starting with a previous saved state; restore from it.
|
||||
engine.state = *(struct saved_state*)state->savedState;
|
||||
}
|
||||
g_engine.initSensors();
|
||||
|
||||
// loop waiting for stuff to do.
|
||||
|
||||
while (1) {
|
||||
// Read all pending events.
|
||||
int ident;
|
||||
int id;
|
||||
int events;
|
||||
struct android_poll_source* source;
|
||||
android_poll_source* source;
|
||||
|
||||
// If not animating, we will block forever waiting for events.
|
||||
// If animating, we loop until all events are read, then continue
|
||||
// to draw the next frame of animation.
|
||||
while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
|
||||
(void**)&source)) >= 0) {
|
||||
|
||||
while ((id = ALooper_pollAll( g_engine.isReady() ? 0 : -1, NULL, &events,
|
||||
(void**)&source) ) >= 0)
|
||||
{
|
||||
// Process this event.
|
||||
if (source != NULL) {
|
||||
if (source != NULL)
|
||||
source->process(state, source);
|
||||
}
|
||||
|
||||
// If a sensor has data, process it now.
|
||||
if (ident == LOOPER_ID_USER) {
|
||||
if (engine.accelerometerSensor != NULL) {
|
||||
ASensorEvent event;
|
||||
while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
|
||||
&event, 1) > 0) {
|
||||
}
|
||||
}
|
||||
}
|
||||
g_engine.processSensors( id );
|
||||
|
||||
// Check if we are exiting.
|
||||
if (state->destroyRequested != 0) {
|
||||
engine_term_display(&engine);
|
||||
if (state->destroyRequested != 0)
|
||||
{
|
||||
g_engine.termDisplay();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (engine.animating) {
|
||||
// Done with events; draw next animation frame.
|
||||
engine.state.angle += .01f;
|
||||
if (engine.state.angle > 1) {
|
||||
engine.state.angle = 0;
|
||||
}
|
||||
|
||||
if( g_engine.isReady() )
|
||||
{
|
||||
// Drawing is throttled to the screen update rate, so there
|
||||
// is no need to do timing here.
|
||||
engine_draw_frame(&engine);
|
||||
g_engine.drawFrame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,15 @@ void TeapotRenderer::init()
|
||||
|
||||
delete[] p;
|
||||
|
||||
updateViewport();
|
||||
_mModel = mat4::translation(0, 0, -15.f);
|
||||
|
||||
mat4 mat = mat4::rotationX(M_PI / 3);
|
||||
_mModel = mat * _mModel;
|
||||
}
|
||||
|
||||
void TeapotRenderer::updateViewport()
|
||||
{
|
||||
//Init Projection matrices
|
||||
int32_t viewport[4];
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
@@ -95,10 +104,6 @@ void TeapotRenderer::init()
|
||||
bool bRotate = false;
|
||||
_mProjection = mat4::perspective(fAspect, 1.f,
|
||||
CAM_NEAR, CAM_FAR);
|
||||
_mModel = mat4::translation(0, 0, -15.f);
|
||||
|
||||
mat4 mat = mat4::rotationX(M_PI / 3);
|
||||
_mModel = mat * _mModel;
|
||||
}
|
||||
|
||||
void TeapotRenderer::unload()
|
||||
|
||||
@@ -38,7 +38,10 @@
|
||||
#include <android/native_window_jni.h>
|
||||
#include <cpu-features.h>
|
||||
|
||||
#include "NDKSupport/NDKSupport.h"
|
||||
#define CLASS_NAME "android/app/NativeActivity"
|
||||
#define APPLICATION_CLASS_NAME "com/sample/teapot/TeapotApplication"
|
||||
|
||||
#include "NDKHelper.h"
|
||||
|
||||
|
||||
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
||||
@@ -95,7 +98,7 @@ public:
|
||||
void update(float dTime);
|
||||
bool bind(tapCamera* camera);
|
||||
void unload();
|
||||
|
||||
void updateViewport();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.sample.helper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Matrix;
|
||||
import android.opengl.GLUtils;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
public class NDKHelper {
|
||||
private static Context context;
|
||||
|
||||
public static void setCotext( Context c )
|
||||
{
|
||||
Log.i("NDKHelper", "set context:" + c);
|
||||
context = c;
|
||||
}
|
||||
|
||||
//
|
||||
//Load Bitmap
|
||||
//Java helper is useful decoding PNG, TIFF etc rather than linking libPng etc separately
|
||||
//
|
||||
private int nextPOT(int i)
|
||||
{
|
||||
int pot = 1;
|
||||
while( pot < i ) pot <<= 1;
|
||||
return pot;
|
||||
}
|
||||
|
||||
private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight)
|
||||
{
|
||||
if(bitmapToScale == null)
|
||||
return null;
|
||||
//get the original width and height
|
||||
int width = bitmapToScale.getWidth();
|
||||
int height = bitmapToScale.getHeight();
|
||||
// create a matrix for the manipulation
|
||||
Matrix matrix = new Matrix();
|
||||
|
||||
// resize the bit map
|
||||
matrix.postScale(newWidth / width, newHeight / height);
|
||||
|
||||
// recreate the new Bitmap and set it back
|
||||
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
|
||||
}
|
||||
|
||||
public void loadTexture(String path)
|
||||
{
|
||||
Bitmap bitmap = null;
|
||||
try
|
||||
{
|
||||
String str = path;
|
||||
if (!path.startsWith("/"))
|
||||
{
|
||||
str = "/" + path;
|
||||
}
|
||||
|
||||
File file = new File(context.getExternalFilesDir(null), str);
|
||||
if( file.canRead() )
|
||||
{
|
||||
bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
|
||||
}
|
||||
else
|
||||
{
|
||||
bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path));
|
||||
}
|
||||
// Matrix matrix = new Matrix();
|
||||
// // resize the bit map
|
||||
// matrix.postScale(-1F, 1F);
|
||||
//
|
||||
// // recreate the new Bitmap and set it back
|
||||
// bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.w("NDKHelper", "Coundn't load a file:" + path);
|
||||
}
|
||||
|
||||
if( bitmap != null )
|
||||
{
|
||||
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
public Bitmap openBitmap(String path, boolean iScalePOT)
|
||||
{
|
||||
Bitmap bitmap = null;
|
||||
try
|
||||
{
|
||||
bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path));
|
||||
if( iScalePOT )
|
||||
{
|
||||
int originalWidth = getBitmapWidth(bitmap);
|
||||
int originalHeight = getBitmapHeight(bitmap);
|
||||
int width = nextPOT(originalWidth);
|
||||
int height = nextPOT(originalHeight);
|
||||
if( originalWidth != width || originalHeight != height )
|
||||
{
|
||||
//Scale it
|
||||
bitmap = scaleBitmap( bitmap, width, height );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.w("NDKHelper", "Coundn't load a file:" + path);
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public int getBitmapWidth(Bitmap bmp) { return bmp.getWidth(); }
|
||||
public int getBitmapHeight(Bitmap bmp) { return bmp.getHeight(); }
|
||||
|
||||
public void getBitmapPixels(Bitmap bmp, int[] pixels)
|
||||
{
|
||||
int w = bmp.getWidth();
|
||||
int h = bmp.getHeight();
|
||||
bmp.getPixels(pixels, 0, w, 0, 0, w, h);
|
||||
}
|
||||
|
||||
public void closeBitmap(Bitmap bmp)
|
||||
{
|
||||
bmp.recycle();
|
||||
}
|
||||
|
||||
public static String getNativeLibraryDirectory(Context appContext) {
|
||||
ApplicationInfo ai = context.getApplicationInfo();
|
||||
|
||||
Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir );
|
||||
|
||||
if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
|
||||
(ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
return ai.nativeLibraryDir;
|
||||
}
|
||||
return "/system/lib/";
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,7 @@ import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class TeapotApplication extends Application {
|
||||
private static Context context;
|
||||
public void onCreate(){
|
||||
context=getApplicationContext();
|
||||
Log.w("native-activity", "onCreate");
|
||||
|
||||
final PackageManager pm = getApplicationContext().getPackageManager();
|
||||
@@ -30,87 +28,4 @@ public class TeapotApplication extends Application {
|
||||
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
|
||||
Toast.makeText(this, applicationName, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
//Load Bitmap
|
||||
private int nextPOT(int i)
|
||||
{
|
||||
int pot = 1;
|
||||
while( pot < i ) pot <<= 1;
|
||||
return pot;
|
||||
}
|
||||
|
||||
private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight)
|
||||
{
|
||||
if(bitmapToScale == null)
|
||||
return null;
|
||||
//get the original width and height
|
||||
int width = bitmapToScale.getWidth();
|
||||
int height = bitmapToScale.getHeight();
|
||||
// create a matrix for the manipulation
|
||||
Matrix matrix = new Matrix();
|
||||
|
||||
// resize the bit map
|
||||
matrix.postScale(newWidth / width, newHeight / height);
|
||||
|
||||
// recreate the new Bitmap and set it back
|
||||
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);
|
||||
}
|
||||
|
||||
public void loadTexture(String path)
|
||||
{
|
||||
Bitmap bitmap = null;
|
||||
try
|
||||
{
|
||||
bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path));
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.w("native-activity", "Coundn't load a file:" + path);
|
||||
}
|
||||
|
||||
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
public Bitmap openBitmap(String path, boolean iScalePOT)
|
||||
{
|
||||
Bitmap bitmap = null;
|
||||
try
|
||||
{
|
||||
bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path));
|
||||
if( iScalePOT )
|
||||
{
|
||||
int originalWidth = getBitmapWidth(bitmap);
|
||||
int originalHeight = getBitmapHeight(bitmap);
|
||||
int width = nextPOT(originalWidth);
|
||||
int height = nextPOT(originalHeight);
|
||||
if( originalWidth != width || originalHeight != height )
|
||||
{
|
||||
//Scale it
|
||||
bitmap = scaleBitmap( bitmap, width, height );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.w("native-activity", "Coundn't load a file:" + path);
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public int getBitmapWidth(Bitmap bmp) { return bmp.getWidth(); }
|
||||
public int getBitmapHeight(Bitmap bmp) { return bmp.getHeight(); }
|
||||
|
||||
public void getBitmapPixels(Bitmap bmp, int[] pixels)
|
||||
{
|
||||
int w = bmp.getWidth();
|
||||
int h = bmp.getHeight();
|
||||
bmp.getPixels(pixels, 0, w, 0, 0, w, h);
|
||||
}
|
||||
|
||||
public void closeBitmap(Bitmap bmp)
|
||||
{
|
||||
bmp.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,12 @@ public class TeapotNativeActivity extends NativeActivity {
|
||||
|
||||
//Hide toolbar
|
||||
int SDK_INT = android.os.Build.VERSION.SDK_INT;
|
||||
if(SDK_INT >= 11 && SDK_INT < 14) {
|
||||
if(SDK_INT >= 11 && SDK_INT < 14)
|
||||
{
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
|
||||
}else if(SDK_INT >= 14){
|
||||
}
|
||||
else if(SDK_INT >= 14)
|
||||
{
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LOW_PROFILE);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user