예제 #1
0
        public virtual void updateTrajectory(float[] translation)
        {
            mVertexBuffer.Position(mTrajectoryCount * 3);
            if (((mTrajectoryCount + 1) * 3) >= MAX_VERTICES)
            {
                Log.Warn(TAG, "Clearing float buffer");
                resetPath();
            }
            float dx = 0, dy = 0, dz = 0;

            try
            {
                dx = mVertexBuffer.Get(mVertexBuffer.Position() - 3) - translation[0];
                dy = mVertexBuffer.Get(mVertexBuffer.Position() - 2) - translation[2];
                dz = mVertexBuffer.Get(mVertexBuffer.Position() - 1) - (-translation[1]);
            }
            catch (System.IndexOutOfRangeException)
            {
                mVertexBuffer.Put(new float[] { translation[0], translation[2], -translation[1] });
                mTrajectoryCount++;
            }
            float distance = (float)Math.Sqrt(dx * dx + dy * dy + dz * dz);

            if (distance > MIN_DISTANCE_CHECK)
            {
                mVertexBuffer.Put(new float[] { translation[0], translation[2], -translation[1] });
                mTrajectoryCount++;
            }
        }
예제 #2
0
 public override FloatBuffer Put(FloatBuffer src)
 {
     if (src is HeapFloatBuffer)
     {
         if (src == this)
         {
             throw new IllegalArgumentException();
         }
         HeapFloatBuffer sb = (HeapFloatBuffer)src;
         int             n  = sb.Remaining();
         if (n > Remaining())
         {
             throw new BufferOverflowException();
         }
         System.Array.Copy(sb.Hb, sb.Ix(sb.Position()), Hb, Ix(Position()), n);
         sb.Position(sb.Position() + n);
         Position(Position() + n);
     }
     else if (src.Direct)
     {
         int n = src.Remaining();
         if (n > Remaining())
         {
             throw new BufferOverflowException();
         }
         src.Get(Hb, Ix(Position()), n);
         Position(Position() + n);
     }
     else
     {
         base.Put(src);
     }
     return(this);
 }
        /// <summary>
        /// Converts a plane polygon from ARCore into a <see cref="Vector3"/> array.
        /// </summary>
        /// <param name="buffer">The float buffer containing 2D vertices of the polygon</param>
        /// <param name="waveVectorArray">The <see cref="Vector3"/> array with the 3D vertices of the polygon</param>
        public static void ToWave(this FloatBuffer buffer, ref Vector3[] waveVectorArray)
        {
            buffer.Rewind();

            var boundaryVertices = buffer.Limit() / 2;

            if (waveVectorArray == null)
            {
                waveVectorArray = new Vector3[boundaryVertices];
            }
            else if (waveVectorArray.Length != boundaryVertices)
            {
                Array.Resize(ref waveVectorArray, boundaryVertices);
            }

            for (int i = 0; i < boundaryVertices; i++)
            {
                waveVectorArray[i].X = buffer.Get();
                waveVectorArray[i].Z = buffer.Get();
            }
        }
예제 #4
0
        public override void draw(float[] viewMatrix, float[] projectionMatrix)
        {
            lock (this)
            {
                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexVBO);

                if (mUpdateVBO.GetAndSet(false))
                {
                    if (mPointCloudBuffer != null)
                    {
                        mPointCloudBuffer.Position(0);
                        // Pass the info to the VBO
                        GLES20.GlBufferData(GLES20.GlArrayBuffer, mPointCloudBuffer.Capacity() * BYTES_PER_FLOAT, mPointCloudBuffer, GLES20.GlStaticDraw);
                        mPointCount = mPointCloudBuffer.Capacity() / 3;
                        float totalZ = 0;
                        for (int i = 0; i < mPointCloudBuffer.Capacity() - 3; i = i + 3)
                        {
                            totalZ = totalZ + mPointCloudBuffer.Get(i + 2);
                        }
                        if (mPointCount != 0)
                        {
                            mAverageZ = totalZ / mPointCount;
                        }
                        // signal the update
                        mUpdateVBO.Set(true);
                    }
                    mPointCloudBuffer = null;
                }

                if (mPointCount > 0)
                {
                    GLES20.GlUseProgram(mProgram);
                    updateMvpMatrix(viewMatrix, projectionMatrix);
                    GLES20.GlVertexAttribPointer(mPosHandle, COORDS_PER_VERTEX, GLES20.GlFloat, false, 0, 0);
                    GLES20.GlEnableVertexAttribArray(mPosHandle);
                    GLES20.GlUniformMatrix4fv(mMVPMatrixHandle, 1, false, MvpMatrix, 0);
                    GLES20.GlDrawArrays(GLES20.GlPoints, 0, mPointCount);
                }
                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
            }
        }
예제 #5
0
        /**
         * Updates the plane model transform matrix and extents.
         */
        private void updatePlaneParameters(float[] planeMatrix, float extentX, float extentZ,
                                           FloatBuffer boundary)
        {
            Array.Copy(planeMatrix, 0, mModelMatrix, 0, 16);
            if (boundary == null)
            {
                mVertexBuffer.Limit(0);
                mIndexBuffer.Limit(0);
                return;
            }

            // Generate a new set of vertices and a corresponding triangle strip index set so that
            // the plane boundary polygon has a fading edge. This is done by making a copy of the
            // boundary polygon vertices and scaling it down around center to push it inwards. Then
            // the index buffer is setup accordingly.
            boundary.Rewind();
            int boundaryVertices = boundary.Limit() / 2;
            int numVertices;
            int numIndices;

            numVertices = boundaryVertices * VERTS_PER_BOUNDARY_VERT;
            // drawn as GL_TRIANGLE_STRIP with 3n-2 triangles (n-2 for fill, 2n for perimeter).
            numIndices = boundaryVertices * INDICES_PER_BOUNDARY_VERT;

            if (mVertexBuffer.Capacity() < numVertices * COORDS_PER_VERTEX)
            {
                int size = mVertexBuffer.Capacity();
                while (size < numVertices * COORDS_PER_VERTEX)
                {
                    size *= 2;
                }
                mVertexBuffer = ByteBuffer.AllocateDirect(BYTES_PER_FLOAT * size)
                                .Order(ByteOrder.NativeOrder()).AsFloatBuffer();
            }
            mVertexBuffer.Rewind();
            mVertexBuffer.Limit(numVertices * COORDS_PER_VERTEX);


            if (mIndexBuffer.Capacity() < numIndices)
            {
                int size = mIndexBuffer.Capacity();
                while (size < numIndices)
                {
                    size *= 2;
                }
                mIndexBuffer = ByteBuffer.AllocateDirect(BYTES_PER_SHORT * size)
                               .Order(ByteOrder.NativeOrder()).AsShortBuffer();
            }
            mIndexBuffer.Rewind();
            mIndexBuffer.Limit(numIndices);

            // Note: when either dimension of the bounding box is smaller than 2*FADE_RADIUS_M we
            // generate a bunch of 0-area triangles.  These don't get rendered though so it works
            // out ok.
            float xScale = Math.Max((extentX - 2 * FADE_RADIUS_M) / extentX, 0.0f);
            float zScale = Math.Max((extentZ - 2 * FADE_RADIUS_M) / extentZ, 0.0f);

            while (boundary.HasRemaining)
            {
                float x = boundary.Get();
                float z = boundary.Get();
                mVertexBuffer.Put(x);
                mVertexBuffer.Put(z);
                mVertexBuffer.Put(0.0f);
                mVertexBuffer.Put(x * xScale);
                mVertexBuffer.Put(z * zScale);
                mVertexBuffer.Put(1.0f);
            }

            // step 1, perimeter
            mIndexBuffer.Put((short)((boundaryVertices - 1) * 2));
            for (int i = 0; i < boundaryVertices; ++i)
            {
                mIndexBuffer.Put((short)(i * 2));
                mIndexBuffer.Put((short)(i * 2 + 1));
            }
            mIndexBuffer.Put((short)1);
            // This leaves us on the interior edge of the perimeter between the inset vertices
            // for boundary verts n-1 and 0.

            // step 2, interior:
            for (int i = 1; i < boundaryVertices / 2; ++i)
            {
                mIndexBuffer.Put((short)((boundaryVertices - 1 - i) * 2 + 1));
                mIndexBuffer.Put((short)(i * 2 + 1));
            }
            if (boundaryVertices % 2 != 0)
            {
                mIndexBuffer.Put((short)((boundaryVertices / 2) * 2 + 1));
            }
        }
        // Bounding box [minX, minY, minZ, maxX, maxY, maxZ].
        private void CalculateBoundingBox(FloatBuffer vertices)
        {
            if (vertices.Limit() < 3)
            {
                mBoundingBoxs[0] = 0.0f;
                mBoundingBoxs[1] = 0.0f;
                mBoundingBoxs[2] = 0.0f;
                mBoundingBoxs[3] = 0.0f;
                mBoundingBoxs[4] = 0.0f;
                mBoundingBoxs[5] = 0.0f;
                return;
            }
            else
            {
                mBoundingBoxs[0] = vertices.Get(0);
                mBoundingBoxs[1] = vertices.Get(1);
                mBoundingBoxs[2] = vertices.Get(2);
                mBoundingBoxs[3] = vertices.Get(0);
                mBoundingBoxs[4] = vertices.Get(1);
                mBoundingBoxs[5] = vertices.Get(2);
            }

            // Use the first three pairs as the initial variables and get the three
            // maximum values and three minimum values.
            int index = 3;

            while (index < vertices.Limit() - 2)
            {
                if (vertices.Get(index) < mBoundingBoxs[0])
                {
                    mBoundingBoxs[0] = vertices.Get(index);
                }
                if (vertices.Get(index) > mBoundingBoxs[3])
                {
                    mBoundingBoxs[3] = vertices.Get(index);
                }
                index++;

                if (vertices.Get(index) < mBoundingBoxs[1])
                {
                    mBoundingBoxs[1] = vertices.Get(index);
                }
                if (vertices.Get(index) > mBoundingBoxs[4])
                {
                    mBoundingBoxs[4] = vertices.Get(index);
                }
                index++;

                if (vertices.Get(index) < mBoundingBoxs[2])
                {
                    mBoundingBoxs[2] = vertices.Get(index);
                }
                if (vertices.Get(index) > mBoundingBoxs[5])
                {
                    mBoundingBoxs[5] = vertices.Get(index);
                }
                index++;
            }
        }