⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lighting.cs

📁 Windows Mobile6 D3D案例
💻 CS
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Windows.Forms;
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;

namespace Microsoft.Samples.MD3DM
{
    // Custom D3D vertex format used by the vertex buffer
    struct MyVertex
    {
        public Vector3 p;       // vertex position
        public Vector3 n;       // vertex normal

        public static readonly VertexFormats Format = 
            VertexFormats.Position | VertexFormats.Normal;
    };
    
    /// <summary>
    /// Main form that renders this sample
    /// </summary>
    public class LightingForm : System.Windows.Forms.Form
    {
        private Microsoft.WindowsMobile.DirectX.Direct3D.Device device = null;
        // Tessellated plane to serve as the walls and floor
        private Mesh wallMesh = null;
        // Representation of point light
        private Mesh sphereMesh = null;  
        // Representation of dir/spot light
        private Mesh coneMesh = null;                 
        // Description of the D3D light
        private Light lightData;           
        // Number of vertices in the wall mesh along X
        private uint numberVertsX = 16;         
        // Number of vertices in the wall mesh along Z         
        private uint numberVertsZ = 16;
        // Number of triangles in the wall mesh                  
        private int numberTriangles = 0;         

        // the tick count when the app started rendering
        private int tickStart = 0;

        // the number of seconds since rendering started
        private float appTime;

        // a helper to record and render fps statistics
        private FpsTimerTool fpsTimer;

        /// <summary>
        /// Application constructor. Sets attributes for the app.
        /// </summary>
        public LightingForm()
        {
            // the number of traingles in the wall mesh
            numberTriangles = (int)((numberVertsX - 1) *
                (numberVertsZ - 1) * 2);

            // Set the window text
            this.Text = "Lighting";

            // Now let's setup our D3D stuff
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            presentParams.EnableAutoDepthStencil = true;
            device = new Device(0, DeviceType.Default, this, CreateFlags.None,
                presentParams);

            // setup those objects which persist through reset
            InitializeDeviceObjects();
            // attach the device reset handler
            device.DeviceReset += new EventHandler(RestoreDeviceObjects);
            // setup any device resources that will not persist through reset
            RestoreDeviceObjects(device, EventArgs.Empty);
        }

        /// <summary>
        /// Called once per frame, the call is the entry point for animating
        /// the scene.
        /// </summary>
        public void FrameMove()
        {
            lightData = device.Lights[2];
            // Rotate through the various light types
            if (((int)appTime % 20) < 10)
                device.Lights[2].Type = LightType.Point;
            else
                device.Lights[2].Type = LightType.Directional;

            // Make sure the light type is supported by the device.  If 
            // VertexProcessingCaps.PositionAllLights is not set, the device
            // does not support point or spot lights, so change light #2's
            // type to a directional light.
            if 
            (!device.DeviceCaps.VertexProcessingCaps.SupportsPositionalLights)
            {
                if (device.Lights[2].Type == LightType.Point)
                    device.Lights[2].Type = LightType.Directional;
            }

            // Values for the light position, direction, and color
            float x = (float)Math.Sin(appTime * 2.000f);
            float y = (float)Math.Sin(appTime * 2.246f);
            float z = (float)Math.Sin(appTime * 2.640f);

            byte r = (byte)((0.5f + 0.5f * x) * 0xff);
            byte g = (byte)((0.5f + 0.5f * y) * 0xff);
            byte b = (byte)((0.5f + 0.5f * z) * 0xff);
            device.Lights[2].Diffuse = System.Drawing.Color.FromArgb(r, g, b);
            device.Lights[2].Range = 100.0f;
    
            switch(device.Lights[2].Type)
            {
                case LightType.Point:
                    device.Lights[2].Position = new Vector3(4.5f * x,
                        4.5f * y, 4.5f * z);
                    device.Lights[2].Attenuation1 = 0.4f;
                    break;
                case LightType.Directional:
                    device.Lights[2].Direction = new Vector3(x, y, z);
                    break;
            }
            device.Lights[2].Update();
        }

        /// <summary>
        /// Called once per frame, the call is the entry point for 3d
        /// rendering. This function sets up render states, clears the
        /// viewport, and renders the scene.
        /// </summary>
        public void Render()
        {
            Matrix matWorld;
            Matrix matTrans;
            Matrix matRotate;

            fpsTimer.StartFrame();

            // Clear the viewport
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, 0x000000ff,
                1.0f, 0);

            device.BeginScene();

            // Turn on light #0 and #2, and turn off light #1
            device.Lights[0].Enabled = true;
            device.Lights[1].Enabled = false;
            device.Lights[2].Enabled = true;

            // Draw the floor
            matTrans = Matrix.Translation(-5.0f, -5.0f, -5.0f);
            matRotate = Matrix.RotationZ(0.0f);
            matWorld = matRotate * matTrans;
            device.SetTransform(TransformType.World, matWorld);
            wallMesh.DrawSubset(0);

            // Draw the back wall
            matTrans = Matrix.Translation(5.0f, -5.0f, -5.0f);
            matRotate = Matrix.RotationZ((float)Math.PI / 2);
            matWorld = matRotate * matTrans;
            device.SetTransform(TransformType.World, matWorld);
            wallMesh.DrawSubset(0);

            // Draw the side wall
            matTrans = Matrix.Translation(-5.0f, -5.0f, 5.0f);
            matRotate = Matrix.RotationX((float)-Math.PI / 2);
            matWorld = matRotate * matTrans;
            device.SetTransform(TransformType.World, matWorld);
            wallMesh.DrawSubset(0);

            // Turn on light #1, and turn off light #0 and #2
            device.Lights[0].Enabled = false;
            device.Lights[1].Enabled = true;
            device.Lights[2].Enabled = false;

            // Draw the mesh representing the light
            if (lightData.Type == LightType.Point)
            {
                // Just position the point light -- no need to orient it
                matWorld = Matrix.Translation(lightData.Position.X, 
                    lightData.Position.Y, lightData.Position.Z);
                device.SetTransform(TransformType.World, matWorld);
                sphereMesh.DrawSubset(0);
            }
            else
            {
                // Position the light and point it in the light's direction
                Vector3 vecFrom = new Vector3(lightData.Position.X,
                    lightData.Position.Y, lightData.Position.Z);
                Vector3 vecAt = new Vector3(
                    lightData.Position.X + lightData.Direction.X, 
                    lightData.Position.Y + lightData.Direction.Y,
                    lightData.Position.Z + lightData.Direction.Z);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -