diff --git a/ndk/platforms/android-17/samples/Teapot/jni/Android.mk b/ndk/platforms/android-17/samples/Teapot/jni/Android.mk index eec31a6a7..6db5a40b1 100644 --- a/ndk/platforms/android-17/samples/Teapot/jni/Android.mk +++ b/ndk/platforms/android-17/samples/Teapot/jni/Android.mk @@ -11,10 +11,10 @@ LOCAL_C_INCLUDES := LOCAL_CFLAGS := LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue helper +LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue ndk_helper include $(BUILD_SHARED_LIBRARY) -$(call import-module,android/helper) +$(call import-module,android/ndk_helper) $(call import-module,android/native_app_glue) $(call import-module,android/cpufeatures) diff --git a/ndk/platforms/android-17/samples/Teapot/jni/TeapotNativeActivity.cpp b/ndk/platforms/android-17/samples/Teapot/jni/TeapotNativeActivity.cpp index f8d8b107e..60eab991b 100644 --- a/ndk/platforms/android-17/samples/Teapot/jni/TeapotNativeActivity.cpp +++ b/ndk/platforms/android-17/samples/Teapot/jni/TeapotNativeActivity.cpp @@ -20,11 +20,6 @@ #include #include -#include - -#include -#include - #include #include #include @@ -34,133 +29,140 @@ #include "TeapotRenderer.h" #include "NDKHelper.h" - +//------------------------------------------------------------------------- +//Preprocessor +//------------------------------------------------------------------------- +#define HELPER_CLASS_NAME "com/sample/helper/NDKHelper" //Class name of helper function //------------------------------------------------------------------------- //Shared state for our app. //------------------------------------------------------------------------- struct android_app; -class engine { - TeapotRenderer _renderer; +class Engine +{ + TeapotRenderer renderer_; - GLContext* _glContext; + ndk_helper::GLContext* gl_context_; - bool _bInitializedResources; - bool _bHasFocus; + bool initialized_resources_; + bool has_focus_; - DoubletapDetector _doubletapDetector; - PinchDetector _pinchDetector; - DragDetector _dragDetector; - perfMonitor _monitor; + ndk_helper::DoubletapDetector doubletap_detector_; + ndk_helper::PinchDetector pinch_detector_; + ndk_helper::DragDetector drag_detector_; + ndk_helper::PerfMonitor monitor_; - tapCamera _tapCamera; + ndk_helper::TapCamera tap_camera_; - android_app* _app; + android_app* app_; - ASensorManager* _sensorManager; - const ASensor* _accelerometerSensor; - ASensorEventQueue* _sensorEventQueue; + ASensorManager* sensor_manager_; + const ASensor* accelerometer_sensor_; + ASensorEventQueue* sensor_event_queue_; - void updateFPS(float fFPS); - void showUI(); - void transformPosition( vec2& vec ); + void UpdateFPS( float fFPS ); + void ShowUI(); + void TransformPosition( ndk_helper::Vec2& vec ); public: - static void handleCmd(struct android_app* app, int32_t cmd); - static int32_t handleInput( android_app* app, AInputEvent* event ); + static void HandleCmd( struct android_app* app, + int32_t cmd ); + static int32_t HandleInput( android_app* app, + AInputEvent* event ); - engine(); - ~engine(); - void setState(android_app* state); - int initDisplay(); - void loadResources(); - void unloadResources(); - void drawFrame(); - void termDisplay(); - void trimMemory(); - bool isReady(); + Engine(); + ~Engine(); + void SetState( android_app* state ); + int InitDisplay(); + void LoadResources(); + void UnloadResources(); + void DrawFrame(); + void TermDisplay(); + void TrimMemory(); + bool IsReady(); - void updatePosition( AInputEvent* event, int32_t iIndex, float& fX, float& fY); + void UpdatePosition( AInputEvent* event, + int32_t iIndex, + float& fX, + float& fY ); - void initSensors(); - void processSensors( int32_t id ); - void suspendSensors(); - void resumeSensors(); + void InitSensors(); + void ProcessSensors( int32_t id ); + void SuspendSensors(); + void ResumeSensors(); }; //------------------------------------------------------------------------- //Ctor //------------------------------------------------------------------------- -engine::engine() : - _bInitializedResources( false ), - _bHasFocus( false ), - _app( NULL ), - _sensorManager( NULL ), - _accelerometerSensor( NULL ), - _sensorEventQueue( NULL ) +Engine::Engine() : + initialized_resources_( false ), + has_focus_( false ), + app_( NULL ), + sensor_manager_( NULL ), + accelerometer_sensor_( NULL ), + sensor_event_queue_( NULL ) { - _glContext = GLContext::getInstance(); + gl_context_ = ndk_helper::GLContext::GetInstance(); } //------------------------------------------------------------------------- //Dtor //------------------------------------------------------------------------- -engine::~engine() +Engine::~Engine() { } /** * Load resources */ -void engine::loadResources() +void Engine::LoadResources() { - _renderer.init(); - _renderer.bind(&_tapCamera); + renderer_.Init(); + renderer_.Bind( &tap_camera_ ); } /** * Unload resources */ -void engine::unloadResources() +void Engine::UnloadResources() { - _renderer.unload(); + renderer_.Unload(); } /** * Initialize an EGL context for the current display. */ -int engine::initDisplay() +int Engine::InitDisplay() { - if( !_bInitializedResources ) + if( !initialized_resources_ ) { - _glContext->init( _app->window ); - loadResources(); - _bInitializedResources = true; + gl_context_->Init( app_->window ); + LoadResources(); + initialized_resources_ = true; } else { // initialize OpenGL ES and EGL - if( EGL_SUCCESS != _glContext->resume( _app->window ) ) + if( EGL_SUCCESS != gl_context_->Resume( app_->window ) ) { - unloadResources(); - loadResources(); + UnloadResources(); + LoadResources(); } } - showUI(); + ShowUI(); // Initialize GL state. - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); + glEnable( GL_CULL_FACE ); + glEnable( GL_DEPTH_TEST ); + glDepthFunc( GL_LEQUAL ); //Note that screen size might have been changed - glViewport(0, 0, - _glContext->getScreenWidth(), - _glContext->getScreenHeight() ); - _renderer.updateViewport(); + glViewport( 0, 0, gl_context_->GetScreenWidth(), gl_context_->GetScreenHeight() ); + renderer_.UpdateViewport(); - _tapCamera.setFlip(1.f, -1.f, -1.f); - _tapCamera.setPinchTransformFactor(2.f, 2.f, 8.f); + tap_camera_.SetFlip( 1.f, -1.f, -1.f ); + tap_camera_.SetPinchTransformFactor( 2.f, 2.f, 8.f ); return 0; } @@ -168,108 +170,108 @@ int engine::initDisplay() /** * Just the current frame in the display. */ -void engine::drawFrame() +void Engine::DrawFrame() { float fFPS; - if( _monitor.update(fFPS) ) + if( monitor_.Update( fFPS ) ) { - updateFPS( fFPS ); + UpdateFPS( fFPS ); } - double dTime = _monitor.getCurrentTime(); - _renderer.update(dTime); + renderer_.Update( monitor_.GetCurrentTime() ); // Just fill the screen with a color. - glClearColor(0.5f, 0.5f, 0.5f, 1.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - _renderer.render(); + glClearColor( 0.5f, 0.5f, 0.5f, 1.f ); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + renderer_.Render(); // Swap - if( EGL_SUCCESS != _glContext->swap() ) + if( EGL_SUCCESS != gl_context_->Swap() ) { - unloadResources(); - loadResources(); + UnloadResources(); + LoadResources(); } } /** * Tear down the EGL context currently associated with the display. */ -void engine::termDisplay() +void Engine::TermDisplay() { - _glContext->suspend(); + gl_context_->Suspend(); } -void engine::trimMemory() +void Engine::TrimMemory() { LOGI( "Trimming memory" ); - _glContext->invalidate(); + gl_context_->Invalidate(); } /** * Process the next input event. */ -int32_t engine::handleInput( android_app* app, AInputEvent* event ) +int32_t Engine::HandleInput( android_app* app, + AInputEvent* event ) { - engine* eng = (engine*)app->userData; - if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) + Engine* eng = (Engine*) app->userData; + if( AInputEvent_getType( event ) == AINPUT_EVENT_TYPE_MOTION ) { - GESTURE_STATE doubleTapState = eng->_doubletapDetector.detect(event); - GESTURE_STATE dragState = eng->_dragDetector.detect(event); - GESTURE_STATE pinchState = eng->_pinchDetector.detect(event); + ndk_helper::GESTURE_STATE doubleTapState = eng->doubletap_detector_.Detect( event ); + ndk_helper::GESTURE_STATE dragState = eng->drag_detector_.Detect( event ); + ndk_helper::GESTURE_STATE pinchState = eng->pinch_detector_.Detect( event ); //Double tap detector has a priority over other detectors - if( doubleTapState == GESTURE_STATE_ACTION ) - { - //Detect double tap - eng->_tapCamera.reset(true); - } - else - { - //Handle drag state - if( dragState & GESTURE_STATE_START ) - { - //Otherwise, start dragging - vec2 v; - eng->_dragDetector.getPointer( v ); - eng->transformPosition( v ); - eng->_tapCamera.beginDrag( v ); - } - else if( dragState & GESTURE_STATE_MOVE ) - { - vec2 v; - eng->_dragDetector.getPointer( v ); - eng->transformPosition( v ); - eng->_tapCamera.drag( v ); - } - else if( dragState & GESTURE_STATE_END ) - { - eng->_tapCamera.endDrag(); - } + if( doubleTapState == ndk_helper::GESTURE_STATE_ACTION ) + { + //Detect double tap + eng->tap_camera_.Reset( true ); + } + else + { + //Handle drag state + if( dragState & ndk_helper::GESTURE_STATE_START ) + { + //Otherwise, start dragging + ndk_helper::Vec2 v; + eng->drag_detector_.GetPointer( v ); + eng->TransformPosition( v ); + eng->tap_camera_.BeginDrag( v ); + } + else if( dragState & ndk_helper::GESTURE_STATE_MOVE ) + { + ndk_helper::Vec2 v; + eng->drag_detector_.GetPointer( v ); + eng->TransformPosition( v ); + eng->tap_camera_.Drag( v ); + } + else if( dragState & ndk_helper::GESTURE_STATE_END ) + { + eng->tap_camera_.EndDrag(); + } - //Handle pinch state - if( pinchState & GESTURE_STATE_START ) - { - //Start new pinch - vec2 v1; - vec2 v2; - eng->_pinchDetector.getPointers( v1, v2 ); - eng->transformPosition( v1 ); - eng->transformPosition( v2 ); - eng->_tapCamera.beginPinch( v1, v2 ); - } - else if( pinchState & GESTURE_STATE_MOVE ) - { - //Multi touch - //Start new pinch - vec2 v1; - vec2 v2; - eng->_pinchDetector.getPointers( v1, v2 ); - eng->transformPosition( v1 ); - eng->transformPosition( v2 ); - eng->_tapCamera.pinch( v1, v2 ); - } - } - return 1; + //Handle pinch state + if( pinchState & ndk_helper::GESTURE_STATE_START ) + { + //Start new pinch + ndk_helper::Vec2 v1; + ndk_helper::Vec2 v2; + eng->pinch_detector_.GetPointers( v1, v2 ); + eng->TransformPosition( v1 ); + eng->TransformPosition( v2 ); + eng->tap_camera_.BeginPinch( v1, v2 ); + } + else if( pinchState & ndk_helper::GESTURE_STATE_MOVE ) + { + //Multi touch + //Start new pinch + ndk_helper::Vec2 v1; + ndk_helper::Vec2 v2; + eng->pinch_detector_.GetPointers( v1, v2 ); + eng->TransformPosition( v1 ); + eng->TransformPosition( v2 ); + eng->tap_camera_.Pinch( v1, v2 ); + } + } + return 1; } return 0; } @@ -277,176 +279,180 @@ int32_t engine::handleInput( android_app* app, AInputEvent* event ) /** * Process the next main command. */ -void engine::handleCmd(struct android_app* app, int32_t cmd) +void Engine::HandleCmd( struct android_app* app, + int32_t cmd ) { - engine* eng = (engine*)app->userData; - switch (cmd) { - case APP_CMD_SAVE_STATE: - break; - case APP_CMD_INIT_WINDOW: - // The window is being shown, get it ready. - if (app->window != NULL) { - eng->initDisplay(); - eng->drawFrame(); - } - break; - case APP_CMD_TERM_WINDOW: - // The window is being hidden or closed, clean it up. - eng->termDisplay(); - eng->_bHasFocus = false; - break; - case APP_CMD_STOP: - break; - case APP_CMD_GAINED_FOCUS: - eng->resumeSensors(); - //Start animation - eng->_bHasFocus = true; - break; - case APP_CMD_LOST_FOCUS: - eng->suspendSensors(); - // Also stop animating. - eng->_bHasFocus = false; - eng->drawFrame(); - break; - case APP_CMD_LOW_MEMORY: - //Free up GL resources - eng->trimMemory(); - break; + Engine* eng = (Engine*) app->userData; + switch( cmd ) + { + case APP_CMD_SAVE_STATE: + break; + case APP_CMD_INIT_WINDOW: + // The window is being shown, get it ready. + if( app->window != NULL ) + { + eng->InitDisplay(); + eng->DrawFrame(); + } + break; + case APP_CMD_TERM_WINDOW: + // The window is being hidden or closed, clean it up. + eng->TermDisplay(); + eng->has_focus_ = false; + break; + case APP_CMD_STOP: + break; + case APP_CMD_GAINED_FOCUS: + eng->ResumeSensors(); + //Start animation + eng->has_focus_ = true; + break; + case APP_CMD_LOST_FOCUS: + eng->SuspendSensors(); + // Also stop animating. + eng->has_focus_ = false; + eng->DrawFrame(); + break; + case APP_CMD_LOW_MEMORY: + //Free up GL resources + eng->TrimMemory(); + break; } } //------------------------------------------------------------------------- //Sensor handlers //------------------------------------------------------------------------- -void engine::initSensors() +void Engine::InitSensors() { - _sensorManager = ASensorManager_getInstance(); - _accelerometerSensor = ASensorManager_getDefaultSensor( _sensorManager, - ASENSOR_TYPE_ACCELEROMETER); - _sensorEventQueue = ASensorManager_createEventQueue( _sensorManager, - _app->looper, LOOPER_ID_USER, NULL, NULL); + sensor_manager_ = ASensorManager_getInstance(); + accelerometer_sensor_ = ASensorManager_getDefaultSensor( sensor_manager_, + ASENSOR_TYPE_ACCELEROMETER ); + sensor_event_queue_ = ASensorManager_createEventQueue( sensor_manager_, app_->looper, + LOOPER_ID_USER, NULL, NULL ); } -void engine::processSensors( int32_t id ) +void Engine::ProcessSensors( int32_t id ) { // If a sensor has data, process it now. if( id == LOOPER_ID_USER ) { - if (_accelerometerSensor != NULL) + if( accelerometer_sensor_ != NULL ) { ASensorEvent event; - while (ASensorEventQueue_getEvents(_sensorEventQueue, - &event, 1) > 0) + while( ASensorEventQueue_getEvents( sensor_event_queue_, &event, 1 ) > 0 ) { } } } } -void engine::resumeSensors() +void Engine::ResumeSensors() { // When our app gains focus, we start monitoring the accelerometer. - if (_accelerometerSensor != NULL) { - ASensorEventQueue_enableSensor(_sensorEventQueue, - _accelerometerSensor); + if( accelerometer_sensor_ != NULL ) + { + ASensorEventQueue_enableSensor( sensor_event_queue_, accelerometer_sensor_ ); // We'd like to get 60 events per second (in us). - ASensorEventQueue_setEventRate(_sensorEventQueue, - _accelerometerSensor, (1000L/60)*1000); + ASensorEventQueue_setEventRate( sensor_event_queue_, accelerometer_sensor_, + (1000L / 60) * 1000 ); } } -void engine::suspendSensors() +void Engine::SuspendSensors() { // When our app loses focus, we stop monitoring the accelerometer. // This is to avoid consuming battery while not being used. - if (_accelerometerSensor != NULL) { - ASensorEventQueue_disableSensor(_sensorEventQueue, - _accelerometerSensor); + if( accelerometer_sensor_ != NULL ) + { + ASensorEventQueue_disableSensor( sensor_event_queue_, accelerometer_sensor_ ); } } - //------------------------------------------------------------------------- //Misc //------------------------------------------------------------------------- -void engine::setState(android_app* state) +void Engine::SetState( android_app* state ) { - _app = state; - _doubletapDetector.setConfiguration( _app->config ); - _dragDetector.setConfiguration( _app->config ); - _pinchDetector.setConfiguration( _app->config ); + app_ = state; + doubletap_detector_.SetConfiguration( app_->config ); + drag_detector_.SetConfiguration( app_->config ); + pinch_detector_.SetConfiguration( app_->config ); } -bool engine::isReady() +bool Engine::IsReady() { - if( _bHasFocus ) + if( has_focus_ ) return true; return false; } -void engine::transformPosition( vec2& vec ) +void Engine::TransformPosition( ndk_helper::Vec2& vec ) { - vec = vec2( 2.0f, 2.0f ) * vec / vec2( _glContext->getScreenWidth(), _glContext->getScreenHeight() ) - vec2( 1.f, 1.f ); + vec = ndk_helper::Vec2( 2.0f, 2.0f ) * vec + / ndk_helper::Vec2( gl_context_->GetScreenWidth(), gl_context_->GetScreenHeight() ) + - ndk_helper::Vec2( 1.f, 1.f ); } -void engine::showUI() +void Engine::ShowUI() { JNIEnv *jni; - _app->activity->vm->AttachCurrentThread(&jni, NULL); + 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 ); + jclass clazz = jni->GetObjectClass( app_->activity->clazz ); + jmethodID methodID = jni->GetMethodID( clazz, "showUI", "()V" ); + jni->CallVoidMethod( app_->activity->clazz, methodID ); - _app->activity->vm->DetachCurrentThread(); + app_->activity->vm->DetachCurrentThread(); return; } -void engine::updateFPS(float fFPS) +void Engine::UpdateFPS( float fFPS ) { JNIEnv *jni; - _app->activity->vm->AttachCurrentThread(&jni, NULL); + 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); + 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(); + app_->activity->vm->DetachCurrentThread(); return; } -engine g_engine; +Engine g_engine; /** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ -void android_main(android_app* state) +void android_main( android_app* state ) { app_dummy(); - g_engine.setState( state ); + g_engine.SetState( state ); //Init helper functions - JNIHelper::init( state->activity ); + ndk_helper::JNIHelper::Init( state->activity, HELPER_CLASS_NAME ); state->userData = &g_engine; - state->onAppCmd = engine::handleCmd; - state->onInputEvent = engine::handleInput; + state->onAppCmd = Engine::HandleCmd; + state->onInputEvent = Engine::HandleInput; #ifdef USE_NDK_PROFILER monstartup("libNativeActivity.so"); #endif // Prepare to monitor accelerometer - g_engine.initSensors(); + g_engine.InitSensors(); // loop waiting for stuff to do. - while (1) { + while( 1 ) + { // Read all pending events. int id; int events; @@ -455,28 +461,28 @@ void android_main(android_app* state) // 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 ((id = ALooper_pollAll( g_engine.isReady() ? 0 : -1, NULL, &events, - (void**)&source) ) >= 0) + while( (id = ALooper_pollAll( g_engine.IsReady() ? 0 : -1, NULL, &events, (void**) &source )) + >= 0 ) { // Process this event. - if (source != NULL) - source->process(state, source); + if( source != NULL ) + source->process( state, source ); - g_engine.processSensors( id ); + g_engine.ProcessSensors( id ); // Check if we are exiting. - if (state->destroyRequested != 0) + if( state->destroyRequested != 0 ) { - g_engine.termDisplay(); + g_engine.TermDisplay(); return; } } - if( g_engine.isReady() ) + if( g_engine.IsReady() ) { // Drawing is throttled to the screen update rate, so there // is no need to do timing here. - g_engine.drawFrame(); + g_engine.DrawFrame(); } } } diff --git a/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.cpp b/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.cpp index aa7c074a0..b7213dce3 100644 --- a/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.cpp +++ b/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.cpp @@ -18,7 +18,6 @@ // TeapotRenderer.cpp // Render a teapot //-------------------------------------------------------------------------------- - //-------------------------------------------------------------------------------- // Include files //-------------------------------------------------------------------------------- @@ -40,257 +39,248 @@ TeapotRenderer::TeapotRenderer() //-------------------------------------------------------------------------------- // Dtor //-------------------------------------------------------------------------------- -TeapotRenderer::~TeapotRenderer() { - unload(); +TeapotRenderer::~TeapotRenderer() +{ + Unload(); } -void TeapotRenderer::init() +void TeapotRenderer::Init() { //Settings - glFrontFace (GL_CCW); + glFrontFace( GL_CCW ); //Load shader - loadShaders( &_shaderParam, "Shaders/VS_ShaderPlain.vsh", "Shaders/ShaderPlain.fsh" ); + LoadShaders( &shader_param_, "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); + num_indices_ = 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; + num_vertices_ = 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 ) + TEAPOT_VERTEX* p = new TEAPOT_VERTEX[num_vertices_]; + for( int32_t i = 0; i < num_vertices_; ++i ) { - p[i].fPos[0] = teapotPositions[iIndex]; - p[i].fPos[1] = teapotPositions[iIndex+1]; - p[i].fPos[2] = teapotPositions[iIndex+2]; + p[i].pos[0] = teapotPositions[iIndex]; + p[i].pos[1] = teapotPositions[iIndex + 1]; + p[i].pos[2] = teapotPositions[iIndex + 2]; - p[i].fNormal[0] = teapotNormals[iIndex]; - p[i].fNormal[1] = teapotNormals[iIndex+1]; - p[i].fNormal[2] = teapotNormals[iIndex+2]; + p[i].normal[0] = teapotNormals[iIndex]; + p[i].normal[1] = teapotNormals[iIndex + 1]; + p[i].normal[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); + glGenBuffers( 1, &vbo_ ); + glBindBuffer( GL_ARRAY_BUFFER, vbo_ ); + glBufferData( GL_ARRAY_BUFFER, iStride * num_vertices_, p, GL_STATIC_DRAW ); + glBindBuffer( GL_ARRAY_BUFFER, 0 ); delete[] p; - updateViewport(); - _mModel = mat4::translation(0, 0, -15.f); + UpdateViewport(); + mat_model_ = ndk_helper::Mat4::Translation( 0, 0, -15.f ); - mat4 mat = mat4::rotationX(M_PI / 3); - _mModel = mat * _mModel; + ndk_helper::Mat4 mat = ndk_helper::Mat4::RotationX( M_PI / 3 ); + mat_model_ = mat * mat_model_; } -void TeapotRenderer::updateViewport() +void TeapotRenderer::UpdateViewport() { //Init Projection matrices int32_t viewport[4]; - glGetIntegerv(GL_VIEWPORT, viewport); - float fAspect = (float)viewport[2] / (float)viewport[3]; + 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); + mat_projection_ = ndk_helper::Mat4::Perspective( fAspect, 1.f, CAM_NEAR, CAM_FAR ); } -void TeapotRenderer::unload() +void TeapotRenderer::Unload() { - if (_vbo) + if( vbo_ ) { - glDeleteBuffers(1, &_vbo); - _vbo = 0; + glDeleteBuffers( 1, &vbo_ ); + vbo_ = 0; } - if (_ibo) + if( ibo_ ) { - glDeleteBuffers(1, &_ibo); - _ibo = 0; + glDeleteBuffers( 1, &ibo_ ); + ibo_ = 0; } - if (_shaderParam._program ) + if( shader_param_.program_ ) { - glDeleteProgram(_shaderParam._program); - _shaderParam._program = 0; + glDeleteProgram( shader_param_.program_ ); + shader_param_.program_ = 0; } } -void TeapotRenderer::update(float fTime) +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)); + mat_view_ = ndk_helper::Mat4::LookAt( ndk_helper::Vec3( CAM_X, CAM_Y, CAM_Z ), + ndk_helper::Vec3( 0.f, 0.f, 0.f ), ndk_helper::Vec3( 0.f, 1.f, 0.f ) ); - if( _camera ) + if( camera_ ) { - _camera->update(); - _mView = _camera->getTransformMatrix() * _mView * _camera->getRotationMatrix() * _mModel; + camera_->Update(); + mat_view_ = camera_->GetTransformMatrix() * mat_view_ + * camera_->GetRotationMatrix() * mat_model_; } else { - _mView = _mView * _mModel; + mat_view_ = mat_view_ * mat_model_; } } -void TeapotRenderer::render() +void TeapotRenderer::Render() { // // Feed Projection and Model View matrices to the shaders - mat4 mVP = _mProjection * _mView; + ndk_helper::Mat4 mat_vp = mat_projection_ * mat_view_; // Bind the VBO - glBindBuffer(GL_ARRAY_BUFFER, _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); + 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); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo_ ); - glUseProgram(_shaderParam._program); + glUseProgram( shader_param_.program_ ); - TEAPOT_MATERIALS material = { - {1.0f, 0.5f, 0.5f}, - {1.0f, 1.0f, 1.0f, 10.f}, - {0.1f, 0.1f, 0.1f}, - }; + 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( shader_param_.material_diffuse_, 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]); + glUniform4f( shader_param_.material_specular_, 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]); + glUniform3f( shader_param_.material_ambient_, 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); + glUniformMatrix4fv( shader_param_.matrix_projection_, 1, GL_FALSE, mat_vp.Ptr() ); + glUniformMatrix4fv( shader_param_.matrix_view_, 1, GL_FALSE, mat_view_.Ptr() ); + glUniform3f( shader_param_.light0_, 100.f, -200.f, -600.f ); - glDrawElements(GL_TRIANGLES, _iNumIndices, GL_UNSIGNED_SHORT, - BUFFER_OFFSET(0)); + glDrawElements( GL_TRIANGLES, num_indices_, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0) ); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindBuffer( GL_ARRAY_BUFFER, 0 ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } -bool TeapotRenderer::loadShaders(SHADER_PARAMS* params, const char* strVsh, const char* strFsh) +bool TeapotRenderer::LoadShaders( SHADER_PARAMS* params, + const char* strVsh, + const char* strFsh ) { GLuint program; - GLuint vertShader, fragShader; - char *vertShaderPathname, *fragShaderPathname; + GLuint vert_shader, frag_shader; + char *vert_shader_pathname, *frag_shader_pathname; // Create shader program program = glCreateProgram(); - LOGI("Created Shader %d", program); + 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); + if( !ndk_helper::shader::CompileShader( &vert_shader, 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); + if( !ndk_helper::shader::CompileShader( &frag_shader, GL_FRAGMENT_SHADER, strFsh ) ) + { + LOGI( "Failed to compile fragment shader" ); + glDeleteProgram( program ); return false; } // Attach vertex shader to program - glAttachShader(program, vertShader); + glAttachShader( program, vert_shader ); // Attach fragment shader to program - glAttachShader(program, fragShader); + glAttachShader( program, frag_shader ); // 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"); + 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( !ndk_helper::shader::LinkProgram( program ) ) + { + LOGI( "Failed to link program: %d", program ); - if (vertShader) { - glDeleteShader(vertShader); - vertShader = 0; + if( vert_shader ) + { + glDeleteShader( vert_shader ); + vert_shader = 0; } - if (fragShader) { - glDeleteShader(fragShader); - fragShader = 0; + if( frag_shader ) + { + glDeleteShader( frag_shader ); + frag_shader = 0; } - if (program) { - glDeleteProgram(program); + if( program ) + { + glDeleteProgram( program ); } return false; } // Get uniform locations - params->_uiMatrixP = glGetUniformLocation(program, "uPMatrix"); - params->_uiMatrixView = glGetUniformLocation(program, "uMVMatrix"); + params->matrix_projection_ = glGetUniformLocation( program, "uPMatrix" ); + params->matrix_view_ = glGetUniformLocation( program, "uMVMatrix" ); - params->_uiLight0 = glGetUniformLocation(program, "vLight0"); - params->_uiMaterialDiffuse = glGetUniformLocation(program, - "vMaterialDiffuse"); - params->_uiMaterialAmbient = glGetUniformLocation(program, - "vMaterialAmbient"); - params->_uiMaterialSpecular = glGetUniformLocation(program, - "vMaterialSpecular"); + params->light0_ = glGetUniformLocation( program, "vLight0" ); + params->material_diffuse_ = glGetUniformLocation( program, "vMaterialDiffuse" ); + params->material_ambient_ = glGetUniformLocation( program, "vMaterialAmbient" ); + params->material_specular_ = glGetUniformLocation( program, "vMaterialSpecular" ); // Release vertex and fragment shaders - if (vertShader) - glDeleteShader(vertShader); - if (fragShader) - glDeleteShader(fragShader); + if( vert_shader ) + glDeleteShader( vert_shader ); + if( frag_shader ) + glDeleteShader( frag_shader ); - params->_program = program; + params->program_ = program; return true; } -bool TeapotRenderer::bind(tapCamera* camera) +bool TeapotRenderer::Bind( ndk_helper::TapCamera* camera ) { - _camera = camera; + camera_ = camera; return true; } diff --git a/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.h b/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.h index 7d6d64676..a04f21946 100644 --- a/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.h +++ b/ndk/platforms/android-17/samples/Teapot/jni/TeapotRenderer.h @@ -43,62 +43,62 @@ #include "NDKHelper.h" - #define BUFFER_OFFSET(i) ((char *)NULL + (i)) struct TEAPOT_VERTEX { - float fPos[3]; - float fNormal[3]; + float pos[3]; + float normal[3]; }; -enum SHADER_ATTRIBUTES { +enum SHADER_ATTRIBUTES +{ ATTRIB_VERTEX, ATTRIB_NORMAL, ATTRIB_UV, }; struct SHADER_PARAMS { - GLuint _program; - GLuint _uiLight0; - GLuint _uiMaterialDiffuse; - GLuint _uiMaterialAmbient; - GLuint _uiMaterialSpecular; + GLuint program_; + GLuint light0_; + GLuint material_diffuse_; + GLuint material_ambient_; + GLuint material_specular_; - GLuint _uiMatrixP; - GLuint _uiMatrixView; + GLuint matrix_projection_; + GLuint matrix_view_; }; struct TEAPOT_MATERIALS { - float diffuse_color[ 3 ]; - float specular_color[ 4 ]; - float ambient_color[ 3 ]; + float diffuse_color[3]; + float specular_color[4]; + float ambient_color[3]; }; class TeapotRenderer { - int32_t _iNumIndices; - int32_t _iNumVertices; - GLuint _ibo; - GLuint _vbo; + int32_t num_indices_; + int32_t num_vertices_; + GLuint ibo_; + GLuint vbo_; - SHADER_PARAMS _shaderParam; - bool loadShaders(SHADER_PARAMS* params, const char* strVsh, const char* strFsh); + SHADER_PARAMS shader_param_; + bool LoadShaders( SHADER_PARAMS* params, const char* strVsh, const char* strFsh ); - mat4 _mProjection; - mat4 _mView; - mat4 _mModel; + ndk_helper::Mat4 mat_projection_; + ndk_helper::Mat4 mat_view_; + ndk_helper::Mat4 mat_model_; - tapCamera* _camera; + ndk_helper::TapCamera* camera_; public: TeapotRenderer(); virtual ~TeapotRenderer(); - void init(); - void render(); - void update(float dTime); - bool bind(tapCamera* camera); - void unload(); - void updateViewport(); + void Init(); + void Render(); + void Update( float dTime ); + bool Bind( ndk_helper::TapCamera* camera ); + void Unload(); + void UpdateViewport(); }; #endif diff --git a/ndk/platforms/android-17/samples/Teapot/src/com/sample/helper/NDKHelper.java b/ndk/platforms/android-17/samples/Teapot/src/com/sample/helper/NDKHelper.java index 47f4b668e..3385a5d98 100644 --- a/ndk/platforms/android-17/samples/Teapot/src/com/sample/helper/NDKHelper.java +++ b/ndk/platforms/android-17/samples/Teapot/src/com/sample/helper/NDKHelper.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + package com.sample.helper; import java.io.File; @@ -10,35 +26,39 @@ import android.content.pm.ApplicationInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; +import android.media.AudioManager; +import android.media.AudioTrack; import android.opengl.GLUtils; import android.util.Log; - -public class NDKHelper { +public class NDKHelper +{ private static Context context; - public static void setCotext( Context c ) + public static void setContext(Context c) { - Log.i("NDKHelper", "set context:" + c); + Log.i("NDKHelper", "setContext:" + c); context = c; } // - //Load Bitmap - //Java helper is useful decoding PNG, TIFF etc rather than linking libPng etc separately + // Load Bitmap + // Java helper is useful decoding PNG, TIFF etc rather than linking libPng + // etc separately // private int nextPOT(int i) { int pot = 1; - while( pot < i ) pot <<= 1; + while (pot < i) + pot <<= 1; return pot; } private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) { - if(bitmapToScale == null) + if (bitmapToScale == null) return null; - //get the original width and height + // get the original width and height int width = bitmapToScale.getWidth(); int height = bitmapToScale.getHeight(); // create a matrix for the manipulation @@ -48,10 +68,11 @@ public class NDKHelper { 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); + return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), + bitmapToScale.getHeight(), matrix, true); } - public void loadTexture(String path) + public boolean loadTexture(String path) { Bitmap bitmap = null; try @@ -63,31 +84,33 @@ public class NDKHelper { } File file = new File(context.getExternalFilesDir(null), str); - if( file.canRead() ) + if (file.canRead()) { bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); - } - else + } else { - bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path)); + bitmap = BitmapFactory.decodeStream(context.getResources().getAssets() + .open(path)); } - // Matrix matrix = new Matrix(); - // // resize the bit map - // matrix.postScale(-1F, 1F); + // Matrix matrix = new Matrix(); + // // resize the bit map + // matrix.postScale(-1F, 1F); // - // // recreate the new Bitmap and set it back - // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + // // recreate the new Bitmap and set it back + // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), + // bitmap.getHeight(), matrix, true); - } - catch (Exception e) { + } catch (Exception e) + { Log.w("NDKHelper", "Coundn't load a file:" + path); + return false; } - if( bitmap != null ) + if (bitmap != null) { GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); } - return; + return true; } @@ -96,30 +119,38 @@ public class NDKHelper { Bitmap bitmap = null; try { - bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path)); - if( iScalePOT ) + 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 ) + if (originalWidth != width || originalHeight != height) { - //Scale it - bitmap = scaleBitmap( bitmap, width, height ); + // Scale it + bitmap = scaleBitmap(bitmap, width, height); } } - } - catch (Exception e) { + } catch (Exception e) + { Log.w("NDKHelper", "Coundn't load a file:" + path); } return bitmap; } - public int getBitmapWidth(Bitmap bmp) { return bmp.getWidth(); } - public int getBitmapHeight(Bitmap bmp) { return bmp.getHeight(); } + public int getBitmapWidth(Bitmap bmp) + { + return bmp.getWidth(); + } + + public int getBitmapHeight(Bitmap bmp) + { + return bmp.getHeight(); + } public void getBitmapPixels(Bitmap bmp, int[] pixels) { @@ -133,15 +164,40 @@ public class NDKHelper { bmp.recycle(); } - public static String getNativeLibraryDirectory(Context appContext) { + public static String getNativeLibraryDirectory(Context appContext) + { ApplicationInfo ai = context.getApplicationInfo(); - Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir ); + Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir); - if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 || - (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { + if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 + || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) + { return ai.nativeLibraryDir; } return "/system/lib/"; } + + public int getNativeAudioBufferSize() + { + int SDK_INT = android.os.Build.VERSION.SDK_INT; + if (SDK_INT >= 17) + { + AudioManager am = (AudioManager) context + .getSystemService(Context.AUDIO_SERVICE); + String framesPerBuffer = am + .getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER); + return Integer.parseInt(framesPerBuffer); + } else + { + return 0; + } + } + + public int getNativeAudioSampleRate() + { + return AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM); + + } + } diff --git a/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotApplication.java b/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotApplication.java index 4a80b6eee..3b24bc439 100644 --- a/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotApplication.java +++ b/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotApplication.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + package com.sample.teapot; import javax.microedition.khronos.opengles.GL10; diff --git a/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotNativeActivity.java b/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotNativeActivity.java index 033beeb77..f6a2e339a 100644 --- a/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotNativeActivity.java +++ b/ndk/platforms/android-17/samples/Teapot/src/com/sample/teapot/TeapotNativeActivity.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + package com.sample.teapot; import android.app.NativeActivity; @@ -32,7 +48,6 @@ public class TeapotNativeActivity extends NativeActivity { } } - // Our popup window, you will call it from your C/C++ code later TeapotNativeActivity _activity; @@ -72,6 +87,15 @@ public class TeapotNativeActivity extends NativeActivity { }}); } + protected void onPause() + { + super.onPause(); + if (_popupWindow != null) { + _popupWindow.dismiss(); + _popupWindow = null; + } + } + public void updateFPS(final float fFPS) { if( _label == null ) diff --git a/ndk/platforms/android-18/samples/MoreTeapots/jni/Android.mk b/ndk/platforms/android-18/samples/MoreTeapots/jni/Android.mk index 651a2cfd3..b53ccb042 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/jni/Android.mk +++ b/ndk/platforms/android-18/samples/MoreTeapots/jni/Android.mk @@ -10,10 +10,10 @@ LOCAL_C_INCLUDES := LOCAL_CFLAGS := LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue helper +LOCAL_STATIC_LIBRARIES := cpufeatures android_native_app_glue ndk_helper include $(BUILD_SHARED_LIBRARY) -$(call import-module,android/helper) +$(call import-module,android/ndk_helper) $(call import-module,android/native_app_glue) $(call import-module,android/cpufeatures) \ No newline at end of file diff --git a/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsNativeActivity.cpp b/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsNativeActivity.cpp index 3202a9259..71924fafa 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsNativeActivity.cpp +++ b/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsNativeActivity.cpp @@ -32,6 +32,13 @@ #include "MoreTeapotsRenderer.h" +//------------------------------------------------------------------------- +//Preprocessor +//------------------------------------------------------------------------- +#define HELPER_CLASS_NAME "com/sample/helper/NDKHelper" //Class name of helper function +//------------------------------------------------------------------------- +//Constants +//------------------------------------------------------------------------- const int32_t NUM_TEAPOTS_X = 8; const int32_t NUM_TEAPOTS_Y = 8; const int32_t NUM_TEAPOTS_Z = 6; @@ -40,128 +47,132 @@ const int32_t NUM_TEAPOTS_Z = 6; //Shared state for our app. //------------------------------------------------------------------------- struct android_app; -class engine { - MoreTeapotsRenderer _renderer; +class Engine +{ + MoreTeapotsRenderer renderer_; - GLContext* _glContext; + ndk_helper::GLContext* gl_context_; - bool _bInitializedResources; - bool _bHasFocus; + bool initialized_resources_; + bool has_focus_; - DoubletapDetector _doubletapDetector; - PinchDetector _pinchDetector; - DragDetector _dragDetector; - perfMonitor _monitor; + ndk_helper::DoubletapDetector doubletap_detector_; + ndk_helper::PinchDetector pinch_detector_; + ndk_helper::DragDetector drag_detector_; + ndk_helper::PerfMonitor monitor_; - tapCamera _tapCamera; + ndk_helper::TapCamera tap_camera_; - android_app* _app; + android_app* app_; - ASensorManager* _sensorManager; - const ASensor* _accelerometerSensor; - ASensorEventQueue* _sensorEventQueue; + ASensorManager* sensor_manager_; + const ASensor* accelerometer_sensor_; + ASensorEventQueue* sensor_event_queue_; - void updateFPS(float fFPS); - void showUI(); - void transformPosition( vec2& vec ); + void UpdateFPS( float fps ); + void ShowUI(); + void TransformPosition( ndk_helper::Vec2& vec ); public: - static void handleCmd(struct android_app* app, int32_t cmd); - static int32_t handleInput( android_app* app, AInputEvent* event ); + static void HandleCmd( struct android_app* app, + int32_t cmd ); + static int32_t HandleInput( android_app* app, + AInputEvent* event ); - engine(); - ~engine(); - void setState(android_app* state); - int initDisplay(); - void loadResources(); - void unloadResources(); - void drawFrame(); - void termDisplay(); - void trimMemory(); - bool isReady(); + Engine(); + ~Engine(); + void SetState( android_app* state ); + int InitDisplay(); + void LoadResources(); + void UnloadResources(); + void DrawFrame(); + void TermDisplay(); + void TrimMemory(); + bool IsReady(); - void updatePosition( AInputEvent* event, int32_t iIndex, float& fX, float& fY); + void UpdatePosition( AInputEvent* event, + int32_t index, + float& x, + float& y ); - void initSensors(); - void processSensors( int32_t id ); - void suspendSensors(); - void resumeSensors(); + void InitSensors(); + void ProcessSensors( int32_t id ); + void SuspendSensors(); + void ResumeSensors(); }; //------------------------------------------------------------------------- //Ctor //------------------------------------------------------------------------- -engine::engine() : - _bInitializedResources( false ), - _bHasFocus( false ), - _app( NULL ), - _sensorManager( NULL ), - _accelerometerSensor( NULL ), - _sensorEventQueue( NULL ) +Engine::Engine() : + initialized_resources_( false ), + has_focus_( false ), + app_( NULL ), + sensor_manager_( NULL ), + accelerometer_sensor_( NULL ), + sensor_event_queue_( NULL ) { - _glContext = GLContext::getInstance(); + gl_context_ = ndk_helper::GLContext::GetInstance(); } //------------------------------------------------------------------------- //Dtor //------------------------------------------------------------------------- -engine::~engine() +Engine::~Engine() { } /** * Load resources */ -void engine::loadResources() +void Engine::LoadResources() { - _renderer.init(NUM_TEAPOTS_X, NUM_TEAPOTS_Y, NUM_TEAPOTS_Z); - _renderer.bind(&_tapCamera); + renderer_.Init( NUM_TEAPOTS_X, NUM_TEAPOTS_Y, NUM_TEAPOTS_Z ); + renderer_.Bind( &tap_camera_ ); } /** * Unload resources */ -void engine::unloadResources() +void Engine::UnloadResources() { - _renderer.unload(); + renderer_.Unload(); } /** * Initialize an EGL context for the current display. */ -int engine::initDisplay() +int Engine::InitDisplay() { - if( !_bInitializedResources ) + if( !initialized_resources_ ) { - _glContext->init( _app->window ); - loadResources(); - _bInitializedResources = true; + gl_context_->Init( app_->window ); + LoadResources(); + initialized_resources_ = true; } else { // initialize OpenGL ES and EGL - if( EGL_SUCCESS != _glContext->resume( _app->window ) ) + if( EGL_SUCCESS != gl_context_->Resume( app_->window ) ) { - unloadResources(); - loadResources(); + UnloadResources(); + LoadResources(); } } - showUI(); + ShowUI(); // Initialize GL state. - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); + glEnable( GL_CULL_FACE ); + glEnable( GL_DEPTH_TEST ); + glDepthFunc( GL_LEQUAL ); //Note that screen size might have been changed - glViewport(0, 0, - _glContext->getScreenWidth(), - _glContext->getScreenHeight() ); - _renderer.updateViewport(); + glViewport( 0, 0, gl_context_->GetScreenWidth(), gl_context_->GetScreenHeight() ); + renderer_.UpdateViewport(); - _tapCamera.setFlip(1.f, -1.f, -1.f); - _tapCamera.setPinchTransformFactor(10.f, 10.f, 8.f); + tap_camera_.SetFlip( 1.f, -1.f, -1.f ); + tap_camera_.SetPinchTransformFactor( 10.f, 10.f, 8.f ); return 0; } @@ -169,108 +180,109 @@ int engine::initDisplay() /** * Just the current frame in the display. */ -void engine::drawFrame() +void Engine::DrawFrame() { - float fFPS; - if( _monitor.update(fFPS) ) + float fps; + if( monitor_.Update( fps ) ) { - updateFPS( fFPS ); + UpdateFPS( fps ); } - double dTime = _monitor.getCurrentTime(); - _renderer.update(dTime); + double dTime = monitor_.GetCurrentTime(); + renderer_.Update( dTime ); // Just fill the screen with a color. - glClearColor(0.5f, 0.5f, 0.5f, 1.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - _renderer.render(); + glClearColor( 0.5f, 0.5f, 0.5f, 1.f ); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + renderer_.Render(); // Swap - if( EGL_SUCCESS != _glContext->swap() ) + if( EGL_SUCCESS != gl_context_->Swap() ) { - unloadResources(); - loadResources(); + UnloadResources(); + LoadResources(); } } /** * Tear down the EGL context currently associated with the display. */ -void engine::termDisplay() +void Engine::TermDisplay() { - _glContext->suspend(); + gl_context_->Suspend(); } -void engine::trimMemory() +void Engine::TrimMemory() { LOGI( "Trimming memory" ); - _glContext->invalidate(); + gl_context_->Invalidate(); } /** * Process the next input event. */ -int32_t engine::handleInput( android_app* app, AInputEvent* event ) +int32_t Engine::HandleInput( android_app* app, + AInputEvent* event ) { - engine* eng = (engine*)app->userData; - if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) + Engine* eng = (Engine*) app->userData; + if( AInputEvent_getType( event ) == AINPUT_EVENT_TYPE_MOTION ) { - GESTURE_STATE doubleTapState = eng->_doubletapDetector.detect(event); - GESTURE_STATE dragState = eng->_dragDetector.detect(event); - GESTURE_STATE pinchState = eng->_pinchDetector.detect(event); + ndk_helper::GESTURE_STATE doubleTapState = eng->doubletap_detector_.Detect( event ); + ndk_helper::GESTURE_STATE dragState = eng->drag_detector_.Detect( event ); + ndk_helper::GESTURE_STATE pinchState = eng->pinch_detector_.Detect( event ); //Double tap detector has a priority over other detectors - if( doubleTapState == GESTURE_STATE_ACTION ) - { - //Detect double tap - eng->_tapCamera.reset(true); - } - else - { - //Handle drag state - if( dragState & GESTURE_STATE_START ) - { - //Otherwise, start dragging - vec2 v; - eng->_dragDetector.getPointer( v ); - eng->transformPosition( v ); - eng->_tapCamera.beginDrag( v ); - } - else if( dragState & GESTURE_STATE_MOVE ) - { - vec2 v; - eng->_dragDetector.getPointer( v ); - eng->transformPosition( v ); - eng->_tapCamera.drag( v ); - } - else if( dragState & GESTURE_STATE_END ) - { - eng->_tapCamera.endDrag(); - } + if( doubleTapState == ndk_helper::GESTURE_STATE_ACTION ) + { + //Detect double tap + eng->tap_camera_.Reset( true ); + } + else + { + //Handle drag state + if( dragState & ndk_helper::GESTURE_STATE_START ) + { + //Otherwise, start dragging + ndk_helper::Vec2 v; + eng->drag_detector_.GetPointer( v ); + eng->TransformPosition( v ); + eng->tap_camera_.BeginDrag( v ); + } + else if( dragState & ndk_helper::GESTURE_STATE_MOVE ) + { + ndk_helper::Vec2 v; + eng->drag_detector_.GetPointer( v ); + eng->TransformPosition( v ); + eng->tap_camera_.Drag( v ); + } + else if( dragState & ndk_helper::GESTURE_STATE_END ) + { + eng->tap_camera_.EndDrag(); + } - //Handle pinch state - if( pinchState & GESTURE_STATE_START ) - { - //Start new pinch - vec2 v1; - vec2 v2; - eng->_pinchDetector.getPointers( v1, v2 ); - eng->transformPosition( v1 ); - eng->transformPosition( v2 ); - eng->_tapCamera.beginPinch( v1, v2 ); - } - else if( pinchState & GESTURE_STATE_MOVE ) - { - //Multi touch - //Start new pinch - vec2 v1; - vec2 v2; - eng->_pinchDetector.getPointers( v1, v2 ); - eng->transformPosition( v1 ); - eng->transformPosition( v2 ); - eng->_tapCamera.pinch( v1, v2 ); - } - } - return 1; + //Handle pinch state + if( pinchState & ndk_helper::GESTURE_STATE_START ) + { + //Start new pinch + ndk_helper::Vec2 v1; + ndk_helper::Vec2 v2; + eng->pinch_detector_.GetPointers( v1, v2 ); + eng->TransformPosition( v1 ); + eng->TransformPosition( v2 ); + eng->tap_camera_.BeginPinch( v1, v2 ); + } + else if( pinchState & ndk_helper::GESTURE_STATE_MOVE ) + { + //Multi touch + //Start new pinch + ndk_helper::Vec2 v1; + ndk_helper::Vec2 v2; + eng->pinch_detector_.GetPointers( v1, v2 ); + eng->TransformPosition( v1 ); + eng->TransformPosition( v2 ); + eng->tap_camera_.Pinch( v1, v2 ); + } + } + return 1; } return 0; } @@ -278,176 +290,180 @@ int32_t engine::handleInput( android_app* app, AInputEvent* event ) /** * Process the next main command. */ -void engine::handleCmd(struct android_app* app, int32_t cmd) +void Engine::HandleCmd( struct android_app* app, + int32_t cmd ) { - engine* eng = (engine*)app->userData; - switch (cmd) { - case APP_CMD_SAVE_STATE: - break; - case APP_CMD_INIT_WINDOW: - // The window is being shown, get it ready. - if (app->window != NULL) { - eng->initDisplay(); - eng->drawFrame(); - } - break; - case APP_CMD_TERM_WINDOW: - // The window is being hidden or closed, clean it up. - eng->termDisplay(); - eng->_bHasFocus = false; - break; - case APP_CMD_STOP: - break; - case APP_CMD_GAINED_FOCUS: - eng->resumeSensors(); - //Start animation - eng->_bHasFocus = true; - break; - case APP_CMD_LOST_FOCUS: - eng->suspendSensors(); - // Also stop animating. - eng->_bHasFocus = false; - eng->drawFrame(); - break; - case APP_CMD_LOW_MEMORY: - //Free up GL resources - eng->trimMemory(); - break; + Engine* eng = (Engine*) app->userData; + switch( cmd ) + { + case APP_CMD_SAVE_STATE: + break; + case APP_CMD_INIT_WINDOW: + // The window is being shown, get it ready. + if( app->window != NULL ) + { + eng->InitDisplay(); + eng->DrawFrame(); + } + break; + case APP_CMD_TERM_WINDOW: + // The window is being hidden or closed, clean it up. + eng->TermDisplay(); + eng->has_focus_ = false; + break; + case APP_CMD_STOP: + break; + case APP_CMD_GAINED_FOCUS: + eng->ResumeSensors(); + //Start animation + eng->has_focus_ = true; + break; + case APP_CMD_LOST_FOCUS: + eng->SuspendSensors(); + // Also stop animating. + eng->has_focus_ = false; + eng->DrawFrame(); + break; + case APP_CMD_LOW_MEMORY: + //Free up GL resources + eng->TrimMemory(); + break; } } //------------------------------------------------------------------------- //Sensor handlers //------------------------------------------------------------------------- -void engine::initSensors() +void Engine::InitSensors() { - _sensorManager = ASensorManager_getInstance(); - _accelerometerSensor = ASensorManager_getDefaultSensor( _sensorManager, - ASENSOR_TYPE_ACCELEROMETER); - _sensorEventQueue = ASensorManager_createEventQueue( _sensorManager, - _app->looper, LOOPER_ID_USER, NULL, NULL); + sensor_manager_ = ASensorManager_getInstance(); + accelerometer_sensor_ = ASensorManager_getDefaultSensor( sensor_manager_, + ASENSOR_TYPE_ACCELEROMETER ); + sensor_event_queue_ = ASensorManager_createEventQueue( sensor_manager_, app_->looper, + LOOPER_ID_USER, NULL, NULL ); } -void engine::processSensors( int32_t id ) +void Engine::ProcessSensors( int32_t id ) { // If a sensor has data, process it now. if( id == LOOPER_ID_USER ) { - if (_accelerometerSensor != NULL) + if( accelerometer_sensor_ != NULL ) { ASensorEvent event; - while (ASensorEventQueue_getEvents(_sensorEventQueue, - &event, 1) > 0) + while( ASensorEventQueue_getEvents( sensor_event_queue_, &event, 1 ) > 0 ) { } } } } -void engine::resumeSensors() +void Engine::ResumeSensors() { // When our app gains focus, we start monitoring the accelerometer. - if (_accelerometerSensor != NULL) { - ASensorEventQueue_enableSensor(_sensorEventQueue, - _accelerometerSensor); + if( accelerometer_sensor_ != NULL ) + { + ASensorEventQueue_enableSensor( sensor_event_queue_, accelerometer_sensor_ ); // We'd like to get 60 events per second (in us). - ASensorEventQueue_setEventRate(_sensorEventQueue, - _accelerometerSensor, (1000L/60)*1000); + ASensorEventQueue_setEventRate( sensor_event_queue_, accelerometer_sensor_, + (1000L / 60) * 1000 ); } } -void engine::suspendSensors() +void Engine::SuspendSensors() { // When our app loses focus, we stop monitoring the accelerometer. // This is to avoid consuming battery while not being used. - if (_accelerometerSensor != NULL) { - ASensorEventQueue_disableSensor(_sensorEventQueue, - _accelerometerSensor); + if( accelerometer_sensor_ != NULL ) + { + ASensorEventQueue_disableSensor( sensor_event_queue_, accelerometer_sensor_ ); } } - //------------------------------------------------------------------------- //Misc //------------------------------------------------------------------------- -void engine::setState(android_app* state) +void Engine::SetState( android_app* state ) { - _app = state; - _doubletapDetector.setConfiguration( _app->config ); - _dragDetector.setConfiguration( _app->config ); - _pinchDetector.setConfiguration( _app->config ); + app_ = state; + doubletap_detector_.SetConfiguration( app_->config ); + drag_detector_.SetConfiguration( app_->config ); + pinch_detector_.SetConfiguration( app_->config ); } -bool engine::isReady() +bool Engine::IsReady() { - if( _bHasFocus ) + if( has_focus_ ) return true; return false; } -void engine::transformPosition( vec2& vec ) +void Engine::TransformPosition( ndk_helper::Vec2& vec ) { - vec = vec2( 2.0f, 2.0f ) * vec / vec2( _glContext->getScreenWidth(), _glContext->getScreenHeight() ) - vec2( 1.f, 1.f ); + vec = ndk_helper::Vec2( 2.0f, 2.0f ) * vec + / ndk_helper::Vec2( gl_context_->GetScreenWidth(), gl_context_->GetScreenHeight() ) + - ndk_helper::Vec2( 1.f, 1.f ); } -void engine::showUI() +void Engine::ShowUI() { JNIEnv *jni; - _app->activity->vm->AttachCurrentThread(&jni, NULL); + 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 ); + jclass clazz = jni->GetObjectClass( app_->activity->clazz ); + jmethodID methodID = jni->GetMethodID( clazz, "showUI", "()V" ); + jni->CallVoidMethod( app_->activity->clazz, methodID ); - _app->activity->vm->DetachCurrentThread(); + app_->activity->vm->DetachCurrentThread(); return; } -void engine::updateFPS(float fFPS) +void Engine::UpdateFPS( float fps ) { JNIEnv *jni; - _app->activity->vm->AttachCurrentThread(&jni, NULL); + 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); + jclass clazz = jni->GetObjectClass( app_->activity->clazz ); + jmethodID methodID = jni->GetMethodID( clazz, "updateFPS", "(F)V" ); + jni->CallVoidMethod( app_->activity->clazz, methodID, fps ); - _app->activity->vm->DetachCurrentThread(); + app_->activity->vm->DetachCurrentThread(); return; } -engine g_engine; +Engine g_engine; /** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ -void android_main(android_app* state) +void android_main( android_app* state ) { app_dummy(); - g_engine.setState( state ); + g_engine.SetState( state ); //Init helper functions - JNIHelper::init( state->activity ); + ndk_helper::JNIHelper::GetInstance()->Init( state->activity, HELPER_CLASS_NAME ); state->userData = &g_engine; - state->onAppCmd = engine::handleCmd; - state->onInputEvent = engine::handleInput; + state->onAppCmd = Engine::HandleCmd; + state->onInputEvent = Engine::HandleInput; #ifdef USE_NDK_PROFILER monstartup("libNativeActivity.so"); #endif // Prepare to monitor accelerometer - g_engine.initSensors(); + g_engine.InitSensors(); // loop waiting for stuff to do. - while (1) { + while( 1 ) + { // Read all pending events. int id; int events; @@ -456,28 +472,28 @@ void android_main(android_app* state) // 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 ((id = ALooper_pollAll( g_engine.isReady() ? 0 : -1, NULL, &events, - (void**)&source) ) >= 0) + while( (id = ALooper_pollAll( g_engine.IsReady() ? 0 : -1, NULL, &events, (void**) &source )) + >= 0 ) { // Process this event. - if (source != NULL) - source->process(state, source); + if( source != NULL ) + source->process( state, source ); - g_engine.processSensors( id ); + g_engine.ProcessSensors( id ); // Check if we are exiting. - if (state->destroyRequested != 0) + if( state->destroyRequested != 0 ) { - g_engine.termDisplay(); + g_engine.TermDisplay(); return; } } - if( g_engine.isReady() ) + if( g_engine.IsReady() ) { // Drawing is throttled to the screen update rate, so there // is no need to do timing here. - g_engine.drawFrame(); + g_engine.DrawFrame(); } } } diff --git a/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.cpp b/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.cpp index 4a4fb2c53..4adf4ed5e 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.cpp +++ b/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.cpp @@ -18,7 +18,6 @@ // MoreTeapotsRenderer.cpp // Render teapots //-------------------------------------------------------------------------------- - //-------------------------------------------------------------------------------- // Include files //-------------------------------------------------------------------------------- @@ -32,7 +31,8 @@ //-------------------------------------------------------------------------------- // Ctor //-------------------------------------------------------------------------------- -MoreTeapotsRenderer::MoreTeapotsRenderer() : _bGeometryInstancingSupport( false ) +MoreTeapotsRenderer::MoreTeapotsRenderer() : + geometry_instancing_support_( false ) { } @@ -40,21 +40,25 @@ MoreTeapotsRenderer::MoreTeapotsRenderer() : _bGeometryInstancingSupport( false //-------------------------------------------------------------------------------- // Dtor //-------------------------------------------------------------------------------- -MoreTeapotsRenderer::~MoreTeapotsRenderer() { - unload(); +MoreTeapotsRenderer::~MoreTeapotsRenderer() +{ + Unload(); } //-------------------------------------------------------------------------------- // Init //-------------------------------------------------------------------------------- -void MoreTeapotsRenderer::init( const int32_t numX, const int32_t numY, const int32_t numZ ) +void MoreTeapotsRenderer::Init( const int32_t numX, + const int32_t numY, + const int32_t numZ ) { - if( GLContext::getInstance()->getGLVersion() >= 3.0 ) + if( ndk_helper::GLContext::GetInstance()->GetGLVersion() >= 3.0 ) { - _bGeometryInstancingSupport = true; + geometry_instancing_support_ = true; } - else if( GLContext::getInstance()->checkExtension("GL_NV_draw_instanced") - && GLContext::getInstance()->checkExtension("GL_NV_uniform_buffer_object") ) + else if( ndk_helper::GLContext::GetInstance()->CheckExtension( "GL_NV_draw_instanced" ) + && ndk_helper::GLContext::GetInstance()->CheckExtension( + "GL_NV_uniform_buffer_object" ) ) { LOGI( "Supported via extension!" ); //_bGeometryInstancingSupport = true; @@ -63,92 +67,87 @@ void MoreTeapotsRenderer::init( const int32_t numX, const int32_t numY, const in } //Settings - glFrontFace (GL_CCW); + glFrontFace( GL_CCW ); //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); + num_indices_ = 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; + num_vertices_ = 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 ) + TEAPOT_VERTEX* p = new TEAPOT_VERTEX[num_vertices_]; + for( int32_t i = 0; i < num_vertices_; ++i ) { - p[i].fPos[0] = teapotPositions[iIndex]; - p[i].fPos[1] = teapotPositions[iIndex+1]; - p[i].fPos[2] = teapotPositions[iIndex+2]; + p[i].pos[0] = teapotPositions[iIndex]; + p[i].pos[1] = teapotPositions[iIndex + 1]; + p[i].pos[2] = teapotPositions[iIndex + 2]; - p[i].fNormal[0] = teapotNormals[iIndex]; - p[i].fNormal[1] = teapotNormals[iIndex+1]; - p[i].fNormal[2] = teapotNormals[iIndex+2]; + p[i].normal[0] = teapotNormals[iIndex]; + p[i].normal[1] = teapotNormals[iIndex + 1]; + p[i].normal[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); + glGenBuffers( 1, &vbo_ ); + glBindBuffer( GL_ARRAY_BUFFER, vbo_ ); + glBufferData( GL_ARRAY_BUFFER, iStride * num_vertices_, p, GL_STATIC_DRAW ); + glBindBuffer( GL_ARRAY_BUFFER, 0 ); delete[] p; //Init Projection matrices - _iX = numX; - _iY = numY; - _iZ = numZ; - _mModels.reserve( _iX * _iY * _iZ ); + teapot_x_ = numX; + teapot_y_ = numY; + teapot_z_ = numZ; + vec_mat_models_.reserve( teapot_x_ * teapot_y_ * teapot_z_ ); - updateViewport(); + UpdateViewport(); - float fTotalWidth = 500.f; - float fGapX = fTotalWidth / (_iX - 1); - float fGapY = fTotalWidth / (_iY - 1); - float fGapZ = fTotalWidth / (_iZ - 1); - float fOffsetX = -fTotalWidth/ 2.f; - float fOffsetY = -fTotalWidth/ 2.f; - float fOffsetZ = -fTotalWidth/ 2.f; + const float total_width = 500.f; + float gap_x = total_width / (teapot_x_ - 1); + float gap_y = total_width / (teapot_y_ - 1); + float gap_z = total_width / (teapot_z_ - 1); + float offset_x = -total_width / 2.f; + float offset_y = -total_width / 2.f; + float offset_z = -total_width / 2.f; - for( int32_t iX = 0; iX < _iX; ++iX ) - for( int32_t iY = 0; iY < _iY; ++iY ) - for( int32_t iZ = 0; iZ < _iZ; ++iZ ) + for( int32_t iX = 0; iX < teapot_x_; ++iX ) + for( int32_t iY = 0; iY < teapot_y_; ++iY ) + for( int32_t iZ = 0; iZ < teapot_z_; ++iZ ) { - _mModels.push_back( mat4::translation( iX * fGapX + fOffsetX, - iY * fGapY + fOffsetY, - iZ * fGapZ + fOffsetZ) ); - _mColors.push_back( - vec3( random() / float(RAND_MAX * 1.1), - random() / float(RAND_MAX * 1.1), - random() / float(RAND_MAX * 1.1) - ) ); + vec_mat_models_.push_back( + ndk_helper::Mat4::Translation( iX * gap_x + offset_x, iY * gap_y + offset_y, + iZ * gap_z + offset_z ) ); + vec_colors_.push_back( + ndk_helper::Vec3( random() / float( RAND_MAX * 1.1 ), + random() / float( RAND_MAX * 1.1 ), + random() / float( RAND_MAX * 1.1 ) ) ); - float fX = random() / float(RAND_MAX) - 0.5f; - float fY = random() / float(RAND_MAX) - 0.5f; - _mVecRotation.push_back( - vec2( fX * 0.05f, fY * 0.05f ) ); - _mCurrentRotation.push_back( - vec2( fX * M_PI, fY * M_PI ) ); + float fX = random() / float( RAND_MAX ) - 0.5f; + float fY = random() / float( RAND_MAX ) - 0.5f; + vec_rotations_.push_back( ndk_helper::Vec2( fX * 0.05f, fY * 0.05f ) ); + vec_current_rotations_.push_back( ndk_helper::Vec2( fX * M_PI, fY * M_PI ) ); } - if( _bGeometryInstancingSupport ) + if( geometry_instancing_support_ ) { // //Create parameter dictionary for shader patch - std::map< std::string, std::string> param; - param[ std::string( "%NUM_TEAPOT%" ) ] = toString( _iX * _iY * _iZ ); - param[ std::string( "%LOCATION_VERTEX%" ) ] = toString( ATTRIB_VERTEX ); - param[ std::string( "%LOCATION_NORMAL%" ) ] = toString( ATTRIB_NORMAL ); - if( _bARBSupport ) - param[ std::string( "%ARB%" ) ] = std::string( "ARB" ); + std::map param; + param[std::string( "%NUM_TEAPOT%" )] = ToString( teapot_x_ * teapot_y_ * teapot_z_ ); + param[std::string( "%LOCATION_VERTEX%" )] = ToString( ATTRIB_VERTEX ); + param[std::string( "%LOCATION_NORMAL%" )] = ToString( ATTRIB_NORMAL ); + if( arb_support_ ) + param[std::string( "%ARB%" )] = std::string( "ARB" ); else - param[ std::string( "%ARB%" ) ] = std::string( "" ); + param[std::string( "%ARB%" )] = std::string( "" ); //Load shader - bool b = loadShadersES3( &_shaderParam, "Shaders/VS_ShaderPlainES3.vsh", "Shaders/ShaderPlainES3.fsh", param ); + bool b = LoadShadersES3( &shader_param_, "Shaders/VS_ShaderPlainES3.vsh", + "Shaders/ShaderPlainES3.fsh", param ); if( b ) { // @@ -156,235 +155,226 @@ void MoreTeapotsRenderer::init( const int32_t numX, const int32_t numY, const in // GLuint bindingPoint = 1; GLuint blockIndex; - blockIndex = glGetUniformBlockIndex( _shaderParam._program, "ParamBlock" ); - glUniformBlockBinding( _shaderParam._program, blockIndex, bindingPoint ); + blockIndex = glGetUniformBlockIndex( shader_param_.program_, "ParamBlock" ); + glUniformBlockBinding( shader_param_.program_, blockIndex, bindingPoint ); //Retrieve array stride value int32_t iNumIndices; - glGetActiveUniformBlockiv( _shaderParam._program, blockIndex, + glGetActiveUniformBlockiv( shader_param_.program_, blockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &iNumIndices ); GLint i[iNumIndices]; GLint stride[iNumIndices]; - glGetActiveUniformBlockiv( _shaderParam._program, blockIndex, + glGetActiveUniformBlockiv( shader_param_.program_, blockIndex, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, i ); - glGetActiveUniformsiv( _shaderParam._program, iNumIndices, (GLuint*)i, GL_UNIFORM_ARRAY_STRIDE, stride ); + glGetActiveUniformsiv( shader_param_.program_, iNumIndices, (GLuint*) i, + GL_UNIFORM_ARRAY_STRIDE, stride ); - _uboMatrixStride = stride[ 0 ] / sizeof(float); - _uboVectorStride = stride[ 2 ] / sizeof(float); + ubo_matrix_stride_ = stride[0] / sizeof(float); + ubo_vector_stride_ = stride[2] / sizeof(float); - glGenBuffers(1, &_ubo); - glBindBuffer(GL_UNIFORM_BUFFER, _ubo); - glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, _ubo); + glGenBuffers( 1, &ubo_ ); + glBindBuffer( GL_UNIFORM_BUFFER, ubo_ ); + glBindBufferBase( GL_UNIFORM_BUFFER, bindingPoint, ubo_ ); //Store color value which wouldn't be updated every frame - int32_t iSize = _iX * _iY * _iZ * - (_uboMatrixStride+_uboMatrixStride+_uboVectorStride); //mat4 + mat4 + vec3 + 1 stride + int32_t iSize = teapot_x_ * teapot_y_ * teapot_z_ + * (ubo_matrix_stride_ + ubo_matrix_stride_ + ubo_vector_stride_); //Mat4 + Mat4 + Vec3 + 1 stride float* pBuffer = new float[iSize]; - float* pColor = pBuffer + _iX * _iY * _iZ * _uboMatrixStride*2; - for( int32_t i = 0; i < _iX * _iY * _iZ; ++i ) + float* pColor = pBuffer + teapot_x_ * teapot_y_ * teapot_z_ * ubo_matrix_stride_ * 2; + for( int32_t i = 0; i < teapot_x_ * teapot_y_ * teapot_z_; ++i ) { - memcpy(pColor, &_mColors[ i ], 3 * sizeof(float)); - pColor += _uboVectorStride; //Assuming std140 layout which is 4 DWORD stride for vectors + memcpy( pColor, &vec_colors_[i], 3 * sizeof(float) ); + pColor += ubo_vector_stride_; //Assuming std140 layout which is 4 DWORD stride for vectors } - glBufferData(GL_UNIFORM_BUFFER, iSize * sizeof(float), pBuffer, GL_DYNAMIC_DRAW); + glBufferData( GL_UNIFORM_BUFFER, iSize * sizeof(float), pBuffer, GL_DYNAMIC_DRAW ); delete[] pBuffer; } else { - LOGI("Shader compilation failed!! Falls back to ES2.0 pass"); //This happens some devices. - _bGeometryInstancingSupport = false; + LOGI( "Shader compilation failed!! Falls back to ES2.0 pass" ); + //This happens some devices. + geometry_instancing_support_ = false; //Load shader for GLES2.0 - loadShaders( &_shaderParam, "Shaders/VS_ShaderPlain.vsh", "Shaders/ShaderPlain.fsh" ); + LoadShaders( &shader_param_, "Shaders/VS_ShaderPlain.vsh", "Shaders/ShaderPlain.fsh" ); } } else { //Load shader for GLES2.0 - loadShaders( &_shaderParam, "Shaders/VS_ShaderPlain.vsh", "Shaders/ShaderPlain.fsh" ); + LoadShaders( &shader_param_, "Shaders/VS_ShaderPlain.vsh", "Shaders/ShaderPlain.fsh" ); } } -void MoreTeapotsRenderer::updateViewport() +void MoreTeapotsRenderer::UpdateViewport() { int32_t viewport[4]; - glGetIntegerv(GL_VIEWPORT, viewport); - float fAspect = (float)viewport[2] / (float)viewport[3]; + 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); + mat_projection_ = ndk_helper::Mat4::Perspective( fAspect, 1.f, CAM_NEAR, CAM_FAR ); } //-------------------------------------------------------------------------------- // Unload //-------------------------------------------------------------------------------- -void MoreTeapotsRenderer::unload() +void MoreTeapotsRenderer::Unload() { - if (_vbo) + if( vbo_ ) { - glDeleteBuffers(1, &_vbo); - _vbo = 0; + glDeleteBuffers( 1, &vbo_ ); + vbo_ = 0; } - if (_ubo) + if( ubo_ ) { - glDeleteBuffers(1, &_ubo); - _ubo = 0; + glDeleteBuffers( 1, &ubo_ ); + ubo_ = 0; } - if (_ibo) + if( ibo_ ) { - glDeleteBuffers(1, &_ibo); - _ibo = 0; + glDeleteBuffers( 1, &ibo_ ); + ibo_ = 0; } - if (_shaderParam._program ) + if( shader_param_.program_ ) { - glDeleteProgram(_shaderParam._program); - _shaderParam._program = 0; + glDeleteProgram( shader_param_.program_ ); + shader_param_.program_ = 0; } } //-------------------------------------------------------------------------------- // Update //-------------------------------------------------------------------------------- -void MoreTeapotsRenderer::update(float fTime) +void MoreTeapotsRenderer::Update( float fTime ) { const float CAM_X = 0.f; const float CAM_Y = 0.f; const float CAM_Z = 2000.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)); + mat_view_ = ndk_helper::Mat4::LookAt( ndk_helper::Vec3( CAM_X, CAM_Y, CAM_Z ), + ndk_helper::Vec3( 0.f, 0.f, 0.f ), ndk_helper::Vec3( 0.f, 1.f, 0.f ) ); - if( _camera ) + if( camera_ ) { - _camera->update(); - _mView = _camera->getTransformMatrix() * _mView * _camera->getRotationMatrix(); + camera_->Update(); + mat_view_ = camera_->GetTransformMatrix() * mat_view_ * camera_->GetRotationMatrix(); } } //-------------------------------------------------------------------------------- // Render //-------------------------------------------------------------------------------- -void MoreTeapotsRenderer::render() +void MoreTeapotsRenderer::Render() { - // Bind the VBO - glBindBuffer(GL_ARRAY_BUFFER, _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_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); + 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); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo_ ); - glUseProgram(_shaderParam._program); + glUseProgram( shader_param_.program_ ); - TEAPOT_MATERIALS material = { - {1.0f, 1.0f, 1.0f, 10.f}, - {0.1f, 0.1f, 0.1f}, - }; + TEAPOT_MATERIALS material = { { 1.0f, 1.0f, 1.0f, 10.f }, { 0.1f, 0.1f, 0.1f }, }; //Update uniforms // //using glUniform3fv here was troublesome.. // - glUniform4f(_shaderParam._uiMaterialSpecular, - material.specular_color[0], - material.specular_color[1], - material.specular_color[2], - material.specular_color[3]); - glUniform3f(_shaderParam._uiMaterialAmbient, - material.ambient_color[0], - material.ambient_color[1], - material.ambient_color[2]); + glUniform4f( shader_param_.material_specular_, material.specular_color[0], + material.specular_color[1], material.specular_color[2], material.specular_color[3] ); + glUniform3f( shader_param_.material_ambient_, material.ambient_color[0], + material.ambient_color[1], material.ambient_color[2] ); - glUniform3f(_shaderParam._uiLight0, 100.f, -200.f, -600.f); + glUniform3f( shader_param_.light0_, 100.f, -200.f, -600.f ); - if( _bGeometryInstancingSupport ) + if( geometry_instancing_support_ ) { // //Geometry instancing, new feature in GLES3.0 // //Update UBO - glBindBuffer(GL_UNIFORM_BUFFER, _ubo); - float* p = (float*)glMapBufferRange(GL_UNIFORM_BUFFER, - 0, _iX * _iY * _iZ * (_uboMatrixStride * 2) * sizeof(float), - GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT); + glBindBuffer( GL_UNIFORM_BUFFER, ubo_ ); + float* p = (float*) glMapBufferRange( GL_UNIFORM_BUFFER, 0, + teapot_x_ * teapot_y_ * teapot_z_ * (ubo_matrix_stride_ * 2) * sizeof(float), + GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT ); float* pMVPMat = p; - float* pMVMat = p + _iX * _iY * _iZ * _uboMatrixStride; - for( int32_t i = 0; i < _iX * _iY * _iZ; ++i ) + float* pMVMat = p + teapot_x_ * teapot_y_ * teapot_z_ * ubo_matrix_stride_; + for( int32_t i = 0; i < teapot_x_ * teapot_y_ * teapot_z_; ++i ) { //Rotation float fX, fY; - _mCurrentRotation[ i ] += _mVecRotation[ i ]; - _mCurrentRotation[ i ].value( fX, fY ); - mat4 mRotation = mat4::rotationX( fX ) * mat4::rotationY( fY ); + vec_current_rotations_[i] += vec_rotations_[i]; + vec_current_rotations_[i].Value( fX, fY ); + ndk_helper::Mat4 mat_rotation = ndk_helper::Mat4::RotationX( fX ) + * ndk_helper::Mat4::RotationY( fY ); // Feed Projection and Model View matrices to the shaders - mat4 mV = _mView * _mModels[ i ] * mRotation; - mat4 mVP = _mProjection * mV; + ndk_helper::Mat4 mat_v = mat_view_ * vec_mat_models_[i] * mat_rotation; + ndk_helper::Mat4 mat_vp = mat_projection_ * mat_v; - memcpy(pMVPMat, mVP.ptr(), sizeof(mV) ); - pMVPMat += _uboMatrixStride; + memcpy( pMVPMat, mat_vp.Ptr(), sizeof(mat_v) ); + pMVPMat += ubo_matrix_stride_; - memcpy(pMVMat, mV.ptr(), sizeof(mV) ); - pMVMat += _uboMatrixStride; + memcpy( pMVMat, mat_v.Ptr(), sizeof(mat_v) ); + pMVMat += ubo_matrix_stride_; } - glUnmapBuffer(GL_UNIFORM_BUFFER); + glUnmapBuffer( GL_UNIFORM_BUFFER ); //Instanced rendering - glDrawElementsInstanced(GL_TRIANGLES, _iNumIndices, GL_UNSIGNED_SHORT, - BUFFER_OFFSET(0), _iX * _iY * _iZ); + glDrawElementsInstanced( GL_TRIANGLES, num_indices_, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0), + teapot_x_ * teapot_y_ * teapot_z_ ); } else { //Regular rendering pass - for( int32_t i = 0; i < _iX * _iY * _iZ; ++i ) + for( int32_t i = 0; i < teapot_x_ * teapot_y_ * teapot_z_; ++i ) { //Set diffuse - float fX, fY, fZ; - _mColors[ i ].value( fX, fY, fZ ); - glUniform4f(_shaderParam._uiMaterialDiffuse, fX, fY, fZ, - 1.f); + float x, y, z; + vec_colors_[i].Value( x, y, z ); + glUniform4f( shader_param_.material_diffuse_, x, y, z, 1.f ); //Rotation - _mCurrentRotation[ i ] += _mVecRotation[ i ]; - _mCurrentRotation[ i ].value( fX, fY ); - mat4 mRotation = mat4::rotationX( fX ) * mat4::rotationY( fY ); + vec_current_rotations_[i] += vec_rotations_[i]; + vec_current_rotations_[i].Value( x, y ); + ndk_helper::Mat4 mat_rotation = ndk_helper::Mat4::RotationX( x ) + * ndk_helper::Mat4::RotationY( y ); // Feed Projection and Model View matrices to the shaders - mat4 mV = _mView * _mModels[ i ] * mRotation; - mat4 mVP = _mProjection * mV; - glUniformMatrix4fv(_shaderParam._uiMatrixP, 1, GL_FALSE, - mVP.ptr()); - glUniformMatrix4fv(_shaderParam._uiMatrixView, 1, GL_FALSE, - mV.ptr()); + ndk_helper::Mat4 mat_v = mat_view_ * vec_mat_models_[i] * mat_rotation; + ndk_helper::Mat4 mat_vp = mat_projection_ * mat_v; + glUniformMatrix4fv( shader_param_.matrix_projection_, 1, GL_FALSE, mat_vp.Ptr() ); + glUniformMatrix4fv( shader_param_.matrix_view_, 1, GL_FALSE, mat_v.Ptr() ); - glDrawElements(GL_TRIANGLES, _iNumIndices, GL_UNSIGNED_SHORT, - BUFFER_OFFSET(0)); + glDrawElements( GL_TRIANGLES, num_indices_, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0) ); } } - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindBuffer( GL_ARRAY_BUFFER, 0 ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } //-------------------------------------------------------------------------------- // LoadShaders //-------------------------------------------------------------------------------- -bool MoreTeapotsRenderer::loadShaders(SHADER_PARAMS* params, const char* strVsh, const char* strFsh) +bool MoreTeapotsRenderer::LoadShaders( SHADER_PARAMS* params, + const char* strVsh, + const char* strFsh ) { // //Shader load for GLES2 @@ -396,75 +386,79 @@ bool MoreTeapotsRenderer::loadShaders(SHADER_PARAMS* params, const char* strVsh, // Create shader program program = glCreateProgram(); - LOGI("Created Shader %d", program); + 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); + if( !ndk_helper::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); + if( !ndk_helper::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); + glAttachShader( program, vertShader ); // Attach fragment shader to program - glAttachShader(program, fragShader); + 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_VERTEX, "myVertex" ); + glBindAttribLocation( program, ATTRIB_NORMAL, "myNormal" ); // Link program - if (!shader::linkProgram(program)) { - LOGI("Failed to link program: %d", program); + if( !ndk_helper::shader::LinkProgram( program ) ) + { + LOGI( "Failed to link program: %d", program ); - if (vertShader) { - glDeleteShader(vertShader); + if( vertShader ) + { + glDeleteShader( vertShader ); vertShader = 0; } - if (fragShader) { - glDeleteShader(fragShader); + if( fragShader ) + { + glDeleteShader( fragShader ); fragShader = 0; } - if (program) { - glDeleteProgram(program); + if( program ) + { + glDeleteProgram( program ); } return false; } // Get uniform locations - params->_uiMatrixP = glGetUniformLocation(program, "uPMatrix"); - params->_uiMatrixView = glGetUniformLocation(program, "uMVMatrix"); + params->matrix_projection_ = glGetUniformLocation( program, "uPMatrix" ); + params->matrix_view_ = glGetUniformLocation( program, "uMVMatrix" ); - params->_uiLight0 = glGetUniformLocation(program, "vLight0"); - params->_uiMaterialDiffuse = glGetUniformLocation(program, - "vMaterialDiffuse"); - params->_uiMaterialAmbient = glGetUniformLocation(program, - "vMaterialAmbient"); - params->_uiMaterialSpecular = glGetUniformLocation(program, - "vMaterialSpecular"); + params->light0_ = glGetUniformLocation( program, "vLight0" ); + params->material_diffuse_ = glGetUniformLocation( program, "vMaterialDiffuse" ); + params->material_ambient_ = glGetUniformLocation( program, "vMaterialAmbient" ); + params->material_specular_ = glGetUniformLocation( program, "vMaterialSpecular" ); // Release vertex and fragment shaders - if (vertShader) - glDeleteShader(vertShader); - if (fragShader) - glDeleteShader(fragShader); + if( vertShader ) + glDeleteShader( vertShader ); + if( fragShader ) + glDeleteShader( fragShader ); - params->_program = program; + params->program_ = program; return true; } -bool MoreTeapotsRenderer::loadShadersES3(SHADER_PARAMS* params, - const char* strVsh, const char* strFsh, +bool MoreTeapotsRenderer::LoadShadersES3( SHADER_PARAMS* params, + const char* strVsh, + const char* strFsh, std::map&shaderParams ) { // @@ -477,80 +471,85 @@ bool MoreTeapotsRenderer::loadShadersES3(SHADER_PARAMS* params, // Create shader program program = glCreateProgram(); - LOGI("Created Shader %d", program); + LOGI( "Created Shader %d", program ); // Create and compile vertex shader - if (!shader::compileShader(&vertShader, GL_VERTEX_SHADER, strVsh, shaderParams)) { - LOGI("Failed to compile vertex shader"); - glDeleteProgram(program); + if( !ndk_helper::shader::CompileShader( &vertShader, GL_VERTEX_SHADER, strVsh, shaderParams ) ) + { + LOGI( "Failed to compile vertex shader" ); + glDeleteProgram( program ); return false; } // Create and compile fragment shader - if (!shader::compileShader(&fragShader, GL_FRAGMENT_SHADER, strFsh, shaderParams)) { - LOGI("Failed to compile fragment shader"); - glDeleteProgram(program); + if( !ndk_helper::shader::CompileShader( &fragShader, GL_FRAGMENT_SHADER, strFsh, + shaderParams ) ) + { + LOGI( "Failed to compile fragment shader" ); + glDeleteProgram( program ); return false; } // Attach vertex shader to program - glAttachShader(program, vertShader); + glAttachShader( program, vertShader ); // Attach fragment shader to program - glAttachShader(program, fragShader); + glAttachShader( program, fragShader ); // Link program - if (!shader::linkProgram(program)) { - LOGI("Failed to link program: %d", program); + if( !ndk_helper::shader::LinkProgram( program ) ) + { + LOGI( "Failed to link program: %d", program ); - if (vertShader) { - glDeleteShader(vertShader); + if( vertShader ) + { + glDeleteShader( vertShader ); vertShader = 0; } - if (fragShader) { - glDeleteShader(fragShader); + if( fragShader ) + { + glDeleteShader( fragShader ); fragShader = 0; } - if (program) { - glDeleteProgram(program); + if( program ) + { + glDeleteProgram( program ); } return false; } // Get uniform locations - params->_uiLight0 = glGetUniformLocation(program, "vLight0"); - params->_uiMaterialAmbient = glGetUniformLocation(program, - "vMaterialAmbient"); - params->_uiMaterialSpecular = glGetUniformLocation(program, - "vMaterialSpecular"); + params->light0_ = glGetUniformLocation( program, "vLight0" ); + params->material_ambient_ = glGetUniformLocation( program, "vMaterialAmbient" ); + params->material_specular_ = glGetUniformLocation( program, "vMaterialSpecular" ); // Release vertex and fragment shaders - if (vertShader) - glDeleteShader(vertShader); - if (fragShader) - glDeleteShader(fragShader); + if( vertShader ) + glDeleteShader( vertShader ); + if( fragShader ) + glDeleteShader( fragShader ); - params->_program = program; + params->program_ = program; return true; } //-------------------------------------------------------------------------------- // Bind //-------------------------------------------------------------------------------- -bool MoreTeapotsRenderer::bind(tapCamera* camera) +bool MoreTeapotsRenderer::Bind( ndk_helper::TapCamera* camera ) { - _camera = camera; + camera_ = camera; return true; } //-------------------------------------------------------------------------------- // Helper functions //-------------------------------------------------------------------------------- -std::string MoreTeapotsRenderer::toString( const int32_t i ) +std::string MoreTeapotsRenderer::ToString( const int32_t i ) { char str[64]; - snprintf( str, sizeof( str ), "%d", i ); + snprintf( str, sizeof(str), "%d", i ); return std::string( str ); } diff --git a/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.h b/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.h index 1cc8aa74a..346d873c4 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.h +++ b/ndk/platforms/android-18/samples/MoreTeapots/jni/MoreTeapotsRenderer.h @@ -47,74 +47,79 @@ struct TEAPOT_VERTEX { - float fPos[3]; - float fNormal[3]; + float pos[3]; + float normal[3]; }; -enum SHADER_ATTRIBUTES { +enum SHADER_ATTRIBUTES +{ ATTRIB_VERTEX, ATTRIB_NORMAL, ATTRIB_COLOR, ATTRIB_UV }; struct SHADER_PARAMS { - GLuint _program; - GLuint _uiLight0; - GLuint _uiMaterialDiffuse; - GLuint _uiMaterialAmbient; - GLuint _uiMaterialSpecular; + GLuint program_; + GLuint light0_; + GLuint material_diffuse_; + GLuint material_ambient_; + GLuint material_specular_; - GLuint _uiMatrixP; - GLuint _uiMatrixView; + GLuint matrix_projection_; + GLuint matrix_view_; }; struct TEAPOT_MATERIALS { - float specular_color[ 4 ]; - float ambient_color[ 3 ]; + float specular_color[4]; + float ambient_color[3]; }; class MoreTeapotsRenderer { - int32_t _iNumIndices; - int32_t _iNumVertices; - GLuint _ibo; - GLuint _vbo; - GLuint _ubo; + int32_t num_indices_; + int32_t num_vertices_; + GLuint ibo_; + GLuint vbo_; + GLuint ubo_; - SHADER_PARAMS _shaderParam; - bool loadShaders(SHADER_PARAMS* params, const char* strVsh, const char* strFsh); - bool loadShadersES3(SHADER_PARAMS* params, - const char* strVsh, const char* strFsh, + SHADER_PARAMS shader_param_; + bool LoadShaders( SHADER_PARAMS* params, + const char* strVsh, + const char* strFsh ); + bool LoadShadersES3( SHADER_PARAMS* params, + const char* strVsh, + const char* strFsh, std::map&shaderParameters ); + ndk_helper::Mat4 mat_projection_; + ndk_helper::Mat4 mat_view_; + std::vector vec_mat_models_; + std::vector vec_colors_; + std::vector vec_rotations_; + std::vector vec_current_rotations_; - mat4 _mProjection; - mat4 _mView; - std::vector _mModels; - std::vector _mColors; - std::vector _mVecRotation; - std::vector _mCurrentRotation; + ndk_helper::TapCamera* camera_; - tapCamera* _camera; + int32_t teapot_x_; + int32_t teapot_y_; + int32_t teapot_z_; + int32_t ubo_matrix_stride_; + int32_t ubo_vector_stride_; + bool geometry_instancing_support_; + bool arb_support_; - int32_t _iX; - int32_t _iY; - int32_t _iZ; - int32_t _uboMatrixStride; - int32_t _uboVectorStride; - bool _bGeometryInstancingSupport; - bool _bARBSupport; - - std::string toString( const int32_t i ); + std::string ToString( const int32_t i ); public: MoreTeapotsRenderer(); virtual ~MoreTeapotsRenderer(); - void init( const int32_t numX, const int32_t numY, const int32_t numZ ); - void render(); - void update(float dTime); - bool bind(tapCamera* camera); - void unload(); - void updateViewport(); + void Init( const int32_t numX, + const int32_t numY, + const int32_t numZ ); + void Render(); + void Update( float dTime ); + bool Bind( ndk_helper::TapCamera* camera ); + void Unload(); + void UpdateViewport(); }; #endif diff --git a/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/helper/NDKHelper.java b/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/helper/NDKHelper.java index 47f4b668e..c864e72bf 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/helper/NDKHelper.java +++ b/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/helper/NDKHelper.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + package com.sample.helper; import java.io.File; @@ -10,35 +26,39 @@ import android.content.pm.ApplicationInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; +import android.media.AudioManager; +import android.media.AudioTrack; import android.opengl.GLUtils; import android.util.Log; - -public class NDKHelper { +public class NDKHelper +{ private static Context context; - public static void setCotext( Context c ) + public static void setContext(Context c) { - Log.i("NDKHelper", "set context:" + c); + Log.i("NDKHelper", "setContext:" + c); context = c; } // - //Load Bitmap - //Java helper is useful decoding PNG, TIFF etc rather than linking libPng etc separately + // Load Bitmap + // Java helper is useful decoding PNG, TIFF etc rather than linking libPng + // etc separately // private int nextPOT(int i) { int pot = 1; - while( pot < i ) pot <<= 1; + while (pot < i) + pot <<= 1; return pot; } private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) { - if(bitmapToScale == null) + if (bitmapToScale == null) return null; - //get the original width and height + // get the original width and height int width = bitmapToScale.getWidth(); int height = bitmapToScale.getHeight(); // create a matrix for the manipulation @@ -48,10 +68,11 @@ public class NDKHelper { 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); + return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), + bitmapToScale.getHeight(), matrix, true); } - public void loadTexture(String path) + public boolean loadTexture(String path) { Bitmap bitmap = null; try @@ -63,31 +84,33 @@ public class NDKHelper { } File file = new File(context.getExternalFilesDir(null), str); - if( file.canRead() ) + if (file.canRead()) { bitmap = BitmapFactory.decodeStream(new FileInputStream(file)); - } - else + } else { - bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path)); + bitmap = BitmapFactory.decodeStream(context.getResources().getAssets() + .open(path)); } - // Matrix matrix = new Matrix(); - // // resize the bit map - // matrix.postScale(-1F, 1F); + // Matrix matrix = new Matrix(); + // // resize the bit map + // matrix.postScale(-1F, 1F); // - // // recreate the new Bitmap and set it back - // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + // // recreate the new Bitmap and set it back + // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), + // bitmap.getHeight(), matrix, true); - } - catch (Exception e) { + } catch (Exception e) + { Log.w("NDKHelper", "Coundn't load a file:" + path); + return false; } - if( bitmap != null ) + if (bitmap != null) { GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); } - return; + return true; } @@ -96,30 +119,38 @@ public class NDKHelper { Bitmap bitmap = null; try { - bitmap = BitmapFactory.decodeStream(context.getResources().getAssets().open(path)); - if( iScalePOT ) + 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 ) + if (originalWidth != width || originalHeight != height) { - //Scale it - bitmap = scaleBitmap( bitmap, width, height ); + // Scale it + bitmap = scaleBitmap(bitmap, width, height); } } - } - catch (Exception e) { + } catch (Exception e) + { Log.w("NDKHelper", "Coundn't load a file:" + path); } return bitmap; } - public int getBitmapWidth(Bitmap bmp) { return bmp.getWidth(); } - public int getBitmapHeight(Bitmap bmp) { return bmp.getHeight(); } + public int getBitmapWidth(Bitmap bmp) + { + return bmp.getWidth(); + } + + public int getBitmapHeight(Bitmap bmp) + { + return bmp.getHeight(); + } public void getBitmapPixels(Bitmap bmp, int[] pixels) { @@ -133,15 +164,39 @@ public class NDKHelper { bmp.recycle(); } - public static String getNativeLibraryDirectory(Context appContext) { + public static String getNativeLibraryDirectory(Context appContext) + { ApplicationInfo ai = context.getApplicationInfo(); - Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir ); + Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir); - if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 || - (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { + if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 + || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) + { return ai.nativeLibraryDir; } return "/system/lib/"; } + + public int getNativeAudioBufferSize() + { + int SDK_INT = android.os.Build.VERSION.SDK_INT; + if (SDK_INT >= 17) + { + AudioManager am = (AudioManager) context + .getSystemService(Context.AUDIO_SERVICE); + String framesPerBuffer = am + .getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER); + return Integer.parseInt(framesPerBuffer); + } else + { + return 0; + } + } + + public int getNativeAudioSampleRate() + { + return AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM); + + } } diff --git a/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsApplication.java b/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsApplication.java index 4eed7cecd..b37d75add 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsApplication.java +++ b/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsApplication.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + package com.sample.moreteapots; import javax.microedition.khronos.opengles.GL10; @@ -20,7 +36,7 @@ public class MoreTeapotsApplication extends Application { private static Context context; public void onCreate(){ context=getApplicationContext(); - NDKHelper.setCotext(context); + NDKHelper.setContext(context); Log.w("native-activity", "onCreate"); final PackageManager pm = getApplicationContext().getPackageManager(); diff --git a/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsNativeActivity.java b/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsNativeActivity.java index 5b22232a8..7f8248a5c 100644 --- a/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsNativeActivity.java +++ b/ndk/platforms/android-18/samples/MoreTeapots/src/com/sample/moreteapots/MoreTeapotsNativeActivity.java @@ -1,3 +1,19 @@ +/* + * 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. + */ + package com.sample.moreteapots; import android.app.NativeActivity; @@ -39,7 +55,6 @@ public class MoreTeapotsNativeActivity extends NativeActivity { _popupWindow = null; } } - // Our popup window, you will call it from your C/C++ code later MoreTeapotsNativeActivity _activity; diff --git a/ndk/sources/android/helper/GLContext.cpp b/ndk/sources/android/helper/GLContext.cpp deleted file mode 100644 index 4595456e8..000000000 --- a/ndk/sources/android/helper/GLContext.cpp +++ /dev/null @@ -1,302 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//-------------------------------------------------------------------------------- -// GLContext.cpp -//-------------------------------------------------------------------------------- - -//-------------------------------------------------------------------------------- -// includes -//-------------------------------------------------------------------------------- -#include -#include "GLContext.h" -#include "gl3stub.h" - -//-------------------------------------------------------------------------------- -// eGLContext -//-------------------------------------------------------------------------------- - -//-------------------------------------------------------------------------------- -// Ctor -//-------------------------------------------------------------------------------- -GLContext::GLContext() : _display(EGL_NO_DISPLAY), - _surface(EGL_NO_SURFACE), - _context(EGL_NO_CONTEXT), - _iWidth( 0 ), - _iHeight( 0 ), - _bES3Support( false ), - _bEGLContextInitialized( false ), - _bGLESInitialized( false ) -{ -} - -void GLContext::initGLES() -{ - if( _bGLESInitialized ) - return; - // - //Initialize OpenGL ES 3 if available - // - const char* versionStr = (const char*)glGetString(GL_VERSION); - if (strstr(versionStr, "OpenGL ES 3.") - && gl3stubInit()) - { - _bES3Support = true; - _fGLVersion = 3.0f; - } - else - { - _fGLVersion = 2.0f; - } - - _bGLESInitialized = true; -} - -//-------------------------------------------------------------------------------- -// Dtor -//-------------------------------------------------------------------------------- -GLContext::~GLContext() -{ - terminate(); -} - -bool GLContext::init( ANativeWindow* window ) -{ - if( _bEGLContextInitialized ) - return true; - - // - //Initialize EGL - // - _window = window; - initEGLSurface(); - initEGLContext(); - initGLES(); - - _bEGLContextInitialized = true; - - return true; -} - -bool GLContext::initEGLSurface() -{ - _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 - }; - _iColorSize = 8; - _iDepthSize = 24; - - EGLint numConfigs; - 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 ); - _iDepthSize = 16; - } - - if ( !numConfigs ) - { - LOGW("Unable to retrieve EGL config"); - return false; - } - - _surface = eglCreateWindowSurface( _display, _config, _window, NULL ); - eglQuerySurface(_display, _surface, EGL_WIDTH, &_iWidth); - eglQuerySurface(_display, _surface, EGL_HEIGHT, &_iHeight); - - /* 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. */ - EGLint format; - eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format); - ANativeWindow_setBuffersGeometry( _window, 0, 0, format); - - return true; -} - -bool GLContext::initEGLContext() -{ - const EGLint contextAttribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, //Request opengl ES2.0 - EGL_NONE - }; - _context = eglCreateContext( _display, _config, NULL, contextAttribs ); - - if( eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE ) - { - LOGW("Unable to eglMakeCurrent"); - return false; - } - - _bContextValid = true; - return true; -} - -EGLint GLContext::swap() -{ - bool b = eglSwapBuffers( _display, _surface); - if( !b ) - { - EGLint err = eglGetError(); - if( err == EGL_BAD_SURFACE ) - { - //Recreate surface - initEGLSurface(); - } - else if( err == EGL_CONTEXT_LOST || err == EGL_BAD_CONTEXT ) - { - //Context has been lost!! - _bContextValid = false; - terminate(); - initEGLContext(); - } - return err; - } - return EGL_SUCCESS; -} - -void GLContext::terminate() -{ - if( _display != EGL_NO_DISPLAY ) - { - eglMakeCurrent( _display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT ); - if ( _context != EGL_NO_CONTEXT ) - { - eglDestroyContext( _display, _context ); - } - - if( _surface != EGL_NO_SURFACE ) - { - eglDestroySurface( _display, _surface ); - } - eglTerminate( _display ); - } - - _display = EGL_NO_DISPLAY; - _context = EGL_NO_CONTEXT; - _surface = EGL_NO_SURFACE; - _bContextValid = false; - -} - -EGLint GLContext::resume(ANativeWindow* window) -{ - if( _bEGLContextInitialized == false ) - { - init( window ); - return EGL_SUCCESS; - } - - int32_t iOriginalWidth = _iWidth; - int32_t iOriginalHeight = _iHeight; - - //Create surface - _window = window; - _surface = eglCreateWindowSurface( _display, _config, _window, NULL ); - eglQuerySurface(_display, _surface, EGL_WIDTH, &_iWidth); - eglQuerySurface(_display, _surface, EGL_HEIGHT, &_iHeight); - - if( _iWidth != iOriginalWidth || _iHeight != iOriginalHeight ) - { - //Screen resized - LOGI("Screen resized"); - } - - if( eglMakeCurrent(_display, _surface, _surface, _context) == EGL_TRUE ) - return EGL_SUCCESS; - - EGLint err = eglGetError(); - LOGW("Unable to eglMakeCurrent %d", err); - - if( err == EGL_CONTEXT_LOST ) - { - //Recreate context - LOGI("Re-creating egl context"); - initEGLContext(); - } - else - { - //Recreate surface - terminate(); - initEGLSurface(); - initEGLContext(); - } - - return err; - -} - -void GLContext::suspend() -{ - if( _surface != EGL_NO_SURFACE ) - { - eglDestroySurface( _display, _surface ); - _surface = EGL_NO_SURFACE; - } -} - -bool GLContext::invalidate() -{ - terminate(); - - _bEGLContextInitialized = false; - return true; -} - - -bool GLContext::checkExtension( const char* extension ) -{ - if( extension == NULL ) - return false; - - std::string extensions = std::string( (char*)glGetString(GL_EXTENSIONS) ); - std::string str = std::string( extension ); - str.append( " " ); - - size_t pos = 0; - if( extensions.find( extension, pos ) != std::string::npos ) - { - return true; - } - - return false; -} diff --git a/ndk/sources/android/helper/JNIHelper.cpp b/ndk/sources/android/helper/JNIHelper.cpp deleted file mode 100644 index a17f5aa59..000000000 --- a/ndk/sources/android/helper/JNIHelper.cpp +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "JNIHelper.h" - -#define CLASS_NAME "android/app/NativeActivity" -#define APPLICATION_CLASS_NAME "com/sample/helper/NDKHelper" - -//--------------------------------------------------------------------------- -//JNI Helper functions -//--------------------------------------------------------------------------- -//Static variable -ANativeActivity* JNIHelper::_activity; -jobject JNIHelper::_objJNIHelper; -jclass JNIHelper::_clsJNIHelper; -std::string JNIHelper::_appName; - -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; -} - -//--------------------------------------------------------------------------- -//Init -//--------------------------------------------------------------------------- -void JNIHelper::init( ANativeActivity* activity ) -{ - _activity = activity; - - JNIEnv *env; - _activity->vm->AttachCurrentThread(&env, NULL); - - //Retrieve app name - jclass android_content_Context = env->GetObjectClass(_activity->clazz); - jmethodID midGetPackageName = env->GetMethodID(android_content_Context, "getPackageName", "()Ljava/lang/String;"); - - jstring packageName= (jstring)env->CallObjectMethod(_activity->clazz, midGetPackageName); - const char* appname = env->GetStringUTFChars(packageName, NULL); - _appName = std::string(appname); - - _activity->vm->DetachCurrentThread(); - -}; - -//--------------------------------------------------------------------------- -//readFile -//--------------------------------------------------------------------------- -bool JNIHelper::readFile(const char* fileName, std::vector& 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(f), - std::istreambuf_iterator()); - 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(CLASS_NAME); - 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, "", - "()V"); - _objJNIHelper = env->NewObject(_clsJNIHelper, constructor); - _objJNIHelper = env->NewGlobalRef(_objJNIHelper); - } - - jstring name = env->NewStringUTF(fileName); -#if 0 - /* Ask the PNG manager for a bitmap */ - mid = env->GetMethodID(_clsJNIHelper, "openBitmap", - "(Ljava/lang/String;Z)Landroid/graphics/Bitmap;"); - - 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; - -} - -std::string JNIHelper::convertString( const char* str, const char* encode ) -{ - if (_activity == NULL) - { - return std::string(""); - } - - JNIEnv *env; - - _activity->vm->AttachCurrentThread(&env, NULL); - - int32_t iLength = strlen( (const char*)str ); - - jbyteArray array = env->NewByteArray( iLength ); - env->SetByteArrayRegion( array, 0, iLength, (const signed char*)str ); - - jstring strEncode = env->NewStringUTF( encode ); - - jclass cls = env->FindClass("java/lang/String"); - jmethodID ctor = env->GetMethodID(cls, "", - "([BLjava/lang/String;)V"); - jstring object = (jstring)env->NewObject( cls, ctor, array, strEncode ); - - const char *cparam = env->GetStringUTFChars( object, NULL ); - - std::string s = std::string(cparam); - - env->ReleaseStringUTFChars( object, cparam ); - _activity->vm->DetachCurrentThread(); - - return s; -} diff --git a/ndk/sources/android/helper/JNIHelper.h b/ndk/sources/android/helper/JNIHelper.h deleted file mode 100644 index 253ce6aff..000000000 --- a/ndk/sources/android/helper/JNIHelper.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, JNIHelper::getAppName(), __VA_ARGS__)) -#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, JNIHelper::getAppName(), __VA_ARGS__)) -#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, JNIHelper::getAppName(), __VA_ARGS__)) - -jclass retrieveClass(JNIEnv *jni, ANativeActivity* activity, const char* className); - -/****************************************************************** - * Helpers to invoke Java methods - * To use this class, add NDKHelper.java as a corresponding helpers in Java side - */ -class JNIHelper -{ -private: - static ANativeActivity* _activity; - static jobject _objJNIHelper; - static jclass _clsJNIHelper; - - static jstring getExternalFilesDir( JNIEnv *env ); - - static std::string _appName; -public: - JNIHelper() - { - }; - ~JNIHelper() { - JNIEnv *env; - _activity->vm->AttachCurrentThread(&env, NULL); - - env->DeleteGlobalRef(_objJNIHelper); - env->DeleteGlobalRef(_clsJNIHelper); - - _activity->vm->DetachCurrentThread(); - - }; - static void init( ANativeActivity* activity ); - static bool readFile( const char* fileName, std::vector& buffer ); - static uint32_t loadTexture(const char* fileName ); - static std::string convertString( const char* str, const char* encode ); - - static const char* getAppName() { - return _appName.c_str(); - }; - - - - -}; diff --git a/ndk/sources/android/helper/gestureDetector.cpp b/ndk/sources/android/helper/gestureDetector.cpp deleted file mode 100644 index 59255b043..000000000 --- a/ndk/sources/android/helper/gestureDetector.cpp +++ /dev/null @@ -1,345 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//-------------------------------------------------------------------------------- -// gestureDetector.cpp -//-------------------------------------------------------------------------------- - -//-------------------------------------------------------------------------------- -// includes -//-------------------------------------------------------------------------------- -#include "gestureDetector.h" - -//-------------------------------------------------------------------------------- -// GestureDetector -//-------------------------------------------------------------------------------- -GestureDetector::GestureDetector() -{ - _fDpFactor = 1.f; -} - -void GestureDetector::setConfiguration(AConfiguration* config) -{ - _fDpFactor = 160.f / AConfiguration_getDensity(config); -} - -//-------------------------------------------------------------------------------- -// TapDetector -//-------------------------------------------------------------------------------- -GESTURE_STATE 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 GESTURE_STATE_ACTION; - } - } - } - break; - } - } - return GESTURE_STATE_NONE; -} - -//-------------------------------------------------------------------------------- -// DoubletapDetector -//-------------------------------------------------------------------------------- -GESTURE_STATE DoubletapDetector::detect(const AInputEvent* motion_event) -{ - if( AMotionEvent_getPointerCount(motion_event) > 1 ) - { - //Only support single double tap - 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 GESTURE_STATE_ACTION; - } - } - 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 GESTURE_STATE_NONE; -} - -void DoubletapDetector::setConfiguration(AConfiguration* config) -{ - _fDpFactor = 160.f / AConfiguration_getDensity(config); - _tapDetector.setConfiguration(config); -} - -//-------------------------------------------------------------------------------- -// PinchDetector -//-------------------------------------------------------------------------------- - -int32_t PinchDetector::findIndex( const AInputEvent* event, int32_t iID ) -{ - int32_t iCount = AMotionEvent_getPointerCount(event); - for( uint32_t i = 0; i < iCount; ++i ) - { - if( iID == AMotionEvent_getPointerId(event, i) ) - return i; - } - return -1; -} - - -GESTURE_STATE PinchDetector::detect(const AInputEvent* event) -{ - GESTURE_STATE ret = GESTURE_STATE_NONE; - int32_t iAction = AMotionEvent_getAction(event); - uint32_t flags = iAction & AMOTION_EVENT_ACTION_MASK; - _event = event; - - int32_t iCount = AMotionEvent_getPointerCount(event); - switch( flags ) - { - case AMOTION_EVENT_ACTION_DOWN: - _vecPointers.push_back( AMotionEvent_getPointerId( event, 0 ) ); - break; - case AMOTION_EVENT_ACTION_POINTER_DOWN: - { - int32_t iIndex = (iAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; - _vecPointers.push_back(AMotionEvent_getPointerId(event, iIndex)); - if( iCount == 2 ) - { - //Start new pinch - ret = GESTURE_STATE_START; - } - } - break; - case AMOTION_EVENT_ACTION_UP: - _vecPointers.pop_back(); - 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::iterator it = _vecPointers.begin(); - std::vector::iterator itEnd = _vecPointers.end(); - int32_t i = 0; - for(;it!=itEnd;++it, ++i) - { - if( *it == iReleasedPointerID ) - { - _vecPointers.erase(it); - break; - } - } - - if( i <= 1 ) - { - //Reset pinch or drag - if( iCount != 2 ) - { - //Start new pinch - ret = GESTURE_STATE_START | GESTURE_STATE_END; - } - } - } - break; - case AMOTION_EVENT_ACTION_MOVE: - switch(iCount) - { - case 1: - break; - default: - //Multi touch - ret = GESTURE_STATE_MOVE; - break; - } - break; - case AMOTION_EVENT_ACTION_CANCEL: - break; - } - - - return ret; -} - -bool PinchDetector::getPointers( vec2& v1, vec2& v2 ) -{ - if( _vecPointers.size() < 2 ) - return false; - - int32_t iIndex = findIndex( _event, _vecPointers[0] ); - if( iIndex == -1 ) - return false; - - float fX = AMotionEvent_getX( _event, iIndex); - float fY = AMotionEvent_getY( _event, iIndex); - - iIndex = findIndex( _event, _vecPointers[1] ); - if( iIndex == -1 ) - return false; - - float fX2 = AMotionEvent_getX( _event, iIndex); - float fY2 = AMotionEvent_getY( _event, iIndex); - - v1 = vec2( fX, fY ); - v2 = vec2( fX2, fY2 ); - - return true; -} - -//-------------------------------------------------------------------------------- -// DragDetector -//-------------------------------------------------------------------------------- - -int32_t DragDetector::findIndex( const AInputEvent* event, int32_t iID ) -{ - int32_t iCount = AMotionEvent_getPointerCount(event); - for( uint32_t i = 0; i < iCount; ++i ) - { - if( iID == AMotionEvent_getPointerId(event, i) ) - return i; - } - return -1; -} - - -GESTURE_STATE DragDetector::detect(const AInputEvent* event) -{ - GESTURE_STATE ret = GESTURE_STATE_NONE; - int32_t iAction = AMotionEvent_getAction(event); - int32_t iIndex = (iAction & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; - uint32_t flags = iAction & AMOTION_EVENT_ACTION_MASK; - _event = event; - - int32_t iCount = AMotionEvent_getPointerCount(event); - switch( flags ) - { - case AMOTION_EVENT_ACTION_DOWN: - _vecPointers.push_back( AMotionEvent_getPointerId( event, 0 ) ); - ret = GESTURE_STATE_START; - break; - case AMOTION_EVENT_ACTION_POINTER_DOWN: - _vecPointers.push_back(AMotionEvent_getPointerId(event, iIndex)); - break; - case AMOTION_EVENT_ACTION_UP: - _vecPointers.pop_back(); - ret = GESTURE_STATE_END; - break; - case AMOTION_EVENT_ACTION_POINTER_UP: - { - int32_t iReleasedPointerID = AMotionEvent_getPointerId(event, iIndex); - - std::vector::iterator it = _vecPointers.begin(); - std::vector::iterator itEnd = _vecPointers.end(); - int32_t i = 0; - for(;it!=itEnd;++it, ++i) - { - if( *it == iReleasedPointerID ) - { - _vecPointers.erase(it); - break; - } - } - - if( i <= 1 ) - { - //Reset pinch or drag - if( iCount == 2 ) - { - ret = GESTURE_STATE_START; - } - } - break; - } - case AMOTION_EVENT_ACTION_MOVE: - switch(iCount) - { - case 1: - //Drag - ret = GESTURE_STATE_MOVE; - break; - default: - break; - } - break; - case AMOTION_EVENT_ACTION_CANCEL: - break; - } - - return ret; -} - -bool DragDetector::getPointer( vec2& v ) -{ - if( _vecPointers.size() < 1 ) - return false; - - int32_t iIndex = findIndex( _event, _vecPointers[0] ); - if( iIndex == -1 ) - return false; - - float fX = AMotionEvent_getX( _event, iIndex); - float fY = AMotionEvent_getY( _event, iIndex); - - v = vec2( fX, fY ); - - return true; -} - diff --git a/ndk/sources/android/helper/gl3stub.c b/ndk/sources/android/helper/gl3stub.c deleted file mode 100644 index 0f0a40c01..000000000 --- a/ndk/sources/android/helper/gl3stub.c +++ /dev/null @@ -1,343 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "gl3stub.h" - -GLboolean gl3stubInit() { - #define FIND_PROC(s) s = (void*)eglGetProcAddress(#s); - FIND_PROC(glReadBuffer); - FIND_PROC(glDrawRangeElements); - FIND_PROC(glTexImage3D); - FIND_PROC(glTexSubImage3D); - FIND_PROC(glCopyTexSubImage3D); - FIND_PROC(glCompressedTexImage3D); - FIND_PROC(glCompressedTexSubImage3D); - FIND_PROC(glGenQueries); - FIND_PROC(glDeleteQueries); - FIND_PROC(glIsQuery); - FIND_PROC(glBeginQuery); - FIND_PROC(glEndQuery); - FIND_PROC(glGetQueryiv); - FIND_PROC(glGetQueryObjectuiv); - FIND_PROC(glUnmapBuffer); - FIND_PROC(glGetBufferPointerv); - FIND_PROC(glDrawBuffers); - FIND_PROC(glUniformMatrix2x3fv); - FIND_PROC(glUniformMatrix3x2fv); - FIND_PROC(glUniformMatrix2x4fv); - FIND_PROC(glUniformMatrix4x2fv); - FIND_PROC(glUniformMatrix3x4fv); - FIND_PROC(glUniformMatrix4x3fv); - FIND_PROC(glBlitFramebuffer); - FIND_PROC(glRenderbufferStorageMultisample); - FIND_PROC(glFramebufferTextureLayer); - FIND_PROC(glMapBufferRange); - FIND_PROC(glFlushMappedBufferRange); - FIND_PROC(glBindVertexArray); - FIND_PROC(glDeleteVertexArrays); - FIND_PROC(glGenVertexArrays); - FIND_PROC(glIsVertexArray); - FIND_PROC(glGetIntegeri_v); - FIND_PROC(glBeginTransformFeedback); - FIND_PROC(glEndTransformFeedback); - FIND_PROC(glBindBufferRange); - FIND_PROC(glBindBufferBase); - FIND_PROC(glTransformFeedbackVaryings); - FIND_PROC(glGetTransformFeedbackVarying); - FIND_PROC(glVertexAttribIPointer); - FIND_PROC(glGetVertexAttribIiv); - FIND_PROC(glGetVertexAttribIuiv); - FIND_PROC(glVertexAttribI4i); - FIND_PROC(glVertexAttribI4ui); - FIND_PROC(glVertexAttribI4iv); - FIND_PROC(glVertexAttribI4uiv); - FIND_PROC(glGetUniformuiv); - FIND_PROC(glGetFragDataLocation); - FIND_PROC(glUniform1ui); - FIND_PROC(glUniform2ui); - FIND_PROC(glUniform3ui); - FIND_PROC(glUniform4ui); - FIND_PROC(glUniform1uiv); - FIND_PROC(glUniform2uiv); - FIND_PROC(glUniform3uiv); - FIND_PROC(glUniform4uiv); - FIND_PROC(glClearBufferiv); - FIND_PROC(glClearBufferuiv); - FIND_PROC(glClearBufferfv); - FIND_PROC(glClearBufferfi); - FIND_PROC(glGetStringi); - FIND_PROC(glCopyBufferSubData); - FIND_PROC(glGetUniformIndices); - FIND_PROC(glGetActiveUniformsiv); - FIND_PROC(glGetUniformBlockIndex); - FIND_PROC(glGetActiveUniformBlockiv); - FIND_PROC(glGetActiveUniformBlockName); - FIND_PROC(glUniformBlockBinding); - FIND_PROC(glDrawArraysInstanced); - FIND_PROC(glDrawElementsInstanced); - FIND_PROC(glFenceSync); - FIND_PROC(glIsSync); - FIND_PROC(glDeleteSync); - FIND_PROC(glClientWaitSync); - FIND_PROC(glWaitSync); - FIND_PROC(glGetInteger64v); - FIND_PROC(glGetSynciv); - FIND_PROC(glGetInteger64i_v); - FIND_PROC(glGetBufferParameteri64v); - FIND_PROC(glGenSamplers); - FIND_PROC(glDeleteSamplers); - FIND_PROC(glIsSampler); - FIND_PROC(glBindSampler); - FIND_PROC(glSamplerParameteri); - FIND_PROC(glSamplerParameteriv); - FIND_PROC(glSamplerParameterf); - FIND_PROC(glSamplerParameterfv); - FIND_PROC(glGetSamplerParameteriv); - FIND_PROC(glGetSamplerParameterfv); - FIND_PROC(glVertexAttribDivisor); - FIND_PROC(glBindTransformFeedback); - FIND_PROC(glDeleteTransformFeedbacks); - FIND_PROC(glGenTransformFeedbacks); - FIND_PROC(glIsTransformFeedback); - FIND_PROC(glPauseTransformFeedback); - FIND_PROC(glResumeTransformFeedback); - FIND_PROC(glGetProgramBinary); - FIND_PROC(glProgramBinary); - FIND_PROC(glProgramParameteri); - FIND_PROC(glInvalidateFramebuffer); - FIND_PROC(glInvalidateSubFramebuffer); - FIND_PROC(glTexStorage2D); - FIND_PROC(glTexStorage3D); - FIND_PROC(glGetInternalformativ); - #undef FIND_PROC - - if (!glReadBuffer || - !glDrawRangeElements || - !glTexImage3D || - !glTexSubImage3D || - !glCopyTexSubImage3D || - !glCompressedTexImage3D || - !glCompressedTexSubImage3D || - !glGenQueries || - !glDeleteQueries || - !glIsQuery || - !glBeginQuery || - !glEndQuery || - !glGetQueryiv || - !glGetQueryObjectuiv || - !glUnmapBuffer || - !glGetBufferPointerv || - !glDrawBuffers || - !glUniformMatrix2x3fv || - !glUniformMatrix3x2fv || - !glUniformMatrix2x4fv || - !glUniformMatrix4x2fv || - !glUniformMatrix3x4fv || - !glUniformMatrix4x3fv || - !glBlitFramebuffer || - !glRenderbufferStorageMultisample || - !glFramebufferTextureLayer || - !glMapBufferRange || - !glFlushMappedBufferRange || - !glBindVertexArray || - !glDeleteVertexArrays || - !glGenVertexArrays || - !glIsVertexArray || - !glGetIntegeri_v || - !glBeginTransformFeedback || - !glEndTransformFeedback || - !glBindBufferRange || - !glBindBufferBase || - !glTransformFeedbackVaryings || - !glGetTransformFeedbackVarying || - !glVertexAttribIPointer || - !glGetVertexAttribIiv || - !glGetVertexAttribIuiv || - !glVertexAttribI4i || - !glVertexAttribI4ui || - !glVertexAttribI4iv || - !glVertexAttribI4uiv || - !glGetUniformuiv || - !glGetFragDataLocation || - !glUniform1ui || - !glUniform2ui || - !glUniform3ui || - !glUniform4ui || - !glUniform1uiv || - !glUniform2uiv || - !glUniform3uiv || - !glUniform4uiv || - !glClearBufferiv || - !glClearBufferuiv || - !glClearBufferfv || - !glClearBufferfi || - !glGetStringi || - !glCopyBufferSubData || - !glGetUniformIndices || - !glGetActiveUniformsiv || - !glGetUniformBlockIndex || - !glGetActiveUniformBlockiv || - !glGetActiveUniformBlockName || - !glUniformBlockBinding || - !glDrawArraysInstanced || - !glDrawElementsInstanced || - !glFenceSync || - !glIsSync || - !glDeleteSync || - !glClientWaitSync || - !glWaitSync || - !glGetInteger64v || - !glGetSynciv || - !glGetInteger64i_v || - !glGetBufferParameteri64v || - !glGenSamplers || - !glDeleteSamplers || - !glIsSampler || - !glBindSampler || - !glSamplerParameteri || - !glSamplerParameteriv || - !glSamplerParameterf || - !glSamplerParameterfv || - !glGetSamplerParameteriv || - !glGetSamplerParameterfv || - !glVertexAttribDivisor || - !glBindTransformFeedback || - !glDeleteTransformFeedbacks || - !glGenTransformFeedbacks || - !glIsTransformFeedback || - !glPauseTransformFeedback || - !glResumeTransformFeedback || - !glGetProgramBinary || - !glProgramBinary || - !glProgramParameteri || - !glInvalidateFramebuffer || - !glInvalidateSubFramebuffer || - !glTexStorage2D || - !glTexStorage3D || - !glGetInternalformativ) - { - return GL_FALSE; - } - - return GL_TRUE; -} - -/* Function pointer definitions */ -GL_APICALL void (* GL_APIENTRY glReadBuffer) (GLenum mode); -GL_APICALL void (* GL_APIENTRY glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid* indices); -GL_APICALL void (* GL_APIENTRY glTexImage3D) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid* pixels); -GL_APICALL void (* GL_APIENTRY glTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid* pixels); -GL_APICALL void (* GL_APIENTRY glCopyTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height); -GL_APICALL void (* GL_APIENTRY glCompressedTexImage3D) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid* data); -GL_APICALL void (* GL_APIENTRY glCompressedTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid* data); -GL_APICALL void (* GL_APIENTRY glGenQueries) (GLsizei n, GLuint* ids); -GL_APICALL void (* GL_APIENTRY glDeleteQueries) (GLsizei n, const GLuint* ids); -GL_APICALL GLboolean (* GL_APIENTRY glIsQuery) (GLuint id); -GL_APICALL void (* GL_APIENTRY glBeginQuery) (GLenum target, GLuint id); -GL_APICALL void (* GL_APIENTRY glEndQuery) (GLenum target); -GL_APICALL void (* GL_APIENTRY glGetQueryiv) (GLenum target, GLenum pname, GLint* params); -GL_APICALL void (* GL_APIENTRY glGetQueryObjectuiv) (GLuint id, GLenum pname, GLuint* params); -GL_APICALL GLboolean (* GL_APIENTRY glUnmapBuffer) (GLenum target); -GL_APICALL void (* GL_APIENTRY glGetBufferPointerv) (GLenum target, GLenum pname, GLvoid** params); -GL_APICALL void (* GL_APIENTRY glDrawBuffers) (GLsizei n, const GLenum* bufs); -GL_APICALL void (* GL_APIENTRY glUniformMatrix2x3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glUniformMatrix3x2fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glUniformMatrix2x4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glUniformMatrix4x2fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glUniformMatrix3x4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glUniformMatrix4x3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glBlitFramebuffer) (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); -GL_APICALL void (* GL_APIENTRY glRenderbufferStorageMultisample) (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height); -GL_APICALL void (* GL_APIENTRY glFramebufferTextureLayer) (GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer); -GL_APICALL GLvoid* (* GL_APIENTRY glMapBufferRange) (GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access); -GL_APICALL void (* GL_APIENTRY glFlushMappedBufferRange) (GLenum target, GLintptr offset, GLsizeiptr length); -GL_APICALL void (* GL_APIENTRY glBindVertexArray) (GLuint array); -GL_APICALL void (* GL_APIENTRY glDeleteVertexArrays) (GLsizei n, const GLuint* arrays); -GL_APICALL void (* GL_APIENTRY glGenVertexArrays) (GLsizei n, GLuint* arrays); -GL_APICALL GLboolean (* GL_APIENTRY glIsVertexArray) (GLuint array); -GL_APICALL void (* GL_APIENTRY glGetIntegeri_v) (GLenum target, GLuint index, GLint* data); -GL_APICALL void (* GL_APIENTRY glBeginTransformFeedback) (GLenum primitiveMode); -GL_APICALL void (* GL_APIENTRY glEndTransformFeedback) (void); -GL_APICALL void (* GL_APIENTRY glBindBufferRange) (GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size); -GL_APICALL void (* GL_APIENTRY glBindBufferBase) (GLenum target, GLuint index, GLuint buffer); -GL_APICALL void (* GL_APIENTRY glTransformFeedbackVaryings) (GLuint program, GLsizei count, const GLchar* const* varyings, GLenum bufferMode); -GL_APICALL void (* GL_APIENTRY glGetTransformFeedbackVarying) (GLuint program, GLuint index, GLsizei bufSize, GLsizei* length, GLsizei* size, GLenum* type, GLchar* name); -GL_APICALL void (* GL_APIENTRY glVertexAttribIPointer) (GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid* pointer); -GL_APICALL void (* GL_APIENTRY glGetVertexAttribIiv) (GLuint index, GLenum pname, GLint* params); -GL_APICALL void (* GL_APIENTRY glGetVertexAttribIuiv) (GLuint index, GLenum pname, GLuint* params); -GL_APICALL void (* GL_APIENTRY glVertexAttribI4i) (GLuint index, GLint x, GLint y, GLint z, GLint w); -GL_APICALL void (* GL_APIENTRY glVertexAttribI4ui) (GLuint index, GLuint x, GLuint y, GLuint z, GLuint w); -GL_APICALL void (* GL_APIENTRY glVertexAttribI4iv) (GLuint index, const GLint* v); -GL_APICALL void (* GL_APIENTRY glVertexAttribI4uiv) (GLuint index, const GLuint* v); -GL_APICALL void (* GL_APIENTRY glGetUniformuiv) (GLuint program, GLint location, GLuint* params); -GL_APICALL GLint (* GL_APIENTRY glGetFragDataLocation) (GLuint program, const GLchar *name); -GL_APICALL void (* GL_APIENTRY glUniform1ui) (GLint location, GLuint v0); -GL_APICALL void (* GL_APIENTRY glUniform2ui) (GLint location, GLuint v0, GLuint v1); -GL_APICALL void (* GL_APIENTRY glUniform3ui) (GLint location, GLuint v0, GLuint v1, GLuint v2); -GL_APICALL void (* GL_APIENTRY glUniform4ui) (GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3); -GL_APICALL void (* GL_APIENTRY glUniform1uiv) (GLint location, GLsizei count, const GLuint* value); -GL_APICALL void (* GL_APIENTRY glUniform2uiv) (GLint location, GLsizei count, const GLuint* value); -GL_APICALL void (* GL_APIENTRY glUniform3uiv) (GLint location, GLsizei count, const GLuint* value); -GL_APICALL void (* GL_APIENTRY glUniform4uiv) (GLint location, GLsizei count, const GLuint* value); -GL_APICALL void (* GL_APIENTRY glClearBufferiv) (GLenum buffer, GLint drawbuffer, const GLint* value); -GL_APICALL void (* GL_APIENTRY glClearBufferuiv) (GLenum buffer, GLint drawbuffer, const GLuint* value); -GL_APICALL void (* GL_APIENTRY glClearBufferfv) (GLenum buffer, GLint drawbuffer, const GLfloat* value); -GL_APICALL void (* GL_APIENTRY glClearBufferfi) (GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil); -GL_APICALL const GLubyte* (* GL_APIENTRY glGetStringi) (GLenum name, GLuint index); -GL_APICALL void (* GL_APIENTRY glCopyBufferSubData) (GLenum readTarget, GLenum writeTarget, GLintptr readOffset, GLintptr writeOffset, GLsizeiptr size); -GL_APICALL void (* GL_APIENTRY glGetUniformIndices) (GLuint program, GLsizei uniformCount, const GLchar* const* uniformNames, GLuint* uniformIndices); -GL_APICALL void (* GL_APIENTRY glGetActiveUniformsiv) (GLuint program, GLsizei uniformCount, const GLuint* uniformIndices, GLenum pname, GLint* params); -GL_APICALL GLuint (* GL_APIENTRY glGetUniformBlockIndex) (GLuint program, const GLchar* uniformBlockName); -GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockiv) (GLuint program, GLuint uniformBlockIndex, GLenum pname, GLint* params); -GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockName) (GLuint program, GLuint uniformBlockIndex, GLsizei bufSize, GLsizei* length, GLchar* uniformBlockName); -GL_APICALL void (* GL_APIENTRY glUniformBlockBinding) (GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding); -GL_APICALL void (* GL_APIENTRY glDrawArraysInstanced) (GLenum mode, GLint first, GLsizei count, GLsizei instanceCount); -GL_APICALL void (* GL_APIENTRY glDrawElementsInstanced) (GLenum mode, GLsizei count, GLenum type, const GLvoid* indices, GLsizei instanceCount); -GL_APICALL GLsync (* GL_APIENTRY glFenceSync) (GLenum condition, GLbitfield flags); -GL_APICALL GLboolean (* GL_APIENTRY glIsSync) (GLsync sync); -GL_APICALL void (* GL_APIENTRY glDeleteSync) (GLsync sync); -GL_APICALL GLenum (* GL_APIENTRY glClientWaitSync) (GLsync sync, GLbitfield flags, GLuint64 timeout); -GL_APICALL void (* GL_APIENTRY glWaitSync) (GLsync sync, GLbitfield flags, GLuint64 timeout); -GL_APICALL void (* GL_APIENTRY glGetInteger64v) (GLenum pname, GLint64* params); -GL_APICALL void (* GL_APIENTRY glGetSynciv) (GLsync sync, GLenum pname, GLsizei bufSize, GLsizei* length, GLint* values); -GL_APICALL void (* GL_APIENTRY glGetInteger64i_v) (GLenum target, GLuint index, GLint64* data); -GL_APICALL void (* GL_APIENTRY glGetBufferParameteri64v) (GLenum target, GLenum pname, GLint64* params); -GL_APICALL void (* GL_APIENTRY glGenSamplers) (GLsizei count, GLuint* samplers); -GL_APICALL void (* GL_APIENTRY glDeleteSamplers) (GLsizei count, const GLuint* samplers); -GL_APICALL GLboolean (* GL_APIENTRY glIsSampler) (GLuint sampler); -GL_APICALL void (* GL_APIENTRY glBindSampler) (GLuint unit, GLuint sampler); -GL_APICALL void (* GL_APIENTRY glSamplerParameteri) (GLuint sampler, GLenum pname, GLint param); -GL_APICALL void (* GL_APIENTRY glSamplerParameteriv) (GLuint sampler, GLenum pname, const GLint* param); -GL_APICALL void (* GL_APIENTRY glSamplerParameterf) (GLuint sampler, GLenum pname, GLfloat param); -GL_APICALL void (* GL_APIENTRY glSamplerParameterfv) (GLuint sampler, GLenum pname, const GLfloat* param); -GL_APICALL void (* GL_APIENTRY glGetSamplerParameteriv) (GLuint sampler, GLenum pname, GLint* params); -GL_APICALL void (* GL_APIENTRY glGetSamplerParameterfv) (GLuint sampler, GLenum pname, GLfloat* params); -GL_APICALL void (* GL_APIENTRY glVertexAttribDivisor) (GLuint index, GLuint divisor); -GL_APICALL void (* GL_APIENTRY glBindTransformFeedback) (GLenum target, GLuint id); -GL_APICALL void (* GL_APIENTRY glDeleteTransformFeedbacks) (GLsizei n, const GLuint* ids); -GL_APICALL void (* GL_APIENTRY glGenTransformFeedbacks) (GLsizei n, GLuint* ids); -GL_APICALL GLboolean (* GL_APIENTRY glIsTransformFeedback) (GLuint id); -GL_APICALL void (* GL_APIENTRY glPauseTransformFeedback) (void); -GL_APICALL void (* GL_APIENTRY glResumeTransformFeedback) (void); -GL_APICALL void (* GL_APIENTRY glGetProgramBinary) (GLuint program, GLsizei bufSize, GLsizei* length, GLenum* binaryFormat, GLvoid* binary); -GL_APICALL void (* GL_APIENTRY glProgramBinary) (GLuint program, GLenum binaryFormat, const GLvoid* binary, GLsizei length); -GL_APICALL void (* GL_APIENTRY glProgramParameteri) (GLuint program, GLenum pname, GLint value); -GL_APICALL void (* GL_APIENTRY glInvalidateFramebuffer) (GLenum target, GLsizei numAttachments, const GLenum* attachments); -GL_APICALL void (* GL_APIENTRY glInvalidateSubFramebuffer) (GLenum target, GLsizei numAttachments, const GLenum* attachments, GLint x, GLint y, GLsizei width, GLsizei height); -GL_APICALL void (* GL_APIENTRY glTexStorage2D) (GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height); -GL_APICALL void (* GL_APIENTRY glTexStorage3D) (GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth); -GL_APICALL void (* GL_APIENTRY glGetInternalformativ) (GLenum target, GLenum internalformat, GLenum pname, GLsizei bufSize, GLint* params); diff --git a/ndk/sources/android/helper/perfMonitor.cpp b/ndk/sources/android/helper/perfMonitor.cpp deleted file mode 100644 index bc4f337d1..000000000 --- a/ndk/sources/android/helper/perfMonitor.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "perfMonitor.h" - -perfMonitor::perfMonitor(): - _dLastTick(0.f), - _tvLastSec(0), - _tickindex(0), - _ticksum(0) -{ - for(int32_t i = 0; i < NUM_SAMPLES; ++i ) - _ticklist[i] = 0; -} - -perfMonitor::~perfMonitor() { -} - -double perfMonitor::updateTick(double currentTick) -{ - _ticksum -= _ticklist[_tickindex]; - _ticksum += currentTick; - _ticklist[_tickindex] = currentTick; - _tickindex = (_tickindex+1)%NUM_SAMPLES; - - return((double)_ticksum/NUM_SAMPLES); -} - -bool perfMonitor::update(float &fFPS) -{ - struct timeval Time; - gettimeofday( &Time, NULL ); - - double time = Time.tv_sec + Time.tv_usec * 1.0/1000000.0; - double dTick = time - _dLastTick; - double d = updateTick( dTick ); - _dLastTick = time; - - if( Time.tv_sec - _tvLastSec >= 1 ) - { - double time = Time.tv_sec + Time.tv_usec * 1.0/1000000.0; - _fCurrentFPS = 1.f / d; - _tvLastSec = Time.tv_sec; - fFPS = _fCurrentFPS; - return true; - } - else - { - fFPS = _fCurrentFPS; - return false; - } -} diff --git a/ndk/sources/android/helper/shader.cpp b/ndk/sources/android/helper/shader.cpp deleted file mode 100644 index f361a7ad7..000000000 --- a/ndk/sources/android/helper/shader.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "shader.h" -#include "JNIHelper.h" - -#define DEBUG (1) - -bool shader::compileShader(GLuint *shader, const GLenum type, - const char *strFileName, const std::map& mapParameters) -{ - std::vector data; - bool b = JNIHelper::readFile(strFileName, data); - if (!b) - { - LOGI("Can not open a file:%s", strFileName); - return false; - } - - //Fill-in parameters - std::string str(data.begin(), data.end()); - std::map::const_iterator it = mapParameters.begin(); - std::map::const_iterator itEnd = mapParameters.end(); - while( it != itEnd ) - { - size_t pos = 0; - while( (pos = str.find( it->first, pos )) != std::string::npos ) - { - str.replace( pos, it->first.length(), it->second ); - pos += it->second.length(); - } - it++; - } - - std::vector v( str.begin(), str.end() ); - str.clear(); - return shader::compileShader( shader, type, v ); -} - -bool shader::compileShader(GLuint *shader, const GLenum type, - const GLchar *source, const int32_t iSize) -{ - if( source == NULL || iSize <= 0 ) - return false; - - *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 - - GLint status; - glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); - if (status == 0) { - glDeleteShader(*shader); - return false; - } - - return true; -} - -bool shader::compileShader(GLuint *shader, const GLenum type, - std::vector& data) -{ - if( !data.size() ) - return false; - - const GLchar *source = (GLchar *) &data[0]; - int32_t iSize = data.size(); - return shader::compileShader( shader, type, source, iSize ); -} - -bool shader::compileShader(GLuint *shader, const GLenum type, - const char *strFileName) { - std::vector data; - bool b = JNIHelper::readFile(strFileName, data); - if (!b) - { - LOGI("Can not open a file:%s", strFileName); - return false; - } - - return shader::compileShader( shader, type, data ); -} - -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; -} diff --git a/ndk/sources/android/helper/shader.h b/ndk/sources/android/helper/shader.h deleted file mode 100644 index 3cdfeb4cd..000000000 --- a/ndk/sources/android/helper/shader.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SHADER_H_ -#define SHADER_H_ - -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -#include "JNIHelper.h" - -/****************************************************************** - * Shader compiler helper - * - * compileShader() with std::map helps patching on a shader on the fly. - * For a example, - * map : %KEY% -> %VALUE% replaces all %KEY% entries in the given shader code to %VALUE" - * - */ -class shader { -public: - static bool compileShader(GLuint *shader, const GLenum type, - std::vector& data); - static bool compileShader(GLuint *shader, const GLenum type, - const GLchar *source, const int32_t iSize); - static bool compileShader(GLuint *shader, const GLenum type, - const char *strFileName); - static bool compileShader(GLuint *shader, const GLenum type, - const char *strFileName, const std::map& mapParameters); - static bool linkProgram(const GLuint prog); - static bool validateProgram(const GLuint prog); -}; - - - - -#endif /* SHADER_H_ */ diff --git a/ndk/sources/android/helper/tapCamera.cpp b/ndk/sources/android/helper/tapCamera.cpp deleted file mode 100644 index 0b0f94b02..000000000 --- a/ndk/sources/android/helper/tapCamera.cpp +++ /dev/null @@ -1,313 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//---------------------------------------------------------- -// tapCamera.cpp -// Camera control with tap -// -//---------------------------------------------------------- - -#include -#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), - _bPinching( false ), - _bMomentum(false), - _fBallRadius( 0.75f ), - _fPinchStartDistanceSQ( 0.f ), - _fRotation( 0.f ), - _fRotationStart( 0.f ), - _fRotationNow( 0.f ), - _fMomentumSteps( 0.f ), - _fFlipZ( 0.f ) -{ - //Init offset - initParameters(); - - _vFlip = vec2( 1.f, -1.f ); - _fFlipZ = -1.f; - _vPinchTransformFactor = vec3( 1.f, 1.f, 1.f ); - - _vBallCenter = vec2( 0, 0 ); - _vBallNow = vec2( 0, 0 ); - _vBallDown = vec2( 0, 0 ); - - _vecPinchStart = vec2( 0, 0 ); - _vecPinchStartCenter = vec2( 0, 0 ); - - _vFlip = vec2( 0, 0 ); - -} - -void tapCamera::initParameters() -{ - //Init parameters - _vecOffset = vec3(); - _vecOffsetNow = vec3(); - - _qBallRot = quaternion(); - _qBallNow = quaternion(); - _qBallNow.toMatrix(_mRotation); - _fRotation = 0.f; - - _vDragDelta = vec2(); - _vecOffsetDelta = vec3(); - - _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 * _vPinchTransformFactor; - - _mTransform = mat4::translation(vec); -} - -mat4& tapCamera::getRotationMatrix() -{ - return _mRotation; -} - -mat4& tapCamera::getTransformMatrix() -{ - return _mTransform; -} - -void tapCamera::reset(const bool bAnimate) -{ - initParameters(); - update(); - -} - -//---------------------------------------------------------- -//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; - - float f = _fPinchStartDistanceSQ / fDistanceSQ; - if( f < 1.f) - f = -1.f / f + 1.0f; - else - f = f - 1.f; - if( isnan(f) ) f = 0.f; - - vec = (v1 + v2) / 2.f - _vecPinchStartCenter; - _vecOffsetNow = vec3( vec, - _fFlipZ * 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); -} diff --git a/ndk/sources/android/helper/tapCamera.h b/ndk/sources/android/helper/tapCamera.h deleted file mode 100644 index 0c013c54d..000000000 --- a/ndk/sources/android/helper/tapCamera.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once -#include -#include -#include - -#include "JNIHelper.h" -#include "vecmath.h" -#include "interpolator.h" - -/****************************************************************** - * Camera control helper class with a tap gesture - * This class is mainly used for 3D space camera control in samples. - * - */ -class tapCamera -{ -private: - //Trackball - vec2 _vBallCenter; - float _fBallRadius; - quaternion _qBallNow; - quaternion _qBallDown; - vec2 _vBallNow; - vec2 _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(); - - vec3 _vPinchTransformFactor; - -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 setPinchTransformFactor(const float fX, const float fY, const float fZ) - { - _vPinchTransformFactor = vec3( fX, fY, fZ); - } - - void reset(const bool bAnimate); - -}; diff --git a/ndk/sources/android/helper/vecmath.cpp b/ndk/sources/android/helper/vecmath.cpp deleted file mode 100644 index 1008523dc..000000000 --- a/ndk/sources/android/helper/vecmath.cpp +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//-------------------------------------------------------------------------------- -// vecmath.cpp -//-------------------------------------------------------------------------------- - -#include "vecmath.h" - -//-------------------------------------------------------------------------------- -// vec3 -//-------------------------------------------------------------------------------- -vec3::vec3(const vec4& vec) -{ - x = vec.x; y = vec.y; z = vec.z; -} - -//-------------------------------------------------------------------------------- -// vec4 -//-------------------------------------------------------------------------------- -vec4 vec4::operator*(const mat4& rhs) const -{ - vec4 out; - out.x = x * rhs.f[0] + y * rhs.f[1] + z * rhs.f[2] + w * rhs.f[3]; - out.y = x * rhs.f[4] + y * rhs.f[5] + z * rhs.f[6] + w * rhs.f[7]; - out.z = x * rhs.f[8] + y * rhs.f[9] + z * rhs.f[10] + w * rhs.f[11]; - out.w = x * rhs.f[12] + y * rhs.f[13] + z * rhs.f[14] + w * rhs.f[15]; - return out; -} - -//-------------------------------------------------------------------------------- -// mat4 -//-------------------------------------------------------------------------------- -mat4::mat4() -{ - for(int32_t i = 0; i < 16; ++i) - f[i] = 0.f; -} - -mat4::mat4(const float* mIn ) -{ - for(int32_t i = 0; i < 16; ++i) - f[i] = mIn[i]; -} - -mat4 mat4::operator*(const mat4& rhs) const -{ - mat4 ret; - ret.f[0] = f[0]*rhs.f[0] + f[4]*rhs.f[1] + f[8]*rhs.f[2] + f[12]*rhs.f[3]; - ret.f[1] = f[1]*rhs.f[0] + f[5]*rhs.f[1] + f[9]*rhs.f[2] + f[13]*rhs.f[3]; - ret.f[2] = f[2]*rhs.f[0] + f[6]*rhs.f[1] + f[10]*rhs.f[2] + f[14]*rhs.f[3]; - ret.f[3] = f[3]*rhs.f[0] + f[7]*rhs.f[1] + f[11]*rhs.f[2] + f[15]*rhs.f[3]; - - ret.f[4] = f[0]*rhs.f[4] + f[4]*rhs.f[5] + f[8]*rhs.f[6] + f[12]*rhs.f[7]; - ret.f[5] = f[1]*rhs.f[4] + f[5]*rhs.f[5] + f[9]*rhs.f[6] + f[13]*rhs.f[7]; - ret.f[6] = f[2]*rhs.f[4] + f[6]*rhs.f[5] + f[10]*rhs.f[6] + f[14]*rhs.f[7]; - ret.f[7] = f[3]*rhs.f[4] + f[7]*rhs.f[5] + f[11]*rhs.f[6] + f[15]*rhs.f[7]; - - ret.f[8] = f[0]*rhs.f[8] + f[4]*rhs.f[9] + f[8]*rhs.f[10] + f[12]*rhs.f[11]; - ret.f[9] = f[1]*rhs.f[8] + f[5]*rhs.f[9] + f[9]*rhs.f[10] + f[13]*rhs.f[11]; - ret.f[10] = f[2]*rhs.f[8] + f[6]*rhs.f[9] + f[10]*rhs.f[10] + f[14]*rhs.f[11]; - ret.f[11] = f[3]*rhs.f[8] + f[7]*rhs.f[9] + f[11]*rhs.f[10] + f[15]*rhs.f[11]; - - ret.f[12] = f[0]*rhs.f[12] + f[4]*rhs.f[13] + f[8]*rhs.f[14] + f[12]*rhs.f[15]; - ret.f[13] = f[1]*rhs.f[12] + f[5]*rhs.f[13] + f[9]*rhs.f[14] + f[13]*rhs.f[15]; - ret.f[14] = f[2]*rhs.f[12] + f[6]*rhs.f[13] + f[10]*rhs.f[14] + f[14]*rhs.f[15]; - ret.f[15] = f[3]*rhs.f[12] + f[7]*rhs.f[13] + f[11]*rhs.f[14] + f[15]*rhs.f[15]; - - return ret; -} - -vec4 mat4::operator*(const vec4& rhs) const -{ - vec4 ret; - ret.x = rhs.x*f[0] + rhs.y*f[4] + rhs.z*f[8] + rhs.w*f[12]; - ret.y = rhs.x*f[1] + rhs.y*f[5] + rhs.z*f[9] + rhs.w*f[13]; - ret.z = rhs.x*f[2] + rhs.y*f[6] + rhs.z*f[10] + rhs.w*f[14]; - ret.w = rhs.x*f[3] + rhs.y*f[7] + rhs.z*f[11] + rhs.w*f[15]; - return ret; -} - -mat4 mat4::inverse() -{ - mat4 ret; - float det_1; - float pos = 0; - float neg = 0; - float temp; - - temp = f[0] * f[5] * f[10]; - if (temp >= 0) pos += temp; else neg += temp; - temp = f[4] * f[9] * f[2]; - if (temp >= 0) pos += temp; else neg += temp; - temp = f[8] * f[1] * f[6]; - if (temp >= 0) pos += temp; else neg += temp; - temp = -f[8] * f[5] * f[2]; - if (temp >= 0) pos += temp; else neg += temp; - temp = -f[4] * f[1] * f[10]; - if (temp >= 0) pos += temp; else neg += temp; - temp = -f[0] * f[9] * f[6]; - if (temp >= 0) pos += temp; else neg += temp; - det_1 = pos + neg; - - if (det_1 == 0.0) - { - //Error - } - else - { - det_1 = 1.0f / det_1; - ret.f[0] = ( f[ 5] * f[10] - f[ 9] * f[ 6] ) * det_1; - ret.f[1] = -( f[ 1] * f[10] - f[ 9] * f[ 2] ) * det_1; - ret.f[2] = ( f[ 1] * f[ 6] - f[ 5] * f[ 2] ) * det_1; - ret.f[4] = -( f[ 4] * f[10] - f[ 8] * f[ 6] ) * det_1; - ret.f[5] = ( f[ 0] * f[10] - f[ 8] * f[ 2] ) * det_1; - ret.f[6] = -( f[ 0] * f[ 6] - f[ 4] * f[ 2] ) * det_1; - ret.f[8] = ( f[ 4] * f[ 9] - f[ 8] * f[ 5] ) * det_1; - ret.f[9] = -( f[ 0] * f[ 9] - f[ 8] * f[ 1] ) * det_1; - ret.f[10] = ( f[ 0] * f[ 5] - f[ 4] * f[ 1] ) * det_1; - - /* Calculate -C * inverse(A) */ - ret.f[12] = - ( f[12] *ret.f[0] + f[13] * ret.f[4] + f[14] *ret.f[8] ); - ret.f[13] = - ( f[12] * ret.f[1] + f[13] * ret.f[5] + f[14] * ret.f[9] ); - ret.f[14] = - ( f[12] * ret.f[2] + f[13] * ret.f[6] + f[14] * ret.f[10] ); - - ret.f[ 3] = 0.0f; - ret.f[ 7] = 0.0f; - ret.f[11] = 0.0f; - ret.f[15] = 1.0f; - } - - *this = ret; - return *this; -} - -//-------------------------------------------------------------------------------- -// Misc -//-------------------------------------------------------------------------------- -mat4 mat4::rotationX( - const float fAngle) -{ - mat4 ret; - float fCosine, fSine; - - fCosine = cosf(fAngle); - fSine = sinf(fAngle); - - ret.f[ 0]=1.0f; ret.f[ 4]=0.0f; ret.f[ 8]=0.0f; ret.f[12]=0.0f; - ret.f[ 1]=0.0f; ret.f[ 5]=fCosine; ret.f[ 9]=fSine; ret.f[13]=0.0f; - ret.f[ 2]=0.0f; ret.f[ 6]=-fSine; ret.f[10]=fCosine; ret.f[14]=0.0f; - ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f; - return ret; -} - -mat4 mat4::rotationY( - const float fAngle) -{ - mat4 ret; - float fCosine, fSine; - - fCosine = cosf(fAngle); - fSine = sinf(fAngle); - - ret.f[ 0]=fCosine; ret.f[ 4]=0.0f; ret.f[ 8]=-fSine; ret.f[12]=0.0f; - ret.f[ 1]=0.0f; ret.f[ 5]=1.0f; ret.f[ 9]=0.0f; ret.f[13]=0.0f; - ret.f[ 2]=fSine; ret.f[ 6]=0.0f; ret.f[10]=fCosine; ret.f[14]=0.0f; - ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f; - return ret; - -} - -mat4 mat4::rotationZ( - const float fAngle) -{ - mat4 ret; - float fCosine, fSine; - - fCosine = cosf(fAngle); - fSine = sinf(fAngle); - - ret.f[ 0]=fCosine; ret.f[ 4]=fSine; ret.f[ 8]=0.0f; ret.f[12]=0.0f; - ret.f[ 1]=-fSine; ret.f[ 5]=fCosine; ret.f[ 9]=0.0f; ret.f[13]=0.0f; - ret.f[ 2]=0.0f; ret.f[ 6]=0.0f; ret.f[10]=1.0f; ret.f[14]=0.0f; - ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f; - return ret; -} - -mat4 mat4::translation( - const float fX, - const float fY, - const float fZ) -{ - mat4 ret; - ret.f[ 0]=1.0f; ret.f[ 4]=0.0f; ret.f[ 8]=0.0f; ret.f[12]=fX; - ret.f[ 1]=0.0f; ret.f[ 5]=1.0f; ret.f[ 9]=0.0f; ret.f[13]=fY; - ret.f[ 2]=0.0f; ret.f[ 6]=0.0f; ret.f[10]=1.0f; ret.f[14]=fZ; - ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f; - return ret; -} - -mat4 mat4::translation( - const vec3 vec) -{ - mat4 ret; - ret.f[ 0]=1.0f; ret.f[ 4]=0.0f; ret.f[ 8]=0.0f; ret.f[12]=vec.x; - ret.f[ 1]=0.0f; ret.f[ 5]=1.0f; ret.f[ 9]=0.0f; ret.f[13]=vec.y; - ret.f[ 2]=0.0f; ret.f[ 6]=0.0f; ret.f[10]=1.0f; ret.f[14]=vec.z; - ret.f[ 3]=0.0f; ret.f[ 7]=0.0f; ret.f[11]=0.0f; ret.f[15]=1.0f; - return ret; -} - -mat4 mat4::perspective( - float width, - float height, - float nearPlane, float farPlane) -{ - float n2 = 2.0f * nearPlane; - float rcpnmf = 1.f/(nearPlane - farPlane); - - mat4 result; - result.f[0] = n2 / width; result.f[4] = 0; result.f[8] = 0; result.f[12] = 0; - result.f[1] = 0; result.f[5] = n2 / height; result.f[9] = 0; result.f[13] = 0; - result.f[2] = 0; result.f[6] = 0; result.f[10] = (farPlane+nearPlane)*rcpnmf; result.f[14] = farPlane*rcpnmf*n2; - result.f[3] = 0; result.f[7] = 0; result.f[11] = -1.0; result.f[15]=0; - - return result; -} - -mat4 mat4::lookAt(const vec3& vEye, const vec3& vAt, const vec3& vUp) -{ - vec3 vForward, vUpNorm, vSide; - mat4 result; - - vForward.x = vEye.x - vAt.x; - vForward.y = vEye.y - vAt.y; - vForward.z = vEye.z - vAt.z; - - vForward.normalize(); - vUpNorm = vUp; - vUpNorm.normalize(); - vSide = vUpNorm.cross( vForward); - vUpNorm = vForward.cross(vSide); - - result.f[0]=vSide.x; result.f[4]=vSide.y; result.f[8]=vSide.z; result.f[12]=0; - result.f[1]=vUpNorm.x; result.f[5]=vUpNorm.y; result.f[9]=vUpNorm.z; result.f[13]=0; - result.f[2]=vForward.x; result.f[6]=vForward.y; result.f[10]=vForward.z; result.f[14]=0; - result.f[3]=0; result.f[7]=0; result.f[11]=0; result.f[15]=1.0; - - result.postTranslate(-vEye.x, -vEye.y, -vEye.z); - return result; -} - diff --git a/ndk/sources/android/helper/vecmath.h b/ndk/sources/android/helper/vecmath.h deleted file mode 100644 index a959dff6e..000000000 --- a/ndk/sources/android/helper/vecmath.h +++ /dev/null @@ -1,1051 +0,0 @@ -/* - * Copyright 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef VECMATH_H_ -#define VECMATH_H_ - -#include -#include "JNIHelper.h" - -/****************************************************************** - * Helper class for vector math operations - * Currently all implementations are in pure C++. - * Each class is an opaque class so caller does not have a direct access - * to each element. This is for an ease of future optimization to use vector operations. - * - */ - -class vec2; -class vec3; -class vec4; -class mat4; - -/****************************************************************** - * 2 elements vector class - * - */ -class vec2 -{ -private: - float x,y; - -public: - friend class vec3; - friend class vec4; - friend class mat4; - friend class quaternion; - - vec2() - { - x = y = 0.f; - } - - vec2(const float fX, const float fY) - { - x = fX; y = fY; - } - - vec2(const vec2& vec) - { - x = vec.x; - y = vec.y; - } - - vec2(const float* pVec) - { - x = (*pVec++); y = (*pVec++); - } - - //Operators - vec2 operator*(const vec2& rhs) const - { - vec2 ret; - ret.x = x*rhs.x; - ret.y = y*rhs.y; - return ret; - } - - vec2 operator/(const vec2& rhs) const - { - vec2 ret; - ret.x = x/rhs.x; - ret.y = y/rhs.y; - return ret; - } - - vec2 operator+(const vec2& rhs) const - { - vec2 ret; - ret.x = x + rhs.x; - ret.y = y + rhs.y; - return ret; - } - - vec2 operator-(const vec2& rhs) const - { - vec2 ret; - ret.x = x - rhs.x; - ret.y = y - rhs.y; - return ret; - } - - vec2& operator+=(const vec2& rhs) - { - x += rhs.x; - y += rhs.y; - return *this; - } - - vec2& operator-=(const vec2& rhs) - { - x -= rhs.x; - y -= rhs.y; - return *this; - } - - vec2& operator*=(const vec2& rhs) - { - x *= rhs.x; - y *= rhs.y; - return *this; - } - - vec2& operator/=(const vec2& rhs) - { - x /= rhs.x; - y /= rhs.y; - return *this; - } - - //External operators - friend vec2 operator-(const vec2& rhs) { return vec2(rhs) *= -1; } - - friend vec2 operator*(const float lhs, const vec2& rhs) - { - vec2 ret; - ret.x = lhs * rhs.x; - ret.y = lhs * rhs.y; - return ret; - } - - friend vec2 operator/(const float lhs, const vec2& rhs) - { - vec2 ret; - ret.x = lhs / rhs.x; - ret.y = lhs / rhs.y; - return ret; - } - - //Operators with float - vec2 operator*(const float& rhs) const - { - vec2 ret; - ret.x = x * rhs; - ret.y = y * rhs; - return ret; - } - - vec2& operator*=(const float& rhs) - { - x = x * rhs; - y = y * rhs; - return *this; - } - - vec2 operator/(const float& rhs) const - { - vec2 ret; - ret.x = x / rhs; - ret.y = y / rhs; - return ret; - } - - vec2& operator/=(const float& rhs) - { - x = x / rhs; - y = y / rhs; - return *this; - } - - //Compare - bool operator==(const vec2& rhs) const - { - if(x != rhs.x || y != rhs.y ) return false; - return true; - } - - bool operator!=(const vec2& rhs) const - { - if(x == rhs.x) - return false; - - return true; - } - - float length() const - { - return sqrtf(x*x + y*y); - } - - vec2 normalize() - { - float len = length(); - x = x / len; - y = y / len; - return *this; - } - - float dot(const vec2& rhs) - { - return x * rhs.x + y*rhs.y; - } - - bool validate() - { - if( isnan( x ) || isnan( y ) ) - return false; - return true; - } - - void value(float& fX, float& fY) - { - fX = x; - fY = y; - } - - void dump() - { - LOGI("vec2 %f %f", x, y); - } -}; - -/****************************************************************** - * 3 elements vector class - * - */ -class vec3 -{ -private: - float x,y,z; - -public: - friend class vec4; - friend class mat4; - friend class quaternion; - - vec3() - { - x = y = z = 0.f; - } - - vec3(const float fX, const float fY, const float fZ) - { - x = fX; y = fY; z = fZ; - } - - vec3(const vec3& vec) - { - x = vec.x; - y = vec.y; - z = vec.z; - } - - vec3(const float* pVec) - { - x = (*pVec++); y = (*pVec++); z = *pVec; - } - - vec3(const vec2& vec, float f ) - { - x = vec.x; - y = vec.y; - z = f; - } - - vec3(const vec4& vec ); - - //Operators - vec3 operator*(const vec3& rhs) const - { - vec3 ret; - ret.x = x*rhs.x; - ret.y = y*rhs.y; - ret.z = z*rhs.z; - return ret; - } - - vec3 operator/(const vec3& rhs) const - { - vec3 ret; - ret.x = x/rhs.x; - ret.y = y/rhs.y; - ret.z = z/rhs.z; - return ret; - } - - vec3 operator+(const vec3& rhs) const - { - vec3 ret; - ret.x = x + rhs.x; - ret.y = y + rhs.y; - ret.z = z + rhs.z; - return ret; - } - - vec3 operator-(const vec3& rhs) const - { - vec3 ret; - ret.x = x - rhs.x; - ret.y = y - rhs.y; - ret.z = z - rhs.z; - return ret; - } - - vec3& operator+=(const vec3& rhs) - { - x += rhs.x; - y += rhs.y; - z += rhs.z; - return *this; - } - - vec3& operator-=(const vec3& rhs) - { - x -= rhs.x; - y -= rhs.y; - z -= rhs.z; - return *this; - } - - vec3& operator*=(const vec3& rhs) - { - x *= rhs.x; - y *= rhs.y; - z *= rhs.z; - return *this; - } - - vec3& operator/=(const vec3& rhs) - { - x /= rhs.x; - y /= rhs.y; - z /= rhs.z; - return *this; - } - - //External operators - friend vec3 operator-(const vec3& rhs) { return vec3(rhs) *= -1; } - - friend vec3 operator*(const float lhs, const vec3& rhs) - { - vec3 ret; - ret.x = lhs * rhs.x; - ret.y = lhs * rhs.y; - ret.z = lhs * rhs.z; - return ret; - } - - friend vec3 operator/(const float lhs, const vec3& rhs) - { - vec3 ret; - ret.x = lhs / rhs.x; - ret.y = lhs / rhs.y; - ret.z = lhs / rhs.z; - return ret; - } - - //Operators with float - vec3 operator*(const float& rhs) const - { - vec3 ret; - ret.x = x * rhs; - ret.y = y * rhs; - ret.z = z * rhs; - return ret; - } - - vec3& operator*=(const float& rhs) - { - x = x * rhs; - y = y * rhs; - z = z * rhs; - return *this; - } - - vec3 operator/(const float& rhs) const - { - vec3 ret; - ret.x = x / rhs; - ret.y = y / rhs; - ret.z = z / rhs; - return ret; - } - - vec3& operator/=(const float& rhs) - { - x = x / rhs; - y = y / rhs; - z = z / rhs; - return *this; - } - - //Compare - bool operator==(const vec3& rhs) const - { - if(x != rhs.x || y != rhs.y || z != rhs.z) return false; - return true; - } - - bool operator!=(const vec3& rhs) const - { - if(x == rhs.x) - return false; - - return true; - } - - float length() const - { - return sqrtf(x*x + y*y + z*z); - } - - vec3 normalize() - { - float len = length(); - x = x / len; - y = y / len; - z = z / len; - return *this; - } - - float dot(const vec3& rhs) - { - return x * rhs.x + y*rhs.y + z*rhs.z; - } - - vec3 cross(const vec3& rhs) - { - vec3 ret; - ret.x = y*rhs.z - z*rhs.y; - ret.y = z*rhs.x - x*rhs.z; - ret.z = x*rhs.y - y*rhs.x; - return ret; - } - - bool validate() - { - if( isnan( x ) || isnan( y ) || isnan( z ) ) - return false; - return true; - } - - void value(float& fX, float& fY, float& fZ) - { - fX = x; - fY = y; - fZ = z; - } - - void dump() - { - LOGI("vec3 %f %f %f", x, y, z); - } -}; - -/****************************************************************** - * 4 elements vector class - * - */ -class vec4 -{ -private: - float x,y,z,w; - -public: - friend class vec3; - friend class mat4; - friend class quaternion; - - vec4() - { - x = y = z = w = 0.f; - } - - vec4(const float fX, const float fY, const float fZ, const float fW ) - { - x = fX; y = fY; z = fZ; w = fW; - } - - vec4(const vec4& vec) - { - x = vec.x; - y = vec.y; - z = vec.z; - w = vec.w; - } - - vec4(const vec3& vec, const float fW ) - { - x = vec.x; y = vec.y; z = vec.z; w = fW; - } - - vec4(const float* pVec) - { - x = (*pVec++); y = (*pVec++); z = *pVec; w = *pVec; - } - - //Operators - vec4 operator*(const vec4& rhs) const - { - vec4 ret; - ret.x = x*rhs.x; - ret.y = y*rhs.y; - ret.z = z*rhs.z; - ret.w = z*rhs.w; - return ret; - } - - vec4 operator/(const vec4& rhs) const - { - vec4 ret; - ret.x = x/rhs.x; - ret.y = y/rhs.y; - ret.z = z/rhs.z; - ret.w = z/rhs.w; - return ret; - } - - vec4 operator+(const vec4& rhs) const - { - vec4 ret; - ret.x = x+rhs.x; - ret.y = y+rhs.y; - ret.z = z+rhs.z; - ret.w = z+rhs.w; - return ret; - } - - vec4 operator-(const vec4& rhs) const - { - vec4 ret; - ret.x = x-rhs.x; - ret.y = y-rhs.y; - ret.z = z-rhs.z; - ret.w = z-rhs.w; - return ret; - } - - vec4& operator+=(const vec4& rhs) - { - x += rhs.x; - y += rhs.y; - z += rhs.z; - w += rhs.w; - return *this; - } - - vec4& operator-=(const vec4& rhs) - { - x -= rhs.x; - y -= rhs.y; - z -= rhs.z; - w -= rhs.w; - return *this; - } - - vec4& operator*=(const vec4& rhs) - { - x *= rhs.x; - y *= rhs.y; - z *= rhs.z; - w *= rhs.w; - return *this; - } - - vec4& operator/=(const vec4& rhs) - { - x /= rhs.x; - y /= rhs.y; - z /= rhs.z; - w /= rhs.w; - return *this; - } - - //External operators - friend vec4 operator-(const vec4& rhs) { return vec4(rhs) *= -1; } - - friend vec4 operator*(const float lhs, const vec4& rhs) - { - vec4 ret; - ret.x = lhs * rhs.x; - ret.y = lhs * rhs.y; - ret.z = lhs * rhs.z; - ret.w = lhs * rhs.w; - return ret; - } - - friend vec4 operator/(const float lhs, const vec4& rhs) - { - vec4 ret; - ret.x = lhs / rhs.x; - ret.y = lhs / rhs.y; - ret.z = lhs / rhs.z; - ret.w = lhs / rhs.w; - return ret; - } - - //Operators with float - vec4 operator*(const float& rhs) const - { - vec4 ret; - ret.x = x * rhs; - ret.y = y * rhs; - ret.z = z * rhs; - ret.w = w * rhs; - return ret; - } - - vec4& operator*=(const float& rhs) - { - x = x * rhs; - y = y * rhs; - z = z * rhs; - w = w * rhs; - return *this; - } - - vec4 operator/(const float& rhs) const - { - vec4 ret; - ret.x = x / rhs; - ret.y = y / rhs; - ret.z = z /rhs; - ret.w = w /rhs; - return ret; - } - - vec4& operator/=(const float& rhs) - { - x = x / rhs; - y = y / rhs; - z = z / rhs; - w = w / rhs; - return *this; - } - - //Compare - bool operator==(const vec4& rhs) const - { - if(x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w) return false; - return true; - } - - bool operator!=(const vec4& rhs) const - { - if(x == rhs.x) - return false; - - return true; - } - - vec4 operator*(const mat4& rhs) const; - - float length() const - { - return sqrtf(x*x + y*y + z*z + w*w); - } - - vec4 normalize() - { - float len = length(); - x = x / len; - y = y / len; - z = z / len; - w = w / len; - return *this; - } - - float dot(const vec3& rhs) - { - return x * rhs.x + y*rhs.y + z*rhs.z; - } - - vec3 cross(const vec3& rhs) - { - vec3 ret; - ret.x = y*rhs.z - z*rhs.y; - ret.y = z*rhs.x - x*rhs.z; - ret.z = x*rhs.y - y*rhs.x; - return ret; - } - - bool validate() - { - if( isnan( x ) || isnan( y ) || isnan( z ) || isnan( w )) - return false; - return true; - } - - void value(float& fX, float& fY, float& fZ, float& fW) - { - fX = x; - fY = y; - fZ = z; - fW = w; - } -}; - -/****************************************************************** - * 4x4 matrix - * - */ -class mat4 -{ -private: - float f[16]; - -public: - friend class vec3; - friend class vec4; - friend class quaternion; - - mat4(); - mat4(const float*); - - mat4 operator*(const mat4& rhs) const; - vec4 operator*(const vec4& rhs) const; - - mat4 operator+(const mat4& rhs) const - { - mat4 ret; - for(int32_t i=0; i < 16; ++i) - { - ret.f[i] = f[i] + rhs.f[i]; - } - return ret; - } - - mat4 operator-(const mat4& rhs) const - { - mat4 ret; - for(int32_t i=0; i < 16; ++i) - { - ret.f[i] = f[i] - rhs.f[i]; - } - return ret; - } - - mat4& operator+=(const mat4& rhs) - { - for(int32_t i=0; i < 16; ++i) - { - f[i] += rhs.f[i]; - } - return *this; - } - - mat4& operator-=(const mat4& rhs) - { - for(int32_t i=0; i < 16; ++i) - { - f[i] -= rhs.f[i]; - } - return *this; - } - - mat4& operator*=(const mat4& rhs) - { - 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]; - - *this = ret; - return *this; - } - - mat4 operator*(const float rhs) - { - mat4 ret; - for(int32_t i=0; i < 16; ++i) - { - ret.f[i] = f[i] * rhs; - } - return ret; - } - - mat4& operator*=(const float rhs) - { - for(int32_t i=0; i < 16; ++i) - { - f[i] *= rhs; - } - return *this; - } - - mat4& operator=(const mat4& rhs) - { - for(int32_t i=0; i < 16; ++i) - { - f[i] = rhs.f[i]; - } - return *this; - } - - mat4 inverse(); - - mat4 transpose() - { - mat4 ret; - ret.f[0] = f[0]; ret.f[1] = f[4]; ret.f[2] = f[8]; ret.f[3] = f[12]; - ret.f[4] = f[1]; ret.f[5] = f[5]; ret.f[6] = f[9]; ret.f[7] = f[13]; - ret.f[8] = f[2]; ret.f[9] = f[6]; ret.f[10] = f[10]; ret.f[11] = f[14]; - ret.f[12] = f[3]; ret.f[13] = f[7]; ret.f[14] = f[11]; ret.f[15] = f[15]; - *this = ret; - return *this; - } - - mat4& postTranslate(float tx, float ty, float tz) - { - f[12] += (tx*f[0])+(ty*f[4])+(tz*f[8]); - f[13] += (tx*f[1])+(ty*f[5])+(tz*f[9]); - f[14] += (tx*f[2])+(ty*f[6])+(tz*f[10]); - f[15] += (tx*f[3])+(ty*f[7])+(tz*f[11]); - return *this; - } - - float* ptr() { - return f; - } - - //-------------------------------------------------------------------------------- - // Misc - //-------------------------------------------------------------------------------- - static mat4 perspective( - float width, - float height, - float nearPlane, float farPlane); - - static mat4 lookAt(const vec3& vEye, const vec3& vAt, const vec3& vUp); - - static mat4 translation( - const float fX, - const float fY, - const float fZ); - static mat4 translation( - const vec3 vec); - - static mat4 rotationX( - const float fAngle); - - static mat4 rotationY(const float fAngle); - - static mat4 rotationZ(const float fAngle); - - static mat4 identity() - { - mat4 ret; - ret.f[0] = 1.f; ret.f[1] = 0; ret.f[2] = 0; ret.f[3] = 0; - ret.f[4] = 0; ret.f[5] = 1.f; ret.f[6] = 0; ret.f[7] = 0; - ret.f[8] = 0; ret.f[9] = 0; ret.f[10] = 1.f;ret.f[11] = 0; - ret.f[12] = 0; ret.f[13] = 0; ret.f[14] = 0; ret.f[15] = 1.f; - return ret; - } - - void dump() - { - LOGI("%f %f %f %f", f[0], f[1], f[2], f[3]); - LOGI("%f %f %f %f", f[4], f[5], f[6], f[7]); - LOGI("%f %f %f %f", f[8], f[9], f[10], f[11]); - LOGI("%f %f %f %f", f[12], f[13], f[14], f[15]); - } -}; - -/****************************************************************** - * quaternion class - * - */ -class quaternion -{ -private: - float x,y,z,w; - -public: - friend class vec3; - friend class vec4; - friend class mat4; - - quaternion() - { - x = 0.f; - y = 0.f; - z = 0.f; - w = 1.f; - } - - quaternion(const float fX, const float fY, const float fZ, const float fW) - { - x = fX; - y = fY; - z = fZ; - w = fW; - } - - quaternion(const vec3 vec, const float fW) - { - x = vec.x; - y = vec.y; - z = vec.z; - w = fW; - } - - quaternion(const float* p) - { - x = *p++; - y = *p++; - z = *p++; - w = *p++; - } - - quaternion operator*(const quaternion rhs) - { - quaternion ret; - ret.x = x * rhs.w + y * rhs.z - z * rhs.y + w * rhs.x; - ret.y = -x * rhs.z + y * rhs.w + z * rhs.x + w * rhs.y; - ret.z = x * rhs.y - y * rhs.x + z * rhs.w + w * rhs.z; - ret.w = -x * rhs.x - y * rhs.y - z * rhs.z + w * rhs.w; - return ret; - } - - quaternion& operator*=(const quaternion rhs) - { - quaternion ret; - ret.x = x * rhs.w + y * rhs.z - z * rhs.y + w * rhs.x; - ret.y = -x * rhs.z + y * rhs.w + z * rhs.x + w * rhs.y; - ret.z = x * rhs.y - y * rhs.x + z * rhs.w + w * rhs.z; - ret.w = -x * rhs.x - y * rhs.y - z * rhs.z + w * rhs.w; - *this = ret; - return *this; - } - - quaternion conjugate() - { - x = -x; - y = -y; - z = -z; - return *this; - } - - //Non destuctive version - quaternion conjugated() - { - quaternion ret; - ret.x = -x; - ret.y = -y; - ret.z = -z; - ret.w = w; - return ret; - } - - void toMatrix(mat4& mat) - { - float x2 = x * x * 2.0f; - float y2 = y * y * 2.0f; - float z2 = z * z * 2.0f; - float xy = x * y * 2.0f; - float yz = y * z * 2.0f; - float zx = z * x * 2.0f; - float xw = x * w * 2.0f; - float yw = y * w * 2.0f; - float zw = z * w * 2.0f; - - mat.f[0] = 1.0f - y2 - z2; - mat.f[1] = xy + zw; - mat.f[2] = zx - yw; - mat.f[4] = xy - zw; - mat.f[5] = 1.0f - z2 - x2; - mat.f[6] = yz + xw; - mat.f[8] = zx + yw; - mat.f[9] = yz - xw; - mat.f[10] = 1.0f - x2 - y2; - - mat.f[3] = mat.f[7] = mat.f[11] = mat.f[12] = mat.f[13] = mat.f[14] = 0.0f; - mat.f[15] = 1.0f; - } - - void toMatrixPreserveTranslate(mat4& mat) - { - float x2 = x * x * 2.0f; - float y2 = y * y * 2.0f; - float z2 = z * z * 2.0f; - float xy = x * y * 2.0f; - float yz = y * z * 2.0f; - float zx = z * x * 2.0f; - float xw = x * w * 2.0f; - float yw = y * w * 2.0f; - float zw = z * w * 2.0f; - - mat.f[0] = 1.0f - y2 - z2; - mat.f[1] = xy + zw; - mat.f[2] = zx - yw; - mat.f[4] = xy - zw; - mat.f[5] = 1.0f - z2 - x2; - mat.f[6] = yz + xw; - mat.f[8] = zx + yw; - mat.f[9] = yz - xw; - mat.f[10] = 1.0f - x2 - y2; - - mat.f[3] = mat.f[7] = mat.f[11] = 0.0f; - mat.f[15] = 1.0f; - } - - static quaternion rotationAxis(const vec3 axis, const float angle ) - { - quaternion ret; - float s = sinf(angle / 2); - ret.x = s * axis.x; - ret.y = s * axis.y; - ret.z = s * axis.z; - ret.w = cosf( angle / 2 ); - return ret; - } - - void value(float& fX, float& fY, float& fZ, float& fW) - { - fX = x; - fY = y; - fZ = z; - fW = w; - } -}; - -#endif /* VECMATH_H_ */ diff --git a/ndk/sources/android/helper/Android.mk b/ndk/sources/android/ndk_helper/Android.mk similarity index 94% rename from ndk/sources/android/helper/Android.mk rename to ndk/sources/android/ndk_helper/Android.mk index a5c3be4e6..e7134da3b 100644 --- a/ndk/sources/android/helper/Android.mk +++ b/ndk/sources/android/ndk_helper/Android.mk @@ -2,7 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) -LOCAL_MODULE:= helper +LOCAL_MODULE:= ndk_helper LOCAL_SRC_FILES:= JNIHelper.cpp interpolator.cpp tapCamera.cpp gestureDetector.cpp perfMonitor.cpp vecmath.cpp GLContext.cpp shader.cpp gl3stub.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) diff --git a/ndk/sources/android/ndk_helper/GLContext.cpp b/ndk/sources/android/ndk_helper/GLContext.cpp new file mode 100644 index 000000000..edfdd6c94 --- /dev/null +++ b/ndk/sources/android/ndk_helper/GLContext.cpp @@ -0,0 +1,294 @@ +/* + * 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. + */ + +//-------------------------------------------------------------------------------- +// GLContext.cpp +//-------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------- +// includes +//-------------------------------------------------------------------------------- +#include +#include "GLContext.h" +#include "gl3stub.h" + +namespace ndk_helper +{ + +//-------------------------------------------------------------------------------- +// eGLContext +//-------------------------------------------------------------------------------- + +//-------------------------------------------------------------------------------- +// Ctor +//-------------------------------------------------------------------------------- +GLContext::GLContext() : + display_( EGL_NO_DISPLAY ), + surface_( EGL_NO_SURFACE ), + context_( EGL_NO_CONTEXT ), + screen_width_( 0 ), + screen_height_( 0 ), + es3_supported_( false ), + egl_context_initialized_( false ), + gles_initialized_( false ) +{ +} + +void GLContext::InitGLES() +{ + if( gles_initialized_ ) + return; + // + //Initialize OpenGL ES 3 if available + // + const char* versionStr = (const char*) glGetString( GL_VERSION ); + if( strstr( versionStr, "OpenGL ES 3." ) && gl3stubInit() ) + { + es3_supported_ = true; + gl_version_ = 3.0f; + } + else + { + gl_version_ = 2.0f; + } + + gles_initialized_ = true; +} + +//-------------------------------------------------------------------------------- +// Dtor +//-------------------------------------------------------------------------------- +GLContext::~GLContext() +{ + Terminate(); +} + +bool GLContext::Init( ANativeWindow* window ) +{ + if( egl_context_initialized_ ) + return true; + + // + //Initialize EGL + // + window_ = window; + InitEGLSurface(); + InitEGLContext(); + InitGLES(); + + egl_context_initialized_ = true; + + return true; +} + +bool GLContext::InitEGLSurface() +{ + 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 }; + color_size_ = 8; + depth_size_ = 24; + + EGLint num_configs; + eglChooseConfig( display_, attribs, &config_, 1, &num_configs ); + + if( !num_configs ) + { + //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, &num_configs ); + depth_size_ = 16; + } + + if( !num_configs ) + { + LOGW( "Unable to retrieve EGL config" ); + return false; + } + + surface_ = eglCreateWindowSurface( display_, config_, window_, NULL ); + eglQuerySurface( display_, surface_, EGL_WIDTH, &screen_width_ ); + eglQuerySurface( display_, surface_, EGL_HEIGHT, &screen_height_ ); + + /* 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. */ + EGLint format; + eglGetConfigAttrib( display_, config_, EGL_NATIVE_VISUAL_ID, &format ); + ANativeWindow_setBuffersGeometry( window_, 0, 0, format ); + + return true; +} + +bool GLContext::InitEGLContext() +{ + const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, //Request opengl ES2.0 + EGL_NONE }; + context_ = eglCreateContext( display_, config_, NULL, context_attribs ); + + if( eglMakeCurrent( display_, surface_, surface_, context_ ) == EGL_FALSE ) + { + LOGW( "Unable to eglMakeCurrent" ); + return false; + } + + context_valid_ = true; + return true; +} + +EGLint GLContext::Swap() +{ + bool b = eglSwapBuffers( display_, surface_ ); + if( !b ) + { + EGLint err = eglGetError(); + if( err == EGL_BAD_SURFACE ) + { + //Recreate surface + InitEGLSurface(); + return EGL_SUCCESS; //Still consider glContext is valid + } + else if( err == EGL_CONTEXT_LOST || err == EGL_BAD_CONTEXT ) + { + //Context has been lost!! + context_valid_ = false; + Terminate(); + InitEGLContext(); + } + return err; + } + return EGL_SUCCESS; +} + +void GLContext::Terminate() +{ + if( display_ != EGL_NO_DISPLAY ) + { + eglMakeCurrent( display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT ); + if( context_ != EGL_NO_CONTEXT ) + { + eglDestroyContext( display_, context_ ); + } + + if( surface_ != EGL_NO_SURFACE ) + { + eglDestroySurface( display_, surface_ ); + } + eglTerminate( display_ ); + } + + display_ = EGL_NO_DISPLAY; + context_ = EGL_NO_CONTEXT; + surface_ = EGL_NO_SURFACE; + context_valid_ = false; + +} + +EGLint GLContext::Resume( ANativeWindow* window ) +{ + if( egl_context_initialized_ == false ) + { + Init( window ); + return EGL_SUCCESS; + } + + int32_t original_widhth = screen_width_; + int32_t original_height = screen_height_; + + //Create surface + window_ = window; + surface_ = eglCreateWindowSurface( display_, config_, window_, NULL ); + eglQuerySurface( display_, surface_, EGL_WIDTH, &screen_width_ ); + eglQuerySurface( display_, surface_, EGL_HEIGHT, &screen_height_ ); + + if( screen_width_ != original_widhth || screen_height_ != original_height ) + { + //Screen resized + LOGI( "Screen resized" ); + } + + if( eglMakeCurrent( display_, surface_, surface_, context_ ) == EGL_TRUE ) + return EGL_SUCCESS; + + EGLint err = eglGetError(); + LOGW( "Unable to eglMakeCurrent %d", err ); + + if( err == EGL_CONTEXT_LOST ) + { + //Recreate context + LOGI( "Re-creating egl context" ); + InitEGLContext(); + } + else + { + //Recreate surface + Terminate(); + InitEGLSurface(); + InitEGLContext(); + } + + return err; + +} + +void GLContext::Suspend() +{ + if( surface_ != EGL_NO_SURFACE ) + { + eglDestroySurface( display_, surface_ ); + surface_ = EGL_NO_SURFACE; + } +} + +bool GLContext::Invalidate() +{ + Terminate(); + + egl_context_initialized_ = false; + return true; +} + +bool GLContext::CheckExtension( const char* extension ) +{ + if( extension == NULL ) + return false; + + std::string extensions = std::string( (char*) glGetString( GL_EXTENSIONS ) ); + std::string str = std::string( extension ); + str.append( " " ); + + size_t pos = 0; + if( extensions.find( extension, pos ) != std::string::npos ) + { + return true; + } + + return false; +} + +} //namespace ndkHelper diff --git a/ndk/sources/android/helper/GLContext.h b/ndk/sources/android/ndk_helper/GLContext.h similarity index 60% rename from ndk/sources/android/helper/GLContext.h rename to ndk/sources/android/ndk_helper/GLContext.h index 29692f283..bbdbd198c 100644 --- a/ndk/sources/android/helper/GLContext.h +++ b/ndk/sources/android/ndk_helper/GLContext.h @@ -17,16 +17,18 @@ //-------------------------------------------------------------------------------- // glContext.h //-------------------------------------------------------------------------------- - #ifndef GLCONTEXT_H_ #define GLCONTEXT_H_ -#include +#include +#include #include -#include -#include + #include "JNIHelper.h" +namespace ndk_helper +{ + //-------------------------------------------------------------------------------- // Constants //-------------------------------------------------------------------------------- @@ -42,36 +44,44 @@ * * Also the class initializes OpenGL ES3 when the compatible driver is installed in the device. * getGLVersion() returns 3.0~ when the device supports OpenGLES3.0 + * + * Thread safety: OpenGL context is expecting used within dedicated single thread, + * thus GLContext class is not designed as a thread-safe */ class GLContext { private: - //ELG configurations - ANativeWindow* _window; - EGLDisplay _display; - EGLSurface _surface; - EGLContext _context; - EGLConfig _config; + //EGL configurations + ANativeWindow* window_; + EGLDisplay display_; + EGLSurface surface_; + EGLContext context_; + EGLConfig config_; //Screen parameters - int32_t _iWidth; - int32_t _iHeight; - int32_t _iColorSize; - int32_t _iDepthSize; + int32_t screen_width_; + int32_t screen_height_; + int32_t color_size_; + int32_t depth_size_; //Flags - bool _bGLESInitialized; - bool _bEGLContextInitialized; - bool _bES3Support; - float _fGLVersion; + bool gles_initialized_; + bool egl_context_initialized_; + bool es3_supported_; + float gl_version_; + bool context_valid_; - void initGLES(); - bool _bContextValid; - void terminate(); - bool initEGLSurface(); - bool initEGLContext(); + void InitGLES(); + void Terminate(); + bool InitEGLSurface(); + bool InitEGLContext(); + + GLContext( GLContext const& ); + void operator=( GLContext const& ); + GLContext(); + virtual ~GLContext(); public: - static GLContext* getInstance() + static GLContext* GetInstance() { //Singleton static GLContext instance; @@ -79,26 +89,37 @@ public: return &instance; } - GLContext( GLContext const& ); - void operator=( GLContext const& ); + bool Init( ANativeWindow* window ); + EGLint Swap(); + bool Invalidate(); - GLContext(); - virtual ~GLContext(); + void Suspend(); + EGLint Resume( ANativeWindow* window ); - bool init( ANativeWindow* window ); - EGLint swap(); - bool invalidate(); + int32_t GetScreenWidth() + { + return screen_width_; + } + int32_t GetScreenHeight() + { + return screen_height_; + } - void suspend(); - EGLint resume(ANativeWindow* window); - - int32_t getScreenWidth() { return _iWidth; } - int32_t getScreenHeight() { return _iHeight; } - - int32_t getBufferColorSize() { return _iColorSize; } - int32_t getBufferDepthSize() { return _iDepthSize; } - float getGLVersion() { return _fGLVersion; } - bool checkExtension( const char* extension ); + int32_t GetBufferColorSize() + { + return color_size_; + } + int32_t GetBufferDepthSize() + { + return depth_size_; + } + float GetGLVersion() + { + return gl_version_; + } + bool CheckExtension( const char* extension ); }; +} //namespace ndkHelper + #endif /* GLCONTEXT_H_ */ diff --git a/ndk/sources/android/ndk_helper/JNIHelper.cpp b/ndk/sources/android/ndk_helper/JNIHelper.cpp new file mode 100644 index 000000000..e06520a4f --- /dev/null +++ b/ndk/sources/android/ndk_helper/JNIHelper.cpp @@ -0,0 +1,376 @@ +/* + * 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 +#include +#include +#include + +#include "JNIHelper.h" + +namespace ndk_helper +{ + +#define CLASS_NAME "android/app/NativeActivity" + +//--------------------------------------------------------------------------- +//JNI Helper functions +//--------------------------------------------------------------------------- + +//--------------------------------------------------------------------------- +//Singleton +//--------------------------------------------------------------------------- +JNIHelper* JNIHelper::GetInstance() +{ + static JNIHelper helper; + return &helper; +} + +//--------------------------------------------------------------------------- +//Ctor +//--------------------------------------------------------------------------- +JNIHelper::JNIHelper() +{ + pthread_mutex_init( &mutex_, NULL ); +} + +//--------------------------------------------------------------------------- +//Dtor +//--------------------------------------------------------------------------- +JNIHelper::~JNIHelper() +{ + pthread_mutex_lock( &mutex_ ); + + JNIEnv *env; + activity_->vm->AttachCurrentThread( &env, NULL ); + + env->DeleteGlobalRef( jni_helper_java_ref_ ); + env->DeleteGlobalRef( jni_helper_java_class_ ); + + activity_->vm->DetachCurrentThread(); + + pthread_mutex_destroy( &mutex_ ); +} + +//--------------------------------------------------------------------------- +//Init +//--------------------------------------------------------------------------- +void JNIHelper::Init( ANativeActivity* activity, + const char* helper_class_name ) +{ + JNIHelper& helper = *GetInstance(); + pthread_mutex_lock( &helper.mutex_ ); + + helper.activity_ = activity; + + JNIEnv *env; + helper.activity_->vm->AttachCurrentThread( &env, NULL ); + + //Retrieve app name + jclass android_content_Context = env->GetObjectClass( helper.activity_->clazz ); + jmethodID midGetPackageName = env->GetMethodID( android_content_Context, "getPackageName", + "()Ljava/lang/String;" ); + + jstring packageName = (jstring) env->CallObjectMethod( helper.activity_->clazz, + midGetPackageName ); + const char* appname = env->GetStringUTFChars( packageName, NULL ); + helper.app_name_ = std::string( appname ); + + jclass cls = helper.RetrieveClass( env, helper_class_name ); + helper.jni_helper_java_class_ = (jclass) env->NewGlobalRef( cls ); + + jmethodID constructor = env->GetMethodID( helper.jni_helper_java_class_, "", "()V" ); + helper.jni_helper_java_ref_ = env->NewObject( helper.jni_helper_java_class_, constructor ); + helper.jni_helper_java_ref_ = env->NewGlobalRef( helper.jni_helper_java_ref_ ); + + env->ReleaseStringUTFChars( packageName, appname ); + helper.activity_->vm->DetachCurrentThread(); + + pthread_mutex_unlock( &helper.mutex_ ); +} + +//--------------------------------------------------------------------------- +//readFile +//--------------------------------------------------------------------------- +bool JNIHelper::ReadFile( const char* fileName, + std::vector* buffer_ref ) +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized.Call init() to initialize the helper" ); + return false; + } + + //First, try reading from externalFileDir; + JNIEnv *env; + jmethodID mid; + + pthread_mutex_lock( &mutex_ ); + activity_->vm->AttachCurrentThread( &env, NULL ); + + jstring str_path = GetExternalFilesDirJString( env ); + const char* path = env->GetStringUTFChars( str_path, NULL ); + std::string s( path ); + + if( fileName[0] != '/' ) + { + s.append( "/" ); + } + s.append( fileName ); + std::ifstream f( s.c_str(), std::ios::binary ); + + env->DeleteLocalRef( str_path ); + env->ReleaseStringUTFChars( str_path, 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_ref->reserve( fileSize ); + buffer_ref->assign( std::istreambuf_iterator( f ), std::istreambuf_iterator() ); + f.close(); + pthread_mutex_unlock( &mutex_ ); + return true; + } + else + { + //Fallback to assetManager + AAssetManager* assetManager = activity_->assetManager; + AAsset* assetFile = AAssetManager_open( assetManager, fileName, AASSET_MODE_BUFFER ); + if( !assetFile ) + { + pthread_mutex_unlock( &mutex_ ); + return false; + } + uint8_t* data = (uint8_t*) AAsset_getBuffer( assetFile ); + int32_t size = AAsset_getLength( assetFile ); + if( data == NULL ) + { + AAsset_close( assetFile ); + + LOGI( "Failed to load:%s", fileName ); + pthread_mutex_unlock( &mutex_ ); + return false; + } + + buffer_ref->reserve( size ); + buffer_ref->assign( data, data + size ); + + AAsset_close( assetFile ); + pthread_mutex_unlock( &mutex_ ); + return true; + } +} + +std::string JNIHelper::GetExternalFilesDir() +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" ); + return std::string( "" ); + } + + pthread_mutex_lock( &mutex_ ); + + //First, try reading from externalFileDir; + JNIEnv *env; + jmethodID mid; + + activity_->vm->AttachCurrentThread( &env, NULL ); + + jstring strPath = GetExternalFilesDirJString( env ); + const char* path = env->GetStringUTFChars( strPath, NULL ); + std::string s( path ); + + env->DeleteLocalRef( strPath ); + env->ReleaseStringUTFChars( strPath, path ); + activity_->vm->DetachCurrentThread(); + + pthread_mutex_unlock( &mutex_ ); + return s; +} + +uint32_t JNIHelper::LoadTexture( const char* file_name ) +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" ); + return 0; + } + + JNIEnv *env; + jmethodID mid; + + pthread_mutex_lock( &mutex_ ); + activity_->vm->AttachCurrentThread( &env, NULL ); + + jstring name = env->NewStringUTF( file_name ); + + 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( jni_helper_java_class_, "loadTexture", "(Ljava/lang/String;)Z" ); + jboolean ret = env->CallBooleanMethod( jni_helper_java_ref_, mid, name ); + if( !ret ) + { + glDeleteTextures( 1, &tex ); + tex = -1; + LOGI( "Texture load failed %s", file_name ); + } + + //Generate mipmap + glGenerateMipmap( GL_TEXTURE_2D ); + + env->DeleteLocalRef( name ); + activity_->vm->DetachCurrentThread(); + pthread_mutex_unlock( &mutex_ ); + + return tex; + +} + +std::string JNIHelper::ConvertString( const char* str, + const char* encode ) +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" ); + return std::string( "" ); + } + + JNIEnv *env; + + pthread_mutex_lock( &mutex_ ); + activity_->vm->AttachCurrentThread( &env, NULL ); + + int32_t iLength = strlen( (const char*) str ); + + jbyteArray array = env->NewByteArray( iLength ); + env->SetByteArrayRegion( array, 0, iLength, (const signed char*) str ); + + jstring strEncode = env->NewStringUTF( encode ); + + jclass cls = env->FindClass( "java/lang/String" ); + jmethodID ctor = env->GetMethodID( cls, "", "([BLjava/lang/String;)V" ); + jstring object = (jstring) env->NewObject( cls, ctor, array, strEncode ); + + const char *cparam = env->GetStringUTFChars( object, NULL ); + + std::string s = std::string( cparam ); + + env->DeleteLocalRef( strEncode ); + env->DeleteLocalRef( object ); + env->ReleaseStringUTFChars( object, cparam ); + activity_->vm->DetachCurrentThread(); + pthread_mutex_unlock( &mutex_ ); + + return s; +} + +//--------------------------------------------------------------------------- +//Audio helpers +//--------------------------------------------------------------------------- +int32_t JNIHelper::GetNativeAudioBufferSize() +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" ); + return 0; + } + + JNIEnv *env; + jmethodID mid; + + pthread_mutex_lock( &mutex_ ); + activity_->vm->AttachCurrentThread( &env, NULL ); + + mid = env->GetMethodID( jni_helper_java_class_, "getNativeAudioBufferSize", "()I" ); + int32_t i = env->CallIntMethod( jni_helper_java_ref_, mid ); + activity_->vm->DetachCurrentThread(); + pthread_mutex_unlock( &mutex_ ); + + return i; +} + +int32_t JNIHelper::GetNativeAudioSampleRate() +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" ); + return 0; + } + + JNIEnv *env; + jmethodID mid; + + pthread_mutex_lock( &mutex_ ); + activity_->vm->AttachCurrentThread( &env, NULL ); + + mid = env->GetMethodID( jni_helper_java_class_, "getNativeAudioSampleRate", "()I" ); + int32_t i = env->CallIntMethod( jni_helper_java_ref_, mid ); + activity_->vm->DetachCurrentThread(); + pthread_mutex_unlock( &mutex_ ); + + return i; +} + +//--------------------------------------------------------------------------- +//Misc implementations +//--------------------------------------------------------------------------- +jclass JNIHelper::RetrieveClass( JNIEnv *jni, + const char* class_name ) +{ + jclass activity_class = jni->FindClass( CLASS_NAME ); + jmethodID get_class_loader = jni->GetMethodID( activity_class, "getClassLoader", + "()Ljava/lang/ClassLoader;" ); + jobject cls = jni->CallObjectMethod( activity_->clazz, get_class_loader ); + jclass class_loader = jni->FindClass( "java/lang/ClassLoader" ); + jmethodID find_class = jni->GetMethodID( class_loader, "loadClass", + "(Ljava/lang/String;)Ljava/lang/Class;" ); + + jstring str_class_name = jni->NewStringUTF( class_name ); + jclass class_retrieved = (jclass) jni->CallObjectMethod( cls, find_class, str_class_name ); + jni->DeleteLocalRef( str_class_name ); + return class_retrieved; +} + +jstring JNIHelper::GetExternalFilesDirJString( JNIEnv *env ) +{ + if( activity_ == NULL ) + { + LOGI( "JNIHelper has not been initialized. Call init() to initialize the helper" ); + return NULL; + } + + // Invoking getExternalFilesDir() java API + jclass cls_Env = env->FindClass( CLASS_NAME ); + 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; +} + +} //namespace ndkHelper diff --git a/ndk/sources/android/ndk_helper/JNIHelper.h b/ndk/sources/android/ndk_helper/JNIHelper.h new file mode 100644 index 000000000..7a77d7616 --- /dev/null +++ b/ndk/sources/android/ndk_helper/JNIHelper.h @@ -0,0 +1,171 @@ +/* + * 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 +#include +#include + +#include +#include + +#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, ndk_helper::JNIHelper::GetInstance()->GetAppName(), __VA_ARGS__)) +#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, ndk_helper::JNIHelper::GetInstance()->GetAppName(), __VA_ARGS__)) +#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, ndk_helper::JNIHelper::GetInstance()->GetAppName(), __VA_ARGS__)) + +namespace ndk_helper +{ + +/****************************************************************** + * Helper functions for JNI calls + * This class wraps JNI calls and provides handy interface calling commonly used features + * in Java SDK. + * Such as + * - loading graphics files (e.g. PNG, JPG) + * - character code conversion + * - retrieving system properties which only supported in Java SDK + * + * NOTE: To use this class, add NDKHelper.java as a corresponding helpers in Java code + */ +class JNIHelper +{ +private: + std::string app_name_; + + ANativeActivity* activity_; + jobject jni_helper_java_ref_; + jclass jni_helper_java_class_; + + //mutex for synchronization + //This class uses singleton pattern and can be invoked from multiple threads, + //each methods locks the mutex for a thread safety + mutable pthread_mutex_t mutex_; + + jstring GetExternalFilesDirJString( JNIEnv *env ); + jclass RetrieveClass( JNIEnv *jni, + const char* class_name ); + + JNIHelper(); + ~JNIHelper(); + JNIHelper( const JNIHelper& rhs ); + JNIHelper& operator=( const JNIHelper& rhs ); + +public: + /* + * To load your own Java classes, JNIHelper requires to be initialized with a ANativeActivity handle. + * This methods need to be called before any call to the helper class. + * Static member of the class + * + * arguments: + * in: activity, pointer to ANativeActivity. Used internally to set up JNI environment + * in: helper_class_name, pointer to Java side helper class name. (e.g. "com/sample/helper/NDKHelper" in samples ) + */ + static void Init( ANativeActivity* activity, + const char* helper_class_name ); + + /* + * Retrieve the singleton object of the helper. + * Static member of the class + + * Methods in the class are designed as thread safe. + */ + static JNIHelper* GetInstance(); + + /* + * Read a file from a strorage. + * First, the method tries to read the file from an external storage. + * If it fails to read, it falls back to use assset manager and try to read the file from APK asset. + * + * arguments: + * in: file_name, file name to read + * out: buffer_ref, pointer to a vector buffer to read a file. + * when the call succeeded, the buffer includes contents of specified file + * when the call failed, contents of the buffer remains same + * return: + * true when file read succeeded + * false when it failed to read the file + */ + bool ReadFile( const char* file_name, + std::vector* buffer_ref ); + + /* + * Load and create OpenGL texture from given file name. + * The method invokes BitmapFactory in Java so it can read jpeg/png formatted files + * + * The methods creates mip-map and set texture parameters like this, + * glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST ); + * glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + * glGenerateMipmap( GL_TEXTURE_2D ); + * + * arguments: + * in: file_name, file name to read, PNG&JPG is supported + * return: + * OpenGL texture name when the call succeeded + * When it failed to load the texture, it returns -1 + */ + uint32_t LoadTexture( const char* file_name ); + + /* + * Convert string from character code other than UTF-8 + * + * arguments: + * in: str, pointer to a string which is encoded other than UTF-8 + * in: encoding, pointer to a character encoding string. + * The encoding string can be any valid java.nio.charset.Charset name + * e.g. "UTF-16", "Shift_JIS" + * return: converted input string as an UTF-8 std::string + */ + std::string ConvertString( const char* str, + const char* encode ); + /* + * Retrieve external file directory through JNI call + * + * return: std::string containing external file diretory + */ + std::string GetExternalFilesDir(); + + /* + * Audio helper + * Retrieves native audio buffer size which is required to achieve low latency audio + * + * return: Native audio buffer size which is a hint to achieve low latency audio + * If the API is not supported (API level < 17), it returns 0 + */ + int32_t GetNativeAudioBufferSize(); + + /* + * Audio helper + * Retrieves native audio sample rate which is required to achieve low latency audio + * + * return: Native audio sample rate which is a hint to achieve low latency audio + */ + int32_t GetNativeAudioSampleRate(); + + /* + * Retrieves application bundle name + * + * return: pointer to an app name string + * + */ + const char* GetAppName() + { + return app_name_.c_str(); + } + +}; + +} //namespace ndkHelper diff --git a/ndk/sources/android/helper/NDKHelper.h b/ndk/sources/android/ndk_helper/NDKHelper.h similarity index 79% rename from ndk/sources/android/helper/NDKHelper.h rename to ndk/sources/android/ndk_helper/NDKHelper.h index da7dc615e..de2a10e63 100644 --- a/ndk/sources/android/helper/NDKHelper.h +++ b/ndk/sources/android/ndk_helper/NDKHelper.h @@ -19,7 +19,13 @@ /****************************************************************** * NDK support helpers + * Utility module to provide misc functionalities that is used widely in native applications, + * such as gesture detection, jni bridge, openGL context etc. * + * The purpose of this module is, + * - Provide best practices using NDK + * - Provide handy utility functions for NDK development + * - Make NDK samples more simpler and readable */ #include "gl3stub.h" //GLES3 stubs #include "GLContext.h" //EGL & OpenGL manager @@ -30,5 +36,4 @@ #include "gestureDetector.h" //Tap/Doubletap/Pinch detector #include "perfMonitor.h" //FPS counter #include "interpolator.h" //Interpolator - #endif diff --git a/ndk/sources/android/ndk_helper/gestureDetector.cpp b/ndk/sources/android/ndk_helper/gestureDetector.cpp new file mode 100644 index 000000000..19d82124b --- /dev/null +++ b/ndk/sources/android/ndk_helper/gestureDetector.cpp @@ -0,0 +1,350 @@ +/* + * 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 "gestureDetector.h" + +//-------------------------------------------------------------------------------- +// gestureDetector.cpp +//-------------------------------------------------------------------------------- +namespace ndk_helper +{ + +//-------------------------------------------------------------------------------- +// includes +//-------------------------------------------------------------------------------- + +//-------------------------------------------------------------------------------- +// GestureDetector +//-------------------------------------------------------------------------------- +GestureDetector::GestureDetector() +{ + dp_factor_ = 1.f; +} + +void GestureDetector::SetConfiguration( AConfiguration* config ) +{ + dp_factor_ = 160.f / AConfiguration_getDensity( config ); +} + +//-------------------------------------------------------------------------------- +// TapDetector +//-------------------------------------------------------------------------------- +GESTURE_STATE TapDetector::Detect( const AInputEvent* motion_event ) +{ + if( AMotionEvent_getPointerCount( motion_event ) > 1 ) + { + //Only support single touch + return false; + } + + int32_t action = AMotionEvent_getAction( motion_event ); + unsigned int flags = action & AMOTION_EVENT_ACTION_MASK; + switch( flags ) + { + case AMOTION_EVENT_ACTION_DOWN: + down_pointer_id_ = AMotionEvent_getPointerId( motion_event, 0 ); + down_x_ = AMotionEvent_getX( motion_event, 0 ); + down_y_ = 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( down_pointer_id_ == AMotionEvent_getPointerId( motion_event, 0 ) ) + { + float x = AMotionEvent_getX( motion_event, 0 ) - down_x_; + float y = AMotionEvent_getY( motion_event, 0 ) - down_y_; + if( x * x + y * y < TOUCH_SLOP * TOUCH_SLOP * dp_factor_ ) + { + LOGI( "TapDetector: Tap detected" ); + return GESTURE_STATE_ACTION; + } + } + } + break; + } + } + return GESTURE_STATE_NONE; +} + +//-------------------------------------------------------------------------------- +// DoubletapDetector +//-------------------------------------------------------------------------------- +GESTURE_STATE DoubletapDetector::Detect( const AInputEvent* motion_event ) +{ + if( AMotionEvent_getPointerCount( motion_event ) > 1 ) + { + //Only support single double tap + return false; + } + + bool tap_detected = tap_detector_.Detect( motion_event ); + + int32_t action = AMotionEvent_getAction( motion_event ); + unsigned int flags = action & AMOTION_EVENT_ACTION_MASK; + switch( flags ) + { + case AMOTION_EVENT_ACTION_DOWN: + { + int64_t eventTime = AMotionEvent_getEventTime( motion_event ); + if( eventTime - last_tap_time_ <= DOUBLE_TAP_TIMEOUT ) + { + float x = AMotionEvent_getX( motion_event, 0 ) - last_tap_x_; + float y = AMotionEvent_getY( motion_event, 0 ) - last_tap_y_; + if( x * x + y * y < DOUBLE_TAP_SLOP * DOUBLE_TAP_SLOP * dp_factor_ ) + { + LOGI( "DoubletapDetector: Doubletap detected" ); + return GESTURE_STATE_ACTION; + } + } + break; + } + case AMOTION_EVENT_ACTION_UP: + if( tap_detected ) + { + last_tap_time_ = AMotionEvent_getEventTime( motion_event ); + last_tap_x_ = AMotionEvent_getX( motion_event, 0 ); + last_tap_y_ = AMotionEvent_getY( motion_event, 0 ); + } + break; + } + return GESTURE_STATE_NONE; +} + +void DoubletapDetector::SetConfiguration( AConfiguration* config ) +{ + dp_factor_ = 160.f / AConfiguration_getDensity( config ); + tap_detector_.SetConfiguration( config ); +} + +//-------------------------------------------------------------------------------- +// PinchDetector +//-------------------------------------------------------------------------------- + +int32_t PinchDetector::FindIndex( const AInputEvent* event, int32_t id ) +{ + int32_t count = AMotionEvent_getPointerCount( event ); + for( uint32_t i = 0; i < count; ++i ) + { + if( id == AMotionEvent_getPointerId( event, i ) ) + return i; + } + return -1; +} + +GESTURE_STATE PinchDetector::Detect( const AInputEvent* event ) +{ + GESTURE_STATE ret = GESTURE_STATE_NONE; + int32_t action = AMotionEvent_getAction( event ); + uint32_t flags = action & AMOTION_EVENT_ACTION_MASK; + event_ = event; + + int32_t count = AMotionEvent_getPointerCount( event ); + switch( flags ) + { + case AMOTION_EVENT_ACTION_DOWN: + vec_pointers_.push_back( AMotionEvent_getPointerId( event, 0 ) ); + break; + case AMOTION_EVENT_ACTION_POINTER_DOWN: + { + int32_t iIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) + >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; + vec_pointers_.push_back( AMotionEvent_getPointerId( event, iIndex ) ); + if( count == 2 ) + { + //Start new pinch + ret = GESTURE_STATE_START; + } + } + break; + case AMOTION_EVENT_ACTION_UP: + vec_pointers_.pop_back(); + break; + case AMOTION_EVENT_ACTION_POINTER_UP: + { + int32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) + >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; + int32_t released_pointer_id = AMotionEvent_getPointerId( event, index ); + + std::vector::iterator it = vec_pointers_.begin(); + std::vector::iterator it_end = vec_pointers_.end(); + int32_t i = 0; + for( ; it != it_end; ++it, ++i ) + { + if( *it == released_pointer_id ) + { + vec_pointers_.erase( it ); + break; + } + } + + if( i <= 1 ) + { + //Reset pinch or drag + if( count != 2 ) + { + //Start new pinch + ret = GESTURE_STATE_START | GESTURE_STATE_END; + } + } + } + break; + case AMOTION_EVENT_ACTION_MOVE: + switch( count ) + { + case 1: + break; + default: + //Multi touch + ret = GESTURE_STATE_MOVE; + break; + } + break; + case AMOTION_EVENT_ACTION_CANCEL: + break; + } + + return ret; +} + +bool PinchDetector::GetPointers( Vec2& v1, Vec2& v2 ) +{ + if( vec_pointers_.size() < 2 ) + return false; + + int32_t index = FindIndex( event_, vec_pointers_[0] ); + if( index == -1 ) + return false; + + float x = AMotionEvent_getX( event_, index ); + float y = AMotionEvent_getY( event_, index ); + + index = FindIndex( event_, vec_pointers_[1] ); + if( index == -1 ) + return false; + + float x2 = AMotionEvent_getX( event_, index ); + float y2 = AMotionEvent_getY( event_, index ); + + v1 = Vec2( x, y ); + v2 = Vec2( x2, y2 ); + + return true; +} + +//-------------------------------------------------------------------------------- +// DragDetector +//-------------------------------------------------------------------------------- + +int32_t DragDetector::FindIndex( const AInputEvent* event, int32_t id ) +{ + int32_t count = AMotionEvent_getPointerCount( event ); + for( uint32_t i = 0; i < count; ++i ) + { + if( id == AMotionEvent_getPointerId( event, i ) ) + return i; + } + return -1; +} + +GESTURE_STATE DragDetector::Detect( const AInputEvent* event ) +{ + GESTURE_STATE ret = GESTURE_STATE_NONE; + int32_t action = AMotionEvent_getAction( event ); + int32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) + >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; + uint32_t flags = action & AMOTION_EVENT_ACTION_MASK; + event_ = event; + + int32_t count = AMotionEvent_getPointerCount( event ); + switch( flags ) + { + case AMOTION_EVENT_ACTION_DOWN: + vec_pointers_.push_back( AMotionEvent_getPointerId( event, 0 ) ); + ret = GESTURE_STATE_START; + break; + case AMOTION_EVENT_ACTION_POINTER_DOWN: + vec_pointers_.push_back( AMotionEvent_getPointerId( event, index ) ); + break; + case AMOTION_EVENT_ACTION_UP: + vec_pointers_.pop_back(); + ret = GESTURE_STATE_END; + break; + case AMOTION_EVENT_ACTION_POINTER_UP: + { + int32_t released_pointer_id = AMotionEvent_getPointerId( event, index ); + + std::vector::iterator it = vec_pointers_.begin(); + std::vector::iterator it_end = vec_pointers_.end(); + int32_t i = 0; + for( ; it != it_end; ++it, ++i ) + { + if( *it == released_pointer_id ) + { + vec_pointers_.erase( it ); + break; + } + } + + if( i <= 1 ) + { + //Reset pinch or drag + if( count == 2 ) + { + ret = GESTURE_STATE_START; + } + } + break; + } + case AMOTION_EVENT_ACTION_MOVE: + switch( count ) + { + case 1: + //Drag + ret = GESTURE_STATE_MOVE; + break; + default: + break; + } + break; + case AMOTION_EVENT_ACTION_CANCEL: + break; + } + + return ret; +} + +bool DragDetector::GetPointer( Vec2& v ) +{ + if( vec_pointers_.size() < 1 ) + return false; + + int32_t iIndex = FindIndex( event_, vec_pointers_[0] ); + if( iIndex == -1 ) + return false; + + float x = AMotionEvent_getX( event_, iIndex ); + float y = AMotionEvent_getY( event_, iIndex ); + + v = Vec2( x, y ); + + return true; +} + +} //namespace ndkHelper + diff --git a/ndk/sources/android/helper/gestureDetector.h b/ndk/sources/android/ndk_helper/gestureDetector.h similarity index 61% rename from ndk/sources/android/helper/gestureDetector.h rename to ndk/sources/android/ndk_helper/gestureDetector.h index f3c8942ce..6ae5d80ef 100644 --- a/ndk/sources/android/helper/gestureDetector.h +++ b/ndk/sources/android/ndk_helper/gestureDetector.h @@ -17,7 +17,6 @@ //-------------------------------------------------------------------------------- // gestureDetector.h //-------------------------------------------------------------------------------- - #ifndef GESTUREDETECTOR_H_ #define GESTUREDETECTOR_H_ @@ -30,6 +29,8 @@ #include "JNIHelper.h" #include "vecmath.h" +namespace ndk_helper +{ //-------------------------------------------------------------------------------- // Constants //-------------------------------------------------------------------------------- @@ -38,11 +39,14 @@ const int32_t TAP_TIMEOUT = 180 * 1000000; const int32_t DOUBLE_TAP_SLOP = 100; const int32_t TOUCH_SLOP = 8; -#define GESTURE_STATE_NONE (0) -#define GESTURE_STATE_START (1) -#define GESTURE_STATE_MOVE (2) -#define GESTURE_STATE_END (4) -#define GESTURE_STATE_ACTION (GESTURE_STATE_START | GESTURE_STATE_END) +enum +{ + GESTURE_STATE_NONE = 0, + GESTURE_STATE_START = 1, + GESTURE_STATE_MOVE = 2, + GESTURE_STATE_END = 4, + GESTURE_STATE_ACTION = (GESTURE_STATE_START | GESTURE_STATE_END), +}; typedef int32_t GESTURE_STATE; /****************************************************************** @@ -55,13 +59,15 @@ typedef int32_t GESTURE_STATE; class GestureDetector { protected: - float _fDpFactor; + float dp_factor_; public: GestureDetector(); - virtual ~GestureDetector() {} - virtual void setConfiguration(AConfiguration* config); + virtual ~GestureDetector() + { + } + virtual void SetConfiguration( AConfiguration* config ); - virtual GESTURE_STATE detect(const AInputEvent* motion_event) = 0; + virtual GESTURE_STATE Detect( const AInputEvent* motion_event ) = 0; }; /****************************************************************** @@ -69,16 +75,20 @@ public: * Returns GESTURE_STATE_ACTION when a tap gesture is detected * */ -class TapDetector : public GestureDetector +class TapDetector: public GestureDetector { private: - int32_t _iDownPointerID; - float _fDownX; - float _fDownY; + int32_t down_pointer_id_; + float down_x_; + float down_y_; public: - TapDetector() {} - virtual ~TapDetector() {} - virtual GESTURE_STATE detect(const AInputEvent* motion_event); + TapDetector() + { + } + virtual ~TapDetector() + { + } + virtual GESTURE_STATE Detect( const AInputEvent* motion_event ); }; /****************************************************************** @@ -86,19 +96,23 @@ public: * Returns GESTURE_STATE_ACTION when a double-tap gesture is detected * */ -class DoubletapDetector : public GestureDetector +class DoubletapDetector: public GestureDetector { private: - TapDetector _tapDetector; - int64_t _lastTapTime; - float _fLastTapX; - float _fLastTapY; + TapDetector tap_detector_; + int64_t last_tap_time_; + float last_tap_x_; + float last_tap_y_; public: - DoubletapDetector() {} - virtual ~DoubletapDetector() {} - virtual GESTURE_STATE detect(const AInputEvent* motion_event); - virtual void setConfiguration(AConfiguration* config); + DoubletapDetector() + { + } + virtual ~DoubletapDetector() + { + } + virtual GESTURE_STATE Detect( const AInputEvent* motion_event ); + virtual void SetConfiguration( AConfiguration* config ); }; /****************************************************************** @@ -108,18 +122,22 @@ public: * When the finger 1,2,3 are tapped and then finger 1 is released, * the detector start new pinch gesture with finger 2 & 3. */ -class PinchDetector : public GestureDetector +class PinchDetector: public GestureDetector { private: - int32_t findIndex( const AInputEvent* event, int32_t iID ); - const AInputEvent* _event; - std::vector _vecPointers; + int32_t FindIndex( const AInputEvent* event, int32_t id ); + const AInputEvent* event_; + std::vector vec_pointers_; public: - PinchDetector() {} - virtual ~PinchDetector() {} - virtual GESTURE_STATE detect(const AInputEvent* event); - bool getPointers( vec2& v1, vec2& v2 ); + PinchDetector() + { + } + virtual ~PinchDetector() + { + } + virtual GESTURE_STATE Detect( const AInputEvent* event ); + bool GetPointers( Vec2& v1, Vec2& v2 ); }; /****************************************************************** @@ -127,17 +145,22 @@ public: * Returns drag gesture state when a drag-tap gesture is detected * */ -class DragDetector : public GestureDetector +class DragDetector: public GestureDetector { private: - int32_t findIndex( const AInputEvent* event, int32_t iID ); - const AInputEvent* _event; - std::vector _vecPointers; + int32_t FindIndex( const AInputEvent* event, int32_t id ); + const AInputEvent* event_; + std::vector vec_pointers_; public: - DragDetector() {} - virtual ~DragDetector() {} - virtual GESTURE_STATE detect(const AInputEvent* event); - bool getPointer( vec2& v ); + DragDetector() + { + } + virtual ~DragDetector() + { + } + virtual GESTURE_STATE Detect( const AInputEvent* event ); + bool GetPointer( Vec2& v ); }; +} //namespace ndkHelper #endif /* GESTUREDETECTOR_H_ */ diff --git a/ndk/sources/android/ndk_helper/gl3stub.c b/ndk/sources/android/ndk_helper/gl3stub.c new file mode 100644 index 000000000..c34d791e5 --- /dev/null +++ b/ndk/sources/android/ndk_helper/gl3stub.c @@ -0,0 +1,512 @@ +/* + * 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 +#include "gl3stub.h" + +GLboolean gl3stubInit() +{ +#define FIND_PROC(s) s = (void*)eglGetProcAddress(#s); + FIND_PROC( glReadBuffer ); + FIND_PROC( glDrawRangeElements ); + FIND_PROC( glTexImage3D ); + FIND_PROC( glTexSubImage3D ); + FIND_PROC( glCopyTexSubImage3D ); + FIND_PROC( glCompressedTexImage3D ); + FIND_PROC( glCompressedTexSubImage3D ); + FIND_PROC( glGenQueries ); + FIND_PROC( glDeleteQueries ); + FIND_PROC( glIsQuery ); + FIND_PROC( glBeginQuery ); + FIND_PROC( glEndQuery ); + FIND_PROC( glGetQueryiv ); + FIND_PROC( glGetQueryObjectuiv ); + FIND_PROC( glUnmapBuffer ); + FIND_PROC( glGetBufferPointerv ); + FIND_PROC( glDrawBuffers ); + FIND_PROC( glUniformMatrix2x3fv ); + FIND_PROC( glUniformMatrix3x2fv ); + FIND_PROC( glUniformMatrix2x4fv ); + FIND_PROC( glUniformMatrix4x2fv ); + FIND_PROC( glUniformMatrix3x4fv ); + FIND_PROC( glUniformMatrix4x3fv ); + FIND_PROC( glBlitFramebuffer ); + FIND_PROC( glRenderbufferStorageMultisample ); + FIND_PROC( glFramebufferTextureLayer ); + FIND_PROC( glMapBufferRange ); + FIND_PROC( glFlushMappedBufferRange ); + FIND_PROC( glBindVertexArray ); + FIND_PROC( glDeleteVertexArrays ); + FIND_PROC( glGenVertexArrays ); + FIND_PROC( glIsVertexArray ); + FIND_PROC( glGetIntegeri_v ); + FIND_PROC( glBeginTransformFeedback ); + FIND_PROC( glEndTransformFeedback ); + FIND_PROC( glBindBufferRange ); + FIND_PROC( glBindBufferBase ); + FIND_PROC( glTransformFeedbackVaryings ); + FIND_PROC( glGetTransformFeedbackVarying ); + FIND_PROC( glVertexAttribIPointer ); + FIND_PROC( glGetVertexAttribIiv ); + FIND_PROC( glGetVertexAttribIuiv ); + FIND_PROC( glVertexAttribI4i ); + FIND_PROC( glVertexAttribI4ui ); + FIND_PROC( glVertexAttribI4iv ); + FIND_PROC( glVertexAttribI4uiv ); + FIND_PROC( glGetUniformuiv ); + FIND_PROC( glGetFragDataLocation ); + FIND_PROC( glUniform1ui ); + FIND_PROC( glUniform2ui ); + FIND_PROC( glUniform3ui ); + FIND_PROC( glUniform4ui ); + FIND_PROC( glUniform1uiv ); + FIND_PROC( glUniform2uiv ); + FIND_PROC( glUniform3uiv ); + FIND_PROC( glUniform4uiv ); + FIND_PROC( glClearBufferiv ); + FIND_PROC( glClearBufferuiv ); + FIND_PROC( glClearBufferfv ); + FIND_PROC( glClearBufferfi ); + FIND_PROC( glGetStringi ); + FIND_PROC( glCopyBufferSubData ); + FIND_PROC( glGetUniformIndices ); + FIND_PROC( glGetActiveUniformsiv ); + FIND_PROC( glGetUniformBlockIndex ); + FIND_PROC( glGetActiveUniformBlockiv ); + FIND_PROC( glGetActiveUniformBlockName ); + FIND_PROC( glUniformBlockBinding ); + FIND_PROC( glDrawArraysInstanced ); + FIND_PROC( glDrawElementsInstanced ); + FIND_PROC( glFenceSync ); + FIND_PROC( glIsSync ); + FIND_PROC( glDeleteSync ); + FIND_PROC( glClientWaitSync ); + FIND_PROC( glWaitSync ); + FIND_PROC( glGetInteger64v ); + FIND_PROC( glGetSynciv ); + FIND_PROC( glGetInteger64i_v ); + FIND_PROC( glGetBufferParameteri64v ); + FIND_PROC( glGenSamplers ); + FIND_PROC( glDeleteSamplers ); + FIND_PROC( glIsSampler ); + FIND_PROC( glBindSampler ); + FIND_PROC( glSamplerParameteri ); + FIND_PROC( glSamplerParameteriv ); + FIND_PROC( glSamplerParameterf ); + FIND_PROC( glSamplerParameterfv ); + FIND_PROC( glGetSamplerParameteriv ); + FIND_PROC( glGetSamplerParameterfv ); + FIND_PROC( glVertexAttribDivisor ); + FIND_PROC( glBindTransformFeedback ); + FIND_PROC( glDeleteTransformFeedbacks ); + FIND_PROC( glGenTransformFeedbacks ); + FIND_PROC( glIsTransformFeedback ); + FIND_PROC( glPauseTransformFeedback ); + FIND_PROC( glResumeTransformFeedback ); + FIND_PROC( glGetProgramBinary ); + FIND_PROC( glProgramBinary ); + FIND_PROC( glProgramParameteri ); + FIND_PROC( glInvalidateFramebuffer ); + FIND_PROC( glInvalidateSubFramebuffer ); + FIND_PROC( glTexStorage2D ); + FIND_PROC( glTexStorage3D ); + FIND_PROC( glGetInternalformativ ); +#undef FIND_PROC + + if( !glReadBuffer || !glDrawRangeElements || !glTexImage3D || !glTexSubImage3D + || !glCopyTexSubImage3D || !glCompressedTexImage3D + || !glCompressedTexSubImage3D || !glGenQueries || !glDeleteQueries + || !glIsQuery || !glBeginQuery || !glEndQuery || !glGetQueryiv + || !glGetQueryObjectuiv || !glUnmapBuffer || !glGetBufferPointerv + || !glDrawBuffers || !glUniformMatrix2x3fv || !glUniformMatrix3x2fv + || !glUniformMatrix2x4fv || !glUniformMatrix4x2fv || !glUniformMatrix3x4fv + || !glUniformMatrix4x3fv || !glBlitFramebuffer + || !glRenderbufferStorageMultisample || !glFramebufferTextureLayer + || !glMapBufferRange || !glFlushMappedBufferRange || !glBindVertexArray + || !glDeleteVertexArrays || !glGenVertexArrays || !glIsVertexArray + || !glGetIntegeri_v || !glBeginTransformFeedback || !glEndTransformFeedback + || !glBindBufferRange || !glBindBufferBase || !glTransformFeedbackVaryings + || !glGetTransformFeedbackVarying || !glVertexAttribIPointer + || !glGetVertexAttribIiv || !glGetVertexAttribIuiv || !glVertexAttribI4i + || !glVertexAttribI4ui || !glVertexAttribI4iv || !glVertexAttribI4uiv + || !glGetUniformuiv || !glGetFragDataLocation || !glUniform1ui + || !glUniform2ui || !glUniform3ui || !glUniform4ui || !glUniform1uiv + || !glUniform2uiv || !glUniform3uiv || !glUniform4uiv || !glClearBufferiv + || !glClearBufferuiv || !glClearBufferfv || !glClearBufferfi || !glGetStringi + || !glCopyBufferSubData || !glGetUniformIndices || !glGetActiveUniformsiv + || !glGetUniformBlockIndex || !glGetActiveUniformBlockiv + || !glGetActiveUniformBlockName || !glUniformBlockBinding + || !glDrawArraysInstanced || !glDrawElementsInstanced || !glFenceSync + || !glIsSync || !glDeleteSync || !glClientWaitSync || !glWaitSync + || !glGetInteger64v || !glGetSynciv || !glGetInteger64i_v + || !glGetBufferParameteri64v || !glGenSamplers || !glDeleteSamplers + || !glIsSampler || !glBindSampler || !glSamplerParameteri + || !glSamplerParameteriv || !glSamplerParameterf || !glSamplerParameterfv + || !glGetSamplerParameteriv || !glGetSamplerParameterfv + || !glVertexAttribDivisor || !glBindTransformFeedback + || !glDeleteTransformFeedbacks || !glGenTransformFeedbacks + || !glIsTransformFeedback || !glPauseTransformFeedback + || !glResumeTransformFeedback || !glGetProgramBinary || !glProgramBinary + || !glProgramParameteri || !glInvalidateFramebuffer + || !glInvalidateSubFramebuffer || !glTexStorage2D || !glTexStorage3D + || !glGetInternalformativ ) + { + return GL_FALSE; + } + + return GL_TRUE; +} + +/* Function pointer definitions */GL_APICALL void (* GL_APIENTRY glReadBuffer)( GLenum mode ); +GL_APICALL void (* GL_APIENTRY glDrawRangeElements)( GLenum mode, + GLuint start, + GLuint end, + GLsizei count, + GLenum type, + const GLvoid* indices ); +GL_APICALL void (* GL_APIENTRY glTexImage3D)( GLenum target, + GLint level, + GLint internalformat, + GLsizei width, + GLsizei height, + GLsizei depth, + GLint border, + GLenum format, + GLenum type, + const GLvoid* pixels ); +GL_APICALL void (* GL_APIENTRY glTexSubImage3D)( GLenum target, + GLint level, + GLint xoffset, + GLint yoffset, + GLint zoffset, + GLsizei width, + GLsizei height, + GLsizei depth, + GLenum format, + GLenum type, + const GLvoid* pixels ); +GL_APICALL void (* GL_APIENTRY glCopyTexSubImage3D)( GLenum target, + GLint level, + GLint xoffset, + GLint yoffset, + GLint zoffset, + GLint x, + GLint y, + GLsizei width, + GLsizei height ); +GL_APICALL void (* GL_APIENTRY glCompressedTexImage3D)( GLenum target, + GLint level, + GLenum internalformat, + GLsizei width, + GLsizei height, + GLsizei depth, + GLint border, + GLsizei imageSize, + const GLvoid* data ); +GL_APICALL void (* GL_APIENTRY glCompressedTexSubImage3D)( GLenum target, + GLint level, + GLint xoffset, + GLint yoffset, + GLint zoffset, + GLsizei width, + GLsizei height, + GLsizei depth, + GLenum format, + GLsizei imageSize, + const GLvoid* data ); +GL_APICALL void (* GL_APIENTRY glGenQueries)( GLsizei n, GLuint* ids ); +GL_APICALL void (* GL_APIENTRY glDeleteQueries)( GLsizei n, const GLuint* ids ); +GL_APICALL GLboolean (* GL_APIENTRY glIsQuery)( GLuint id ); +GL_APICALL void (* GL_APIENTRY glBeginQuery)( GLenum target, GLuint id ); +GL_APICALL void (* GL_APIENTRY glEndQuery)( GLenum target ); +GL_APICALL void (* GL_APIENTRY glGetQueryiv)( GLenum target, GLenum pname, GLint* params ); +GL_APICALL void (* GL_APIENTRY glGetQueryObjectuiv)( GLuint id, + GLenum pname, + GLuint* params ); +GL_APICALL GLboolean (* GL_APIENTRY glUnmapBuffer)( GLenum target ); +GL_APICALL void (* GL_APIENTRY glGetBufferPointerv)( GLenum target, + GLenum pname, + GLvoid** params ); +GL_APICALL void (* GL_APIENTRY glDrawBuffers)( GLsizei n, const GLenum* bufs ); +GL_APICALL void (* GL_APIENTRY glUniformMatrix2x3fv)( GLint location, + GLsizei count, + GLboolean transpose, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glUniformMatrix3x2fv)( GLint location, + GLsizei count, + GLboolean transpose, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glUniformMatrix2x4fv)( GLint location, + GLsizei count, + GLboolean transpose, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glUniformMatrix4x2fv)( GLint location, + GLsizei count, + GLboolean transpose, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glUniformMatrix3x4fv)( GLint location, + GLsizei count, + GLboolean transpose, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glUniformMatrix4x3fv)( GLint location, + GLsizei count, + GLboolean transpose, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glBlitFramebuffer)( GLint srcX0, + GLint srcY0, + GLint srcX1, + GLint srcY1, + GLint dstX0, + GLint dstY0, + GLint dstX1, + GLint dstY1, + GLbitfield mask, + GLenum filter ); +GL_APICALL void (* GL_APIENTRY glRenderbufferStorageMultisample)( GLenum target, + GLsizei samples, + GLenum internalformat, + GLsizei width, + GLsizei height ); +GL_APICALL void (* GL_APIENTRY glFramebufferTextureLayer)( GLenum target, + GLenum attachment, + GLuint texture, + GLint level, + GLint layer ); +GL_APICALL GLvoid* (* GL_APIENTRY glMapBufferRange)( GLenum target, + GLintptr offset, + GLsizeiptr length, + GLbitfield access ); +GL_APICALL void (* GL_APIENTRY glFlushMappedBufferRange)( GLenum target, + GLintptr offset, + GLsizeiptr length ); +GL_APICALL void (* GL_APIENTRY glBindVertexArray)( GLuint array ); +GL_APICALL void (* GL_APIENTRY glDeleteVertexArrays)( GLsizei n, const GLuint* arrays ); +GL_APICALL void (* GL_APIENTRY glGenVertexArrays)( GLsizei n, GLuint* arrays ); +GL_APICALL GLboolean (* GL_APIENTRY glIsVertexArray)( GLuint array ); +GL_APICALL void (* GL_APIENTRY glGetIntegeri_v)( GLenum target, + GLuint index, + GLint* data ); +GL_APICALL void (* GL_APIENTRY glBeginTransformFeedback)( GLenum primitiveMode ); +GL_APICALL void (* GL_APIENTRY glEndTransformFeedback)( void ); +GL_APICALL void (* GL_APIENTRY glBindBufferRange)( GLenum target, + GLuint index, + GLuint buffer, + GLintptr offset, + GLsizeiptr size ); +GL_APICALL void (* GL_APIENTRY glBindBufferBase)( GLenum target, + GLuint index, + GLuint buffer ); +GL_APICALL void (* GL_APIENTRY glTransformFeedbackVaryings)( GLuint program, + GLsizei count, + const GLchar* const * varyings, + GLenum bufferMode ); +GL_APICALL void (* GL_APIENTRY glGetTransformFeedbackVarying)( GLuint program, + GLuint index, + GLsizei bufSize, + GLsizei* length, + GLsizei* size, + GLenum* type, + GLchar* name ); +GL_APICALL void (* GL_APIENTRY glVertexAttribIPointer)( GLuint index, + GLint size, + GLenum type, + GLsizei stride, + const GLvoid* pointer ); +GL_APICALL void (* GL_APIENTRY glGetVertexAttribIiv)( GLuint index, + GLenum pname, + GLint* params ); +GL_APICALL void (* GL_APIENTRY glGetVertexAttribIuiv)( GLuint index, + GLenum pname, + GLuint* params ); +GL_APICALL void (* GL_APIENTRY glVertexAttribI4i)( GLuint index, + GLint x, + GLint y, + GLint z, + GLint w ); +GL_APICALL void (* GL_APIENTRY glVertexAttribI4ui)( GLuint index, + GLuint x, + GLuint y, + GLuint z, + GLuint w ); +GL_APICALL void (* GL_APIENTRY glVertexAttribI4iv)( GLuint index, const GLint* v ); +GL_APICALL void (* GL_APIENTRY glVertexAttribI4uiv)( GLuint index, const GLuint* v ); +GL_APICALL void (* GL_APIENTRY glGetUniformuiv)( GLuint program, + GLint location, + GLuint* params ); +GL_APICALL GLint (* GL_APIENTRY glGetFragDataLocation)( GLuint program, + const GLchar *name ); +GL_APICALL void (* GL_APIENTRY glUniform1ui)( GLint location, GLuint v0 ); +GL_APICALL void (* GL_APIENTRY glUniform2ui)( GLint location, GLuint v0, GLuint v1 ); +GL_APICALL void (* GL_APIENTRY glUniform3ui)( GLint location, + GLuint v0, + GLuint v1, + GLuint v2 ); +GL_APICALL void (* GL_APIENTRY glUniform4ui)( GLint location, + GLuint v0, + GLuint v1, + GLuint v2, + GLuint v3 ); +GL_APICALL void (* GL_APIENTRY glUniform1uiv)( GLint location, + GLsizei count, + const GLuint* value ); +GL_APICALL void (* GL_APIENTRY glUniform2uiv)( GLint location, + GLsizei count, + const GLuint* value ); +GL_APICALL void (* GL_APIENTRY glUniform3uiv)( GLint location, + GLsizei count, + const GLuint* value ); +GL_APICALL void (* GL_APIENTRY glUniform4uiv)( GLint location, + GLsizei count, + const GLuint* value ); +GL_APICALL void (* GL_APIENTRY glClearBufferiv)( GLenum buffer, + GLint drawbuffer, + const GLint* value ); +GL_APICALL void (* GL_APIENTRY glClearBufferuiv)( GLenum buffer, + GLint drawbuffer, + const GLuint* value ); +GL_APICALL void (* GL_APIENTRY glClearBufferfv)( GLenum buffer, + GLint drawbuffer, + const GLfloat* value ); +GL_APICALL void (* GL_APIENTRY glClearBufferfi)( GLenum buffer, + GLint drawbuffer, + GLfloat depth, + GLint stencil ); +GL_APICALL const GLubyte* (* GL_APIENTRY glGetStringi)( GLenum name, GLuint index ); +GL_APICALL void (* GL_APIENTRY glCopyBufferSubData)( GLenum readTarget, + GLenum writeTarget, + GLintptr readOffset, + GLintptr writeOffset, + GLsizeiptr size ); +GL_APICALL void (* GL_APIENTRY glGetUniformIndices)( GLuint program, + GLsizei uniformCount, + const GLchar* const * uniformNames, + GLuint* uniformIndices ); +GL_APICALL void (* GL_APIENTRY glGetActiveUniformsiv)( GLuint program, + GLsizei uniformCount, + const GLuint* uniformIndices, + GLenum pname, + GLint* params ); +GL_APICALL GLuint (* GL_APIENTRY glGetUniformBlockIndex)( GLuint program, + const GLchar* uniformBlockName ); +GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockiv)( GLuint program, + GLuint uniformBlockIndex, + GLenum pname, + GLint* params ); +GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockName)( GLuint program, + GLuint uniformBlockIndex, + GLsizei bufSize, + GLsizei* length, + GLchar* uniformBlockName ); +GL_APICALL void (* GL_APIENTRY glUniformBlockBinding)( GLuint program, + GLuint uniformBlockIndex, + GLuint uniformBlockBinding ); +GL_APICALL void (* GL_APIENTRY glDrawArraysInstanced)( GLenum mode, + GLint first, + GLsizei count, + GLsizei instanceCount ); +GL_APICALL void (* GL_APIENTRY glDrawElementsInstanced)( GLenum mode, + GLsizei count, + GLenum type, + const GLvoid* indices, + GLsizei instanceCount ); +GL_APICALL GLsync (* GL_APIENTRY glFenceSync)( GLenum condition, GLbitfield flags ); +GL_APICALL GLboolean (* GL_APIENTRY glIsSync)( GLsync sync ); +GL_APICALL void (* GL_APIENTRY glDeleteSync)( GLsync sync ); +GL_APICALL GLenum (* GL_APIENTRY glClientWaitSync)( GLsync sync, + GLbitfield flags, + GLuint64 timeout ); +GL_APICALL void (* GL_APIENTRY glWaitSync)( GLsync sync, + GLbitfield flags, + GLuint64 timeout ); +GL_APICALL void (* GL_APIENTRY glGetInteger64v)( GLenum pname, GLint64* params ); +GL_APICALL void (* GL_APIENTRY glGetSynciv)( GLsync sync, + GLenum pname, + GLsizei bufSize, + GLsizei* length, + GLint* values ); +GL_APICALL void (* GL_APIENTRY glGetInteger64i_v)( GLenum target, + GLuint index, + GLint64* data ); +GL_APICALL void (* GL_APIENTRY glGetBufferParameteri64v)( GLenum target, + GLenum pname, + GLint64* params ); +GL_APICALL void (* GL_APIENTRY glGenSamplers)( GLsizei count, GLuint* samplers ); +GL_APICALL void (* GL_APIENTRY glDeleteSamplers)( GLsizei count, const GLuint* samplers ); +GL_APICALL GLboolean (* GL_APIENTRY glIsSampler)( GLuint sampler ); +GL_APICALL void (* GL_APIENTRY glBindSampler)( GLuint unit, GLuint sampler ); +GL_APICALL void (* GL_APIENTRY glSamplerParameteri)( GLuint sampler, + GLenum pname, + GLint param ); +GL_APICALL void (* GL_APIENTRY glSamplerParameteriv)( GLuint sampler, + GLenum pname, + const GLint* param ); +GL_APICALL void (* GL_APIENTRY glSamplerParameterf)( GLuint sampler, + GLenum pname, + GLfloat param ); +GL_APICALL void (* GL_APIENTRY glSamplerParameterfv)( GLuint sampler, + GLenum pname, + const GLfloat* param ); +GL_APICALL void (* GL_APIENTRY glGetSamplerParameteriv)( GLuint sampler, + GLenum pname, + GLint* params ); +GL_APICALL void (* GL_APIENTRY glGetSamplerParameterfv)( GLuint sampler, + GLenum pname, + GLfloat* params ); +GL_APICALL void (* GL_APIENTRY glVertexAttribDivisor)( GLuint index, GLuint divisor ); +GL_APICALL void (* GL_APIENTRY glBindTransformFeedback)( GLenum target, GLuint id ); +GL_APICALL void (* GL_APIENTRY glDeleteTransformFeedbacks)( GLsizei n, const GLuint* ids ); +GL_APICALL void (* GL_APIENTRY glGenTransformFeedbacks)( GLsizei n, GLuint* ids ); +GL_APICALL GLboolean (* GL_APIENTRY glIsTransformFeedback)( GLuint id ); +GL_APICALL void (* GL_APIENTRY glPauseTransformFeedback)( void ); +GL_APICALL void (* GL_APIENTRY glResumeTransformFeedback)( void ); +GL_APICALL void (* GL_APIENTRY glGetProgramBinary)( GLuint program, + GLsizei bufSize, + GLsizei* length, + GLenum* binaryFormat, + GLvoid* binary ); +GL_APICALL void (* GL_APIENTRY glProgramBinary)( GLuint program, + GLenum binaryFormat, + const GLvoid* binary, + GLsizei length ); +GL_APICALL void (* GL_APIENTRY glProgramParameteri)( GLuint program, + GLenum pname, + GLint value ); +GL_APICALL void (* GL_APIENTRY glInvalidateFramebuffer)( GLenum target, + GLsizei numAttachments, + const GLenum* attachments ); +GL_APICALL void (* GL_APIENTRY glInvalidateSubFramebuffer)( GLenum target, + GLsizei numAttachments, + const GLenum* attachments, + GLint x, + GLint y, + GLsizei width, + GLsizei height ); +GL_APICALL void (* GL_APIENTRY glTexStorage2D)( GLenum target, + GLsizei levels, + GLenum internalformat, + GLsizei width, + GLsizei height ); +GL_APICALL void (* GL_APIENTRY glTexStorage3D)( GLenum target, + GLsizei levels, + GLenum internalformat, + GLsizei width, + GLsizei height, + GLsizei depth ); +GL_APICALL void (* GL_APIENTRY glGetInternalformativ)( GLenum target, + GLenum internalformat, + GLenum pname, + GLsizei bufSize, + GLint* params ); diff --git a/ndk/sources/android/helper/gl3stub.h b/ndk/sources/android/ndk_helper/gl3stub.h similarity index 60% rename from ndk/sources/android/helper/gl3stub.h rename to ndk/sources/android/ndk_helper/gl3stub.h index a6ca63612..537dd28b5 100644 --- a/ndk/sources/android/helper/gl3stub.h +++ b/ndk/sources/android/ndk_helper/gl3stub.h @@ -16,31 +16,32 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* -** Copyright (c) 2007-2013 The Khronos Group Inc. -** -** Permission is hereby granted, free of charge, to any person obtaining a -** copy of this software and/or associated documentation files (the -** "Materials"), to deal in the Materials without restriction, including -** without limitation the rights to use, copy, modify, merge, publish, -** distribute, sublicense, and/or sell copies of the Materials, and to -** permit persons to whom the Materials are furnished to do so, subject to -** the following conditions: -** -** The above copyright notice and this permission notice shall be included -** in all copies or substantial portions of the Materials. -** -** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. -*/ + ** Copyright (c) 2007-2013 The Khronos Group Inc. + ** + ** Permission is hereby granted, free of charge, to any person obtaining a + ** copy of this software and/or associated documentation files (the + ** "Materials"), to deal in the Materials without restriction, including + ** without limitation the rights to use, copy, modify, merge, publish, + ** distribute, sublicense, and/or sell copies of the Materials, and to + ** permit persons to whom the Materials are furnished to do so, subject to + ** the following conditions: + ** + ** The above copyright notice and this permission notice shall be included + ** in all copies or substantial portions of the Materials. + ** + ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. + */ /* * This files is for apps that want to use ES3 if present, @@ -62,8 +63,8 @@ GLboolean gl3stubInit(); /* OpenGL ES 3.0 */ -typedef unsigned short GLhalf; -typedef khronos_int64_t GLint64; +typedef unsigned short GLhalf; +typedef khronos_int64_t GLint64; typedef khronos_uint64_t GLuint64; typedef struct __GLsync *GLsync; @@ -388,110 +389,110 @@ typedef struct __GLsync *GLsync; /* OpenGL ES 3.0 */ -extern GL_APICALL void (* GL_APIENTRY glReadBuffer) (GLenum mode); -extern GL_APICALL void (* GL_APIENTRY glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid* indices); -extern GL_APICALL void (* GL_APIENTRY glTexImage3D) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid* pixels); -extern GL_APICALL void (* GL_APIENTRY glTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid* pixels); -extern GL_APICALL void (* GL_APIENTRY glCopyTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height); -extern GL_APICALL void (* GL_APIENTRY glCompressedTexImage3D) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid* data); -extern GL_APICALL void (* GL_APIENTRY glCompressedTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid* data); -extern GL_APICALL void (* GL_APIENTRY glGenQueries) (GLsizei n, GLuint* ids); -extern GL_APICALL void (* GL_APIENTRY glDeleteQueries) (GLsizei n, const GLuint* ids); -extern GL_APICALL GLboolean (* GL_APIENTRY glIsQuery) (GLuint id); -extern GL_APICALL void (* GL_APIENTRY glBeginQuery) (GLenum target, GLuint id); -extern GL_APICALL void (* GL_APIENTRY glEndQuery) (GLenum target); -extern GL_APICALL void (* GL_APIENTRY glGetQueryiv) (GLenum target, GLenum pname, GLint* params); -extern GL_APICALL void (* GL_APIENTRY glGetQueryObjectuiv) (GLuint id, GLenum pname, GLuint* params); -extern GL_APICALL GLboolean (* GL_APIENTRY glUnmapBuffer) (GLenum target); -extern GL_APICALL void (* GL_APIENTRY glGetBufferPointerv) (GLenum target, GLenum pname, GLvoid** params); -extern GL_APICALL void (* GL_APIENTRY glDrawBuffers) (GLsizei n, const GLenum* bufs); -extern GL_APICALL void (* GL_APIENTRY glUniformMatrix2x3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glUniformMatrix3x2fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glUniformMatrix2x4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glUniformMatrix4x2fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glUniformMatrix3x4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glUniformMatrix4x3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glBlitFramebuffer) (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); -extern GL_APICALL void (* GL_APIENTRY glRenderbufferStorageMultisample) (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height); -extern GL_APICALL void (* GL_APIENTRY glFramebufferTextureLayer) (GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer); -extern GL_APICALL GLvoid* (* GL_APIENTRY glMapBufferRange) (GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access); -extern GL_APICALL void (* GL_APIENTRY glFlushMappedBufferRange) (GLenum target, GLintptr offset, GLsizeiptr length); -extern GL_APICALL void (* GL_APIENTRY glBindVertexArray) (GLuint array); -extern GL_APICALL void (* GL_APIENTRY glDeleteVertexArrays) (GLsizei n, const GLuint* arrays); -extern GL_APICALL void (* GL_APIENTRY glGenVertexArrays) (GLsizei n, GLuint* arrays); -extern GL_APICALL GLboolean (* GL_APIENTRY glIsVertexArray) (GLuint array); -extern GL_APICALL void (* GL_APIENTRY glGetIntegeri_v) (GLenum target, GLuint index, GLint* data); -extern GL_APICALL void (* GL_APIENTRY glBeginTransformFeedback) (GLenum primitiveMode); -extern GL_APICALL void (* GL_APIENTRY glEndTransformFeedback) (void); -extern GL_APICALL void (* GL_APIENTRY glBindBufferRange) (GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size); -extern GL_APICALL void (* GL_APIENTRY glBindBufferBase) (GLenum target, GLuint index, GLuint buffer); -extern GL_APICALL void (* GL_APIENTRY glTransformFeedbackVaryings) (GLuint program, GLsizei count, const GLchar* const* varyings, GLenum bufferMode); -extern GL_APICALL void (* GL_APIENTRY glGetTransformFeedbackVarying) (GLuint program, GLuint index, GLsizei bufSize, GLsizei* length, GLsizei* size, GLenum* type, GLchar* name); -extern GL_APICALL void (* GL_APIENTRY glVertexAttribIPointer) (GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid* pointer); -extern GL_APICALL void (* GL_APIENTRY glGetVertexAttribIiv) (GLuint index, GLenum pname, GLint* params); -extern GL_APICALL void (* GL_APIENTRY glGetVertexAttribIuiv) (GLuint index, GLenum pname, GLuint* params); -extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4i) (GLuint index, GLint x, GLint y, GLint z, GLint w); -extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4ui) (GLuint index, GLuint x, GLuint y, GLuint z, GLuint w); -extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4iv) (GLuint index, const GLint* v); -extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4uiv) (GLuint index, const GLuint* v); -extern GL_APICALL void (* GL_APIENTRY glGetUniformuiv) (GLuint program, GLint location, GLuint* params); -extern GL_APICALL GLint (* GL_APIENTRY glGetFragDataLocation) (GLuint program, const GLchar *name); -extern GL_APICALL void (* GL_APIENTRY glUniform1ui) (GLint location, GLuint v0); -extern GL_APICALL void (* GL_APIENTRY glUniform2ui) (GLint location, GLuint v0, GLuint v1); -extern GL_APICALL void (* GL_APIENTRY glUniform3ui) (GLint location, GLuint v0, GLuint v1, GLuint v2); -extern GL_APICALL void (* GL_APIENTRY glUniform4ui) (GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3); -extern GL_APICALL void (* GL_APIENTRY glUniform1uiv) (GLint location, GLsizei count, const GLuint* value); -extern GL_APICALL void (* GL_APIENTRY glUniform2uiv) (GLint location, GLsizei count, const GLuint* value); -extern GL_APICALL void (* GL_APIENTRY glUniform3uiv) (GLint location, GLsizei count, const GLuint* value); -extern GL_APICALL void (* GL_APIENTRY glUniform4uiv) (GLint location, GLsizei count, const GLuint* value); -extern GL_APICALL void (* GL_APIENTRY glClearBufferiv) (GLenum buffer, GLint drawbuffer, const GLint* value); -extern GL_APICALL void (* GL_APIENTRY glClearBufferuiv) (GLenum buffer, GLint drawbuffer, const GLuint* value); -extern GL_APICALL void (* GL_APIENTRY glClearBufferfv) (GLenum buffer, GLint drawbuffer, const GLfloat* value); -extern GL_APICALL void (* GL_APIENTRY glClearBufferfi) (GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil); +extern GL_APICALL void (* GL_APIENTRY glReadBuffer) (GLenum mode); +extern GL_APICALL void (* GL_APIENTRY glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid* indices); +extern GL_APICALL void (* GL_APIENTRY glTexImage3D) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid* pixels); +extern GL_APICALL void (* GL_APIENTRY glTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid* pixels); +extern GL_APICALL void (* GL_APIENTRY glCopyTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height); +extern GL_APICALL void (* GL_APIENTRY glCompressedTexImage3D) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid* data); +extern GL_APICALL void (* GL_APIENTRY glCompressedTexSubImage3D) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid* data); +extern GL_APICALL void (* GL_APIENTRY glGenQueries) (GLsizei n, GLuint* ids); +extern GL_APICALL void (* GL_APIENTRY glDeleteQueries) (GLsizei n, const GLuint* ids); +extern GL_APICALL GLboolean (* GL_APIENTRY glIsQuery) (GLuint id); +extern GL_APICALL void (* GL_APIENTRY glBeginQuery) (GLenum target, GLuint id); +extern GL_APICALL void (* GL_APIENTRY glEndQuery) (GLenum target); +extern GL_APICALL void (* GL_APIENTRY glGetQueryiv) (GLenum target, GLenum pname, GLint* params); +extern GL_APICALL void (* GL_APIENTRY glGetQueryObjectuiv) (GLuint id, GLenum pname, GLuint* params); +extern GL_APICALL GLboolean (* GL_APIENTRY glUnmapBuffer) (GLenum target); +extern GL_APICALL void (* GL_APIENTRY glGetBufferPointerv) (GLenum target, GLenum pname, GLvoid** params); +extern GL_APICALL void (* GL_APIENTRY glDrawBuffers) (GLsizei n, const GLenum* bufs); +extern GL_APICALL void (* GL_APIENTRY glUniformMatrix2x3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glUniformMatrix3x2fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glUniformMatrix2x4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glUniformMatrix4x2fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glUniformMatrix3x4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glUniformMatrix4x3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glBlitFramebuffer) (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); +extern GL_APICALL void (* GL_APIENTRY glRenderbufferStorageMultisample) (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height); +extern GL_APICALL void (* GL_APIENTRY glFramebufferTextureLayer) (GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer); +extern GL_APICALL GLvoid* (* GL_APIENTRY glMapBufferRange) (GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access); +extern GL_APICALL void (* GL_APIENTRY glFlushMappedBufferRange) (GLenum target, GLintptr offset, GLsizeiptr length); +extern GL_APICALL void (* GL_APIENTRY glBindVertexArray) (GLuint array); +extern GL_APICALL void (* GL_APIENTRY glDeleteVertexArrays) (GLsizei n, const GLuint* arrays); +extern GL_APICALL void (* GL_APIENTRY glGenVertexArrays) (GLsizei n, GLuint* arrays); +extern GL_APICALL GLboolean (* GL_APIENTRY glIsVertexArray) (GLuint array); +extern GL_APICALL void (* GL_APIENTRY glGetIntegeri_v) (GLenum target, GLuint index, GLint* data); +extern GL_APICALL void (* GL_APIENTRY glBeginTransformFeedback) (GLenum primitiveMode); +extern GL_APICALL void (* GL_APIENTRY glEndTransformFeedback) (void); +extern GL_APICALL void (* GL_APIENTRY glBindBufferRange) (GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size); +extern GL_APICALL void (* GL_APIENTRY glBindBufferBase) (GLenum target, GLuint index, GLuint buffer); +extern GL_APICALL void (* GL_APIENTRY glTransformFeedbackVaryings) (GLuint program, GLsizei count, const GLchar* const* varyings, GLenum bufferMode); +extern GL_APICALL void (* GL_APIENTRY glGetTransformFeedbackVarying) (GLuint program, GLuint index, GLsizei bufSize, GLsizei* length, GLsizei* size, GLenum* type, GLchar* name); +extern GL_APICALL void (* GL_APIENTRY glVertexAttribIPointer) (GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid* pointer); +extern GL_APICALL void (* GL_APIENTRY glGetVertexAttribIiv) (GLuint index, GLenum pname, GLint* params); +extern GL_APICALL void (* GL_APIENTRY glGetVertexAttribIuiv) (GLuint index, GLenum pname, GLuint* params); +extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4i) (GLuint index, GLint x, GLint y, GLint z, GLint w); +extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4ui) (GLuint index, GLuint x, GLuint y, GLuint z, GLuint w); +extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4iv) (GLuint index, const GLint* v); +extern GL_APICALL void (* GL_APIENTRY glVertexAttribI4uiv) (GLuint index, const GLuint* v); +extern GL_APICALL void (* GL_APIENTRY glGetUniformuiv) (GLuint program, GLint location, GLuint* params); +extern GL_APICALL GLint (* GL_APIENTRY glGetFragDataLocation) (GLuint program, const GLchar *name); +extern GL_APICALL void (* GL_APIENTRY glUniform1ui) (GLint location, GLuint v0); +extern GL_APICALL void (* GL_APIENTRY glUniform2ui) (GLint location, GLuint v0, GLuint v1); +extern GL_APICALL void (* GL_APIENTRY glUniform3ui) (GLint location, GLuint v0, GLuint v1, GLuint v2); +extern GL_APICALL void (* GL_APIENTRY glUniform4ui) (GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3); +extern GL_APICALL void (* GL_APIENTRY glUniform1uiv) (GLint location, GLsizei count, const GLuint* value); +extern GL_APICALL void (* GL_APIENTRY glUniform2uiv) (GLint location, GLsizei count, const GLuint* value); +extern GL_APICALL void (* GL_APIENTRY glUniform3uiv) (GLint location, GLsizei count, const GLuint* value); +extern GL_APICALL void (* GL_APIENTRY glUniform4uiv) (GLint location, GLsizei count, const GLuint* value); +extern GL_APICALL void (* GL_APIENTRY glClearBufferiv) (GLenum buffer, GLint drawbuffer, const GLint* value); +extern GL_APICALL void (* GL_APIENTRY glClearBufferuiv) (GLenum buffer, GLint drawbuffer, const GLuint* value); +extern GL_APICALL void (* GL_APIENTRY glClearBufferfv) (GLenum buffer, GLint drawbuffer, const GLfloat* value); +extern GL_APICALL void (* GL_APIENTRY glClearBufferfi) (GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil); extern GL_APICALL const GLubyte* (* GL_APIENTRY glGetStringi) (GLenum name, GLuint index); -extern GL_APICALL void (* GL_APIENTRY glCopyBufferSubData) (GLenum readTarget, GLenum writeTarget, GLintptr readOffset, GLintptr writeOffset, GLsizeiptr size); -extern GL_APICALL void (* GL_APIENTRY glGetUniformIndices) (GLuint program, GLsizei uniformCount, const GLchar* const* uniformNames, GLuint* uniformIndices); -extern GL_APICALL void (* GL_APIENTRY glGetActiveUniformsiv) (GLuint program, GLsizei uniformCount, const GLuint* uniformIndices, GLenum pname, GLint* params); -extern GL_APICALL GLuint (* GL_APIENTRY glGetUniformBlockIndex) (GLuint program, const GLchar* uniformBlockName); -extern GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockiv) (GLuint program, GLuint uniformBlockIndex, GLenum pname, GLint* params); -extern GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockName) (GLuint program, GLuint uniformBlockIndex, GLsizei bufSize, GLsizei* length, GLchar* uniformBlockName); -extern GL_APICALL void (* GL_APIENTRY glUniformBlockBinding) (GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding); -extern GL_APICALL void (* GL_APIENTRY glDrawArraysInstanced) (GLenum mode, GLint first, GLsizei count, GLsizei instanceCount); -extern GL_APICALL void (* GL_APIENTRY glDrawElementsInstanced) (GLenum mode, GLsizei count, GLenum type, const GLvoid* indices, GLsizei instanceCount); -extern GL_APICALL GLsync (* GL_APIENTRY glFenceSync) (GLenum condition, GLbitfield flags); -extern GL_APICALL GLboolean (* GL_APIENTRY glIsSync) (GLsync sync); -extern GL_APICALL void (* GL_APIENTRY glDeleteSync) (GLsync sync); -extern GL_APICALL GLenum (* GL_APIENTRY glClientWaitSync) (GLsync sync, GLbitfield flags, GLuint64 timeout); -extern GL_APICALL void (* GL_APIENTRY glWaitSync) (GLsync sync, GLbitfield flags, GLuint64 timeout); -extern GL_APICALL void (* GL_APIENTRY glGetInteger64v) (GLenum pname, GLint64* params); -extern GL_APICALL void (* GL_APIENTRY glGetSynciv) (GLsync sync, GLenum pname, GLsizei bufSize, GLsizei* length, GLint* values); -extern GL_APICALL void (* GL_APIENTRY glGetInteger64i_v) (GLenum target, GLuint index, GLint64* data); -extern GL_APICALL void (* GL_APIENTRY glGetBufferParameteri64v) (GLenum target, GLenum pname, GLint64* params); -extern GL_APICALL void (* GL_APIENTRY glGenSamplers) (GLsizei count, GLuint* samplers); -extern GL_APICALL void (* GL_APIENTRY glDeleteSamplers) (GLsizei count, const GLuint* samplers); -extern GL_APICALL GLboolean (* GL_APIENTRY glIsSampler) (GLuint sampler); -extern GL_APICALL void (* GL_APIENTRY glBindSampler) (GLuint unit, GLuint sampler); -extern GL_APICALL void (* GL_APIENTRY glSamplerParameteri) (GLuint sampler, GLenum pname, GLint param); -extern GL_APICALL void (* GL_APIENTRY glSamplerParameteriv) (GLuint sampler, GLenum pname, const GLint* param); -extern GL_APICALL void (* GL_APIENTRY glSamplerParameterf) (GLuint sampler, GLenum pname, GLfloat param); -extern GL_APICALL void (* GL_APIENTRY glSamplerParameterfv) (GLuint sampler, GLenum pname, const GLfloat* param); -extern GL_APICALL void (* GL_APIENTRY glGetSamplerParameteriv) (GLuint sampler, GLenum pname, GLint* params); -extern GL_APICALL void (* GL_APIENTRY glGetSamplerParameterfv) (GLuint sampler, GLenum pname, GLfloat* params); -extern GL_APICALL void (* GL_APIENTRY glVertexAttribDivisor) (GLuint index, GLuint divisor); -extern GL_APICALL void (* GL_APIENTRY glBindTransformFeedback) (GLenum target, GLuint id); -extern GL_APICALL void (* GL_APIENTRY glDeleteTransformFeedbacks) (GLsizei n, const GLuint* ids); -extern GL_APICALL void (* GL_APIENTRY glGenTransformFeedbacks) (GLsizei n, GLuint* ids); -extern GL_APICALL GLboolean (* GL_APIENTRY glIsTransformFeedback) (GLuint id); -extern GL_APICALL void (* GL_APIENTRY glPauseTransformFeedback) (void); -extern GL_APICALL void (* GL_APIENTRY glResumeTransformFeedback) (void); -extern GL_APICALL void (* GL_APIENTRY glGetProgramBinary) (GLuint program, GLsizei bufSize, GLsizei* length, GLenum* binaryFormat, GLvoid* binary); -extern GL_APICALL void (* GL_APIENTRY glProgramBinary) (GLuint program, GLenum binaryFormat, const GLvoid* binary, GLsizei length); -extern GL_APICALL void (* GL_APIENTRY glProgramParameteri) (GLuint program, GLenum pname, GLint value); -extern GL_APICALL void (* GL_APIENTRY glInvalidateFramebuffer) (GLenum target, GLsizei numAttachments, const GLenum* attachments); -extern GL_APICALL void (* GL_APIENTRY glInvalidateSubFramebuffer) (GLenum target, GLsizei numAttachments, const GLenum* attachments, GLint x, GLint y, GLsizei width, GLsizei height); -extern GL_APICALL void (* GL_APIENTRY glTexStorage2D) (GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height); -extern GL_APICALL void (* GL_APIENTRY glTexStorage3D) (GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth); -extern GL_APICALL void (* GL_APIENTRY glGetInternalformativ) (GLenum target, GLenum internalformat, GLenum pname, GLsizei bufSize, GLint* params); +extern GL_APICALL void (* GL_APIENTRY glCopyBufferSubData) (GLenum readTarget, GLenum writeTarget, GLintptr readOffset, GLintptr writeOffset, GLsizeiptr size); +extern GL_APICALL void (* GL_APIENTRY glGetUniformIndices) (GLuint program, GLsizei uniformCount, const GLchar* const* uniformNames, GLuint* uniformIndices); +extern GL_APICALL void (* GL_APIENTRY glGetActiveUniformsiv) (GLuint program, GLsizei uniformCount, const GLuint* uniformIndices, GLenum pname, GLint* params); +extern GL_APICALL GLuint (* GL_APIENTRY glGetUniformBlockIndex) (GLuint program, const GLchar* uniformBlockName); +extern GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockiv) (GLuint program, GLuint uniformBlockIndex, GLenum pname, GLint* params); +extern GL_APICALL void (* GL_APIENTRY glGetActiveUniformBlockName) (GLuint program, GLuint uniformBlockIndex, GLsizei bufSize, GLsizei* length, GLchar* uniformBlockName); +extern GL_APICALL void (* GL_APIENTRY glUniformBlockBinding) (GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding); +extern GL_APICALL void (* GL_APIENTRY glDrawArraysInstanced) (GLenum mode, GLint first, GLsizei count, GLsizei instanceCount); +extern GL_APICALL void (* GL_APIENTRY glDrawElementsInstanced) (GLenum mode, GLsizei count, GLenum type, const GLvoid* indices, GLsizei instanceCount); +extern GL_APICALL GLsync (* GL_APIENTRY glFenceSync) (GLenum condition, GLbitfield flags); +extern GL_APICALL GLboolean (* GL_APIENTRY glIsSync) (GLsync sync); +extern GL_APICALL void (* GL_APIENTRY glDeleteSync) (GLsync sync); +extern GL_APICALL GLenum (* GL_APIENTRY glClientWaitSync) (GLsync sync, GLbitfield flags, GLuint64 timeout); +extern GL_APICALL void (* GL_APIENTRY glWaitSync) (GLsync sync, GLbitfield flags, GLuint64 timeout); +extern GL_APICALL void (* GL_APIENTRY glGetInteger64v) (GLenum pname, GLint64* params); +extern GL_APICALL void (* GL_APIENTRY glGetSynciv) (GLsync sync, GLenum pname, GLsizei bufSize, GLsizei* length, GLint* values); +extern GL_APICALL void (* GL_APIENTRY glGetInteger64i_v) (GLenum target, GLuint index, GLint64* data); +extern GL_APICALL void (* GL_APIENTRY glGetBufferParameteri64v) (GLenum target, GLenum pname, GLint64* params); +extern GL_APICALL void (* GL_APIENTRY glGenSamplers) (GLsizei count, GLuint* samplers); +extern GL_APICALL void (* GL_APIENTRY glDeleteSamplers) (GLsizei count, const GLuint* samplers); +extern GL_APICALL GLboolean (* GL_APIENTRY glIsSampler) (GLuint sampler); +extern GL_APICALL void (* GL_APIENTRY glBindSampler) (GLuint unit, GLuint sampler); +extern GL_APICALL void (* GL_APIENTRY glSamplerParameteri) (GLuint sampler, GLenum pname, GLint param); +extern GL_APICALL void (* GL_APIENTRY glSamplerParameteriv) (GLuint sampler, GLenum pname, const GLint* param); +extern GL_APICALL void (* GL_APIENTRY glSamplerParameterf) (GLuint sampler, GLenum pname, GLfloat param); +extern GL_APICALL void (* GL_APIENTRY glSamplerParameterfv) (GLuint sampler, GLenum pname, const GLfloat* param); +extern GL_APICALL void (* GL_APIENTRY glGetSamplerParameteriv) (GLuint sampler, GLenum pname, GLint* params); +extern GL_APICALL void (* GL_APIENTRY glGetSamplerParameterfv) (GLuint sampler, GLenum pname, GLfloat* params); +extern GL_APICALL void (* GL_APIENTRY glVertexAttribDivisor) (GLuint index, GLuint divisor); +extern GL_APICALL void (* GL_APIENTRY glBindTransformFeedback) (GLenum target, GLuint id); +extern GL_APICALL void (* GL_APIENTRY glDeleteTransformFeedbacks) (GLsizei n, const GLuint* ids); +extern GL_APICALL void (* GL_APIENTRY glGenTransformFeedbacks) (GLsizei n, GLuint* ids); +extern GL_APICALL GLboolean (* GL_APIENTRY glIsTransformFeedback) (GLuint id); +extern GL_APICALL void (* GL_APIENTRY glPauseTransformFeedback) (void); +extern GL_APICALL void (* GL_APIENTRY glResumeTransformFeedback) (void); +extern GL_APICALL void (* GL_APIENTRY glGetProgramBinary) (GLuint program, GLsizei bufSize, GLsizei* length, GLenum* binaryFormat, GLvoid* binary); +extern GL_APICALL void (* GL_APIENTRY glProgramBinary) (GLuint program, GLenum binaryFormat, const GLvoid* binary, GLsizei length); +extern GL_APICALL void (* GL_APIENTRY glProgramParameteri) (GLuint program, GLenum pname, GLint value); +extern GL_APICALL void (* GL_APIENTRY glInvalidateFramebuffer) (GLenum target, GLsizei numAttachments, const GLenum* attachments); +extern GL_APICALL void (* GL_APIENTRY glInvalidateSubFramebuffer) (GLenum target, GLsizei numAttachments, const GLenum* attachments, GLint x, GLint y, GLsizei width, GLsizei height); +extern GL_APICALL void (* GL_APIENTRY glTexStorage2D) (GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height); +extern GL_APICALL void (* GL_APIENTRY glTexStorage3D) (GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth); +extern GL_APICALL void (* GL_APIENTRY glGetInternalformativ) (GLenum target, GLenum internalformat, GLenum pname, GLsizei bufSize, GLint* params); #ifdef __cplusplus } diff --git a/ndk/sources/android/helper/interpolator.cpp b/ndk/sources/android/ndk_helper/interpolator.cpp similarity index 59% rename from ndk/sources/android/helper/interpolator.cpp rename to ndk/sources/android/ndk_helper/interpolator.cpp index 137b64f42..4167051a2 100644 --- a/ndk/sources/android/helper/interpolator.cpp +++ b/ndk/sources/android/ndk_helper/interpolator.cpp @@ -18,62 +18,68 @@ #include #include "interpolator.h" +namespace ndk_helper +{ //------------------------------------------------- //Ctor //------------------------------------------------- -interpolator::interpolator() +Interpolator::Interpolator() { - m_listParams.clear(); + list_params_.clear(); } //------------------------------------------------- //Dtor //------------------------------------------------- -interpolator::~interpolator() +Interpolator::~Interpolator() { - m_listParams.clear(); + list_params_.clear(); } -void interpolator::clear() +void Interpolator::Clear() { - m_listParams.clear(); + list_params_.clear(); } -interpolator& interpolator::set(float start,float dest, INTERPOLATOR_TYPE type, double duration) +Interpolator& Interpolator::Set( const float start, + const float dest, + const INTERPOLATOR_TYPE type, + const double duration ) { //init the parameters for the interpolation process - _dStartTime = perfMonitor::getCurrentTime(); - _dDestTime = _dStartTime + duration; - _type = type; + start_time_ = PerfMonitor::GetCurrentTime(); + dest_time_ = start_time_ + duration; + type_ = type; - _fStartValue = start; - _fDestValue = dest; + start_value_ = start; + dest_value_ = dest; return *this; } -interpolator& interpolator::add(const float dest, - INTERPOLATOR_TYPE type, double duration) +Interpolator& Interpolator::Add( const float dest, + const INTERPOLATOR_TYPE type, + const double duration ) { - interpolatorParam param; - param.fDestValue = dest; - param.type = type; - param.dDuration = duration; - m_listParams.push_back( param ); + InterpolatorParams param; + param.dest_value_ = dest; + param.type_ = type; + param.duration_ = duration; + list_params_.push_back( param ); return *this; } -bool interpolator::update( const double currentTime, float& p ) +bool Interpolator::Update( const double current_time, float& p ) { bool bContinue; - if( currentTime >= _dDestTime ) + if( current_time >= dest_time_ ) { - p = _fDestValue; - if( m_listParams.size () ) + p = dest_value_; + if( list_params_.size() ) { - interpolatorParam& item = m_listParams.front(); - set(_fDestValue, item.fDestValue, item.type, item.dDuration ); - m_listParams.pop_front(); + InterpolatorParams& item = list_params_.front(); + Set( dest_value_, item.dest_value_, item.type_, item.duration_ ); + list_params_.pop_front(); bContinue = true; } @@ -84,18 +90,22 @@ bool interpolator::update( const double currentTime, float& p ) } else { - float t = (float)(currentTime - _dStartTime); - float d = (float)(_dDestTime - _dStartTime); - float b = _fStartValue; - float c = _fDestValue - _fStartValue; - p = getFormula(_type, t, b, d, c); + float t = (float) (current_time - start_time_); + float d = (float) (dest_time_ - start_time_); + float b = start_value_; + float c = dest_value_ - start_value_; + p = GetFormula( type_, t, b, d, c ); bContinue = true; } return bContinue; } -float interpolator::getFormula(INTERPOLATOR_TYPE type, float t, float b, float d, float c) +float Interpolator::GetFormula( const INTERPOLATOR_TYPE type, + const float t, + const float b, + const float d, + const float c ) { float t1; switch( type ) @@ -112,17 +122,17 @@ float interpolator::getFormula(INTERPOLATOR_TYPE type, float t, float b, float case INTERPOLATOR_TYPE_EASEOUTQUAD: // quadratic (t^2) easing out - decelerating to zero velocity t1 = t / d; - return (-c * t1 * (t1-2) + b); + return (-c * t1 * (t1 - 2) + b); case INTERPOLATOR_TYPE_EASEINOUTQUAD: // quadratic easing in/out - acceleration until halfway, then deceleration - t1 = t / d / 2; - if (t1 < 1) - return ( c/2 * t1 * t1 + b); + t1 = t / d / 2; + if( t1 < 1 ) + return (c / 2 * t1 * t1 + b); else { - t1 = t1 -1; - return (-c/2 * (t1 * (t1-2) - 1) + b); + t1 = t1 - 1; + return (-c / 2 * (t1 * (t1 - 2) - 1) + b); } case INTERPOLATOR_TYPE_EASEINCUBIC: // cubic easing in - accelerating from zero velocity @@ -138,12 +148,12 @@ float interpolator::getFormula(INTERPOLATOR_TYPE type, float t, float b, float // cubic easing in - accelerating from zero velocity t1 = t / d / 2; - if ( t1 < 1) - return (c/2 * t1 * t1 * t1 + b); + if( t1 < 1 ) + return (c / 2 * t1 * t1 * t1 + b); else { t1 -= 2; - return (c/2 * (t1 * t1 * t1 + 2 ) + b); + return (c / 2 * (t1 * t1 * t1 + 2) + b); } case INTERPOLATOR_TYPE_EASEINQUART: // quartic easing in - accelerating from zero velocity @@ -152,18 +162,20 @@ float interpolator::getFormula(INTERPOLATOR_TYPE type, float t, float b, float case INTERPOLATOR_TYPE_EASEINEXPO: // exponential (2^t) easing in - accelerating from zero velocity - if (t==0) + if( t == 0 ) return b; else - return (c*powf(2,(10*(t/d-1)))+b); + return (c * powf( 2, (10 * (t / d - 1)) ) + b); case INTERPOLATOR_TYPE_EASEOUTEXPO: // exponential (2^t) easing out - decelerating to zero velocity - if (t==d) - return (b+c); + if( t == d ) + return (b + c); else - return (c * (-powf(2,-10*t/d)+1)+b); + return (c * (-powf( 2, -10 * t / d ) + 1) + b); default: return 0; } } + +} //namespace ndkHelper diff --git a/ndk/sources/android/helper/interpolator.h b/ndk/sources/android/ndk_helper/interpolator.h similarity index 61% rename from ndk/sources/android/helper/interpolator.h rename to ndk/sources/android/ndk_helper/interpolator.h index 66e887ba6..1226e334b 100644 --- a/ndk/sources/android/helper/interpolator.h +++ b/ndk/sources/android/ndk_helper/interpolator.h @@ -24,6 +24,9 @@ #include "perfMonitor.h" #include +namespace ndk_helper +{ + enum INTERPOLATOR_TYPE { INTERPOLATOR_TYPE_LINEAR, @@ -38,41 +41,49 @@ enum INTERPOLATOR_TYPE INTERPOLATOR_TYPE_EASEOUTEXPO, }; -struct interpolatorParam { - float fDestValue; - INTERPOLATOR_TYPE type; - double dDuration; +struct InterpolatorParams +{ + float dest_value_; + INTERPOLATOR_TYPE type_; + double duration_; }; /****************************************************************** * Interpolates values with several interpolation methods */ -class interpolator { +class Interpolator +{ private: - double _dStartTime; - double _dDestTime; - INTERPOLATOR_TYPE _type; + double start_time_; + double dest_time_; + INTERPOLATOR_TYPE type_; - float _fStartValue; - float _fDestValue; - std::list< interpolatorParam > m_listParams; + float start_value_; + float dest_value_; + std::list list_params_; - float getFormula(INTERPOLATOR_TYPE type, float t, float b, float d, float c); + float GetFormula( const INTERPOLATOR_TYPE type, + const float t, + const float b, + const float d, + const float c ); public: - interpolator(); - ~interpolator(); + Interpolator(); + ~Interpolator(); - interpolator& set(const float start, + Interpolator& Set( const float start, const float dest, - INTERPOLATOR_TYPE type, double duration); + const INTERPOLATOR_TYPE type, + double duration ); - interpolator& add(const float dest, - INTERPOLATOR_TYPE type, double duration); + Interpolator& Add( const float dest, + const INTERPOLATOR_TYPE type, + const double duration ); - bool update( const double currentTime, float& p ); + bool Update( const double currentTime, float& p ); - void clear(); + void Clear(); }; - +} //namespace ndkHelper #endif /* INTERPOLATOR_H_ */ diff --git a/ndk/sources/android/ndk_helper/perfMonitor.cpp b/ndk/sources/android/ndk_helper/perfMonitor.cpp new file mode 100644 index 000000000..dad0e6868 --- /dev/null +++ b/ndk/sources/android/ndk_helper/perfMonitor.cpp @@ -0,0 +1,72 @@ +/* + * 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" + +namespace ndk_helper +{ + +PerfMonitor::PerfMonitor() : + last_tick_( 0.f ), + tv_last_sec_( 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 tick = time - last_tick_; + double d = UpdateTick( tick ); + last_tick_ = time; + + if( Time.tv_sec - tv_last_sec_ >= 1 ) + { + double time = Time.tv_sec + Time.tv_usec * 1.0 / 1000000.0; + current_FPS_ = 1.f / d; + tv_last_sec_ = Time.tv_sec; + fFPS = current_FPS_; + return true; + } + else + { + fFPS = current_FPS_; + return false; + } +} + +} //namespace ndkHelper + diff --git a/ndk/sources/android/helper/perfMonitor.h b/ndk/sources/android/ndk_helper/perfMonitor.h similarity index 64% rename from ndk/sources/android/helper/perfMonitor.h rename to ndk/sources/android/ndk_helper/perfMonitor.h index c57f45bc2..f418c436b 100644 --- a/ndk/sources/android/helper/perfMonitor.h +++ b/ndk/sources/android/ndk_helper/perfMonitor.h @@ -22,35 +22,40 @@ #include #include "JNIHelper.h" +namespace ndk_helper +{ + const int32_t NUM_SAMPLES = 100; /****************************************************************** * Helper class for a performance monitoring and get current tick time */ -class perfMonitor { +class PerfMonitor +{ private: - float _fCurrentFPS; - time_t _tvLastSec; + float current_FPS_; + time_t tv_last_sec_; - double _dLastTick; - int32_t _tickindex; - double _ticksum; - double _ticklist[ NUM_SAMPLES ]; + double last_tick_; + int32_t tickindex_; + double ticksum_; + double ticklist_[NUM_SAMPLES]; - double updateTick(double currentTick); + double UpdateTick( double current_tick ); public: - perfMonitor(); - virtual ~perfMonitor(); + PerfMonitor(); + virtual ~PerfMonitor(); - bool update(float &fFPS); + bool Update( float &fFPS ); - static double getCurrentTime() + static double GetCurrentTime() { - struct timeval Time; - gettimeofday( &Time, NULL ); - double dTime = Time.tv_sec + Time.tv_usec * 1.0/1000000.0; - return dTime; + struct timeval time; + gettimeofday( &time, NULL ); + double ret = time.tv_sec + time.tv_usec * 1.0 / 1000000.0; + return ret; } }; +} //namespace ndkHelper #endif /* PERFMONITOR_H_ */ diff --git a/ndk/sources/android/ndk_helper/shader.cpp b/ndk/sources/android/ndk_helper/shader.cpp new file mode 100644 index 000000000..74b3887a0 --- /dev/null +++ b/ndk/sources/android/ndk_helper/shader.cpp @@ -0,0 +1,192 @@ +/* + * 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 +#include + +#include "shader.h" +#include "JNIHelper.h" + +namespace ndk_helper +{ + +#define DEBUG (1) + +bool shader::CompileShader( GLuint *shader, + const GLenum type, + const char *str_file_name, + const std::map& map_parameters ) +{ + std::vector data; + if( !JNIHelper::GetInstance()->ReadFile( str_file_name, &data ) ) + { + LOGI( "Can not open a file:%s", str_file_name ); + return false; + } + + const char REPLACEMENT_TAG = '*'; + //Fill-in parameters + std::string str( data.begin(), data.end() ); + std::string str_replacement_map( data.size(), ' ' ); + + std::map::const_iterator it = map_parameters.begin(); + std::map::const_iterator itEnd = map_parameters.end(); + while( it != itEnd ) + { + size_t pos = 0; + while( (pos = str.find( it->first, pos )) != std::string::npos ) + { + //Check if the sub string is already touched + + size_t replaced_pos = str_replacement_map.find( REPLACEMENT_TAG, pos ); + if( replaced_pos == std::string::npos || replaced_pos > pos ) + { + + str.replace( pos, it->first.length(), it->second ); + str_replacement_map.replace( pos, it->first.length(), it->first.length(), + REPLACEMENT_TAG ); + pos += it->second.length(); + } + else + { + //The replacement target has been touched by other tag, skipping them + pos += it->second.length(); + } + } + it++; + } + + LOGI( "Patched Shdader:\n%s", str.c_str() ); + + std::vector v( str.begin(), str.end() ); + str.clear(); + return shader::CompileShader( shader, type, v ); +} + +bool shader::CompileShader( GLuint *shader, + const GLenum type, + const GLchar *source, + const int32_t iSize ) +{ + if( source == NULL || iSize <= 0 ) + return false; + + *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 + + GLint status; + glGetShaderiv( *shader, GL_COMPILE_STATUS, &status ); + if( status == 0 ) + { + glDeleteShader( *shader ); + return false; + } + + return true; +} + +bool shader::CompileShader( GLuint *shader, + const GLenum type, + std::vector& data ) +{ + if( !data.size() ) + return false; + + const GLchar *source = (GLchar *) &data[0]; + int32_t iSize = data.size(); + return shader::CompileShader( shader, type, source, iSize ); +} + +bool shader::CompileShader( GLuint *shader, + const GLenum type, + const char *strFileName ) +{ + std::vector data; + bool b = JNIHelper::GetInstance()->ReadFile( strFileName, &data ); + if( !b ) + { + LOGI( "Can not open a file:%s", strFileName ); + return false; + } + + return shader::CompileShader( shader, type, data ); +} + +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; +} + +} //namespace ndkHelper + diff --git a/ndk/sources/android/ndk_helper/shader.h b/ndk/sources/android/ndk_helper/shader.h new file mode 100644 index 000000000..d362a5344 --- /dev/null +++ b/ndk/sources/android/ndk_helper/shader.h @@ -0,0 +1,124 @@ +/* + * 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 + +#include +#include +#include + +#include +#include + +#include + +#include "JNIHelper.h" + +namespace ndk_helper +{ + +namespace shader +{ + +/****************************************************************** + * Shader compiler helper + * namespace: ndkHelper::shader + * + */ + +/****************************************************************** + * CompileShader() with vector + * + * arguments: + * out: shader, shader variable + * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER) + * in: data, source vector + * return: true if a shader compilation succeeded, false if it failed + * + */ +bool CompileShader( GLuint *shader, const GLenum type, std::vector& data ); + +/****************************************************************** + * CompileShader() with buffer + * + * arguments: + * out: shader, shader variable + * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER) + * in: source, source buffer + * in: iSize, buffer size + * return: true if a shader compilation succeeded, false if it failed + * + */ +bool CompileShader( GLuint *shader, + const GLenum type, + const GLchar *source, + const int32_t iSize ); + +/****************************************************************** + * CompileShader() with filename + * + * arguments: + * out: shader, shader variable + * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER) + * in: strFilename, filename + * return: true if a shader compilation succeeded, false if it failed + * + */ +bool CompileShader( GLuint *shader, const GLenum type, const char *strFileName ); + +/****************************************************************** + * CompileShader() with std::map helps patching on a shader on the fly. + * + * arguments: + * out: shader, shader variable + * in: type, shader type (i.e. GL_VERTEX_SHADER/GL_FRAGMENT_SHADER) + * in: mapParameters + * For a example, + * map : %KEY% -> %VALUE% replaces all %KEY% entries in the given shader code to %VALUE" + * return: true if a shader compilation succeeded, false if it failed + * + */ +bool CompileShader( GLuint *shader, + const GLenum type, + const char *str_file_name, + const std::map& map_parameters ); + +/****************************************************************** + * LinkProgram() + * + * arguments: + * in: program, program + * return: true if a shader linkage succeeded, false if it failed + * + */ +bool LinkProgram( const GLuint prog ); + +/****************************************************************** + * validateProgram() + * + * arguments: + * in: program, program + * return: true if a shader validation succeeded, false if it failed + * + */ +bool ValidateProgram( const GLuint prog ); +} //namespace shader + +} //namespace ndkHelper +#endif /* SHADER_H_ */ diff --git a/ndk/sources/android/ndk_helper/tapCamera.cpp b/ndk/sources/android/ndk_helper/tapCamera.cpp new file mode 100644 index 000000000..5a4ff8bda --- /dev/null +++ b/ndk/sources/android/ndk_helper/tapCamera.cpp @@ -0,0 +1,320 @@ +/* + * 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 +#include "tapCamera.h" + +namespace ndk_helper +{ + +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() : + dragging_( false ), + pinching_( false ), + momentum_( false ), + ball_radius_( 0.75f ), + pinch_start_distance_SQ_( 0.f ), + camera_rotation_( 0.f ), + camera_rotation_start_( 0.f ), + camera_rotation_now_( 0.f ), + momemtum_steps_( 0.f ), + flip_z_( 0.f ) +{ + //Init offset + InitParameters(); + + vec_flip_ = Vec2( 1.f, -1.f ); + flip_z_ = -1.f; + vec_pinch_transform_factor_ = Vec3( 1.f, 1.f, 1.f ); + + vec_ball_center_ = Vec2( 0, 0 ); + vec_ball_now_ = Vec2( 0, 0 ); + vec_ball_down_ = Vec2( 0, 0 ); + + vec_pinch_start_ = Vec2( 0, 0 ); + vec_pinch_start_center_ = Vec2( 0, 0 ); + + vec_flip_ = Vec2( 0, 0 ); + +} + +void TapCamera::InitParameters() +{ + //Init parameters + vec_offset_ = Vec3(); + vec_offset_now_ = Vec3(); + + quat_ball_rot_ = Quaternion(); + quat_ball_now_ = Quaternion(); + quat_ball_now_.ToMatrix( mat_rotation_ ); + camera_rotation_ = 0.f; + + vec_drag_delta_ = Vec2(); + vec_offset_delta_ = Vec3(); + + momentum_ = false; +} + +//---------------------------------------------------------- +// Dtor +//---------------------------------------------------------- +TapCamera::~TapCamera() +{ + +} + +void TapCamera::Update() +{ + if( momentum_ ) + { + float momenttum_steps = momemtum_steps_; + + //Momentum rotation + Vec2 v = vec_drag_delta_; + BeginDrag( Vec2() ); //NOTE:This call reset _VDragDelta + Drag( v * vec_flip_ ); + + //Momentum shift + vec_offset_ += vec_offset_delta_; + + BallUpdate(); + EndDrag(); + + //Decrease deltas + vec_drag_delta_ = v * MOMENTUM_FACTOR_DECREASE; + vec_offset_delta_ = vec_offset_delta_ * MOMENTUM_FACTOR_DECREASE_SHIFT; + + //Count steps + momemtum_steps_ = momenttum_steps * MOMENTUM_FACTOR_DECREASE; + if( momemtum_steps_ < MOMENTUM_FACTOR_THRESHOLD ) + { + momentum_ = false; + } + } + else + { + vec_drag_delta_ *= MOMENTUM_FACTOR; + vec_offset_delta_ = vec_offset_delta_ * MOMENTUM_FACTOR; + BallUpdate(); + } + + Vec3 vec = vec_offset_ + vec_offset_now_; + Vec3 vec_tmp( TRANSFORM_FACTOR, -TRANSFORM_FACTOR, TRANSFORM_FACTORZ ); + + vec *= vec_tmp * vec_pinch_transform_factor_; + + mat_transform_ = Mat4::Translation( vec ); +} + +Mat4& TapCamera::GetRotationMatrix() +{ + return mat_rotation_; +} + +Mat4& TapCamera::GetTransformMatrix() +{ + return mat_transform_; +} + +void TapCamera::Reset( const bool bAnimate ) +{ + InitParameters(); + Update(); + +} + +//---------------------------------------------------------- +//Drag control +//---------------------------------------------------------- +void TapCamera::BeginDrag( const Vec2& v ) +{ + if( dragging_ ) + EndDrag(); + + if( pinching_ ) + EndPinch(); + + Vec2 vec = v * vec_flip_; + vec_ball_now_ = vec; + vec_ball_down_ = vec_ball_now_; + + dragging_ = true; + momentum_ = false; + vec_last_input_ = vec; + vec_drag_delta_ = Vec2(); +} + +void TapCamera::EndDrag() +{ + quat_ball_down_ = quat_ball_now_; + quat_ball_rot_ = Quaternion(); + + dragging_ = false; + momentum_ = true; + momemtum_steps_ = 1.0f; +} + +void TapCamera::Drag( const Vec2& v ) +{ + if( !dragging_ ) + return; + + Vec2 vec = v * vec_flip_; + vec_ball_now_ = vec; + + vec_drag_delta_ = vec_drag_delta_ * MOMENTUM_FACTOR + (vec - vec_last_input_); + vec_last_input_ = vec; +} + +//---------------------------------------------------------- +//Pinch controll +//---------------------------------------------------------- +void TapCamera::BeginPinch( const Vec2& v1, const Vec2& v2 ) +{ + if( dragging_ ) + EndDrag(); + + if( pinching_ ) + EndPinch(); + + BeginDrag( Vec2() ); + + vec_pinch_start_center_ = (v1 + v2) / 2.f; + + Vec2 vec = v1 - v2; + float x_diff; + float y_diff; + vec.Value( x_diff, y_diff ); + + pinch_start_distance_SQ_ = x_diff * x_diff + y_diff * y_diff; + camera_rotation_start_ = atan2f( y_diff, x_diff ); + camera_rotation_now_ = 0; + + pinching_ = true; + momentum_ = false; + + //Init momentum factors + vec_offset_delta_ = Vec3(); +} + +void TapCamera::EndPinch() +{ + pinching_ = false; + momentum_ = true; + momemtum_steps_ = 1.f; + vec_offset_ += vec_offset_now_; + camera_rotation_ += camera_rotation_now_; + vec_offset_now_ = Vec3(); + + camera_rotation_now_ = 0; + + EndDrag(); +} + +void TapCamera::Pinch( const Vec2& v1, const Vec2& v2 ) +{ + if( !pinching_ ) + return; + + //Update momentum factor + vec_offset_last_ = vec_offset_now_; + + float x_diff, y_diff; + Vec2 vec = v1 - v2; + vec.Value( x_diff, y_diff ); + + float fDistanceSQ = x_diff * x_diff + y_diff * y_diff; + + float f = pinch_start_distance_SQ_ / fDistanceSQ; + if( f < 1.f ) + f = -1.f / f + 1.0f; + else + f = f - 1.f; + if( isnan( f ) ) + f = 0.f; + + vec = (v1 + v2) / 2.f - vec_pinch_start_center_; + vec_offset_now_ = Vec3( vec, flip_z_ * f ); + + //Update momentum factor + vec_offset_delta_ = vec_offset_delta_ * MOMENTUM_FACTOR + + (vec_offset_now_ - vec_offset_last_); + + // + //Update ration quaternion + float fRotation = atan2f( y_diff, x_diff ); + camera_rotation_now_ = fRotation - camera_rotation_start_; + + //Trackball rotation + quat_ball_rot_ = Quaternion( 0.f, 0.f, sinf( -camera_rotation_now_ * 0.5f ), + cosf( -camera_rotation_now_ * 0.5f ) ); +} + +//---------------------------------------------------------- +//Trackball controll +//---------------------------------------------------------- +void TapCamera::BallUpdate() +{ + if( dragging_ ) + { + Vec3 vec_from = PointOnSphere( vec_ball_down_ ); + Vec3 vec_to = PointOnSphere( vec_ball_now_ ); + + Vec3 vec = vec_from.Cross( vec_to ); + float w = vec_from.Dot( vec_to ); + + Quaternion qDrag = Quaternion( vec, w ); + qDrag = qDrag * quat_ball_down_; + quat_ball_now_ = quat_ball_rot_ * qDrag; + } + quat_ball_now_.ToMatrix( mat_rotation_ ); +} + +Vec3 TapCamera::PointOnSphere( Vec2& point ) +{ + Vec3 ball_mouse; + float mag; + Vec2 vec = (point - vec_ball_center_) / ball_radius_; + mag = vec.Dot( vec ); + if( mag > 1.f ) + { + float scale = 1.f / sqrtf( mag ); + vec *= scale; + ball_mouse = Vec3( vec, 0.f ); + } + else + { + ball_mouse = Vec3( vec, sqrtf( 1.f - mag ) ); + } + return ball_mouse; +} + +} //namespace ndkHelper diff --git a/ndk/sources/android/ndk_helper/tapCamera.h b/ndk/sources/android/ndk_helper/tapCamera.h new file mode 100644 index 000000000..7d124e8e2 --- /dev/null +++ b/ndk/sources/android/ndk_helper/tapCamera.h @@ -0,0 +1,112 @@ +/* + * 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 +#include +#include + +#include "JNIHelper.h" +#include "vecmath.h" +#include "interpolator.h" + +namespace ndk_helper +{ + +/****************************************************************** + * Camera control helper class with a tap gesture + * This class is mainly used for 3D space camera control in samples. + * + */ +class TapCamera +{ +private: + //Trackball + Vec2 vec_ball_center_; + float ball_radius_; + Quaternion quat_ball_now_; + Quaternion quat_ball_down_; + Vec2 vec_ball_now_; + Vec2 vec_ball_down_; + Quaternion quat_ball_rot_; + + bool dragging_; + bool pinching_; + + //Pinch related info + Vec2 vec_pinch_start_; + Vec2 vec_pinch_start_center_; + float pinch_start_distance_SQ_; + + //Camera shift + Vec3 vec_offset_; + Vec3 vec_offset_now_; + + //Camera Rotation + float camera_rotation_; + float camera_rotation_start_; + float camera_rotation_now_; + + //Momentum support + bool momentum_; + Vec2 vec_drag_delta_; + Vec2 vec_last_input_; + Vec3 vec_offset_last_; + Vec3 vec_offset_delta_; + float momemtum_steps_; + + Vec2 vec_flip_; + float flip_z_; + + Mat4 mat_rotation_; + Mat4 mat_transform_; + + Vec3 vec_pinch_transform_factor_; + + 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 x, const float y, const float z ) + { + vec_flip_ = Vec2( x, y ); + flip_z_ = z; + } + + void SetPinchTransformFactor( const float x, const float y, const float z ) + { + vec_pinch_transform_factor_ = Vec3( x, y, z ); + } + + void Reset( const bool bAnimate ); + +}; + +} //namespace ndkHelper diff --git a/ndk/sources/android/ndk_helper/vecmath.cpp b/ndk/sources/android/ndk_helper/vecmath.cpp new file mode 100644 index 000000000..f4ae5f318 --- /dev/null +++ b/ndk/sources/android/ndk_helper/vecmath.cpp @@ -0,0 +1,379 @@ +/* + * Copy_right 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * y_ou may_ not use this file ex_cept 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 ex_press or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//-------------------------------------------------------------------------------- +// vecmath.cpp +//-------------------------------------------------------------------------------- +#include "vecmath.h" + +namespace ndk_helper +{ + +//-------------------------------------------------------------------------------- +// 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& vec_eye, const Vec3& vec_at, const Vec3& vec_up ) +{ + Vec3 vec_forward, vec_up_norm, vec_side; + Mat4 result; + + vec_forward.x_ = vec_eye.x_ - vec_at.x_; + vec_forward.y_ = vec_eye.y_ - vec_at.y_; + vec_forward.z_ = vec_eye.z_ - vec_at.z_; + + vec_forward.Normalize(); + vec_up_norm = vec_up; + vec_up_norm.Normalize(); + vec_side = vec_up_norm.Cross( vec_forward ); + vec_up_norm = vec_forward.Cross( vec_side ); + + result.f_[0] = vec_side.x_; + result.f_[4] = vec_side.y_; + result.f_[8] = vec_side.z_; + result.f_[12] = 0; + result.f_[1] = vec_up_norm.x_; + result.f_[5] = vec_up_norm.y_; + result.f_[9] = vec_up_norm.z_; + result.f_[13] = 0; + result.f_[2] = vec_forward.x_; + result.f_[6] = vec_forward.y_; + result.f_[10] = vec_forward.z_; + result.f_[14] = 0; + result.f_[3] = 0; + result.f_[7] = 0; + result.f_[11] = 0; + result.f_[15] = 1.0; + + result.PostTranslate( -vec_eye.x_, -vec_eye.y_, -vec_eye.z_ ); + return result; +} + +} //namespace ndkHelper + diff --git a/ndk/sources/android/ndk_helper/vecmath.h b/ndk/sources/android/ndk_helper/vecmath.h new file mode 100644 index 000000000..0692e3d01 --- /dev/null +++ b/ndk/sources/android/ndk_helper/vecmath.h @@ -0,0 +1,1116 @@ +/* + * 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 VECMATH_H_ +#define VECMATH_H_ + +#include +#include "JNIHelper.h" + +namespace ndk_helper +{ + +/****************************************************************** + * Helper class for vector math operations + * Currently all implementations are in pure C++. + * Each class is an opaque class so caller does not have a direct access + * to each element. This is for an ease of future optimization to use vector operations. + * + */ + +class Vec2; +class Vec3; +class Vec4; +class Mat4; + +/****************************************************************** + * 2 elements vector class + * + */ +class Vec2 +{ +private: + float x_; + float y_; + +public: + friend class Vec3; + friend class Vec4; + friend class Mat4; + friend class Quaternion; + + Vec2() + { + x_ = y_ = 0.f; + } + + Vec2( const float fX, const float fY ) + { + x_ = fX; + y_ = fY; + } + + Vec2( const Vec2& vec ) + { + x_ = vec.x_; + y_ = vec.y_; + } + + Vec2( const float* pVec ) + { + x_ = (*pVec++); + y_ = (*pVec++); + } + + //Operators + Vec2 operator*( const Vec2& rhs ) const + { + Vec2 ret; + ret.x_ = x_ * rhs.x_; + ret.y_ = y_ * rhs.y_; + return ret; + } + + Vec2 operator/( const Vec2& rhs ) const + { + Vec2 ret; + ret.x_ = x_ / rhs.x_; + ret.y_ = y_ / rhs.y_; + return ret; + } + + Vec2 operator+( const Vec2& rhs ) const + { + Vec2 ret; + ret.x_ = x_ + rhs.x_; + ret.y_ = y_ + rhs.y_; + return ret; + } + + Vec2 operator-( const Vec2& rhs ) const + { + Vec2 ret; + ret.x_ = x_ - rhs.x_; + ret.y_ = y_ - rhs.y_; + return ret; + } + + Vec2& operator+=( const Vec2& rhs ) + { + x_ += rhs.x_; + y_ += rhs.y_; + return *this; + } + + Vec2& operator-=( const Vec2& rhs ) + { + x_ -= rhs.x_; + y_ -= rhs.y_; + return *this; + } + + Vec2& operator*=( const Vec2& rhs ) + { + x_ *= rhs.x_; + y_ *= rhs.y_; + return *this; + } + + Vec2& operator/=( const Vec2& rhs ) + { + x_ /= rhs.x_; + y_ /= rhs.y_; + return *this; + } + + //External operators + friend Vec2 operator-( const Vec2& rhs ) + { + return Vec2( rhs ) *= -1; + } + + friend Vec2 operator*( const float lhs, const Vec2& rhs ) + { + Vec2 ret; + ret.x_ = lhs * rhs.x_; + ret.y_ = lhs * rhs.y_; + return ret; + } + + friend Vec2 operator/( const float lhs, const Vec2& rhs ) + { + Vec2 ret; + ret.x_ = lhs / rhs.x_; + ret.y_ = lhs / rhs.y_; + return ret; + } + + //Operators with float + Vec2 operator*( const float& rhs ) const + { + Vec2 ret; + ret.x_ = x_ * rhs; + ret.y_ = y_ * rhs; + return ret; + } + + Vec2& operator*=( const float& rhs ) + { + x_ = x_ * rhs; + y_ = y_ * rhs; + return *this; + } + + Vec2 operator/( const float& rhs ) const + { + Vec2 ret; + ret.x_ = x_ / rhs; + ret.y_ = y_ / rhs; + return ret; + } + + Vec2& operator/=( const float& rhs ) + { + x_ = x_ / rhs; + y_ = y_ / rhs; + return *this; + } + + //Compare + bool operator==( const Vec2& rhs ) const + { + if( x_ != rhs.x_ || y_ != rhs.y_ ) + return false; + return true; + } + + bool operator!=( const Vec2& rhs ) const + { + if( x_ == rhs.x_ ) + return false; + + return true; + } + + float Length() const + { + return sqrtf( x_ * x_ + y_ * y_ ); + } + + Vec2 Normalize() + { + float len = Length(); + x_ = x_ / len; + y_ = y_ / len; + return *this; + } + + float Dot( const Vec2& rhs ) + { + return x_ * rhs.x_ + y_ * rhs.y_; + } + + bool Validate() + { + if( isnan( x_ ) || isnan( y_ ) ) + return false; + return true; + } + + void Value( float& fX, float& fY ) + { + fX = x_; + fY = y_; + } + + void Dump() + { + LOGI( "Vec2 %f %f", x_, y_ ); + } +}; + +/****************************************************************** + * 3 elements vector class + * + */ +class Vec3 +{ +private: + float x_, y_, z_; + +public: + friend class Vec4; + friend class Mat4; + friend class Quaternion; + + Vec3() + { + x_ = y_ = z_ = 0.f; + } + + Vec3( const float fX, const float fY, const float fZ ) + { + x_ = fX; + y_ = fY; + z_ = fZ; + } + + Vec3( const Vec3& vec ) + { + x_ = vec.x_; + y_ = vec.y_; + z_ = vec.z_; + } + + Vec3( const float* pVec ) + { + x_ = (*pVec++); + y_ = (*pVec++); + z_ = *pVec; + } + + Vec3( const Vec2& vec, float f ) + { + x_ = vec.x_; + y_ = vec.y_; + z_ = f; + } + + Vec3( const Vec4& vec ); + + //Operators + Vec3 operator*( const Vec3& rhs ) const + { + Vec3 ret; + ret.x_ = x_ * rhs.x_; + ret.y_ = y_ * rhs.y_; + ret.z_ = z_ * rhs.z_; + return ret; + } + + Vec3 operator/( const Vec3& rhs ) const + { + Vec3 ret; + ret.x_ = x_ / rhs.x_; + ret.y_ = y_ / rhs.y_; + ret.z_ = z_ / rhs.z_; + return ret; + } + + Vec3 operator+( const Vec3& rhs ) const + { + Vec3 ret; + ret.x_ = x_ + rhs.x_; + ret.y_ = y_ + rhs.y_; + ret.z_ = z_ + rhs.z_; + return ret; + } + + Vec3 operator-( const Vec3& rhs ) const + { + Vec3 ret; + ret.x_ = x_ - rhs.x_; + ret.y_ = y_ - rhs.y_; + ret.z_ = z_ - rhs.z_; + return ret; + } + + Vec3& operator+=( const Vec3& rhs ) + { + x_ += rhs.x_; + y_ += rhs.y_; + z_ += rhs.z_; + return *this; + } + + Vec3& operator-=( const Vec3& rhs ) + { + x_ -= rhs.x_; + y_ -= rhs.y_; + z_ -= rhs.z_; + return *this; + } + + Vec3& operator*=( const Vec3& rhs ) + { + x_ *= rhs.x_; + y_ *= rhs.y_; + z_ *= rhs.z_; + return *this; + } + + Vec3& operator/=( const Vec3& rhs ) + { + x_ /= rhs.x_; + y_ /= rhs.y_; + z_ /= rhs.z_; + return *this; + } + + //External operators + friend Vec3 operator-( const Vec3& rhs ) + { + return Vec3( rhs ) *= -1; + } + + friend Vec3 operator*( const float lhs, const Vec3& rhs ) + { + Vec3 ret; + ret.x_ = lhs * rhs.x_; + ret.y_ = lhs * rhs.y_; + ret.z_ = lhs * rhs.z_; + return ret; + } + + friend Vec3 operator/( const float lhs, const Vec3& rhs ) + { + Vec3 ret; + ret.x_ = lhs / rhs.x_; + ret.y_ = lhs / rhs.y_; + ret.z_ = lhs / rhs.z_; + return ret; + } + + //Operators with float + Vec3 operator*( const float& rhs ) const + { + Vec3 ret; + ret.x_ = x_ * rhs; + ret.y_ = y_ * rhs; + ret.z_ = z_ * rhs; + return ret; + } + + Vec3& operator*=( const float& rhs ) + { + x_ = x_ * rhs; + y_ = y_ * rhs; + z_ = z_ * rhs; + return *this; + } + + Vec3 operator/( const float& rhs ) const + { + Vec3 ret; + ret.x_ = x_ / rhs; + ret.y_ = y_ / rhs; + ret.z_ = z_ / rhs; + return ret; + } + + Vec3& operator/=( const float& rhs ) + { + x_ = x_ / rhs; + y_ = y_ / rhs; + z_ = z_ / rhs; + return *this; + } + + //Compare + bool operator==( const Vec3& rhs ) const + { + if( x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ ) + return false; + return true; + } + + bool operator!=( const Vec3& rhs ) const + { + if( x_ == rhs.x_ ) + return false; + + return true; + } + + float Length() const + { + return sqrtf( x_ * x_ + y_ * y_ + z_ * z_ ); + } + + Vec3 Normalize() + { + float len = Length(); + x_ = x_ / len; + y_ = y_ / len; + z_ = z_ / len; + return *this; + } + + float Dot( const Vec3& rhs ) + { + return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; + } + + Vec3 Cross( const Vec3& rhs ) + { + Vec3 ret; + ret.x_ = y_ * rhs.z_ - z_ * rhs.y_; + ret.y_ = z_ * rhs.x_ - x_ * rhs.z_; + ret.z_ = x_ * rhs.y_ - y_ * rhs.x_; + return ret; + } + + bool Validate() + { + if( isnan( x_ ) || isnan( y_ ) || isnan( z_ ) ) + return false; + return true; + } + + void Value( float& fX, float& fY, float& fZ ) + { + fX = x_; + fY = y_; + fZ = z_; + } + + void Dump() + { + LOGI( "Vec3 %f %f %f", x_, y_, z_ ); + } +}; + +/****************************************************************** + * 4 elements vector class + * + */ +class Vec4 +{ +private: + float x_, y_, z_, w_; + +public: + friend class Vec3; + friend class Mat4; + friend class Quaternion; + + Vec4() + { + x_ = y_ = z_ = w_ = 0.f; + } + + Vec4( const float fX, const float fY, const float fZ, const float fW ) + { + x_ = fX; + y_ = fY; + z_ = fZ; + w_ = fW; + } + + Vec4( const Vec4& vec ) + { + x_ = vec.x_; + y_ = vec.y_; + z_ = vec.z_; + w_ = vec.w_; + } + + Vec4( const Vec3& vec, const float fW ) + { + x_ = vec.x_; + y_ = vec.y_; + z_ = vec.z_; + w_ = fW; + } + + Vec4( const float* pVec ) + { + x_ = (*pVec++); + y_ = (*pVec++); + z_ = *pVec; + w_ = *pVec; + } + + //Operators + Vec4 operator*( const Vec4& rhs ) const + { + Vec4 ret; + ret.x_ = x_ * rhs.x_; + ret.y_ = y_ * rhs.y_; + ret.z_ = z_ * rhs.z_; + ret.w_ = z_ * rhs.w_; + return ret; + } + + Vec4 operator/( const Vec4& rhs ) const + { + Vec4 ret; + ret.x_ = x_ / rhs.x_; + ret.y_ = y_ / rhs.y_; + ret.z_ = z_ / rhs.z_; + ret.w_ = z_ / rhs.w_; + return ret; + } + + Vec4 operator+( const Vec4& rhs ) const + { + Vec4 ret; + ret.x_ = x_ + rhs.x_; + ret.y_ = y_ + rhs.y_; + ret.z_ = z_ + rhs.z_; + ret.w_ = z_ + rhs.w_; + return ret; + } + + Vec4 operator-( const Vec4& rhs ) const + { + Vec4 ret; + ret.x_ = x_ - rhs.x_; + ret.y_ = y_ - rhs.y_; + ret.z_ = z_ - rhs.z_; + ret.w_ = z_ - rhs.w_; + return ret; + } + + Vec4& operator+=( const Vec4& rhs ) + { + x_ += rhs.x_; + y_ += rhs.y_; + z_ += rhs.z_; + w_ += rhs.w_; + return *this; + } + + Vec4& operator-=( const Vec4& rhs ) + { + x_ -= rhs.x_; + y_ -= rhs.y_; + z_ -= rhs.z_; + w_ -= rhs.w_; + return *this; + } + + Vec4& operator*=( const Vec4& rhs ) + { + x_ *= rhs.x_; + y_ *= rhs.y_; + z_ *= rhs.z_; + w_ *= rhs.w_; + return *this; + } + + Vec4& operator/=( const Vec4& rhs ) + { + x_ /= rhs.x_; + y_ /= rhs.y_; + z_ /= rhs.z_; + w_ /= rhs.w_; + return *this; + } + + //External operators + friend Vec4 operator-( const Vec4& rhs ) + { + return Vec4( rhs ) *= -1; + } + + friend Vec4 operator*( const float lhs, const Vec4& rhs ) + { + Vec4 ret; + ret.x_ = lhs * rhs.x_; + ret.y_ = lhs * rhs.y_; + ret.z_ = lhs * rhs.z_; + ret.w_ = lhs * rhs.w_; + return ret; + } + + friend Vec4 operator/( const float lhs, const Vec4& rhs ) + { + Vec4 ret; + ret.x_ = lhs / rhs.x_; + ret.y_ = lhs / rhs.y_; + ret.z_ = lhs / rhs.z_; + ret.w_ = lhs / rhs.w_; + return ret; + } + + //Operators with float + Vec4 operator*( const float& rhs ) const + { + Vec4 ret; + ret.x_ = x_ * rhs; + ret.y_ = y_ * rhs; + ret.z_ = z_ * rhs; + ret.w_ = w_ * rhs; + return ret; + } + + Vec4& operator*=( const float& rhs ) + { + x_ = x_ * rhs; + y_ = y_ * rhs; + z_ = z_ * rhs; + w_ = w_ * rhs; + return *this; + } + + Vec4 operator/( const float& rhs ) const + { + Vec4 ret; + ret.x_ = x_ / rhs; + ret.y_ = y_ / rhs; + ret.z_ = z_ / rhs; + ret.w_ = w_ / rhs; + return ret; + } + + Vec4& operator/=( const float& rhs ) + { + x_ = x_ / rhs; + y_ = y_ / rhs; + z_ = z_ / rhs; + w_ = w_ / rhs; + return *this; + } + + //Compare + bool operator==( const Vec4& rhs ) const + { + if( x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_ ) + return false; + return true; + } + + bool operator!=( const Vec4& rhs ) const + { + if( x_ == rhs.x_ ) + return false; + + return true; + } + + Vec4 operator*( const Mat4& rhs ) const; + + float Length() const + { + return sqrtf( x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_ ); + } + + Vec4 Normalize() + { + float len = Length(); + x_ = x_ / len; + y_ = y_ / len; + z_ = z_ / len; + w_ = w_ / len; + return *this; + } + + float Dot( const Vec3& rhs ) + { + return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; + } + + Vec3 Cross( const Vec3& rhs ) + { + Vec3 ret; + ret.x_ = y_ * rhs.z_ - z_ * rhs.y_; + ret.y_ = z_ * rhs.x_ - x_ * rhs.z_; + ret.z_ = x_ * rhs.y_ - y_ * rhs.x_; + return ret; + } + + bool Validate() + { + if( isnan( x_ ) || isnan( y_ ) || isnan( z_ ) || isnan( w_ ) ) + return false; + return true; + } + + void Value( float& fX, float& fY, float& fZ, float& fW ) + { + fX = x_; + fY = y_; + fZ = z_; + fW = w_; + } +}; + +/****************************************************************** + * 4x4 matrix + * + */ +class Mat4 +{ +private: + float f_[16]; + +public: + friend class Vec3; + friend class Vec4; + friend class Quaternion; + + Mat4(); + Mat4( const float* ); + + Mat4 operator*( const Mat4& rhs ) const; + Vec4 operator*( const Vec4& rhs ) const; + + Mat4 operator+( const Mat4& rhs ) const + { + Mat4 ret; + for( int32_t i = 0; i < 16; ++i ) + { + ret.f_[i] = f_[i] + rhs.f_[i]; + } + return ret; + } + + Mat4 operator-( const Mat4& rhs ) const + { + Mat4 ret; + for( int32_t i = 0; i < 16; ++i ) + { + ret.f_[i] = f_[i] - rhs.f_[i]; + } + return ret; + } + + Mat4& operator+=( const Mat4& rhs ) + { + for( int32_t i = 0; i < 16; ++i ) + { + f_[i] += rhs.f_[i]; + } + return *this; + } + + Mat4& operator-=( const Mat4& rhs ) + { + for( int32_t i = 0; i < 16; ++i ) + { + f_[i] -= rhs.f_[i]; + } + return *this; + } + + Mat4& operator*=( const Mat4& rhs ) + { + 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]; + + *this = ret; + return *this; + } + + Mat4 operator*( const float rhs ) + { + Mat4 ret; + for( int32_t i = 0; i < 16; ++i ) + { + ret.f_[i] = f_[i] * rhs; + } + return ret; + } + + Mat4& operator*=( const float rhs ) + { + for( int32_t i = 0; i < 16; ++i ) + { + f_[i] *= rhs; + } + return *this; + } + + Mat4& operator=( const Mat4& rhs ) + { + for( int32_t i = 0; i < 16; ++i ) + { + f_[i] = rhs.f_[i]; + } + return *this; + } + + Mat4 Inverse(); + + Mat4 Transpose() + { + Mat4 ret; + ret.f_[0] = f_[0]; + ret.f_[1] = f_[4]; + ret.f_[2] = f_[8]; + ret.f_[3] = f_[12]; + ret.f_[4] = f_[1]; + ret.f_[5] = f_[5]; + ret.f_[6] = f_[9]; + ret.f_[7] = f_[13]; + ret.f_[8] = f_[2]; + ret.f_[9] = f_[6]; + ret.f_[10] = f_[10]; + ret.f_[11] = f_[14]; + ret.f_[12] = f_[3]; + ret.f_[13] = f_[7]; + ret.f_[14] = f_[11]; + ret.f_[15] = f_[15]; + *this = ret; + return *this; + } + + Mat4& PostTranslate( float tx, float ty, float tz ) + { + f_[12] += (tx * f_[0]) + (ty * f_[4]) + (tz * f_[8]); + f_[13] += (tx * f_[1]) + (ty * f_[5]) + (tz * f_[9]); + f_[14] += (tx * f_[2]) + (ty * f_[6]) + (tz * f_[10]); + f_[15] += (tx * f_[3]) + (ty * f_[7]) + (tz * f_[11]); + return *this; + } + + float* Ptr() + { + return f_; + } + + //-------------------------------------------------------------------------------- + // Misc + //-------------------------------------------------------------------------------- + static Mat4 Perspective( float width, float height, float nearPlane, float farPlane ); + + static Mat4 LookAt( const Vec3& vEye, const Vec3& vAt, const Vec3& vUp ); + + static Mat4 Translation( const float fX, const float fY, const float fZ ); + static Mat4 Translation( const Vec3 vec ); + + static Mat4 RotationX( const float angle ); + + static Mat4 RotationY( const float angle ); + + static Mat4 RotationZ( const float angle ); + + static Mat4 Identity() + { + Mat4 ret; + ret.f_[0] = 1.f; + ret.f_[1] = 0; + ret.f_[2] = 0; + ret.f_[3] = 0; + ret.f_[4] = 0; + ret.f_[5] = 1.f; + ret.f_[6] = 0; + ret.f_[7] = 0; + ret.f_[8] = 0; + ret.f_[9] = 0; + ret.f_[10] = 1.f; + ret.f_[11] = 0; + ret.f_[12] = 0; + ret.f_[13] = 0; + ret.f_[14] = 0; + ret.f_[15] = 1.f; + return ret; + } + + void Dump() + { + LOGI( "%f %f %f %f", f_[0], f_[1], f_[2], f_[3] ); + LOGI( "%f %f %f %f", f_[4], f_[5], f_[6], f_[7] ); + LOGI( "%f %f %f %f", f_[8], f_[9], f_[10], f_[11] ); + LOGI( "%f %f %f %f", f_[12], f_[13], f_[14], f_[15] ); + } +}; + +/****************************************************************** + * Quaternion class + * + */ +class Quaternion +{ +private: + float x_, y_, z_, w_; + +public: + friend class Vec3; + friend class Vec4; + friend class Mat4; + + Quaternion() + { + x_ = 0.f; + y_ = 0.f; + z_ = 0.f; + w_ = 1.f; + } + + Quaternion( const float fX, const float fY, const float fZ, const float fW ) + { + x_ = fX; + y_ = fY; + z_ = fZ; + w_ = fW; + } + + Quaternion( const Vec3 vec, const float fW ) + { + x_ = vec.x_; + y_ = vec.y_; + z_ = vec.z_; + w_ = fW; + } + + Quaternion( const float* p ) + { + x_ = *p++; + y_ = *p++; + z_ = *p++; + w_ = *p++; + } + + Quaternion operator*( const Quaternion rhs ) + { + Quaternion ret; + ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_; + ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_; + ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_; + ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_; + return ret; + } + + Quaternion& operator*=( const Quaternion rhs ) + { + Quaternion ret; + ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_; + ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_; + ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_; + ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_; + *this = ret; + return *this; + } + + Quaternion Conjugate() + { + x_ = -x_; + y_ = -y_; + z_ = -z_; + return *this; + } + + //Non destuctive version + Quaternion Conjugated() + { + Quaternion ret; + ret.x_ = -x_; + ret.y_ = -y_; + ret.z_ = -z_; + ret.w_ = w_; + return ret; + } + + void ToMatrix( Mat4& mat ) + { + float x2 = x_ * x_ * 2.0f; + float y2 = y_ * y_ * 2.0f; + float z2 = z_ * z_ * 2.0f; + float xy = x_ * y_ * 2.0f; + float yz = y_ * z_ * 2.0f; + float zx = z_ * x_ * 2.0f; + float xw = x_ * w_ * 2.0f; + float yw = y_ * w_ * 2.0f; + float zw = z_ * w_ * 2.0f; + + mat.f_[0] = 1.0f - y2 - z2; + mat.f_[1] = xy + zw; + mat.f_[2] = zx - yw; + mat.f_[4] = xy - zw; + mat.f_[5] = 1.0f - z2 - x2; + mat.f_[6] = yz + xw; + mat.f_[8] = zx + yw; + mat.f_[9] = yz - xw; + mat.f_[10] = 1.0f - x2 - y2; + + mat.f_[3] = mat.f_[7] = mat.f_[11] = mat.f_[12] = mat.f_[13] = mat.f_[14] = 0.0f; + mat.f_[15] = 1.0f; + } + + void ToMatrixPreserveTranslate( Mat4& mat ) + { + float x2 = x_ * x_ * 2.0f; + float y2 = y_ * y_ * 2.0f; + float z2 = z_ * z_ * 2.0f; + float xy = x_ * y_ * 2.0f; + float yz = y_ * z_ * 2.0f; + float zx = z_ * x_ * 2.0f; + float xw = x_ * w_ * 2.0f; + float yw = y_ * w_ * 2.0f; + float zw = z_ * w_ * 2.0f; + + mat.f_[0] = 1.0f - y2 - z2; + mat.f_[1] = xy + zw; + mat.f_[2] = zx - yw; + mat.f_[4] = xy - zw; + mat.f_[5] = 1.0f - z2 - x2; + mat.f_[6] = yz + xw; + mat.f_[8] = zx + yw; + mat.f_[9] = yz - xw; + mat.f_[10] = 1.0f - x2 - y2; + + mat.f_[3] = mat.f_[7] = mat.f_[11] = 0.0f; + mat.f_[15] = 1.0f; + } + + static Quaternion RotationAxis( const Vec3 axis, const float angle ) + { + Quaternion ret; + float s = sinf( angle / 2 ); + ret.x_ = s * axis.x_; + ret.y_ = s * axis.y_; + ret.z_ = s * axis.z_; + ret.w_ = cosf( angle / 2 ); + return ret; + } + + void Value( float& fX, float& fY, float& fZ, float& fW ) + { + fX = x_; + fY = y_; + fZ = z_; + fW = w_; + } +}; + +} //namespace ndk_helper +#endif /* VECMATH_H_ */