GLPointsFrame.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
3 import android.graphics.Rect;
4 import android.opengl.GLES10;
5 
6 import java.nio.ByteBuffer;
7 import java.nio.ByteOrder;
8 import java.nio.IntBuffer;
9 
10 public class GLPointsFrame extends GLFrame {
11  private Frame mTexture;
12  private IntBuffer mGlTexture;
13  private float mDeltaX = 0;
14  private float mDeltaY = 0;
15  private float mRotationFactor = 0.1f;
16 
17  public synchronized void setTextureFrame(Frame frame) {
18  if(mTexture != null)
19  mTexture.close();
20  mTexture = frame.clone();
21  }
22  public int getTexture() { return mGlTexture.array()[0]; }
23 
24  private void drawPoints(float[] verArray, byte[] colorArray)
25  {
26  if(colorArray != null){
27  ByteBuffer tex = ByteBuffer.allocateDirect(colorArray.length);
28  tex.order(ByteOrder.nativeOrder());
29  tex.put(colorArray);
30  tex.position(0);
31 
32  GLES10.glEnableClientState(GLES10.GL_COLOR_ARRAY);
33  GLES10.glColorPointer(4, GLES10.GL_UNSIGNED_BYTE, 0, tex);
34  }
35 
36  ByteBuffer ver = ByteBuffer.allocateDirect(verArray.length * 4);
37  ver.order(ByteOrder.nativeOrder());
38  ver.asFloatBuffer().put(verArray);
39 
40  GLES10.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
41  GLES10.glVertexPointer(3, GLES10.GL_FLOAT, 0, ver);
42  GLES10.glDrawArrays(GLES10.GL_POINTS,0,verArray.length / 3);
43 
44  GLES10.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
45  if(colorArray != null)
46  GLES10.glDisableClientState(GLES10.GL_COLOR_ARRAY);
47  }
48 
49  @Override
50  public synchronized void draw(Rect rect)
51  {
52  if (mFrame == null || !(mFrame.is(Extension.POINTS)))
53  return;
54 
55  setViewport(rect);
56 
57  GLES10.glMatrixMode(GLES10.GL_PROJECTION);
58  GLES10.glPushMatrix();
59  GLES10.glLoadIdentity();
60 
61  GLES10.glOrthof(1f, -1f, -1f, 1f, -7f, 0f);
62 
63  GLES10.glRotatef(180, 0.0f, 0.0f, 1.0f);
64  GLES10.glRotatef(-mDeltaY * mRotationFactor, 1.0f, 0.0f, 0.0f);
65  GLES10.glRotatef(mDeltaX * mRotationFactor, 0.0f, 1.0f, 0.0f);
66 
68  float[] data = points.getVertices();
69  byte[] tex = mTexture == null ? createTexture(data, 1.2f) : createTexture(points);
70  drawPoints(data, tex);
71 
72  GLES10.glMatrixMode(GLES10.GL_PROJECTION);
73  GLES10.glPopMatrix();
74  }
75 
76  //fallback to gray scale if no texture is available
77  private byte[] createTexture(float[] data, float maxRange){
78  int size = data.length / 3;
79  byte[] texture = new byte[size * 4];
80  for(int i = 0; i < size; i++){
81  float z = data[i * 3 + 2];
82  byte val = (byte) (z / maxRange * 255);
83  for(int j = 0; j < 3; j++)
84  texture[i*4+j] = val;
85  texture[i*4+3] = (byte) 255;
86  }
87  return texture;
88  }
89 
91  if(mTexture == null)
92  return null;
93 
94  VideoFrame vf = mTexture.as(Extension.VIDEO_FRAME);
95  int textureSize = vf.getWidth() * vf.getHeight();
96  int pointCount = points.getCount();
97  float[] pointsData = points.getVertices();
98  byte[] textureData = new byte[textureSize * 3];
99  byte[] texture = new byte[pointCount * 4];
100  float[] textureCoordinates = points.getTextureCoordinates();
101 
102  vf.getData(textureData);
103  int w = vf.getWidth();
104  int h = vf.getHeight();
105  for(int i = 0; i < pointCount; i++){
106  if(pointsData[i * 3 + 2] == 0)
107  continue;
108  float x = (float) Math.round(textureCoordinates[2 * i] * w);
109  float y = (float) Math.round(textureCoordinates[2 * i + 1] * h);
110  if(x <= 0 || y <= 0 || x >= w || y >= h)
111  continue;
112  long texIndex = (long) (x + y * w);
113  for(int j = 0; j < 3; j++)
114  texture[i*4+j] = textureData[(int) (texIndex * 3 + j)];
115  texture[i*4+3] = (byte) 255;
116  }
117  return texture;
118  }
119 
120  @Override
121  public synchronized void close() {
122  if(mFrame != null)
123  mFrame.close();
124  if(mGlTexture != null)
125  GLES10.glDeleteTextures(1, mGlTexture);
126  mGlTexture = null;
127  }
128 
129  public synchronized void rotate(float deltaX, float deltaY) {
130  mDeltaX += deltaX;
131  mDeltaY += deltaY;
132  }
133 }
GLint y
synchronized void setTextureFrame(Frame frame)
synchronized void rotate(float deltaX, float deltaY)
GLdouble GLdouble GLdouble w
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
GLdouble GLdouble z
The texture class.
Definition: example.hpp:402
GLuint GLfloat * val
void drawPoints(float[] verArray, byte[] colorArray)
GLdouble f
GLsizeiptr size
GLdouble x
GLint j
byte[] createTexture(float[] data, float maxRange)
Definition: example.hpp:70
unsigned char byte
Definition: src/types.h:52
boolean is(Extension extension)
Definition: Frame.java:9
GLuint texture
int i
Definition: parser.hpp:150
GLdouble GLdouble GLint GLint const GLdouble * points


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16