Esempio n. 1
0
        static void Main(string[] args)
        {
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));

            using (TSOSample sample = new TSOSample())
                using (TSOForm form = new TSOForm())
                {
                    if (sample.InitializeApplication(form))
                    {
                        sample.LoadCurrentTMOFile("sample.tmo");
                        sample.TAHDump(args);

                        //form.Show();
                        long wait      = (long)(10000000.0f / 60.0f);
                        long nextTicks = DateTime.Now.Ticks;
                        // While the form is still valid, render and process messages
                        while (form.Created)
                        {
                            if (DateTime.Now.Ticks >= nextTicks)
                            {
                                sample.FrameMove();
                                if (DateTime.Now.Ticks < nextTicks + wait)
                                {
                                    sample.Render();
                                }
                                nextTicks += wait;
                            }
                            Application.DoEvents();
                        }
                    }
                }
        }
Esempio n. 2
0
        public bool InitializeApplication(TSOForm form)
        {
            this.form = form;

            for (int i = 0; i < keysEnabled.Length; i++)
            {
                keysEnabled[i] = true;
            }
            form.KeyDown += new KeyEventHandler(form_OnKeyDown);
            form.KeyUp   += new KeyEventHandler(form_OnKeyUp);

            form.MouseDown += new MouseEventHandler(form_OnMouseDown);
            form.MouseMove += new MouseEventHandler(form_OnMouseMove);

            form.DragDrop  += new DragEventHandler(form_OnDragDrop);
            form.DragEnter += new DragEventHandler(form_OnDragEnter);

            PresentParameters pp = new PresentParameters();

            try
            {
                pp.Windowed               = true;
                pp.SwapEffect             = SwapEffect.Discard;
                pp.BackBufferFormat       = Format.X8R8G8B8;
                pp.BackBufferCount        = 1;
                pp.EnableAutoDepthStencil = true;
                pp.AutoDepthStencilFormat = DepthFormat.D16;

                int adapter_ordinal = Manager.Adapters.Default.Adapter;

                int ret, quality;
                if (Manager.CheckDeviceMultiSampleType(adapter_ordinal, DeviceType.Hardware, pp.BackBufferFormat, pp.Windowed, MultiSampleType.FourSamples, out ret, out quality))
                {
                    pp.MultiSample        = MultiSampleType.FourSamples;
                    pp.MultiSampleQuality = quality - 1;
                }

                CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
                Caps        caps  = Manager.GetDeviceCaps(adapter_ordinal, DeviceType.Hardware);
                if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
                {
                    flags = CreateFlags.HardwareVertexProcessing;
                }
                if (caps.DeviceCaps.SupportsPureDevice)
                {
                    flags |= CreateFlags.PureDevice;
                }
                device = new Device(adapter_ordinal, DeviceType.Hardware, form.Handle, flags, pp);

                FontDescription fd = new FontDescription();
                fd.Height   = 24;
                fd.FaceName = "MS Gothic";
                font        = new Direct3D.Font(device, fd);
            }
            catch (DirectXException ex)
            {
                Console.WriteLine("Error: " + ex);
                return(false);
            }

            Directory.SetCurrentDirectory(Application.StartupPath);

            boneMatrices = new Matrix[maxBones];

            string effect_file = @"toonshader.cgfx";

            if (!File.Exists(effect_file))
            {
                Console.WriteLine("File not found: " + effect_file);
                return(false);
            }
            string compile_error;

            effect = Effect.FromFile(device, effect_file, null, ShaderFlags.None, null, out compile_error);
            if (compile_error != null)
            {
                Console.WriteLine(compile_error);
                return(false);
            }
            handle_LocalBoneMats = effect.GetParameter(null, "LocalBoneMats");

            int devw = 0;
            int devh = 0;

            using (Surface surface = device.DepthStencilSurface)
            {
                devw = surface.Description.Width;
                devh = surface.Description.Height;
            }
            Console.WriteLine("dev {0}x{1}", devw, devh);

            dev_surface = device.GetRenderTarget(0);
            dev_zbuf    = device.DepthStencilSurface;

            camera.Update();
            camera.SetTranslation(camTranslation);

            Transform_Projection = Matrix.PerspectiveFovLH(
                Geometry.DegreeToRadian(30.0f),
                (float)device.Viewport.Width / (float)device.Viewport.Height,
                1.0f,
                1000.0f);

            // xxx: for w-buffering
            device.Transform.Projection = Transform_Projection;

            if (effect != null)
            {
                effect.SetValue("proj", Transform_Projection);
            }

            device.RenderState.Lighting = false;
            device.RenderState.CullMode = Cull.CounterClockwise;

            device.RenderState.AlphaBlendEnable = true;
            device.SetTextureStageState(0, TextureStageStates.AlphaOperation, (int)TextureOperation.Modulate);
            device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, (int)TextureArgument.TextureColor);
            device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, (int)TextureArgument.Current);

            device.RenderState.SourceBlend      = Blend.SourceAlpha;
            device.RenderState.DestinationBlend = Blend.InvSourceAlpha;

            device.RenderState.IndexedVertexBlendEnable = true;

            current_tmo = new TMOFile();
            return(true);
        }