纹理三角形

时间:2016-09-05 09:19:40

标签: c# android xamarin opengl-es-1.1

我正在尝试使用此代码

使用OpenTK ES构建三角形
using System;
using OpenTK;
using OpenTK.Graphics.ES11;
using OpenTK.Platform.Android;
using Android.Content;

namespace TexturedTriangle
{
    class GLView1  AndroidGameView
    {
        public GLView1(Context context)  base(context)
        { }
        protected override void CreateFrameBuffer()
        {
            base.CreateFrameBuffer();
        }


        Vector2[] TexCoords =
        {
            new Vector2(0, 0),
            new Vector2(1, 0),
            new Vector2(0, 1),
        };
        Vector2[] Vertices =
        {
            new Vector2(0f, 0f),
            new Vector2(1f, 0f),
            new Vector2(0f, 1f),
        };
        int[] Tex = new int[1];
        int[] VBOs = new int[2];
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GL.ClearColor(0, 0, 0, 1f);
            GL.Clear((int)All.ColorBufferBit);

            GL.MatrixMode(All.Projection);
            GL.LoadIdentity();
            GL.MatrixMode(All.Modelview);
            GL.LoadIdentity();

            GL.Enable(All.Texture2D);


            GL.GenBuffers(2, VBOs);

            GL.BindBuffer(All.ArrayBuffer, VBOs[0]);
            GL.BufferData(All.ArrayBuffer,
                          new IntPtr(Vertices.Length  Vector2.SizeInBytes),  
                          Vertices, All.StaticDraw);

            GL.BindBuffer(All.ArrayBuffer, VBOs[1]);
            GL.BufferData(All.ArrayBuffer,
                          new IntPtr(TexCoords.Length  Vector2.SizeInBytes), 
                          TexCoords, All.StaticDraw);

            int[] Data = new int[32  32];

            for (int I = 0; I  32  32; I++)
            {
                if (I % 3 == 0) Data[I] = 255;
                if (I % 3 == 1) Data[I] = 65280;
                if (I % 3 == 2) Data[I] = 16711680;
            }

            GL.GenTextures(1, Tex);

            GL.BindTexture(All.Texture2D, Tex[0]);

            GL.TexParameter(All.Texture2D, All.TextureMinFilter, 
                            (int)All.Nearest);
            GL.TexParameter(All.Texture2D, All.TextureMagFilter,    
                            (int)All.Nearest);

            GL.TexImage2D(All.Texture2D, 0, 3, 32, 32, 0, All.Rgba, 
                          All.UnsignedByte, Data);

            GL.EnableClientState(All.VertexArray);
            GL.EnableClientState(All.TextureCoordArray);

            Run();
        }
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.BindBuffer(All.ArrayBuffer, VBOs[0]);
            GL.VertexPointer(2, All.Float,                                                 
                             BlittableValueType.StrideOf(Vertices)  
                             IntPtr.Zero);

            GL.BindBuffer(All.ArrayBuffer, VBOs[1]);
            GL.TexCoordPointer(2, All.Float,                    
                               BilittableValueType.StrideOf(TexCoords),
                               IntPtr.Zero);

            GL.DrawArrays(All.Triangles, 0, 3);

            SwapBuffers();
        }
    }
}

但只有一个白色的三角形

Textured triangle OpenGL ES

同时这段代码:

using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenTK;

namespace TexturedTriangle
{
    class Program
    {
        static void Main()
        {
            GameWindow Window = new GameWindow(512, 512, GraphicsMode.Default, "Textured Triangle");
            int[] Tex = new int[1];
            int[] VBOs = new int[2];

            Vector2[] TexCoords =
            {
                new Vector2(0, 0),
                new Vector2(1, 0),
                new Vector2(0, 1),
            };

            Vector2[] Vertices =
            {
                new Vector2(0f, 0f),
                new Vector2(1f, 0f),
                new Vector2(0f, 1f),
            };
            Window.Load += (sender, e) =>
            {
                GL.ClearColor(0, 0, 0, 1f);
                GL.Clear(ClearBufferMask.ColorBufferBit);

                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();

                GL.Enable(EnableCap.Texture2D);


                GL.GenBuffers(2, VBOs);

                GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(Vertices.Length * Vector2.SizeInBytes), Vertices, BufferUsageHint.StaticDraw);

                GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[1]);
                GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(TexCoords.Length * Vector2.SizeInBytes), TexCoords, BufferUsageHint.StaticDraw);

                int[] Data = new int[32 * 32];

                for (int I = 0; I < 32 * 32; I++)
                {
                    if (I % 3 == 0) Data[I] = 255;       
                    if (I % 3 == 1) Data[I] = 65280;     
                    if (I % 3 == 2) Data[I] = 16711680;  
                }

                GL.GenTextures(1, Tex);

                GL.BindTexture(TextureTarget.Texture2D, Tex[0]);

                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);

                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Three, 32, 32, 0, PixelFormat.Rgba, PixelType.UnsignedByte, Data);

                GL.EnableClientState(ArrayCap.VertexArray);
                GL.EnableClientState(ArrayCap.TextureCoordArray);
            };
            Window.RenderFrame += (sender, e) =>
            {
                GL.Clear(ClearBufferMask.ColorBufferBit);


                GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[0]);
                GL.VertexPointer(2, VertexPointerType.Float, BlittableValueType.StrideOf(Vertices), IntPtr.Zero);

                GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[1]);
                GL.TexCoordPointer(2, TexCoordPointerType.Float, BlittableValueType.StrideOf(TexCoords), IntPtr.Zero);

                GL.DrawArrays(PrimitiveType.Triangles, 0, 3);


                Window.SwapBuffers();
            };

            Window.Run();
        }
    }
}

作品和输出是

Textured triangle OpenTK

GL.TexImage2D();

之后

GL.GetError();给出InvalidValue

文件:www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml

问题是什么?

1 个答案:

答案 0 :(得分:0)

我发现错误,InternalFormat和Format必须匹配,否则会给出InvalidValue。