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


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:35:00