/// <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(); } } }
/// <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() { 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() { 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; } }
/// <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(); }
/// <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; } }
public Cloth(string name, string texture_name, int rows, int columns, double spacing, float mass) : base(name) { try { num_rows = rows; num_columns = columns; m_Texture = GraphicsUtility.CreateTexture(CGameEngine.Device3D, texture_name); nodes = new Node[rows + 1, columns + 1]; num_faces = rows * columns * 2; num_springs = columns * (rows + 1) + rows * (columns + 1) + columns * rows * 2; springs = new Spring[num_springs]; wind.X = 0.0f; wind.Y = 0.0f; wind.Z = 0.0f; num_nodes = (rows + 1) * (columns + 1); m_vertices = new CustomVertex.PositionNormalTextured[num_nodes]; float mass_per_node = mass / num_nodes; for (int r = 0; r <= rows; r++) { for (int c = 0; c <= columns; c++) { nodes[r, c] = new Node(mass_per_node, -(c * spacing), -(r * spacing), 0.0, c == 0 && (r == 0 || r == rows)); } } // Create a buffer for rendering the cloth m_VB = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), num_nodes, CGameEngine.Device3D, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default); m_IB = new IndexBuffer(typeof(short), num_faces * 3, CGameEngine.Device3D, Usage.WriteOnly, Pool.Managed); indices = new short[num_faces * 3]; m_IB.Created += new System.EventHandler(this.PopulateIndexBuffer); this.PopulateIndexBuffer(m_IB, null); m_VB.Created += new System.EventHandler(this.PopulateBuffer); this.PopulateBuffer(m_VB, null); // create the springs int index = 0; for (int r = 0; r <= rows; r++) { for (int c = 0; c <= columns; c++) { if (c < columns) { springs[index].node1.row = r; springs[index].node1.column = c; springs[index].node2.row = r; springs[index].node2.column = c + 1; springs[index].spring_constant = spring_tension; springs[index].damping = damping; Vector3 length = nodes[r, c].position - nodes[r, c + 1].position; springs[index].length = length.Length(); index++; } if (r < rows) { springs[index].node1.row = r; springs[index].node1.column = c; springs[index].node2.row = r + 1; springs[index].node2.column = c; springs[index].spring_constant = spring_tension; springs[index].damping = damping; Vector3 length = nodes[r, c].position - nodes[r + 1, c].position; springs[index].length = length.Length(); index++; } if (r < (rows - 1) && c < (columns - 1)) { springs[index].node1.row = r; springs[index].node1.column = c; springs[index].node2.row = r + 1; springs[index].node2.column = c + 1; springs[index].spring_constant = spring_tension; springs[index].damping = damping; Vector3 length = nodes[r, c].position - nodes[r + 1, c + 1].position; springs[index].length = length.Length(); index++; } if (r < (rows - 1) && c > 0) { springs[index].node1.row = r; springs[index].node1.column = c; springs[index].node2.row = r + 1; springs[index].node2.column = c - 1; springs[index].spring_constant = spring_tension; springs[index].damping = damping; Vector3 length = nodes[r, c].position - nodes[r + 1, c - 1].position; springs[index].length = length.Length(); index++; } } } m_physics_thread = new Thread(new ThreadStart(DoPhysics)); m_physics_thread.Start(); m_bValid = true; } catch (DirectXException d3de) { Console.AddLine("Unable to create cloth for " + name); Console.AddLine(d3de.ErrorString); } catch (Exception e) { Console.AddLine("Unable to create cloth for " + name); Console.AddLine(e.Message); } }
/// <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(); }
/// <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; } }