Jump to content
  • Advertisement
Sign in to follow this  
Telastyn

[.net] [C#/MDX] Programatic Texture/Image Generation

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on making a little map generator. Unfortunately, I've run across something that just seems "off". I imagine it's something I just don't get; that MSDN documentation doesn't make clear... Anyways, this sample code, I'd think would make two 4 pixel greyscale bitmaps:
50  100
100 200
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Csharp_texturetest2 {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 winobj = new Form1();
            
            PresentParameters pp = new PresentParameters();
            pp.EnableAutoDepthStencil = false;
            pp.BackBufferCount = 1;
            pp.DeviceWindow = winobj;
            pp.PresentationInterval = PresentInterval.Immediate;
            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;
            pp.BackBufferFormat = Format.A8R8G8B8;

            Device d3ddev = new Device(0, DeviceType.Hardware, winobj, CreateFlags.SoftwareVertexProcessing, pp);

            int[] arrdim = new int[2] { 2, 2 };
            byte[,] buf;
            Texture test = new Texture(d3ddev, 2, 2, 1, Usage.SoftwareProcessing, Format.L8, Pool.Managed);
            buf = (byte[,])test.LockRectangle(typeof(byte), 0, LockFlags.None, arrdim);
            buf[0, 0] = 50;
            buf[1, 0] = 100;
            buf[0, 1] = 100;
            buf[1, 1] = 200;
            test.UnlockRectangle(0);
            TextureLoader.Save("test.bmp", ImageFileFormat.Bmp, test);

            Texture test2 = new Texture(d3ddev, 2, 2, 1, Usage.SoftwareProcessing, Format.L8, Pool.Managed);
            byte[] buf2;
            buf2 = (byte[])test2.LockRectangle(typeof(byte), 0, LockFlags.None, 4);
            buf2[0] = 50;
            buf2[1] = 100;
            buf2[2] = 100;
            buf2[3] = 200;
            test2.UnlockRectangle(0);
            TextureLoader.Save("Test2.bmp", ImageFileFormat.Bmp, test2);
            Application.Run(winobj);
        }
    }
}

Unfortunately, neither does. They both produce identical 4 pixel bitmaps:
50  100
0   0
Once again, I imagine it's something I'm overlooking...

Share this post


Link to post
Share on other sites
Advertisement
More interesting findings:

Texture test4 = new Texture(d3ddev, 2, 2, 1, Usage.SoftwareProcessing, Format.L8, Pool.Managed);

UInt16[] buf4;
buf4 = (UInt16[])test4.LockRectangle(typeof(UInt16), 0, LockFlags.None, 4);
buf4[0] = 255;
buf4[1] = 0;
buf4[2] = 65280;
buf4[3] = 0;
test4.UnlockRectangle(0);
TextureLoader.Save("Test4.bmp", ImageFileFormat.Bmp, test4);




Yields

255 0
0 255


65535 for 0 and 2 will fill the bitmap white, and changes to 1 and 3 make no noticable changes to the bitmap...

Share this post


Link to post
Share on other sites
Try using the form of lock that returns the pitch as an out parameter. The rows of the returned data may be longer than what you specified as your rectangle. It is a pain, but directx does not guarantee that the returned data is not wider than you expect.

Share this post


Link to post
Share on other sites
Icky. I'd think the 2d arrays would be handled gracefully even if the pitch extended beyond the index....

Anyways, this edit [and keeping the array assignments] fixes things for anyone searching about this problem:

int[] arrdim = new int[2] { 2, 2 };
int p;
byte[,] buf;
Texture test = new Texture(d3ddev, 2, 2, 1, Usage.SoftwareProcessing, Format.L8, Pool.Managed);
//buf = (byte[,])test.LockRectangle(typeof(byte),0,LockFlags.None,arrdim);
buf = (byte[,])test.LockRectangle(typeof(byte), 0, LockFlags.None, out p, arrdim);
while (p != arrdim[1]) {
arrdim[1] = p;
test.UnlockRectangle(0);
buf = (byte[,])test.LockRectangle(typeof(byte), 0, LockFlags.None, out p, arrdim);
}



Thanks for the pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!