public void Render(IXNAGame _game) { for (int i = 0; i < spheres.Count; i++) { sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center); sphereMesh.Render(game); } }
public void Draw(GameTime gameTime, Camera camera) { if (IsVisible(camera)) { float distance = (position - camera.CameraPosition).Length(); float angularSize = (float)Math.Tan((planetRadius + atmosRadius) / distance); float sizeInPixels = angularSize * GraphicsDevice.Viewport.Height / MathHelper.ToRadians(45.0f); if (sizeInPixels > 6.0f) { Matrix worldMatrix = Matrix.CreateFromYawPitchRoll(rotation.X, 0, 0); worldMatrix = worldMatrix * Matrix.CreateFromYawPitchRoll(0, 0, rotation.Y); worldMatrix.Translation = position; Vector3 lightDir = Vector3.Forward; if (atmosphereDiffuse != null) { // Draw the cloud layer. atmosphereEffect.Begin(); atmosphereEffect.Techniques[0].Passes[0].Begin(); atmosWorldMatrix.SetValue(worldMatrix); atmosViewMatrix.SetValue(camera.View); atmosProjectionMatrix.SetValue(camera.Projection); atmosDiffuseTexture.SetValue(atmosphereDiffuse); atmosLightPosition.SetValue(lightDir); atmosLightColor.SetValue(new Vector4(1, 1, 1, 1)); atmosphereEffect.CommitChanges(); GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace; GraphicsDevice.RenderState.AlphaBlendEnable = true; GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha; GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; GraphicsDevice.RenderState.DepthBufferEnable = false; GraphicsDevice.RenderState.DepthBufferWriteEnable = false; atmosphereMesh.Render(GraphicsDevice); GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace; GraphicsDevice.RenderState.AlphaBlendEnable = false; GraphicsDevice.RenderState.DepthBufferEnable = true; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; atmosphereEffect.Techniques[0].Passes[0].End(); atmosphereEffect.End(); } } } }
public void Draw(GameTime gameTime, Camera camera) { if (IsVisible(camera)) { float distance = (position - camera.CameraPosition).Length(); float angularSize = (float)Math.Tan(radius / distance); float sizeInPixels = angularSize * GraphicsDevice.Viewport.Height / camera.FieldOfView; if (sizeInPixels > 6.0f) { Matrix worldMatrix = Matrix.CreateFromYawPitchRoll(rotation.X, 0, 0); worldMatrix = worldMatrix * Matrix.CreateFromYawPitchRoll(0, 0, rotation.Y); worldMatrix.Translation = position; Vector3 lightDir = Vector3.Forward; planetEffect.Begin(); planetEffect.Techniques[0].Passes[0].Begin(); planetWorldMatrix.SetValue(worldMatrix); planetViewMatrix.SetValue(camera.View); planetProjectionMatrix.SetValue(camera.Projection); planetDiffuseTexture.SetValue(planetDiffuse); planetBumpTexture.SetValue(planetBump); planetSpecTexture.SetValue(planetSpec); planetNightTexture.SetValue(planetNight); planetLightPosition.SetValue(lightDir); planetLightColor.SetValue(new Vector4(1, 1, 1, 1)); planetSpecularPow.SetValue(20.0f); planetSpecularIntensity.SetValue(1.0f); planetCameraPos.SetValue(camera.CameraPosition); planetHazeColor.SetValue(hazeColor); planetEffect.CommitChanges(); GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace; GraphicsDevice.RenderState.DepthBufferEnable = true; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; GraphicsDevice.RenderState.AlphaBlendEnable = false; planet.Render(GraphicsDevice); GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace; planetEffect.Techniques[0].Passes[0].End(); planetEffect.End(); } } }
public void Draw(GameTime gameTime, Camera camera) { // Figure out where on screen to draw halo Effect sprite Matrix viewProj = camera.View * camera.Projection; Vector4 projResult = Vector4.Transform(position, viewProj); float halfScreenY = ((float)GraphicsDevice.Viewport.Height / 2.0f); float halfScreenX = ((float)GraphicsDevice.Viewport.Width / 2.0f); Vector2 screenPos = new Vector2(((projResult.X / projResult.W) * halfScreenX) + halfScreenX, halfScreenY - ((projResult.Y / projResult.W) * halfScreenY)); // First check of projResult.W is to determine // if camera is facing the sun or turned away from the sun // projResult.W is negative if camera is facing away if ((projResult.W > 0.0f) && IsVisible(screenPos)) { Matrix worldMatrix = Matrix.CreateFromYawPitchRoll(rotation.X, 0, 0); worldMatrix = worldMatrix * Matrix.CreateFromYawPitchRoll(0, 0, rotation.Y); worldMatrix.Translation = position; // Draw Halo effect around star halo.Begin(); halo.Draw(haloDiffuse, screenPos, null, haloColor, 0.0f, spriteCenter, 1.0f, SpriteEffects.None, 0.0f); halo.End(); // Draw Star starEffect.Begin(); starEffect.Techniques[0].Passes[0].Begin(); starWorldMatrix.SetValue(worldMatrix); starViewMatrix.SetValue(camera.View); starProjectionMatrix.SetValue(camera.Projection); starDiffuseTexture.SetValue(starDiffuse); starColorData.SetValue(starColor); starEffect.CommitChanges(); GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace; GraphicsDevice.RenderState.DepthBufferEnable = true; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; star.Render(GraphicsDevice); GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace; starEffect.Techniques[0].Passes[0].End(); starEffect.End(); } }
public void Draw(GameTime gameTime, Camera camera) { if (IsVisible(camera)) { float distance = (position - camera.CameraPosition).Length(); float angularSize = (float)Math.Tan(radius / distance); float sizeInPixels = angularSize * GraphicsDevice.Viewport.Height / camera.FieldOfView; if (sizeInPixels > 6.0f) { Matrix worldMatrix = Matrix.CreateFromYawPitchRoll(rotation.X, 0, 0); worldMatrix = worldMatrix * Matrix.CreateFromYawPitchRoll(0, 0, rotation.Y); worldMatrix.Translation = position; //Vector3 lightDir = Vector3.Forward; Vector3 lightDir = new Vector3(-.1f, 0, -1); planetWorldMatrix.SetValue(worldMatrix); planetViewMatrix.SetValue(camera.View); planetProjectionMatrix.SetValue(camera.Projection); planetDiffuseTexture.SetValue(planetDiffuse); planetBumpTexture.SetValue(planetBump); planetSpecTexture.SetValue(planetSpec); planetNightTexture.SetValue(planetNight); planetLightPosition.SetValue(lightDir); planetLightColor.SetValue(new Vector4(.7f, .3f, 0, 1)); planetSpecularPow.SetValue(2.0f); planetSpecularIntensity.SetValue(1f); planetCameraPos.SetValue(camera.CameraPosition); planetHazeColor.SetValue(hazeColor); planetEffect.Techniques[0].Passes[0].Apply(); GraphicsDevice.RasterizerState = RasterizerState.CullClockwise; GraphicsDevice.DepthStencilState = DepthStencilState.Default; BlendStateHelper.BeginApply(GraphicsDevice); BlendStateHelper.AlphaBlendEnable = false; BlendStateHelper.EndApply(GraphicsDevice); planet.Render(GraphicsDevice); GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; } } }
public void TestOBJToRAMMeshConverterPerObjectVisual() { var c = new OBJToRAMMeshConverter(new RAMTextureFactory()); var importer = new ObjImporter(); importer.AddMaterialFileStream("Town001.mtl", new FileStream(TestFiles.TownMtl, FileMode.Open)); importer.ImportObjFile(TestFiles.TownObj); var meshes = c.CreateMeshesFromObjects(importer); var texturePool = new TexturePool(); var meshpartPool = new MeshPartPool(); var vertexDeclarationPool = new VertexDeclarationPool(); var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool); vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements); var spheres = new List <ClientPhysicsTestSphere>(); var engine = new PhysicsEngine(); PhysicsDebugRendererXNA debugRenderer = null; var builder = new MeshPhysicsActorBuilder(new MeshPhysicsPool()); TheWizards.Client.ClientPhysicsQuadTreeNode root; int numNodes = 20; root = new ClientPhysicsQuadTreeNode( new BoundingBox( new Vector3(-numNodes * numNodes / 2f, -100, -numNodes * numNodes / 2f), new Vector3(numNodes * numNodes / 2f, 100, numNodes * numNodes / 2f))); QuadTree.Split(root, 5); var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root); var physicsElementFactory = physicsElementFactoryXNA.Factory; var physicsElements = new List <MeshStaticPhysicsElement>(); for (int i = 0; i < 0 * 100 + 1 * meshes.Count; i++) { var mesh = meshes[i]; var el = renderer.AddMesh(mesh); el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2); var pEl = physicsElementFactory.CreateStaticElement(mesh, Matrix.Identity); physicsElements.Add(pEl); } var game = new XNAGame(); game.IsFixedTimeStep = false; game.DrawFps = true; game.SpectaterCamera.FarClip = 5000; game.Graphics1.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs e) { DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode; e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format; e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width; e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height; game.SpectaterCamera.AspectRatio = displayMode.Width / (float)displayMode.Height; }; game.Graphics1.ToggleFullScreen(); var sphereMesh = new SphereMesh(0.3f, 20, Color.Green); var visualizer = new QuadTreeVisualizerXNA(); game.AddXNAObject(physicsElementFactoryXNA); game.AddXNAObject(texturePool); game.AddXNAObject(meshpartPool); game.AddXNAObject(vertexDeclarationPool); game.AddXNAObject(renderer); game.InitializeEvent += delegate { engine.Initialize(); debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene); debugRenderer.Initialize(game); sphereMesh.Initialize(game); }; bool showPhysics = true; game.DrawEvent += delegate { if (game.Keyboard.IsKeyPressed(Keys.P)) { showPhysics = !showPhysics; } if (showPhysics) { debugRenderer.Render(game); } visualizer.RenderNodeGroundBoundig(game, root, delegate(ClientPhysicsQuadTreeNode node, out Color col) { col = Color.Green; return(node.PhysicsObjects.Count == 0); }); visualizer.RenderNodeGroundBoundig(game, root, delegate(ClientPhysicsQuadTreeNode node, out Color col) { col = Color.Orange; return(node.PhysicsObjects.Count > 0); }); for (int i = 0; i < physicsElements.Count; i++) { var el = physicsElements[i]; //game.LineManager3D.AddBox(BoundingBox.CreateFromSphere( el.BoundingSphere), Color.Orange); } for (int i = 0; i < spheres.Count; i++) { sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center); sphereMesh.Render(game); } }; game.UpdateEvent += delegate { engine.Update(game.Elapsed); sphereMesh.Update(game); if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F)) { var iSphere = new ClientPhysicsTestSphere(engine.Scene, game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection , 0.3f); iSphere.InitDynamic(); iSphere.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 10; spheres.Add(iSphere); } for (int i = 0; i < spheres.Count; i++) { spheres[i].Update(root, game); } }; game.Run(); }
public void TestOBJToRAMMeshConverterPerObjectVisualCool() { var textureFactory = new RAMTextureFactory(); var c = new OBJToRAMMeshConverter(textureFactory); var importer = new ObjImporter(); importer.AddMaterialFileStream("Town001.mtl", new FileStream("../GameData/Town/OBJ03/Town001.mtl", FileMode.Open)); importer.ImportObjFile("../GameData/Town/OBJ03/Town001.obj"); var meshes = c.CreateMeshesFromObjects(importer); var texturePool = new TexturePool(); var meshpartPool = new MeshPartPool(); var vertexDeclarationPool = new VertexDeclarationPool(); var renderer = new SimpleMeshRenderer(texturePool, meshpartPool, vertexDeclarationPool); vertexDeclarationPool.SetVertexElements <TangentVertex>(TangentVertex.VertexElements); var spheres = new List <ClientPhysicsTestSphere>(); var engine = new PhysicsEngine(); PhysicsDebugRendererXNA debugRenderer = null; var root = CreatePhysicsQuadtree(20, 5); var physicsElementFactoryXNA = new MeshPhysicsFactoryXNA(engine, root); var physicsElementFactory = physicsElementFactoryXNA.Factory; var physicsElements = new List <MeshStaticPhysicsElement>(); for (int i = 0; i < 0 * 100 + 1 * meshes.Count; i++) { var mesh = meshes[i]; var el = renderer.AddMesh(mesh); el.WorldMatrix = Matrix.CreateTranslation(Vector3.Right * 0 * 2 + Vector3.UnitZ * 0 * 2); var pEl = physicsElementFactory.CreateStaticElement(mesh, Matrix.Identity); physicsElements.Add(pEl); } var gameMeshes = new List <OBJParserTest.TestGameMesh>(); var game = new XNAGame(); game.IsFixedTimeStep = false; game.DrawFps = true; game.SpectaterCamera.FarClip = 5000; game.Graphics1.PreparingDeviceSettings += delegate(object sender, PreparingDeviceSettingsEventArgs e) { DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode; e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format; e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width; e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height; game.SpectaterCamera.AspectRatio = displayMode.Width / (float)displayMode.Height; }; game.Graphics1.ToggleFullScreen(); var barrelMesh = OBJParserTest.GetBarrelMesh(c); var crateMesh = OBJParserTest.GetCrateMesh(c); var sphereMesh = new SphereMesh(0.3f, 20, Color.Green); var visualizer = new QuadTreeVisualizerXNA(); game.AddXNAObject(physicsElementFactoryXNA); game.AddXNAObject(texturePool); game.AddXNAObject(meshpartPool); game.AddXNAObject(vertexDeclarationPool); game.AddXNAObject(renderer); game.InitializeEvent += delegate { engine.Initialize(); debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene); debugRenderer.Initialize(game); sphereMesh.Initialize(game); for (int i = 0; i < meshes.Count; i++) { var mesh = meshes[i]; var data = mesh.GetCollisionData(); /*if (data.TriangleMesh != null) * physicsElementFactory.MeshPhysicsPool.PreloadTriangleMesh(engine.Scene, data.TriangleMesh);*/ } }; bool showPhysics = true; game.DrawEvent += delegate { if (game.Keyboard.IsKeyPressed(Keys.P)) { showPhysics = !showPhysics; } if (showPhysics) { debugRenderer.Render(game); } /*visualizer.RenderNodeGroundBoundig(game, root, * delegate(ClientPhysicsQuadTreeNode node, out Color col) * { * col = Color.Green; * * return node.PhysicsObjects.Count == 0; * }); * * visualizer.RenderNodeGroundBoundig(game, root, * delegate(ClientPhysicsQuadTreeNode node, out Color col) * { * col = Color.Orange; * * return node.PhysicsObjects.Count > 0; * });*/ for (int i = 0; i < physicsElements.Count; i++) { var el = physicsElements[i]; //game.LineManager3D.AddBox(BoundingBox.CreateFromSphere( el.BoundingSphere), Color.Orange); } for (int i = 0; i < spheres.Count; i++) { sphereMesh.WorldMatrix = Matrix.CreateTranslation(spheres[i].Center); sphereMesh.Render(game); } }; game.UpdateEvent += delegate { engine.Update(game.Elapsed); sphereMesh.Update(game); if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.F)) { var pEl = physicsElementFactory.CreateDynamicElement(crateMesh, Matrix.CreateTranslation( game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection)); pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30; var rEl = renderer.AddMesh(crateMesh); gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl)); } if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.E)) { var pEl = physicsElementFactory.CreateDynamicElement(barrelMesh, Matrix.CreateTranslation( game.SpectaterCamera.CameraPosition + game.SpectaterCamera.CameraDirection)); pEl.Actor.LinearVelocity = game.SpectaterCamera.CameraDirection * 30; var rEl = renderer.AddMesh(barrelMesh); gameMeshes.Add(new OBJParserTest.TestGameMesh(rEl, pEl)); } for (int i = 0; i < gameMeshes.Count; i++) { var m = gameMeshes[i]; m.RenderElement.WorldMatrix = m.PhysicsElement.World; } }; game.Run(); }