Exemplo n.º 1
1
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate");

    glSurfaceView = new GLSurfaceView(this);

    // Check if the system supports OpenGL ES 2.0.
    final ActivityManager activityManager =
        (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
    final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

    final MyRenderer mRenderer = new MyRenderer(this);

    if (supportsEs2) {
      // Request an OpenGL ES 2.0 compatible context.
      glSurfaceView.setEGLContextClientVersion(2);

      // Set the renderer to our demo renderer, defined below.
      glSurfaceView.setRenderer(mRenderer);
      // save the context when onPause is called
      glSurfaceView.setPreserveEGLContextOnPause(true);
      rendererSet = true;
    } else {
      Toast.makeText(this, "This device does not support OpenGL ES 2.0.", Toast.LENGTH_LONG).show();
      return;
    }

    glSurfaceView.setOnTouchListener(
        new View.OnTouchListener() {
          float previousX, previousY;

          @Override
          public boolean onTouch(View v, MotionEvent event) {

            // Dump touch event to log for debug
            // dumpEvent(event);

            // Handle touch events here...
            if (event != null) {
              switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                  previousX = event.getX();
                  previousY = event.getY();
                  Log.d(TAG, "mode=DRAG");
                  mode = DRAG;
                  break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                  Log.d(TAG, "mode=NONE");
                  mode = NONE;
                  break;
                case MotionEvent.ACTION_POINTER_DOWN:
                  oldDist = spacing(event);
                  Log.d(TAG, "oldDist=" + oldDist);
                  if (oldDist > 10f) {
                    midPoint(mid, event);
                    mode = ZOOM;
                    Log.d(TAG, "mode=ZOOM");
                  }
                  break;
                case MotionEvent.ACTION_MOVE:
                  if (mode == DRAG) {
                    final float deltaX = event.getX() - previousX;
                    final float deltaY = event.getY() - previousY;

                    previousX = event.getX();
                    previousY = event.getY();

                    glSurfaceView.queueEvent(
                        new Runnable() {
                          @Override
                          public void run() {
                            mRenderer.handleTouchDrag(deltaX, deltaY);
                          }
                        });
                  } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 10f) {
                      final float scale = newDist / oldDist;
                      Log.d(TAG, "scale=" + scale);

                      glSurfaceView.queueEvent(
                          new Runnable() {
                            @Override
                            public void run() {
                              mRenderer.handlePinchZoom(scale);
                            }
                          });
                    }
                  }
                  break;
              }
              return true;
            } else {
              return false;
            }
          }
        });
    setContentView(glSurfaceView);
    getActionBar().setTitle("Awesome Face");
    getActionBar().setDisplayHomeAsUpEnabled(true);
  }
  @Override
  public void onSetScript() {
    mSurfaceView = new GLSurfaceView(mActivity);

    mSurfaceView.setPreserveEGLContextOnPause(true);
    mSurfaceView.setEGLContextClientVersion(3);
    mSurfaceView.setEGLContextFactory(mContextFactory);
    mSurfaceView.setEGLConfigChooser(mConfigChooser);
    mSurfaceView.setEGLWindowSurfaceFactory(mWindowSurfaceFactory);
    mSurfaceView.setRenderer(mRenderer);
    mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    mActivity.setContentView(mSurfaceView);

    final DisplayMetrics metrics = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    final VrAppSettings appSettings = mActivity.getAppSettings();
    final int screenWidthPixels = Math.max(metrics.widthPixels, metrics.heightPixels);
    final int screenHeightPixels = Math.min(metrics.widthPixels, metrics.heightPixels);
    final int frameBufferWidth = appSettings.getFramebufferPixelsWide();
    final int frameBufferHeight = appSettings.getFramebufferPixelsHigh();
    final SurfaceHolder holder = mSurfaceView.getHolder();
    holder.setFormat(PixelFormat.TRANSLUCENT);

    if ((-1 != frameBufferHeight) && (-1 != frameBufferWidth)) {
      if ((screenWidthPixels != frameBufferWidth) && (screenHeightPixels != frameBufferHeight)) {
        Log.v(TAG, "--- window configuration ---");
        Log.v(TAG, "--- width: %d", frameBufferWidth);
        Log.v(TAG, "--- height: %d", frameBufferHeight);
        // a different resolution of the native window requested
        holder.setFixedSize((int) frameBufferWidth, (int) frameBufferHeight);
        Log.v(TAG, "----------------------------");
      }
    }
  }
 // Avoid destroying the surface (onSurfaceCreated will not be invoked)
 @TargetApi(11)
 private void setPreserveContext(Object view) {
   int sdkVersion = android.os.Build.VERSION.SDK_INT;
   if (sdkVersion >= 11 && view instanceof GLSurfaceView) {
     try {
       super.setPreserveEGLContextOnPause(true);
     } catch (Exception e) {
     }
   }
 }
Exemplo n.º 4
0
  private VideoRendererGui(GLSurfaceView surface) {
    this.surface = surface;
    // Create an OpenGL ES 2.0 context.
    surface.setPreserveEGLContextOnPause(true);
    surface.setEGLContextClientVersion(2);
    surface.setRenderer(this);
    surface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    yuvImageRenderers = new ArrayList<YuvImageRenderer>();
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    settingsFragment = new SettingsFragment();

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPreferences.registerOnSharedPreferenceChangeListener(this);

    // Debug
    //		setContentView(R.layout.title);

    mGLSurfaceView = new MyGLSurfaceView(this);
    // Create an OpenGL ES 2.0 context
    mGLSurfaceView.setEGLContextClientVersion(2);
    mGLSurfaceView.setPreserveEGLContextOnPause(true);
    //		mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    setContentView(mGLSurfaceView);

    // The app user object
    mUser = new User();

    mStarMapperRenderer = new StarMapperRenderer(this, mUser);

    // Set the StarMapperRenderer for drawing on the GLSurfaceView
    mGLSurfaceView.setRenderer(mStarMapperRenderer);

    // Retrieving location
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria locCriteria = new Criteria();
    // gps
    locCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    locCriteria.setBearingRequired(false);
    locCriteria.setAltitudeRequired(false);
    locCriteria.setCostAllowed(true);
    locCriteria.setSpeedRequired(false);
    locCriteria.setPowerRequirement(Criteria.POWER_LOW);

    String locProvider = mLocationManager.getBestProvider(locCriteria, true);
    Location loc = mLocationManager.getLastKnownLocation(locProvider);

    mUser.setGeoLocation((float) loc.getLatitude(), (float) loc.getLongitude());
    mUser.setGeomagneticField();
    mUser.setZenith();

    // Initializing touch variables
    mPreviousX1 = 0;
    mPreviousY1 = 0;
    mPreviousX2 = 0;
    mPreviousY2 = 0;
    mPointerIndex = 0;
    mCurrentTouchState = TouchState.INIT;
    mZoomer = new Zoom();

    // Initializing auto sensors
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometerSensor = null;
    mMagneticFieldSensor = null;
    mAccelerometerModel = null;
    mMagneticFieldModel = null;
    // Check for necessary sensors
    if ((mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) == null)
        || (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) == null)) {
      // phone doesn't have appropriate sensors, only use manual mode
      mUseAutoSensorMode = false;
    } else {
      // phone has sensors, initialize them
      mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      mMagneticFieldSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
      mUseAutoSensorMode = true;
      mAccelerometerModel = new AccelerometerModel(mUser, mStarMapperRenderer, mGLSurfaceView);
      mMagneticFieldModel = new MagneticFieldModel(mUser, mStarMapperRenderer, mGLSurfaceView);
    }

    if (mAccelerometerSensor != null) {
      mSensorManager.registerListener(
          mAccelerometerModel, mAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
    if (mMagneticFieldSensor != null) {
      mSensorManager.registerListener(
          mMagneticFieldModel, mMagneticFieldSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
    // ****** DEBUG ******
    // mUseAutoSensorMode = false;

    // flinger
    flinger =
        new GestureDetector(
            this,
            new Flinger(
                new FlingListener() {
                  public void fling(float distanceX, float distanceY) {
                    float radsPerPixel =
                        mStarMapperRenderer.mFovYRad / mStarMapperRenderer.mScreenHeight;
                    changeMapX(-distanceX * radsPerPixel);
                    changeMapY(-distanceY * radsPerPixel);

                    mStarMapperRenderer.mLookX = mUser.getLookX();
                    mStarMapperRenderer.mLookY = mUser.getLookY();
                    mStarMapperRenderer.mLookZ = mUser.getLookZ();
                    mStarMapperRenderer.mUpX = mUser.getNormalX();
                    mStarMapperRenderer.mUpY = mUser.getNormalY();
                    mStarMapperRenderer.mUpZ = mUser.getNormalZ();
                  }
                }));
  }