C# 无法使用GLK/OpenTK渲染图像

C# 无法使用GLK/OpenTK渲染图像,c#,xamarin.ios,C#,Xamarin.ios,我们正在尝试使用Xamarin制作一个应用程序,它将在特定屏幕上的GLKView中显示一个小的动画人脸。我们已经寻找了渲染精灵的解决方案,我们提出的最佳解决方案来自于。我们甚至在GLKView中绘制一个简单的图像都有困难,而且这种方法没有真正的意义。我们正在将其从iOS转换为Xamarin C#,因此某些调用之间存在差异,但我们已尝试保持大多数片段的圆滑 以下是与此相关的代码部分: public class Sprite : NSObject { public void Render()

我们正在尝试使用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);
    }
}
在此GLKViewController中调用Sprite.Render(),如下所示:

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();
    }
}
public类AnimationViewController:GLKViewController
{
GLKView动画视图;
语境;
精灵玩家;
GLKBASE效应;
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
context=neweaglcontext(EAGLRenderingAPI.OpenGLES2);
if(上下文==null)
Console.WriteLine(“未能创建ES上下文…”);
animationView=新的GLKView(新的矩形f(UIScreen.MainScreen.Bounds.Width*0.05f,
UIScreen.MainScreen.Bounds.Height*0.05f,
UIScreen.MainScreen.Bounds.Width*0.9f,
UIScreen.MainScreen.Bounds.Height*0.75f),上下文);
SetCurrentContext(上下文);
animationView.DrawInRect+=新事件处理程序(animationView\u DrawInRect);
View.AddSubview(animationView);
效果=新的GLKBaseEffect();
Matrix4 projectionMatrix=Matrix4.CreateOrthographic偏心(0,animationView.Frame.Width,0,animationView.Frame.Height,-1024,1024);
effect.Transform.ProjectionMatrix=ProjectionMatrix;
player=新精灵(@“player.png”,效果);
}
void animationView_DrawInRect(对象发送方,GLKViewDrawEventArgs e)
{
GL.ClearColor(0.98f、0.98f、0.98f、1.0f);
//总账清除((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
总账BlendFunc(BlendingFactorSrc.SrcAlpha,BlendingFactorDest.oneminssrcalpha);
总账启用(启用上限混合);
player.Render();
}
}
指向整个代码文件的链接:


看起来问题只是第二次调用
VertexAttributePointer
时的输入错误。第二个
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"));