コード例 #1
0
ファイル: Helpers.cs プロジェクト: zulis/Sandbox
        public static void SetTextureFromResource(ICore core, TVMesh mesh, Bitmap texture, int groupID)
        {
            var ms = new MemoryStream();

            texture.Save(ms, ImageFormat.Png);
            var data = new byte[ms.Length];

            ms.Seek(0, 0);
            data = ms.ToArray();
            ms.Dispose();
            texture.Dispose();
            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
            int      addr   = handle.AddrOfPinnedObject().ToInt32();

            handle.Free();
            string ds    = core.Globals.GetDataSourceFromMemory(addr, data.Length - 1);
            int    texID = core.TextureFactory.LoadTexture(ds);

            mesh.SetTexture(texID, groupID);
        }
コード例 #2
0
        //Inicializace a vytvoreni mistnosti
        private void VyvorMistnost()
        {
            //Inicializace mistnosti
            Room = Scene.CreateMeshBuilder("RoomMesh");

            //Nacteni textur
            VytvorTextury();

            //Pridani sten a nastaveni pozice mistnosti
            Room.AddWall3D(Globals.GetTex("Wall"), 150.0f, -350.0f, -350.0f, -350.0f, 350.0f, 5.0f, false, false, -50.0f, 5.0f, 5.0f);
            Room.AddWall3D(Globals.GetTex("Wall"), -350.0f, -350.0f, -350.0f, 150.0f, 350.0f, 5.0f, false, false, -50.0f, 5.0f, 5.0f);
            Room.AddWall3D(Globals.GetTex("Wall"), -350.0f, 150.0f, 150.0f, 150.0f, 350.0f, 5.0f, false, false, -50.0f, 5.0f, 5.0f);
            Room.AddWall3D(Globals.GetTex("Wall"), 150.0f, 150.0f, 150.0f, -350.0f, 350.0f, 5.0f, false, false, -50.0f, 5.0f, 5.0f);
            Room.AddFloor(Globals.GetTex("Floor"), -350.0f, -350f, 150.0f, 150.0f, -50.0f, 20.0f, 20.0f, true);
            Room.AddFloor(Globals.GetTex("Floor"), -350.0f, -350.0f, 150.0f, 150.0f, 300.0f, 10.0f, 10.0f, true);
            Room.SetPosition((float)30.0f, (float)-30.0f, (float)150.0f);

            //Pridani stolku
            Obj1 = new TVMesh();
            Obj1 = Scene.CreateMeshBuilder("Obj1");
            Obj1.LoadXFile("Meshe\\stul.x", true, true);
            Obj1.SetPosition(-57.0f, -80.0f, 38.0f);
            Obj1.SetTexture(Globals.GetTex("Wood"), -1);
            Obj1.SetMeshName("other");

            Obj2 = new TVMesh();
            Obj2 = Scene.CreateMeshBuilder("Obj2");
            Obj2.LoadXFile("Meshe\\okraj.x", true, true);
            Obj2.SetPosition(xc + 17.5f, yc - 1.0f, zc + 17.5f);
            Obj2.SetTexture(Globals.GetTex("Wood2"), -1);
            Obj2.SetMeshName("other");
        }
コード例 #3
0
ファイル: Mesh.cs プロジェクト: zulis/Sandbox
        private void LoadTextures()
        {
            if (!settings.UseAdvanceTexturing)
            {
                // Set default texture for mesh with no textures.
                for (int i = 0; i < mesh.GetGroupCount(); i++)
                {
                    TV_TEXTURE textureInfo = Core.TextureFactory.GetTextureInfo(mesh.GetTexture(i));
                    if (textureInfo.Name.Equals(Helpers.TEXTURE_BLANK))
                    {
                        SetDefaultTexture(mesh, i);
                    }
                }
            }
            else if (!enableLightning)
            {
                mesh.SetMaterial(0);
                mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_LIGHTMAP, lightmapIdx);
                mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
                mesh.SetShadowCast(false, false);
            }
            else
            {
                mesh.SetShadowCast(true, true);
                var lightMode = LightMode.None;

                int defaultNormalTexId   = -1;
                int defaultSpecularTexId = -1;

                for (var i = 0; i < mesh.GetGroupCount(); i++)
                {
                    var textureInfo = Core.TextureFactory.GetTextureInfo(mesh.GetTexture(i));

                    var normalTexture       = string.Empty;
                    var normalTextureName   = string.Empty;
                    var specularTexture     = string.Empty;
                    var specularTextureName = string.Empty;
                    var heightTexture       = string.Empty;
                    var heightTextureName   = string.Empty;

                    if (textureInfo.Name.Equals(Helpers.TEXTURE_BLANK))
                    {
                        Helpers.SetTextureFromResource(core, mesh, Resources.defaultTexture, i);
                    }
                    else if (Helpers.IsTextureFromMemory(textureInfo.Filename))
                    {
                        mesh.SetTexture(Core.TextureFactory.LoadTexture(textureInfo.Name), i);
                    }
                    else
                    {
                        var texture = textureInfo.Filename;

                        var fileInfo = new FileInfo(textureInfo.Filename);
                        normalTextureName = fileInfo.Name.Replace(fileInfo.Extension,
                                                                  settings.TextureNormalSuffix +
                                                                  fileInfo.Extension);
                        normalTexture     = Directory.GetParent(textureInfo.Filename) + @"\" + normalTextureName;
                        heightTextureName = fileInfo.Name.Replace(fileInfo.Extension,
                                                                  settings.TextureHeightSuffix +
                                                                  fileInfo.Extension);
                        heightTexture       = Directory.GetParent(textureInfo.Filename) + @"\" + heightTextureName;
                        specularTextureName = fileInfo.Name.Replace(fileInfo.Extension,
                                                                    settings.TextureSpecularSuffix +
                                                                    fileInfo.Extension);
                        specularTexture = Directory.GetParent(textureInfo.Filename) + @"\" + specularTextureName;
                        //mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_BASETEXTURE, core.Globals.GetTex(textureInfo.Name), i);
                    }

                    var normalId = -1;
                    if (File.Exists(normalTexture))
                    {
                        normalId = core.TextureFactory.LoadTexture(normalTexture, normalTextureName);
                    }
                    else
                    {
                        if (defaultNormalTexId == -1)
                        {
                            normalId           = Helpers.LoadTextureFromResourceToMemory(core, Resources.defaultNormal);
                            defaultNormalTexId = normalId;
                        }
                        else
                        {
                            normalId = defaultNormalTexId;
                        }
                    }

                    mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_NORMALMAP, normalId, i);

                    if (lightMode != LightMode.Offset)
                    {
                        lightMode = LightMode.Normal;
                    }

                    #region Set specular texture
                    // http://www.truevision3d.com/forums/empty-t19109.0.html;msg131232#msg131232
                    var specularId = -1;

                    if (File.Exists(specularTexture))
                    {
                        specularId = Core.TextureFactory.AddAlphaChannel(normalId,
                                                                         Core.TextureFactory.LoadTexture(specularTexture),
                                                                         specularTexture);
                    }
                    else
                    {
                        if (defaultSpecularTexId == -1)
                        {
                            defaultSpecularTexId = Helpers.LoadTextureFromResourceToMemory(core, Resources.defaultSpecular);
                        }

                        specularId = Core.TextureFactory.AddAlphaChannel(normalId, defaultSpecularTexId, "defaultSpecular");
                    }

                    mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_SPECULARMAP, specularId, i);
                    #endregion

                    if (File.Exists(heightTexture))
                    {
                        mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_HEIGHTMAP,
                                          Core.TextureFactory.LoadTexture(heightTexture), i);
                        lightMode = LightMode.Offset;
                    }

                    if (core.MaterialIdx == -1)
                    {
                        TV_COLOR ambient  = new TV_COLOR(0.25f, 0.25f, 0.25f, 1f);
                        TV_COLOR diffuse  = new TV_COLOR(1f, 1f, 1f, 1f);
                        TV_COLOR specular = new TV_COLOR(0.35f, 0.35f, 0.35f, 1f);
                        TV_COLOR emissive = new TV_COLOR(1f, 1f, 1f, 1f);
                        float    power    = 100;

                        core.MaterialIdx = Core.MaterialFactory.CreateMaterial(Guid.NewGuid().ToString());
                        core.MaterialFactory.SetAmbient(core.MaterialIdx, ambient.r, ambient.g, ambient.b, ambient.a);
                        core.MaterialFactory.SetDiffuse(core.MaterialIdx, diffuse.r, diffuse.g, diffuse.b, diffuse.a);
                        core.MaterialFactory.SetSpecular(core.MaterialIdx, specular.r, specular.g, specular.b, specular.a);
                        core.MaterialFactory.SetEmissive(core.MaterialIdx, emissive.r, emissive.g, emissive.b, emissive.a);
                        core.MaterialFactory.SetPower(core.MaterialIdx, power);
                    }

                    mesh.SetMaterial(core.MaterialIdx, i);
                }

                switch (lightMode)
                {
                case LightMode.Normal:
                    mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                    break;

                case LightMode.Offset:
                    mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_OFFSETBUMPMAPPING_TANGENTSPACE);
                    break;

                case LightMode.Managed:
                    mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
                    break;

                default:
                    mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_NONE);
                    break;
                }
            }
        }
コード例 #4
0
ファイル: Form_Main.cs プロジェクト: salih18200/orcs
        private void InitObjects()
        {
            #region Car
            //Building PK9
            pk_9 = scene.CreateMeshBuilder("pk");
            // load the object from an x file
            pk_9.LoadTVM(@"Models\pk8.tvm", false, false);
            // set its position
            pk_9.SetPosition(5.0f, 0.0f, 50.0f);
            // make the table 3x larger
            pk_9.SetScale(3, 3, 3);
            // rotate it 25 degrees around the Y 3D Axis
            pk_9.RotateY(25, true);
            // set the tables texture
            pk_9.SetShadowCast(true, true);
            pk_9.SetTexture(globals.GetTex("pk9tex"), 0);

            //Chassis
            m_chassis = scene.CreateMeshBuilder("mChassis");
            m_chassis.LoadTVM(@"Models\chassis.tvm", false, false);
            m_chassis.SetShadowCast(true, true);
            m_chassis.SetTexture(globals.GetTex("ChassisSTI"), 0);
            //m_chassis.SetTextureEx(0, globals.GetTex("ChassisSTI"), 1);
            //m_chassis.SetTextureEx(1, globals.GetTex("ChassisSTI"), 1);
            m_chassis.SetTexture(globals.GetTex("UnderCarriage"), 2);
            m_chassis.SetMaterial(matWindow, 1);
            m_chassis.SetAlphaTest(true, 0, true, 1);
            m_chassis.SetBlendingMode(CONST_TV_BLENDINGMODE.TV_BLEND_ADD, 1);
            m_chassis.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_chassis.SetShadowCast(true, true);

            //Front Left Wheel
            float scale = 1f;
            m_fl = scene.CreateMeshBuilder("mfl");
            m_fl.LoadTVM(@"Models\wheel_l.tvm", true, true);
            m_fl.SetScale(scale, scale, scale);
            m_fl.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_fl.SetMaterial(matWheels);
            m_fl.SetTexture(globals.GetTex("Wheel"));
            m_fl.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_fl.SetShadowCast(true, true);

            //Front Right Wheel
            m_rl = scene.CreateMeshBuilder("mrl");
            m_rl.LoadTVM(@"Models\wheel_l.tvm", true, true);
            m_rl.SetScale(scale, scale, scale);
            m_rl.SetMaterial(matWheels);
            m_rl.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_rl.SetTexture(globals.GetTex("Wheel"));
            m_rl.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_rl.SetShadowCast(true, false);
            m_rl.SetShadowCast(true, true);

            //Rear Left Wheel
            m_fr = scene.CreateMeshBuilder("mfr");
            m_fr.LoadTVM(@"Models\wheel_r.tvm", true, true);
            m_fr.SetScale(scale, scale, scale);
            m_fr.SetMaterial(matWheels);
            m_fr.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_fr.SetTexture(globals.GetTex("Wheel"));
            m_fr.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_fr.SetShadowCast(true, false);

            //Rear Right Wheel
            m_rr = scene.CreateMeshBuilder("mrr");
            m_rr.LoadTVM(@"Models\wheel_r.tvm", true, true);
            m_rr.SetScale(scale, scale, scale);
            m_rr.SetMaterial(matWheels);
            m_rr.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_rr.SetTexture(globals.GetTex("Wheel"));
            m_rr.SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
            m_rr.SetShadowCast(true, false);
            m_rr.SetShadowCast(true, true);

            m_chassis.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
            m_chassis.SetMaterial(matVehicleBody);
            m_chassis.ComputeNormals();
            m_chassis.ComputeBoundings();
            m_chassis.SetScale(scale, scale, scale);
            #endregion

            //Add The Physics to the chassis
            pbi_chassis = physics.CreateMeshBody(1500, m_chassis, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CONVEXHULL); //1500
            physics.SetAutoFreeze(pbi_chassis, false);
            physics.SetBodyPosition(pbi_chassis, 0f, 15, 0f);
            physics.SetBodyRotation(pbi_chassis, 0f, 0f, 0f);

            //Create The Vehicle
            car_ID = physics.CreateVehicle(pbi_chassis);

            //Do Suspention Settings
            float susheight = 1.5f; //distance from chassis to wheel 0.5f
            float susplen = 1.5f; // 10
            float susshock = 40f; //Springiness of suspension 10
            float susspring = 300f; //Stiffness of suspension 400
            flw = physics.AddVehicleWheelEx(car_ID, 25f, 0.5f * scale, 0.372f * scale+0.1f, new TV_3DVECTOR(1, 0, 0), -0.8f * scale, -susheight * scale - 0.1f, 1.25f * scale + 0.5f, 1, 0, 0, susplen, susshock, susspring, m_fl); //fl
            frw = physics.AddVehicleWheelEx(car_ID, 25f, 0.5f * scale, 0.372f * scale+0.1f, new TV_3DVECTOR(1, 0, 0), 0.8f * scale, -susheight * scale - 0.1f, 1.25f * scale + 0.5f, 1, 0, 0, susplen, susshock, susspring, m_fr); //fr
            rlw = physics.AddVehicleWheelEx(car_ID, 25f, 0.5f * scale, 0.372f * scale+0.1f, new TV_3DVECTOR(1, 0, 0), -0.8f * scale, -susheight * scale - 0.1f, -1.425f * scale + 0.2f, 1, 0, 0, susplen, susshock, susspring, m_rl); //rl
            rrw = physics.AddVehicleWheelEx(car_ID, 25f, 0.5f * scale, 0.372f * scale+0.1f, new TV_3DVECTOR(1, 0, 0), 0.8f * scale, -susheight * scale - 0.1f, -1.425f * scale + 0.2f, 1, 0, 0, susplen, susshock, susspring, m_rr); //rr

            //Change the car's center of mass / make it drive better
            physics.SetBodyCenterOfMass(car_ID, new TV_3DVECTOR(0, -1.0f, 10f));

            //Add wheel frictions
            //Note that this code will also stop sliding on slopes
            float sideslip = 0.1f;
            float sideslipcoef = 0f;
            float maxlongslide = 10000f;
            float maxlongslidecoef = 0f;
            physics.SetVehicleWheelParameters(car_ID, flw, sideslip, sideslipcoef, maxlongslide, maxlongslidecoef);
            physics.SetVehicleWheelParameters(car_ID, frw, sideslip, sideslipcoef, maxlongslide, maxlongslidecoef);
            physics.SetVehicleWheelParameters(car_ID, rlw, sideslip, sideslipcoef, maxlongslide, maxlongslidecoef);
            physics.SetVehicleWheelParameters(car_ID, rrw, sideslip, sideslipcoef, maxlongslide, maxlongslidecoef);
        }
コード例 #5
0
ファイル: Helpers.cs プロジェクト: zulis/Sandbox
 public static void SetTextureFromResource(ICore core, TVMesh mesh, Bitmap texture, int groupID)
 {
     var ms = new MemoryStream();
     texture.Save(ms, ImageFormat.Png);
     var data = new byte[ms.Length];
     ms.Seek(0, 0);
     data = ms.ToArray();
     ms.Dispose();
     texture.Dispose();
     GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
     int addr = handle.AddrOfPinnedObject().ToInt32();
     handle.Free();
     string ds = core.Globals.GetDataSourceFromMemory(addr, data.Length - 1);
     int texID = core.TextureFactory.LoadTexture(ds);
     mesh.SetTexture(texID, groupID);
 }