Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 纹理缺少颜色_C#_Opengl_Mono_Opentk - Fatal编程技术网

C# 纹理缺少颜色

C# 纹理缺少颜色,c#,opengl,mono,opentk,C#,Opengl,Mono,Opentk,我有一张照片: 窗口显示两个纹理,左侧纹理是通过将其绑定到FBO并渲染到FBO(50个随机三角形)生成的 正确的纹理是由glTexImage2D和蒙娜丽莎的全彩图像生成的,但正如您所看到的,图像缺少红色,我认为问题出在OpenGL部分,因为我已经研究了传递给glTexImage2D的字节,它们确实包含红色 这里是C#/Mono/OpenTK源代码 using System; using System.Drawing; using System.Drawing.Imaging; using Sy

我有一张照片:

窗口显示两个纹理,左侧纹理是通过将其绑定到FBO并渲染到FBO(50个随机三角形)生成的

正确的纹理是由glTexImage2D和蒙娜丽莎的全彩图像生成的,但正如您所看到的,图像缺少红色,我认为问题出在OpenGL部分,因为我已经研究了传递给glTexImage2D的字节,它们确实包含红色

这里是C#/Mono/OpenTK源代码

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
using OpenTK.Platform.X11;
using OpenTK.Input;

using ManOCL;


namespace GlTest
{
    public class MainClass: GameWindow
    {
        public MainClass()
            : base(800, 600, new GraphicsMode(new ColorFormat(0, 0, 0, 0), 32, 0))
        {
            this.VSync = VSyncMode.Off;
        }

        public uint fbo;
        public uint textureA;
        public uint textureB;

        public int textureAWidth = 1024;
        public int textureAHeight = 1024;

        public int textureBWidth;
        public int textureBHeight;

        Random rand = new Random();

        protected override void OnLoad(EventArgs e)
        {
            GL.Enable(EnableCap.Blend);
            GL.ShadeModel(ShadingModel.Smooth);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.CullFace);

//            GL.PolygonMode(MaterialFace.Back, PolygonMode.Line);

            // Create Color Tex
            GL.GenTextures(1, out textureA);
            GL.BindTexture(TextureTarget.Texture2D, textureA);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, textureAWidth, textureAHeight, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToBorder);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToBorder);

            GL.GenTextures(1, out textureB);
            GL.BindTexture(TextureTarget.Texture2D, textureB);

            Bitmap bmp = new Bitmap("/Projects/MonaLisa/MonaLisa.bmp");

            Console.WriteLine(textureBWidth = bmp.Width);
            Console.WriteLine(textureBHeight = bmp.Height);

            //get the data out of the bitmap
            System.Drawing.Imaging.BitmapData bmpBits = bmp.LockBits
            (
                new System.Drawing.Rectangle(0,0,textureBWidth, textureBHeight),
                System.Drawing.Imaging.ImageLockMode.ReadOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppRgb
            );

            for (int row = 0; row < 1; row++)
            {
                for (int col = 0; col < 32; col++)
                {
                    Console.WriteLine
                    (
                        "{0}, {1}, {2}, {3}",
                        Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 0),
                        Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 1),
                        Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 2),
                        Marshal.ReadByte(bmpBits.Scan0, (row * textureBWidth + col) * 4 + 3)
                    );

                }
            }

            Console.WriteLine(bmpBits.Width);
            Console.WriteLine(bmpBits.Height);


            GL.TexImage2D
            (
                TextureTarget.Texture2D,
                0,
                PixelInternalFormat.Rgba,
                textureBWidth,
                textureBHeight,
                0,
                OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
                PixelType.UnsignedByte,
                bmpBits.Scan0
            );

//          GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, textureBWidth, textureBHeight, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToBorder);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToBorder);

            //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver
            bmp.UnlockBits(bmpBits);

            // Create a FBO and attach the textures
            GL.Ext.GenFramebuffers(1, out fbo);
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fbo);
            GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureA, 0);

            #region Test for Error
            switch (GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
            {
            case FramebufferErrorCode.FramebufferCompleteExt:
                {
                    Console.WriteLine("FBO: The framebuffer is complete and valid for rendering.");
                    break;
                }
            case FramebufferErrorCode.FramebufferIncompleteAttachmentExt:
                {
                    Console.WriteLine("FBO: One or more attachment points are not framebuffer attachment complete. This could mean there’s no texture attached or the format isn’t renderable. For color textures this means the base format must be RGB or RGBA and for depth textures it must be a DEPTH_COMPONENT format. Other causes of this error are that the width or height is zero or the z-offset is out of range in case of render to volume.");
                    break;
                }
            case FramebufferErrorCode.FramebufferIncompleteMissingAttachmentExt:
                {
                    Console.WriteLine("FBO: There are no attachments.");
                    break;
                }
            /* case  FramebufferErrorCode.GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT: 
                 {
                     Console.WriteLine("FBO: An object has been attached to more than one attachment point.");
                     break;
                 }*/
            case FramebufferErrorCode.FramebufferIncompleteDimensionsExt:
                {
                    Console.WriteLine("FBO: Attachments are of different size. All attachments must have the same width and height.");
                    break;
                }
            case FramebufferErrorCode.FramebufferIncompleteFormatsExt:
                {
                    Console.WriteLine("FBO: The color attachments have different format. All color attachments must have the same format.");
                    break;
                }
            case FramebufferErrorCode.FramebufferIncompleteDrawBufferExt:
                {
                    Console.WriteLine("FBO: An attachment point referenced by GL.DrawBuffers() doesn’t have an attachment.");
                    break;
                }
            case FramebufferErrorCode.FramebufferIncompleteReadBufferExt:
                {
                    Console.WriteLine("FBO: The attachment point referenced by GL.ReadBuffers() doesn’t have an attachment.");
                    break;
                }
            case FramebufferErrorCode.FramebufferUnsupportedExt:
                {
                    Console.WriteLine("FBO: This particular FBO configuration is not supported by the implementation.");
                    break;
                }
            default:
                {
                    Console.WriteLine("FBO: Status unknown. (yes, this is really bad.)");
                    break;
                }
            }

            // using FBO might have changed states, e.g. the FBO might not support stereoscopic views or double buffering
            int[] queryinfo = new int[6];
            GL.GetInteger(GetPName.MaxColorAttachmentsExt, out queryinfo[0]);
            GL.GetInteger(GetPName.AuxBuffers, out queryinfo[1]);
            GL.GetInteger(GetPName.MaxDrawBuffers, out queryinfo[2]);
            GL.GetInteger(GetPName.Stereo, out queryinfo[3]);
            GL.GetInteger(GetPName.Samples, out queryinfo[4]);
            GL.GetInteger(GetPName.Doublebuffer, out queryinfo[5]);
            Console.WriteLine("max. ColorBuffers: " + queryinfo[0] + " max. AuxBuffers: " + queryinfo[1] + " max. DrawBuffers: " + queryinfo[2] +
                               "\nStereo: " + queryinfo[3] + " Samples: " + queryinfo[4] + " DoubleBuffer: " + queryinfo[5]);

            Console.WriteLine("Last GL Error: " + GL.GetError());
            #endregion Test for Error


            GL.PushAttrib(AttribMask.ViewportBit);
            {
                GL.Viewport(0, 0, textureAWidth, textureAHeight);

                OpenTK.Matrix4 orthogonal = OpenTK.Matrix4.CreateOrthographicOffCenter(0, 1, 0, 1, -3, 3);
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadMatrix(ref orthogonal);

                Matrix4 lookat = Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadMatrix(ref lookat);


                // clear the screen in red, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
                GL.ClearColor(0f, 0f, 0f, 0f);
                GL.Clear(ClearBufferMask.ColorBufferBit);

                // smack 50 random triangles into the FBO's textures
                GL.Begin(BeginMode.Triangles);
                {
                    for (int i = 0; i < 50; i++)
                    {
                        GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
                        GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
                        GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
                        GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
                        GL.Color4(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())), ((float)(rand.NextDouble())));
                        GL.Vertex3(((float)(rand.NextDouble())), ((float)(rand.NextDouble())), 0);
                    }
                }
                GL.End();
            }
            GL.PopAttrib();         
            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // disable rendering into the FBO

            GL.ClearColor(1f, 1f, 1f, 0.0f);
            GL.Color3(1f, 1f, 1f);

            GL.Enable(EnableCap.Texture2D); // enable Texture Mapping
            GL.BindTexture(TextureTarget.Texture2D, 0); // bind default texture

//          IntPtr cglContext = Monobjc.OpenGL.CGL.GetCurrentContext();
//          
//          IntPtr cglShareGroup = Monobjc.OpenGL.CGL.GetShareGroup(cglContext);
//          
//          ManOCL.Context.ShareWithCGL(cglShareGroup);
//          
//          Kernel kernel = ManOCL.Kernel.Create
//          (
//              "kernel1",
//              @"__kernel void kernel1(__global int *srcimg, __global int * output, __global int *smp)
//              {
//              }",
//              new Argument[]
//              {
//                  DeviceGlobalMemory.CreateFromArray(new int[1]),
//                  DeviceGlobalMemory.CreateFromArray(new int[2]),
//                  DeviceGlobalMemory.CreateFromArray(new int[1])
//              }
//          );

//          Console.WriteLine("Success!");
//          Console.WriteLine(kernel);
        }

        protected override void OnUnload(EventArgs e)
        {
            // Clean up what we allocated before exiting
            GL.DeleteTextures(1, ref textureA);
            GL.DeleteTextures(1, ref textureB);
            GL.Ext.DeleteFramebuffers(1, ref fbo);

            base.OnUnload(e);
        }

        protected override void OnResize (EventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);

            double aspect_ratio = Width / (double)Height;

            OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4 * 3 / 2, (float)aspect_ratio, 1, 64);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref perspective);

            Matrix4 lookat = Matrix4.LookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref lookat);

            base.OnResize(e);
        }

        protected override void OnUpdateFrame (FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            if (Keyboard[Key.Escape])
            {
                this.Exit();
            }
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            this.Title = "Frames per Second: " + (1.0 / e.Time);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.PushMatrix();
            {
                // Draw the Color Texture
                GL.Translate(-1f, 0f, 0f);
                GL.BindTexture(TextureTarget.Texture2D, textureA);
                GL.Begin(BeginMode.Quads);
                {
                    GL.TexCoord2(0f, 1f);
                    GL.Vertex2(-1.0f, 1.0f);
                    GL.TexCoord2(0.0f, 0.0f);
                    GL.Vertex2(-1.0f, -1.0f);
                    GL.TexCoord2(1.0f, 0.0f);
                    GL.Vertex2(1.0f, -1.0f);
                    GL.TexCoord2(1.0f, 1.0f);
                    GL.Vertex2(1.0f, 1.0f);
                }
                GL.End();

                GL.Translate(2f, 0f, 0f);
                GL.BindTexture(TextureTarget.Texture2D, textureB);
                GL.Begin(BeginMode.Quads);
                {
                    GL.TexCoord2(0f, 0f);
                    GL.Vertex2(-1.0f, 1.0f);

                    GL.TexCoord2(0.0f, 1.0f);
                    GL.Vertex2(-1.0f, -1.0f);

                    GL.TexCoord2(1.0f, 1.0f);
                    GL.Vertex2(1.0f, -1.0f);

                    GL.TexCoord2(1.0f, 0.0f);
                    GL.Vertex2(1.0f, 1.0f);
                }
                GL.End();
                GL.Translate(0f, 0f, 0f);               
            }
            GL.PopMatrix();

            this.SwapBuffers();
        }

        #region public static void Main()

        /// <summary>
        /// Entry point of this example.
        /// </summary>
        [STAThread]
        public static void Main()
        {           
            using (MainClass example = new MainClass())
            {
                example.Title = "FrameBufferObjects";
                example.Run(1.0, 0.0);
            }
        }

        #endregion
    }
}
使用系统;
使用系统图;
使用系统、绘图、成像;
使用System.Drawing.Drawing2D;
使用System.Runtime.InteropServices;
使用OpenTK;
使用OpenTK.Graphics;
使用OpenTK.Graphics.OpenGL;
使用OpenTK.Platform;
使用OpenTK.Platform.X11;
使用OpenTK.Input;
使用ManOCL;
名称空间GlTest
{
公共类MainClass:GameWindow
{
公共类()
:base(800600,新图形模式(新颜色格式(0,0,0,0,32,0))
{
this.VSync=VSyncMode.Off;
}
公共单位外资银行;
公共单位结构;
公共信息网络;
公共int-textureAWidth=1024;
公共int-textureAHeight=1024;
公共宽度;
公共高度;
Random rand=新的Random();
受保护的覆盖无效加载(事件参数e)
{
总账启用(启用上限混合);
GL.ShadeModel(ShadingModel.Smooth);
总账BlendFunc(BlendingFactorSrc.SrcAlpha,BlendingFactorDest.oneminssrcalpha);
总账禁用(启用CAP.深度测试);
总账禁用(启用CAP.CullFace);
//GL.PolygonMode(MaterialFace.Back,PolygonMode.Line);
//创建彩色纹理
总纲:纤毛(1,外纤毛);
GL.BindTexture(TextureTarget.Texture2D,textureA);
GL.TexImage2D(TextureTarget.Texture2D,0,PixelInternalFormat.Rgba8,textureAWidth,textureAHeight,0,OpenTK.Graphics.OpenGL.PixelFormat.Rgba,PixelType.UnsignedByte,IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMapFilter,(int)TextureMapFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapS,(int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapT,(int)TextureWrapMode.ClampToBorder);
总目:GenTextures(1,外纹理B);
GL.BindTexture(TextureTarget.Texture2D,textureB);
位图bmp=新位图(“/Projects/MonaLisa/MonaLisa.bmp”);
Console.WriteLine(textureBWidth=bmp.Width);
Console.WriteLine(textureBHeight=bmp.Height);
//从位图中获取数据
System.Drawing.Imaging.BitmapData bmpBits=bmp.LockBits
(
新系统。绘图。矩形(0,0,TextureB宽度,TextureB高度),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppRgb
);
对于(int行=0;行<1;行++)
{
for(int col=0;col<32;col++)
{
控制台写入线
(
"{0}, {1}, {2}, {3}",
Marshal.ReadByte(bmpBits.Scan0,(行*textureBWidth+col)*4+0),
Marshal.ReadByte(bmpBits.Scan0,(行*textureBWidth+col)*4+1),
Marshal.ReadByte(bmpBits.Scan0,(行*textureBWidth+col)*4+2),
Marshal.ReadByte(bmpBits.Scan0,(行*textureBWidth+col)*4+3)
);
}
}
控制台写入线(bmpBits.Width);
控制台写入线(bmpBits.高度);
GL.TexImage2D
(
TextureTarget.Texture2D,
0,
PixelInternalFormat.Rgba,
纹理宽度,
身高,
0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
PixelType.UnsignedByte,
bmpBits.Scan0
);
//GL.TexImage2D(TextureTarget.Texture2D,0,PixelInternalFormat.Rgba8,textureBWidth,textureBHeight,0,OpenTK.Graphics.OpenGL.PixelFormat.Rgba,PixelType.UnsignedByte,IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMapFilter,(int)TextureMapFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapS,(int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapT,(int)TextureWrapMode.ClampToBorder);
//释放位图数据(我们不再需要它,因为它已经被传递到OpenGL驱动程序)
bmp.UnlockBits(bmpBits);
//创建FBO并附加纹理
GL.Ext.GenFramebuffers(1,输出fbo);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt,fbo);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt,FramebufferAttachment.ColorAttachment0Ext,TextureTarget.Texture2D,textureA,0);
#误差区域检验
开关(GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt))
{
案例FramebufferErrorCode.FrameBufferCompleteText:
{
WriteLine(“FBO:帧缓冲区已完成且可用于呈现。”);
打破
}
案例FramebufferErrorCode.FrameBufferIncompleAttachmentText:
{
Console.WriteLine(“FBO:一个或多个附件
        GL.TexImage2D
        (
            TextureTarget.Texture2D,
            0,
            PixelInternalFormat.Rgba,
            textureBWidth,
            textureBHeight,
            0,
            OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
            PixelType.UnsignedByte,
            bmpBits.Scan0
        );
OpenTK.Graphics.OpenGL.PixelFormat.Bgra;