public void TestMeshPartPool() { XNAGame game = new XNAGame(); game.DrawFps = true; var pool = new MeshPartPool(); game.AddXNAObject(pool); RAMMeshPart meshPart = new RAMMeshPart(); meshPart.SetGeometryData(MeshPartGeometryData.CreateTestSquare()); DefaultModelShader shader = null; VertexDeclaration decl = null; game.InitializeEvent += delegate { decl = TangentVertexExtensions.CreateVertexDeclaration(game); shader = new DefaultModelShader(game, new EffectPool()); shader.DiffuseColor = Color.Red.ToVector4(); shader.Technique = DefaultModelShader.TechniqueType.Colored; }; game.DrawEvent += delegate { game.GraphicsDevice.RenderState.CullMode = CullMode.None; game.GraphicsDevice.VertexDeclaration = decl; shader.ViewProjection = game.Camera.ViewProjection; shader.World = Matrix.CreateTranslation(Vector3.Right * 0 * 3) * Matrix.CreateScale(10); shader.DrawPrimitives(delegate { var vb = pool.GetVertexBuffer(meshPart); var ib = pool.GetIndexBuffer(meshPart); var vertexCount = meshPart.GetGeometryData().GetSourceVector3(MeshPartGeometryData.Semantic.Position).Length; game.GraphicsDevice.Vertices[0].SetSource(vb, 0, TangentVertex.SizeInBytes); game.GraphicsDevice.Indices = ib; game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, vertexCount / 3); }); }; game.Run(); }
public VertexDeclaration GetVertexDeclaration <T>() { // Buffer the vertexdeclarations + return decl for given type T return(TangentVertexExtensions.CreateVertexDeclaration(game)); }
public void Initialize(IXNAGame _game) { game = _game; //get the sizes of the backbuffer, in order to have matching render targets int backBufferWidth = _game.GraphicsDevice.PresentationParameters.BackBufferWidth; int backBufferHeight = _game.GraphicsDevice.PresentationParameters.BackBufferHeight; initializeDeferredRendering(_game, backBufferWidth, backBufferHeight); initializeDeferredShading(_game, backBufferWidth, backBufferHeight); ambientOcclusionRT = new RenderTarget2D(_game.GraphicsDevice, backBufferWidth, backBufferHeight, 1, SurfaceFormat.Color); //TODO: use better channel here blurRT = new RenderTarget2D(_game.GraphicsDevice, backBufferWidth, backBufferHeight, 1, SurfaceFormat.Color); //TODO: use better channel here downsampleSize = new Point(backBufferWidth / 2, backBufferHeight / 2); downsampleHalfPixel.X = 0.5f / downsampleSize.X; downsampleHalfPixel.Y = 0.5f / downsampleSize.Y; downsampleRT = new RenderTarget2D(_game.GraphicsDevice, downsampleSize.X, downsampleSize.Y, 1, SurfaceFormat.Color); downsampleBlurYRT = new RenderTarget2D(_game.GraphicsDevice, downsampleSize.X, downsampleSize.Y, 1, SurfaceFormat.Color); deferredShader = loadShader("DeferredShader.fx"); ambientOcclusionShader = loadShader("AmbientOcclusion.fx"); blurShader = loadShader("Blur.fx"); downsampleShader = loadShader("Downsample.fx"); fullScreenQuad = new FullScreenQuad(GraphicsDevice); tangentVertexDeclaration = TangentVertexExtensions.CreateVertexDeclaration(game); mesh = merchantsHouseMesh; texturePool = new Rendering.TexturePool(); texturePool.Initialize(game); meshPartPool = new MeshPartPool(); meshPartPool.Initialize(game); boxTexture = Texture2D.FromFile(GraphicsDevice, new FileStream(woodPlanksBarePath, FileMode.Open, FileAccess.Read, FileShare.Read)); halfPixel.X = 0.5f / GraphicsDevice.PresentationParameters.BackBufferWidth; halfPixel.Y = 0.5f / GraphicsDevice.PresentationParameters.BackBufferHeight; checkerTexture = Texture2D.FromFile(game.GraphicsDevice, EmbeddedFile.GetStream( "MHGameWork.TheWizards.Rendering.Files.Checker.png", "Checker.png")); randomNormalsTexture = Texture2D.FromFile(game.GraphicsDevice, EmbeddedFile.GetStream( "MHGameWork.TheWizards.Rendering.Deferred.Files.RandomNormals.png", "RandomNormals.png")); initializeToneMap(); }
public void TestRenderDefaultModelShader() { var shaders = new List <DefaultModelShader>(); DefaultModelShader shader = null; VertexBuffer vb = null; VertexDeclaration decl = null; XNAGame game = new XNAGame(); game.InitializeEvent += delegate { decl = TangentVertexExtensions.CreateVertexDeclaration(game); TangentVertex[] vertices = new TangentVertex[4]; vertices[0] = new TangentVertex(Vector3.Zero, Vector2.Zero, Vector3.Up, Vector3.Zero); vertices[1] = new TangentVertex(Vector3.Forward, Vector2.UnitX, Vector3.Up, Vector3.Zero); vertices[2] = new TangentVertex(Vector3.Forward + Vector3.Right, Vector2.UnitX + Vector2.UnitY, Vector3.Up, Vector3.Zero); vertices[3] = new TangentVertex(Vector3.Right, Vector2.UnitY, Vector3.Up, Vector3.Zero); vb = new VertexBuffer(game.GraphicsDevice, typeof(TangentVertex), vertices.Length, BufferUsage.None); vb.SetData(vertices); shader = new DefaultModelShader(game, new EffectPool()); DefaultModelShader s; Texture2D tex = Texture2D.FromFile(game.GraphicsDevice, File.OpenRead(TestFiles.WoodPlanksBareJPG)); s = shader.Clone(); s.DiffuseColor = Color.Red.ToVector4(); s.Technique = DefaultModelShader.TechniqueType.Colored; shaders.Add(s); s = shader.Clone(); s.DiffuseTexture = tex; s.Technique = DefaultModelShader.TechniqueType.Textured; shaders.Add(s); }; game.DrawEvent += delegate { game.GraphicsDevice.RenderState.CullMode = CullMode.None; game.GraphicsDevice.VertexDeclaration = decl; shader.ViewProjection = game.Camera.ViewProjection; for (int i = 0; i < shaders.Count; i++) { //shaders[i].ViewProjection = game.Camera.ViewProjection; shaders[i].World = Matrix.CreateTranslation(Vector3.Right * i * 3) * Matrix.CreateScale(10); shaders[i].DrawPrimitives(delegate() { game.GraphicsDevice.Vertices[0].SetSource(vb, 0, TangentVertex.SizeInBytes); game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2); } ); } }; game.Run(); }
} // GenerateVertexAndIndexBuffers() /// <summary> /// Render the animated model (will call UpdateAnimation internally, /// but if you do that yourself before calling this method, it gets /// optimized out). Rendering always uses the skinnedNormalMapping shader /// with the DiffuseSpecular20 technique. /// </summary> /// <param name="renderMatrix">Render matrix</param> public void Render(Matrix renderMatrix, ShaderEffect effect) { if (vertexBuffer == null || indexBuffer == null) { return; } // Make sure we use the correct vertex declaration for our shader. //game.GraphicsDevice.VertexDeclaration = TangentVertex.VertexDeclaration; // Set the world matrix for this object (often Identity). // The renderMatrix is directly applied to the matrices we use // as bone matrices for the shader (has to be done this way because // the bone matrices are transmitted transposed and we could lose // important render matrix translation data if we do not apply it there). //BaseGame.WorldMatrix = objectMatrix; // And set all bone matrices (Goblin has 40, but we support up to 80). //ShaderEffect.skinnedNormalMapping.SetBoneMatrices( // GetBoneMatrices( renderMatrix ) ); // Rendering is pretty straight forward (if you know how anyway). //ShaderEffect.skinnedNormalMapping.Render( // material, "DiffuseSpecular20", // RenderVertices ); Matrix worldMatrix = //Matrix.CreateFromYawPitchRoll( -MathHelper.PiOver2, -MathHelper.PiOver2, 0 ) * //Matrix.CreateFromYawPitchRoll( 0, -MathHelper.PiOver2, 0 ) renderMatrix; //* Matrix.CreateFromYawPitchRoll( -MathHelper.PiOver2, -MathHelper.PiOver2, 0 ); effect.SetParametersCollada(material); //effect.ViewProjMatrix = model.Game.Camera.ViewProjection; //Microsoft.Xna.Framework.Graphics.SamplerState s = new SamplerState(); //object o = effect.Effect.Parameters[ "DiffuseTextureSampler " ]; if (verticesSkinned.Count > 0) { /*worldMatrix = Matrix.Identity; * worldMatrix = model.bones[ 0 ].invBoneSkinMatrix * model.bones[ 0 ].initialMatrix;*/ /*worldMatrix = objectMatrix; * effect.WorldMatrix = worldMatrix; * effect.WorldViewProjMatrix = worldMatrix * model.Game.Camera.ViewProjection; * worldMatrix = Matrix.Identity;*/ /*game.GraphicsDevice.VertexDeclaration = SkinnedTangentVertex.VertexDeclaration; * * * //SetBoneMatrices( effect, model.GetBoneMatrices( worldMatrix ) ); * * * * effect.RenderCollada( "SpecularPerPixelColored", RenderVerticesSkinned );*/ effect.Effect.Parameters["viewProj"].SetValue(model.Game.Camera.ViewProjection); effect.Effect.Parameters["world"].SetValue(objectMatrix); //Matrix mat = Matrix.Identity; //mat.Translation = new Vector3( 1, 0, 0 ); //effect.Effect.Parameters[ "world" ].SetValue( mat ); effect.Effect.Parameters["cameraPos"].SetValue(model.Game.Camera.ViewInverse.Translation); if (vertexDeclaration != null) { vertexDeclaration = SkinnedTangentVertex.CreateVertexDeclaration(game); } game.GraphicsDevice.VertexDeclaration = vertexDeclaration; SetBoneMatrices(effect, model.GetBoneMatrices(renderMatrix)); if (material.DiffuseTexture == null) { effect.RenderCollada("DiffuseSpecularColored20", RenderVerticesSkinned); } else { effect.RenderCollada("DiffuseSpecular20", RenderVerticesSkinned); } } else { worldMatrix = objectMatrix * worldMatrix; effect.WorldMatrix = worldMatrix; effect.WorldViewProjMatrix = worldMatrix * model.Game.Camera.ViewProjection; if (vertexDeclaration != null) { vertexDeclaration = TangentVertexExtensions.CreateVertexDeclaration(game); } game.GraphicsDevice.VertexDeclaration = vertexDeclaration; if (material.DiffuseTexture == null) { effect.RenderCollada("SpecularPerPixelColored", RenderVertices); } else if (material.NormalTexture == null || game.Keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Y)) { effect.RenderCollada("SpecularPerPixel", RenderVertices); } else { if (game.Keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.N)) { effect.RenderCollada("SpecularPerPixel", RenderVertices); } else { //Normal Mapping effect.RenderCollada("SpecularPerPixelNormalMapping", RenderVertices); } } } } // Render(renderMatrix)