Renderer.java
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.introlab.rtabmap;
18 
19 import java.util.Vector;
20 import java.util.concurrent.locks.ReentrantLock;
21 
22 import android.app.Activity;
23 import android.app.ProgressDialog;
24 import android.opengl.GLES20;
25 import android.opengl.GLSurfaceView;
26 import android.opengl.Matrix;
27 import android.util.Log;
28 import android.widget.Toast;
29 
30 import javax.microedition.khronos.egl.EGLConfig;
31 import javax.microedition.khronos.opengles.GL10;
32 
33 // Renderer renders graphic content. This includes the point cloud,
34 // ground grid, camera frustum, camera axis, and trajectory based on the Tango
35 // device's pose.
36 public class Renderer implements GLSurfaceView.Renderer {
37 
38  private final float[] mtrxProjection = new float[16];
39  private final float[] mtrxView = new float[16];
40  private final float[] mtrxProjectionAndView = new float[16];
41 
42  private TextManager mTextManager = null;
43  private float mSurfaceHeight = 0.0f;
44  private float mTextColor = 1.0f;
45  private int mOffset = 0;
46 
47  private Vector<TextObject> mTexts;
48 
49  private static RTABMapActivity mActivity;
51  mActivity = c;
52  }
53 
54  private ProgressDialog mProgressDialog = null;
55  private Toast mToast = null;
56 
57  private boolean mTextChanged = false;
58  private ReentrantLock mTextLock = new ReentrantLock();
59 
60  public void setProgressDialog(ProgressDialog progressDialog)
61  {
62  mProgressDialog = progressDialog;
63  }
64 
65  public void setToast(Toast toast)
66  {
67  mToast = toast;
68  }
69 
70  public void setOffset(int offset)
71  {
72  mOffset = offset;
73  }
74 
75  // Render loop of the Gl context.
76  public void onDrawFrame(GL10 useGLES20instead) {
77 
78  try
79  {
80  final int value = RTABMapLib.render();
81 
82  if(mTextManager!=null)
83  {
84  if(mTextChanged)
85  {
86  mTextChanged = false;
87  Vector<TextObject> txtcollection = new Vector<TextObject>();
88 
89  mTextLock.lock();
90  try {
91  if(mTexts.size() > 0)
92  {
93  txtcollection.addAll(mTexts);
94  }
95  } finally {
96  mTextLock.unlock();
97  }
98 
99  // Prepare the text for rendering
100  mTextManager.PrepareDraw(txtcollection);
101  }
102 
103  float[] mvp = new float[16];
104  Matrix.translateM(mvp, 0, mtrxProjectionAndView, 0, 0, mOffset, 0);
105  mTextManager.Draw(mvp);
106  }
107 
108  if(value != 0 && mProgressDialog != null && mProgressDialog.isShowing())
109  {
110  mActivity.runOnUiThread(new Runnable() {
111  public void run() {
112  if(!RTABMapActivity.DISABLE_LOG) Log.i("RTABMapActivity", "Renderer: dismiss dialog, value received=" + String.valueOf(value));
113  mProgressDialog.dismiss();
114  mActivity.resetNoTouchTimer();
115  }
116  });
117  }
118  if(value==-1)
119  {
120  mActivity.runOnUiThread(new Runnable() {
121  public void run() {
122  if(mToast!=null)
123  {
124  mToast.makeText(mActivity, String.format("Out of Memory!"), Toast.LENGTH_SHORT).show();
125  }
126  }
127  });
128  }
129  else if(value==-2)
130  {
131  mActivity.runOnUiThread(new Runnable() {
132  public void run() {
133  if(mToast!=null)
134  {
135  mToast.makeText(mActivity, String.format("Rendering Error!"), Toast.LENGTH_SHORT).show();
136  }
137  }
138  });
139  }
140  }
141  catch(final Exception e)
142  {
143  mActivity.runOnUiThread(new Runnable() {
144  public void run() {
145  if(mToast!=null)
146  {
147  mToast.makeText(mActivity, String.format("Rendering error! %s", e.getMessage()), Toast.LENGTH_SHORT).show();
148  }
149  }
150 
151  });
152  }
153  }
154 
155  // Called when the surface size changes.
156  public void onSurfaceChanged(GL10 useGLES20instead, int width, int height) {
157 
158  RTABMapLib.setupGraphic(width, height);
159 
160  mSurfaceHeight = (float)height;
161 
162  // Clear our matrices
163  for(int i=0;i<16;i++)
164  {
165  mtrxProjection[i] = 0.0f;
166  mtrxView[i] = 0.0f;
167  mtrxProjectionAndView[i] = 0.0f;
168  }
169 
170  // Setup our screen width and height for normal sprite translation.
171  Matrix.orthoM(mtrxProjection, 0, 0f, width, 0.0f, height, 0, 50);
172 
173  // Set the camera position (View matrix)
174  Matrix.setLookAtM(mtrxView, 0, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
175 
176  // Calculate the projection and view transformation
177  Matrix.multiplyMM(mtrxProjectionAndView, 0, mtrxProjection, 0, mtrxView, 0);
178  }
179 
180  // Called when the surface is created or recreated.
181  public void onSurfaceCreated(GL10 useGLES20instead, EGLConfig config) {
182 
184 
185  // Create our text manager
186  mTextManager = new TextManager(mActivity);
187  mTextManager.setColor(mTextColor);
188  }
189 
190  public void updateTexts(String[] texts)
191  {
192  if(mTextManager != null && mSurfaceHeight > 0.0f)
193  {
194  Vector<TextObject> textObjects = new Vector<TextObject>();
195  float offset = mSurfaceHeight-mTextManager.getMaxTextHeight();
196  if(texts != null)
197  {
198  for(int i=0;i<texts.length; ++i)
199  {
200  if(texts[i]!=null && texts[i].length()>0)
201  {
202  TextObject txt = new TextObject(texts[i], 0, offset);
203  textObjects.add(txt);
204  }
205  offset-=mTextManager.getMaxTextHeight();
206  }
207  }
208 
209  mTextLock.lock();
210  try {
211  mTexts = textObjects;
212  } finally {
213  mTextLock.unlock();
214  }
215 
216  mTextChanged = true;
217  }
218  }
219 
220  public void setTextColor(float color)
221  {
222  mTextColor = color;
223  if(mTextManager != null)
224  {
225  mTextManager.setColor(mTextColor);
226  }
227  }
228 }
static native void setupGraphic(int width, int height)
f
final float[] mtrxProjection
Definition: Renderer.java:38
GLM_FUNC_DECL genType e()
void setProgressDialog(ProgressDialog progressDialog)
Definition: Renderer.java:60
void onDrawFrame(GL10 useGLES20instead)
Definition: Renderer.java:76
void updateTexts(String[] texts)
Definition: Renderer.java:190
void onSurfaceChanged(GL10 useGLES20instead, int width, int height)
Definition: Renderer.java:156
final float[] mtrxProjectionAndView
Definition: Renderer.java:40
void setTextColor(float color)
Definition: Renderer.java:220
ProgressDialog mProgressDialog
Definition: Renderer.java:54
static RTABMapActivity mActivity
Definition: Renderer.java:49
Renderer(RTABMapActivity c)
Definition: Renderer.java:50
void PrepareDraw(Vector< TextObject > txtcollection)
Vector< TextObject > mTexts
Definition: Renderer.java:47
void onSurfaceCreated(GL10 useGLES20instead, EGLConfig config)
Definition: Renderer.java:181
static native int render()
GLM_FUNC_DECL genType::value_type length(genType const &x)
void setOffset(int offset)
Definition: Renderer.java:70
void run(ClassLoader *loader)
void setToast(Toast toast)
Definition: Renderer.java:65
static native void initGlContent()


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32