/// <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; } }
/// <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); }
/// <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); }
/// <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(); } }
/// <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); } }
/// <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(); }
/// <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(); }