DistanceRenderer.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.view;
00018 
00019 import android.content.Context;
00020 import android.content.SharedPreferences;
00021 import android.opengl.GLSurfaceView;
00022 import android.opengl.GLU;
00023 import android.preference.PreferenceManager;
00024 
00025 import java.util.List;
00026 
00027 import javax.microedition.khronos.egl.EGLConfig;
00028 import javax.microedition.khronos.opengles.GL10;
00029 
00035 class DistanceRenderer implements GLSurfaceView.Renderer {
00036 
00043   private static final float MIN_FOV_DISTANCE = 3f;
00048   private static final float MAX_FOV_DISTANCE = 8f;
00056   private static final float MIN_DISTANCE_ZOOM = -5.196152424f;
00060   private static final float MAX_DISTANCE_ZOOM = -13.856406465f;
00064   private static final float DISTANCE_VIEW_FIELD_OF_VIEW = 60f;
00070   private static final double DISTANCE_VIEW_ZOOM_MULTIPLIER = 1 / Math.tan(Math
00071       .toRadians(DISTANCE_VIEW_FIELD_OF_VIEW / 2));
00076   private static final String DISTANCE_VIEW_ZOOM_LOCK_KEY = "DISTANCE_VIEW_ZOOM_LOCK";
00080   private static final String DISTANCE_VIEW_ZOOM_MODE_KEY = "DISTANCE_VIEW_ZOOM_MODE";
00084   private static final String DISTANCE_VIEW_ZOOM_VALUE_KEY = "DISTANCE_VIEW_ZOOM_VALUE";
00088   private DistancePoints rangeLines;
00095   private float theta;
00100   private float zoom = MAX_DISTANCE_ZOOM;
00104   private ZoomMode zoomMode = ZoomMode.CLUTTER_ZOOM_MODE;
00108   private boolean zoomLocked;
00109   private SharedPreferences sharedPreferences;
00110   private SharedPreferences.Editor editor;
00111 
00112   @Override
00113   public void onSurfaceChanged(GL10 gl, int w, int h) {
00114     gl.glViewport(0, 0, w, h);
00115     gl.glMatrixMode(GL10.GL_PROJECTION);
00116     gl.glLoadIdentity();
00117     GLU.gluPerspective(gl, DISTANCE_VIEW_FIELD_OF_VIEW, (float) w / (float) h, 0.1f, 100.0f);
00118     gl.glMatrixMode(GL10.GL_MODELVIEW);
00119     gl.glLoadIdentity();
00120     gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
00121   }
00122 
00123   @Override
00124   public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
00125     gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
00126     rangeLines = new DistancePoints();
00127   }
00128 
00129   @Override
00130   public void onDrawFrame(GL10 gl) {
00131     gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
00132     gl.glLoadIdentity();
00133     // Move the camera up/down based on desired zoom level.
00134     gl.glTranslatef(0, 0, zoom);
00135     // Draw the distance triangles.
00136     rangeLines.drawRange(gl);
00137     // Draw the reference lines.
00138     rangeLines.drawReferenceMarker(gl);
00139     // Draw the robot.
00140     rangeLines.drawRobot(gl);
00141     gl.glRotatef(theta, 0, 0, -1f);
00142   }
00143 
00150   public void setRotation(float theta) {
00151     this.theta = theta;
00152   }
00153 
00161   public void setNormalizedZoom(float normalizedZoomValue) {
00162     if (zoomMode == ZoomMode.CUSTOM_ZOOM_MODE) {
00163       setZoom((1 - normalizedZoomValue) * (MAX_FOV_DISTANCE - MIN_FOV_DISTANCE) + MIN_FOV_DISTANCE);
00164     }
00165   }
00166 
00170   public void setZoomMode(ZoomMode mode) {
00171     zoomMode = mode;
00172   }
00173 
00177   public void lockZoom() {
00178     zoomLocked = true;
00179   }
00180 
00184   public void unlockZoom() {
00185     zoomLocked = false;
00186   }
00187 
00195   public void currentSpeed(double speed) {
00196     if (zoomMode == ZoomMode.VELOCITY_ZOOM_MODE) {
00197       setZoom((float) (Math.abs(speed) * ((MAX_FOV_DISTANCE - MIN_FOV_DISTANCE) + MIN_FOV_DISTANCE)));
00198     }
00199   }
00200 
00219   public void updateRange(List<Float> range, float maxRange, float minRange, float minTh,
00220       float thIncrement, float minDistToObject) {
00221     if (zoomMode == ZoomMode.CLUTTER_ZOOM_MODE) {
00222       // The closest object should be at the 80% of FOV mark.
00223       setZoom(minDistToObject * 1.25f);
00224     }
00225     // Update the distance ranges based on the incoming data.
00226     rangeLines.updateRange(range, maxRange, minRange, minTh, thIncrement);
00227   }
00228 
00235   public void loadPreferences(Context context) {
00236     sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
00237     int tmpMode =
00238         sharedPreferences.getInt(DISTANCE_VIEW_ZOOM_MODE_KEY, ZoomMode.CUSTOM_ZOOM_MODE.ordinal());
00239     if (tmpMode == ZoomMode.CUSTOM_ZOOM_MODE.ordinal()) {
00240       zoomMode = ZoomMode.CUSTOM_ZOOM_MODE;
00241     } else if (tmpMode == ZoomMode.CLUTTER_ZOOM_MODE.ordinal()) {
00242       zoomMode = ZoomMode.CLUTTER_ZOOM_MODE;
00243     } else if (tmpMode == ZoomMode.VELOCITY_ZOOM_MODE.ordinal()) {
00244       zoomMode = ZoomMode.VELOCITY_ZOOM_MODE;
00245     }
00246     zoomLocked = sharedPreferences.getBoolean(DISTANCE_VIEW_ZOOM_LOCK_KEY, false);
00247     editor = sharedPreferences.edit();
00248   }
00249 
00257   public void savePreferences(Context context) {
00258     editor = sharedPreferences.edit();
00259     editor.putInt(DISTANCE_VIEW_ZOOM_MODE_KEY, zoomMode.ordinal());
00260     editor.putFloat(DISTANCE_VIEW_ZOOM_VALUE_KEY, zoom);
00261     editor.putBoolean(DISTANCE_VIEW_ZOOM_LOCK_KEY, zoomLocked);
00262     editor.apply();
00263   }
00264 
00271   private void setZoom(float distanceFromCenter) {
00272     if (!zoomLocked) {
00273       // Bounds checking.
00274       if (distanceFromCenter < MIN_FOV_DISTANCE) {
00275         zoom = MIN_DISTANCE_ZOOM;
00276       } else if (distanceFromCenter > MAX_FOV_DISTANCE) {
00277         zoom = MAX_DISTANCE_ZOOM;
00278       } else {
00279         zoom = (float) (-distanceFromCenter * DISTANCE_VIEW_ZOOM_MULTIPLIER);
00280       }
00281     }
00282   }
00283 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07