• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c# - OpenGL ES 2.0/MonoTouch : Texture is colorized red

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:43:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我目前正在将一个立方体贴图加载到我的应用程序中,但它以红色调显示。 编辑: 使用 2D 纹理时也存在 channel 问题,似乎 channel 的顺序不正确。有什么方法可以使用 iOS 方法更改 channel 的顺序吗?

这是纹理加载的代码:

public TextureCube (Generic3DView device, UIImage right, UIImage left, UIImage top, UIImage bottom, UIImage front, UIImage back)
    : base(device)
{
    _Device = device;
    GL.GenTextures (1, ref _Handle);
    GL.BindTexture (TextureType, _Handle);
    LoadTexture(All.TextureCubeMapPositiveX, right);
    LoadTexture(All.TextureCubeMapNegativeX, left);
    LoadTexture(All.TextureCubeMapPositiveY, top);
    LoadTexture(All.TextureCubeMapNegativeY, bottom);   
    LoadTexture(All.TextureCubeMapPositiveZ, front);
    LoadTexture(All.TextureCubeMapNegativeZ, back);
        
    GL.TexParameter(All.TextureCubeMap, All.TextureMinFilter, (Int32)All.LinearMipmapLinear);
    GL.TexParameter(All.TextureCubeMap, All.TextureMagFilter, (Int32)All.Linear);
    GL.GenerateMipmap(All.TextureCubeMap);
}
            
private void LoadTexture(All usage, UIImage image) 
{
    GL.TexImage2D(usage, 0, (Int32)All.Rgba, (Int32)image.Size.Width,
                  (Int32)image.Size.Height, 0, All.Rgba, All.UnsignedByte, RequestImagePixelData(image));
}

protected CGBitmapContext CreateARGBBitmapContext (CGImage inImage)
{
    var pixelsWide = inImage.Width;
    var pixelsHigh = inImage.Height;
    var bitmapBytesPerRow = pixelsWide * 4;
    var bitmapByteCount = bitmapBytesPerRow * pixelsHigh;
    //Note implicit colorSpace.Dispose() 
    using (var colorSpace = CGColorSpace.CreateDeviceRGB()) {
         //Allocate the bitmap and create context
         var bitmapData = Marshal.AllocHGlobal (bitmapByteCount);
         if (bitmapData == IntPtr.Zero) {
             throw new Exception ("Memory not allocated.");
         }

         var context = new CGBitmapContext (bitmapData, pixelsWide, pixelsHigh, 8,
                            bitmapBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst);
         if (context == null) {
              throw new Exception ("Context not created");
         }
         return context;
     }
}

//Store pixel data as an ARGB Bitmap
protected IntPtr RequestImagePixelData (UIImage inImage)
{
     var imageSize = inImage.Size;
     CGBitmapContext ctxt = CreateARGBBitmapContext (inImage.CGImage);
     var rect = new RectangleF (0.0f, 0.0f, imageSize.Width, imageSize.Height);
     ctxt.DrawImage (rect, inImage.CGImage);
     var data = ctxt.Data;
     return data;
}

我认为 channel 是反转的,但也许有一种方法可以在没有一些自定义代码的情况下反转位图。

这是渲染的图像(忽略它前面的花哨模型):

Image rendered

以及预期的图像: Image expected

编辑:

GL_INVALID_OPERATION 问题已修复,但并未解决红色纹理的问题。

顶点着色器:

attribute vec3 position;            
uniform mat4 modelViewMatrix;           

varying mediump vec3 texture;           
        
void main()
{   
    texture = position.xyz;         
    gl_Position = modelViewMatrix * vec4(position.xyz, 1.0);    
}

片段着色器:

varying mediump vec3 texture;
uniform samplerCube cubeMap;        
        
void main()
{
    mediump vec3 cube = vec3(textureCube(cubeMap, texture));
    gl_FragColor = vec4(cube.xyz, 1.0);
}



Best Answer-推荐答案


问题是你的函数 CreateARGBBitmapContext 行

var context = new CGBitmapContext (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst);

如果你改变了

CGImageAlphaInfo.PremultipliedFirst 

CGImageAlphaInfo.PremultipliedLast 

这应该可以修复您的代码。

关于c# - OpenGL ES 2.0/MonoTouch : Texture is colorized red,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7986153/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap