While testing a build of my game on an XP machine some weeks back, I found that some of the textures, even those not using alpha blending, were being rendered as white rectangles on the VM. On the physical Windows 7 machine, they displayed perfectly.
From doing various google searches, I am led to believe that white textures are pretty much an error state and therefore I'm doing something wrong. (Which I then further clarified with glGetError)
The game used to work on the XP VM so I'm at a loss what I did to cause it - after I originally got textures working way back when I haven't really touched the code as it hasn't been nessecary and as I'm still pretty much a beginner at OpenGL I don't want to break things by mistake.
Part of the problem is that I'm not even sure if I really have broken the game, or if it's the VM itself - while testing I noted that if I changed the color depth of the VM from 32bit to lower, then the game window always displayed a solid red color, nothing else. Of course, that could still be a fault with my code ;)
Here's examples of what the game looks like running on Windows 7:
[attachment=11290:good1.png][attachment=11291:good2.png]
And this is what happens on the XP VM:
[attachment=11288:bad1.png][attachment=11289:bad2.png]
What is truly frustrating about this whole issue is it only affects some graphics, and it's always the same ones.
I added a call to glGetError after calling glTexImage2D and this is returning InvalidValue. But that doesn't make sense to me - why it would it return fine on one machine and not another, and why wouldn't it affect all graphics instead of just some? This is the output of a debug log I added:
15/09/2012 20:04:45: Debug: Assigning texture 1 (splash) [Hint: Linear, Flags: None]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Activated Scene:
15/09/2012 20:04:45: Debug: Assigning texture 2 (gamebackground) [Hint: Nearest, Flags: None]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 3 (bonusbackground) [Hint: Nearest, Flags: None]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 4 (exitbackground) [Hint: Nearest, Flags: None]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 5 (statusbanner) [Hint: Nearest, Flags: None]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 6 (maintitle) [Hint: Linear, Flags: None]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 7 (optionstitle) [Hint: Linear, Flags: Alpha]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 8 (howtotitle) [Hint: Linear, Flags: Alpha]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 9 (creditstitle) [Hint: Linear, Flags: Alpha]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 10 (pausedtitle) [Hint: Linear, Flags: Alpha]
15/09/2012 20:04:45: Debug: InvalidValue
15/09/2012 20:04:45: Debug: Assigning texture 11 (debug) [Hint: Linear, Flags: Alpha]
15/09/2012 20:04:45: Debug: NoError
15/09/2012 20:04:45: Debug: Assigning texture 12 (rock-72_0) [Hint: Nearest, Flags: Alpha]
15/09/2012 20:04:45: Debug: NoError
15/09/2012 20:04:45: Debug: Assigning texture 13 (rock-32s_0) [Hint: Nearest, Flags: Alpha]
15/09/2012 20:04:45: Debug: NoError
15/09/2012 20:04:45: Debug: Assigning texture 14 (rock-36s_0) [Hint: Nearest, Flags: Alpha]
15/09/2012 20:04:45: Debug: NoError
15/09/2012 20:04:45: Debug: Assigning texture 15 (uni564-12s_0) [Hint: Nearest, Flags: Alpha]
15/09/2012 20:04:45: Debug: NoError
15/09/2012 20:04:45: Debug: Assigning texture 16 (JewelRush) [Hint: Nearest, Flags: Alpha]
15/09/2012 20:04:45: Debug: NoError
Clearly it's not liking a lot of graphics independent of settings.
The code is scatted about in different classes, hopefully I've gathered up all the relevant bits here. As mentioned I'm still a noob when it comes to OpenGL, so I'm currently doing all texture rendering via glDrawArrays.
Initializing texture support:
[source lang="csharp"]GL.Disable(EnableCap.CullFace);
GL.Enable(EnableCap.Texture2D);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
[/source]
Binding a System.Drawing.Bitmap to OpenGL:
[source lang="c#"] public override void Rebind()
{
BitmapData data;
Bitmap bitmap;
int magnificationFilter;
int minificationFilter;
bitmap = (Bitmap)this.Image;
if (this.TextureId == 0)
{
GL.GenTextures(1, out _textureId);
Log.WriteLog(LogLevel.Debug, "Assigning texture {0} ({1}) [Hint: {2}, Flags: {3}]", this.TextureId, this.Name, this.Hint, this.Flags);
}
OpenGL.BindTexture(this.TextureId);
data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
switch (this.Hint)
{
case TextureHintMode.Nearest:
magnificationFilter = (int)TextureMagFilter.Nearest;
minificationFilter = (int)TextureMinFilter.Nearest;
break;
default:
magnificationFilter = (int)TextureMagFilter.Linear;
minificationFilter = (int)TextureMinFilter.Linear;
break;
}
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, minificationFilter);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, magnificationFilter);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
Log.WriteLog(LogLevel.Debug, GL.GetError().ToString());
bitmap.UnlockBits(data);
}[/source]
Although not shown in the above code, I experimented by creating a new bitmap object with an explicit RGBA pixel type and then drew the original image onto this prior to calling LockBits etc, this had no effect.
Part of my SpriteBatch class that handles drawing with as few calls to glBindTexture as possible:
[source lang="csharp"]public override void Draw()
{
if (this.Size != 0)
{
if (_requiresBlend)
GL.Enable(EnableCap.Blend);
OpenGL.BindTexture(_textureId);
this.SetupPointers();
GL.DrawArrays(BeginMode.Triangles, 0, this.Size);
this.Size = 0;
if (!_requiresBlend)
{
GL.Disable(EnableCap.Blend);
_requiresBlend = false;
}
}
}
private void SetupPointers()
{
GL.EnableClientState(ArrayCap.ColorArray);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.VertexPointer(VertexDimensions, VertexPointerType.Double, 0, _vertexPositions);
GL.ColorPointer<BrColor>(ColorDimensions, ColorPointerType.Float, 0, _vertexColors);
GL.TexCoordPointer<Point>(UVDimensions, TexCoordPointerType.Float, 0, _vertexUVs);
}[/source]
Apologies for the somewhat rambling post, if anyone has any suggestions as to where I'm going wrong I'd be grateful.
Thanks;
Richard Moss