示例#1
0
 static RockPopulator()
 {
     model = ((Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\rocks\\rock_0.obj")).GetChildGeom(0);
     model.Material.SetValue(Material.SPECULAR_EXPONENT, 90);
     model.Material.SetValue(Material.SHININESS, 0.3f);
     model.Material.SetValue(Material.ROUGHNESS, 0.12f);
 }
示例#2
0
 public void Init()
 {
     fbo.Init();
     Mesh mesh = MeshFactory.CreateQuad();
     mesh.Material.SetValue(Material.MATERIAL_DEPTHMASK, false);
     mesh.Material.SetValue(Material.MATERIAL_DEPTHTEST, false);
     quadGeom = new Geometry(mesh);
     postFilters.Add(new Filters.DefaultPostFilter());
 }
 public override void Init()
 {
     model = ((Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\dome.obj")).GetChildGeom(0);
     model.Material.SetValue(Material.MATERIAL_FACETOCULL, 1);
     model.Material.SetValue(Material.MATERIAL_DEPTHMASK, false);
     model.Material.SetValue(Material.MATERIAL_DEPTHTEST, false);
     model.Material.Bucket = RenderManager.Bucket.Sky;
     model.SetShader(typeof(SkyShader));
     model.SetLocalScale(new Math.Vector3f(100f));
     rootNode.AddChild(model);
 }
示例#4
0
 public override GameObject Clone()
 {
     Geometry res = new Geometry();
     res.SetLocalTranslation(this.GetLocalTranslation());
     res.SetLocalScale(this.GetLocalScale());
     res.SetLocalRotation(this.GetLocalRotation());
     res.Mesh = this.mesh;
     res.shader = this.shader;
     res.depthShader = this.depthShader;
     res.normalsShader = this.normalsShader;
     res.ShaderProperties = this.ShaderProperties;
     return res;
 }
示例#5
0
 static GrassPopulator()
 {
     model = ((Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\grass\\grass.obj")).GetChildGeom(0);
     model.Material.SetValue(Material.COLOR_DIFFUSE, new Vector4f(0.5f, 0.48f, 0.52f, 1f));
     model.Material.SetValue(Material.MATERIAL_BLENDMODE, 1);
     model.Material.SetValue(Material.MATERIAL_CASTSHADOWS, false);
     model.Material.SetValue(Material.MATERIAL_ALPHADISCARD, 0.2f);
     model.Material.SetValue(Material.SPECULAR_EXPONENT, 60);
     model.Material.SetValue(Material.SHININESS, 0.1f);
     model.Material.SetValue(Material.ROUGHNESS, 0.0f);
     model.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
     model.Material.Bucket = RenderManager.Bucket.Transparent;
 }
示例#6
0
 public override GameObject Clone()
 {
     Geometry res = new Geometry();
     res.SetLocalTranslation(this.GetLocalTranslation());
     res.SetLocalScale(this.GetLocalScale());
     res.SetLocalRotation(this.GetLocalRotation());
     res.Material = this.Material.Clone();
     res.Mesh = this.mesh;
     res.shader = this.shader;
     res.depthShader = this.depthShader;
     res.normalsShader = this.normalsShader;
     res.ShaderProperties = this.ShaderProperties;
     for (int i = 0; i < controls.Count; i++)
     {
         res.AddController(controls[i]);
     }
     return res;
 }
示例#7
0
        public override void Init()
        {
            ShadowMappingComponent smc;
            RenderManager.AddComponent(smc = new ShadowMappingComponent(cam, Environment, new int[] { 2048, 1024 }));
            smc.RenderMode = ShadowMappingComponent.ShadowRenderMode.Forward;

              //  Node floor = (Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\scene.apx");
              //  rootNode.AddChild(floor);
              //  PhysicsWorld.AddObject(floor, 0);
            Geometry cube = new Geometry(MeshFactory.CreateCube(new Vector3f(-15, -0.5f, -15f), new Vector3f(15f, 0.5f, 15f)));
            rootNode.AddChild(cube);
            PhysicsWorld.AddObject(cube, 0);

            Node n = (Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\monkeyhq.obj");
            n.SetLocalTranslation(new Vector3f(0, 50, 0));
            rootNode.AddChild(n);
            //PhysicsWorld.AddObject(n, 5, Scene.Physics.PhysicsWorld.PhysicsShape.Box);
            PhysicsWorld.AddCharacter((DefaultCamera)cam, InputManager, n, 1);
        }
示例#8
0
        public override void Init()
        {
            // ShadowMappingComponent smc;
               // RenderManager.AddComponent(smc = new ShadowMappingComponent(cam, Environment));
               // smc.RenderMode = ShadowMappingComponent.ShadowRenderMode.Forward;

            Geometry floor = new Geometry(MeshFactory.CreateCube(new Math.Vector3f(-5f, -0.5f, -5f), new Math.Vector3f(5, 0.5f, 5f)));
            rootNode.AddChild(floor);
            PhysicsWorld.AddObject(floor, 0.0f);

            Node test = (Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\sphere16.obj");
            test.SetLocalTranslation(new Math.Vector3f(0, 25, 0));
            rootNode.AddChild(test);

            PhysicsWorld.AddObject(test, 5.0f);

            Sound cat = (Sound)AssetManager.Load(AssetManager.GetAppPath() + "\\sounds\\cat.wav");
            AudioNode an = new AudioNode(cat);
            test.AddChild(an);

            InputManager.AddKeyboardEvent(new Input.KeyboardEvent(Input.InputManager.KeyboardKey.Space, () => { an.Play(); }));
        }
示例#9
0
        static TreePopulator()
        {
            /*  tree = (Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\tree\\pine\\LoblollyPine.obj");

            tree.GetChildGeom(1).Material.SetValue("tree_height", tree.GetLocalBoundingBox().Max.y);
            tree.GetChildGeom(1).SetShader(typeof(BarkShader));

            tree.GetChildGeom(1).DepthShader = ShaderManager.GetShader(typeof(BarkShader), new ShaderProperties()
                .SetProperty("DEPTH", true)
                .SetProperty(Material.TEXTURE_DIFFUSE, true));

            tree.GetChildGeom(2).Material.SetValue("tree_height", tree.GetLocalBoundingBox().Max.y);
            tree.GetChildGeom(2).Material.SetValue(Material.MATERIAL_BLENDMODE, 1);
            tree.GetChildGeom(2).Material.SetValue(Material.MATERIAL_CULLENABLED, false);
            tree.GetChildGeom(2).Material.SetValue(Material.MATERIAL_ALPHADISCARD, 0.7f);
            tree.GetChildGeom(2).Material.Bucket = Rendering.RenderManager.Bucket.Transparent;
            tree.GetChildGeom(2).SetShader(typeof(LeafShader));

            tree.GetChildGeom(2).DepthShader = ShaderManager.GetShader(typeof(LeafShader), new ShaderProperties()
                .SetProperty("DEPTH", true)
                .SetProperty(Material.TEXTURE_DIFFUSE, true));*/

            tree = new Node();

            Geometry g = new Geometry(MeshFactory.CreateQuad());
            g.SetLocalScale(new Vector3f(15));
            g.SetLocalTranslation(new Vector3f(0, 10, 0));
            g.Material.SetValue(Material.MATERIAL_CASTSHADOWS, 0);
            g.Material.SetValue(Material.MATERIAL_BLENDMODE, 1);
            g.Material.SetValue(Material.TEXTURE_DIFFUSE, AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\models\\tree\\pine\\billboard.png"));
            g.Material.SetValue(Material.SHININESS, 0.0f);
            g.Material.SetValue(Material.MATERIAL_ALPHADISCARD, 0.5f);
            g.Material.Bucket = RenderManager.Bucket.Transparent;
            g.SetShader(typeof(GrassShader));
            g.Material.SetValue("fade_start", 250.0f);
            g.Material.SetValue("fade_end", 260.0f);
            tree.AddChild(g);
        }
示例#10
0
        private void NrmGenerator_Load(object sender, EventArgs e)
        {
            Mesh quadMesh = MeshFactory.CreateQuad();
            quadGeom = new Geometry(quadMesh);
            quadGeom.SetShader(ShaderManager.GetShader(typeof(NormalMapShader)));
            MtlViewerGame mtlPreview = new MtlViewerGame();
            mtlPreview.Rotate = false;
            mtlPreview.Camera.Enabled = false;
            mtlPreview.Camera.Width = 256;
            mtlPreview.Camera.Height = 256;

              //  mtlPreview.Camera = new ApexEngine.Rendering.Cameras.OrthoCamera(-2, 2, -2, 2, -2, 2);
            mtlPreview.Camera.Translation = new ApexEngine.Math.Vector3f(0, 0, -3);
            mtlPreview.RootNode.AddChild(quadGeom);
            ApexEngineControl mtlViewer = new ApexEngineControl(mtlPreview);
            mtlViewer.Framerate = 50;
            mtlViewer.Dock = DockStyle.Fill;
            pnlObj.Controls.Add(mtlViewer);

            quadGeom.Material.SetValue("delta_value", 5.0f);
            //mtlPreview.RenderManager.PostProcessor.PostFilters.Add(new BlurPostFilter(true));
            //mtlPreview.RenderManager.PostProcessor.PostFilters.Add(new BlurPostFilter(false));
        }
        public override object Load(LoadedAsset asset)
        {
            XmlReader xmlReader = XmlReader.Create(asset.Data);
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    if (xmlReader.Name == "position")
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));
                        Vector3f vec = new Vector3f(x, y, z);
                        positions.Add(vec);
                    }
                    else if (xmlReader.Name == "normal")
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));
                        Vector3f vec = new Vector3f(x, y, z);
                        normals.Add(vec);
                    }
                    else if (xmlReader.Name == "texcoord")
                    {
                        float x = float.Parse(xmlReader.GetAttribute(0));
                        float y = float.Parse(xmlReader.GetAttribute(1));
                        Vector2f tc = new Vector2f(x, -y);
                        texCoords.Add(tc);
                    }
                    else if (xmlReader.Name == "face")
                    {
                        if (!useSubmeshes)
                        {
                            faces.Add(int.Parse(xmlReader.GetAttribute(0)));
                            faces.Add(int.Parse(xmlReader.GetAttribute(1)));
                            faces.Add(int.Parse(xmlReader.GetAttribute(2)));
                        }
                        else if (useSubmeshes)
                        {
                            CurrentSubmesh().faces.Add(int.Parse(xmlReader.GetAttribute(0)));
                            CurrentSubmesh().faces.Add(int.Parse(xmlReader.GetAttribute(1)));
                            CurrentSubmesh().faces.Add(int.Parse(xmlReader.GetAttribute(2)));
                        }
                    }
                    else if (xmlReader.Name == "skeletonlink")
                    {
                        string parentPath = System.IO.Directory.GetParent(asset.FilePath).ToString();
                        string skeletonPath = parentPath + "\\" + xmlReader.GetAttribute(0) + ".xml";
                        skeleton = (Skeleton)AssetManager.Load(skeletonPath, OgreXmlSkeletonLoader.GetInstance());
                    }
                    else if (xmlReader.Name == "vertexboneassignment" || xmlReader.Name == "boneassignment")
                    {
                        int vidx = int.Parse(xmlReader.GetAttribute("vertexindex"));
                        float boneWeight = float.Parse(xmlReader.GetAttribute("weight"));
                        int boneIndex = int.Parse(xmlReader.GetAttribute("boneindex"));
                        AddToBoneAssigns(vidx, new BoneAssign(vidx, boneWeight, boneIndex));
                    }
                    else if (xmlReader.Name == "submesh")
                    {
                        useSubmeshes = true;
                        if (xmlReader.GetAttribute("operationtype") != null)
                        {
                            // material
                            Submesh sm = new Submesh();
                            subMeshes.Add(sm);
                        }
                    }
                }
            }
            xmlReader.Close();
            List<Vertex> vertices = new List<Vertex>();
            if (!useSubmeshes)
            {
                LoopThrough(faces, ref vertices);
            }
            else
            {
                for (int i = subMeshes.Count - 1; i > -1; i--)
                {
                    Submesh s = subMeshes[i];
                    if (s.faces.Count > 0)
                        LoopThrough(s.faces, ref s.vertices);
                    else
                        subMeshes.Remove(s);
                }
            }

            if (skeleton.GetNumBones() > 0)
            {
                for (int i = 0; i < skeleton.GetNumBones(); i++)
                    skeleton.GetBone(i).SetToBindingPose();
                skeleton.GetBone(0).CalculateBindingRotation();
                skeleton.GetBone(0).CalculateBindingTranslation();
                for (int i = 0; i < skeleton.GetNumBones(); i++)
                {
                    skeleton.GetBone(i).StoreBindingPose();
                    skeleton.GetBone(i).ClearPose();
                }
                skeleton.GetBone(0).UpdateTransform();
            }
            bool hasAnimations = skeleton.GetAnimations().Count > 0;
            AnimationController animControl = new AnimationController(skeleton);
            Node res = new Node();
            if (!useSubmeshes)
            {
                Mesh mesh = new Mesh();
                mesh.SetSkeleton(skeleton);
                mesh.SetVertices(vertices);
                Geometry geom = new Geometry();
                geom.Mesh = mesh;
                res.AddChild(geom);
            }
            else
            {
                for (int i = 0; i < subMeshes.Count; i++)
                {
                    Submesh sm = subMeshes[i];

                    Mesh mesh = new Mesh();
                    mesh.SetSkeleton(skeleton);
                    mesh.SetVertices(sm.vertices);
                    Geometry geom = new Geometry();
                    geom.Mesh = mesh;
                    res.AddChild(geom);
                }
            }
            if (hasAnimations)
            {
                res.AddController(animControl);
            }
            ResetLoader();
            return res;
        }
示例#12
0
 /// <summary>
 /// Calculates the bounding box of the mesh and sets the mesh to [0, 0, 0].
 /// </summary>
 /// <param name="mesh"></param>
 public static void SetToOrigin(Geometry geom)
 {
     Mesh mesh = geom.Mesh;
     if (mesh.BoundingBox == null)
         mesh.BoundingBox = mesh.CreateBoundingBox();
     Vector3f difference = geom.GetLocalTranslation().Subtract(mesh.BoundingBox.Center);
     for (int i = 0; i < mesh.indices.Count; i++)
     {
         mesh.vertices[mesh.indices[i]].SetPosition(mesh.vertices[mesh.indices[i]].GetPosition().Add(difference));
     }
     mesh.SetVertices(mesh.vertices, mesh.indices);
     if (geom.GetController(typeof(Scene.Physics.RigidBodyControl)) != null)
     {
         Scene.Physics.RigidBodyControl rbc = (Scene.Physics.RigidBodyControl)geom.GetController(typeof(Scene.Physics.RigidBodyControl));
     }
     geom.SetLocalTranslation(mesh.BoundingBox.Center);
 }
示例#13
0
 private static void SaveGeometry(Geometry geom, XmlWriter writer)
 {
     writer.WriteStartElement(TOKEN_GEOMETRY);
     if (geom.Name != "root")
         writer.WriteAttributeString(TOKEN_NAME, geom.Name);
     else
         writer.WriteAttributeString(TOKEN_NAME, "root_model");
     writer.WriteStartElement(TOKEN_TRANSLATION);
     writer.WriteAttributeString("x", geom.GetLocalTranslation().x.ToString());
     writer.WriteAttributeString("y", geom.GetLocalTranslation().y.ToString());
     writer.WriteAttributeString("z", geom.GetLocalTranslation().z.ToString());
     writer.WriteStartElement(TOKEN_SCALE);
     writer.WriteAttributeString("x", geom.GetLocalScale().x.ToString());
     writer.WriteAttributeString("y", geom.GetLocalScale().y.ToString());
     writer.WriteAttributeString("z", geom.GetLocalScale().z.ToString());
     writer.WriteEndElement();
     writer.WriteStartElement(TOKEN_ROTATION);
     writer.WriteAttributeString("x", geom.GetLocalRotation().x.ToString());
     writer.WriteAttributeString("y", geom.GetLocalRotation().y.ToString());
     writer.WriteAttributeString("z", geom.GetLocalRotation().z.ToString());
     writer.WriteAttributeString("w", geom.GetLocalRotation().w.ToString());
     writer.WriteEndElement();
     writer.WriteEndElement();
     if (geom.Mesh != null)
     {
         SaveMesh(geom.Mesh, writer);
     }
     SaveMaterial(geom.Material, writer);
     writer.WriteEndElement();
 }
示例#14
0
        public override void Init()
        {
            Environment.AmbientLight.Color.Set(0.4f, 0.25f, 0.1f, 1.0f);
            Environment.FogColor.Set(0.2f, 0.3f, 0.45f, 1.0f);
            Environment.DirectionalLight.Direction.Set(0.6f, 1, 0.6f).NormalizeStore();
            Environment.DirectionalLight.Color.Set(1.0f, 0.85f, 0.65f, 1.0f);
            ((PerspectiveCamera)Camera).FieldOfView = 75;
            Camera.Far = 330;

            ShadowMappingComponent smc;
            RenderManager.AddComponent(smc = new ShadowMappingComponent(cam, Environment, new int[] { 2048, 1024 }));
            smc.RenderMode = ShadowMappingComponent.ShadowRenderMode.Forward;

            RenderManager.PostProcessor.PostFilters.Add(new FXAAFilter());

            const float curb_scale_y = 0.4f;

            road = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\road.jpg");
            roadnrm = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\road_nrm.jpg");
            roadCorner = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\road_corner.jpg");
            roadCornernrm = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\road_corner_nrm.jpg");
            curbModel = AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\curb.obj");
            curbCorner = AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\curb_corner.obj");
            curbCorner.SetLocalScale(new Vector3f(1, curb_scale_y, 1));

            Random rand = new Random();

            int xAmt = rand.Next(2, 7), zAmt = rand.Next(2, 7), scaleX = rand.Next(10, 30), scaleZ = rand.Next(10, 30);

            int offset = 3;

            AddRoadCorner(new Line(new Vector3f(0, 0, -2), new Vector3f(0, 0, 0)));

            for (int x = 0; x <= xAmt; x++)
            {
                for (int i = 0; i < zAmt; i++)
                {
                    AddRoad(new Line(new Vector3f(x * scaleX, 0, i * scaleZ), new Vector3f(x * scaleX, 0, (i + 1) * scaleZ - 2)));
                    AddRoadCorner(new Line(new Vector3f(x * scaleX, 0, (i + 1) * scaleZ - 2), new Vector3f(x * scaleX, 0, (i + 1) * scaleZ)));

                    if (x == 0)
                    {
                        // long bar
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set(x * scaleX - 1, 0, (i + 1) * scaleZ - 1);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180));
                        curb2.SetLocalScale(new Vector3f(1, curb_scale_y, scaleZ));
                        curbNode.AddChild(curb2);

                    }
                    else if (x == (xAmt - 1))
                    {
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set((x + 1) * scaleX + 1, 0, (i) * scaleZ - 1);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalScale(new Vector3f(1, curb_scale_y, scaleZ));
                        curbNode.AddChild(curb2);
                    }

                    Node curb0 = (Node)curbModel.Clone();
                    tmpVec.Set(x * scaleX + 1, 0, i * scaleZ + 1);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb0.SetLocalTranslation(tmpVec);
                    curb0.SetLocalScale(new Vector3f(1, curb_scale_y, scaleZ - offset - 1));
                    curbNode.AddChild(curb0);

                    Node curb1 = (Node)curbModel.Clone();
                    tmpVec.Set(x * scaleX - 1, 0, (i + 1) * scaleZ - offset);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb1.SetLocalTranslation(tmpVec);
                    curb1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180));
                    curb1.SetLocalScale(new Vector3f(1, curb_scale_y, scaleZ - offset - 1));
                    curbNode.AddChild(curb1);

                    if (x != 0)
                    {
                        Node curbCorner1 = (Node)curbCorner.Clone();
                        tmpVec.Set((x) * scaleX - 2, 0, (i + 1) * scaleZ - offset + 1);
                        tmpVec.y = HeightRoad(tmpVec);
                        curbCorner1.SetLocalTranslation(tmpVec);
                        curbCorner1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90));
                        curbNode.AddChild(curbCorner1);
                    }

                    if (x != xAmt)
                    {
                        Node curbCorner0 = (Node)curbCorner.Clone();
                        tmpVec.Set(x * scaleX + 2, 0, i * scaleZ);
                        tmpVec.y = HeightRoad(tmpVec);
                        curbCorner0.SetLocalTranslation(tmpVec);
                        curbCorner0.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 90));
                        curbNode.AddChild(curbCorner0);
                    }
                }
            }

            for (int z = 0; z <= zAmt; z++)
            {
                for (int i = 0; i < xAmt; i++)
                {
                    AddRoad(new Line(new Vector3f(i * scaleX + 1, 0, z * scaleZ - 1), new Vector3f((i + 1) * scaleX - 1, 0, z * scaleZ - 1)));
                    AddRoadCorner(new Line(new Vector3f((i + 1) * scaleX - 1, 0, z * scaleZ - 1), new Vector3f((i + 1) * scaleX + 1, 0, z * scaleZ - 1)));

                    // long pieces
                    if (z == 0)
                    {
                        // long bar
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set(i * scaleX, 0, z * scaleZ - 2);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90));
                        curb2.SetLocalScale(new Vector3f(scaleX, curb_scale_y, 1));
                        curbNode.AddChild(curb2);
                    }
                    else if (z == (zAmt - 1))
                    {
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set((i + 1) * scaleX, 0, (z + 1) * scaleZ);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90 + 180));
                        curb2.SetLocalScale(new Vector3f(scaleX, curb_scale_y, 1));
                        curbNode.AddChild(curb2);
                    }

                    Node curb0 = (Node)curbModel.Clone();
                    tmpVec.Set((i + 1) * scaleX - 2, 0, z * scaleZ);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb0.SetLocalTranslation(tmpVec);
                    curb0.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 90));
                    curb0.SetLocalScale(new Vector3f(scaleX - offset - 1, curb_scale_y, 1));
                    curbNode.AddChild(curb0);

                    Node curb1 = (Node)curbModel.Clone();
                    tmpVec.Set((i) * scaleX + offset - 1, 0, z * scaleZ - 2);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb1.SetLocalTranslation(tmpVec);
                    curb1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90));
                    curb1.SetLocalScale(new Vector3f(scaleX - offset - 1, curb_scale_y, 1));
                    curbNode.AddChild(curb1);

                    if (z != zAmt)
                    {
                        Node curbCorner0 = (Node)curbCorner.Clone();
                        Vector3f curbVec1 = new Vector3f((i + 1) * scaleX - 1, 0, z * scaleZ + 1);
                        curbVec1.y = HeightRoad(curbVec1);
                        curbCorner0.SetLocalTranslation(curbVec1);
                        curbCorner0.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180));
                        curbNode.AddChild(curbCorner0);
                    }
                    if (z != 0)
                    {
                        Node curbCorner1 = (Node)curbCorner.Clone();
                        Vector3f curbVec1 = new Vector3f((i) * scaleX + offset - 2, 0, z * scaleZ - 3);
                        curbVec1.y = HeightRoad(curbVec1);
                        curbCorner1.SetLocalTranslation(curbVec1);
                        curbCorner1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 360));
                        curbNode.AddChild(curbCorner1);
                    }
                }
            }

            Geometry roadMerged = new Geometry(MeshUtil.MergeMeshes(roadNode));
            Geometry curbMerged = new Geometry(MeshUtil.MergeMeshes(curbNode));

            rootNode.AddChild(curbMerged);

            roadMerged.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
            roadMerged.Material.SetValue(Material.TEXTURE_DIFFUSE, road);
            roadMerged.Material.SetValue(Material.TEXTURE_NORMAL, roadnrm);
            roadMerged.Material.SetValue(Material.SHININESS, 0.3f);
            roadMerged.Material.SetValue(Material.ROUGHNESS, 0.3f);

            Geometry roadCornerMerged = new Geometry(MeshUtil.MergeMeshes(roadCornerNode));

            roadCornerMerged.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
            roadCornerMerged.Material.SetValue(Material.TEXTURE_DIFFUSE, roadCorner);
            roadCornerMerged.Material.SetValue(Material.TEXTURE_NORMAL, roadCornernrm);
            roadCornerMerged.Material.SetValue(Material.SHININESS, 0.3f);
            roadCornerMerged.Material.SetValue(Material.ROUGHNESS, 0.3f);

            rootNode.AddChild(roadCornerMerged);
            rootNode.AddChild(roadMerged);

            /*

                        addroad(new line(new vector3f(0, 0, 0), new vector3f(0, 0, 10)));
                        AddRoad(new Line(new Vector3f(0, 0, 10), new Vector3f(1, 0, 12)));
                        AddRoad(new Line(new Vector3f(1, 0, 12), new Vector3f(5, 0, 12)));*/

            /*

            for (int x = 0; x < 16; x ++)
            {
                    AddRoad(new Line(new Vector3f((float)System.Math.Sin(x)*13, 0, (float)System.Math.Cos(x) * 13), new Vector3f((float)System.Math.Sin(x+1) * 13, 0, (float)System.Math.Cos(x+1) * 13)));
            }
            */
            /*     AddRoad(new Line(new Vector3f(0, 0, 10), new Vector3f(-3, 0, 11)));
                 AddRoad(new Line(new Vector3f(-3, 0, 11), new Vector3f(-4, 0, 12)));
                 AddRoad(new Line(new Vector3f(-4, 0, 12), new Vector3f(-5, 0, 13)));
                 AddRoad(new Line(new Vector3f(-5, 0, 13), new Vector3f(-6, 0, 14)));*/
            //    AddRoad(new Line(new Vector3f(0, 0, 10), new Vector3f(10, 0, 10)));

            /*  for (int i = 0; i < 4; i++)
              {
                  Line myline = new Line(lastLine.To, lastLine.To.Add(new Vector3f(rand.Next(-15, 15),0, rand.Next(-15, 15))));
                  Geometry quad = new Geometry(QuadFromLine(myline));
                  quad.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
                  quad.Material.SetValue(Material.TEXTURE_DIFFUSE, road);
                  quad.Material.SetValue(Material.SHININESS, 0.0f);
                  lastLine = myline;

                  rootNode.AddChild(quad);
              }
              */
        }
示例#15
0
 public static Geometry MergeGeometry(GameObject gameObject)
 {
     List<Mesh> meshes;
     List<Matrix4f> transforms = new List<Matrix4f>();
     List<Material> materials = new List<Material>();
     List<Shader> shaders = new List<Shader>();
     meshes = RenderUtil.GatherMeshes(gameObject, materials, transforms, shaders);
     Mesh m =  MergeMeshes(meshes, materials, transforms);
     Geometry geom = new Geometry(m);
     if (shaders.Count > 0)
         geom.SetShader(shaders[0]);
     if (materials.Count > 0)
         geom.Material = materials[0];
     return geom;
 }
示例#16
0
        public override void Init()
        {
            Environment.AmbientLight.Color.Set(0.1f, 0.1f, 0.1f, 1.0f);
            Environment.FogColor.Set(0.2f, 0.3f, 0.45f, 1.0f);
            Environment.DirectionalLight.Direction.Set(1f, 1, 1f).NormalizeStore();
            Environment.DirectionalLight.Color.Set(1.0f, 1.0f, 1.0f, 1.0f);
            ((PerspectiveCamera)Camera).FieldOfView = 75;
            Camera.Far = 330;

            ShadowMappingComponent smc;
            RenderManager.AddComponent(smc = new ShadowMappingComponent(cam, Environment, new int[] {2048,1024}));
            smc.RenderMode = ShadowMappingComponent.ShadowRenderMode.Forward;

            /*Rendering.Light.PointLight pl = new Rendering.Light.PointLight();
            pl.Color = new Color4f(1f, 0.0f, 0.0f, 1.0f);
            pl.Position = new Vector3f(0.0f, 4.0f, 0.0f);
            Environment.PointLights.Add(pl);*/

              /*  Rendering.Light.PointLight pl2 = new Rendering.Light.PointLight();
            pl2.Color = new Color4f(0.9f, 0.3f, 0.2f, 1.0f);
            pl2.Position = new Vector3f(0.0f, 3.0f, 7.0f);
            Environment.PointLights.Add(pl2);*/

            Geometry skyboxGeom = new Geometry(MeshFactory.CreateCube(new Vector3f(-10), new Vector3f(10)));
            skyboxGeom.Material.SetValue(Material.MATERIAL_FACETOCULL, 1);
            skyboxGeom.Material.SetValue(Material.MATERIAL_CASTSHADOWS, false);
               // rootNode.AddChild(skyboxGeom);

            GameObject grid = AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\grid\\grid.obj");
            rootNode.AddChild(grid);

            Node loadedApx = (Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\mitsuba.obj");
               // loadedApx.SetLocalTranslation(new Math.Vector3f(0f, 0, 0));

            Cubemap skybox = Cubemap.LoadCubemap(new string[] { AssetManager.GetAppPath() + "\\textures\\lostvalley\\lostvalley_right.jpg",
                                                                 AssetManager.GetAppPath() + "\\textures\\lostvalley\\lostvalley_left.jpg",
                                                                 AssetManager.GetAppPath() + "\\textures\\lostvalley\\lostvalley_top.jpg",
                                                                 AssetManager.GetAppPath() + "\\textures\\lostvalley\\lostvalley_top.jpg",
                                                                 AssetManager.GetAppPath() + "\\textures\\lostvalley\\lostvalley_front.jpg",
                                                                 AssetManager.GetAppPath() + "\\textures\\lostvalley\\lostvalley_back.jpg" });

               // Texture nrm = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\normal_metal.jpg");

            Random rand = new Random();
            for (int x = 0; x < 5; x++)
            {
                for (int z = 0; z < 5; z++)
                {
                    Node thing = (Node)loadedApx.Clone();
                    thing.SetLocalTranslation(new Vector3f((x * 3) - 6, 5, (z * 3) - 6));
                    thing.GetChildGeom(0).Material.SetValue(Material.TEXTURE_ENV, skybox);
                    thing.GetChildGeom(0).Material.SetValue(Material.SHININESS, 0.5f);
                    thing.GetChildGeom(0).Material.SetValue(Material.ROUGHNESS, 0.3f);
                    thing.GetChildGeom(0).UpdateShaderProperties();

                    thing.GetChildGeom(1).Material.SetValue(Material.COLOR_DIFFUSE, new Color4f(1f, 0f, 0f, 1.0f));
                    thing.GetChildGeom(1).Material.SetValue(Material.TEXTURE_ENV, skybox);
                    thing.GetChildGeom(1).Material.SetValue(Material.SHININESS, (x+1) / 5.0f);
                    thing.GetChildGeom(1).Material.SetValue(Material.ROUGHNESS, (z+1) / 5.0f);
                    thing.GetChildGeom(1).UpdateShaderProperties();
               //     thing.GetChildGeom(1).Material.SetValue(Material.TEXTURE_NORMAL, nrm);
                    rootNode.AddChild(thing);
                }
            }
               /* Node thing = (Node)AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\pbr\\sibenik.obj");
            for (int i = 0; i < thing.Children.Count; i++)
            {
                thing.GetChildGeom(i).Material.SetValue(Material.TEXTURE_ENV, skybox);
                thing.GetChildGeom(i).Material.SetValue(Material.SHININESS, 0.2f);
                thing.GetChildGeom(i).Material.SetValue(Material.ROUGHNESS, 0.2f);
                thing.GetChildGeom(i).UpdateShaderProperties();
            }
            rootNode.AddChild(thing);*/

            RenderManager.PostProcessor.PostFilters.Add(new FXAAFilter());
        }
示例#17
0
 public void RemoveGeometry(Geometry geom)
 {
     if (geometries.Contains(geom))
         geometries.Remove(geom);
 }
示例#18
0
 public void AddGeometry(Geometry geom)
 {
     if (!geometries.Contains(geom))
         geometries.Add(geom);
 }
示例#19
0
        public override object Load(LoadedAsset asset)
        {
            XmlReader xmlReader = XmlReader.Create(asset.Data);
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    if (xmlReader.Name == ApxExporter.TOKEN_NODE)
                    {
                        node = true;
                        geom = false;
                        string name = xmlReader.GetAttribute(ApxExporter.TOKEN_NAME);
                        Node n = new Node(name);
                        if (lastNode != null)
                            lastNode.AddChild(n);
                        lastNode = n;
                        nodes.Add(n);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_GEOMETRY)
                    {
                        node = false;
                        geom = true;
                        string name = xmlReader.GetAttribute(ApxExporter.TOKEN_NAME);
                        Geometry g = new Geometry();
                        g.Name = name;
                        if (lastNode != null)
                            lastNode.AddChild(g);
                        geoms.Add(g);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_MATERIAL)
                    {
                        Material mat = new Material();
                        mats.Add(mat);
                        string bucketStr = xmlReader.GetAttribute(ApxExporter.TOKEN_MATERIAL_BUCKET);
                        RenderManager.Bucket bucket;
                        if (bucketStr != null)
                            Enum.TryParse<RenderManager.Bucket>(bucketStr, out bucket);
                        else
                            bucket = RenderManager.Bucket.Opaque;
                        Console.WriteLine("Bucket: " + bucket.ToString());
                        mat.Bucket = bucket;
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_MATERIAL_PROPERTY)
                    {
                        Material lastMaterial = mats[mats.Count - 1];

                        string name = xmlReader.GetAttribute(ApxExporter.TOKEN_NAME);
                        string type = xmlReader.GetAttribute(ApxExporter.TOKEN_TYPE);
                        string val = xmlReader.GetAttribute(ApxExporter.TOKEN_VALUE);

                        object value = null;
                        if (type == ApxExporter.TOKEN_TYPE_STRING)
                            value = val;
                        else if (type == ApxExporter.TOKEN_TYPE_INT)
                            value = int.Parse(val);
                        else if (type == ApxExporter.TOKEN_TYPE_BOOLEAN)
                            value = bool.Parse(val);
                        else if (type == ApxExporter.TOKEN_TYPE_FLOAT)
                            value = float.Parse(val);
                        else if (type == ApxExporter.TOKEN_TYPE_VECTOR2)
                            value = ParseVector2(val);
                        else if (type == ApxExporter.TOKEN_TYPE_VECTOR3)
                            value = ParseVector3(val);
                        else if (type == ApxExporter.TOKEN_TYPE_VECTOR4)
                            value = ParseVector4(val);
                        else if (type == ApxExporter.TOKEN_TYPE_TEXTURE)
                        {
                            string texPath = val;
                            string parentPath = System.IO.Directory.GetParent(asset.FilePath).ToString();
                            string finalTexPath = parentPath + "\\" + texPath;
                            if (System.IO.File.Exists(finalTexPath))
                            {
                                value = AssetManager.LoadTexture(finalTexPath);
                            }
                            else if (System.IO.File.Exists(texPath)) // absolute path
                            {
                                value = AssetManager.LoadTexture(texPath);
                            }
                            else
                                value = null;
                        }

                        lastMaterial.SetValue(name, value);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_TRANSLATION)
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));

                        Vector3f vec = new Vector3f(x, y, z);
                        GameObject go = null;
                        if (node)
                            go = nodes[nodes.Count - 1];
                        else if (geom)
                            go = geoms[geoms.Count - 1];
                        go.SetLocalTranslation(vec);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_ROTATION)
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));
                        float w = float.Parse(xmlReader.GetAttribute("w"));

                        Quaternion quat = new Quaternion(x, y, z, w);
                        GameObject go = null;
                        if (node)
                            go = nodes[nodes.Count - 1];
                        else if (geom)
                            go = geoms[geoms.Count - 1];
                        go.SetLocalRotation(quat);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_SCALE)
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));

                        Vector3f vec = new Vector3f(x, y, z);
                        GameObject go = null;
                        if (node)
                            go = nodes[nodes.Count - 1];
                        else if (geom)
                            go = geoms[geoms.Count - 1];
                        go.SetLocalScale(vec);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_MESH)
                    {
                        meshes.Add(new Mesh());
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_VERTICES)
                    {
                        List<Vertex> newVList = new List<Vertex>();
                        vertices.Add(newVList);

                        List<Vector3f> newPList = new List<Vector3f>();
                        positions.Add(newPList);

                        List<Vector3f> newNList = new List<Vector3f>();
                        normals.Add(newNList);

                        List<Vector2f> newT0List = new List<Vector2f>();
                        texcoords0.Add(newT0List);

                        List<Vector2f> newT1List = new List<Vector2f>();
                        texcoords1.Add(newT1List);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_POSITION)
                    {
                        List<Vector3f> pos = positions[positions.Count - 1];

                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));

                        Vector3f position = new Vector3f(x, y, z);
                        pos.Add(position);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_NORMAL)
                    {
                        List<Vector3f> nor = normals[normals.Count - 1];

                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));

                        Vector3f normal = new Vector3f(x, y, z);
                        nor.Add(normal);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_TEXCOORD0)
                    {
                        List<Vector2f> tc0 = texcoords0[texcoords0.Count - 1];

                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));

                        Vector2f tc = new Vector2f(x, y);
                        tc0.Add(tc);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_TEXCOORD1)
                    {
                        List<Vector2f> tc1 = texcoords1[texcoords1.Count - 1];

                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));

                        Vector2f tc = new Vector2f(x, y);
                        tc1.Add(tc);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_FACES)
                    {
                        faces.Add(new List<int>());
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_FACE)
                    {
                        List<int> fList = faces[faces.Count - 1];
                        for (int i = 0; i < 3; i++)
                        {
                            string val = xmlReader.GetAttribute("i" + i.ToString());
                            if (val != "")
                            {
                                string[] tokens = val.Split('/');
                                for (int j = 0; j < tokens.Length; j++)
                                {
                                    fList.Add(int.Parse(tokens[j]));
                                }
                            }
                        }
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_SKELETON)
                    {
                        skeletons.Add(new Skeleton());
                        bones.Add(new List<Bone>());
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_SKELETON_ASSIGN)
                    {
                        string assign = xmlReader.GetAttribute(ApxExporter.TOKEN_ID);
                        skeletonAssigns.Add(int.Parse(assign));
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_BONE)
                    {
                        string name = xmlReader.GetAttribute(ApxExporter.TOKEN_NAME);
                        string parent = xmlReader.GetAttribute(ApxExporter.TOKEN_PARENT);
                        Bone bone = new Bone(name);
                        List<Bone> lastBL = bones[bones.Count - 1];
                        if (!string.IsNullOrEmpty(parent))
                        {
                            foreach (Bone b in lastBL)
                            {
                                if (b.Name == parent)
                                {
                                    b.AddChild(bone);
                                }
                            }
                        }
                        List<Bone> skel = bones[bones.Count - 1];
                        skel.Add(bone);
                        Skeleton lastSkeleton = skeletons[skeletons.Count - 1];
                        lastSkeleton.AddBone(bone);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_BONE_BINDPOSITION)
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));

                        Vector3f vec = new Vector3f(x, y, z);
                        List<Bone> skel = bones[bones.Count - 1];
                        if (skel.Count > 0)
                        {
                            Bone lastBone = skel[skel.Count - 1];
                            lastBone.SetBindTranslation(vec);
                        }
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_BONE_BINDROTATION)
                    {
                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));
                        float w = float.Parse(xmlReader.GetAttribute("w"));

                        List<Bone> skel = bones[bones.Count - 1];
                        if (skel.Count > 0)
                        {
                            Bone lastBone = skel[skel.Count - 1];
                            //   lastBone.SetBindAxisAngle(new Vector3f(x, y, z), w);
                            lastBone.SetBindRotation(new Quaternion(x, y, z, w));
                        }
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_BONE_ASSIGNS)
                    {
                        boneAssigns.Add(new List<BoneAssign>());
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_BONE_ASSIGN)
                    {
                        int vertIdx = int.Parse(xmlReader.GetAttribute(ApxExporter.TOKEN_VERTEXINDEX));
                        int boneIdx = int.Parse(xmlReader.GetAttribute(ApxExporter.TOKEN_BONEINDEX));
                        float boneWeight = float.Parse(xmlReader.GetAttribute(ApxExporter.TOKEN_BONEWEIGHT));
                        List<BoneAssign> ba = boneAssigns[boneAssigns.Count - 1];
                        ba.Add(new BoneAssign(vertIdx, boneWeight, boneIdx));
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_ANIMATIONS)
                    {
                        hasAnimations = true;
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_ANIMATION)
                    {
                        string name = xmlReader.GetAttribute(ApxExporter.TOKEN_NAME);
                        Animation anim = new Animation(name);
                        animations.Add(anim);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_ANIMATION_TRACK)
                    {
                        string bone = xmlReader.GetAttribute(ApxExporter.TOKEN_BONE);
                        Bone b = skeletons[skeletons.Count - 1].GetBone(bone);
                        if (b != null)
                        {
                            AnimationTrack track = new AnimationTrack(b);
                            animations[animations.Count - 1].AddTrack(track);
                        }
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_KEYFRAME)
                    {
                        float time = float.Parse(xmlReader.GetAttribute(ApxExporter.TOKEN_TIME));

                        Keyframe frame = new Keyframe(time, null, null);
                        Animation canim = animations[animations.Count - 1];
                        AnimationTrack ctrack = canim.GetTrack(canim.GetTracks().Count - 1);
                        ctrack.AddKeyframe(frame);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_KEYFRAME_TRANSLATION)
                    {
                        Animation canim = animations[animations.Count - 1];
                        AnimationTrack ctrack = canim.GetTrack(canim.GetTracks().Count - 1);
                        Keyframe lastFrame = ctrack.frames[ctrack.frames.Count - 1];

                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));

                        Vector3f vec = new Vector3f(x, y, z);
                        lastFrame.SetTranslation(vec);
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_KEYFRAME_ROTATION)
                    {
                        Animation canim = animations[animations.Count - 1];
                        AnimationTrack ctrack = canim.GetTrack(canim.GetTracks().Count - 1);
                        Keyframe lastFrame = ctrack.frames[ctrack.frames.Count - 1];

                        float x = float.Parse(xmlReader.GetAttribute("x"));
                        float y = float.Parse(xmlReader.GetAttribute("y"));
                        float z = float.Parse(xmlReader.GetAttribute("z"));
                        float w = float.Parse(xmlReader.GetAttribute("w"));

                        Quaternion rot = new Quaternion(x, y, z, w);
                        lastFrame.SetRotation(rot);
                    }
                } // start element
                else if (xmlReader.NodeType == XmlNodeType.EndElement)
                {
                    if (xmlReader.Name == ApxExporter.TOKEN_NODE)
                    {
                        if (lastNode != null)
                        {
                            if (lastNode.GetParent() != null)
                            {
                                lastNode = lastNode.GetParent();
                            }
                            else
                            {
                                lastNode = null;
                            }
                        }
                        node = false;
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_GEOMETRY)
                    {
                        geom = false;
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_MATERIAL)
                    {
                        if (geoms.Count > 0)
                        {
                            int lastGeomIndex = geoms.Count - 1;
                            Geometry parent = geoms[lastGeomIndex];
                            int lastMatIndex = mats.Count - 1;
                            Material m = mats[lastMatIndex];
                            geomMats.Add(parent, m);
                        }
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_SKELETON)
                    {
                    }
                    else if (xmlReader.Name == ApxExporter.TOKEN_MODEL)
                    {
                        // end of model, load in meshes
                        EndModel();
                    }
                } // end element
            }
            xmlReader.Close();
            Node finalNode = new Node();
            foreach (Node n in nodes)
                if (n.GetParent() == null)
                    finalNode.AddChild(n);
            this.ResetLoader();
            return finalNode;
        }
示例#20
0
        public override void Init()
        {
            road = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\road.jpg");
            roadCorner = AssetManager.LoadTexture(AssetManager.GetAppPath() + "\\textures\\road_corner.jpg");
            curbModel = AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\curb.obj");
            curbCorner = AssetManager.LoadModel(AssetManager.GetAppPath() + "\\models\\curb_corner.obj");

            Random rand = new Random();

            int xAmt = rand.Next(2, 7), zAmt = rand.Next(2, 7), scaleX = rand.Next(10, 30), scaleZ = rand.Next(10, 30);

            int offset = 3;

            AddRoadCorner(new Line(new Vector3f(0, 0, -2), new Vector3f(0, 0, 0)));

            for (int x = 0; x <= xAmt; x++)
            {
                for (int i = 0; i < zAmt; i++)
                {
                    AddRoad(new Line(new Vector3f(x * scaleX, 0, i * scaleZ), new Vector3f(x * scaleX, 0, (i + 1) * scaleZ - 2)));
                    AddRoadCorner(new Line(new Vector3f(x * scaleX, 0, (i + 1) * scaleZ - 2), new Vector3f(x * scaleX, 0, (i + 1) * scaleZ)));

                    if (x == 0)
                    {
                        // long bar
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set(x * scaleX - 1, 0, (i + 1) * scaleZ - 1);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180));
                        curb2.SetLocalScale(new Vector3f(1, 1, scaleZ));
                        curbNode.AddChild(curb2);

                    }
                    else if (x == (xAmt - 1))
                    {
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set((x + 1) * scaleX + 1, 0, (i) * scaleZ - 1);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalScale(new Vector3f(1, 1, scaleZ));
                        curbNode.AddChild(curb2);
                    }

                    Node curb0 = (Node)curbModel.Clone();
                    tmpVec.Set(x * scaleX + 1, 0, i * scaleZ + 1);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb0.SetLocalTranslation(tmpVec);
                    curb0.SetLocalScale(new Vector3f(1, 1, scaleZ - offset - 1));
                    curbNode.AddChild(curb0);

                    Node curb1 = (Node)curbModel.Clone();
                    tmpVec.Set(x * scaleX - 1, 0, (i + 1) * scaleZ - offset);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb1.SetLocalTranslation(tmpVec);
                    curb1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180));
                    curb1.SetLocalScale(new Vector3f(1, 1, scaleZ - offset - 1));
                    curbNode.AddChild(curb1);

                    if (x != 0)
                    {
                        Node curbCorner1 = (Node)curbCorner.Clone();
                        tmpVec.Set((x) * scaleX - 2, 0, (i + 1) * scaleZ - offset + 1);
                        tmpVec.y = HeightRoad(tmpVec);
                        curbCorner1.SetLocalTranslation(tmpVec);
                        curbCorner1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90));
                        curbNode.AddChild(curbCorner1);
                    }

                    if (x != xAmt)
                    {
                        Node curbCorner0 = (Node)curbCorner.Clone();
                        tmpVec.Set(x * scaleX + 2, 0, i * scaleZ);
                        tmpVec.y = HeightRoad(tmpVec);
                        curbCorner0.SetLocalTranslation(tmpVec);
                        curbCorner0.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 90));
                        curbNode.AddChild(curbCorner0);
                    }
                }
            }

            for (int z = 0; z <= zAmt; z++)
            {
                for (int i = 0; i < xAmt; i++)
                {
                    AddRoad(new Line(new Vector3f(i * scaleX + 1, 0, z * scaleZ - 1), new Vector3f((i + 1) * scaleX - 1, 0, z * scaleZ - 1)));
                    AddRoadCorner(new Line(new Vector3f((i + 1) * scaleX - 1, 0, z * scaleZ - 1), new Vector3f((i + 1) * scaleX + 1, 0, z * scaleZ - 1)));

                    // long pieces
                    if (z == 0)
                    {
                        // long bar
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set(i * scaleX, 0, z * scaleZ - 2);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90));
                        curb2.SetLocalScale(new Vector3f(scaleX, 1, 1));
                        curbNode.AddChild(curb2);
                    }
                    else if (z == (zAmt - 1))
                    {
                        Node curb2 = (Node)curbModel.Clone();
                        tmpVec.Set((i + 1) * scaleX, 0, (z + 1) * scaleZ);
                        tmpVec.y = HeightRoad(tmpVec);
                        curb2.SetLocalTranslation(tmpVec);
                        curb2.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90 + 180));
                        curb2.SetLocalScale(new Vector3f(scaleX, 1, 1));
                        curbNode.AddChild(curb2);
                    }

                    Node curb0 = (Node)curbModel.Clone();
                    tmpVec.Set((i + 1) * scaleX - 2, 0, z * scaleZ);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb0.SetLocalTranslation(tmpVec);
                    curb0.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 90));
                    curb0.SetLocalScale(new Vector3f(scaleX - offset - 1, 1, 1));
                    curbNode.AddChild(curb0);

                    Node curb1 = (Node)curbModel.Clone();
                    tmpVec.Set((i) * scaleX + offset - 1, 0, z * scaleZ - 2);
                    tmpVec.y = HeightRoad(tmpVec);
                    curb1.SetLocalTranslation(tmpVec);
                    curb1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180 + 90));
                    curb1.SetLocalScale(new Vector3f(scaleX - offset - 1, 1, 1));
                    curbNode.AddChild(curb1);

                    if (z != zAmt)
                    {
                        Node curbCorner0 = (Node)curbCorner.Clone();
                        Vector3f curbVec1 = new Vector3f((i + 1) * scaleX - 1, 0, z * scaleZ + 1);
                        curbVec1.y = HeightRoad(curbVec1);
                        curbCorner0.SetLocalTranslation(curbVec1);
                        curbCorner0.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 180));
                        curbNode.AddChild(curbCorner0);
                    }
                    if (z != 0)
                    {
                        Node curbCorner1 = (Node)curbCorner.Clone();
                        Vector3f curbVec1 = new Vector3f((i) * scaleX + offset - 2, 0, z * scaleZ - 3);
                        curbVec1.y = HeightRoad(curbVec1);
                        curbCorner1.SetLocalTranslation(curbVec1);
                        curbCorner1.SetLocalRotation(new Quaternion().SetFromAxis(Vector3f.UnitY, 360));
                        curbNode.AddChild(curbCorner1);
                    }
                }
            }

            Geometry roadMerged = new Geometry(MeshUtil.MergeMeshes(roadNode));
            Geometry curbMerged = new Geometry(MeshUtil.MergeMeshes(curbNode));

            rootNode.AddChild(curbMerged);

            roadMerged.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
            roadMerged.Material.SetValue(Material.TEXTURE_DIFFUSE, road);
            roadMerged.Material.SetValue(Material.SHININESS, 0.0f);

            Geometry roadCornerMerged = new Geometry(MeshUtil.MergeMeshes(roadCornerNode));

            roadCornerMerged.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
            roadCornerMerged.Material.SetValue(Material.TEXTURE_DIFFUSE, roadCorner);
            roadCornerMerged.Material.SetValue(Material.SHININESS, 0.0f);

            rootNode.AddChild(roadCornerMerged);
            rootNode.AddChild(roadMerged);

            /*

                        addroad(new line(new vector3f(0, 0, 0), new vector3f(0, 0, 10)));
                        AddRoad(new Line(new Vector3f(0, 0, 10), new Vector3f(1, 0, 12)));
                        AddRoad(new Line(new Vector3f(1, 0, 12), new Vector3f(5, 0, 12)));*/

            /*

            for (int x = 0; x < 16; x ++)
            {
                    AddRoad(new Line(new Vector3f((float)System.Math.Sin(x)*13, 0, (float)System.Math.Cos(x) * 13), new Vector3f((float)System.Math.Sin(x+1) * 13, 0, (float)System.Math.Cos(x+1) * 13)));
            }
            */
            /*     AddRoad(new Line(new Vector3f(0, 0, 10), new Vector3f(-3, 0, 11)));
                 AddRoad(new Line(new Vector3f(-3, 0, 11), new Vector3f(-4, 0, 12)));
                 AddRoad(new Line(new Vector3f(-4, 0, 12), new Vector3f(-5, 0, 13)));
                 AddRoad(new Line(new Vector3f(-5, 0, 13), new Vector3f(-6, 0, 14)));*/
            //    AddRoad(new Line(new Vector3f(0, 0, 10), new Vector3f(10, 0, 10)));

            /*  for (int i = 0; i < 4; i++)
              {
                  Line myline = new Line(lastLine.To, lastLine.To.Add(new Vector3f(rand.Next(-15, 15),0, rand.Next(-15, 15))));
                  Geometry quad = new Geometry(QuadFromLine(myline));
                  quad.Material.SetValue(Material.MATERIAL_CULLENABLED, false);
                  quad.Material.SetValue(Material.TEXTURE_DIFFUSE, road);
                  quad.Material.SetValue(Material.SHININESS, 0.0f);
                  lastLine = myline;

                  rootNode.AddChild(quad);
              }
              */
        }