首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OPENGLES 20 `GlUniform4fv```为对象着色的方法,在C#安卓中抛出异常

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

Stack Overflow用户
提问于 2021-03-02 02:32:32
回答 1查看 31关注 0票数 2

我正试着用C#在Android Xamarin中画一个三角形。三角形是一个带有draw方法的类。在这个绘制方法中,有一个openGL20方法用于给我们刚刚创建的三角形对象着色。无论何时在

方法到达此颜色对象方法时,将引发此异常

..。我真的不能理解这个错误是什么意思,但这是到目前为止我使用的代码

代码语言:javascript
运行
复制
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, 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颜色方法

代码语言:javascript
运行
复制
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,0.0f, //top
            -0.5f,-0.311004243f,0f  , //bottom left
            0.5f, -0.311004243f,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, 0, vertexCount);
            //Disable vertex array
            GLES20.GlDisableVertexAttribArray(positionHandle);
        }
    }

请帮我成功给这个三角形物体上色,我哪里做错了?

EN

Stack Overflow用户

回答已采纳

发布于 2021-03-02 03:08:16

您使用的是一个大小为3的浮点数组,用于vec4均匀分布。将alpha数组添加到颜色数组中,如下所示

代码语言:javascript
运行
复制
private float[] color = new float[] { 0.63671875f, 0.76953125f, 0.22265625f };

使用

代码语言:javascript
运行
复制
private float[] color = new float[] { 0.63671875f, 0.76953125f, 0.22265625f,1.0f};

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

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66427661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档