在Xamarin中使用GL-ES在锚点之间画线

时间:2019-02-12 10:43:54

标签: xamarin opengl-es arcore

使用Xamarin参阅Hello AR:Here

我尝试在锚点之间添加线,这些锚点是用户在AR环境中从Android手机的摄像头视图中交互式放置的。

我创建了一个渲染器:

CreateOnGlThread

中初始化时
public void CreateOnGlThread(Context context)
    {
        ByteBuffer bb = ByteBuffer.AllocateDirect(LineCoords.Length * 4);
        bb.Order(ByteOrder.NativeOrder());
        VertexBuffer = bb.AsFloatBuffer();
        VertexBuffer.Put(LineCoords);
        VertexBuffer.Position(0);

        int vertexShader = ShaderUtil.loadShader(GLES20.GlVertexShader, VertexShaderCode, TAG);
        int fragmentShader = ShaderUtil.loadShader(GLES20.GlFragmentShader, FragmentShaderCode, TAG);

        mProgram = GLES20.GlCreateProgram();
        GLES20.GlAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.GlAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.GlLinkProgram(mProgram);

        //Android.Opengl.Matrix.SetIdentityM(mModelMatrix, 0);
    }

OnDrawFrame 中,锚确实已成功放置在AR空间中,并添加了下面的线以继续在它们之间绘制线。

            mPlaneRenderer.DrawPlanes(planes, camera.DisplayOrientedPose, projmtx);

            // Visualize anchors created by touch.
            float scaleFactor = 1.0f;

            LastAnthor = null;
            foreach (var anchor in mAnchors)
            {
                if (anchor.TrackingState != TrackingState.Tracking)
                    continue;

                anchor.Pose.ToMatrix(mAnchorMatrix, 0);
                Log.Info(TAG, "Anchor: " + anchor.Pose.Tx() + "," + anchor.Pose.Ty() + "," + anchor.Pose.Tz());

                mVirtualObject.updateModelMatrix(mAnchorMatrix, scaleFactor);
                mVirtualObject.Draw(viewmtx, projmtx, lightIntensity);

                //-- add to draw line
                if ( LastAnthor != null)
                {
                    try
                    {
                        mLineRenderer.SetVerts(LastAnthor, anchor);
                        mLineRenderer.SetColor(0.8f, 0.8f, 0f, 1.0f);
                        mLineRenderer.DrawLine(viewmtx, projmtx, lightIntensity);


                    } catch (System.Exception ei)
                    {
                        Log.Error(TAG, "Exception draw line:", ei);
                    }

                }
                LastAnthor = anchor;

还有实际的 DrawLine 函数,最后没有显示该行,但是显示了锚点。

public void DrawLine(float[] cameraView, float[] cameraPerspective, float lightIntensity)
    {
        GLES20.GlUseProgram(mProgram);
        PositionHandle = GLES20.GlGetAttribLocation(mProgram, "vPosition");
        GLES20.GlEnableVertexAttribArray(PositionHandle);

        GLES20.GlVertexAttribPointer(PositionHandle, COORDS_PER_VERTEX,
                     GLES20.GlFloat, false, VertexStride, VertexBuffer);

        ColorHandle = GLES20.GlGetUniformLocation(mProgram, "vColor");
        GLES20.GlUniform4fv(ColorHandle, 1, color, 0);

        mModelViewProjectionUniform = GLES20.GlGetUniformLocation(mProgram, "uMVPMatrix");
        ShaderUtil.CheckGLError(TAG, "GlGetUniformLocation");

        Android.Opengl.Matrix.MultiplyMM(mModelViewMatrix, 0, cameraView, 0, mModelMatrix, 0);
        Android.Opengl.Matrix.MultiplyMM(mModelViewProjectionMatrix, 0, cameraPerspective, 0, mModelViewMatrix, 0);
        GLES20.GlUniformMatrix4fv(mModelViewProjectionUniform, 1, false, mModelViewProjectionMatrix, 0);

        Log.Info(TAG, "VertexCount: " + VertexCount +", " + VertexBuffer);
        GLES20.GlDrawArrays(GLES20.GlLines, 0, VertexCount);
        ShaderUtil.CheckGLError(TAG, "GlDrawArrays");

        Log.Info(TAG, "Draw Line Completed.");

        GLES20.GlDisableVertexAttribArray(PositionHandle);
    }

0 个答案:

没有答案