Xamarin Forms GLView在iOS上保持空白

时间:2017-09-12 13:11:28

标签: opengl-es xamarin.ios xamarin.forms glsl

我正在尝试使用Xamarin Forms为iOS和Android构建一个应用程序,展示了LiquidFun和OpenGL ES 3的流畅性。在Android上我得到了它,但在iOS上我的GL视图保持黑色。

从liquidFun我得到一个指针,其中包含显示流体的所有必需信息,这只是传递给OpenGL,但这似乎有效,因为在Android上工作正常,如果我使用OpenGL ES2上下文,相同的代码可以使用GLSL 200着色器。

private void Render()
{
    EAGLContext.SetCurrentContext(context);  //Context is OpenGL ES 3

    if (!initialized)
    {
        InitPixelSize();

        GL.GenFramebuffers(1, out mainFB);
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, mainFB);

        GL.GenRenderbuffers(1, out colorRenderBuffer);
        GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, colorRenderBuffer);

        GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferInternalFormat.Rgb8, widthPixel, heightPixel);
        GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, RenderbufferTarget.Renderbuffer, colorRenderBuffer);

        GL.Viewport(0, 0, widthPixel, heightPixel);

        CreateProgramWithShaders();
        initialized = true;
    }

    int drawFramebuffer, renderbuffer;
    GL.GetInteger(GetPName.FramebufferBinding, out drawFramebuffer);
    GL.GetInteger(GetPName.RenderbufferBinding, out renderbuffer);
    int firstFrameBuffer = drawFramebuffer; 

    // Render to post-processing framebuffer
    GL.BindFramebuffer(FramebufferTarget.Framebuffer, firstFrameBuffer);
    GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer);
    CheckGLError("After BindFramebuffer to drawFramebuffer");

    GL.ClearColor(Color4.Black);
    GL.Clear(ClearBufferMask.ColorBufferBit);
    CheckGLError("After ClearColor and Clear");

    // Render the soup
    GL.Enable(EnableCap.Blend);
    CheckGLError("After Enable");
    GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
    CheckGLError("After BlendFunc");
    DrawBase();
    CheckGLError("After DrawBase");

    GL.Disable(EnableCap.Blend);
    CheckGLError("After Disabel");

    //GL.Finish();
    CheckGLError("After finish");
}


private void DrawBase()
{
    GL.UseProgram(drawingProgram);
    CheckGLError("After UseProgram");

    // Enable
    GL.EnableVertexAttribArray(positionSlot);
    GL.EnableVertexAttribArray(projectionSlot);
    CheckGLError("After Enable Vertex...");

    SetupVBOs();
    CheckGLError("After SetupVBOs");

    GL.DrawArrays(BeginMode.Points, 0, positionProvider.GetParticleCount());
    CheckGLError("After DrawArrays");
    //GL.DrawElements(BeginMode.Points, positionProvider.GetParticleCount(), DrawElementsType.UnsignedInt, positionProvider.GetPositions());

    // Disable
    GL.DisableVertexAttribArray(positionSlot);
    GL.DisableVertexAttribArray(projectionSlot);
    CheckGLError("After Disabel Vertex...");
}

private void SetupVBOs()
{
    GL.VertexAttribPointer(positionSlot, 2, VertexAttribPointerType.Float, false, Vector2.SizeInBytes, positionProvider.GetPositions()); //2
    CheckGLError("In SetupVBOs - After GL Vertex Attrib");

    GL.UniformMatrix4(projectionSlot, false, ref projectionMatrix);
    CheckGLError("In SetupVBOs - After Uniform");
}

private void CreateProgramWithShaders()
{
    drawingProgram = GL.CreateProgram();
    if (drawingProgram == 0)
    throw new InvalidOperationException("Unable to create program");
    CheckGLError("Create drawingProgram");
    Logger.Add2Log("drawingProgram: " + drawingProgram);

    int vertexShader = OpenGLHelper.LoadShader("KomplexVertexShader", ShaderType.VertexShader);
    int fragmentShader = OpenGLHelper.LoadShader("KomplexFragmentShader", ShaderType.FragmentShader);

    GL.BindAttribLocation(drawingProgram, 0, "Position");
    CheckGLError("Bind Attrib Location");

    GL.AttachShader(drawingProgram, vertexShader);
    GL.AttachShader(drawingProgram, fragmentShader);
    CheckGLError("After Attach shader");

    if (!OpenGLHelper.LinkAndCheckProgram(drawingProgram))
    {
        Logger.Add2Log("Error when linking program for KomplexVertexShader and KomplexFragmentShader");
    }
    CheckGLError("Linking Program");

    // Attribute (Pointer only)       
    positionSlot = GL.GetAttribLocation(drawingProgram, "Position");
    projectionSlot = GL.GetUniformLocation(drawingProgram, "Projection");
    Logger.Add2Log("Attribute locations: " + positionSlot + " - " + projectionSlot);
}

运行此代码不会显示任何OpenGL错误。

希望你能帮助我! 谢谢,亨德里克

0 个答案:

没有答案