Texture changes:
* The GL texture id is now readonly. * Added Size property. * Added GetData() for reading data back from the GPU. * Added SetEmpty() for creating an empty texture of a given size.
This commit is contained in:
@@ -80,6 +80,8 @@ namespace OpenRA.FileFormats.Graphics
|
|||||||
void SetData(Bitmap bitmap);
|
void SetData(Bitmap bitmap);
|
||||||
void SetData(uint[,] colors);
|
void SetData(uint[,] colors);
|
||||||
void SetData(byte[] colors, int width, int height);
|
void SetData(byte[] colors, int width, int height);
|
||||||
|
byte[] GetData();
|
||||||
|
Size Size { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IFrameBuffer
|
public interface IFrameBuffer
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ namespace OpenRA.Renderer.Cg
|
|||||||
var texture = (Texture)t;
|
var texture = (Texture)t;
|
||||||
var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name);
|
var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name);
|
||||||
if (param != IntPtr.Zero && texture != null)
|
if (param != IntPtr.Zero && texture != null)
|
||||||
Tao.Cg.CgGl.cgGLSetupSampler(param, texture.texture);
|
Tao.Cg.CgGl.cgGLSetupSampler(param, texture.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetVec(string name, float x)
|
public void SetVec(string name, float x)
|
||||||
|
|||||||
@@ -115,8 +115,8 @@ namespace OpenRA.Renderer.Glsl
|
|||||||
|
|
||||||
foreach (var kv in textures)
|
foreach (var kv in textures)
|
||||||
{
|
{
|
||||||
Gl.glActiveTextureARB( Gl.GL_TEXTURE0_ARB + kv.Key );
|
Gl.glActiveTextureARB(Gl.GL_TEXTURE0_ARB + kv.Key);
|
||||||
Gl.glBindTexture( Gl.GL_TEXTURE_2D, ((Texture)kv.Value).texture );
|
Gl.glBindTexture(Gl.GL_TEXTURE_2D, ((Texture)kv.Value).ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* configure blend state */
|
/* configure blend state */
|
||||||
|
|||||||
@@ -79,6 +79,8 @@ namespace OpenRA.Renderer.Null
|
|||||||
public void SetData(Bitmap bitmap) { }
|
public void SetData(Bitmap bitmap) { }
|
||||||
public void SetData(uint[,] colors) { }
|
public void SetData(uint[,] colors) { }
|
||||||
public void SetData(byte[] colors, int width, int height) { }
|
public void SetData(byte[] colors, int width, int height) { }
|
||||||
|
public byte[] GetData() { return new byte[0]; }
|
||||||
|
public Size Size { get { return new Size(0, 0); } }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NullFrameBuffer : IFrameBuffer
|
public class NullFrameBuffer : IFrameBuffer
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ namespace OpenRA.Renderer.SdlCommon
|
|||||||
{
|
{
|
||||||
public class Texture : ITexture
|
public class Texture : ITexture
|
||||||
{
|
{
|
||||||
public int texture; /* temp: can be internal again once shaders are in shared code */
|
int texture;
|
||||||
|
public int ID { get { return texture; } }
|
||||||
|
|
||||||
|
Size size;
|
||||||
|
public Size Size { get { return size; } }
|
||||||
|
|
||||||
public Texture()
|
public Texture()
|
||||||
{
|
{
|
||||||
@@ -59,6 +63,7 @@ namespace OpenRA.Renderer.SdlCommon
|
|||||||
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
|
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
|
||||||
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
|
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
|
||||||
|
|
||||||
|
size = new Size(width, height);
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
fixed (byte* ptr = &colors[0])
|
fixed (byte* ptr = &colors[0])
|
||||||
@@ -81,6 +86,7 @@ namespace OpenRA.Renderer.SdlCommon
|
|||||||
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
|
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
|
||||||
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height));
|
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height));
|
||||||
|
|
||||||
|
size = new Size(width, height);
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
fixed (uint* ptr = &colors[0,0])
|
fixed (uint* ptr = &colors[0,0])
|
||||||
@@ -99,6 +105,7 @@ namespace OpenRA.Renderer.SdlCommon
|
|||||||
if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height))
|
if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height))
|
||||||
bitmap = new Bitmap(bitmap, bitmap.Size.NextPowerOf2());
|
bitmap = new Bitmap(bitmap, bitmap.Size.NextPowerOf2());
|
||||||
|
|
||||||
|
size = new Size(bitmap.Width, bitmap.Height);
|
||||||
var bits = bitmap.LockBits(bitmap.Bounds(),
|
var bits = bitmap.LockBits(bitmap.Bounds(),
|
||||||
ImageLockMode.ReadOnly,
|
ImageLockMode.ReadOnly,
|
||||||
PixelFormat.Format32bppArgb);
|
PixelFormat.Format32bppArgb);
|
||||||
@@ -109,5 +116,34 @@ namespace OpenRA.Renderer.SdlCommon
|
|||||||
ErrorHandler.CheckGlError();
|
ErrorHandler.CheckGlError();
|
||||||
bitmap.UnlockBits(bits);
|
bitmap.UnlockBits(bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] GetData()
|
||||||
|
{
|
||||||
|
var data = new byte[4*size.Width * size.Height];
|
||||||
|
|
||||||
|
ErrorHandler.CheckGlError();
|
||||||
|
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
fixed (byte *ptr = &data[0])
|
||||||
|
{
|
||||||
|
IntPtr intPtr = new IntPtr((void*)ptr);
|
||||||
|
Gl.glGetTexImage(Gl.GL_TEXTURE_2D, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetEmpty(int width, int height)
|
||||||
|
{
|
||||||
|
if (!Exts.IsPowerOf2(width) || !Exts.IsPowerOf2(height))
|
||||||
|
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
|
||||||
|
|
||||||
|
size = new Size(width, height);
|
||||||
|
PrepareTexture();
|
||||||
|
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, width, height,
|
||||||
|
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, null);
|
||||||
|
ErrorHandler.CheckGlError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user