Move Teapot to android-17 and set APP_ABI := all
For the default configuration to run on more devices: ARM/Intel/MIPS >= API17 Change-Id: I52145374d0c51624c7a295dc8092f8492e591dee
This commit is contained in:
34
ndk/platforms/android-17/samples/Teapot/AndroidManifest.xml
Normal file
34
ndk/platforms/android-17/samples/Teapot/AndroidManifest.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.sample.teapot"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="11"
|
||||
android:targetSdkVersion="17" />
|
||||
<uses-feature android:glEsVersion="0x00020000"></uses-feature>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme"
|
||||
android:hasCode="true"
|
||||
android:name="com.sample.teapot.TeapotApplication"
|
||||
>
|
||||
|
||||
<!-- Our activity is the built-in NativeActivity framework class.
|
||||
This will take care of integrating with our NDK code. -->
|
||||
<activity android:name="com.sample.teapot.TeapotNativeActivity"
|
||||
android:label="@string/app_name"
|
||||
android:configChanges="orientation|keyboardHidden">
|
||||
<!-- Tell NativeActivity the name of or .so -->
|
||||
<meta-data android:name="android.app.lib_name"
|
||||
android:value="NativeActivity" />
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// ShaderPlain.fsh
|
||||
//
|
||||
|
||||
#define USE_PHONG (1)
|
||||
|
||||
uniform lowp vec3 vMaterialAmbient;
|
||||
uniform lowp vec4 vMaterialSpecular;
|
||||
|
||||
varying lowp vec4 colorDiffuse;
|
||||
|
||||
#if USE_PHONG
|
||||
uniform highp vec3 vLight0;
|
||||
varying mediump vec3 position;
|
||||
varying mediump vec3 normal;
|
||||
#else
|
||||
varying lowp vec4 colorSpecular;
|
||||
#endif
|
||||
|
||||
void main()
|
||||
{
|
||||
#if USE_PHONG
|
||||
mediump vec3 halfVector = normalize(-vLight0 + position);
|
||||
mediump float NdotH = max(dot(normal, halfVector), 0.0);
|
||||
mediump float fPower = vMaterialSpecular.w;
|
||||
mediump float specular = pow(NdotH, fPower);
|
||||
|
||||
lowp vec4 colorSpecular = vec4( vMaterialSpecular.xyz * specular, 1 );
|
||||
gl_FragColor = colorDiffuse + colorSpecular;
|
||||
#else
|
||||
gl_FragColor = colorDiffuse + colorSpecular;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// ShaderPlain.vsh
|
||||
//
|
||||
|
||||
#define USE_PHONG (1)
|
||||
|
||||
attribute highp vec3 myVertex;
|
||||
attribute highp vec3 myNormal;
|
||||
attribute mediump vec2 myUV;
|
||||
attribute mediump vec4 myBone;
|
||||
|
||||
varying mediump vec2 texCoord;
|
||||
varying lowp vec4 colorDiffuse;
|
||||
|
||||
#if USE_PHONG
|
||||
varying mediump vec3 position;
|
||||
varying mediump vec3 normal;
|
||||
#else
|
||||
varying lowp vec4 colorSpecular;
|
||||
#endif
|
||||
|
||||
uniform highp mat4 uMVMatrix;
|
||||
uniform highp mat4 uPMatrix;
|
||||
|
||||
uniform highp vec3 vLight0;
|
||||
|
||||
uniform lowp vec4 vMaterialDiffuse;
|
||||
uniform lowp vec3 vMaterialAmbient;
|
||||
uniform lowp vec4 vMaterialSpecular;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
highp vec4 p = vec4(myVertex,1);
|
||||
gl_Position = uPMatrix * p;
|
||||
|
||||
texCoord = myUV;
|
||||
|
||||
highp vec3 worldNormal = vec3(mat3(uMVMatrix[0].xyz, uMVMatrix[1].xyz, uMVMatrix[2].xyz) * myNormal);
|
||||
highp vec3 ecPosition = p.xyz;
|
||||
|
||||
colorDiffuse = dot( worldNormal, normalize(-vLight0+ecPosition) ) * vMaterialDiffuse + vec4( vMaterialAmbient, 1 );
|
||||
|
||||
#if USE_PHONG
|
||||
normal = worldNormal;
|
||||
position = ecPosition;
|
||||
#else
|
||||
highp vec3 halfVector = normalize(ecPosition - vLight0);
|
||||
|
||||
highp float NdotH = max(-dot(worldNormal, halfVector), 0.0);
|
||||
float fPower = vMaterialSpecular.w;
|
||||
highp float specular = min( pow(NdotH, fPower), 1.0);
|
||||
colorSpecular = vec4( vMaterialSpecular.xyz * specular, 1 );
|
||||
#endif
|
||||
}
|
||||
25
ndk/platforms/android-17/samples/Teapot/jni/Android.mk
Normal file
25
ndk/platforms/android-17/samples/Teapot/jni/Android.mk
Normal file
@@ -0,0 +1,25 @@
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
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_CFLAGS :=
|
||||
|
||||
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2
|
||||
LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
$(call import-module,android/native_app_glue)
|
||||
$(call import-module,android/cpufeatures)
|
||||
@@ -0,0 +1,4 @@
|
||||
APP_PLATFORM := android-9
|
||||
APP_ABI := all
|
||||
|
||||
APP_STL := stlport_static
|
||||
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 );
|
||||
|
||||
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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_ */
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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_ */
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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_ */
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
};
|
||||
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
1028
ndk/platforms/android-17/samples/Teapot/jni/NDKSupport/vecmath.h
Normal file
1028
ndk/platforms/android-17/samples/Teapot/jni/NDKSupport/vecmath.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,535 @@
|
||||
/*
|
||||
* 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 files
|
||||
//--------------------------------------------------------------------------------
|
||||
#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 "NDKSupport/NDKSupport.h"
|
||||
#include "TeapotRenderer.h"
|
||||
|
||||
/**
|
||||
* Our saved state data.
|
||||
*/
|
||||
struct saved_state {
|
||||
float angle;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
};
|
||||
|
||||
/**
|
||||
* Shared state for our 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;
|
||||
|
||||
DoubletapDetector _doubletapDetector;
|
||||
perfMonitor _monitor;
|
||||
};
|
||||
|
||||
|
||||
void showUI(android_app* app)
|
||||
{
|
||||
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 updateFPS(android_app* app, 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
//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);
|
||||
}
|
||||
if ( !numConfigs )
|
||||
{
|
||||
LOGW("Unable to retrieve EGL config");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
// 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);
|
||||
|
||||
engine->_renderer.init();
|
||||
engine->_renderer.bind(&engine->_tapCamera);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Just the current frame in the display.
|
||||
*/
|
||||
static void engine_draw_frame(struct engine* engine) {
|
||||
if (engine->display == NULL) {
|
||||
// No display.
|
||||
return;
|
||||
}
|
||||
|
||||
float fFPS;
|
||||
bool b = engine->_monitor.update(fFPS);
|
||||
if( b )
|
||||
{
|
||||
updateFPS( engine->app, fFPS );
|
||||
}
|
||||
|
||||
struct timeval Time;
|
||||
gettimeofday( &Time, NULL );
|
||||
double dTime = Time.tv_sec + Time.tv_usec * 1.0/1000000.0 ;
|
||||
|
||||
engine->_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();
|
||||
|
||||
eglSwapBuffers(engine->display, engine->surface);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
int32_t iCount = AMotionEvent_getPointerCount(event);
|
||||
for( int32_t i = 0; i < iCount; ++i )
|
||||
{
|
||||
if( iID == AMotionEvent_getPointerId(event, i) )
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void updatePosition( engine* engine, AInputEvent* event, int32_t iIndex, float& fX, float& fY)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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));
|
||||
|
||||
//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;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the next main command.
|
||||
*/
|
||||
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
|
||||
struct engine* engine = (struct 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;
|
||||
}
|
||||
break;
|
||||
case APP_CMD_TERM_WINDOW:
|
||||
// The window is being hidden or closed, clean it up.
|
||||
engine_term_display(engine);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
// Also stop animating.
|
||||
engine->animating = 0;
|
||||
engine_draw_frame(engine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
app_dummy();
|
||||
|
||||
state->userData = &engine;
|
||||
state->onAppCmd = engine_handle_cmd;
|
||||
state->onInputEvent = engine_handle_input;
|
||||
engine.app = state;
|
||||
engine._doubletapDetector.setConfiguration(state->config);
|
||||
|
||||
//Init helper functions
|
||||
JNIHelper::init( state->activity );
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// loop waiting for stuff to do.
|
||||
|
||||
while (1) {
|
||||
// Read all pending events.
|
||||
int ident;
|
||||
int events;
|
||||
struct 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) {
|
||||
|
||||
// Process this event.
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we are exiting.
|
||||
if (state->destroyRequested != 0) {
|
||||
engine_term_display(&engine);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (engine.animating) {
|
||||
// Done with events; draw next animation frame.
|
||||
engine.state.angle += .01f;
|
||||
if (engine.state.angle > 1) {
|
||||
engine.state.angle = 0;
|
||||
}
|
||||
|
||||
// Drawing is throttled to the screen update rate, so there
|
||||
// is no need to do timing here.
|
||||
engine_draw_frame(&engine);
|
||||
}
|
||||
}
|
||||
}
|
||||
291
ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.cpp
Normal file
291
ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.cpp
Normal file
@@ -0,0 +1,291 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// TeapotRenderer.cpp
|
||||
// Render a teapot
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Include files
|
||||
//--------------------------------------------------------------------------------
|
||||
#include "TeapotRenderer.h"
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Teapot model data
|
||||
//--------------------------------------------------------------------------------
|
||||
#include "teapot.inl"
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Ctor
|
||||
//--------------------------------------------------------------------------------
|
||||
TeapotRenderer::TeapotRenderer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Dtor
|
||||
//--------------------------------------------------------------------------------
|
||||
TeapotRenderer::~TeapotRenderer() {
|
||||
unload();
|
||||
}
|
||||
|
||||
void TeapotRenderer::init()
|
||||
{
|
||||
//Settings
|
||||
glFrontFace (GL_CCW);
|
||||
|
||||
//Load shader
|
||||
loadShaders( &_shaderParam, "Shaders/VS_ShaderPlain.vsh", "Shaders/ShaderPlain.fsh" );
|
||||
|
||||
//Create Index buffer
|
||||
_iNumIndices = sizeof(teapotIndices) / sizeof(teapotIndices[0]);
|
||||
glGenBuffers(1, &_ibo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
|
||||
sizeof( teapotIndices ) , teapotIndices,
|
||||
GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
//Create VBO
|
||||
_iNumVertices = sizeof(teapotPositions) / sizeof(teapotPositions[0]) / 3;
|
||||
int32_t iStride = sizeof(TEAPOT_VERTEX);
|
||||
int32_t iIndex = 0;
|
||||
TEAPOT_VERTEX* p = new TEAPOT_VERTEX[_iNumVertices];
|
||||
for( int32_t i = 0; i < _iNumVertices; ++i )
|
||||
{
|
||||
p[i].fPos[0] = teapotPositions[iIndex];
|
||||
p[i].fPos[1] = teapotPositions[iIndex+1];
|
||||
p[i].fPos[2] = teapotPositions[iIndex+2];
|
||||
|
||||
p[i].fNormal[0] = teapotNormals[iIndex];
|
||||
p[i].fNormal[1] = teapotNormals[iIndex+1];
|
||||
p[i].fNormal[2] = teapotNormals[iIndex+2];
|
||||
iIndex += 3;
|
||||
}
|
||||
glGenBuffers(1, &_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, iStride * _iNumVertices,
|
||||
p, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
delete[] p;
|
||||
|
||||
//Init Projection matrices
|
||||
int32_t viewport[4];
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
float fAspect = (float)viewport[2] / (float)viewport[3];
|
||||
|
||||
const float CAM_NEAR = 5.f;
|
||||
const float CAM_FAR = 10000.f;
|
||||
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()
|
||||
{
|
||||
if (_vbo)
|
||||
{
|
||||
glDeleteBuffers(1, &_vbo);
|
||||
_vbo = 0;
|
||||
}
|
||||
|
||||
if (_ibo)
|
||||
{
|
||||
glDeleteBuffers(1, &_ibo);
|
||||
_ibo = 0;
|
||||
}
|
||||
|
||||
if (_shaderParam._program )
|
||||
{
|
||||
glDeleteProgram(_shaderParam._program);
|
||||
_shaderParam._program = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void TeapotRenderer::update(float fTime)
|
||||
{
|
||||
const float CAM_X = 0.f;
|
||||
const float CAM_Y = 0.f;
|
||||
const float CAM_Z = 700.f;
|
||||
|
||||
_mView = mat4::lookAt(vec3(CAM_X, CAM_Y, CAM_Z),
|
||||
vec3(0.f, 0.f, 0.f), vec3(0.f, 1.f, 0.f));
|
||||
|
||||
if( _camera )
|
||||
{
|
||||
_camera->update();
|
||||
_mView = _camera->getTransformMatrix() * _mView * _camera->getRotationMatrix() * _mModel;
|
||||
}
|
||||
else
|
||||
{
|
||||
_mView = _mView * _mModel;
|
||||
}
|
||||
}
|
||||
|
||||
void TeapotRenderer::render()
|
||||
{
|
||||
//
|
||||
// Feed Projection and Model View matrices to the shaders
|
||||
mat4 mVP = _mProjection * _mView;
|
||||
|
||||
// Bind the VBO
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||
|
||||
int32_t iStride = sizeof(TEAPOT_VERTEX);
|
||||
// Pass the vertex data
|
||||
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, iStride,
|
||||
BUFFER_OFFSET( 0 ));
|
||||
glEnableVertexAttribArray(ATTRIB_VERTEX);
|
||||
|
||||
glVertexAttribPointer(ATTRIB_NORMAL, 3, GL_FLOAT, GL_FALSE, iStride,
|
||||
BUFFER_OFFSET( 3 * sizeof(GLfloat) ));
|
||||
glEnableVertexAttribArray(ATTRIB_NORMAL);
|
||||
|
||||
|
||||
// Bind the IB
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
|
||||
|
||||
glUseProgram(_shaderParam._program);
|
||||
|
||||
TEAPOT_MATERIALS material = {
|
||||
{1.0f, 0.5f, 0.5f},
|
||||
{1.0f, 1.0f, 1.0f, 10.f},
|
||||
{0.1f, 0.1f, 0.1f},
|
||||
};
|
||||
|
||||
//Update uniforms
|
||||
glUniform4f(_shaderParam._uiMaterialDiffuse,
|
||||
material.diffuse_color[0],
|
||||
material.diffuse_color[1],
|
||||
material.diffuse_color[2],
|
||||
1.f);
|
||||
|
||||
glUniform4f(_shaderParam._uiMaterialSpecular,
|
||||
material.specular_color[0],
|
||||
material.specular_color[1],
|
||||
material.specular_color[2],
|
||||
material.specular_color[3]);
|
||||
//
|
||||
//using glUniform3fv here was troublesome
|
||||
//
|
||||
glUniform3f(_shaderParam._uiMaterialAmbient,
|
||||
material.ambient_color[0],
|
||||
material.ambient_color[1],
|
||||
material.ambient_color[2]);
|
||||
|
||||
glUniformMatrix4fv(_shaderParam._uiMatrixP, 1, GL_FALSE,
|
||||
mVP.ptr());
|
||||
glUniformMatrix4fv(_shaderParam._uiMatrixView, 1, GL_FALSE,
|
||||
_mView.ptr());
|
||||
glUniform3f(_shaderParam._uiLight0, 100.f, -200.f, -600.f);
|
||||
|
||||
glDrawElements(GL_TRIANGLES, _iNumIndices, GL_UNSIGNED_SHORT,
|
||||
BUFFER_OFFSET(0));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
bool TeapotRenderer::loadShaders(SHADER_PARAMS* params, const char* strVsh, const char* strFsh)
|
||||
{
|
||||
GLuint program;
|
||||
GLuint vertShader, fragShader;
|
||||
char *vertShaderPathname, *fragShaderPathname;
|
||||
|
||||
// Create shader program
|
||||
program = glCreateProgram();
|
||||
LOGI("Created Shader %d", program);
|
||||
|
||||
// Create and compile vertex shader
|
||||
if (!shader::compileShader(&vertShader, GL_VERTEX_SHADER, strVsh)) {
|
||||
LOGI("Failed to compile vertex shader");
|
||||
glDeleteProgram(program);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and compile fragment shader
|
||||
if (!shader::compileShader(&fragShader, GL_FRAGMENT_SHADER, strFsh)) {
|
||||
LOGI("Failed to compile fragment shader");
|
||||
glDeleteProgram(program);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Attach vertex shader to program
|
||||
glAttachShader(program, vertShader);
|
||||
|
||||
// Attach fragment shader to program
|
||||
glAttachShader(program, fragShader);
|
||||
|
||||
// Bind attribute locations
|
||||
// this needs to be done prior to linking
|
||||
glBindAttribLocation(program, ATTRIB_VERTEX, "myVertex");
|
||||
glBindAttribLocation(program, ATTRIB_NORMAL, "myNormal");
|
||||
glBindAttribLocation(program, ATTRIB_UV, "myUV");
|
||||
|
||||
// Link program
|
||||
if (!shader::linkProgram(program)) {
|
||||
LOGI("Failed to link program: %d", program);
|
||||
|
||||
if (vertShader) {
|
||||
glDeleteShader(vertShader);
|
||||
vertShader = 0;
|
||||
}
|
||||
if (fragShader) {
|
||||
glDeleteShader(fragShader);
|
||||
fragShader = 0;
|
||||
}
|
||||
if (program) {
|
||||
glDeleteProgram(program);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get uniform locations
|
||||
params->_uiMatrixP = glGetUniformLocation(program, "uPMatrix");
|
||||
params->_uiMatrixView = glGetUniformLocation(program, "uMVMatrix");
|
||||
|
||||
params->_uiLight0 = glGetUniformLocation(program, "vLight0");
|
||||
params->_uiMaterialDiffuse = glGetUniformLocation(program,
|
||||
"vMaterialDiffuse");
|
||||
params->_uiMaterialAmbient = glGetUniformLocation(program,
|
||||
"vMaterialAmbient");
|
||||
params->_uiMaterialSpecular = glGetUniformLocation(program,
|
||||
"vMaterialSpecular");
|
||||
|
||||
// Release vertex and fragment shaders
|
||||
if (vertShader)
|
||||
glDeleteShader(vertShader);
|
||||
if (fragShader)
|
||||
glDeleteShader(fragShader);
|
||||
|
||||
params->_program = program;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TeapotRenderer::bind(tapCamera* camera)
|
||||
{
|
||||
_camera = camera;
|
||||
return true;
|
||||
}
|
||||
|
||||
102
ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.h
Normal file
102
ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Teapot Renderer.h
|
||||
// Renderer for teapots
|
||||
//--------------------------------------------------------------------------------
|
||||
#ifndef _TEAPOTRENDERER_H
|
||||
#define _TEAPOTRENDERER_H
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
// Include files
|
||||
//--------------------------------------------------------------------------------
|
||||
#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 "NDKSupport/NDKSupport.h"
|
||||
|
||||
|
||||
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
||||
|
||||
struct TEAPOT_VERTEX
|
||||
{
|
||||
float fPos[3];
|
||||
float fNormal[3];
|
||||
};
|
||||
|
||||
enum SHADER_ATTRIBUTES {
|
||||
ATTRIB_VERTEX, ATTRIB_NORMAL, ATTRIB_UV,
|
||||
};
|
||||
|
||||
struct SHADER_PARAMS
|
||||
{
|
||||
GLuint _program;
|
||||
GLuint _uiLight0;
|
||||
GLuint _uiMaterialDiffuse;
|
||||
GLuint _uiMaterialAmbient;
|
||||
GLuint _uiMaterialSpecular;
|
||||
|
||||
GLuint _uiMatrixP;
|
||||
GLuint _uiMatrixView;
|
||||
};
|
||||
|
||||
struct TEAPOT_MATERIALS
|
||||
{
|
||||
float diffuse_color[ 3 ];
|
||||
float specular_color[ 4 ];
|
||||
float ambient_color[ 3 ];
|
||||
};
|
||||
|
||||
class TeapotRenderer
|
||||
{
|
||||
int32_t _iNumIndices;
|
||||
int32_t _iNumVertices;
|
||||
GLuint _ibo;
|
||||
GLuint _vbo;
|
||||
|
||||
SHADER_PARAMS _shaderParam;
|
||||
bool loadShaders(SHADER_PARAMS* params, const char* strVsh, const char* strFsh);
|
||||
|
||||
mat4 _mProjection;
|
||||
mat4 _mView;
|
||||
mat4 _mModel;
|
||||
|
||||
tapCamera* _camera;
|
||||
public:
|
||||
TeapotRenderer();
|
||||
virtual ~TeapotRenderer();
|
||||
void init();
|
||||
void render();
|
||||
void update(float dTime);
|
||||
bool bind(tapCamera* camera);
|
||||
void unload();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
2057
ndk/platforms/android-17/samples/Teapot/jni/teapot.inl
Normal file
2057
ndk/platforms/android-17/samples/Teapot/jni/teapot.inl
Normal file
File diff suppressed because it is too large
Load Diff
3
ndk/platforms/android-17/samples/Teapot/lint.xml
Normal file
3
ndk/platforms/android-17/samples/Teapot/lint.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<lint>
|
||||
</lint>
|
||||
14
ndk/platforms/android-17/samples/Teapot/project.properties
Normal file
14
ndk/platforms/android-17/samples/Teapot/project.properties
Normal file
@@ -0,0 +1,14 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system edit
|
||||
# "ant.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
#
|
||||
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
|
||||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||
|
||||
# Project target.
|
||||
target=Google Inc.:Google APIs:17
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="top"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textViewFPS"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="right"
|
||||
android:text="0.0 FPS"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
android:textColor="@android:color/white" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,11 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme for API 11+. This theme completely replaces
|
||||
AppBaseTheme from res/values/styles.xml on API 11+ devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
|
||||
<!-- API 11 theme customizations can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,12 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme for API 14+. This theme completely replaces
|
||||
AppBaseTheme from BOTH res/values/styles.xml and
|
||||
res/values-v11/styles.xml on API 14+ devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
|
||||
<!-- API 14 theme customizations can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,5 @@
|
||||
<resources>
|
||||
|
||||
<string name="app_name">Teapot</string>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,20 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme, dependent on API level. This theme is replaced
|
||||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Light">
|
||||
<!--
|
||||
Theme customizations available in newer API levels can go in
|
||||
res/values-vXX/styles.xml, while customizations related to
|
||||
backward-compatibility can go here.
|
||||
-->
|
||||
</style>
|
||||
|
||||
<!-- Application theme. -->
|
||||
<style name="AppTheme" parent="AppBaseTheme">
|
||||
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.sample.teapot;
|
||||
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Matrix;
|
||||
import android.opengl.GLUtils;
|
||||
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();
|
||||
ApplicationInfo ai;
|
||||
try {
|
||||
ai = pm.getApplicationInfo( this.getPackageName(), 0);
|
||||
} catch (final NameNotFoundException e) {
|
||||
ai = null;
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.sample.teapot;
|
||||
|
||||
import android.app.NativeActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup.MarginLayoutParams;
|
||||
import android.view.WindowManager.LayoutParams;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.PopupWindow;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class TeapotNativeActivity extends NativeActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
//Hide toolbar
|
||||
int SDK_INT = android.os.Build.VERSION.SDK_INT;
|
||||
if(SDK_INT >= 11 && SDK_INT < 14) {
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
|
||||
}else if(SDK_INT >= 14){
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LOW_PROFILE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Our popup window, you will call it from your C/C++ code later
|
||||
|
||||
TeapotNativeActivity _activity;
|
||||
PopupWindow _popupWindow;
|
||||
TextView _label;
|
||||
|
||||
public void showUI()
|
||||
{
|
||||
if( _popupWindow != null )
|
||||
return;
|
||||
|
||||
_activity = this;
|
||||
|
||||
this.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LayoutInflater layoutInflater
|
||||
= (LayoutInflater)getBaseContext()
|
||||
.getSystemService(LAYOUT_INFLATER_SERVICE);
|
||||
View popupView = layoutInflater.inflate(R.layout.widgets, null);
|
||||
_popupWindow = new PopupWindow(
|
||||
popupView,
|
||||
LayoutParams.WRAP_CONTENT,
|
||||
LayoutParams.WRAP_CONTENT);
|
||||
|
||||
LinearLayout mainLayout = new LinearLayout(_activity);
|
||||
MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
params.setMargins(0, 0, 0, 0);
|
||||
_activity.setContentView(mainLayout, params);
|
||||
|
||||
// Show our UI over NativeActivity window
|
||||
_popupWindow.showAtLocation(mainLayout, Gravity.TOP | Gravity.LEFT, 10, 10);
|
||||
_popupWindow.update();
|
||||
|
||||
_label = (TextView)popupView.findViewById(R.id.textViewFPS);
|
||||
|
||||
}});
|
||||
}
|
||||
|
||||
public void updateFPS(final float fFPS)
|
||||
{
|
||||
if( _label == null )
|
||||
return;
|
||||
|
||||
_activity = this;
|
||||
this.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
_label.setText(String.format("%2.2f FPS", fFPS));
|
||||
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user