2014-04-01 5 views
0

OpenTK를 통해 OpenGL을 사용하여 PNG를로드하려고하는데 이미지가 매우 섬세하고 색상이 잘못되었습니다.opentk에 png로드가 예상대로 작동하지 않습니다.

코드 :

private void Create(SurfaceFormat format) 
     { 
      textureHandle = (uint)GL.GenTexture(); 
      //bind texture 
      GL.BindTexture(TextureTarget.Texture2D, textureHandle); 
      Log.Error("Bound Texture: " + GL.GetError()); 
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); 
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); 
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)format.WrapMode); 
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)format.WrapMode); 
      Log.Error("Created Texture Parameters: " + GL.GetError()); 
      GL.TexImage2D(TextureTarget.Texture2D, 0, format.InternalFormat, Width, Height, 0, format.PixelFormat, format.SourceType, format.Pixels); 
      Log.Error("Created Image: " + GL.GetError()); 
      //unbind texture 
      GL.BindTexture(TextureTarget.Texture2D, 0); 

      //create fbo 
      fboHandle = (uint)GL.GenFramebuffer(); 
      GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboHandle); 
      GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureHandle, 0); 
      GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
      Log.Error("Created Framebuffer: " + GL.GetError()); 
     } 
     public void CreateFromPNG(string filePath) 
     { 
      //check if the file exists 
      if (System.IO.File.Exists(filePath)) 
      { 
       //make a bitmap out of the file on the disk 
       System.Drawing.Bitmap textureBitmap = new System.Drawing.Bitmap(filePath); 
       //get the data out of the bitmap 
       System.Drawing.Imaging.BitmapData textureData = 
       textureBitmap.LockBits(
         new System.Drawing.Rectangle(0, 0, textureBitmap.Width, textureBitmap.Height), 
         System.Drawing.Imaging.ImageLockMode.ReadOnly, 
         System.Drawing.Imaging.PixelFormat.Format32bppArgb 
        ); 
       if(textureBitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) 
       { 
        Log.Error("PNG Pixel format not supported ("+ filePath +") -> " + textureBitmap.PixelFormat.ToString()); 
        return; 
       } 
       SurfaceFormat format = new SurfaceFormat(); 
       format.Pixels = textureData.Scan0; 
       format.SourceType = PixelType.Byte; 
       Create(format); 
       //free the bitmap data (we dont need it anymore because it has been passed to the OpenGL driver 
       textureBitmap.UnlockBits(textureData); 

      } 
     } 

은 내가 잘못 여기서 뭐하는 거지?

편집 : 그것은를 고정 : 나는 EDIT2

format.SourceType = PixelType.UnsignedByte; 
format.PixelFormat = PixelFormat.Bgra; 

를 통해 색상을 고정. 블렌딩이 제대로 활성화되지 않은 경우 투명도가있는 png가 왜곡됩니다.

GL.Enable(EnableCap.Blend); 
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); 

그러나 왜곡은 여전히 ​​

+0

빠른 수정이 필요한 경우 png, gdi 형식 argb, opengl 형식 bgra ... 대신 jpg ...를 사용하십시오. –

답변

0

왜곡이 in this topic을 설명 어떤 관련이있을 수 있습니다에 대한 것입니다.

System.Drawing.Imaging.PixelFormat.Format32bppPArgb을 사용해보십시오.