public bool Load(string filename) { // shadows Engine.Graphics.SceneManager.ShadowTechnique = ShadowTechnique.SHADOWTYPE_TEXTURE_MODULATIVE; Engine.Graphics.SceneManager.AmbientLight = this.ambientLight; Engine.Graphics.SceneManager.ShadowColour = this.ambientLight; if (!LoadWorldEntities()) { return(false); } // static mesh // TODO: implement depth shadow mapping // http://www.ogre3d.org/tikiwiki/Depth+Shadow+Mapping&bl=y&fullscreen=y staticGeometryCaster = Engine.Graphics.SceneManager.CreateStaticGeometry("StaticGeometryCaster"); staticGeometryReceiver = Engine.Graphics.SceneManager.CreateStaticGeometry("StaticGeometryReceiver"); staticGeometryCaster.CastShadows = true; StreamReader reader = new StreamReader(filename); string line = reader.ReadLine(); // skip header if (line != null) { line = reader.ReadLine(); } while (line != null) { string[] bits = line.Split('|'); string key = bits[0]; Vector3 position = StringConverter.ParseVector3(bits[1]); Quaternion orientation = StringConverter.ParseQuaternion(bits[2]); worldEntityMap[key].Spawn(position, orientation); line = reader.ReadLine(); } reader.Close(); staticGeometryCaster.Build(); staticGeometryReceiver.Build(); Engine.Graphics.Camera.Position = new Vector3(0, 0, 0); Engine.Graphics.Camera.Yaw(new Radian(new Degree(-15))); Engine.Sound.SetListenerPosition(Engine.Graphics.Camera.Position, Engine.Graphics.Camera.Direction); return(true); }
/// <summary> /// This method creates 4000 quads and attaches them to the scenegraph as static geometry /// </summary> public void StaticGeometry() { Quad(); staticGeo = mSceneMgr.CreateStaticGeometry("staticQuads"); // Initializes the static geometry container for (int i = 0; i < 200; i++) { for (int j = 0; j < 200; j++) { manualObjEntity = mSceneMgr.CreateEntity("Quad"); // Loads the quad in an entity staticGeo.AddEntity(manualObjEntity, new Vector3(i * 5, j * 5, 0)); // adds it to the scenegraph as static geo } } staticGeo.Build(); // Prepares the static geometry to be rendered }
public void StaticGeometry() { Quad(); staticGeometry = mSceneMgr.CreateStaticGeometry("staticQuads"); for (int i = 0; i < 200; i++) { for (int j = 0; j < 200; j++) { cubeEntity = mSceneMgr.CreateEntity("Quad"); staticGeometry.AddEntity(cubeEntity, new Vector3(i * 5, j * 5, 0)); } } staticGeometry.Build(); }
public bool Load(string filename) { // shadows Engine.Graphics.SceneManager.ShadowTechnique = ShadowTechnique.SHADOWTYPE_TEXTURE_MODULATIVE; Engine.Graphics.SceneManager.AmbientLight = this.ambientLight; Engine.Graphics.SceneManager.ShadowColour = this.ambientLight; if (!LoadWorldEntities()) return false; // static mesh // TODO: implement depth shadow mapping // http://www.ogre3d.org/tikiwiki/Depth+Shadow+Mapping&bl=y&fullscreen=y staticGeometryCaster = Engine.Graphics.SceneManager.CreateStaticGeometry("StaticGeometryCaster"); staticGeometryReceiver = Engine.Graphics.SceneManager.CreateStaticGeometry("StaticGeometryReceiver"); staticGeometryCaster.CastShadows = true; StreamReader reader = new StreamReader(filename); string line = reader.ReadLine(); // skip header if (line != null) line = reader.ReadLine(); while (line != null) { string[] bits = line.Split('|'); string key = bits[0]; Vector3 position = StringConverter.ParseVector3(bits[1]); Quaternion orientation = StringConverter.ParseQuaternion(bits[2]); worldEntityMap[key].Spawn(position, orientation); line = reader.ReadLine(); } reader.Close(); staticGeometryCaster.Build(); staticGeometryReceiver.Build(); Engine.Graphics.Camera.Position = new Vector3(0, 0, 0); Engine.Graphics.Camera.Yaw(new Radian(new Degree(-15))); Engine.Sound.SetListenerPosition(Engine.Graphics.Camera.Position, Engine.Graphics.Camera.Direction); return true; }
// Lock the node dictionary, and rebuild the static // geometry for objects of this kind protected void Rebuild(SceneManager mgr, Dictionary <long, WorldEntity> nodeDictionary) { log.DebugFormat("Entering StaticGeometryHelper.Rebuild for geometry '{0}'", name); long tickStart = TimeTool.CurrentTime; try { nodesAddedSinceLastRebuild = 0; nodesRemovedSinceLastRebuild = 0; force = false; Monitor.Enter(mgr); if (objectGeometry != null) { objectGeometry.Reset(); } else { objectGeometry = new StaticGeometry(mgr, name); } // Dictionary mapping Material into a list of // ObjectNodes in which some submesh uses the material Dictionary <Material, MaterialAndNodeCounts> materialsUsedMap = new Dictionary <Material, MaterialAndNodeCounts>(); lock (nodeDictionary) { foreach (WorldEntity entity in nodeDictionary.Values) { if (entity is ObjectNode) { ObjectNode node = (ObjectNode)entity; // For now, we only consider "props" that have an associated SceneNode // and are direct descendants of the root scene node, and are of the right // kind, i.e., don't have a perception radius if this static geometry is for // little nodes, and vice versa. // log.DebugFormat("StaticGeometry.Rebuild: Examining node {0}, oid {1}, type {2}, sceneNode {3}, InStaticGeometry {4}, top-level {5}", // node.Name, node.Oid, node.ObjectType, node.SceneNode, node.InStaticGeometry, node.SceneNode.Parent == mgr.RootSceneNode); if (node.ObjectType == ObjectNodeType.Prop && (node.InStaticGeometry || (node.SceneNode != null && node.SceneNode.Parent == mgr.RootSceneNode)) && RightKind(node)) { foreach (Material m in node.Entity.SubEntityMaterials) { MaterialAndNodeCounts nodesUsingMaterial; if (!materialsUsedMap.TryGetValue(m, out nodesUsingMaterial)) { nodesUsingMaterial = new MaterialAndNodeCounts(); materialsUsedMap[m] = nodesUsingMaterial; } nodesUsingMaterial.materialUseCount++; int subMeshUseCount; Dictionary <ObjectNode, int> submeshUseCounts = nodesUsingMaterial.submeshUseCounts; if (!submeshUseCounts.TryGetValue(node, out subMeshUseCount)) { submeshUseCounts[node] = 1; } else { submeshUseCounts[node] = subMeshUseCount + 1; } } } } } } // Now we have a count of uses of each material, and // for each node, the number of subentities that use the // material. Now we need to calculate the number of // instance of sharings for each object node Dictionary <ObjectNode, bool> candidateNodes = new Dictionary <ObjectNode, bool>(); foreach (MaterialAndNodeCounts counts in materialsUsedMap.Values) { if (counts.materialUseCount > 1) { foreach (KeyValuePair <ObjectNode, int> pair in counts.submeshUseCounts) { candidateNodes[pair.Key] = true; } } } Dictionary <ObjectNode, int> staticNodes = new Dictionary <ObjectNode, int>(); foreach (KeyValuePair <ObjectNode, bool> pair in candidateNodes) { ObjectNode candidate = pair.Key; bool useIt = pair.Value; if (useIt) { staticNodes[candidate] = 0; } } if (staticNodes.Count == 0) { log.InfoFormat("StaticGeometryHelper.Rebuild: Didn't build static geometry {0} because object count was zero", name); } else { log.InfoFormat("StaticGeometryHelper.Rebuild: {0} ObjectNodes", staticNodes.Count); foreach (ObjectNode staticNode in staticNodes.Keys) { SceneNode sc = staticNode.SceneNode; if (!staticNode.InStaticGeometry) { sc.RemoveFromParent(); staticNode.InStaticGeometry = true; } log.DebugFormat("StaticGeometryHelper.Rebuild: Add node {0} with name {1} to static geometry", staticNode.Oid, staticNode.Name); objectGeometry.AddSceneNode(sc); } } if (lastStaticNodes != null) { foreach (ObjectNode node in lastStaticNodes.Keys) { if (!staticNodes.ContainsKey(node)) { // Only 1 instance of the mesh, so make sure that if in a former build it was in // static geometry, that we add it back to the scene graph. if (node.InStaticGeometry) { SceneNode sn = node.SceneNode; if (sn != null) { mgr.RootSceneNode.AddChild(sn); } node.InStaticGeometry = false; } } } } if (staticNodes.Count > 0) { objectGeometry.Build(); } lastStaticNodes = staticNodes; timeOfLastRebuild = TimeTool.CurrentTime; } finally { Monitor.Exit(mgr); } log.InfoFormat("StaticGeometryHelper.Rebuild: Rebuild of geometry '{0}' took {1} ms", name, TimeTool.CurrentTime - tickStart); }
/// <summary> /// This method creates 4000 quads and attaches them to the scenegraph as static geometry /// </summary> public void StaticGeometry() { Quad(); staticGeo = mSceneMgr.CreateStaticGeometry("staticQuads"); // Initializes the static geometry container for(int i=0; i<200; i++) for (int j = 0; j < 200; j++) { manualObjEntity = mSceneMgr.CreateEntity("Quad"); // Loads the quad in an entity staticGeo.AddEntity(manualObjEntity, new Vector3(i * 5, j * 5, 0)); // adds it to the scenegraph as static geo } staticGeo.Build(); // Prepares the static geometry to be rendered }
/// <summary> /// initialize the scene /// </summary> private void InitializeScene() { //create scene mgr sceneMgr = root.CreateSceneManager(SceneType.ST_GENERIC); // Every viewport has a camera associated with it. // The second number is a z-order. Higher z-order viewports // are rendered on top (in the case of multiple viewports). Camera cam = sceneMgr.CreateCamera("cam"); root.AutoCreatedWindow.AddViewport(cam, 0); Viewport vp = root.AutoCreatedWindow.GetViewport(0); vp.BackgroundColour = ColourValue.Black; // have the frustum set the aspect ratio automatically cam.AutoAspectRatio = true; Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); Quaternion orientation = Quaternion.IDENTITY; // In Ogre, an entity is a renderable object. An entity must be attached // to a scene node, however, in order to be rendered. Every entity (really, // every object in Ogre) must be assigned a unique name. shipCam = new ArcballCamera(cam); shipCam.Radius = 250.0f; shipCam.Target = sceneMgr.RootSceneNode; Light l = sceneMgr.CreateLight("point1"); l.DiffuseColour = new ColourValue(1.0f, 1.0f, 1.0f); l.Position = Vector3.UNIT_Y * 100.0f; l.CastShadows = true; l.Type = Light.LightTypes.LT_POINT; sceneMgr.SetSkyBox(true, "Space", 5000); grid = sceneMgr.CreateStaticGeometry("grid"); float radius = 100.0f; Entity cube; const int NUM_CUBES_HALF_WIDTH = 2; for (int i = -NUM_CUBES_HALF_WIDTH; i < NUM_CUBES_HALF_WIDTH; ++i) { for (int j = -NUM_CUBES_HALF_WIDTH; j < NUM_CUBES_HALF_WIDTH; ++j) { for (int k = -NUM_CUBES_HALF_WIDTH; k < NUM_CUBES_HALF_WIDTH; ++k) { if (i != 0) { cube = sceneMgr.CreateEntity("cube-" + i + "-" + j + "-" + k, SceneManager.PrefabType.PT_CUBE); grid.AddEntity(cube, new Vector3(i, j, k) * radius * 10); } } } } grid.Build(); shipMgr = new ShipManager(this); }
/// <summary> /// initialize the scene /// </summary> private void InitializeScene() { //create scene mgr sceneMgr = root.CreateSceneManager(SceneType.ST_GENERIC); // Every viewport has a camera associated with it. // The second number is a z-order. Higher z-order viewports // are rendered on top (in the case of multiple viewports). Camera cam = sceneMgr.CreateCamera("cam"); root.AutoCreatedWindow.AddViewport(cam, 0); Viewport vp = root.AutoCreatedWindow.GetViewport(0); vp.BackgroundColour = ColourValue.Black; // have the frustum set the aspect ratio automatically cam.AutoAspectRatio = true; Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); Quaternion orientation = Quaternion.IDENTITY; // In Ogre, an entity is a renderable object. An entity must be attached // to a scene node, however, in order to be rendered. Every entity (really, // every object in Ogre) must be assigned a unique name. shipCam = new ArcballCamera(cam); shipCam.Radius = 250.0f; shipCam.Target = sceneMgr.RootSceneNode; Light l = sceneMgr.CreateLight("point1"); l.DiffuseColour = new ColourValue(1.0f, 1.0f, 1.0f); l.Position = Vector3.UNIT_Y * 100.0f; l.CastShadows = true; l.Type = Light.LightTypes.LT_POINT; sceneMgr.SetSkyBox(true, "Space", 5000); grid = sceneMgr.CreateStaticGeometry("grid"); float radius = 100.0f; Entity cube; const int NUM_CUBES_HALF_WIDTH = 2; for (int i = -NUM_CUBES_HALF_WIDTH; i < NUM_CUBES_HALF_WIDTH; ++i) for (int j = -NUM_CUBES_HALF_WIDTH; j < NUM_CUBES_HALF_WIDTH; ++j) for (int k = -NUM_CUBES_HALF_WIDTH; k < NUM_CUBES_HALF_WIDTH; ++k) { if (i != 0) { cube = sceneMgr.CreateEntity("cube-" + i + "-" + j + "-" + k, SceneManager.PrefabType.PT_CUBE); grid.AddEntity(cube, new Vector3(i, j, k) * radius * 10); } } grid.Build(); shipMgr = new ShipManager(this); }
public void CreateScene() { scene.SetSkyBox(true, "Examples/SpaceSkyBox"); SetupLighting(); Plane plane = new Plane(); plane.normal = Vector3.UNIT_Y; plane.d = 0; MeshManager.Singleton.CreatePlane("MyPlane", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane, 14500, 14500, 10, 10, true, 1, 50, 50, Vector3.UNIT_Z); Entity planeEnt = scene.CreateEntity("plane", "MyPlane"); planeEnt.SetMaterialName("Examples/GrassFloor"); planeEnt.CastShadows = false; scene.RootSceneNode.CreateChildSceneNode().AttachObject(planeEnt); Vector3 minV = new Vector3(-2000, 0, -2000); Vector3 maxV = new Vector3(2000, 0, 2000); CreateGrassMesh(); Entity e = scene.CreateEntity("1", GRASS_MESH_NAME); StaticGeometry s = scene.CreateStaticGeometry("bing");//, 1); s.RegionDimensions = new Vector3(1000, 1000, 1000); s.Origin = new Vector3(-500, 500, -500); //Set the region origin so the centre is at 0 world for (int x = -1950; x < 1950; x += 150) { for (int z = -1950; z < 1950; z += 150) { Vector3 pos = new Vector3(x + Math.RangeRandom(-25, 25), 0, z + Math.RangeRandom(-25, 25)); Quaternion orientation = new Quaternion(); orientation.FromAngleAxis(Math.RangeRandom(0, 359), Vector3.UNIT_Y); Vector3 scale = new Vector3(1, Math.RangeRandom(0.85f, 1.15f), 1); s.AddEntity(e, pos, orientation, scale); } } s.Build(); StaticGeom = s; //Mesh mesh = MeshManager.Singleton.Load("ogrehead.mesh", ResourceGroupManager.DefaultResourceGroupName); //short src, dest; //if (!mesh.SuggestTangentVectorBuildParams( VertexElementSemantic.VES_POSITION, out src, out dest)) { // mesh.BuildTangentVectors( VertexElementSemantic.VES_POSITION,src, dest); //} //e = scene.CreateEntity("head", "ogrehead.mesh"); //e.SetMaterialName("Examples/OffsetMapping/Specular"); //HeadNode = scene.RootSceneNode.CreateChildSceneNode(); //HeadNode.AttachObject(e); //HeadNode.Scale = new Vector3(7, 7, 7); //HeadNode.Position = new Vector3(0, 200, 0); //if (e.GetSubEntity(0).NormalizeNormals == false) { // LogManager.Singleton.LogMessage("aie aie aie"); //} Root.Singleton.RenderSystem.SetNormaliseNormals(true); //camera.Move(new Vector3(0, 350, 0)); }