Medals:  Joined: 8/20/2011(UTC) Posts: 1,421 Location: Hannover
Thanks: 18 times Was thanked: 97 time(s) in 92 post(s)
|
Hi there. I am not sure what you mean by "update a texture every frame" and "FillTextureData should be split into two methods as it's both creating the actual texture and then setting the texture data.". Why would this be useful for you? If I understand you correctly you just want to upload new data to an existing texture handle. Since the source code is all there you can fix this by just changing a single line of code (I admit we did not test this much and this is obviously wrong, I have already fixed it for the next release): From OpenTKTexture.cs: Code:
// Create the texture in OpenGL and use the textureHandle from now on.
GL.GenTextures(1, out textureHandle);
Is now changed to: Code:
// Create the texture in OpenGL and use the textureHandle from now on.
// Note: If the texture was already created before and not disposed, just
// use the existing handle, this allows overwriting the texture data.
if (textureHandle == MathHelper.InvalidIndex)
{
GL.GenTextures(1, out textureHandle);
}
And here is a little new unit test to show how to update texture data (runs with about 3000fps in my android simulator under windows I am currently testing out, probably more with OpenTK): Code:
#region UpdateTextureDynamically
/// <summary>
/// Simple test that shows how to update rgb texture data dynamically.
/// </summary>
[Test]
public static void UpdateTextureDynamically()
{
// Define a 8x8 texture with some colors.
Color[] textureColors = new[]
{
Color.Red, Color.Green, Color.Yellow, Color.Blue,
Color.Black, Color.White, Color.Brown, Color.Orange,
Color.Green, Color.Yellow, Color.Blue, Color.Black,
Color.White, Color.Brown, Color.Orange, Color.Red,
Color.Yellow, Color.Blue, Color.Black, Color.White,
Color.Brown, Color.Orange, Color.Red, Color.Green,
Color.Blue, Color.Black, Color.White, Color.Brown,
Color.Orange, Color.Red, Color.Green, Color.Yellow,
Color.Black, Color.White, Color.Brown, Color.Orange,
Color.Red, Color.Green, Color.Yellow, Color.Blue,
Color.White, Color.Brown, Color.Orange, Color.Red,
Color.Green, Color.Yellow, Color.Blue, Color.Black,
Color.Brown, Color.Orange, Color.Red, Color.Green,
Color.Yellow, Color.Blue, Color.Black, Color.White,
Color.Orange, Color.Red, Color.Green, Color.Yellow,
Color.Blue, Color.Black, Color.White, Color.Brown,
};
// Convert the colors to rgb data and create texture out of it plus
// pass it into a material for rendering.
Texture dynamicTexture = Texture.Create(
Texture.ConvertColorToRgb(textureColors), new Size(8, 8),
BlendMode.Opaque, false);
Material2D dynamicMaterial = new Material2D(dynamicTexture);
// Add an offset to the textureColors array to keep changing texture data
int textureColorOffset = 0;
Color[] rotatedTextureColors = new Color[textureColors.Length];
Application.Start(delegate
{
// Update the texture with new data each tick
for (int num = 0; num < rotatedTextureColors.Length; num++)
{
rotatedTextureColors[num] =
textureColors[(num + textureColorOffset) % textureColors.Length];
}
textureColorOffset++;
dynamicTexture.UpdateTextureData(
Texture.ConvertColorToRgb(rotatedTextureColors));
// And show the material with the dynamic texture normally
dynamicMaterial.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
});
}
#endregion
It uses the new helper method UpdateTextureData in Texture.cs, which just uses the FillTextureData method like before, but adds some checks: Code:
#region UpdateTextureData
/// <summary>
/// Update the texture data with a given RGB or RGBA byte array. Unlike
/// Create and FillTextureData this method is called after the texture has
/// been created and should only be used to update it without changing the
/// width, height or texture format.
/// </summary>
public void UpdateTextureData(byte[] newImageData)
{
// Currently only uncompressed rgb or rgba textures can be updated.
if (extensionUsed !=
(HasAlpha
? "rgba"
: "rgb"))
{
throw new NotSupportedException(
"Sorry, the extension '" + extensionUsed + "' for the loaded " +
"texture '" + Name + "' currently is not supported in this method " +
"to update the texture data. Please create or load the texture " +
"from rgb or rgba data!");
}
// Make sure the texture size fits the data.
int numberOfPixels =
newImageData.Length /
(HasAlpha
? 4
: 3);
int expectedPixels = (int)Size.Width * (int)Size.Height;
if (expectedPixels != numberOfPixels)
{
throw new ArgumentException(
"Sorry, unable to update texture data. Only '" + numberOfPixels +
"' pixels were submitted, but the texture size is '" + Size +
"', expecting '" + expectedPixels + "' pixels!");
}
// And finally update the texture data like for the first upload.
FillTextureData((int)Size.Width, (int)Size.Height, newImageData,
extensionUsed);
}
#endregion
If I missed something please be more clear on what you want to accomplish or maybe try to fix it yourself. The graphics source code is all there, all of the changes I made above can be made by anyone else too. Patching something is quick and easy for me and saves time. Thanks for the suggestion though, hopefully the fix is useful for you.
|
 1 user thanked Benjamin for this useful post.
|
|