无法使用GLK/OpenTK渲染图像

本文关键字:OpenTK 图像 GLK | 更新日期: 2023-09-27 18:20:05

我们正在尝试使用Xamarin制作一个应用程序,该应用程序将在特定屏幕上的GLKView中有一个小的动画人脸。我们一直在寻找渲染精灵的解决方案,我们提出的最佳解决方案源于此解决方案。我们甚至在GLKView中绘制一个简单的图像时都遇到了问题,并且输出中的错误实际上没有意义。我们正在将其从iOS转换为Xamarin C#,因此某些调用之间存在差异,但我们已尝试保持大多数部分的一致性。

以下是与此相关的代码部分:

public class Sprite : NSObject
{
    public void Render()
    {
        Effect.Texture2d0.GLName = TextureInfo.Name;
        Effect.Texture2d0.Enabled = true;
        Effect.PrepareToDraw();
        GL.EnableVertexAttribArray((int)GLKVertexAttrib.Position);
        GL.EnableVertexAttribArray((int)GLKVertexAttrib.TexCoord0);
        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(Quad));
        Marshal.StructureToPtr(Quad, ptr, false);
        int offset = (int)ptr;
        GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "geomertryVertex"));
        GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "textureVertex"));
        GL.DrawArrays(BeginMode.TriangleStrip, 0, 4);
        Marshal.FreeHGlobal(ptr);
    }
}

Sprite.Render()在此GLKViewController中调用:

public class AnimationViewController : GLKViewController
{
    GLKView animationView;
    EAGLContext context;
    Sprite player;
    GLKBaseEffect effect;
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        context = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
        if (context == null)
            Console.WriteLine("Failed to create ES context...");
        animationView = new GLKView(new RectangleF(UIScreen.MainScreen.Bounds.Width * 0.05f,
                                          UIScreen.MainScreen.Bounds.Height * 0.05f,
                                          UIScreen.MainScreen.Bounds.Width * 0.9f,
                                          UIScreen.MainScreen.Bounds.Height * 0.75f), context);
        EAGLContext.SetCurrentContext(context);
        animationView.DrawInRect += new EventHandler<GLKViewDrawEventArgs>(animationView_DrawInRect);
        View.AddSubview(animationView);
        effect = new GLKBaseEffect();
        Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0, animationView.Frame.Width, 0, animationView.Frame.Height, -1024, 1024);
        effect.Transform.ProjectionMatrix = projectionMatrix;
        player = new Sprite(@"Player.png", effect);
    }
    void animationView_DrawInRect(object sender, GLKViewDrawEventArgs e)
    {
        GL.ClearColor(0.98f, 0.98f, 0.98f, 1.0f);
        //GL.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
        GL.Enable(EnableCap.Blend);
        player.Render();
    }
}

链接到整个代码文件:

  • Sprite类和相关结构
  • AnimationViewController类

无法使用GLK/OpenTK渲染图像

看起来问题只是对VertexAttribPointer的第二次调用中的拼写错误。第二个GLKVertexAttrib.Position应该是GLKVertexAttrib.TexCoord0:

GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "geomertryVertex"));
GL.VertexAttribPointer((uint)GLKVertexAttrib.TexCoord0, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "textureVertex"));