2011-11-02 106 views
2

我目前正在将一个立方体贴图加载到我的应用程序中,但它显示为红色。 编辑:使用2D纹理时,通道问题也存在,看起来通道的顺序不正确。有什么方法可以使用iOS方法更改频道的顺序?OpenGL ES 2.0/MonoTouch:纹理是彩色的红色

这对纹理加载代码:

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; 
} 

我认为信道被反转,但也许有一种方法反转位图没有一些自定义代码。

这是被描绘的图像(忽略在它前面的花式模型):

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); 
} 
+0

你可以发布你正在得到的图片和你期望得到的图片吗? – NickLH

+0

我上传了两张图片。 –

+0

什么代码正在绘制图片? – Dani

回答

2

的问题是你的函数CreateARGBBitmapContext行

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

如果更改

CGImageAlphaInfo.PremultipliedFirst 

CGImageAlphaInfo.PremultipliedLast 

应该解决您的代码。

+0

我会在可能的时候测试它。当它工作时,我会给你答案的奖励。 –

0

你使用计划(glUseProgram)使用glUniform过吗?因为在这种情况下它不起作用并会产生错误。

您还可以检查glUniform man page(最后)中导致GL错误的原因。

+0

事实证明,我们在这里有两个错误,但你带我去解决第一个问题(GL_INVALID_OPERATION)。我在绘图代码中添加了另一个'GL.GetUniformLocation',并且注意到位置已经改变(不要问我为什么,当着色器完全链接时我得到位置)。红色纹理的问题仍然存在。 –

1

经过一番测试,我决定使用“XnaTouch”的代码来加载纹理,这解决了红色纹理的问题。

当然,这还没有结束,因为加载png图像时一直没有alpha通道。因为这是不可接受的,并且消耗了很多时间,我决定编写一个dds加载器(基于http://humus.name/的代码)。

0

我看到您在TexImage2D步骤中正在使用RGBA进行这两种设置。根据你的原始图像以及你的最终图像如何红色来判断,我建议将其中一个换成BGRA。