public bool Initialze(DDX11 D3D, IntPtr windowHandle, DSystemConfiguration configuration) { // Create the user interface object. UserInterface = new DUserInterface(); // Initialize the user interface object. if (!UserInterface.Initialize(D3D, configuration)) { return(false); } // Create the camera object Camera = new DCamera(); // Initialize a base view matrix with the camera for 2D user interface rendering. Camera.SetPosition(0.0f, 0.0f, -10.0f); Camera.Render(); Camera.RenderBaseViewMatrix(); // Create the position object. Position = new DPosition(); // Set the initial position and rotation of the viewer.28.0f, 5.0f, -10.0f Position.SetPosition(512.0f, 30.0f, 1034.0f); Position.SetRotation(0.0f, 180.0f, 0.0f); // Create the light object. Light = new DLight(); // Initialize the light object. Light.SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); Light.Direction = new Vector3(-0.5f, -1.0f, -0.5f); // Create and initialize the frustum object. Frustum = new DFrustum(); Frustum.Initialize(DSystemConfiguration.ScreenDepth); // Create the sky dome object. SkyDomeModel = new DSkyDome(); // Initialize the sky dome object. if (!SkyDomeModel.Initialize(D3D.Device, "skydome.txt")) { return(false); } // Initialize the terrain object. Terrain = new DTerrain(); // Initialize the ground model object. if (!Terrain.Initialize(D3D.Device, "setupS2TutTerr08.txt")) { return(false); } // Set the UI to display by default. DisplayUI = true; // Set wire frame rendering initially to enabled. WireFrame = false; // Set the rendering of cell lines initially to enabled. CellLines = true; return(true); }
public void Render(DeviceContext deviceContext, DFrustum frustum, DTerrainShader terrainShader) { // Reset the number of the triangles that are drawn for this frame. DrawCount = 0; // Render each node that is visible at the parent node and moving down the tree. RenderNode(deviceContext, ParentNode, frustum, terrainShader); }
private void RenderNode(DeviceContext deviceContext, DNodeType node, DFrustum frustum, DTerrainShader terrainShader) { // Check to see if the node can be viewed, height doesn't matter in a quad tree. // If it can't be seen then none of its children can either, so don't continue down the tree, this is where the speed is gained. if (!frustum.CheckCube(new SharpDX.Vector3(node.positionX, 0.0f, node.positionZ), (node.width / 2.0f))) // 63.7506409 { return; } // If it can be seen then check all four child nodes to see if they can also be seen. int count = 0; for (int i = 0; i < 4; i++) { // parentNode.width > 0.0f && parentNode.Nodes[i].VertexBuffer != null if (node.Nodes[i].width > 0.0f) //node.Nodes != null/ parentNode.Nodes[i].width > 0.0f { count++; RenderNode(deviceContext, node.Nodes[i], frustum, terrainShader); } } // If there were any children nodes then there is no need to continue as parent nodes won't contain any triangles to render. if (count != 0) { return; } int vertexCount = node.TriangleCount * 3; // Otherwise if this node can be seen and has triangles in it then render these triangles. // Set vertex buffer stride and offset. // Set the vertex buffer to active in the input assembler so it can be rendered. deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(node.VertexBuffer, Utilities.SizeOf <DTerrain.DVertexType>(), 0)); // Set the index buffer to active in the input assembler so it can be rendered. deviceContext.InputAssembler.SetIndexBuffer(node.IndexBuffer, Format.R32_UInt, 0); // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles. deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; // Determine the number of indices in this node. int indexCount = node.TriangleCount * 3; // Call the terrain shader to render the polygons in this node. terrainShader.RenderShader(deviceContext, indexCount); // Increase the count of the number of polygons that have been rendered during this frame. DrawCount += node.TriangleCount; }
public bool RenderCell(DeviceContext deviceContext, int cellID, DFrustum frustum) { // Check if the cell is visible. If it is not visible then just return and don't render it. if (!frustum.CheckRectangle2(TerrainCells[cellID].m_maxWidth, TerrainCells[cellID].m_maxHeight, TerrainCells[cellID].m_maxDepth, TerrainCells[cellID].m_minWidth, TerrainCells[cellID].m_minHeight, TerrainCells[cellID].m_minDepth)) { // Increment the number of cells that were culled. m_cellsCulled++; return(false); } // If it is visible then render it. TerrainCells[cellID].Render(deviceContext); // Add the polygons in the cell to the render count. m_renderCount += (TerrainCells[cellID].VertexCount / 3); // Increment the number of cells that were actually drawn. m_cellsDrawn++; return(true); }
// Methods. public bool Initialize(DSystemConfiguration configuration, IntPtr windowHandle) { try { // Create the input object. The input object will be used to handle reading the keyboard and mouse input from the user. Input = new DInput(); // Initialize the input object. if (!Input.Initialize(configuration, windowHandle)) { return(false); } // Create the Direct3D object. D3D = new DDX11(); // Initialize the Direct3D object. if (!D3D.Initialize(configuration, windowHandle)) { return(false); } // Create the camera object Camera = new DCamera(); // Initialize a base view matrix with the camera for 2D user interface rendering. Camera.SetPosition(0.0f, 0.0f, -1.0f); Camera.Render(); Matrix baseViewMatrix = Camera.ViewMatrix; // Set the initial position of the camera. (Since the ViewMatrix is already created from a base position.) Camera.SetPosition(50.0f, 18.0f, 7.0f); // Create the model object. TerrainModel = new DTerrain(); // Initialize the terrain object. if (!TerrainModel.Initialize(D3D.Device, "heightMap01.bmp", "dirt03.bmp")) { return(false); } // Create the position object. Position = new DPosition(); // Set the initial position of the viewer to the same as the initial camera position. Position.SetPosition(Camera.GetPosition().X, Camera.GetPosition().Y, Camera.GetPosition().Z); // Position.SetRotation(0.0f, 249.067368f, 0.0f); // Create the fps object. FPS = new DFPS(); // Initialize the fps object. FPS.Initialize(); // Create the cpu object. CPU = new DCPU(); // Initialize the cpu object. CPU.Initialize(); // Create the text object. Text = new DText(); // Initialize the text object. if (!Text.Initialize(D3D.Device, D3D.DeviceContext, windowHandle, configuration.Width, configuration.Height, baseViewMatrix)) { return(false); } // Set the video card information in the text object. if (!Text.SetVideoCard(D3D.VideoCardDescription, D3D.VideoCardMemory, D3D.DeviceContext)) { return(false); } // Create the color shader object.HeightMapTerrainShader TerrainShader = new DTerrainShader(); // Initialize the color shader object. if (!TerrainShader.Initialize(D3D.Device, windowHandle)) { return(false); } // Create the light object. Light = new DLight(); // Initialize the light object. Light.SetAmbientColor(0.05f, 0.05f, 0.05f, 1.0f); Light.SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); Light.Direction = new Vector3(-0.5f, -1.0f, 0.0f); // Create the frustum object. Frustum = new DFrustum(); // Create the quad tree object. QuadTree = new DQuadTree(); // Initialize the quad tree object. if (!QuadTree.Initialize(TerrainModel, D3D.Device)) { return(false); } return(true); } catch (Exception ex) { MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'"); return(false); } }
// Methods. public bool Initialize(DSystemConfiguration configuration, IntPtr windowHandle) { try { // Create the Direct3D object. D3D = new DDX11(); // Initialize the Direct3D object. if (!D3D.Initialize(configuration, windowHandle)) { return(false); } // Create the camera object Camera = new DCamera(); // Initialize a base view matrix the camera for 2D user interface rendering. Camera.SetPosition(0, 0, -1); Camera.Render(); var baseViewMatrix = Camera.ViewMatrix; // Create the text object. Text = new DTextClass(); if (!Text.Initialize(D3D.Device, D3D.DeviceContext, windowHandle, configuration.Width, configuration.Height, baseViewMatrix)) { return(false); } // Create the model class. Model = new DModel(); // Initialize the model object. if (!Model.Initialize(D3D.Device, "sphere.txt", "seafloor.bmp")) { MessageBox.Show("Could not initialize the model object", "Error", MessageBoxButtons.OK); return(false); } // Create the light shader object. LightShader = new DLightShader(); // Initialize the light shader object. if (!LightShader.Initialize(D3D.Device, windowHandle)) { MessageBox.Show("Could not initialize the light shader", "Error", MessageBoxButtons.OK); return(false); } // Create the light object. Light = new DLight(); // Initialize the light object. Light.SetDirection(0.0f, 0.0f, 1.0f); // Create the model list object. ModelList = new DModelList(); // Initialize the model list object. if (!ModelList.Initialize(25)) { MessageBox.Show("Could not initialize the model list object", "Error", MessageBoxButtons.OK); return(false); } // Create the frustum object. Frustum = new DFrustum(); return(true); } catch (Exception ex) { MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'"); return(false); } }