public void CreatePresentParameters() { //初始化设备参数 present_params = new PresentParameters(); #if FULL_SCREEN //全屏 present_params.Windowed = false; present_params.BackBufferCount = 2; present_params.BackBufferWidth = 1024; present_params.BackBufferHeight = 768; present_params.BackBufferFormat = Format.X8R8G8B8; present_params.SwapEffect = SwapEffect.Flip; present_params.EnableAutoDepthStencil = true; present_params.AutoDepthStencilFormat = DepthFormat.D24X8; #else //窗口 present_params.Windowed = true; present_params.BackBufferWidth = ClientSize.Width; present_params.BackBufferHeight = ClientSize.Height; present_params.SwapEffect = SwapEffect.Flip; present_params.BackBufferCount = 1; present_params.EnableAutoDepthStencil = true; present_params.AutoDepthStencilFormat = D3DConfiguration.GetAppropriateDepthFormat(); //谨慎开启全屏反锯齿FSAA:其性能冲击非常大 if (FSAA) { int quality; MultiSampleType type; D3DConfiguration.GetAppropriateMultiSampleType(out type, out quality); if (type == MultiSampleType.NonMaskable) { present_params.SwapEffect = SwapEffect.Discard; //必须! present_params.MultiSample = type; //开启全屏反锯齿FSAA present_params.MultiSampleQuality = quality - 1; //容许的最高级别 } } #endif }
////////////////////////////////////////////////////////////////////////////////////////////////////// public bool InitializeGraphics() { //初始化设备参数 CreatePresentParameters(); Device.IsUsingEventHandlers = false; //关闭MDX的自动事件布线机制! CreateFlags cf = D3DConfiguration.GetAppropriateCreateFlags(); device = new Device(0, DeviceType.Hardware, this, cf, present_params); device.DeviceReset += new EventHandler(this.OnDeviceReset); device.DeviceLost += new EventHandler(this.OnDeviceLost); gdifont = new System.Drawing.Font("新宋体", 12); //GDI字体准备 axises = new CoordAxies(); //初始化坐标轴 SetupDevice(); //重建所有资源 camera = new Camera(am.radius * 5f); //镜头准备 return(true); }
////////////////////////////////////////////////////////////////////////////////////////////////////// public void Render() { //计算下一帧的模型 am.Animate(delta_time); if (deviceLost) { AttemptRecovery(); } if (deviceLost) { return; } device.RenderState.Lighting = false; device.RenderState.CullMode = Cull.CounterClockwise; //右手系拣选 device.RenderState.SlopeScaleDepthBias = 2F; device.SamplerState[0].MinFilter = D3DConfiguration.GetAppropriateTextureMinFilter(); //缩小滤波 device.SamplerState[0].MagFilter = D3DConfiguration.GetAppropriateTextureMagFilter(); //放大滤波 if (DisplayWireFrame == 1 || DisplayWireFrame == 2) //显示贴图线框或纯线框 { device.RenderState.FillMode = FillMode.WireFrame; } else //显示贴图 { device.RenderState.FillMode = FillMode.Solid; } if (DrawBackground) { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0F, 0); } else { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkGray, 1.0F, 0); } //关键:只要alpha值大于等于1的点才显示并更新zbuf!即,alpha=0的点直接不画,也不会影响zbuf! //注意:盟2的abi里面没有透明色的概念,不知道盟3有没有 //device.RenderState.AlphaTestEnable = true; //device.RenderState.AlphaFunction = Compare.GreaterEqual; //device.RenderState.ReferenceAlpha = 1; device.BeginScene(); { SetupMatrices(); //--------------------------------------------------------------------------------------- device.VertexFormat = CustomVertex.PositionColoredTextured.Format; device.SetStreamSource(0, am.vexbuf, 0); for (int i = 0; i < am.texture.Length; i++) { if (DisplayWireFrame == 2) //纯线框 { device.SetTexture(0, null); } else //贴图线框或者是贴图 { device.SetTexture(0, am.texture[i]); } //注意:无需Alpha贴图功能,盟2的abi里面没有Alpha贴图的概念,不知道盟3有没有 //device.RenderState.SourceBlend = Blend.One; //device.RenderState.DestinationBlend = Blend.Zero; //device.RenderState.AlphaBlendEnable = false; int count = (am.txtoffset[i + 1] - am.txtoffset[i]) / 3; if (count != 0) { device.DrawPrimitives(PrimitiveType.TriangleList, am.txtoffset[i], count); } } //--------------------------------------------------------------------------------------- if (DrawShadow) //从前向后绘制,Z深度优化 { sm.Animation(); //计算下一帧的阴影 device.VertexFormat = CustomVertex.PositionColored.Format; device.SetStreamSource(0, sm.shadowbuf, 0); device.SetTexture(0, null); device.DrawPrimitives(PrimitiveType.TriangleList, 0, sm.totalf); } //--------------------------------------------------------------------------------------- if (DrawCoordinateAxies) { axises.Render(device); PrintAxiesComment(); } //--------------------------------------------------------------------------------------- switch (DrawInformationText) { case 1: PrintMessageOnScene(); break; case 2: PrintBoneHierarchyOnScene(); break; } } device.EndScene(); try { device.Present(); } catch (DeviceLostException) { deviceLost = true; } }