Beispiel #1
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);
            try
            {
                // Load texture map. Note that this is a special texture, which has a
                // height field stored in the alpha channel
                embossTexture = GraphicsUtility.CreateTexture(device, "emboss1.dds", Format.A8R8G8B8);

                // Load geometry
                renderObject = new GraphicsMesh();
                renderObject.Create(device, "tiger.x");

                // Set attributes for geometry
                renderObject.SetVertexFormat(device, EmbossVertex.Format);
                renderObject.IsUsingMaterials = false;

                // Compute the object's tangents and binormals, whaich are needed for the
                // emboss-tecnhique's texture-coordinate shifting calculations
                ComputeTangentsAndBinormals();
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
        }
Beispiel #2
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);
            drawingFontSmall.InitializeDeviceObjects(device);

            try
            {
                // Create textures
                groundTexture   = GraphicsUtility.CreateTexture(device, "Ground2.bmp", Format.Unknown);
                particleTexture = GraphicsUtility.CreateTexture(device, "Particle.bmp", Format.Unknown);
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }


            // Check if we can do the reflection effect
            canDoALphaBlend = (Caps.SourceBlendCaps.SupportsSourceAlpha) && (Caps.DestinationBlendCaps.SupportsInverseSourceAlpha);

            if (canDoALphaBlend)
            {
                canDrawReflection = true;
            }
        }
Beispiel #3
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            // Load the file objects
            try
            {
                shinyTeapot.Create(device, "teapot.x");
                skyBox.Create(device, "lobby_skybox.x");
                sphereMapTexture = GraphicsUtility.CreateTexture(device, "spheremap.bmp");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }

            // Set mesh properties
            shinyTeapot.SetVertexFormat(device, EnvMappedVertex.Format);

            // Restore the device-dependent objects
            font.InitializeDeviceObjects(device);
            string sEffect = null;

            for (int i = 0; i < effectString.Length; i++)
            {
                sEffect += effectString[i];
            }

            effect = Effect.FromString(device, sEffect, null, 0, null);
        }
Beispiel #4
0
        /// <summary>
        /// Create a file based normap map
        /// </summary>
        void CreateFileBasedNormalMap()
        {
            Texture fileBasedSource = null;

            try
            {
                // Load the texture from a file
                fileBasedSource = GraphicsUtility.CreateTexture(device, "earthbump.bmp", Format.A8R8G8B8);
                SurfaceDescription desc = fileBasedSource.GetLevelDescription(0);
                fileBasedNormalMap = new Texture(device, desc.Width, desc.Height, fileBasedSource.LevelCount, 0, Format.A8R8G8B8, Pool.Managed);
                TextureLoader.ComputeNormalMap(fileBasedNormalMap, fileBasedSource, 0, Channel.Red, 1.0f);
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
            finally
            {
                if (fileBasedSource != null)
                {
                    fileBasedSource.Dispose();
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            // Initialize the font's internal textures
            font.InitializeDeviceObjects(device);

            // Load the main file object
            for (int i = 0; i < 2; i++)
            {
                if (fileObject[i] == null)
                {
                    fileObject[i] = new GraphicsMesh();
                }
            }

            try
            {
                // Load a .X file
                fileObject[0].Create(device, "Heli.x");
                fileObject[1].Create(device, "airplane 2.x");
                if ((squareVertexBuffer == null) || (squareVertexBuffer.Disposed))
                {
                    // Create a big square for rendering the stencilbuffer contents
                    squareVertexBuffer          = new VertexBuffer(typeof(Vector4), 4, device, Usage.WriteOnly, VertexFormats.Transformed, Pool.Default);
                    squareVertexBuffer.Created += new System.EventHandler(this.SquareVBCreated);
                    this.SquareVBCreated(squareVertexBuffer, null);
                }
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
        }
Beispiel #6
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            // Load the file objects
            try
            {
                shinyTeapotMesh.Create(device, "teapot.x");
                skyBoxMesh.Create(device, "lobby_skybox.x");
                airplaneMesh.Create(device, "airplane 2.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }

            // Set mesh properties
            airplaneMesh.SetVertexFormat(device, VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture1);
            shinyTeapotMesh.SetVertexFormat(device, EnvMappedVertex.Format);

            // Restore the device-dependent objects
            drawingFont.InitializeDeviceObjects(device);
            string sEffect = null;

            for (int i = 0; i < effectString.Length; i++)
            {
                sEffect += effectString[i];
            }

            effect = Effect.FromString(device, sEffect, null, 0, null);
        }
Beispiel #7
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            // Initialize the font's internal textures
            font.InitializeDeviceObjects(device);

            // Load the main file object
            if (airplane == null)
            {
                airplane = new GraphicsMesh();
            }

            try
            {
                airplane.Create(device, "airplane 2.x");

                // Load the terrain
                if (terrainObject == null)
                {
                    terrainObject = new GraphicsMesh();
                }

                terrainObject.Create(device, "SeaFloor.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }

            // Set a reasonable vertex type
            terrainObject.SetVertexFormat(device, MeshVertex.Format);
            airplane.SetVertexFormat(device, MeshVertex.Format);

            // Tweak the terrain vertices to add some bumpy terrain
            if (terrainObject != null)
            {
                // Get access to the mesh vertices
                VertexBuffer vertBuffer  = null;
                MeshVertex[] vertices    = null;
                int          numVertices = terrainObject.SystemMesh.NumberVertices;
                vertBuffer = terrainObject.SystemMesh.VertexBuffer;
                vertices   = (MeshVertex[])vertBuffer.Lock(0, typeof(MeshVertex), 0, numVertices);

                Random r = new Random();
                // Add some more bumpiness to the terrain object
                for (int i = 0; i < numVertices; i++)
                {
                    vertices[i].p.Y += 1 * (float)r.NextDouble();
                    vertices[i].p.Y += 2 * (float)r.NextDouble();
                    vertices[i].p.Y += 1 * (float)r.NextDouble();
                }

                vertBuffer.Unlock();
                vertBuffer.Dispose();
            }
        }
Beispiel #8
0
        /// <summary>
        /// The device exists, but may have just been Reset().  Resources in
        /// Pool.Default and any other device state that persists during
        /// rendering should be set here.  Render states, matrices, textures,
        /// etc., that don't change during rendering can be set once here to
        /// avoid redundant state setting during Render() or FrameMove().
        /// </summary>
        protected override void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
        {
            // Restore mesh's local memory objects
            blendObject.RestoreDeviceObjects(device, null);

            // Get access to the mesh vertex and index buffers
            vertexBuffer = blendObject.LocalMesh.VertexBuffer;
            indexBuffer  = blendObject.LocalMesh.IndexBuffer;
            numVertices  = blendObject.LocalMesh.NumberVertices;
            numFaces     = blendObject.LocalMesh.NumberFaces;

            if (BehaviorFlags.SoftwareVertexProcessing ||
                Caps.VertexShaderVersion.Major >= 1)
            {
                // Setup the vertex declaration
                VertexElement[] decl = VertexInformation.DeclaratorFromFormat(BlendVertex.Format);
                declaration = new VertexDeclaration(device, decl);

                // Create vertex shader from a file
                try
                {
                    shader = GraphicsUtility.CreateVertexShader(device, "blend.vsh");
                }
                catch
                {
                    SampleException d3de = new MediaNotFoundException();
                    HandleSampleException(d3de, ApplicationMessage.ApplicationMustExit);
                    throw d3de;
                }
            }
            // Set miscellaneous render states
            renderState.ZBufferEnable = true;
            renderState.Ambient       = System.Drawing.Color.FromArgb(0x00404040);

            // Set the projection matrix
            float fAspect = device.PresentationParameters.BackBufferWidth / (float)device.PresentationParameters.BackBufferHeight;

            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, fAspect, 1.0f, 10000.0f);

            // Set the app view matrix for normal viewing
            Vector3 vEyePt    = new Vector3(0.0f, -5.0f, -10.0f);
            Vector3 vLookatPt = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 vUpVec    = new Vector3(0.0f, 1.0f, 0.0f);

            device.Transform.View = Matrix.LookAtLH(vEyePt, vLookatPt, vUpVec);

            // Create a directional light. (Use yellow light to distinguish from
            // vertex shader case.)
            GraphicsUtility.InitLight(device.Lights[0], LightType.Directional, -0.5f, -1.0f, 1.0f);
            device.Lights[0].Diffuse = System.Drawing.Color.Yellow;
            device.Lights[0].Commit();
            device.Lights[0].Enabled = true;
            renderState.Lighting     = true;
        }
Beispiel #9
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            // Initialize the font's internal textures
            drawingFont.InitializeDeviceObjects(device);

            try
            {
                // Create the tree textures
                for (int i = 0; i < treeTextureFileNames.Length; i++)
                {
                    treeTextures[i] = GraphicsUtility.CreateTexture(device, treeTextureFileNames[i]);
                }
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }


            try
            {
                // Load the skybox
                skyBoxMesh.Create(device, "SkyBox2.x");

                // Load the terrain
                terrainMesh.Create(device, "SeaFloor.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }

            // Add some "hilliness" to the terrain
            VertexBuffer tempVertexBuffer;

            tempVertexBuffer = terrainMesh.SystemMesh.VertexBuffer;
            CustomVertex.PositionTextured[] pVertices;
            int numberVertices = terrainMesh.SystemMesh.NumberVertices;

            pVertices = (CustomVertex.PositionTextured[])tempVertexBuffer.Lock(0, typeof(CustomVertex.PositionTextured), 0, numberVertices);
            for (int i = 0; i < numberVertices; i++)
            {
                pVertices[i].Y = HeightField(pVertices[i].X, pVertices[i].Z);
            }
            tempVertexBuffer.Unlock();
        }
Beispiel #10
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);

            // Load the file objects
            try
            {
                skyBox.Create(device, "lobby_skyboxInv.x");
                GenerateFishEyeLens(20, 20, 1.0f);
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
        }
Beispiel #11
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);

            // Set up the geometry objects
            try
            {
                if (teapotMesh == null)
                {
                    teapotMesh = new GraphicsMesh();
                }

                teapotMesh.Create(device, "teapot.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
        }
Beispiel #12
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            font.InitializeDeviceObjects(device);
            try
            {
                // Load the texture for the background image
                backTex = GraphicsUtility.CreateTexture(device, "lake.bmp", Format.Unknown);
                // Create the bump map texture
                CreateBumpMap(256, 256);

                // Create a square for rendering the lens
                if ((lens == null) || (lens.Disposed))
                {
                    // We only need to create this buffer once
                    lens          = new VertexBuffer(typeof(BumpVertex), 4, device, Usage.WriteOnly, BumpVertex.Format, Pool.Default);
                    lens.Created += new System.EventHandler(this.LensCreated);
                    // Call it manually the first time
                    this.LensCreated(lens, null);
                }

                // Create a square for rendering the background
                if ((background == null) || (background.Disposed))
                {
                    // We only need to create this buffer once
                    background          = new VertexBuffer(typeof(CustomVertex.TransformedColoredTextured), 4, device, Usage.WriteOnly, CustomVertex.TransformedColoredTextured.Format, Pool.Default);
                    background.Created += new System.EventHandler(this.BackGroundCreated);
                    // Call it manually the first time
                    this.BackGroundCreated(background, null);
                }
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
        }
Beispiel #13
0
 /// <summary>
 /// The device has been created.  Resources that are not lost on
 /// Reset() can be created here -- resources in Pool.Managed,
 /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
 /// CreateImageSurface are never lost and can be created here.  Vertex
 /// shaders and pixel shaders can also be created here as they are not
 /// lost on Reset().
 /// </summary>
 protected override void InitializeDeviceObjects()
 {
     font.InitializeDeviceObjects(device);
     try
     {
         background = GraphicsUtility.CreateTexture(device, "LobbyXPos.jpg", Format.A8R8G8B8);
         // create a bumpmap from info in source surface
         CreateBumpMap();
         if ((waterBuffer == null) || (waterBuffer.Disposed))
         {
             // We only need to create this buffer once
             waterBuffer          = new VertexBuffer(typeof(BumpVertex), 4, device, Usage.WriteOnly, BumpVertex.Format, Pool.Managed);
             waterBuffer.Created += new System.EventHandler(this.WaterBufferCreated);
             // Call it manually the first time
             this.WaterBufferCreated(waterBuffer, null);
         }
     }
     catch
     {
         SampleException e = new MediaNotFoundException();
         HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
         throw e;
     }
 }
Beispiel #14
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);
            try
            {
                // Load the texture for the background image
                background = GraphicsUtility.CreateTexture(device, "lake.bmp", Format.R5G6B5);
                // Create the bumpmap.
                bumpMap = CreateBumpMap(256, 256, Format.V8U8);
                if (bumpMap == null)
                {
                    throw new InvalidOperationException();
                }

                // Create a square for rendering the background
                if ((backgroundVertex == null) || (backgroundVertex.Disposed))
                {
                    // We only need to create this buffer once
                    backgroundVertex          = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
                    backgroundVertex.Created += new System.EventHandler(this.BackgroundVertexCreated);
                    // Call it manually the first time
                    this.BackgroundVertexCreated(backgroundVertex, null);
                }
                // See if EMBM and projected vertices are supported at the same time
                // in the fixed-function shader.  If not, switch to using a vertex shader.
                isUsingVertexShader = false;
                SetEMBMStates();
                device.VertexShader = null;
                device.VertexFormat = CustomVertex.PositionTextured.Format;

                ValidateDeviceParams validParams = device.ValidateDevice();
                if (validParams.Result != 0)
                {
                    isUsingVertexShader = true;
                }

                // If TextureCaps.Projected is set, projected textures are computed
                // per pixel, so this sample will work fine with just a quad for the water
                // model.  If it's not set, textures are projected per vertex rather than
                // per pixel, so distortion will be visible unless we use more vertices.
                if (Caps.TextureCaps.SupportsProjected && !isUsingVertexShader)
                {
                    numVertx = 2;               // Number of vertices in the ground grid along X
                    numVertz = 2;               // Number of vertices in the ground grid along Z
                }
                else
                {
                    numVertx = 8;                                   // Number of vertices in the ground grid along X
                    numVertz = 8;                                   // Number of vertices in the ground grid along Z
                }
                numTriangles = (numVertx - 1) * (numVertz - 1) * 2; // Number of triangles in the ground


                // Create a square for rendering the water
                if ((waterBuffer == null) || (waterBuffer.Disposed))
                {
                    // We only need to create this buffer once
                    waterBuffer          = new VertexBuffer(typeof(CustomVertex.PositionTextured), 3 * numTriangles, device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
                    waterBuffer.Created += new System.EventHandler(this.WaterBufferCreated);
                    // Call it manually the first time
                    this.WaterBufferCreated(waterBuffer, null);
                }
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
        }
Beispiel #15
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            VertexBuffer pMeshSourceVB = null;
            IndexBuffer  pMeshSourceIB = null;

            Vertex[]       src           = null;
            GraphicsStream dst           = null;
            GraphicsMesh   DolphinMesh01 = new GraphicsMesh();
            GraphicsMesh   DolphinMesh02 = new GraphicsMesh();
            GraphicsMesh   DolphinMesh03 = new GraphicsMesh();
            GraphicsMesh   SeaFloorMesh  = new GraphicsMesh();

            // Initialize the font's internal textures
            drawingFont.InitializeDeviceObjects(device);

            try
            {
                // Create texture for the dolphin
                dolphinTexture = GraphicsUtility.CreateTexture(device, "Dolphin.bmp");

                // Create textures for the seafloor
                seaFloorTexture = GraphicsUtility.CreateTexture(device, "SeaFloor.bmp");

                // Create textures for the water caustics
                for (int t = 0; t < 32; t++)
                {
                    string name = string.Format("Caust{0:D2}.tga", t);
                    causticTextures[t] = GraphicsUtility.CreateTexture(device, name);
                }

                // Load the file-based mesh objects
                DolphinMesh01.Create(device, "dolphin1.x");
                DolphinMesh02.Create(device, "dolphin2.x");
                DolphinMesh03.Create(device, "dolphin3.x");
                SeaFloorMesh.Create(device, "SeaFloor.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }
            // Set the FVF type to match the vertex format we want
            DolphinMesh01.SetVertexFormat(device, Vertex.Format);
            DolphinMesh02.SetVertexFormat(device, Vertex.Format);
            DolphinMesh03.SetVertexFormat(device, Vertex.Format);
            SeaFloorMesh.SetVertexFormat(device, Vertex.Format);

            // Get the number of vertices and faces for the meshes
            numDolphinVertices  = DolphinMesh01.SystemMesh.NumberVertices;
            numDolphinFaces     = DolphinMesh01.SystemMesh.NumberFaces;
            numSeaFloorVertices = SeaFloorMesh.SystemMesh.NumberVertices;
            numSeaFloorFaces    = SeaFloorMesh.SystemMesh.NumberFaces;

            // Create the dolphin and seafloor vertex and index buffers
            dolphinVertexBuffer1 = new VertexBuffer(typeof(Vertex), numDolphinVertices, device, Usage.WriteOnly, 0, Pool.Managed);
            dolphinVertexBuffer2 = new VertexBuffer(typeof(Vertex), numDolphinVertices, device, Usage.WriteOnly, 0, Pool.Managed);
            dolphinVertexBuffer3 = new VertexBuffer(typeof(Vertex), numDolphinVertices, device, Usage.WriteOnly, 0, Pool.Managed);
            seaFloorVertexBuffer = new VertexBuffer(typeof(Vertex), numSeaFloorVertices, device, Usage.WriteOnly, 0, Pool.Managed);
            dolphinIndexBuffer   = new IndexBuffer(typeof(short), numDolphinFaces * 3, device, Usage.WriteOnly, Pool.Managed);
            seaFloorIndexBuffer  = new IndexBuffer(typeof(short), numSeaFloorFaces * 3, device, Usage.WriteOnly, Pool.Managed);

            // Copy vertices for mesh 01
            pMeshSourceVB = DolphinMesh01.SystemMesh.VertexBuffer;
            dst           = dolphinVertexBuffer1.Lock(0, DXHelp.GetTypeSize(typeof(Vertex)) * numDolphinVertices, 0);
            src           = (Vertex[])pMeshSourceVB.Lock(0, typeof(Vertex), 0, numDolphinVertices);
            dst.Write(src);
            dolphinVertexBuffer1.Unlock();
            pMeshSourceVB.Unlock();
            pMeshSourceVB.Dispose();

            // Copy vertices for mesh 2
            pMeshSourceVB = DolphinMesh02.SystemMesh.VertexBuffer;
            dst           = dolphinVertexBuffer2.Lock(0, DXHelp.GetTypeSize(typeof(Vertex)) * numDolphinVertices, 0);
            src           = (Vertex[])pMeshSourceVB.Lock(0, typeof(Vertex), 0, numDolphinVertices);
            dst.Write(src);
            dolphinVertexBuffer2.Unlock();
            pMeshSourceVB.Unlock();
            pMeshSourceVB.Dispose();

            // Copy vertices for mesh 3
            pMeshSourceVB = DolphinMesh03.SystemMesh.VertexBuffer;
            dst           = dolphinVertexBuffer3.Lock(0, DXHelp.GetTypeSize(typeof(Vertex)) * numDolphinVertices, 0);
            src           = (Vertex[])pMeshSourceVB.Lock(0, typeof(Vertex), 0, numDolphinVertices);
            dst.Write(src);
            dolphinVertexBuffer3.Unlock();
            pMeshSourceVB.Unlock();
            pMeshSourceVB.Dispose();

            // Copy vertices for the seafloor mesh, and add some bumpiness
            pMeshSourceVB = SeaFloorMesh.SystemMesh.VertexBuffer;
            dst           = seaFloorVertexBuffer.Lock(0, DXHelp.GetTypeSize(typeof(Vertex)) * numSeaFloorVertices, 0);
            src           = (Vertex[])pMeshSourceVB.Lock(0, typeof(Vertex), 0, numSeaFloorVertices);

            System.Random r = new System.Random();
            for (int i = 0; i < numSeaFloorVertices; i++)
            {
                src[i].p.Y += (r.Next() / (float)int.MaxValue);
                src[i].p.Y += (r.Next() / (float)int.MaxValue);
                src[i].p.Y += (r.Next() / (float)int.MaxValue);
                src[i].tu  *= 10;
                src[i].tv  *= 10;
            }
            dst.Write(src);
            seaFloorVertexBuffer.Unlock();
            pMeshSourceVB.Unlock();
            pMeshSourceVB.Dispose();

            GraphicsStream dstib = null;

            short[] srcib = null;

            // Copy indices for the dolphin mesh
            pMeshSourceIB = DolphinMesh01.SystemMesh.IndexBuffer;
            dstib         = dolphinIndexBuffer.Lock(0, DXHelp.GetTypeSize(typeof(short)) * numDolphinFaces * 3, 0);
            srcib         = (short[])pMeshSourceIB.Lock(0, typeof(short), 0, numDolphinFaces * 3);
            dstib.Write(srcib);
            dolphinIndexBuffer.Unlock();
            pMeshSourceIB.Unlock();
            pMeshSourceIB.Dispose();

            // Copy indices for the seafloor mesh
            pMeshSourceIB = SeaFloorMesh.SystemMesh.IndexBuffer;
            dstib         = seaFloorIndexBuffer.Lock(0, DXHelp.GetTypeSize(typeof(short)) * numSeaFloorFaces * 3, 0);
            srcib         = (short[])pMeshSourceIB.Lock(0, typeof(short), 0, numSeaFloorFaces * 3);
            dstib.Write(srcib);
            seaFloorIndexBuffer.Unlock();
            pMeshSourceIB.Unlock();
            pMeshSourceIB.Dispose();
        }
Beispiel #16
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            drawingFont.InitializeDeviceObjects(device);
            // Load an object to render
            try
            {
                if (blendObject == null)
                {
                    blendObject = new GraphicsMesh();
                }

                blendObject.Create(device, "mslogo.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }


            if ((BehaviorFlags.HardwareVertexProcessing || BehaviorFlags.MixedVertexProcessing) &&
                Caps.VertexShaderVersion.Major < 1)
            {
                // No VS available, so don't try to use it or allow user to
                // switch to it
                useShader        = false;
                mnuUseVS.Enabled = false;
            }
            else if (Caps.MaxVertexBlendMatrices < 2)
            {
                // No blend matrices available, so don't try to use them or
                // allow user to switch to them
                useShader        = true;
                mnuUseVS.Enabled = false;
            }
            else
            {
                // Both techniques available, so default to blend matrices and
                // allow the user to switch techniques
                useShader        = false;
                mnuUseVS.Enabled = true;
            }

            // Set a custom FVF for the mesh
            blendObject.SetVertexFormat(device, BlendVertex.Format);

            // Add blending weights to the mesh
            // Gain acces to the mesh's vertices
            VertexBuffer vb = null;

            BlendVertex[] vertices    = null;
            int           numVertices = blendObject.SystemMesh.NumberVertices;

            vb       = blendObject.SystemMesh.VertexBuffer;
            vertices = (BlendVertex[])vb.Lock(0, typeof(BlendVertex), 0, numVertices);

            // Calculate the min/max z values for all the vertices
            float fMinX = 1e10f;
            float fMaxX = -1e10f;

            for (int i = 0; i < numVertices; i++)
            {
                if (vertices[i].v.X < fMinX)
                {
                    fMinX = vertices[i].v.X;
                }
                if (vertices[i].v.X > fMaxX)
                {
                    fMaxX = vertices[i].v.X;
                }
            }

            for (int i = 0; i < numVertices; i++)
            {
                // Set the blend factors for the vertices
                float a = (vertices[i].v.X - fMinX) / (fMaxX - fMinX);
                vertices[i].blend = 1.0f - (float)Math.Sin(a * (float)Math.PI * 1.0f);
            }

            // Done with the mesh's vertex buffer data
            vb.Unlock();
            vb.Dispose();
        }
Beispiel #17
0
        /// <summary>
        /// The device has been created.  Resources that are not lost on
        /// Reset() can be created here -- resources in Pool.Managed,
        /// Pool.Scratch, or Pool.SystemMemory.  Image surfaces created via
        /// CreateImageSurface are never lost and can be created here.  Vertex
        /// shaders and pixel shaders can also be created here as they are not
        /// lost on Reset().
        /// </summary>
        protected override void InitializeDeviceObjects()
        {
            // Initialize the font's internal textures
            drawingFont.InitializeDeviceObjects(device);

            // Load the main file object
            if (helicopterMesh == null)
            {
                helicopterMesh = new GraphicsMesh();
            }

            try
            {
                helicopterMesh.Create(device, "Heli.x");

                // Load the terrain
                if (terrainMesh == null)
                {
                    terrainMesh = new GraphicsMesh();
                }

                terrainMesh.Create(device, "SeaFloor.x");
            }
            catch
            {
                SampleException e = new MediaNotFoundException();
                HandleSampleException(e, ApplicationMessage.ApplicationMustExit);
                throw e;
            }

            // Tweak the terrain vertices to add some bumpy terrain
            if (terrainMesh != null)
            {
                // Set FVF to VertexFVF
                terrainMesh.SetVertexFormat(device, MeshVertex.Format);

                // Get access to the mesh vertices
                VertexBuffer tempVertexBuffer = null;
                MeshVertex[] vertices         = null;
                int          numVertices      = terrainMesh.SystemMesh.NumberVertices;
                tempVertexBuffer = terrainMesh.SystemMesh.VertexBuffer;
                vertices         = (MeshVertex[])tempVertexBuffer.Lock(0, typeof(MeshVertex), 0, numVertices);

                for (int i = 0; i < numVertices; i++)
                {
                    Vector3 v00 = new Vector3(vertices[i].p.X + 0.0f, 0.0f, vertices[i].p.Z + 0.0f);
                    Vector3 v10 = new Vector3(vertices[i].p.X + 0.1f, 0.0f, vertices[i].p.Z + 0.0f);
                    Vector3 v01 = new Vector3(vertices[i].p.X + 0.0f, 0.0f, vertices[i].p.Z + 0.1f);
                    v00.Y = HeightField(1 * v00.X, 1 * v00.Z);
                    v10.Y = HeightField(1 * v10.X, 1 * v10.Z);
                    v01.Y = HeightField(1 * v01.X, 1 * v01.Z);

                    Vector3 n = Vector3.Cross((v01 - v00), (v10 - v00));
                    n.Normalize();

                    vertices[i].p.Y = v00.Y;
                    vertices[i].n.X = n.X;
                    vertices[i].n.Y = n.Y;
                    vertices[i].n.Z = n.Z;
                    vertices[i].tu *= 10;
                    vertices[i].tv *= 10;
                }

                tempVertexBuffer.Unlock();
            }

            if (mirrorVertexBuffer == null)
            {
                // Create a big square for rendering the mirror, we don't need to recreate this every time, if the VertexBuffer
                // is destroyed (by a call to Reset for example), it will automatically be recreated and the 'Created' event fired.
                mirrorVertexBuffer          = new VertexBuffer(typeof(MirrorVertex), 4, device, Usage.WriteOnly, MirrorVertex.Format, Pool.Default);
                mirrorVertexBuffer.Created += new System.EventHandler(this.MirrorCreated);
                // Manually fire the created event the first time
                this.MirrorCreated(mirrorVertexBuffer, null);
            }
        }