コード例 #1
0
        public Texture GetTexture(Device device)
        {
            if (texture != null)
            {
                unsafe
                {
                    if ((int)texture.UnmanagedComPointer == 0x0)
                    {
                        goto createTexture;
                    }
                }
                return(texture);
            }

createTexture:

            Format dFormat = Format.A4R4G4B4;

            int line = width * 2;

            switch (format)
            {
            case ImageFormat.FORMAT_8888: dFormat = Format.A8R8G8B8; line = width * 4; break;

            case ImageFormat.FORMAT_565: dFormat = Format.R5G6B5; break;

            case ImageFormat.FORMAT_BIN: dFormat = Format.Unknown; line = width / 128; break;
            }

            if (device.DeviceCaps.TextureCaps.SupportsPower2)
            {
                texture = new Texture(device, Math.Min(round(width), device.DeviceCaps.MaxTextureWidth), Math.Min(round(height), device.DeviceCaps.MaxTextureHeight), 0, Usage.Dynamic, dFormat, Pool.Default);
            }
            else
            {
                texture = new Texture(device, width, height, 0, Usage.Dynamic | Usage.AutoGenerateMipMap, dFormat, Pool.Default);
            }

            if (format == ImageFormat.FORMAT_BIN)
            {
                return(texture);                                 // TODO
            }
            int pitch;

            using (Microsoft.DirectX.GraphicsStream g = texture.LockRectangle(0, LockFlags.Discard, out pitch))
            {
                byte[] buf = GetBitmapBytes();
                for (int i = 0; i < height; i++)
                {
                    int offset = pitch * i;
                    g.Position = offset;
                    g.Write(buf, line * i, line);
                }
            }
            texture.UnlockRectangle(0);

            //texture = new Texture(device, GetBitmap(), Usage.Dynamic, Pool.Default);

            return(texture);
        }
コード例 #2
0
ファイル: myFrame.cs プロジェクト: braindef/c-sharp
        public void OnCreateVertexBuffer(object sender, System.EventArgs e)
        {
            Microsoft.DirectX.Direct3D.VertexBuffer vb = (Microsoft.DirectX.Direct3D.VertexBuffer)sender;
            Microsoft.DirectX.GraphicsStream        gs = vb.Lock(0, 0, 0);
            Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[] verts = new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[6];

            verts[0].X     = 50;
            verts[0].Y     = 50;
            verts[0].Z     = 0.5f;
            verts[0].Rhw   = 1;
            verts[0].Color = System.Drawing.Color.Red.ToArgb();

            verts[1].X   = 250;
            verts[1].Y   = 50;
            verts[1].Z   = 0.5f;
            verts[1].Rhw = 1;
            //verts[1].Color = System.Drawing.Color.Lime.ToArgb();

            verts[2].X   = 250;
            verts[2].Y   = 50.1f;
            verts[2].Z   = 0.5f;
            verts[2].Rhw = 1;
            //verts[2].Color = System.Drawing.Color.Red.ToArgb();

            verts[3].X   = 250;
            verts[3].Y   = 50;
            verts[3].Z   = 0.5f;
            verts[3].Rhw = 1;
            //verts[3].Color = System.Drawing.Color.Red.ToArgb();

            verts[4].X   = 250;
            verts[4].Y   = 250;
            verts[4].Z   = 0.5f;
            verts[4].Rhw = 1;
            //verts[4].Color = System.Drawing.Color.Red.ToArgb();

            verts[5].X   = 50;
            verts[5].Y   = 250;
            verts[5].Z   = 0.5f;
            verts[5].Rhw = 1;
            //verts[5].Color = System.Drawing.Color.Red.ToArgb();

            gs.Write(verts);
            vb.Unlock();
        }
コード例 #3
0
        public void SetAxisLine(Device d3dDevice)
        {
            m_CoordinateAxis = new VertexBuffer(typeof(CustomVertex.PositionColored), 6,
                                                d3dDevice, 0, CustomVertex.PositionColored.Format, Pool.Managed);

            CustomVertex.PositionColored[] posColoredVerts = new CustomVertex.PositionColored[6];

            posColoredVerts[0].Position = new Microsoft.DirectX.Vector3(0, 0, 0);
            posColoredVerts[0].Color    = System.Drawing.Color.Red.ToArgb();
            posColoredVerts[1].Position = new Microsoft.DirectX.Vector3(10, 0, 0);
            posColoredVerts[1].Color    = System.Drawing.Color.Red.ToArgb();
            posColoredVerts[2].Position = new Microsoft.DirectX.Vector3(0, 0, 0);
            posColoredVerts[2].Color    = System.Drawing.Color.Green.ToArgb();
            posColoredVerts[3].Position = new Microsoft.DirectX.Vector3(0, 10, 0);
            posColoredVerts[3].Color    = System.Drawing.Color.Green.ToArgb();
            posColoredVerts[4].Position = new Microsoft.DirectX.Vector3(0, 0, 10);
            posColoredVerts[4].Color    = System.Drawing.Color.Blue.ToArgb();
            posColoredVerts[5].Position = new Microsoft.DirectX.Vector3(0, 0, 0);
            posColoredVerts[5].Color    = System.Drawing.Color.Blue.ToArgb();

            Microsoft.DirectX.GraphicsStream gstm = m_CoordinateAxis.Lock(0, 0, LockFlags.None);
            gstm.Write(posColoredVerts);
            m_CoordinateAxis.Unlock();
        }
コード例 #4
0
        unsafe private void button1_Click(object sender, EventArgs e)
        {
            //DirectX初期化
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed   = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            Device device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
            //アルファロード
            Bitmap alphaImage = null;

            System.Drawing.Imaging.BitmapData alphaBitmapData = null;
            int ah = 0;
            int aw = 0;

            if (textBox1.Text != "")
            {
                alphaImage = new Bitmap(textBox1.Text);
                ah         = alphaImage.Height;
                aw         = alphaImage.Width;
                Rectangle rect = new Rectangle(0, 0, aw, ah);
                alphaBitmapData = new System.Drawing.Imaging.BitmapData();
                alphaImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb, alphaBitmapData);
            }
            //イメージ一枚づつ処理
            foreach (string s in listBox1.Items)
            {
                textBox2.Text = "状態:処理中...";
                //イメージロード
                Bitmap    image = new Bitmap(s);
                int       h     = image.Height;
                int       w     = image.Width;
                Rectangle rect  = new Rectangle(0, 0, w, h);
                System.Drawing.Imaging.BitmapData bitmapData = new System.Drawing.Imaging.BitmapData();
                image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bitmapData);
                uint *src;
                uint *dst;
                if ((alphaImage != null) && (ah == h) && (aw == w))
                {
                    //アルファ置換
                    src = (uint *)alphaBitmapData.Scan0;
                    dst = (uint *)bitmapData.Scan0;
                    for (uint y = 0; y < h; ++y)
                    {
                        for (uint x = 0; x < w; ++x)
                        {
                            dst[x] = (dst[x] & 0xffffff) | ((src[x] & 0xff) << 24);
                        }
                        src += alphaBitmapData.Stride / 4;
                        dst += bitmapData.Stride / 4;
                    }
                }
                //テクスチャ化
                Texture t = new Texture(device, w, h, 1, Usage.None, Format.A8R8G8B8, Pool.SystemMemory);
                Microsoft.DirectX.GraphicsStream gs = t.LockRectangle(0, LockFlags.None);
                dst = (uint *)bitmapData.Scan0;
                for (uint y = 0; y < h; ++y)
                {
                    for (uint x = 0; x < w; ++x)
                    {
                        gs.Write(dst[x]);
                    }
                    dst += bitmapData.Stride / 4;
                }
                t.UnlockRectangle(0);
                image.UnlockBits(bitmapData);
                string dstFilename = System.IO.Path.GetDirectoryName(s);
                dstFilename += "\\";
                dstFilename += System.IO.Path.GetFileNameWithoutExtension(s);
                dstFilename += ".dds";
                TextureLoader.Save(dstFilename, ImageFileFormat.Dds, t);
                //後始末
                image.Dispose();
                image = null;
                if (alphaImage != null)
                {
                    alphaImage.Dispose();
                    alphaImage = null;
                }
                bitmapData      = null;
                alphaBitmapData = null;
                t.Dispose();
                t = null;
                gs.Close();
                gs = null;
            }
            textBox2.Text = "状態:準備よし.";
        }