Exemple #1
0
        public LodLevel AddLodLevel(Object3dInfo info, GenericMaterial material, float distStart, float distEnd)
        {
            var i = new LodLevel(info, material, distStart, distEnd);

            LodLevels.Add(i);
            return(i);
        }
Exemple #2
0
 public static Mesh3d Create(Object3dInfo objectInfo, GenericMaterial material)
 {
     var m = new Mesh3d();
     m.AddLodLevel(objectInfo, material, 0, 99999);
     m.AddInstance(new Mesh3dInstance(new TransformationManager(Vector3.Zero), ""));
     return m;
 }
Exemple #3
0
        public RoadScene()
        {
            Object3dInfo waterInfo = Object3dGenerator.CreateGround(new Vector2(-200, -200), new Vector2(200, 200), new Vector2(100, 100), Vector3.UnitY);

            /*
            var color = GenericMaterial.FromMedia("checked.png");
            Mesh3d water = new Mesh3d(waterInfo, color);
            water.SetMass(0);
            water.SetCollisionShape(new BulletSharp.StaticPlaneShape(Vector3.UnitY, 0));
            World.Root.Add(water);
            */
            var lod1 = Object3dInfo.LoadFromObjSingle(Media.Get("train.obj"));
               // lod1.MakeDoubleFaced()
            var mat = new GenericMaterial(new Vector4(1, 1, 1, 1));
            var dragon = new Mesh3d(lod1, mat);
            dragon.Transformation.Scale(19);
               // dragon.DisableDepthWrite = true;
            Add(dragon);
            /*
            var roadtile = Object3dGenerator.CreateGround(new Vector2(-0.47f, -0.5f) * 20, new Vector2(0.47f, 0.5f) * 20, new Vector2(1, 1), Vector3.UnitY);
            var roadsInstances = new InstancedMesh3d(roadtile, GenericMaterial.FromMedia("roadtex.png", "roadnormaltex.png"));
            var advancer = new Vector3(0, 0, (roadtile.GetAxisAlignedBox() * 2).Z);
            roadsInstances.Transformations.Add(new TransformationManager(new Vector3(0, 0.1f, 0)));
            for(int i = 0; i < 1600; i++)
            {
                roadsInstances.Transformations.Add(new TransformationManager(new Vector3(0, 0.1f, 0) + advancer * i));
            }
            roadsInstances.UpdateMatrix();
            Add(roadsInstances);*/
        }
Exemple #4
0
        public static Mesh3d Create(Object3dInfo objectInfo, GenericMaterial material)
        {
            var m = new Mesh3d();

            m.AddLodLevel(objectInfo, material, 0, 99999);
            m.AddInstance(new Mesh3dInstance(new TransformationManager(Vector3.Zero), ""));
            return(m);
        }
Exemple #5
0
 public KeyframeAnimatedMesh3d(List<Object3dInfo> infos, GenericMaterial material)
 {
     Transformation = new TransformationManager(Vector3.Zero);
     Frames = new List<Mesh3d>();
     CurrentFrame = 0;
     foreach(var i in infos)
     {
         Frames.Add(new Mesh3d(i, material));
     }
 }
Exemple #6
0
 public LodLevel(Object3dInfo o3i, GenericMaterial gm, float distStart, float distEnd)
 {
     DistanceStart = distStart;
     DistanceEnd = distEnd;
     Info3d = o3i;
     Material = gm;
     ModelMatricesBuffer = new ShaderStorageBuffer();
     RotationMatricesBuffer = new ShaderStorageBuffer();
     Ids = new ShaderStorageBuffer();
 }
Exemple #7
0
 public InstancedMesh3d(Object3dInfo objectInfo, GenericMaterial material)
 {
     ModelMatricesBuffer = new ShaderStorageBuffer();
     RotationMatricesBuffer = new ShaderStorageBuffer();
     Randomizer = new Random();
     Transformations = new List<TransformationManager>();
     Instances = 0;
     ObjectInfo = objectInfo;
     Material = material;
     UpdateMatrix();
 }
Exemple #8
0
 public void AddLodLevel(float distance, Object3dInfo info, GenericMaterial material)
 {
     if(LodLevels == null)
         LodLevels = new List<LodLevelData>();
     LodLevels.Add(new LodLevelData()
     {
         Info3d = info,
         Material = material,
         Distance = distance
     });
     LodLevels.Sort((a, b) => (int)((a.Distance - b.Distance) * 100.0)); // *100 to preserve precision
 }
Exemple #9
0
 public Mesh3d(Object3dInfo objectInfo, GenericMaterial material)
 {
     // ModelMatricesBuffer = new ShaderStorageBuffer();
        // RotationMatricesBuffer = new ShaderStorageBuffer();
     DisableDepthWrite = false;
     Instances = 1;
     MainObjectInfo = objectInfo;
     MainMaterial = material;
     Transformation = new TransformationManager(Vector3.Zero, Quaternion.Identity, 1.0f);
     UpdateMatrix();
     MeshColoredID = new Vector3((float)Randomizer.NextDouble(), (float)Randomizer.NextDouble(), (float)Randomizer.NextDouble());
 }
Exemple #10
0
 public ProjectionLight(Vector3 position, Quaternion rotation, int mapwidth, int mapheight, float fov, float near, float far)
 {
     FarPlane = far;
     camera = new Camera(position, Vector3.Zero, mapwidth / mapheight, fov, near, far);
     camera.LookAt(Vector3.Zero);
     FBO = new Framebuffer(mapwidth, mapheight, true);
     //FBO.ColorInternalFormat = PixelInternalFormat.Rgba8;
     //FBO.ColorPixelFormat = PixelFormat.Rgba;
     //FBO.ColorPixelType = PixelType.UnsignedByte;
     Shader = GenericMaterial.FromName("ConeLight");
     ViewPort = new Size(mapwidth, mapheight);
 }
Exemple #11
0
 public LodLevel(Object3dInfo o3i, GenericMaterial gm, float distStart, float distEnd)
 {
     DistanceStart = distStart;
     DistanceEnd   = distEnd;
     Info3d        = o3i;
     if (Info3d != null)
     {
         BoundingBoxInfo3d = Generators.Object3dGenerator.CreateCube(o3i.BoundingBoxMin, o3i.BoundingBoxMax, Vector2.One).AsObject3dInfo();
     }
     else
     {
         BoundingBoxInfo3d = Object3dInfo.Empty;
     }
     Material         = gm;
     ModelInfosBuffer = new ShaderStorageBuffer();
 }
Exemple #12
0
        public void SetUniforms(GenericMaterial material)
        {
            ShaderProgram shader = material.GetShaderProgram();
            bool shaderSwitchResult = Material.Use();

            // if(Sun.Current != null) Sun.Current.BindToShader(shader); per mesh

            shader.SetUniform("SpecularComponent", SpecularComponent);
            shader.SetUniform("DiffuseComponent", DiffuseComponent);
            shader.SetUniform("SpecularSize", SpecularSize);
            shader.SetUniform("IgnoreLighting", false);

            shader.SetUniform("RandomSeed1", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed2", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed3", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed4", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed5", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed6", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed7", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed8", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed9", (float)Randomizer.NextDouble());
            shader.SetUniform("RandomSeed10", (float)Randomizer.NextDouble());

            shader.SetUniform("Time", (float)(DateTime.Now - GLThread.StartTime).TotalMilliseconds / 1000);

            shader.SetUniform("UseAlphaMask", 0);

            //LastMaterialHash = Material.GetShaderProgram().GetHashCode();
            // per world
            shader.SetUniform("Instances", Instances);
            shader.SetUniform("ViewMatrix", Camera.Current.ViewMatrix);
            shader.SetUniform("ProjectionMatrix", Camera.Current.ProjectionMatrix);
            shader.SetUniform("LogEnchacer", 0.01f);

            shader.SetUniform("CameraPosition", Camera.Current.Transformation.GetPosition());
            shader.SetUniform("FarPlane", Camera.Current.Far);
            shader.SetUniform("resolution", new Vector2(GLThread.Resolution.Width, GLThread.Resolution.Height));
        }
Exemple #13
0
        public static List<Mesh3d> LoadSceneFromObj(string objfile, string mtlfile, float scale = 1.0f)
        {
            string[] lines = File.ReadAllLines(objfile);
            var objs = ParseOBJString(lines);
            var mtllib = LoadMaterialsFromMtl(mtlfile);
            List<Mesh3d> meshes = new List<Mesh3d>();
            Dictionary<string, GenericMaterial> texCache = new Dictionary<string, GenericMaterial>();
            Dictionary<Color, GenericMaterial> colorCache = new Dictionary<Color, GenericMaterial>();
            Dictionary<GenericMaterial, MaterialInfo> mInfos = new Dictionary<GenericMaterial, MaterialInfo>();
            Dictionary<GenericMaterial, List<Object3dInfo>> linkCache = new Dictionary<GenericMaterial, List<Object3dInfo>>();
            var colorPink = new GenericMaterial(Color.Pink);
            mInfos = new Dictionary<GenericMaterial, MaterialInfo>();
            foreach(var obj in objs)
            {
                var mat = mtllib.ContainsKey(obj.MaterialName) ? mtllib[obj.MaterialName] : null;
                GenericMaterial material = null;
                if(mat != null && mat.TextureName.Length > 0)
                {
                    if(texCache.ContainsKey(mat.TextureName + mat.AlphaMask))
                    {
                        material = texCache[mat.TextureName + mat.AlphaMask];
                        mInfos[material] = mat;
                    }
                    else
                    {
                        var m = GenericMaterial.FromMedia(Path.GetFileName(mat.TextureName));
                        m.NormalMapScale = 10;
                        material = m;
                        mInfos[material] = mat;
                        texCache.Add(mat.TextureName + mat.AlphaMask, material);
                        // material = colorPink;
                    }
                    //material = new GenericMaterial(Color.Pink);
                }
                else if(mat != null)
                {
                    if(colorCache.ContainsKey(mat.DiffuseColor))
                    {
                        material = colorCache[mat.DiffuseColor];
                        mInfos[material] = mat;
                    }
                    else
                    {
                        material = new GenericMaterial(Color.White);
                        mInfos[material] = mat;
                        colorCache.Add(mat.DiffuseColor, material);
                    }
                }
                else
                {
                    material = colorPink;
                    mInfos[material] = mat;
                }

                for(int i = 0; i < obj.VBO.Count; i += 8)
                {
                    obj.VBO[i] *= scale;
                    obj.VBO[i + 1] *= scale;
                    obj.VBO[i + 2] *= scale;
                }
                var o3di = new Object3dInfo(obj.VBO, obj.Indices);
                if(!linkCache.ContainsKey(material))
                    linkCache.Add(material, new List<Object3dInfo> { o3di });
                else
                    linkCache[material].Add(o3di);
            }
            foreach(var kv in linkCache)
            {
                Object3dInfo o3di = kv.Value[0];
                if(kv.Value.Count > 1)
                {
                    foreach(var e in kv.Value.Skip(1))
                        o3di.Append(e);
                }
                var trans = o3di.GetAverageTranslationFromZero();
                o3di.OriginToCenter();
                //o3di.CorrectFacesByNormals();
                // o3di.CorrectFacesByNormals();
                Mesh3d mesh = new Mesh3d(o3di, kv.Key);
                kv.Key.SpecularComponent = 1.0f - mInfos[kv.Key].SpecularStrength + 0.01f;
                kv.Key.Roughness = (mInfos[kv.Key].SpecularStrength);
                kv.Key.ReflectionStrength = 1.0f - (mInfos[kv.Key].SpecularStrength);
                kv.Key.DiffuseComponent = mInfos[kv.Key].DiffuseColor.GetBrightness() + 0.01f;
                if(mInfos[kv.Key].AlphaMask.Length > 1)
                    (kv.Key as GenericMaterial).SetAlphaMaskFromMedia(mInfos[kv.Key].AlphaMask);
                if(mInfos[kv.Key].BumpMapName.Length > 1)
                    ((GenericMaterial)kv.Key).SetBumpMapFromMedia(mInfos[kv.Key].BumpMapName);
                // mesh.SpecularComponent = kv.Key.SpecularStrength;
                mesh.Transformation.Translate(trans);
                // mesh.SetCollisionShape(o3di.GetConvexHull(mesh.Transformation.GetPosition(), 1.0f, 1.0f));
                meshes.Add(mesh);
            }
            return meshes;
        }
Exemple #14
0
        public ManyCubesScene()
        {
            Object3dInfo wall = Object3dGenerator.CreateCube(new Vector3(10.0f, 10.0f, 1.0f), new Vector2(1, 1));
            InstancedMesh3d wallsInst = new InstancedMesh3d(wall, new GenericMaterial(Color.Red));
            wallsInst.Transformations.Add(new TransformationManager(new Vector3(0, 5, 10), Quaternion.Identity, 1));
            wallsInst.Transformations.Add(new TransformationManager(new Vector3(0, 5, -10), Quaternion.Identity, 1));
            wallsInst.Transformations.Add(new TransformationManager(new Vector3(10, 5, 0), Quaternion.FromAxisAngle(Vector3.UnitY, MathHelper.PiOver2), 1));
            wallsInst.Transformations.Add(new TransformationManager(new Vector3(-10, 5, 0), Quaternion.FromAxisAngle(Vector3.UnitY, MathHelper.PiOver2), 1));
            wallsInst.Instances = 4;
            wallsInst.UpdateMatrix();
            World.Root.CreateRigidBody(0, wallsInst.Transformations[0].GetWorldTransform(), new BulletSharp.BoxShape(wall.GetAxisAlignedBox()/2), null);
            World.Root.CreateRigidBody(0, wallsInst.Transformations[1].GetWorldTransform(), new BulletSharp.BoxShape(wall.GetAxisAlignedBox() / 2), null);
            World.Root.CreateRigidBody(0, wallsInst.Transformations[2].GetWorldTransform(), new BulletSharp.BoxShape(wall.GetAxisAlignedBox() / 2), null);
            World.Root.CreateRigidBody(0, wallsInst.Transformations[3].GetWorldTransform(), new BulletSharp.BoxShape(wall.GetAxisAlignedBox() / 2), null);
            Add(wallsInst);

            World.Root.CreateRigidBody(0, Matrix4.Identity, new BulletSharp.StaticPlaneShape(Vector3.UnitY, 0), null);
            /*
            var roadtile = Object3dGenerator.CreateGround(new Vector2(-0.47f, -0.5f) * 20, new Vector2(0.47f, 0.5f) * 20, new Vector2(1, 1), Vector3.UnitY);
            var roadsInstances = new InstancedMesh3d(roadtile, GenericMaterial.FromMedia("roadtex.png", "roadnormaltex.png"));
            var advancer = new Vector3(0, 0, (roadtile.GetAxisAlignedBox() * 2).Z);
            roadsInstances.Transformations.Add(new TransformationManager(new Vector3(0, 0.1f, 0)));
            for(int i = 0; i < 120; i++)
            {
                roadsInstances.Transformations.Add(new TransformationManager(new Vector3(0, 0.1f, 0) + advancer * i));
            }
            roadsInstances.UpdateMatrix();
            Add(roadsInstances);*/

            //Object3dInfo cube = Object3dGenerator.CreateCube(new Vector3(1, 1, 1), new Vector2(1, 1));
            var cube = Object3dInfo.LoadFromObjSingle(Media.Get("portalcube.obj"));
            cube.MakeDoubleFaced();
            GenericMaterial material = new GenericMaterial(new Vector4(1, 1, 1, 1.000f));
            int allCount = 0;
            var meshes = new List<Mesh3d>();
            Mesh3d lastmesh = null;
            var rand = new Random();
            var sphere3dInfo = Object3dInfo.LoadFromObjSingle(Media.Get("lightsphere.obj"));
            sphere3dInfo.Normalize();
            for(int y = 0; y < 100; y++)
            {
                Mesh3d mesh = new Mesh3d(cube, material);
                mesh.DisableDepthWrite = true;
                mesh.Transformation.SetPosition(new Vector3(0, (y + 10.0f) * 12.0f, 0));
                Vector3 scaleRand = new Vector3((float)rand.NextDouble() * 6.0f + 5.0f, (float)rand.NextDouble() * 6.0f + 5.0f, (float)rand.NextDouble() * 6.0f + 5.0f);
                mesh.SetMass(11.0f);
                mesh.Transformation.Scale(1);
                mesh.SetCollisionShape(new BulletSharp.BoxShape(cube.GetAxisAlignedBox() / 2));
                meshes.Add(mesh);
                World.Root.PhysicalWorld.AddCollisionObject(mesh.CreateRigidBody());
                if(lastmesh != null)
                {
                    //var offset = (mesh.Transformation.GetPosition() - lastmesh.Transformation.GetPosition()) / 2;
                    //var cst = new BulletSharp.FixedConstraint(mesh.PhysicalBody, lastmesh.PhysicalBody, (-offset).ToMatrix(), offset.ToMatrix());
                    //World.Root.PhysicalWorld.AddConstraint(cst, true);
                }

                lastmesh = mesh;
            }

            var inst = InstancedMesh3d.FromSimilarMesh3dList(meshes);
            GLThread.OnUpdate += (o, e) =>
            {
                inst.UpdateMatrix();
                //wallsInst.UpdateMatrix();
            };
            Add(inst);
            Console.WriteLine("allCount " + allCount);
        }
Exemple #15
0
        public static List <Mesh3d> LoadSceneFromObj(string objfile, string mtlfile, float scale = 1.0f)
        {
            string[]      lines  = File.ReadAllLines(Media.Get(objfile));
            var           objs   = ParseOBJString(lines);
            var           mtllib = LoadMaterialsFromMtl(Media.Get(mtlfile));
            List <Mesh3d> meshes = new List <Mesh3d>();
            Dictionary <string, GenericMaterial>          texCache   = new Dictionary <string, GenericMaterial>();
            Dictionary <Vector3, GenericMaterial>         colorCache = new Dictionary <Vector3, GenericMaterial>();
            Dictionary <GenericMaterial, MaterialInfo>    mInfos     = new Dictionary <GenericMaterial, MaterialInfo>();
            Dictionary <GenericMaterial, Object3dManager> linkCache  = new Dictionary <GenericMaterial, Object3dManager>();
            var colorPink = new GenericMaterial(Color.White);

            foreach (var obj in objs)
            {
                Console.WriteLine("Loading " + obj.Name);
                Console.WriteLine("GC MEMORY USED {0} MB", GC.GetTotalMemory(true) / 1024.0 / 1024);
                var             mat      = mtllib.ContainsKey(obj.MaterialName) ? mtllib[obj.MaterialName] : new MaterialInfo();
                GenericMaterial material = colorPink;
                if (mat != null && mat.TextureName.Length > 0)
                {
                    var m = new GenericMaterial();
                    m.SetDiffuseTexture(Path.GetFileName(mat.TextureName));
                    m.SpecularTexture = m.DiffuseTexture;
                    // m.NormalMapScale = 10;
                    material = m;

                    material.Name    = obj.MaterialName;
                    mInfos[material] = mat;
                    // texCache.Add(mat.TextureName + mat.AlphaMask, material);
                    // material = colorPink;
                }
                else if (mat != null)
                {
                    material         = new GenericMaterial(mat.DiffuseColor);
                    mInfos[material] = mat;
                }
                else
                {
                    material         = colorPink;
                    mInfos[material] = mat;
                }

                for (int i = 0; i < obj.VBO.Count; i++)
                {
                    obj.VBO[i].Position *= scale;
                }
                var o3di = new Object3dManager(obj.VBO);
                o3di.Name = obj.Name;
                // if(!linkCache.ContainsKey(material))
                linkCache.Add(material, o3di);
                // else
                //     linkCache[material].Add(o3di);
            }
            foreach (var kv in linkCache)
            {
                Object3dManager o3di = kv.Value;

                // var trans = o3di.GetAverageTranslationFromZero();
                // o3di.OriginToCenter();
                //o3di.CorrectFacesByNormals();
                // o3di.CorrectFacesByNormals();
                var oi = new Object3dInfo(o3di.Vertices);
                oi.Manager = o3di;
                // GC.Collect();
                Mesh3d mesh = Mesh3d.Create(oi, kv.Key);
                //kv.Key.SpecularComponent = 1.0f - mInfos[kv.Key].SpecularStrength + 0.01f;
                kv.Key.Roughness     = mInfos[kv.Key].SpecularStrength;
                kv.Key.DiffuseColor  = mInfos[kv.Key].DiffuseColor;
                kv.Key.SpecularColor = mInfos[kv.Key].SpecularColor;
                // kv.Key.ReflectionStrength = 1.0f - (mInfos[kv.Key].SpecularStrength);
                //kv.Key.DiffuseComponent = mInfos[kv.Key].DiffuseColor.GetBrightness() + 0.01f;
                var kva = kv.Key;
                if (!mInfos.ContainsKey(kva))
                {
                    kva = mInfos.Keys.First();
                }
                if (mInfos[kva].BumpMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetBumpTexture(mInfos[kv.Key].BumpMapName);
                }
                if (mInfos[kva].RoughnessMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetRoughnessTexture(mInfos[kv.Key].RoughnessMapName);
                }
                if (mInfos[kva].NormapMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetNormalsTexture(mInfos[kv.Key].NormapMapName);
                }
                if (mInfos[kva].TextureName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetDiffuseTexture(mInfos[kv.Key].TextureName);
                }
                // mesh.SpecularComponent = kv.Key.SpecularStrength;
                //  mesh.GetInstance(0).Translate(trans);
                // mesh.SetCollisionShape(o3di.GetConvexHull(mesh.Transformation.GetPosition(),
                // 1.0f, 1.0f));
                meshes.Add(mesh);
            }
            return(meshes);
        }
Exemple #16
0
        public CarScene()
        {
            Object3dInfo waterInfo = Object3dGenerator.CreateGround(new Vector2(-200, -200), new Vector2(200, 200), new Vector2(100, 100), Vector3.UnitY);

            var color = GenericMaterial.FromMedia("checked.png", "183_norm.JPG");
            Mesh3d water = new Mesh3d(waterInfo, color);
            water.SetMass(0);
            water.Translate(0, -10, 0);
            water.SetCollisionShape(new BulletSharp.StaticPlaneShape(Vector3.UnitY, 0));
            World.Root.Add(water);

            var lod1 = Object3dInfo.LoadFromRaw(Media.Get("lucy_lod1.vbo.raw"), Media.Get("lucy_lod1.indices.raw"));
            var mat = new GenericMaterial(new Vector4(0, 0, 1, 1f));
            var dragon = new Mesh3d(lod1, mat);
            dragon.Transformation.Scale(2);
            Add(dragon);

            var rand = new Random();
            var grasslod0 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod1.obj"));
            var grasslod1 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod1.obj"));
            var grasslod2 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod2.obj"));
            var grasslod3 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod3.obj"));
            var grasscolor = new GenericMaterial(Color.DarkGreen);
            var grassInstanced = new InstancedMesh3d(grasslod2, grasscolor);
            for(int x = -15; x < 15; x++)
            {
                for(int y = -15; y < 15; y++)
                {
                    grassInstanced.Transformations.Add(new TransformationManager(new Vector3(x, 0, y), Quaternion.FromAxisAngle(Vector3.UnitY, (float)rand.NextDouble() * MathHelper.Pi), new Vector3(1, 1, 1)));
                    grassInstanced.Instances++;
                }
            }
            grassInstanced.UpdateMatrix();
            Add(grassInstanced);

            ProjectionLight redConeLight = new ProjectionLight(new Vector3(1, 25, 1), Quaternion.Identity, 5000, 5000, MathHelper.PiOver2, 1.0f, 10000.0f);
            redConeLight.LightColor = new Vector4(1, 1, 1, 1);
            redConeLight.BuildOrthographicProjection(500, 500, -100, 100);
            redConeLight.camera.LookAt(Vector3.Zero);

            LightPool.Add(redConeLight);

            var wheel3dInfo = Object3dInfo.LoadFromObjSingle(Media.Get("fcarwheel.obj"));
            var car3dInfo = Object3dInfo.LoadFromObjSingle(Media.Get("fcarbody.obj"));

            var car = new Mesh3d(car3dInfo, new GenericMaterial(Color.LightGreen));
            car.SetMass(200);
            //car.Translate(0, 3.76f, 0);
            car.SetCollisionShape(new BoxShape(0.5f, 0.5f, 0.5f));
            car.CreateRigidBody(new Vector3(0, -25, 0), true);
            car.PhysicalBody.SetSleepingThresholds(0, 0);
            car.PhysicalBody.ContactProcessingThreshold = 0;
            car.PhysicalBody.CcdMotionThreshold = 0;
            CurrentCar = car;
            World.Root.Add(car);

            GLThread.OnUpdate += (o, e) =>
            {
                redConeLight.SetPosition(car.GetPosition() + new Vector3(0, 15, 0));
            };

            var shape = new SphereShape(0.5f);
            //var shape = wheel3dInfo.GetAccurateCollisionShape();
            var wheelLF = new Mesh3d(wheel3dInfo, new GenericMaterial(Color.White));
            wheelLF.SetMass(100);
            wheelLF.Translate(1.640539f, 0.48866f, -1.94906f);
            wheelLF.SetCollisionShape(shape);
            wheelLF.CreateRigidBody();
            World.Root.Add(wheelLF);
            var wheelRF = new Mesh3d(wheel3dInfo, new GenericMaterial(Color.White));
            wheelRF.SetMass(100);
            wheelRF.Translate(1.640539f, 0.48866f, 1.94906f);
            wheelRF.SetCollisionShape(shape);
            wheelRF.CreateRigidBody();
            World.Root.Add(wheelRF);
            var wheelLR = new Mesh3d(wheel3dInfo, new GenericMaterial(Color.White));
            wheelLR.SetMass(100);
            wheelLR.Translate(-1.640539f, 0.48866f, -1.94906f);
            wheelLR.SetCollisionShape(shape);
            wheelLR.CreateRigidBody();
            World.Root.Add(wheelLR);
            var wheelRR = new Mesh3d(wheel3dInfo, new GenericMaterial(Color.White));
            wheelRR.SetMass(100);
            wheelRR.Translate(-1.640539f, 0.48866f, 1.94906f);
            wheelRR.SetCollisionShape(shape);
            wheelRR.CreateRigidBody();
            World.Root.Add(wheelRR);

            wheelLF.PhysicalBody.SetSleepingThresholds(0, 0);
            wheelLF.PhysicalBody.ContactProcessingThreshold = 0;
            wheelLF.PhysicalBody.CcdMotionThreshold = 0;

            wheelRF.PhysicalBody.SetSleepingThresholds(0, 0);
            wheelRF.PhysicalBody.ContactProcessingThreshold = 0;
            wheelRF.PhysicalBody.CcdMotionThreshold = 0;

            wheelLR.PhysicalBody.SetSleepingThresholds(0, 0);
            wheelLR.PhysicalBody.ContactProcessingThreshold = 0;
            wheelLR.PhysicalBody.CcdMotionThreshold = 0;

            wheelLR.PhysicalBody.SetSleepingThresholds(0, 0);
            wheelLR.PhysicalBody.ContactProcessingThreshold = 0;
            wheelLR.PhysicalBody.CcdMotionThreshold = 0;

            wheelRR.PhysicalBody.SetSleepingThresholds(0, 0);
            wheelRR.PhysicalBody.ContactProcessingThreshold = 0;
            wheelRR.PhysicalBody.CcdMotionThreshold = 0;

            wheelLF.PhysicalBody.Friction = 1200;
            wheelRF.PhysicalBody.Friction = 1200;
            wheelLR.PhysicalBody.Friction = 1200;
            wheelRR.PhysicalBody.Friction = 1200;

            car.PhysicalBody.SetIgnoreCollisionCheck(wheelLF.PhysicalBody, true);
            car.PhysicalBody.SetIgnoreCollisionCheck(wheelRF.PhysicalBody, true);
            car.PhysicalBody.SetIgnoreCollisionCheck(wheelLR.PhysicalBody, true);
            car.PhysicalBody.SetIgnoreCollisionCheck(wheelRR.PhysicalBody, true);

            // location left rear x -5.27709 y 1.56474 z -3.13906
            // location right rear x -5.27709 y 1.56474 z 3.13906
            // location left front x 5.500539 y 1.56474 z -3.13906
            // location right front x 5.500539 y 1.56474 z 3.13906
            //public HingeConstraint(RigidBody rigidBodyA, RigidBody rigidBodyB, Vector3 pivotInA, Vector3 pivotInB, Vector3 axisInA, Vector3 axisInB);

            var frontAxis = new HingeConstraint(wheelLF.PhysicalBody, wheelRF.PhysicalBody,
                new Vector3(1.640539f, 0.48866f, 1.94906f), new Vector3(1.640539f, 0.48866f, -1.94906f), Vector3.UnitZ, Vector3.UnitZ);

            var rearAxis = new HingeConstraint(wheelLR.PhysicalBody, wheelRR.PhysicalBody,
                new Vector3(-1.640539f, 0.48866f, 1.94906f), new Vector3(-1.640539f, 0.48866f, -1.94906f), Vector3.UnitZ, Vector3.UnitZ);

            var centerAxis1 = new HingeConstraint(car.PhysicalBody, wheelLF.PhysicalBody,
                new Vector3(1.640539f, -0.48866f, -1.94906f), new Vector3(0), Vector3.UnitZ, Vector3.UnitZ);
            var centerAxis2 = new HingeConstraint(car.PhysicalBody, wheelRF.PhysicalBody,
                new Vector3(1.640539f, -0.48866f, 1.94906f), new Vector3(0), Vector3.UnitZ, Vector3.UnitZ);

            var centerAxis3 = new HingeConstraint(car.PhysicalBody, wheelLR.PhysicalBody,
                new Vector3(-1.640539f, -0.48866f, 1.94906f), new Vector3(0), Vector3.UnitZ, Vector3.UnitZ);
            var centerAxis4 = new HingeConstraint(car.PhysicalBody, wheelRR.PhysicalBody,
                new Vector3(-1.640539f, -0.48866f, -1.94906f), new Vector3(0), Vector3.UnitZ, Vector3.UnitZ);

            centerAxis1.SetLimit(0, 10, 0, 0, 0);
            centerAxis2.SetLimit(0, 10, 0, 0, 0);
            centerAxis3.SetLimit(0, 10, 0, 0, 0);
            centerAxis4.SetLimit(0, 10, 0, 0, 0);

            //World.Root.PhysicalWorld.AddConstraint(frontAxis);
            // World.Root.PhysicalWorld.AddConstraint(rearAxis);
              //  centerAxis1.SetLimit(0, 0.01f);
               // centerAxis2.SetLimit(0, 0.01f);
            //centerAxis3.SetLimit(0, 0.01f);
            //centerAxis4.SetLimit(0, 0.01f);
            World.Root.PhysicalWorld.AddConstraint(centerAxis1);
            World.Root.PhysicalWorld.AddConstraint(centerAxis2);
            World.Root.PhysicalWorld.AddConstraint(centerAxis3);
            World.Root.PhysicalWorld.AddConstraint(centerAxis4);

            GLThread.OnKeyDown += (object sender, OpenTK.Input.KeyboardKeyEventArgs e) =>
            {
                if(e.Key == OpenTK.Input.Key.R)
                {
                    car.SetOrientation(Quaternion.Identity);
                }
                if(e.Key == OpenTK.Input.Key.Space)
                {
                    centerAxis1.EnableMotor = false;
                    centerAxis2.EnableMotor = false;
                }
                if(e.Key == OpenTK.Input.Key.Up)
                {
                    centerAxis1.EnableMotor = true;
                    centerAxis1.EnableAngularMotor(true, -100.0f, 10.0f);
                    centerAxis2.EnableMotor = true;
                    centerAxis2.EnableAngularMotor(true, -100.0f, 10.0f);
                }
                if(e.Key == OpenTK.Input.Key.Down)
                {

                    centerAxis1.EnableMotor = true;
                    centerAxis1.EnableAngularMotor(true, 100.0f, 6.0f);
                    centerAxis2.EnableMotor = true;
                    centerAxis2.EnableAngularMotor(true, 100.0f, 6.0f);
                }
                if(e.Key == OpenTK.Input.Key.Left)
                {
                    float angle = 0.6f / (car.PhysicalBody.LinearVelocity.Length + 1.0f);
                    centerAxis3.SetFrames(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, 1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    centerAxis4.SetFrames(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, -1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    //centerAxis3.SetAxis(new Vector3(1, 0, 1));
                    //centerAxis4.SetAxis(new Vector3(1, 0, 1));
                }
                if(e.Key == OpenTK.Input.Key.Right)
                {
                    float angle = 0.6f / (car.PhysicalBody.LinearVelocity.Length + 1.0f);
                    centerAxis3.SetFrames(Matrix4.CreateRotationY(-angle) * Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, 1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    centerAxis4.SetFrames(Matrix4.CreateRotationY(-angle) * Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, -1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    //centerAxis3.SetAxis(new Vector3(-1, 0, 1));
                    //centerAxis4.SetAxis(new Vector3(-1, 0, 1));
                }
            };
            GLThread.OnKeyUp += (object sender, OpenTK.Input.KeyboardKeyEventArgs e) =>
            {
                if(e.Key == OpenTK.Input.Key.Up)
                {

                    centerAxis1.EnableMotor = false;
                    centerAxis2.EnableMotor = false;
                }
                if(e.Key == OpenTK.Input.Key.Down)
                {

                    centerAxis1.EnableMotor = false;
                    centerAxis2.EnableMotor = false;
                }
                if(e.Key == OpenTK.Input.Key.Left)
                {
                    centerAxis3.SetFrames(Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, 1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    centerAxis4.SetFrames(Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, -1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    //centerAxis3.SetAxis(new Vector3(0, 0, 1));
                    //centerAxis4.SetAxis(new Vector3(0, 0, 1));
                }
                if(e.Key == OpenTK.Input.Key.Right)
                {
                    centerAxis3.SetFrames( Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, 1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    centerAxis4.SetFrames(Matrix4.CreateTranslation(new Vector3(-1.640539f, -0.48866f, -1.94906f)), Matrix4.CreateTranslation(new Vector3(0)));
                    //centerAxis3.SetAxis(new Vector3(0, 0, 1));
                    //centerAxis4.SetAxis(new Vector3(0, 0, 1));
                }
            };
        }
Exemple #17
0
 public LodLevel AddLodLevel(Object3dInfo info, GenericMaterial material, float distStart, float distEnd)
 {
     var i = new LodLevel(info, material, distStart, distEnd);
     LodLevels.Add(i);
     return i;
 }
Exemple #18
0
        public OldCityScene()
        {
            /*  var scene = Object3dInfo.LoadSceneFromObj(Media.Get("desertcity.obj"), Media.Get("desertcity.mtl"), 1.0f);
              foreach(var ob in scene)
              {
                  ob.SetMass(0);
                  this.Add(ob);
              }*/
            // var scene = Object3dInfo.LoadSceneFromObj(Media.Get("cryteksponza.obj"),
            // Media.Get("cryteksponza.mtl"), 0.03f);
            var scene = new GameScene("Scene.scene");
            scene.Load();
            var cnt = scene.Meshes.Count;
            var sc = new Vector3(2.0f, 2.0f, 2.0f);
            for(var i = 0; i < cnt; i++)
            {
                var o = scene.Meshes[i];
                var b = o.GetInstance(0).GetPosition();
                b = new Vector3(b.X * 0.25f, b.Y * 0.25f, b.Z * 0.25f);
                o.GetInstance(0).SetPosition(b);
                o.GetInstance(0).Scale(0.25f);
               // var bdshape = o.GetLodLevel(0).Info3d.GetConvexHull();
               // var bd = Game.World.Physics.CreateBody(0, o.GetInstance(0), bdshape);
               // bd.Enable();
                Game.World.Scene.Add(o);
                o.GetLodLevel(0).Info3d.Manager = null;
                //   bd.Enable();

            }
            var hbal = Object3dManager.LoadFromRaw(Media.Get("statue2.raw"));
            /*int hbalc = hbal.Vertices.Count;
            for(int i = 0; i < hbalc; i += 3)
            {
                var v1 = hbal.Vertices[i].Position; 
                var v2 = hbal.Vertices[i + 1].Position;
                var v3 = hbal.Vertices[i + 2].Position;
                var n = Vector3.Cross(v2 - v1, v3 - v1).Normalized();
                hbal.Vertices[i].Normal = n;
                hbal.Vertices[i + 1].Normal = n;
                hbal.Vertices[i + 2].Normal = n;
            }*/
              var lucy2 = Mesh3d.Create(new Object3dInfo(hbal.Vertices), new GenericMaterial());
              lucy2.GetInstance(0).Scale(0.5f);
              lucy2.GetInstance(0).Translate(0, 0, 0);
              lucy2.GetLodLevel(0).Material.Roughness = 0.06f;
              lucy2.GetLodLevel(0).Material.DiffuseColor = new Vector3(0, 0, 0);
              lucy2.GetLodLevel(0).Material.SpecularColor = new Vector3(1, 1, 1);
              Game.World.Scene.Add(lucy2);
              GC.Collect();

         //   var sss = Object3dManager.LoadSceneFromObj("sintel.obj", "sintel.mtl");
          //  sss.ForEach((a) => Game.World.Scene.Add(a));
            /*
            SimplePointLight spl = new SimplePointLight(new Vector3(0, 2, 0), new Vector3(12, 12, 12));
            spl.Angle = 90;
            spl.SetOrientation(new Vector3(1, -1, 0).ToQuaternion(Vector3.UnitY));
            float t = 0;
            Game.OnUpdate += (z, xx) =>{
                spl.SetPosition((float)Math.Sin(t), 2, 0);
                    t += (float)0.01;
            };
            Game.World.Scene.Add(spl);*/
            /*
            var lss = new List<SimplePointLight>();
            for(int x = 0; x < 500; x++)
            {
                SimplePointLight light = new SimplePointLight(new Vector3(-4 + (8 * (x/100.0f)), 1, 0), new Vector4((float)Math.Sin(x * 0.1), (float)Math.Cos(x*0.6), (float)Math.Tan(x * 0.9), 1.0f));
                Game.World.Scene.Add(light);
                lss.Add(light);
            }

            Game.OnBeforeDraw += (oo, ee) =>
            {
                float a = (float)(DateTime.Now - Game.StartTime).TotalMilliseconds / 1000;
                for(int x = 0; x < 100; x++)
                {
                    lss[x].SetPosition(new Vector3(-4 + (8 * (x / 100.0f)), 1 + (float)Math.Sin(x * 0.1 + a), (float)Math.Cos(x * 0.6 + a)));
                }
            };*/

            // var viperobj = Object3dManager.LoadSceneFromObj(Media.Get("viper.obj"), Media.Get("viper.mtl"), 0.1f);
            // viperobj.ForEach((a) => a.GetInstance(0).Rotate(Quaternion.FromAxisAngle(Vector3.UnitY, MathHelper.DegreesToRadians(90))));
            //  viperobj.ForEach((a) => Game.World.Scene.Add(a));

            /*   var lucy2 = Mesh3d.Create(new Object3dInfo(Object3dManager.LoadFromRaw(Media.Get("lucy.vbo.raw")).Vertices), GenericMaterial.FromColor(Color.Gold));
               lucy2.GetInstance(0).Scale(0.2f);
               lucy2.GetLodLevel(0).Material.Roughness = 0.2f;
               lucy2.GetLodLevel(0).Material.Metalness = 0.01f;
               Game.World.Scene.Add(lucy2);*/

            var testScene = new Scene();
            //var lucyobj = Object3dInfo.LoadFromRaw(Media.Get("lucy.vbo.raw"), Media.Get("lucy.indices.raw"));
            var lucyobj = new Object3dInfo(Object3dManager.LoadFromObjSingle(Media.Get("sph1.obj")).Vertices);

            var bbmaterial = new GenericMaterial();

            var lucy = Mesh3d.Create(lucyobj, bbmaterial);
            lucy.GetInstance(0).Scale(0.3f);
            testScene.Add(lucy);
            Game.World.Scene.Add(testScene);
            Commons.PickedMesh = lucy;
            Commons.Picked = lucy.GetInstance(0);
            
            Renderer pp = new Renderer(512, 512);
            CubeMapFramebuffer cubens = new CubeMapFramebuffer(512, 512);
            var tex = new CubeMapTexture(cubens.TexColor);
            Game.DisplayAdapter.MainRenderer.CubeMap = tex;
            cubens.SetPosition(new Vector3(0, 1, 0));
            Game.OnKeyPress += (o, e) =>
            {
                if(e.KeyChar == 'z')
                    cubens.SetPosition(Camera.MainDisplayCamera.GetPosition());
            };
            Game.OnBeforeDraw += (od, dsd) =>
            {
                if((lucy.GetInstance(0).GetPosition() - cubens.GetPosition()).Length > 0.01f)
                {
                    cubens.SetPosition(lucy.GetInstance(0).GetPosition());
                    pp.RenderToCubeMapFramebuffer(cubens);
                    Game.DisplayAdapter.MainRenderer.CubeMap = tex;
                    tex.Handle = cubens.TexColor;
                }
            };
            var barrelmanager = Object3dManager.LoadFromObjSingle(Media.Get("barrel.obj"));
            var barrelinfo = new Object3dInfo(barrelmanager.Vertices);
            var barrelshape = Physics.CreateConvexCollisionShape(barrelmanager);
            var barrels = Mesh3d.Create(barrelinfo, new GenericMaterial());
            barrels.AutoRecalculateMatrixForOver16Instances = true;
            barrels.GetLodLevel(0).Material.Roughness = 0.0f;
            barrels.ClearInstances();

            Game.OnKeyUp += (ox, oe) =>
            {
                if(oe.Key == OpenTK.Input.Key.Keypad0)
                {
                    var instance = barrels.AddInstance(new TransformationManager(Camera.MainDisplayCamera.GetPosition()));
                    var phys = Game.World.Physics.CreateBody(0.7f, instance, barrelshape);
                    phys.Enable();
                    phys.Body.LinearVelocity += Camera.MainDisplayCamera.GetDirection() * 6;
                }
            };
            Game.World.Scene.Add(barrels);
            /*
            List<Mesh3d> cubes = new List<Mesh3d>();
            for(int i = 0; i < 80; i++)
                cubes.Add(new Mesh3d(Object3dInfo.LoadFromObjSingle(Media.Get("normalizedcube.obj")), new GenericMaterial(Color.White)));
            cubes.ForEach((a) => Game.World.Scene.Add(a));

            List<SimplePointLight> lights = new List<SimplePointLight>();
            for(int i = 0; i < 80; i++)
            {
                lights.Add(new SimplePointLight(new Vector3(0), Vector4.Zero));
                Game.World.Scene.Add(lights[i]);
            }
                Game.OnBeforeDraw += (o, e) =>
            {
                float timeadd = (float)(DateTime.Now - Game.StartTime).TotalMilliseconds * 0.00001f;
                for(int x = 0; x < 80; x++)
                {
                    var center = new Vector3((x - 40) * 0.1f, (float)Math.Cos(x+ timeadd * x), (float)Math.Sin(x + timeadd * x));
                    lights[x].SetPosition(center);
                    lights[x].Color = new Vector4((float)Math.Sin(x + timeadd * x), (float)Math.Cos(x + timeadd * x), (float)Math.Cos(x + timeadd * 3 * x), 0.1f);

                    cubes[x].MainMaterial.Color = lights[x].Color;
                    cubes[x].Transformation.SetPosition(center);
                    cubes[x].Transformation.SetScale(0.05f);
                }
            };*/
            /*
            var scene = new VEngine.FileFormats.GameScene("home1.scene");
            //var instances = InstancedMesh3d.FromMesh3dList(testroom);
            scene.OnObjectFinish += (o, a) =>
            {
                if(!(a is Mesh3d))
                    return;
                var ob = a as Mesh3d;
                ob.SetOrientation(ob.GetPosition());
                //ob.SetCollisionShape(ob.MainObjectInfo.GetAccurateCollisionShape());
                //ob.SpecularComponent = 0.1f;
                //ob.MainMaterial.ReflectionStrength = ob.MainMaterial.SpecularComponent;
                //ob.SetCollisionShape(ob.ObjectInfo.GetAccurateCollisionShape());
                // ob.Material = new SolidColorMaterial(new Vector4(1, 1, 1, 0.1f));
                //(ob.MainMaterial as GenericMaterial).Type = GenericMaterial.MaterialType.WetDrops;
                //(ob.MainMaterial as GenericMaterial).BumpMap = null;
                this.Add(ob);
            };
            scene.Load();
            SimplePointLight p = new SimplePointLight(new Vector3(0, 3, 0), new Vector4(-2.2f, -2.2f, -2.2f, 2.6f));
            //LightPool.Add(p);
            Game.OnKeyPress += (o, e) =>
            {
                if(e.KeyChar == 'o')
                    p.Color = new Vector4(p.Color.X + 1.1f, p.Color.Y + 1.1f, p.Color.Z + 1.1f, p.Color.W);
                if(e.KeyChar == 'l')
                    p.Color = new Vector4(p.Color.X - 1.1f, p.Color.Y - 1.1f, p.Color.Z - 1.1f, p.Color.W);
                if(e.KeyChar == 'i')
                    p.Color = new Vector4(p.Color.X, p.Color.Y, p.Color.Z, p.Color.W + 1);
                if(e.KeyChar == 'k')
                    p.Color = new Vector4(p.Color.X, p.Color.Y, p.Color.Z, p.Color.W - 1);
            };*/
            // var s = VEngine.FileFormats.GameScene.FromMesh3dList(scene);
            // System.IO.File.WriteAllText("Scene.scene", s); Object3dInfo[] skydomeInfo =
            // Object3dInfo.LoadFromObj(Media.Get("sponza_verysimple.obj")); var sponza =
            // Object3dInfo.LoadSceneFromObj(Media.Get("cryteksponza.obj"),
            // Media.Get("cryteksponza.mtl"), 0.03f); List<Mesh3d> meshes = new List<Mesh3d>();
            /*   List<GenericMaterial> mats = new List<GenericMaterial>
               {
                   new GenericMaterial(new Vector4(1f, 0.6f, 0.6f, 1.0f)) {Roughness = 0.2f },
                   new GenericMaterial(new Vector4(0.9f, 0.9f, 0.9f, 1.0f)) {Roughness = 0.5f },
                   new GenericMaterial(new Vector4(0.6f, 0.6f, 1f, 1.0f)) {Roughness = 0.2f },
                   new GenericMaterial(new Vector4(1, 1, 2.05f, 1.0f)) {Roughness = 0.8f },
                   new GenericMaterial(new Vector4(0.6f, 1f, 1f, 1.0f)) {Roughness = 0.2f },
                   new GenericMaterial(new Vector4(1f, 0.6f, 1f, 1.0f)),
                   new GenericMaterial(new Vector4(1, 0, 0.1f, 1.0f)),
                   new GenericMaterial(new Vector4(1, 0, 0.1f, 1.0f)),
                   new GenericMaterial(new Vector4(1, 0, 0.1f, 1.0f)),
                   new GenericMaterial(new Vector4(1, 0, 0.1f, 1.0f)),
               };
               int ix = 0;
               foreach(var sd in skydomeInfo)
               {
                   var skydomeMaterial = mats[ix++ % mats.Count];
                   var skydome = new Mesh3d(sd, skydomeMaterial);
                   skydome.Scale(3);
                   meshes.Add(skydome);
               }*/
            // var Tracer = new PathTracer(); Tracer.PrepareTrianglesData(meshes);
            /* List<Triangle> triangles = new List<Triangle>();
             foreach(var mesh in scene)
             {
                 var Triangle = new Triangle();
                 Triangle.Tag = mesh;
                 var vertices = mesh.MainObjectInfo.GetOrderedVertices();
                 var normals = mesh.MainObjectInfo.GetOrderedNormals();
                 for(int i = 0; i < vertices.Count; i++)
                 {
                     var vertex = new Vertex()
                     {
                         Position = vertices[i],
                         Normal = normals[i],
                         Albedo = mesh.MainMaterial.Color.Xyz
                     };
                     vertex.Tranform(mesh.Matrix);
                     Triangle.Vertices.Add(vertex);
                     if(Triangle.Vertices.Count == 3)
                     {
                         triangles.Add(Triangle);
                         Triangle = new Triangle();
                         Triangle.Tag = mesh;
                     }
                 }
             }
             SceneOctalTree tree = new SceneOctalTree();
             Triangle[] trcopy = new Triangle[triangles.Count];
             triangles.CopyTo(trcopy);
             tree.CreateFromTriangleList(trcopy.ToList());
             var TriangleCount = triangles.Count;
             // lets prepare byte array layout posx, posy, poz, norx, nory, norz, albr, albg, albz
             List<byte> bytes = new List<byte>();
             foreach(var triangle in triangles)
             {
                 foreach(var vertex in triangle.Vertices)
                 {
                     bytes.AddRange(BitConverter.GetBytes(vertex.Position.X));
                     bytes.AddRange(BitConverter.GetBytes(vertex.Position.Y));
                     bytes.AddRange(BitConverter.GetBytes(vertex.Position.Z));
                     bytes.AddRange(BitConverter.GetBytes((float)(triangle.Tag as Mesh3d).MainMaterial.Roughness));

                     bytes.AddRange(BitConverter.GetBytes(vertex.Normal.X));
                     bytes.AddRange(BitConverter.GetBytes(vertex.Normal.Y));
                     bytes.AddRange(BitConverter.GetBytes(vertex.Normal.Z));
                     bytes.AddRange(BitConverter.GetBytes(0.0f));

                     bytes.AddRange(BitConverter.GetBytes(vertex.Albedo.X));
                     bytes.AddRange(BitConverter.GetBytes(vertex.Albedo.Y));
                     bytes.AddRange(BitConverter.GetBytes(vertex.Albedo.Z));
                     bytes.AddRange(BitConverter.GetBytes(0.0f));
                 }
             }
             var octree = new SceneOctalTree();
             octree.CreateFromTriangleList(triangles);
             InstancedMesh3d ims = new InstancedMesh3d(Object3dInfo.LoadFromObjSingle(Media.Get("normalizedcube.obj")), new GenericMaterial(Color.White));
             Action<SceneOctalTree.Box> a = null;
             a = new Action<SceneOctalTree.Box>((box) =>
             {
                 if(box.Children.Count == 0)
                     ims.Transformations.Add(new TransformationManager(new Vector3(box.Center), Quaternion.Identity, box.Radius));
                 else
                     foreach(var b in box.Children)
                         a(b);
             });
             foreach(var b in octree.BoxTree.Children)
             {
                 a(b);
             }
             ims.UpdateMatrix();
             Add(ims);
             */
            //   PostProcessing.Tracer = Tracer;
            //var protagonist = Object3dInfo.LoadSceneFromObj(Media.Get("protagonist.obj"), Media.Get("protagonist.mtl"), 1.0f);
            //foreach(var o in protagonist)
            //    Add(o);
            /*
           var fountainWaterObj = Object3dInfo.LoadFromObjSingle(Media.Get("turbinegun.obj"));
           var water = new Mesh3d(fountainWaterObj, new GenericMaterial(new Vector4(1, 1, 1, 1)));
           water.Transformation.Scale(1.0f);
           water.Translate(0, 10, 0);
           Add(water);*/
            /*
             Object3dInfo waterInfo = Object3dGenerator.CreateTerrain(new Vector2(-200, -200), new Vector2(200, 200), new Vector2(30, 30), Vector3.UnitY, 333, (x, y) => 0);

            var color = new GenericMaterial(Color.SkyBlue);
            color.SetBumpMapFromMedia("cobblestone.jpg");
           // color.Type = GenericMaterial.MaterialType.Water;
            Mesh3d water2 = new Mesh3d(waterInfo, color);
            water2.SetMass(0);
             color.Roughness = 0.8f;
            water2.Translate(0, 0.1f, 0);
            water2.MainMaterial.ReflectionStrength = 1;
            //water.SetCollisionShape(new BulletSharp.StaticPlaneShape(Vector3.UnitY, 0));
            Add(water2);
            var dragon3dInfo = Object3dInfo.LoadFromObjSingle(Media.Get("apple.obj"));
            dragon3dInfo.ScaleUV(0.1f);
            var mat = GenericMaterial.FromMedia("skin.jpg");
            var dragon = new Mesh3d(dragon3dInfo, mat);
            //mat.Type = GenericMaterial.MaterialType.WetDrops;
            //dragon.Scale(5);
            dragon.SetMass(0);
            dragon.SetCollisionShape(dragon3dInfo.GetAccurateCollisionShape());
            Add(dragon);
             /*
             Object3dInfo waterInfo = Object3dGenerator.CreateTerrain(new Vector2(-200, -200), new Vector2(200, 200), new Vector2(100, 100), Vector3.UnitY, 333, (x, y) => 0);

             var color = new GenericMaterial(Color.Green);
             color.SetBumpMapFromMedia("grassbump.png");
             color.Type = GenericMaterial.MaterialType.Grass;
             Mesh3d water = new Mesh3d(waterInfo, color);
             water.SetMass(0);
             water.Translate(0, 1, 0);
             water.SetCollisionShape(new BulletSharp.StaticPlaneShape(Vector3.UnitY, 0));
             Add(water);

             var dragon3dInfo = Object3dInfo.LoadFromRaw(Media.Get("lucymidres.vbo.raw"), Media.Get("lucymidres.indices.raw"));
             dragon3dInfo.ScaleUV(0.1f);
             var dragon = new Mesh3d(dragon3dInfo, new GenericMaterial(Color.White));
             //dragon.Translate(0, 0, 20);
             dragon.Scale(80);
             Add(dragon);
             */
        }
Exemple #19
0
        public NatureScene()
        {
            var sun = new Sun(new Vector3(0.1f, -1, 0).ToQuaternion(Vector3.UnitY), new Vector4(1, 0.97f, 0.92f, 120), 100, 70, 10, 4, 1);
            GLThread.OnUpdate += (o, e) =>
            {
                var kb = OpenTK.Input.Keyboard.GetState();
                if(kb.IsKeyDown(OpenTK.Input.Key.U))
                {
                    var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), -0.01f);
                    sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                }
                if(kb.IsKeyDown(OpenTK.Input.Key.J))
                {
                    var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), 0.01f);
                    sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                }
                if(kb.IsKeyDown(OpenTK.Input.Key.H))
                {
                    var quat = Quaternion.FromAxisAngle(Vector3.UnitY, -0.01f);
                    sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                }
                if(kb.IsKeyDown(OpenTK.Input.Key.K))
                {
                    var quat = Quaternion.FromAxisAngle(Vector3.UnitY, 0.01f);
                    sun.Orientation = Quaternion.Multiply(sun.Orientation, quat);
                }
            };
            int scale = 2;
            /*
            var protagonist = Object3dInfo.LoadSceneFromObj(Media"protagonist.obj", "protagonist.mtl", 1.0f);
            foreach(var o in protagonist)
                Add(o);*/

            var hmap = System.IO.File.ReadAllBytes(Media.Get("map1.raw"));
            int count = 0;
            Func<uint, uint, float> terrainGen = (x, y) =>
            {
                int yy = (int)(((double)y / (1024.0 / scale)) * 2000);
                int xx = (int)(((double)x / (1024.0 / scale)) * 3000);
                int ix = (int)(yy * 3000  + xx) * 3;
                count++;
                if(count % 1000 == 0)
                    Console.WriteLine(count * 100 / ((1024 / scale) * (1024 / scale)));
                return ix < hmap.Length ? hmap[ix] * 0.3f : 0;
            };
            Object3dGenerator.UseCache = false;
            Object3dInfo terrainInfo = Object3dGenerator.CreateTerrain(new Vector2(-1500, -1000), new Vector2(1500, 1000), new Vector2(1096, 1096), Vector3.UnitY, 1024 / scale, terrainGen);
            hmap = null;
            GC.Collect();

            Object3dInfo waterInfo = Object3dGenerator.CreateTerrain(new Vector2(-1500, -1000), new Vector2(1500, 1000), new Vector2(1096, 1096), Vector3.UnitY, 121, (x, y) => 0);

            //var terrainInfo = Object3dInfo.LoadFromObjSingle(Media.Get("terrain11.obj"));
            //terrainInfo.ScaleUV(100);
            var color = GenericMaterial.FromMedia("151.JPG", "151_norm.JPG");
            color.SetBumpMapFromMedia("grassbump.png");
            color.Type = GenericMaterial.MaterialType.Grass;
            var terrain = new Mesh3d(terrainInfo, color);
               // terrain.Scale(5);
            Add(terrain);

            var waterMat = new GenericMaterial(new Vector4(0.55f, 0.74f, 0.97f, 1.0f));
            waterMat.SetNormalMapFromMedia("151_norm.JPG");
            waterMat.Type = GenericMaterial.MaterialType.Water;
            var water = new Mesh3d(waterInfo, waterMat);
            water.Transformation.Translate(0, 15, 0);
            //water.DisableDepthWrite = true;
            //water.ReflectionStrength = 1;
            Add(water);
            /*
            var dragon3dInfo = Object3dInfo.LoadFromRaw(Media.Get("lucyhires.vbo.raw"), Media.Get("lucyhires.indices.raw"));
            dragon3dInfo.ScaleUV(0.1f);
            var dragon = new Mesh3d(dragon3dInfo, SingleTextureMaterial.FromMedia("180.jpg", "180_norm.jpg"));
            Add(dragon);
            */
               // var daetest = Object3dInfo.LoadFromObjSingle(Media.Get("carreragt.obj"));
              //  daetest.CorrectFacesByNormals();
               // var car = new Mesh3d(daetest, new SolidColorMaterial(Color.Red));
               // Add(car);
               /* var rand = new Random();

            var scene1 = Object3dInfo.LoadSceneFromObj(Media.Get("tree1.obj"), Media.Get("tree1.mtl"), 12);
            List<Mesh3d> trees = new List<Mesh3d>();
            for(int i = 0; i < 44; i++)
            {
                foreach(var ob in scene1)
                {
                    var copy = new Mesh3d(ob.MainObjectInfo, ob.MainMaterial);
                    copy.Translate(rand.Next(-40, 40), 10, rand.Next(-40, 40));
                    copy.Scale((float)rand.NextDouble() + 1.0f);
                    trees.Add(copy);
                }
            }
            InstancedMesh3d.FromMesh3dList(trees).ForEach((a) =>
            {
                a.UpdateMatrix();
                Add(a);
            });
            var grasslod0 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod1.obj"));
            var grasslod1 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod1.obj"));
            var grasslod2 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod2.obj"));
            var grasslod3 = Object3dInfo.LoadFromObjSingle(Media.Get("grasslod3.obj"));
            //var grassBlock3dInfo = Object3dInfo.LoadFromRaw(Media.Get("grassblock2.vbo.raw"), Media.Get("grassblock2.indices.raw"));
            //grassBlock3dInfo.MakeDoubleFaced();
            var grasscolor = new GenericMaterial(Color.DarkGreen);
            var grassInstanced = new InstancedMesh3d(grasslod3, grasscolor);
            //grassInstanced.AddLodLevel(15, grasslod0, grasscolor);
            //grassInstanced.AddLodLevel(60, grasslod1, grasscolor);
            //grassInstanced.AddLodLevel(200, grasslod2, grasscolor);
            //grassInstanced.AddLodLevel(600, grasslod3, grasscolor);
               // GLThread.CreateTimer(() =>
            //{
            //    grassInstanced.RecalculateLod();
            //}, 100).Start();
            /*for(int x = -500; x < 500; x++)
            {
                for(int y = -500; y < 500; y++)
                {
                    grassInstanced.Transformations.Add(new TransformationManager(new Vector3(x, 0, y), Quaternion.FromAxisAngle(Vector3.UnitY, (float)rand.NextDouble() * MathHelper.Pi), new Vector3(1, 1, 1)));
                    grassInstanced.Instances++;
                }
            }
            grassInstanced.UpdateMatrix();
            Add(grassInstanced);*/
            /*
            var grassBlock3dInfo2 = Object3dInfo.LoadFromRaw(Media.Get("grassblock2.vbo.raw"), Media.Get("grassblock2.indices.raw"));
            //grassBlock3dInfo2.MakeDoubleFaced();
            var grassInstanced2 = new InstancedMesh3d(grassBlock3dInfo2, new SolidColorMaterial(Color.DarkGoldenrod));
            for(int x = -20; x < 20; x++)
            {
                for(int y = -20; y < 20; y++)
                {
                    grassInstanced2.Transformations.Add(new TransformationManager(new Vector3(x * 2, 0, y * 2), Quaternion.FromAxisAngle(Vector3.UnitY, (float)rand.NextDouble() * MathHelper.Pi), 2.3f));
                    grassInstanced2.Instances++;
                }
            }
            grassInstanced2.UpdateMatrix();
            Add(grassInstanced2);*/
            /*var scene = Object3dInfo.LoadSceneFromObj(Media.Get("cryteksponza.obj"), Media.Get("cryteksponza.mtl"), 0.03f);
            //var instances = InstancedMesh3d.FromMesh3dList(testroom);
            foreach(var ob in scene)
            {
                //ob.SetMass(0);
                // ob.SetCollisionShape(ob.ObjectInfo.GetAccurateCollisionShape());
                //ob.SpecularComponent = 0.1f;
                this.Add(ob);
            }*/

            Object3dInfo skydomeInfo = Object3dInfo.LoadFromObjSingle(Media.Get("usky.obj"));
            var skydomeMaterial = GenericMaterial.FromMedia("skyreal.png");
            var skydome = new Mesh3d(skydomeInfo, skydomeMaterial);
            skydome.Transformation.Scale(55000);
            skydome.Transformation.Translate(0, -100, 0);
            skydome.MainMaterial.IgnoreLighting = true;
            skydome.MainMaterial.DiffuseComponent = 0.2f;
            World.Root.Add(skydome);
        }