C# OpenGL ES 2.0/MonoTouch:纹理为红色
我目前正在将一个立方体贴图加载到我的应用程序中,但它显示为红色。 编辑:使用2D纹理时也存在通道问题,通道顺序似乎不正确。是否有任何方法可以使用iOS方法更改频道的顺序 这是纹理加载的代码:C# OpenGL ES 2.0/MonoTouch:纹理为红色,c#,iphone,ios,opengl-es,xamarin.ios,C#,Iphone,Ios,Opengl Es,Xamarin.ios,我目前正在将一个立方体贴图加载到我的应用程序中,但它显示为红色。 编辑:使用2D纹理时也存在通道问题,通道顺序似乎不正确。是否有任何方法可以使用iOS方法更改频道的顺序 这是纹理加载的代码: public TextureCube (Generic3DView device, UIImage right, UIImage left, UIImage top, UIImage bottom, UIImage front, UIImage back) : base(device) {
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;
}
我认为通道是反转的,但是也许有一种方法可以在没有自定义代码的情况下反转位图
这是渲染的图像(忽略其前面的奇特模型):
以及预期的图像:
编辑:
GL_无效_操作问题已修复,但无法解决红色纹理的问题
顶点着色器:
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);
}
在使用glUniform之前,您是否使用了该程序(与glUseProgram一起使用)?因为它不工作,在这种情况下会产生错误
您还可以检查(最后)中GL错误的原因。经过一些测试后,我决定使用“XnaTouch”中的代码加载纹理,这解决了红色纹理的问题
当然,这还没有结束,因为加载png图像时没有alpha通道。因为这是不可接受的,并且需要花费很多时间,所以我决定编写一个dds加载程序(基于中的代码)。在
TexImage2D
步骤中,我发现您对这两种设置都使用了RGBA。根据原始图像的蓝色和生成图像的红色来判断,我建议将其中一个图像替换为BGRA。问题在于函数CreateARGBBitmapContext行
var context = new CGBitmapContext (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst);
如果你改变
CGImageAlphaInfo.PremultipliedFirst
到
这应该可以修复你的代码。你能发布一张你得到的和你期望得到的图像吗?什么代码在绘制图片?添加了着色器,VertexBuffer代码可以完美地工作,没有任何问题(当然对纹理没有影响)。结果我们这里有两个bug,但是你带我去解决第一个问题(GL_无效操作)。我在我的绘图代码中添加了另一个“GL.GetUniformLocation”,并注意到位置已更改(不要问我为什么,我在着色器完全链接时获得了位置)。红色纹理的问题仍然存在。我会在可能的情况下测试它。当它起作用的时候,我会给你答案的奖励。
CGImageAlphaInfo.PremultipliedLast