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  private ARCoreSharedCamera mCamera = null;
48 
49  private Vector<TextObject> mTexts;
50 
51  private static RTABMapActivity mActivity;
53  mActivity = c;
54  mDisplayRotationHelper = new DisplayRotationHelper(/*context=*/ c);
55  }
56 
57  private ProgressDialog mProgressDialog = null;
58  private Toast mToast = null;
59 
60  private boolean mTextChanged = false;
61  private ReentrantLock mTextLock = new ReentrantLock();
62 
63  public void setProgressDialog(ProgressDialog progressDialog)
64  {
65  mProgressDialog = progressDialog;
66  }
67 
68  public void setToast(Toast toast)
69  {
70  mToast = toast;
71  }
72 
73  public void setOffset(int offset)
74  {
75  mOffset = offset;
76  }
77 
78  public void setCamera(ARCoreSharedCamera camera)
79  {
80  mCamera = camera;
81  if(mCamera!=null)
82  {
83  mDisplayRotationHelper.onDisplayChanged(0);
84  mDisplayRotationHelper.updateSessionIfNeeded(mCamera);
85  }
86  }
87 
88  // Render loop of the Gl context.
89  public void onDrawFrame(GL10 useGLES20instead) {
90 
91  synchronized (this) {
92  if(mActivity.nativeApplication != 0)
93  {
94  int step = 0;
95  try
96  {
97  if(mCamera!=null)
98  {
99  step=1;
100  mCamera.updateGL();
101 
102  }
103 
104  step=2;
105 
106  final int value = RTABMapLib.render(mActivity.nativeApplication);
107 
108  if(mTextManager!=null)
109  {
110  step=3;
111  if(mTextChanged)
112  {
113  mTextChanged = false;
114  Vector<TextObject> txtcollection = new Vector<TextObject>();
115 
116  mTextLock.lock();
117  try {
118  if(mTexts.size() > 0)
119  {
120  txtcollection.addAll(mTexts);
121  }
122  } finally {
123  mTextLock.unlock();
124  }
125 
126  // Prepare the text for rendering
127  mTextManager.PrepareDraw(txtcollection);
128  }
129 
130  float[] mvp = new float[16];
131  Matrix.translateM(mvp, 0, mtrxProjectionAndView, 0, 0, mOffset, 0);
132  mTextManager.Draw(mvp);
133  }
134  step=4;
135  if(value != 0 && mProgressDialog != null && mProgressDialog.isShowing())
136  {
137  mActivity.runOnUiThread(new Runnable() {
138  public void run() {
139  if(!RTABMapActivity.DISABLE_LOG) Log.i("RTABMapActivity", "Renderer: dismiss dialog, value received=" + String.valueOf(value));
140  mProgressDialog.dismiss();
141  mActivity.resetNoTouchTimer();
142  }
143  });
144  }
145  if(value==-1)
146  {
147  mActivity.runOnUiThread(new Runnable() {
148  public void run() {
149  if(mToast!=null)
150  {
151  mToast.makeText(mActivity.getApplicationContext(), String.format("Out of Memory!"), Toast.LENGTH_SHORT).show();
152  }
153  }
154  });
155  }
156  else if(value==-2)
157  {
158  mActivity.runOnUiThread(new Runnable() {
159  public void run() {
160  if(mToast!=null)
161  {
162  mToast.makeText(mActivity.getApplicationContext(), String.format("Rendering Error!"), Toast.LENGTH_SHORT).show();
163  }
164  }
165  });
166  }
167  }
168  catch(final Exception e)
169  {
170  final int stepF = step;
171  mActivity.runOnUiThread(new Runnable() {
172  public void run() {
173  if(mToast!=null)
174  {
175  String msg = String.format("Rendering error! (exception=%s) step=%d", e.getMessage(), stepF);
176  Log.e("RTABMapActivity", msg);
177  mToast.makeText(mActivity.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
178  }
179  }
180 
181  });
182  }
183  }
184  }
185  }
186 
187  // Called when the surface size changes.
188  public void onSurfaceChanged(GL10 useGLES20instead, int width, int height) {
189 
190  if(mActivity.nativeApplication!=0)
191  {
192  RTABMapLib.setupGraphic(mActivity.nativeApplication, width, height);
193  }
194 
195  mDisplayRotationHelper.onSurfaceChanged(width, height);
196  if(mCamera!=null)
197  {
198  mDisplayRotationHelper.updateSessionIfNeeded(mCamera);
199  }
200 
201  mSurfaceHeight = (float)height;
202 
203  // Clear our matrices
204  for(int i=0;i<16;i++)
205  {
206  mtrxProjection[i] = 0.0f;
207  mtrxView[i] = 0.0f;
208  mtrxProjectionAndView[i] = 0.0f;
209  }
210 
211  // Setup our screen width and height for normal sprite translation.
212  Matrix.orthoM(mtrxProjection, 0, 0f, width, 0.0f, height, 0, 50);
213 
214  // Set the camera position (View matrix)
215  Matrix.setLookAtM(mtrxView, 0, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
216 
217  // Calculate the projection and view transformation
218  Matrix.multiplyMM(mtrxProjectionAndView, 0, mtrxProjection, 0, mtrxView, 0);
219  }
220 
221  // Called when the surface is created or recreated.
222  public void onSurfaceCreated(GL10 useGLES20instead, EGLConfig config) {
223 
224  if(mActivity.nativeApplication != 0)
225  {
227  }
228 
229  // Create our text manager
230  mTextManager = new TextManager(mActivity);
231  mTextManager.setColor(mTextColor);
232  }
233 
234  public void updateTexts(String[] texts)
235  {
236  if(mTextManager != null && mSurfaceHeight > 0.0f)
237  {
238  Vector<TextObject> textObjects = new Vector<TextObject>();
239  float offset = mSurfaceHeight-mTextManager.getMaxTextHeight();
240  if(texts != null)
241  {
242  for(int i=0;i<texts.length; ++i)
243  {
244  if(texts[i]!=null && texts[i].length()>0)
245  {
246  TextObject txt = new TextObject(texts[i], 0, offset);
247  textObjects.add(txt);
248  }
249  offset-=mTextManager.getMaxTextHeight();
250  }
251  }
252 
253  mTextLock.lock();
254  try {
255  mTexts = textObjects;
256  } finally {
257  mTextLock.unlock();
258  }
259 
260  mTextChanged = true;
261  }
262  }
263 
264  public void setTextColor(float color)
265  {
266  mTextColor = color;
267  if(mTextManager != null)
268  {
269  mTextManager.setColor(mTextColor);
270  }
271  }
272 }
static native int render(long nativeApplication)
ARCoreSharedCamera mCamera
Definition: Renderer.java:46
void run(class_loader::ClassLoader *loader)
f
final float [] mtrxProjection
Definition: Renderer.java:38
GLM_FUNC_DECL genType e()
void setProgressDialog(ProgressDialog progressDialog)
Definition: Renderer.java:63
GLM_FUNC_DECL genType step(genType const &edge, genType const &x)
static native void setupGraphic(long nativeApplication, int width, int height)
PM::Matrix Matrix
void onDrawFrame(GL10 useGLES20instead)
Definition: Renderer.java:89
void updateTexts(String[] texts)
Definition: Renderer.java:234
void onSurfaceChanged(GL10 useGLES20instead, int width, int height)
Definition: Renderer.java:188
final float [] mtrxProjectionAndView
Definition: Renderer.java:40
void setTextColor(float color)
Definition: Renderer.java:264
void setCamera(ARCoreSharedCamera camera)
Definition: Renderer.java:78
ProgressDialog mProgressDialog
Definition: Renderer.java:57
static native void initGlContent(long nativeApplication)
static RTABMapActivity mActivity
Definition: Renderer.java:51
Renderer(RTABMapActivity c)
Definition: Renderer.java:52
DisplayRotationHelper mDisplayRotationHelper
Definition: Renderer.java:47
void PrepareDraw(Vector< TextObject > txtcollection)
void updateSessionIfNeeded(ARCoreSharedCamera session)
Vector< TextObject > mTexts
Definition: Renderer.java:49
void onSurfaceCreated(GL10 useGLES20instead, EGLConfig config)
Definition: Renderer.java:222
GLM_FUNC_DECL genType::value_type length(genType const &x)
void setOffset(int offset)
Definition: Renderer.java:73
void setToast(Toast toast)
Definition: Renderer.java:68


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29