private bool RenderReflectionScene() { // Use the camera to render the reflection and create a reflection view matrix. Camera.RenderReflection(WaterHeight); // Get the camera reflection view matrix instead of the normal view matrix. var viewMatrix = Camera.ReflectionViewMatrix; // Get the world and projection matrices from the d3d object. var worldMatrix = D3D.WorldMatrix; var projectionMatrix = D3D.ProjectionMatrix; // Translate to where the bath model will be rendered. Matrix.Translation(0f, 6f, 8f, out worldMatrix); // Put the wall model vertex and index buffers on the graphics pipeline to prepare them for drawing. WallModel.Render(D3D.DeviceContext); // Render the wall model using the light shader and the reflection view matrix. if (!LightShader.Render(D3D.DeviceContext, WallModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, WallModel.TextureCollection.Select(item => item.TextureResource).First(), Light.Direction, Light.AmbientColor, Light.DiffuseColour, Camera.GetPosition(), Light.SpecularColor, Light.SpecularPower)) { return(false); } return(true); }
public override void MapLoaded(TeeEngine engine, TiledMap map, MapEventArgs mapEventArgs) { LightShader lightShader = (LightShader)engine.GetPostGameShader("LightShader"); lightShader.Enabled = false; Random random = new Random(); for (int i = 0; i < 50; i++) { int px = (int)Math.Ceiling(random.NextDouble() * engine.Map.pxWidth); int py = (int)Math.Ceiling(random.NextDouble() * engine.Map.pxHeight); Bat bat = new Bat(px, py); Coin coin = new Coin(px, py, 100, (CoinType)random.Next(3)); // Switch between adding bats and coins to the map. if (i % 2 == 0) { engine.AddEntity(bat); } else { engine.AddEntity(coin); } } base.MapLoaded(engine, map, mapEventArgs); }
public void Shutdown() { // Release the light object. Light = null; // Release the camera object. Camera = null; // Release the light shader object. LightShader?.ShutDown(); LightShader = null; // Release the deferred shader object. DeferredShader?.ShutDown(); DeferredShader = null; // Release the deferred buffers object. DeferredBuffers?.Shutdown(); DeferredBuffers = null; // Release the full screen ortho window object. FullScreenWindow?.Shutdown(); FullScreenWindow = null; // Release the tree object. Model?.Shutdown(); Model = null; // Release the input object. Input?.Shutdown(); Input = null; // Release the Direct3D object. D3D?.ShutDown(); D3D = null; }
public void Shutdown() { // Release the light object. Light = null; // Release the camera object. Camera = null; // Release the texture shader object. TextureShader?.ShutDown(); TextureShader = null; // Release the debug window object. DebugWindow?.Shutdown(); DebugWindow = null; // Release the render to texture object. RenderTexture?.Shutdown(); RenderTexture = null; // Release the light shader object. LightShader?.ShutDown(); LightShader = null; // Release the model object. Model?.Shutdown(); Model = null; // Release the Direct3D object. D3D?.ShutDown(); D3D = null; }
private bool RenderScene() { // Generate the view matrix based on the camera position. Camera.Render(); // Get the world, view, and projection matrices from camera and d3d objects. var viewMatrix = Camera.ViewMatrix; var worldMatrix = D3D.WorldMatrix; var projectionMatrix = D3D.ProjectionMatrix; // Rotate the world matrix by the rotation value so that the triangle will spin. Rotate(); // Rotate the world matrix by the rotation value so that the triangle will spin. Matrix.RotationY(Rotation, out worldMatrix); // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. Model.Render(D3D.DeviceContext); // Render the model using the color shader. if (!LightShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.TextureCollection.Select(item => item.TextureResource).First(), Light.Direction, Light.DiffuseColour)) { return(false); } return(true); }
protected override void LoadContent() { ItemRepository.LoadRepositoryXml("Items/ItemRepository.xml", Content); LightShader = new LightShader(this.GraphicsDevice, CIRCLE_POINT_ACCURACY); LightShader.AmbientLight = new Color(30, 15, 15); LightShader.Enabled = false; Engine.RegisterGameShader("LightShader", LightShader); MapEventArgs mapArgs = new MapEventArgs(); mapArgs.SetProperty("Target", "CaveExit"); Engine.LoadMap("Content/Maps/cave_example.tmx", mapArgs); CurrentSampler = SamplerStates[SamplerIndex]; Engine.DrawingOptions.ShowEntityDebugInfo = false; Engine.DrawingOptions.ShowBoundingBoxes = false; Engine.DrawingOptions.ShowTileGrid = false; Engine.LoadContent(); SpriteBatch = new SpriteBatch(GraphicsDevice); DefaultSpriteFont = Content.Load <SpriteFont>(@"Fonts\DefaultSpriteFont"); }
private bool Render(float rotation) { // Clear the buffer to begin the scene. D3D.BeginScene(0f, 0f, 0f, 1f); // Generate the view matrix based on the camera position. Camera.Render(); // Get the world, view, and projection matrices from camera and d3d objects. Matrix viewMatrix = Camera.ViewMatrix; Matrix worldMatrix = D3D.WorldMatrix; Matrix projectionMatrix = D3D.ProjectionMatrix; // Rotate the world matrix by the rotation value so that the triangle will spin. Matrix.RotationY(rotation, out worldMatrix); // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. Model.Render(D3D.DeviceContext); // Render the model using the color shader. if (!LightShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.Texture.TextureResource, Light.Direction, Light.DiffuseColour)) { return(false); } // Present the rendered scene to the screen. D3D.EndScene(); return(true); }
public override RenderableLightPrimitive GetRenderablePrimitive() { LightShader shader = new LightShader(Renderer); using (ShaderBytecode bytecode = new ShaderBytecode(new DataStream(File.ReadAllBytes("Shaders\\DeferredPointLight.vs"), true, false))) { shader.VertexShader = new VertexShader(Renderer.Device, bytecode); shader.InputLayout = new InputLayout(Renderer.Device, bytecode, new[] { new InputElement("Position", 0, Format.R32G32B32_Float, sizeof(float) * 0, 0), }); } using (ShaderBytecode bytecode = new ShaderBytecode(new DataStream(File.ReadAllBytes("Shaders\\DeferredPointLightShadowless.ps"), true, false))) { shader.PixelShader = new PixelShader(Renderer.Device, bytecode); } using (ShaderBytecode bytecode = new ShaderBytecode(new DataStream(File.ReadAllBytes("Shaders\\DeferredPointLightShadowless.ps"), true, false))) { shader.PixelShaderShadowless = new PixelShader(Renderer.Device, bytecode); } shader.Topology = PrimitiveTopology.TriangleList; ConstantBufferWrapper MatricesCBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 32, ShaderType.VertexShader, 0); ConstantBufferWrapper LightCBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 64, ShaderType.PixelShader, 0); ConstantBufferWrapper ShadowCBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 16 * 4, ShaderType.PixelShader, 1); MatricesCBuffer.Semantics.Add(Semantic.WorldViewProj); MatricesCBuffer.Semantics.Add(Semantic.World); LightCBuffer.Semantics.Add(Semantic.View); LightCBuffer.Semantics.Add(Semantic.ViewInverse); LightCBuffer.Semantics.Add(Semantic.ViewProjInverse); LightCBuffer.Semantics.Add(Semantic.CameraPosition); shader.ConstantBuffers.Add(MatricesCBuffer); shader.ConstantBuffers.Add(LightCBuffer); shader.ConstantBuffers.Add(ShadowCBuffer); GeometricPrimitive prim = new SpherePrimitive(Renderer, 1, 16); DataStream str = new DataStream(prim.GeometryData.Positions, true, false); Buffer vertexBuffer = new Buffer(Renderer.Device, str, new BufferDescription() { SizeInBytes = (int)str.Length, BindFlags = BindFlags.VertexBuffer, StructureByteStride = 3 * sizeof(float), CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, }); int vertexCount = prim.GeometryData.VertexCount; DataStream IndicesStream = new DataStream(prim.GeometryData.Indices.ToArray(), true, true); int indexCount = prim.GeometryData.IndexCount; Buffer indexBuffer = new Buffer(Renderer.Device, IndicesStream, sizeof(ushort) * indexCount, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, sizeof(ushort)); prim.Dispose(); return new RenderableLightPrimitive(shader, vertexBuffer, vertexCount, indexBuffer, indexCount); }
public override void MapLoaded(TeeEngine engine, TiledMap map, MapEventArgs mapEventArgs) { LightShader lightShader = (LightShader)engine.GetPostGameShader("LightShader"); lightShader.Enabled = true; base.MapLoaded(engine, map, mapEventArgs); }
public RenderableLightPrimitive(LightShader shader, Buffer vertexbuffer, int vertexCount, Buffer indexbuffer, int indexCount) { this.Shader = shader; this.VertexBuffer = vertexbuffer; this.IndexBuffer = indexbuffer; this.VertexCount = vertexCount; this.IndexCount = indexCount; }
public void ShutDown() { // Release the font shader object. FontShader?.Shuddown(); FontShader = null; // Release the light shader object. LightShader?.ShutDown(); LightShader = null; }
public override void PostInitialize(GameTime gameTime, GameEngine.TeeEngine engine) { // Todo: this should technically NOT be here. this.Pos += new Vector2(Width / 2.0f, Height / 2.0f); LightShader lightShader = (LightShader)engine.GetPostGameShader("LightShader"); lightShader.LightSources.Add(this); }
public bool RenderLightShader(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 ambiant, Vector4 diffuse, Vector3 cameraPosition, Vector4 specular, float specualrPower) { // Render the model using the light shader. if (!LightShader.Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambiant, diffuse, cameraPosition, specular, specualrPower)) { return(false); } return(true); }
public bool RenderLightShader(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 diffuse) { // Render the model using the light shader. if (!LightShader.Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuse)) { return(false); } return(true); }
public void ShutDown() { // Release the bump map shader object. BumpMapShader?.ShutDown(); BumpMapShader = null; // Release the light shader object. LightShader?.ShutDown(); LightShader = null; // Release the texture shader object. TextureShader?.ShutDown(); TextureShader = null; }
public void Shutdown() { // Release the frustum object. Frustum = null; // Release the model list object. if (ModelList != null) { ModelList.Shutdown(); ModelList = null; } // Release the light object. Light = null; // Release the light shader object. if (LightShader != null) { LightShader.Shuddown(); LightShader = null; } // Release the model object. if (Model != null) { Model.Shutdown(); Model = null; } // Release the text object. if (Text != null) { Text.Shutdown(); Text = null; } // Release the camera object. if (Camera != null) { Camera = null; } // Release the Direct3D object. if (D3D != null) { D3D.Shutdown(); D3D = null; } }
public void Dispose() { Timer = null; _light = null; _camera = null; _lightShader.Dispose(); _lightShader = null; _object.Dispose(); _object = null; _directX.Dispose(); _directX = null; }
public void ShutDown() { Timer = null; Light = null; Camera = null; // Release the LightShader and Light objects. LightShader?.ShutDown(); LightShader = null; // Release the model object. Model?.Shutdown(); Model = null; // Release the Direct3D object. D3D?.ShutDown(); D3D = null; }
private bool Render() { // Render the depth of the scene to a texture. if (!RenderSceneToTexture()) { return(false); } // Clear the scene. D3D.BeginScene(0.0f, 0.5f, 0.8f, 1.0f); // Get the matrices. Matrix worldMatrix = D3D.WorldMatrix; Matrix cameraBaseViewMatrix = Camera.BaseViewMatrix; Matrix orthoMatrix = D3D.OrthoMatrix; // Turn off the Z buffer to begin all 2D rendering. D3D.TurnZBufferOff(); // Put the full screen ortho window vertex and index buffers on the graphics pipeline to prepare them for drawing. if (!FullScreenWindow.Render(D3D.DeviceContext)) { return(false); } // Render the full screen ortho window using the deferred light shader and the render buffers. if (!LightShader.Render(D3D.DeviceContext, FullScreenWindow.IndexCount, worldMatrix, cameraBaseViewMatrix, orthoMatrix, DeferredBuffers.ShaderResourceViewArray[0], DeferredBuffers.ShaderResourceViewArray[1], Light.Direction)) { return(false); } // Turn the Z buffer back on now that all 2D rendering has completed. D3D.TurnZBufferOn(); // Present the rendered scene to the screen. D3D.EndScene(); return(true); }
public void Shutdown() { // Release the light object. Light = null; // Release the camera object. Camera = null; // Release the water shader object. WaterShader?.ShutDown(); WaterShader = null; // Release the refraction shader object. RefractionShader?.ShutDown(); RefractionShader = null; /// Release the render to texture object. RenderReflectionTexture?.Shutdown(); RenderReflectionTexture = null; // Release the render to texture object. RenderRefractionTexture?.Shutdown(); RenderRefractionTexture = null; // Release the light shader object. LightShader?.ShutDown(); LightShader = null; // Release the model object. GroundModel?.Shutdown(); GroundModel = null; // Release the model object. WallModel?.Shutdown(); WallModel = null; // Release the model object. BathModel?.Shutdown(); BathModel = null; // Release the model object. WaterModel?.Shutdown(); WaterModel = null; // Release the Direct3D object. D3D?.ShutDown(); D3D = null; }
public Renderer() { CreateWindow(); DirectX = new Dx11(Form); ScreenSize = new Vector2I(ConfigurationManager.Config.Width, ConfigurationManager.Config.Height); Light = new Light { Direction = new Vector3(1.0f, -1.0f, 0.0f), Color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f), AmbiantColor = new Vector4(0.16f, 0.16f, 0.16f, 1.0f), SpecularPower = 32.0f, SpecularColor = new Vector4(1.0f, 1.0f, 0.7f, 1.0f) }; ColorShader = new ColorShader(DirectX.Device); TextureShader = new TextureShader(DirectX.Device); LightShader = new LightShader(DirectX.Device); TranslateShader = new TranslateShader(DirectX.Device); CircleShader = new FontShader(DirectX.Device); FontShader = new FontShader(DirectX.Device); TextManager = new TextManager(DirectX.Device, ConfigurationManager.Config.Width, ConfigurationManager.Config.Height); TextureManager = new TextureManager(DirectX.Device); _renderables = new List <RenderableGameComponent>(); }
public bool Initialise(Dimension size, IntPtr windowHandle) { var result = true; try { _directX = new DirectX(); result &= _directX.Initialise(size, windowHandle); Timer = new Timer(); result &= Timer.Initialise(); _camera = new Camera(); _camera.SetPosition(new Coordinate3D <float>(0, 0, -10)); _object = new Object(); result &= _object.Initialise(_directX.Device, "Cube.txt", "seafloor.bmp"); _lightShader = new LightShader(); result &= _lightShader.Initialise(_directX.Device, windowHandle); _light = new Light() { DiffuseColour = new Vector4(1, 1, 1, 1), Direction = new Vector3(0, 0, 1) }; return(result); } catch (Exception ex) { Log.WriteToFile(ErrorLevel.Error, "Window.Initialise", ex, true); return(false); } }
public void Shutdown() { // Release the frustum object. Frustum = null; // Release the light object. Light = null; Camera = null; // Release the model list object. ModelList?.Shutdown(); ModelList = null; // Release the light shader object. LightShader?.ShutDown(); LightShader = null; // Release the model object. Model?.Shutdown(); Model = null; // Release the text object. Text?.Shutdown(); Text = null; // Release the Direct3D object. D3D?.ShutDown(); D3D = null; }
/// <summary> /// Creates a new deferred renderer. /// </summary> /// <param name="width">Width of the render targets.</param> /// <param name="height">Height of the render targets.</param> public DeferredRenderer(int width, int height) { this.lightCube = Model.CreateCube(1.1f); this.Width = width; this.Height = height; // Create graphics buffer this.PositionBuffer = CreateTexture(width, height); this.NormalBuffer = CreateTexture(width, height); this.DiffuseLightBuffer = CreateTexture(width, height); this.SpecularLightBuffer = CreateTexture(width, height); this.sceneBuffer = CreateTexture(width, height); // Create 3d shaders this.geometryPixelShader = new ShaderFragment(ShaderType.FragmentShader); this.geometryPixelShader.Compile( @"#version 410 core uniform sampler2D meshNormalMap; uniform sampler2D meshDiffuseTexture; uniform float mtlSpecularPower; in vec3 position; in vec3 normal; in vec3 tangent; in vec3 bitangent; in vec2 uv; layout(location = 0) out vec4 positionOut; layout(location = 1) out vec4 normalOut; void main() { if(texture(meshDiffuseTexture, uv).a < 0.5f) discard; positionOut = vec4(position, 1); vec3 bump = normalize(2.0f * texture(meshNormalMap, uv).xyz - 1.0f); normalOut.xyz = mat3(tangent, bitangent, normal) * bump; normalOut.w = mtlSpecularPower; //normalOut.xyz = normal; }"); this.lightShader = new LightShader(); // Create post processing shaders this.gammaCorrectionShader = new GammaCorrectionShader(); this.gammaCorrectionShader.Gamma = 2.2f; this.lightScatteringShader = new LightScatteringShader(); this.lightScatteringShader.OcclusionBuffer = this.NormalBuffer; this.ditheringShader = new DitheringShader(); this.tonemappingShader = new TonemappingShader(); this.tonemappingShader.HdrExposure = 1.25f; this.tonemappingShader.WhitePoint = 5.0f; this.blurShader = new BlurShader(); this.blurShader.BlurStrength = new Vector2(0.04f, 0.04f); this.bloomCombineShader = new CombineShader(); this.highPassShader = new HighPassShader(); this.highPassShader.BloomThreshold = 0.95f; // Create post processing stages this.preBloomStages = new PostProcessingStage(this.lightScatteringShader) { TargetTexture = CreateTexture(width, height), Stage = new PostProcessingStage(this.gammaCorrectionShader) { TargetTexture = CreateTexture(width, height) } }; this.bloomStages = new PostProcessingStage(this.highPassShader) { TargetTexture = CreateTexture(width, height), Stage = new PostProcessingStage(this.blurShader) { TargetTexture = CreateTexture(width / 2, height / 2), Stage = new PostProcessingStage(this.blurShader) { TargetTexture = CreateTexture(width / 2, height / 2), Stage = new PostProcessingStage(this.bloomCombineShader) { TargetTexture = CreateTexture(width, height), } } } }; this.postBloomStages = new PostProcessingStage(this.tonemappingShader) { TargetTexture = CreateTexture(width, height), Stage = new PostProcessingStage(this.ditheringShader) { TargetTexture = CreateTexture(width, height), } }; //this.tonemapping = new PostProcessingStage(this.tonemappingShader); //this.tonemapping.TargetTexture = CreateTexture(width, height); this.depthBuffer = new RenderBuffer(width, height); this.frameBufferGeometry = new FrameBuffer(this.depthBuffer, this.PositionBuffer, this.NormalBuffer); this.frameBufferClearDiffuse = new FrameBuffer(this.DiffuseLightBuffer); this.frameBufferClearSpecular = new FrameBuffer(this.SpecularLightBuffer); this.frameBufferLights = new FrameBuffer(this.depthBuffer, this.DiffuseLightBuffer, this.SpecularLightBuffer); this.frameBufferScene = new FrameBuffer(this.depthBuffer, this.sceneBuffer); this.DefaultShader = new ObjectShader(); }
public bool Initialize(SystemConfiguration configuration, IntPtr windowHandle) { try { // Create the Direct3D object. D3D = new DX11(); // Initialize the Direct3D object. if (!D3D.Initialize(configuration, windowHandle)) { MessageBox.Show("Could not initialize Direct3D", "Error", MessageBoxButtons.OK); return(false); } // Create the camera object Camera = new Camera(); // Initialize a base view matrix the camera for 2D user interface rendering. Camera.SetPosition(0, 0, -10); Camera.Render(); var baseViewMatrix = Camera.ViewMatrix; // Create the model class. Model = new Model(); // Initialize the model object. if (!Model.Initialize(D3D.Device, "cube.txt", new[] { "seafloor.dds" })) { MessageBox.Show("Could not initialize the model object", "Error", MessageBoxButtons.OK); return(false); } // Create the shader object. LightShader = new LightShader(); // Initialize the 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 Light(); // Initialize the light object. Light.SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f); Light.SetDiffuseColor(1, 1, 1, 1f); Light.SetDirection(0, 0, 1); Light.SetSpecularColor(0, 1, 1, 1); Light.SetSpecularPower(16); // Create the render to texture object. RenderTexture = new RenderTexture(); // Initialize the render to texture object. if (!RenderTexture.Initialize(D3D.Device, configuration)) { return(false); } // Create the debug window object. DebugWindow = new DebugWindow(); // Initialize the debug window object. if (!DebugWindow.Initialize(D3D.Device, configuration.Width, configuration.Height, 100, 100 * configuration.Height / configuration.Width)) { MessageBox.Show("Could not initialize the debug window object.", "Error", MessageBoxButtons.OK); return(false); } // Create the texture shader object. TextureShader = new TextureShader(); // Initialize the texture shader object. if (!TextureShader.Initialize(D3D.Device, windowHandle)) { MessageBox.Show("Could not initialize the texture shader object.", "Error", MessageBoxButtons.OK); return(false); } return(true); } catch (Exception ex) { MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'"); return(false); } }
public override void PostDestroy(GameTime gameTime, GameEngine.TeeEngine engine) { LightShader lightShader = (LightShader)engine.GetPostGameShader("LightShader"); lightShader.LightSources.Remove(this); }
public void Shutdown() { // Release the fog shader object. if (FogShader != null) { FogShader.Shuddown(); FogShader = null; } // Release the texture shader object. if (TextureShader != null) { TextureShader.Shuddown(); TextureShader = null; } // Release the debug window object. if (DebugWindow != null) { DebugWindow.Shutdown(); DebugWindow = null; } // Release the render to texture object. if (RenderTexture != null) { RenderTexture.Shutdown(); RenderTexture = null; } // Release the light object. Light = null; // Release the shader object. if (LightShader != null) { LightShader.Shuddown(); LightShader = null; } // Release the model object. if (Model != null) { Model.Shutdown(); Model = null; } // Release the light shader object. if (BumpMapShader != null) { BumpMapShader.Shuddown(); BumpMapShader = null; } // Release the model object. if (BumpMapModel != null) { BumpMapModel.Shutdown(); BumpMapModel = null; } // Release the text object. if (Text != null) { Text.Shutdown(); Text = null; } // Release the camera object. if (Camera != null) { Camera = null; } // Release the Direct3D object. if (D3D != null) { D3D.Shutdown(); D3D = null; } }
public bool Initialize(SystemConfiguration configuration, IntPtr windowHandle) { try { // Create the Direct3D object. D3D = new DX11(); // Initialize the Direct3D object. if (!D3D.Initialize(configuration, windowHandle)) { MessageBox.Show("Could not initialize Direct3D", "Error", MessageBoxButtons.OK); return(false); } // Create the camera object Camera = new Camera(); // 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 Text(); if (!Text.Initialize(D3D.Device, D3D.DeviceContext, windowHandle, configuration.Width, configuration.Height, baseViewMatrix)) { MessageBox.Show("Could not initialize the text object", "Error", MessageBoxButtons.OK); return(false); } // Create the model class. Model = new Model(); // Initialize the model object. if (!Model.Initialize(D3D.Device, "sphere.txt", "seafloor.dds")) { MessageBox.Show("Could not initialize the model object", "Error", MessageBoxButtons.OK); return(false); } // Create the light shader object. LightShader = new LightShader(); // 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 Light(); // Initialize the light object. Light.SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f); Light.SetDiffuseColor(1, 0, 0, 1f); Light.SetDirection(1, 0, 1); Light.SetSpecularColor(0, 1, 1, 1); Light.SetSpecularPower(32); // Create the model list object. ModelList = new ModelList(); // 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 Frustum(); return(true); } catch (Exception ex) { MessageBox.Show("Could not initialize Direct3D\nError is '" + ex.Message + "'"); return(false); } }
public bool Render() { // Clear the buffer to begin the scene. D3D.BeginScene(0f, 0f, 0f, 1f); // Generate the view matrix based on the camera position. Camera.Render(); // Get the world, view, and projection matrices from camera and d3d objects. var viewMatrix = Camera.ViewMatrix; var worldMatrix = D3D.WorldMatrix; var projectionMatrix = D3D.ProjectionMatrix; var orthoMatrix = D3D.OrthoMatrix; // Construct the frustum. Frustum.ConstructFrustum(SystemConfiguration.ScreenDepth, projectionMatrix, viewMatrix); // Initialize the count of the models that have been rendered. var renderCount = 0; Vector3 position; Vector4 color; // Go through all models and render them only if they can seen by the camera view. for (int index = 0; index < ModelList.ModelCount; index++) { // Get the position and color of the sphere model at this index. ModelList.GetData(index, out position, out color); // Set the radius of the sphere to 1.0 since this is already known. var radius = 1.0f; // Check if the sphere model is in the view frustum. var renderModel = Frustum.CheckSphere(position, radius); // If it can be seen then render it, if not skip this model and check the next sphere. if (renderModel) { // Move the model to the location it should be rendered at. worldMatrix = Matrix.Translation(position); // Put the model vertex and index buffer on the graphics pipeline to prepare them for drawing. Model.Render(D3D.DeviceContext); // Render the model using the color shader. if (!LightShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, Model.Texture.TextureResource, Light.Direction, Light.AmbientColor, color, Camera.GetPosition(), Light.SpecularColor, Light.SpecularPower)) { return(false); } // Reset to the original world matrix. worldMatrix = D3D.WorldMatrix; // Since this model was rendered then increase the count for this frame. renderCount++; } } // Set the number of the models that was actually rendered this frame. if (!Text.SetRenderCount(renderCount, D3D.DeviceContext)) { return(false); } // Turn off the Z buffer to begin all 2D rendering. D3D.TurnZBufferOff(); // Turn on the alpha blending before rendering the text. D3D.TurnOnAlphaBlending(); // Render the text string. if (!Text.Render(D3D.DeviceContext, worldMatrix, orthoMatrix)) { return(false); } // Turn off the alpha blending before rendering the text. D3D.TurnOffAlphaBlending(); // Turn on the Z buffer to begin all 2D rendering. D3D.TurnZBufferOn(); // Present the rendered scene to the screen. D3D.EndScene(); return(true); }
public Sky(IContext context) { _skydome = new ObjModel(context.DirectX.Device, "skydome.obj", context.TextureManager.Create("Sky.png")); _shader = context.Shaders.Get <LightShader>(); }
private bool RenderScene() { // Generate the view matrix based on the camera position. Camera.Render(); // Get the world, view, and projection matrices from camera and d3d objects. var viewMatrix = Camera.ViewMatrix; var worldMatrix = D3D.WorldMatrix; var projectionMatrix = D3D.ProjectionMatrix; #region Render Ground Model // Translate to where the ground model will be rendered. Matrix.Translation(0f, 1f, 0f, out worldMatrix); // Put the ground model vertex and index buffers on the graphics pipeline to prepare them for drawing. GroundModel.Render(D3D.DeviceContext); // Render the ground model using the light shader. if (!LightShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, GroundModel.TextureCollection.Select(item => item.TextureResource).First(), Light.Direction, Light.AmbientColor, Light.DiffuseColour, Camera.GetPosition(), Light.SpecularColor, Light.SpecularPower)) { return(false); } #endregion // Reset the world matrix. worldMatrix = D3D.WorldMatrix; #region Render Wall Model // Translate to where the ground model will be rendered. Matrix.Translation(0f, 6f, 8f, out worldMatrix); // Put the wall model vertex and index buffers on the graphics pipeline to prepare them for drawing. WallModel.Render(D3D.DeviceContext); // Render the wall model using the light shader. if (!LightShader.Render(D3D.DeviceContext, WallModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, WallModel.TextureCollection.Select(item => item.TextureResource).First(), Light.Direction, Light.AmbientColor, Light.DiffuseColour, Camera.GetPosition(), Light.SpecularColor, Light.SpecularPower)) { return(false); } #endregion // Reset the world matrix. worldMatrix = D3D.WorldMatrix; #region Render Bath Model // Translate to where the bath model will be rendered. Matrix.Translation(0f, 2f, 0f, out worldMatrix); // Put the bath model vertex and index buffers on the graphics pipeline to prepare them for drawing. BathModel.Render(D3D.DeviceContext); // Render the bath model using the light shader. if (!LightShader.Render(D3D.DeviceContext, BathModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, BathModel.TextureCollection.Select(item => item.TextureResource).First(), Light.Direction, Light.AmbientColor, Light.DiffuseColour, Camera.GetPosition(), Light.SpecularColor, Light.SpecularPower)) { return(false); } #endregion // Reset the world matrix. worldMatrix = D3D.WorldMatrix; #region Render Water Model // Get the camera reflection view matrix. var reflectionMatrix = Camera.ReflectionViewMatrix; // Translate to where the water model will be rendered. Matrix.Translation(0f, WaterHeight, 0f, out worldMatrix); // Put the water model vertex and index buffers on the graphics pipeline to prepare them for drawing. WaterModel.Render(D3D.DeviceContext); // Render the bath model using the light shader. if (!WaterShader.Render(D3D.DeviceContext, WaterModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix, reflectionMatrix, RenderReflectionTexture.ShaderResourceView, RenderRefractionTexture.ShaderResourceView, WaterModel.TextureCollection.Select(item => item.TextureResource).First(), WaterTranslation, 0.1f)) // was 0.01f for scale originally. { return(false); } #endregion return(true); }