It is currently Sat May 18, 2013 10:52 pm



Reply to topic  [ 3 posts ] 
 How to control stereo-frames separately with C#? - SOLVED 
Author Message
One Eyed Hopeful

Joined: Mon May 09, 2011 2:41 am
Posts: 2
I’m trying to make a very simple application which would display different images on each eye. I have Asus VG236H monitor and NVIDIA 3D Vision kit, the stereo 3D shutter glasses. The I’m using C#, .NET Framework 2.0, DirectX 9 (Managed Direct X) and Visual Studio 2008. I have been searching high and low for examples and tutorials, have actually found a couple and based those I have created the program but for some reason I can’t get it working.

When looking for examples how to display different images for each eye, many people keep referring to the NVIDIA presentation at GDC 09 (the famous GDC09-3DVision-The_In_and_Out.pdf document) and the pages 37-40. My code is mainly constructed based on that example:
1. I'm loading two textures (Red.png and Blue.png) on surface (_imageLeft and _imageRight), in function LoadSurfaces()
2. Set3D() function puts those two images side-by-side to one bigger image which has the size of 2x Screen width and Screen height + 1 (_imageBuf).
3. Set3D() function continues by appending the stereo signature on the last row.
4. OnPaint()-function takes the back buffer (_backBuf) and copies the content of the combined image (_imageBuf) to it.

When I run the program, shutter glasses start working, but I only see the two images side-by-side on the screen. Could someone help out and tell me what am I doing wrong? I believe that solving this problem might also help others as there does not yet seem to be a simple example how to do this with C#.

Here are the tactical parts of my code. Complete project as attachment.
Attachment:
NVStereoTest.rar

Code:
        public void InitializeDevice()
        {
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed = false;
            presentParams.BackBufferFormat = Format.A8R8G8B8;
            presentParams.BackBufferWidth = _size.Width;
            presentParams.BackBufferHeight = _size.Height;
            presentParams.BackBufferCount = 1;
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.PresentationInterval = PresentInterval.One;
            _device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }

        public void LoadSurfaces()
        {
            _imageBuf = _device.CreateOffscreenPlainSurface(_size.Width * 2, _size.Height + 1, Format.A8R8G8B8, Pool.Default);

            _imageLeft = Surface.FromBitmap(_device, (Bitmap)Bitmap.FromFile("Blue.png"), Pool.Default);
            _imageRight = Surface.FromBitmap(_device, (Bitmap)Bitmap.FromFile("Red.png"), Pool.Default);
        }

        private void Set3D()
        {
            Rectangle destRect = new Rectangle(0, 0, _size.Width, _size.Height);
            _device.StretchRectangle(_imageLeft, _size, _imageBuf, destRect, TextureFilter.None);
            destRect.X = _size.Width;
            _device.StretchRectangle(_imageRight, _size, _imageBuf, destRect, TextureFilter.None);

            GraphicsStream gStream = _imageBuf.LockRectangle(LockFlags.None);

            byte[] data = new byte[] {0x44, 0x33, 0x56, 0x4e,   //NVSTEREO_IMAGE_SIGNATURE         = 0x4433564e
                                      0x00, 0x00, 0x0F, 0x00,   //Screen width * 2 = 1920*2 = 3840 = 0x00000F00;
                                      0x00, 0x00, 0x04, 0x38,   //Screen height = 1080             = 0x00000438;
                                      0x00, 0x00, 0x00, 0x20,   //dwBPP = 32                       = 0x00000020;
                                      0x00, 0x00, 0x00, 0x02};  //dwFlags = SIH_SCALE_TO_FIT       = 0x00000002;

            gStream.Seek(_size.Width * 2 * _size.Height * 4, System.IO.SeekOrigin.Begin);   //last row
            gStream.Write(data, 0, data.Length);

            gStream.Close();

            _imageBuf.UnlockRectangle();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            _device.BeginScene();

            // Get the Backbuffer then Stretch the Surface on it.
            _backBuf = _device.GetBackBuffer(0, 0, BackBufferType.Mono);
            _device.StretchRectangle(_imageBuf, new Rectangle(0, 0, _size.Width * 2, _size.Height + 1), _backBuf, new Rectangle(0, 0, _size.Width, _size.Height), TextureFilter.None);
            _backBuf.ReleaseGraphics();

            _device.EndScene();

            _device.Present();

            this.Invalidate();
        }


You do not have the required permissions to view the files attached to this post.


Last edited by Andows on Mon May 09, 2011 6:32 am, edited 1 time in total.



Mon May 09, 2011 4:00 am
Profile
One Eyed Hopeful

Joined: Mon May 09, 2011 2:41 am
Posts: 2
A friend of mine found the problem. The bytes in the stereo signature were in reversed order. Here is the correct order:
Code:
            byte[] data = new byte[] {0x4e, 0x56, 0x33, 0x44,   //NVSTEREO_IMAGE_SIGNATURE         = 0x4433564e;
                                      0x00, 0x0F, 0x00, 0x00,   //Screen width * 2 = 1920*2 = 3840 = 0x00000F00;
                                      0x38, 0x04, 0x00, 0x00,   //Screen height = 1080             = 0x00000438;
                                      0x20, 0x00, 0x00, 0x00,   //dwBPP = 32                       = 0x00000020;
                                      0x02, 0x00, 0x00, 0x00};  //dwFlags = SIH_SCALE_TO_FIT       = 0x00000002;


The code works perfectly after this change. This code might even serve as a good tutorial for someone else attempting the same thing. :)


Mon May 09, 2011 6:28 am
Profile
3D Angel Eyes (Moderator)
User avatar

Joined: Sat Apr 12, 2008 8:18 pm
Posts: 10022
Thanks for posting. This may prove useful for some members.

_________________
Image


Mon May 09, 2011 5:58 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by STSoftware.