Renderer.java
Go to the documentation of this file.
00001 /*
00002  * Copyright 2014 Google Inc. All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of 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,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package com.introlab.rtabmap;
00018 
00019 import java.util.Vector;
00020 import java.util.concurrent.locks.ReentrantLock;
00021 
00022 import android.app.Activity;
00023 import android.app.ProgressDialog;
00024 import android.opengl.GLES20;
00025 import android.opengl.GLSurfaceView;
00026 import android.opengl.Matrix;
00027 import android.util.Log;
00028 import android.widget.Toast;
00029 
00030 import javax.microedition.khronos.egl.EGLConfig;
00031 import javax.microedition.khronos.opengles.GL10;
00032 
00033 // Renderer renders graphic content. This includes the point cloud,
00034 // ground grid, camera frustum, camera axis, and trajectory based on the Tango
00035 // device's pose.
00036 public class Renderer implements GLSurfaceView.Renderer {
00037 
00038         private final float[] mtrxProjection = new float[16];
00039         private final float[] mtrxView = new float[16];
00040         private final float[] mtrxProjectionAndView = new float[16];
00041 
00042         private TextManager mTextManager = null;
00043         private float mSurfaceHeight = 0.0f;
00044         private float mTextColor = 1.0f;
00045         private int mOffset = 0;
00046         
00047         private Vector<TextObject> mTexts;
00048 
00049         private static RTABMapActivity mActivity;
00050         public Renderer(RTABMapActivity c) {
00051                 mActivity = c;
00052         }
00053 
00054         private ProgressDialog mProgressDialog = null;
00055         private Toast mToast = null;
00056         
00057         private boolean mTextChanged = false;
00058         private ReentrantLock mTextLock = new ReentrantLock();
00059 
00060         public void setProgressDialog(ProgressDialog progressDialog)
00061         {
00062                 mProgressDialog = progressDialog;
00063         }
00064 
00065         public void setToast(Toast toast)
00066         {
00067                 mToast = toast;
00068         }
00069         
00070         public void setOffset(int offset)
00071         {
00072                 mOffset = offset;
00073         }
00074 
00075         // Render loop of the Gl context.
00076         public void onDrawFrame(GL10 useGLES20instead) {
00077 
00078                 try
00079                 {
00080                         final int value = RTABMapLib.render();
00081 
00082                         if(mTextManager!=null)
00083                         {
00084                                 if(mTextChanged)
00085                                 {
00086                                         mTextChanged = false;
00087                                         Vector<TextObject> txtcollection = new Vector<TextObject>();
00088                                         
00089                                         mTextLock.lock();
00090                                     try {
00091                                         if(mTexts.size() > 0)
00092                                         {
00093                                                 txtcollection.addAll(mTexts);
00094                                         }
00095                                     } finally {
00096                                         mTextLock.unlock();
00097                                     }
00098                                     
00099                                         // Prepare the text for rendering
00100                                         mTextManager.PrepareDraw(txtcollection);
00101                                 }
00102                                 
00103                                 float[] mvp = new float[16];
00104                                 Matrix.translateM(mvp, 0, mtrxProjectionAndView, 0, 0, mOffset, 0);
00105                                 mTextManager.Draw(mvp);
00106                         }
00107 
00108                         if(value != 0 && mProgressDialog != null && mProgressDialog.isShowing())
00109                         {
00110                                 mActivity.runOnUiThread(new Runnable() {
00111                                         public void run() {
00112                                                 if(!RTABMapActivity.DISABLE_LOG) Log.i("RTABMapActivity", "Renderer: dismiss dialog, value received=" + String.valueOf(value));
00113                                                 mProgressDialog.dismiss();
00114                                                 mActivity.resetNoTouchTimer();
00115                                         }
00116                                 });
00117                         }
00118                         if(value==-1)
00119                         {
00120                                 mActivity.runOnUiThread(new Runnable() {
00121                                         public void run() {
00122                                                 if(mToast!=null)
00123                                                 {
00124                                                         mToast.makeText(mActivity, String.format("Out of Memory!"), Toast.LENGTH_SHORT).show();
00125                                                 }
00126                                         }
00127                                 });
00128                         }
00129                         else if(value==-2)
00130                         {
00131                                 mActivity.runOnUiThread(new Runnable() {
00132                                         public void run() {
00133                                                 if(mToast!=null)
00134                                                 {
00135                                                         mToast.makeText(mActivity, String.format("Rendering Error!"), Toast.LENGTH_SHORT).show();
00136                                                 }
00137                                         }
00138                                 });
00139                         }
00140                 }
00141                 catch(final Exception e)
00142                 {
00143                         mActivity.runOnUiThread(new Runnable() {
00144                                 public void run() {
00145                                         if(mToast!=null)
00146                                         {
00147                                                 mToast.makeText(mActivity, String.format("Rendering error! %s", e.getMessage()), Toast.LENGTH_SHORT).show();
00148                                         }
00149                                 }
00150 
00151                         });
00152                 }
00153         }
00154 
00155         // Called when the surface size changes.
00156         public void onSurfaceChanged(GL10 useGLES20instead, int width, int height) {
00157                 
00158                 RTABMapLib.setupGraphic(width, height);
00159                 
00160                 mSurfaceHeight = (float)height;
00161 
00162                 // Clear our matrices
00163                 for(int i=0;i<16;i++)
00164                 {
00165                         mtrxProjection[i] = 0.0f;
00166                         mtrxView[i] = 0.0f;
00167                         mtrxProjectionAndView[i] = 0.0f;
00168                 }
00169 
00170                 // Setup our screen width and height for normal sprite translation.
00171                 Matrix.orthoM(mtrxProjection, 0, 0f, width, 0.0f, height, 0, 50);
00172 
00173                 // Set the camera position (View matrix)
00174                 Matrix.setLookAtM(mtrxView, 0, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
00175 
00176                 // Calculate the projection and view transformation
00177                 Matrix.multiplyMM(mtrxProjectionAndView, 0, mtrxProjection, 0, mtrxView, 0);
00178         }
00179 
00180         // Called when the surface is created or recreated.
00181         public void onSurfaceCreated(GL10 useGLES20instead, EGLConfig config) {
00182 
00183                 RTABMapLib.initGlContent();
00184                 
00185                 // Create our text manager
00186                 mTextManager = new TextManager(mActivity);
00187                 mTextManager.setColor(mTextColor);
00188         }
00189         
00190         public void updateTexts(String[] texts)
00191         {
00192                 if(mTextManager != null && mSurfaceHeight > 0.0f)
00193                 {
00194                         Vector<TextObject> textObjects = new Vector<TextObject>();
00195                         float offset = mSurfaceHeight-mTextManager.getMaxTextHeight();
00196                         if(texts != null)
00197                         {
00198                                 for(int i=0;i<texts.length; ++i)
00199                                 {
00200                                         if(texts[i]!=null && texts[i].length()>0)
00201                                         {
00202                                                 TextObject txt = new TextObject(texts[i], 0, offset);
00203                                                 textObjects.add(txt);
00204                                         }
00205                                         offset-=mTextManager.getMaxTextHeight();
00206                                 }
00207                         }
00208         
00209                         mTextLock.lock();
00210                         try {
00211                                 mTexts = textObjects;
00212                         } finally {
00213                                 mTextLock.unlock();
00214                         }
00215         
00216                         mTextChanged = true;
00217                 }
00218         }
00219         
00220         public void setTextColor(float color) 
00221         {
00222                 mTextColor = color;
00223                 if(mTextManager != null)
00224                 {
00225                         mTextManager.setColor(mTextColor);
00226                 }
00227         }
00228 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:21