public ShadowRenderer(GraphicsDevice device, ContentManager content, string shaderPath, string shaderPathSkinned) { mShader = new ShadowShader(content, shaderPath); mShaderSkinned = new ShadowShader(content, shaderPathSkinned); mGraphicsDevice = device; }
private void Render(RenderTarget target, Camera camera, Scene scene, ShadowShader shader, bool skinnedRendering) { var light = scene.mSky.Light; shader.mLightSpaceMatrix = light.mShadow.mViewMatrix * light.mShadow.mProjectionMatrix; shader.Apply(); // Now render our actor in batched mode foreach (var actorBatch in scene.mActorBatches) { var meshData = actorBatch.mMesh.mMeshData; // When using the unskinned shader we don't want to render skinned meshes if (meshData.mIsSkinned && !skinnedRendering) { continue; } // When using the skinned shader we don't want to render unskinned meshes if (!meshData.mIsSkinned && skinnedRendering) { continue; } mGraphicsDevice.SetVertexBuffer(actorBatch.mMesh.VertexBuffer); mGraphicsDevice.Indices = actorBatch.mMesh.IndexBuffer; // When we render shadows we dont care about materials so we // can just iterate of the number of actors instead of using // another loop to consider all the materials foreach (var actor in actorBatch.mActors) { if (!actor.mCastShadow) { continue; } shader.ApplyModelMatrix(actor.ModelMatrix); if (skinnedRendering) { shader.ApplyBoneTransformations(actor.mAnimator.mTransformations); } mGraphicsDevice.DrawIndexedPrimitives(meshData.mPrimitiveType, 0, 0, meshData.mTotalNumPrimitives); } } }
public MainWindow() { InitializeComponent(); _shadowQuality = ShadowQuality.Smooth; _grid = new Cell[CellRows, CellCols]; _smoothedIntensities = new float[CellRows, CellCols]; for (int r = 0; r < CellRows; r++) { for (int c = 0; c < CellCols; c++) { _grid[r, c] = new Cell(); } } _gameObjects = new List <IGameObject>(); _rayEmitters = new List <IRayEmitter>(); var player = new Character(new Vector2(400, 200)); _gameObjects.Add(player); _rayEmitters.Add(player); _rayTracer = new RayTracer(_grid); _shadowShader = new ShadowShader(); _caveBrush = new TextureBrush(new Bitmap("Content/background.png")); _background = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb); _frameBuffer = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb); _writeableBitmap = new WriteableBitmap(Width, Height, 96, 96, PixelFormats.Bgr32, null); Canvas.Source = _writeableBitmap; using (Graphics graphics = Graphics.FromImage(_frameBuffer)) { graphics.Clear(Color.Black); } AddSurfaces(); _gameTime = new GameTime(); _controller = new Controller(); _isRendering = true; _renderThread = new Thread(RenderLoop); _renderThread.Start(); }
private static void RenderShadowShader(this Image <Rgba32> screen, float[,] zBuffer) { var shadowBuffer = new float[ScreenWidth, ScreenHeight]; var cameraPosition = new Vector3(1, 1, 3); var cameraTarget = new Vector3(0, 0, 0); var cameraUp = new Vector3(0, 1, 0); var viewPort = CreateViewPort(ScreenWidth / 8, ScreenHeight / 8, ScreenWidth * 3 / 4, ScreenHeight * 3 / 4); var projection = CreateProjection(-1 / (cameraPosition - cameraTarget).Length()); var modelView = CreateLookAt(cameraPosition, cameraTarget, cameraUp); var light = new Vector3(1, 1, 1); light = (projection * modelView).Multiply(light).Normalize(); var shadowProjection = Matrix4x4.Identity; var shadowModelView = CreateLookAt(light, cameraTarget, cameraUp); var model = ModelUtils.LoadModel(@"Resources\Diablo3\Model.obj"); var texture = TextureUtils.LoadTexture(@"Resources\Diablo3\Diffuse.png"); var normalMap = TextureUtils.LoadTexture(@"Resources\Diablo3\Normal.png"); var specularMap = TextureUtils.LoadTexture(@"Resources\Diablo3\Specular.png"); specularMap.Mutate(context => context.Flip(FlipType.Vertical)); IShader shader = new ZBufferShader { Transform = viewPort * shadowProjection * shadowModelView }; screen.Render(model, shader, shadowBuffer); shader = new ShadowShader { Transform = viewPort * projection * modelView, MIT = (projection * modelView).Invert().Transpose(), ShadowTransform = viewPort * shadowProjection * shadowModelView * (viewPort * projection * modelView).Invert(), Light = light, Texture = texture, NormalMap = normalMap, SpecularMap = specularMap, ShadowBuffer = shadowBuffer }; screen.Mutate(context => context.Fill(Rgba32.Black)); screen.Render(model, shader, zBuffer); }
public TerrainRenderer(Camera camera) { this.camera = camera; shader = new RenderShader(); shadowShader = new ShadowShader(); quadTree = new TerrainQuadTree(); worldTransform = Matrix4.CreateScale(Map.MapData.MapSize) * Matrix4.CreateTranslation(-Map.MapData.MapSize / 2.0f, 0.0f, -Map.MapData.MapSize / 2.0f); texture = new Texture(); textureNoise = new TextureNoise(12312234); NormalMap = new NormalMap(); HeightMap = new HeightMap(NormalMap); SplatMap = new SplatMap(); picker = new TerrainPicker(camera, HeightMap); loadTextures(); build(); Update(); }
// TODO: Do meshes need to be unloaded? public void Dispose() { Shader?.Dispose(); ShadowShader?.Dispose(); }
public void Render() { Stopwatch sw = new Stopwatch(); sw.Start(); DirectBitmap bitmap = new DirectBitmap(width, height); Rasterizer rasterizer = new Rasterizer(); //// start of shadow buffer rendering Matrix4x4 SB_ModelView = Matrix4x4Extensions.LookAt(light_dir, center, up); Matrix4x4 SB_Projection = Matrix4x4.Identity; Matrix4x4 ViewPort = Matrix4x4Extensions.Viewport(width / 8, height / 8, width * 3 / 4, height * 3 / 4); SB_Projection.M43 = 0; var shader = new DepthShader(model, ViewPort, SB_Projection, SB_ModelView); for (int i = 0; i < model.FacesV.Count; i++) { Vector4[] screen_coords = new Vector4[3]; for (int j = 0; j < model.FacesV[i].Length; j++) { screen_coords[j] = shader.Vertex(i, j); } rasterizer.Triangle(screen_coords, shader, bitmap, pictureBox, shadow_buffer, true); } //// end of shadow buffer rendering //// start of picture render by shadow shader //// Matrix4x4 M = ViewPort * SB_Projection * SB_ModelView; Matrix4x4 ModelView = Matrix4x4Extensions.LookAt(eye, center, up); Matrix4x4 Projection = Matrix4x4.Identity; Projection.M43 = -1.0f / (eye - center).Norm(); var sh_shader = new ShadowShader(model, ViewPort, Projection, ModelView, light_dir, shadow_buffer, width); sh_shader.uniform_m = Projection * ModelView; sh_shader.uniform_mit = sh_shader.uniform_m.Inverse().Transpose(); sh_shader.uniform_m_shadow = M * (ViewPort * Projection * ModelView).Inverse(); for (int i = 0; i < model.FacesV.Count; i++) { Vector4[] screen_coords = new Vector4[3]; for (int j = 0; j < model.FacesV[i].Length; j++) { screen_coords[j] = sh_shader.Vertex(i, j); } rasterizer.Triangle(screen_coords, sh_shader, bitmap, pictureBox, z_buffer); } //// end of picture render by shadow shader //// sw.Stop(); model.Texture.Dispose(); bitmap.Bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); InfoViewer.ShowDiagnosticInfo ($"Render Time: {sw.Elapsed.TotalMilliseconds:f2} ms", new PointF(10, 30), Graphics.FromImage(bitmap.Bitmap)); // output final render pictureBox.Image = bitmap.Bitmap; }
private bool RenderBlackAndWhiteShadows() { // Set the render target to be the render to texture. BlackWhiteRenderTexture.SetRenderTarget(D3D.DeviceContext); // Clear the render to texture. BlackWhiteRenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f); // Generate the view matrix based on the camera's position. Camera.Render(); // Generate the light view matrix based on the light's position. Light.GenerateViewMatrix(); // Get the world, view, and projection matrices from the camera and d3d objects. Matrix cameraViewMatrix = Camera.ViewMatrix; Matrix worldMatrix = D3D.WorldMatrix; Matrix projectionMatrix = D3D.ProjectionMatrix; // Get the light's view and projection matrices from the light object. Matrix lightViewMatrix = Light.ViewMatrix; Matrix lightProjectionMatrix = Light.ProjectionMatrix; // Setup the translation matrix for the cube model. Vector3 cubePosition = CubeModel.GetPosition(); Matrix.Translation(cubePosition.X, cubePosition.Y, cubePosition.Z, out worldMatrix); // Render the cube model using the shadow shader. CubeModel.Render(D3D.DeviceContext); if (!ShadowShader.Render(D3D.DeviceContext, CubeModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, RenderTexture.ShaderResourceView, Light.Position)) { return(false); } // Reset the world matrix. worldMatrix = D3D.WorldMatrix; // Setup the translation matrix for the sphere model. Vector3 spherePosition = SphereModel.GetPosition(); Matrix.Translation(spherePosition.X, spherePosition.Y, spherePosition.Z, out worldMatrix); // Render the sphere model using the shadow shader. SphereModel.Render(D3D.DeviceContext); if (!ShadowShader.Render(D3D.DeviceContext, SphereModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, RenderTexture.ShaderResourceView, Light.Position)) { return(false); } // Reset the world matrix. worldMatrix = D3D.WorldMatrix; // Setup the translation matrix for the ground model. Vector3 groundPosition = GroundModel.GetPosition(); Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMatrix); // Render the ground model using the shadow shader. GroundModel.Render(D3D.DeviceContext); if (!ShadowShader.Render(D3D.DeviceContext, SphereModel.IndexCount, worldMatrix, cameraViewMatrix, projectionMatrix, lightViewMatrix, lightProjectionMatrix, RenderTexture.ShaderResourceView, Light.Position)) { return(false); } // Reset the render target back to the original back buffer and not the render to texture anymore. D3D.SetBackBufferRenderTarget(); // Reset the viewport back to the original. D3D.ResetViewPort(); return(true); }
public void Shutdown() { // Release the light object. Light = null; // Release the camera object. Camera = null; // Release the soft shadow shader object. SoftShadowShader?.ShutDown(); SoftShadowShader = null; // Release the full screen ortho window object. FullScreenWindow?.Shutdown(); FullScreenWindow = null; // Release the up sample render to texture object. UpSampleTexure?.Shutdown(); UpSampleTexure = null; // Release the vertical blur shader object. VerticalBlurShader?.ShutDown(); VerticalBlurShader = null; // Release the vertical blur render to texture object. VerticalBlurTexture?.Shutdown(); VerticalBlurTexture = null; // Release the horizontal blur shader object. HorizontalBlurShader?.ShutDown(); HorizontalBlurShader = null; // Release the horizontal blur render to texture object. HorizontalBlurTexture?.Shutdown(); HorizontalBlurTexture = null; // Release the texture shader object. TextureShader?.ShutDown(); TextureShader = null; // Release the small ortho window object. SmallWindow?.Shutdown(); SmallWindow = null; // Release the down sample render to texture object. DownSampleTexure?.Shutdown(); DownSampleTexure = null; // Release the shadow shader object. ShadowShader?.ShutDown(); ShadowShader = null; // Release the black and white render to texture. BlackWhiteRenderTexture?.Shutdown(); BlackWhiteRenderTexture = null; // Release the depth shader object. DepthShader?.ShutDown(); DepthShader = null; /// Release the render to texture object. RenderTexture?.Shutdown(); RenderTexture = null; // Release the ground model object. GroundModel?.Shutdown(); GroundModel = null; // Release the sphere model object. SphereModel?.Shutdown(); SphereModel = null; // Release the cube model object. CubeModel?.Shutdown(); CubeModel = null; // Release the Direct3D object. D3D?.ShutDown(); D3D = null; }
static void Main(string[] args) { Window w = new Window(1920, 1080); EngineCore.AddImage("default.png", "default"); DefaultShader ds = new DefaultShader("default.vert", "default.frag"); String defaultShader = EngineCore.AddShader(ds, "Default"); GUIShader gui = new GUIShader("gui.vert", "gui.frag"); String guiShader = EngineCore.AddShader(gui, "GUI"); WaterShader water = new WaterShader("water.vert", "water.frag"); String waterShader = EngineCore.AddShader(water, "Water"); SkyboxShader skybox = new SkyboxShader("skybox.vert", "skybox.frag"); String skyboxShader = EngineCore.AddShader(skybox, "Skybox"); ShadowShader shadow = new ShadowShader("shadow.vert", "shadow.frag"); String shadowShader = EngineCore.AddShader(shadow, "Shadow"); TerrainShader terrain = new TerrainShader("terrain.vert", "terrain.frag"); String terrainShader = EngineCore.AddShader(terrain, "Terrain"); String cubeModel = EngineCore.AddModel("cubeything.obj", "cube"); String terrainModel = EngineCore.AddModel("terrain.obj", "terrain"); String quad = EngineCore.AddModel("quad.obj", "Quad"); String cubeObj = EngineCore.AddModel("cube.obj", "Cube"); EngineCore.AddModel("tree.obj", "tree"); EngineCore.AddModel("rock.obj", "rock"); Light l = new Light(new Vector3(0, 3, 0), System.Drawing.Color.White); ds.lights.Add(l); terrain.lights.Add(l); Light l2 = new Light(new Vector3(0, 10, 0), System.Drawing.Color.White); water.lights.Add(l2); EngineCore.AddImage("tree.png", "treeImg"); EngineCore.AddImage("rock.png", "rockImg"); String boatObj = EngineCore.AddModel("boat.obj", "Boat"); EngineCore.AddImage("boards.jpg", "Boards"); Boat boat = new Boat(new Vector3(2, 0.2f, 2), new Vector3(0, 34, 0), new Vector3(0.25f, 0.25f, 0.25f), "Boards"); EngineCore.AddObject("Boat", boat); String ground = EngineCore.AddImage("grass.jpg", "Ground"); Terrain terrainObj = new Terrain("New Terrain", "Ground", "heightMap.png"); EngineCore.AddObject("Terrain", terrainObj); EngineCore.AddImage("dudv.png", "DuDvMap"); EngineCore.AddImage("normal.png", "NormalMap"); Water waterObj = new Water(new Vector3(0, 0.25f, 0), System.Drawing.Color.Blue, new Vector2(1920, 1080), "DuDvMap", new Vector3(10, 10, 10)); waterObj.AttachNormalMap("NormalMap"); EngineCore.AddObject("water", waterObj); EngineCore.AddSkybox(new string[] { "Skybox/xpos.png", "Skybox/xneg.png", "Skybox/ypos.png", "Skybox/yneg.png", "Skybox/zpos.png", "Skybox/zneg.png" }, "Skybox", 500); w.Run(); }