Clone texture.

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
mcaden
Goblin
Posts: 206
Joined: Wed Jul 02, 2008 9:29 am
Location: Texas, USA
Contact:

Clone texture.

Post by mcaden »

I'm manually editing textures and I need to create a history. I figured I'd simply use Image to load the textureptr into an image - keeping an array of images. However, Image.loadDynamicImage which is the only function I know of to copy texturptr to image is actually simply copying a pointer....

So, if I save a texture to my array, change the texture, add the new texture to the array, change the texture, add it...what I actually end up with is the last texture 3 times.

I need a clone the pixel data. I was thinking using Image.loadRawData might work, but when I tried to get a stream from textureptr, it crashed (I tried several days ago, don't have the code I used any more for now). I was just wondering if anybody else could point me in the right direction.
Katalyst Studios indie game dev project
Squamster - open source mesh viewer/painter
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 9

Re: Clone texture.

Post by nullsquared »

Check the API docs on Texture and HardwarePixelBuffer.

You can probably get away with the easy copyToTexture()
User avatar
mcaden
Goblin
Posts: 206
Joined: Wed Jul 02, 2008 9:29 am
Location: Texas, USA
Contact:

Re: Clone texture.

Post by mcaden »

copyToTexture() was the first thing that came to mind. Problem is I'm using Mogre, which has a bad time manually destroying textures. I want to keep a history of the last 10-20 edits to a texture. I may end up with hundreds of edits, so not destroying old textures would leave hundreds of textures within memory. I need to be able to hold onto a copy of my texture at each edit in an array, hold onto it until my array hits a certain number, then destroy it and release it's memory. Basically it's a stack that auto-destroys every texture past the history limit.
Katalyst Studios indie game dev project
Squamster - open source mesh viewer/painter
User avatar
mcaden
Goblin
Posts: 206
Joined: Wed Jul 02, 2008 9:29 am
Location: Texas, USA
Contact:

Re: Clone texture.

Post by mcaden »

So, I found it's working if I just save the pixelbox in an array.

However, I'm having problems with blitfrommemory now...

Code: Select all

        private void undo()
        {
            if (texList.Items.Count > 0 && undoList.Count > 0)
            {
                LogManager.Singleton.LogMessage("-= Undoing Action =-");
                unsafe
                {
                    TexturePtr currentTex = TextureManager.Singleton.GetByName(meshPainter.getCurrentTextureName());
                    byte[] bytes = new byte[currentTex.Width * currentTex.Height * 4];
                    fixed (byte* ptr = &bytes[0])
                    {
                        LogManager.Singleton.LogMessage("Saving Redo Snapshot...");
                        PixelBox textureSnapshot = new PixelBox(currentTex.Width, currentTex.Height, 1, currentTex.Format, (IntPtr)ptr);
                        currentTex.GetBuffer().BlitToMemory(textureSnapshot);
                        redoList.Insert(0, textureSnapshot);

                        LogManager.Singleton.LogMessage("Blitting Undo Snapshot...");
                        currentTex.GetBuffer().BlitFromMemory(undoList[0]);
                        LogManager.Singleton.LogMessage("Removing snapshot from list...");
                        undoList.RemoveAt(0);
                        //undoList.TrimExcess();
                    }
                }
                LogManager.Singleton.LogMessage("-= Undo List Updated, Action Count is: " + undoList.Count.ToString() + " =-");
            }
        }
This works, for a random amount of times between 1-10...then it'll crash with an access violation exception. It's always:

Code: Select all

currentTex.GetBuffer().BlitFromMemory(undoList[0]);
Katalyst Studios indie game dev project
Squamster - open source mesh viewer/painter
o0hamish0o
Gnoblar
Posts: 7
Joined: Thu Jan 08, 2009 6:45 pm
Location: Houston, Texas
Contact:

Re: Clone texture.

Post by o0hamish0o »

Hi,

I didn't try, but I'm wondering if you need to do this type of thing?
mBuffer.Lock(HardwareBuffer.LockOptions.HBL_NORMAL);

PixelBox pBox = mBuffer.CurrentLock;
Marshal.Copy(frame, 0, pBox.data, frame.Length);

mBuffer.Unlock();

from this thread:
http://www.ogre3d.org/addonforums/viewt ... 6f4e2718c6

-o0hamish0o
User avatar
mcaden
Goblin
Posts: 206
Joined: Wed Jul 02, 2008 9:29 am
Location: Texas, USA
Contact:

Re: Clone texture.

Post by mcaden »

That did the trick perfectly.
Katalyst Studios indie game dev project
Squamster - open source mesh viewer/painter
Post Reply