微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OPENGLES 20 ```GlUniform4fv``` 在 C# Android 中为抛出异常的对象着色的方法

如何解决OPENGLES 20 ```GlUniform4fv``` 在 C# Android 中为抛出异常的对象着色的方法

我正在尝试使用 C# 在 Android Xamarin 中绘制一个三角形。三角形是一个带有 draw 方法的类。在这个 draw 方法中,有一个 openGL20 方法来为我们刚刚创建的三角形对象着色。每当 draw 方法中的执行到达此颜色对象方法时,都会抛出此异常 java.lang.IllegalArgumentException Message=length - offset < count*4 < needed。 我真的不明白这个错误是什么意思,但这是目前使用的代码

 public class Myglrenderer : java.lang.Object,GLSurfaceView.IRenderer
    {
     //Renderer method to draw the triangle object
       public void OnDrawFrame(IGL10 gl)
        {
            GLES20.GlClear(GLES20.GlColorBufferBit);
            Triangle triangle = new Triangle();
            triangle.draw();
        }
       //Method to set the view and the height of the painting window
          public void OnSurfaceChanged(IGL10 gl,int width,int height)
        {
            GLES20.GlViewport(0,width,height);
        }
         public void OnSurfaceCreated(IGL10 gl,Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            //Set the background frame color
            GLES20.GlClearColor(0.0f,0.0f,1.0f,0.0f);
            GLES20.GlDrawArrays(GLES20.GlColorBufferBit,2,10);
         }
    }

下面的代码定义了 Triangle 类和抛出注释异常的 OPENGL20 颜色方法

 public class Triangle
    {
        private FloatBuffer vertexBuffer;

        //Number of co-ordinates per vertex in this context
        private static readonly int Coords_per_vert = 3;

        private static float[] triangleCoords = new float[] {
            0.0f,0.622008459f,//top
            -0.5f,-0.311004243f,0f,//bottom left
            0.5f,0.0f   //bottom right
        };

        //Set color with red,green,blue and alpha values
        private float[] color = new float[] { 0.63671875f,0.76953125f,0.22265625f };

        private readonly int mProgram;

        public Triangle()
        {
            //Initialize vertex byte buffer for shape co-ordinates
            ByteBuffer bb = ByteBuffer.AllocateDirect(triangleCoords.Length * 4);
            //Use the device native byte order
            bb.Order(ByteOrder.NativeOrder());
            FloatBuffer myfloat = bb.AsFloatBuffer();
            //Create floating point buffer from ByteBuffer
            vertexBuffer = bb.AsFloatBuffer();
            vertexBuffer.Put(triangleCoords);
            vertexBuffer.Position(0);

            int vertexShader = Myglrenderer.loadShader(GLES20.GlVertexShader,vertexshadercode);
            int fragmentShader = Myglrenderer.loadShader(GLES20.GlFragmentShader,fragmentshadercode);
            //Create empty opengles program
            mProgram = GLES20.GlCreateProgram();
            //Add vertex shader to program
            GLES20.GlAttachShader(mProgram,vertexShader);
            //Add Fragment to shader
            GLES20.GlAttachShader(mProgram,fragmentShader);
            //Create openGL program executables
            GLES20.GlLinkProgram(mProgram);
        }

        private readonly string vertexshadercode = "attribute vec4 vPosition;" +
                                                                                            "void main(){" +
                                                                                            "  gl_Position=vPosition;" +
                                                                                             "}";

        private readonly string fragmentshadercode = "precision mediump float;" +
                                                                                                 "uniform vec4 vColor;" +
                                                                                                  "void main(){" +
                                                                                                    "gl_FragColor=vColor;" +
                                                                                                       "}";

        private int positionHandle,colorHandle;
        private readonly int vertexCount = triangleCoords.Length / Coords_per_vert;
        private readonly int vertexStride = Coords_per_vert * 4;

        //Create a method for drawing the triangle
        public void draw()
        {
            //Add Program to open GLES Environment
            GLES20.gluseProgram(mProgram);
            //Get handle to vertex shader's vPosition member
            positionHandle = GLES20.GlGetAttribLocation(mProgram,"vPosition");
            //Enable a handle to the triangle's vertices
            GLES20.GlEnabLevertexAttribArray(positionHandle);
            //Prepare the triangle co ordinate data
            GLES20.GlVertexAttribPointer(positionHandle,Coords_per_vert,GLES20.GlFloat,false,vertexStride,vertexBuffer);
            //Get handle to fragment shader's vColor Member
            colorHandle = GLES20.GlGetUniformlocation(mProgram,"vColor");
            //Set color for drawing the triangle
            GLES20.gluniform4fv(colorHandle,1,color,3);
            
            //Draw the triangle,this method causes an exception
            GLES20.GlDrawArrays(GLES20.GlTriangles,vertexCount);
            //disable vertex array
            GLES20.GldisabLevertexAttribArray(positionHandle);
        }
    }

请帮我成功给三角形对象上色,我做错了什么

解决方法

您正在使用 3 个大小的浮点数组作为 vec4 制服。 像这样将 alpha 数组添加到您的颜色数组中 替换

(Module header)
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"

'>> In procedure
(set objects etc...)
strFrom1 = email.SenderEmailAddress
strFrom2 = email.Sender.Address
strTo1 = email.To
If strTo1 <> "" Then
  strTo2 = email.Recipients(1).PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
Else
  strTo2 = ""
End If

(loop mails and rules...)
strDom = "*" & somedomain.com
'>> Test if mail is to/from somedomain.com
If strFrom2 Like strDom Or strTo1 Like strDom Or strTo2 Like strDom Then
  (do stuff: Move,delete)
End If
(do the rest)

  private float[] color = new float[] { 0.63671875f,0.76953125f,0.22265625f };

第二个数组的最后一个成员表示正在绘制的颜色的透明度或不透明度

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。