Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public bool Render(DeviceContext deviceContext, DTerrainShader shader, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Vector4 ambiantColour, Vector4 diffuseColour, Vector3 lightDirection)
        {
            // Set vertex buffer stride and offset.
            // Set the shader parameters that it will use for rendering.
            if (!shader.SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, ambiantColour, diffuseColour, lightDirection))
            {
                return(false);
            }

            // Set the type of primitive that should be rendered from the vertex buffers, in this case triangles.
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            // Render each material group.
            for (int i = 0; i < m_MaterialCount; i++)
            {
                // Set the vertex buffer to active in the input assembler so it can be rendered.
                deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(Materials[i].vertexBuffer, Utilities.SizeOf <DVertexType>(), 0));

                // Set the index buffer to active in the input assembler so it can be rendered.
                deviceContext.InputAssembler.SetIndexBuffer(Materials[i].indexBuffer, Format.R32_UInt, 0);

                // If the material group has a valid second texture index then this is a blended terrain polygon.
                bool result;
                if (Materials[i].textureIndex2 != -1)
                {
                    result = shader.SetShaderTextures(deviceContext, Textures[Materials[i].textureIndex1].TextureResource, Textures[Materials[i].textureIndex2].TextureResource, Textures[Materials[i].alphaIndex].TextureResource, false);
                }
                else  // If not then it is just a single textured polygon.
                {
                    result = shader.SetShaderTextures(deviceContext, Textures[Materials[i].textureIndex1].TextureResource, null, null, false);
                }

                // Check if the textures were set or not.
                if (!result)
                {
                    return(false);
                }

                // Now render the prepared buffers with the shader.
                shader.RenderShader(deviceContext, Materials[i].indexCount);
            }

            return(true);
        }
Ejemplo n.º 3
0
        // 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);
                }

                // #region Initialize System
                // 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, 2.0f, -7.0f);

                // Create the model object.
                TerrainModel = new DTerrain();

                // Initialize the terrain object.
                if (!TerrainModel.Initialize(D3D.Device, "heightmap01.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);

                // 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 font shader object.
                FontShader = new DFontShader();

                // Initialize the font shader object.
                if (!FontShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // 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.
                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.0f, 0.0f, 0.75f);

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

                // #region Initialize System
                // Create the Direct3D object.
                D3D = new DDX11();

                // Initialize the Direct3D object.
                if (!D3D.Initialize(configuration, windowHandle))
                {
                    return(false);
                }

                // Create the position object.
                Position = new DPosition();

                // Set the initial position and rotation of the viewer.
                Position.SetPosition(280.379f, 24.5225f, 367.018f);
                Position.SetRotation(19.6834f, 222.013f, 0.0f);

                // 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.RenderBaseViewMatrix();
                Matrix baseViewMatrix = Camera.BaseViewMatrix;

                // 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, -0.75f, 0.25f);

                // Create the model object.
                TerrainModel = new DTerrainHeightMap();

                // Initialize the terrain object.
                if (!TerrainModel.Initialize(D3D.Device, "hm.bmp", "cm.bmp", 20.0f, "dirt04.bmp", "normal01.bmp"))
                {
                    return(false);
                }

                // Create the color shader object.
                TerrainShader = new DTerrainShader();

                //// Initialize the color shader object.
                if (!TerrainShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // Create the sky dome object.
                SkyDome = new DSkyDome();

                // Initialize the sky dome object.
                if (!SkyDome.Initialize(D3D.Device))
                {
                    return(false);
                }

                // Create the sky dome shader object.
                SkyDomeShader = new DSkyDomeShader();

                // Initialize the sky dome shader object.
                if (!SkyDomeShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // Create the sky plane object.
                SkyPlane = new DSkyPlane();

                // Initialize the sky plane object.
                if (!SkyPlane.Initialze(D3D.Device, "cloud001.bmp", "perturb001.bmp"))
                {
                    return(false);
                }

                // Create the sky plane shader object.
                SkyPlaneShader = new DSkyPlaneShader();

                // Initialize the sky plane shader object.
                if (!SkyPlaneShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // 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 refraction render to texture object.
                RefractionTexture = new DRenderTexture();

                // Initialize the refraction render to texture object.
                if (!RefractionTexture.Initialize(D3D.Device, configuration))
                {
                    return(false);
                }

                // Create the reflection render to texture object.
                ReflectionTexture = new DRenderTexture();

                // Initialize the reflection render to texture object.
                if (!ReflectionTexture.Initialize(D3D.Device, configuration))
                {
                    return(false);
                }

                // Create the reflection shader object.
                ReflectionShader = new DReflectionShader();

                // Initialize the reflection shader object.
                if (!ReflectionShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // Create the water object.
                WaterModel = new DWater();

                // Initialize the water object.
                if (!WaterModel.Initilize(D3D.Device, "waternormal.bmp", 3.75f, 110.0f))
                {
                    return(false);
                }

                // Create the water shader object.
                WaterShader = new DWaterShader();

                // Initialize the water shader object.
                if (!WaterShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'");
                return(false);
            }
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        // 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, -10.0f);
                Camera.RenderBaseViewMatrix();
                Matrix baseViewMatrix = Camera.BaseViewMatrix;

                // Set the initial position of the camera.
                Camera.SetPosition(100.0f, 2.0f, 5.0f);

                // Create the model object.
                TerrainModel = new DTerrain();

                // Initialize the terrain object.
                if (!TerrainModel.Initialize(D3D.Device, "heightmap01.bmp", "dirt02.bmp", "colorm01.bmp", "detail001.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);

                // 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 terrain shader object.
                TerrainShader = new DTerrainShader();

                // Initialize the terrain 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 debug window bitmap object.
                DebugWindow = new DDebugWindow();

                // Initialize the debug window bitmap object.
                if (!DebugWindow.Initialize(D3D.Device, configuration.Width, configuration.Height, 256, 256))
                {
                    return(false);
                }

                // Create the texture shader object.
                TextureShader = new DTextureShader();

                // Initialize the texture shader object.
                if (!TextureShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // Create the render to texture object.
                RenderTexture = new DRenderTexture();

                // Initialize the render to texture object.
                if (!RenderTexture.Initialize(D3D.Device, configuration))
                {
                    return(false);
                }

                // Create the depth shader object.
                DepthShader = new DDepthShader();

                // Initialize the depth shader object.
                if (!DepthShader.Initialize(D3D.Device, windowHandle))
                {
                    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 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);
                }

                // #region Initialize System
                // Create the Direct3D object.
                D3D = new DDX11();

                // Initialize the Direct3D object.
                if (!D3D.Initialize(configuration, windowHandle))
                {
                    return(false);
                }

                // Create the position object.
                Position = new DPosition();

                // Set the initial position and rotation of the viewer.
                Position.SetPosition(15.0f, 13.0f, 20.0f);
                Position.SetRotation(25.0f, 180.0f, 0.0f);

                // Create the camera object
                Camera = new DCamera();

                // Create the light object.
                Light = new DLight();

                // Initialize the light object.
                Light.Direction = new Vector3(0.5f, -0.75f, 0.0f);

                // Create the model object.
                TerrainModel = new DTerrainHeightMap();

                // Initialize the terrain object.
                if (!TerrainModel.Initialize(D3D.Device, "hm01.bmp", 10.0f))
                {
                    return(false);
                }

                // Create the color shader object.
                TerrainShader = new DTerrainShader();

                //// Initialize the color shader object.
                if (!TerrainShader.Initialize(D3D.Device, windowHandle))
                {
                    return(false);
                }

                // Create the first color texture object.
                ColourTexture1 = new DTexture();

                // Load the first color texture object.
                if (!ColourTexture1.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "dirt001.bmp"))
                {
                    return(false);
                }

                // Create the second color texture object.
                ColourTexture2 = new DTexture();

                // Load the second color texture object.
                if (!ColourTexture2.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "dirt004.bmp"))
                {
                    return(false);
                }

                // Create the third color texture object.
                ColourTexture3 = new DTexture();

                // Load the third color texture object.
                if (!ColourTexture3.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "dirt002.bmp"))
                {
                    return(false);
                }

                // Create the fourth color texture object.
                ColourTexture4 = new DTexture();

                // Load the forth color texture object.
                if (!ColourTexture4.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "stone001.bmp"))
                {
                    return(false);
                }

                // Create the first alpha texture object.
                AlphaTexture1 = new DTexture();

                // Load the first alpha texture object.
                if (!AlphaTexture1.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "alphaRoad001.bmp"))
                {
                    return(false);
                }

                // Create the first normal texture object.
                NormalTexture1 = new DTexture();

                // Load the first alpha/Normal texture object.
                if (!NormalTexture1.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "normal001.bmp"))
                {
                    return(false);
                }

                // Create the second normal texture object.
                NormalTexture2 = new DTexture();

                // Load the second alpha/Normal texture object.
                if (!NormalTexture2.Initialize(D3D.Device, DSystemConfiguration.DataFilePath + "normal002.bmp"))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'");
                return(false);
            }
        }