Atlas editor performance question

Started by
5 comments, last by bolshas 16 years, 5 months ago
Hi, I'm developing an atlas editor and have this code for updating the certain areas:

        public void SetTerrainTexture(int x, int y, Bitmap image)
        {
            Graphics g = Graphics.FromImage(MainImage);
            g.DrawImage(image, new Point(x * 256, y * 256));
            g.Dispose();        
        }
        public void UpdateTerrainTexture()
        {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            MainImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Position = 0;
            MainTexture = TextureLoader.FromStream(device, stream);
        }
Each update takes about 1 sec. The question what would be the way to optimise this? Thanks!
Advertisement
Loading and saving a PNG even to memory will be the slow bit. Either save an uncompressed format like TGA or BMP, or find another way to do it.

If it wasn't managed code I'd suggest a combination of GetDIBits() and D3DXLoadSurfaceFromMemory(), but I'm not sure what the managed equivalents are.
Consider threading - it should be nice and simple with .NET as well [grin]

Your code will probably choke on two main parts - the encoding as PNG and the actual writing to disk. Both could be offloaded into a seperate thread and allow your main app to continue processing with little impact.

Then again, accessing D3DX across multiple threads isn't always a good idea (but ISTR MDX is thread-safe by default) but try it and see.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Yes, threading helped a bit. That's how i did it:

public void SetTerrainTexture(int x, int y, Bitmap image)        {            StreamWrite = new System.Threading.Thread(new System.Threading.ThreadStart(delegate { SetTerrainTextureThread(x, y, image); }));            StreamWrite.IsBackground = true;            StreamWrite.Start();        }



Anyways now it still takes time to lead the image on the texture but the app doesn't hang. Cool for now.

Another problem now:

I have heard that textures from stream should be ^2 in size. So here are my details:

map size: 16x16
single quad size in texture map: 256x256
texture map size: 4096x4096

The problem: the quads seem to overloop by about a half on top of each other.

All suggestions are welcome!
Quote:Original post by bolshas
The problem: the quads seem to overloop by about a half on top of each other.
I don't really follow your description here - any chance of a screenshot or diagram to demonstrate?

As for the 2N texture sizes - it's best to keep these dimensions if you can, but it's not set in stone. There are legitimate cases when it's better not to use 2N...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Ok, here's the function for creating the texture:
MainImage = new Bitmap(2048, 2048); public void SetTerrainTexture(int x, int y, Bitmap image)        {            StreamWrite = new System.Threading.Thread(new System.Threading.ThreadStart(delegate { SetTerrainTextureThread(x, y, image); }));            StreamWrite.IsBackground = true;            StreamWrite.Start();        }private void SetTerrainTextureThread(int x, int y, Bitmap image)        {            Graphics g = Graphics.FromImage(MainImage);            g.DrawImage(image, x * 256, y * 256);            g.Dispose();            Debug.WriteLine(image.Height);            Debug.WriteLine(MainImage.Height);                        System.IO.MemoryStream stream = new System.IO.MemoryStream();            MainImage.Save(stream, ImageFormat.Bmp);            stream.Position = 0;            MainTexture = TextureLoader.FromStream(device, stream);        }


I'm not sure how to upload my image here so I'll try to explain: I got a terrain of 9x9 vertices (8x8 quads) with a displayed grid among vertices.

I call SetTerrainTexture with x and y 0 and 0 respectively and image is 256x256.

So I should see the image covering exactly one quad since MainImage is 2048x2048 and 2048/256 = 8.

But I see the image overlapping the grid by about 10%.

I don't get it - all my sizes are ^2 even the terrain.

Any ideas are welcome :)
Just solved it - it was a problem with my Tu and Tv.

Another question - what is the best way of texturing large terrains?

For example if my single quad is 256x256 and my card supports 4096x4096 then it means that i can afford 16x16 texture. Should I create multiple subsets or meshes or is there another way?

[Edited by - bolshas on November 3, 2007 10:34:40 AM]

This topic is closed to new replies.

Advertisement