示例#1
0
        /// <summary>
        /// Crear flechas de debug para normales, tangentes y binormales
        /// </summary>
        private void loadDebugArrows(TgcMeshBumpMapping mesh)
        {
            //Obtener vertexBuffer
            TgcMeshBumpMapping.BumpMappingVertex[] vertexBuffer = (TgcMeshBumpMapping.BumpMappingVertex[])mesh.D3dMesh.LockVertexBuffer(
                typeof(TgcMeshBumpMapping.BumpMappingVertex), LockFlags.ReadOnly, mesh.D3dMesh.NumberVertices);
            mesh.D3dMesh.UnlockVertexBuffer();

            for (int i = 0; i < vertexBuffer.Length; i++)
            {
                TgcMeshBumpMapping.BumpMappingVertex v = vertexBuffer[i];
                normals.Add(TgcArrow.fromDirection(v.Position, v.Normal * 50, Color.Blue, Color.Yellow, 0.5f, new Vector2(2f, 4f)));
                tangents.Add(TgcArrow.fromDirection(v.Position, v.Tangent * 50, Color.Red, Color.Yellow, 0.5f, new Vector2(2f, 4f)));
                binormals.Add(TgcArrow.fromDirection(v.Position, v.Binormal * 50, Color.Green, Color.Yellow, 0.5f, new Vector2(2f, 4f)));
            }
        }
示例#2
0
        /// <summary>
        /// Crear un TgcMeshBumpMapping en base a un TgcMesh y su normalMap.
        /// Solo esta soportado un TgcMehs MeshRenderType = DiffuseMap
        /// </summary>
        public static TgcMeshBumpMapping fromTgcMesh(TgcMesh mesh, TgcTexture[] normalMaps)
        {
            if (mesh.RenderType != MeshRenderType.DIFFUSE_MAP)
            {
                throw new Exception("Solo esta soportado MeshRenderType = DiffuseMap");
            }


            //Obtener vertexBuffer original
            TgcSceneLoader.DiffuseMapVertex[] origVertexBuffer = (TgcSceneLoader.DiffuseMapVertex[])mesh.D3dMesh.LockVertexBuffer(
                typeof(TgcSceneLoader.DiffuseMapVertex), LockFlags.ReadOnly, mesh.D3dMesh.NumberVertices);
            mesh.D3dMesh.UnlockVertexBuffer();

            //Crear nuevo Mesh de DirectX
            int  triCount = origVertexBuffer.Length / 3;
            Mesh d3dMesh  = new Mesh(triCount, origVertexBuffer.Length, MeshFlags.Managed, BumpMappingVertexElements, GuiController.Instance.D3dDevice);

            //Calcular normales recorriendo los triangulos
            Vector3[] normals = new Vector3[origVertexBuffer.Length];
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i] = new Vector3(0, 0, 0);
            }
            for (int i = 0; i < triCount; i++)
            {
                //Los 3 vertices del triangulo
                TgcSceneLoader.DiffuseMapVertex v1 = origVertexBuffer[i * 3];
                TgcSceneLoader.DiffuseMapVertex v2 = origVertexBuffer[i * 3 + 1];
                TgcSceneLoader.DiffuseMapVertex v3 = origVertexBuffer[i * 3 + 2];

                //Face-normal (left-handend)
                Vector3 a = v2.Position - v1.Position;
                Vector3 b = v3.Position - v1.Position;
                Vector3 n = Vector3.Cross(a, b);

                //Acumular normal del vertice segun todas sus Face-normal
                normals[i * 3]     += n;
                normals[i * 3 + 1] += n;
                normals[i * 3 + 2] += n;
            }

            //Normalizar normales
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i] = Vector3.Normalize(normals[i]);
            }

            //Crear nuevo VertexBuffer
            using (VertexBuffer vb = d3dMesh.VertexBuffer)
            {
                //Iterar sobre triangulos
                GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
                for (int i = 0; i < triCount; i++)
                {
                    //Vertices originales
                    TgcSceneLoader.DiffuseMapVertex vOrig1 = origVertexBuffer[i * 3];
                    TgcSceneLoader.DiffuseMapVertex vOrig2 = origVertexBuffer[i * 3 + 1];
                    TgcSceneLoader.DiffuseMapVertex vOrig3 = origVertexBuffer[i * 3 + 2];

                    //Nuevo vertice 1
                    BumpMappingVertex v1 = new BumpMappingVertex();
                    v1.Position = vOrig1.Position;
                    v1.Color    = vOrig1.Color;
                    v1.Tu       = vOrig1.Tu;
                    v1.Tv       = vOrig1.Tv;
                    v1.Normal   = normals[i * 3];

                    //Nuevo vertice 2
                    BumpMappingVertex v2 = new BumpMappingVertex();
                    v2.Position = vOrig2.Position;
                    v2.Color    = vOrig2.Color;
                    v2.Tu       = vOrig2.Tu;
                    v2.Tv       = vOrig2.Tv;
                    v2.Normal   = normals[i * 3 + 1];

                    //Nuevo vertice 3
                    BumpMappingVertex v3 = new BumpMappingVertex();
                    v3.Position = vOrig3.Position;
                    v3.Color    = vOrig3.Color;
                    v3.Tu       = vOrig3.Tu;
                    v3.Tv       = vOrig3.Tv;
                    v3.Normal   = normals[i * 3 + 2];

                    //Calcular tangente y binormal para todo el triangulo y cargarlas en cada vertice
                    Vector3 tangent;
                    Vector3 binormal;
                    TgcMeshBumpMapping.computeTangentBinormal(v1, v2, v3, out tangent, out binormal);
                    v1.Tangent  = tangent;
                    v1.Binormal = binormal;
                    v2.Tangent  = tangent;
                    v2.Binormal = binormal;
                    v3.Tangent  = tangent;
                    v3.Binormal = binormal;

                    //Cargar VertexBuffer
                    data.Write(v1);
                    data.Write(v2);
                    data.Write(v3);
                }

                vb.Unlock();
            }

            //Cargar IndexBuffer en forma plana
            using (IndexBuffer ib = d3dMesh.IndexBuffer)
            {
                short[] indices = new short[origVertexBuffer.Length];
                for (int i = 0; i < indices.Length; i++)
                {
                    indices[i] = (short)i;
                }
                ib.SetData(indices, 0, LockFlags.None);
            }

            //Clonar texturas y materials
            TgcTexture[] diffuseMaps = new TgcTexture[mesh.DiffuseMaps.Length];
            Material[]   materials   = new Material[mesh.Materials.Length];
            for (int i = 0; i < mesh.DiffuseMaps.Length; i++)
            {
                diffuseMaps[i] = mesh.DiffuseMaps[i].clone();
                materials[i]   = TgcD3dDevice.DEFAULT_MATERIAL;
            }

            //Cargar attributeBuffer
            if (diffuseMaps.Length > 1)
            {
                int[] origAttributeBuffer = mesh.D3dMesh.LockAttributeBufferArray(LockFlags.None);
                int[] newAttributeBuffer  = d3dMesh.LockAttributeBufferArray(LockFlags.None);
                Array.Copy(origAttributeBuffer, newAttributeBuffer, origAttributeBuffer.Length);
                mesh.D3dMesh.UnlockAttributeBuffer();
                d3dMesh.UnlockAttributeBuffer(newAttributeBuffer);
            }


            //Crear mesh de BumpMapping Mesh
            TgcMeshBumpMapping bumpMesh = new TgcMeshBumpMapping(d3dMesh, mesh.Name, mesh.RenderType);

            bumpMesh.diffuseMaps      = diffuseMaps;
            bumpMesh.materials        = materials;
            bumpMesh.normalMaps       = normalMaps;
            bumpMesh.layer            = mesh.Layer;
            bumpMesh.alphaBlendEnable = mesh.AlphaBlendEnable;
            bumpMesh.UserProperties   = mesh.UserProperties;
            bumpMesh.boundingBox      = mesh.BoundingBox.clone();
            bumpMesh.enabled          = true;

            return(bumpMesh);
        }
        /// <summary>
        /// Crear un TgcMeshBumpMapping en base a un TgcMesh y su normalMap.
        /// Solo esta soportado un TgcMehs MeshRenderType = DiffuseMap
        /// </summary>
        public static TgcMeshBumpMapping fromTgcMesh(TgcMesh mesh, TgcTexture[] normalMaps)
        {
            if (mesh.RenderType != MeshRenderType.DIFFUSE_MAP)
            {
                throw new Exception("Solo esta soportado MeshRenderType = DiffuseMap");
            }

            //Obtener vertexBuffer original
            TgcSceneLoader.DiffuseMapVertex[] origVertexBuffer = (TgcSceneLoader.DiffuseMapVertex[])mesh.D3dMesh.LockVertexBuffer(
                        typeof(TgcSceneLoader.DiffuseMapVertex), LockFlags.ReadOnly, mesh.D3dMesh.NumberVertices);
            mesh.D3dMesh.UnlockVertexBuffer();

            //Crear nuevo Mesh de DirectX
            int triCount = origVertexBuffer.Length / 3;
            Mesh d3dMesh = new Mesh(triCount, origVertexBuffer.Length, MeshFlags.Managed, BumpMappingVertexElements, GuiController.Instance.D3dDevice);

            //Calcular normales recorriendo los triangulos
            Vector3[] normals = new Vector3[origVertexBuffer.Length];
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i] = new Vector3(0, 0, 0);
            }
            for (int i = 0; i < triCount; i++)
            {
                //Los 3 vertices del triangulo
                TgcSceneLoader.DiffuseMapVertex v1 = origVertexBuffer[i * 3];
                TgcSceneLoader.DiffuseMapVertex v2 = origVertexBuffer[i * 3 + 1];
                TgcSceneLoader.DiffuseMapVertex v3 = origVertexBuffer[i * 3 + 2];

                //Face-normal (left-handend)
                Vector3 a = v2.Position - v1.Position;
                Vector3 b = v3.Position - v1.Position;
                Vector3 n = Vector3.Cross(a, b);

                //Acumular normal del vertice segun todas sus Face-normal
                normals[i * 3] += n;
                normals[i * 3 + 1] += n;
                normals[i * 3 + 2] += n;
            }

            //Normalizar normales
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i] = Vector3.Normalize(normals[i]);
            }

            //Crear nuevo VertexBuffer
            using (VertexBuffer vb = d3dMesh.VertexBuffer)
            {
                //Iterar sobre triangulos
                GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
                for (int i = 0; i < triCount; i++)
                {
                    //Vertices originales
                    TgcSceneLoader.DiffuseMapVertex vOrig1 = origVertexBuffer[i * 3];
                    TgcSceneLoader.DiffuseMapVertex vOrig2 = origVertexBuffer[i * 3 + 1];
                    TgcSceneLoader.DiffuseMapVertex vOrig3 = origVertexBuffer[i * 3 + 2];

                    //Nuevo vertice 1
                    BumpMappingVertex v1 = new BumpMappingVertex();
                    v1.Position = vOrig1.Position;
                    v1.Color = vOrig1.Color;
                    v1.Tu = vOrig1.Tu;
                    v1.Tv = vOrig1.Tv;
                    v1.Normal = normals[i * 3];

                    //Nuevo vertice 2
                    BumpMappingVertex v2 = new BumpMappingVertex();
                    v2.Position = vOrig2.Position;
                    v2.Color = vOrig2.Color;
                    v2.Tu = vOrig2.Tu;
                    v2.Tv = vOrig2.Tv;
                    v2.Normal = normals[i * 3 + 1];

                    //Nuevo vertice 3
                    BumpMappingVertex v3 = new BumpMappingVertex();
                    v3.Position = vOrig3.Position;
                    v3.Color = vOrig3.Color;
                    v3.Tu = vOrig3.Tu;
                    v3.Tv = vOrig3.Tv;
                    v3.Normal = normals[i * 3 + 2];

                    //Calcular tangente y binormal para todo el triangulo y cargarlas en cada vertice
                    Vector3 tangent;
                    Vector3 binormal;
                    TgcMeshBumpMapping.computeTangentBinormal(v1, v2, v3, out tangent, out binormal);
                    v1.Tangent = tangent;
                    v1.Binormal = binormal;
                    v2.Tangent = tangent;
                    v2.Binormal = binormal;
                    v3.Tangent = tangent;
                    v3.Binormal = binormal;

                    //Cargar VertexBuffer
                    data.Write(v1);
                    data.Write(v2);
                    data.Write(v3);
                }

                vb.Unlock();
            }

            //Cargar IndexBuffer en forma plana
            using (IndexBuffer ib = d3dMesh.IndexBuffer)
            {
                short[] indices = new short[origVertexBuffer.Length];
                for (int i = 0; i < indices.Length; i++)
                {
                    indices[i] = (short)i;
                }
                ib.SetData(indices, 0, LockFlags.None);
            }

            //Clonar texturas y materials
            TgcTexture[] diffuseMaps = new TgcTexture[mesh.DiffuseMaps.Length];
            Material[] materials = new Material[mesh.Materials.Length];
            for (int i = 0; i < mesh.DiffuseMaps.Length; i++)
            {
                diffuseMaps[i] = mesh.DiffuseMaps[i].clone();
                materials[i] = TgcD3dDevice.DEFAULT_MATERIAL;
            }

            //Cargar attributeBuffer
            if (diffuseMaps.Length > 1)
            {
                int[] origAttributeBuffer = mesh.D3dMesh.LockAttributeBufferArray(LockFlags.None);
                int[] newAttributeBuffer = d3dMesh.LockAttributeBufferArray(LockFlags.None);
                Array.Copy(origAttributeBuffer, newAttributeBuffer, origAttributeBuffer.Length);
                mesh.D3dMesh.UnlockAttributeBuffer();
                d3dMesh.UnlockAttributeBuffer(newAttributeBuffer);
            }

            //Crear mesh de BumpMapping Mesh
            TgcMeshBumpMapping bumpMesh = new TgcMeshBumpMapping(d3dMesh, mesh.Name, mesh.RenderType);
            bumpMesh.diffuseMaps = diffuseMaps;
            bumpMesh.materials = materials;
            bumpMesh.normalMaps = normalMaps;
            bumpMesh.layer = mesh.Layer;
            bumpMesh.alphaBlendEnable = mesh.AlphaBlendEnable;
            bumpMesh.UserProperties = mesh.UserProperties;
            bumpMesh.boundingBox = mesh.BoundingBox.clone();
            bumpMesh.enabled = true;

            return bumpMesh;
        }
示例#4
0
        public override void init()
        {
            Device d3dDevice = GuiController.Instance.D3dDevice;


            //Cargar textura de CubeMap para Environment Map, fijo para todos los meshes
            cubeMap = TextureLoader.FromCubeFile(d3dDevice, GuiController.Instance.ExamplesMediaDir + "Shaders\\CubeMap.dds");

            //Cargar Shader personalizado de EnvironmentMap
            effect = TgcShaders.loadEffect(GuiController.Instance.ExamplesMediaDir + "Shaders\\EnvironmentMap_Integrador2.fx");


            //Cargar escenario, pero inicialmente solo hacemos el parser, para separar los objetos que son solo luces y no meshes
            string         scenePath = GuiController.Instance.ExamplesDir + "Lights\\NormalMapRoom\\NormalMapRoom-TgcScene.xml";
            string         mediaPath = GuiController.Instance.ExamplesDir + "Lights\\NormalMapRoom\\";
            TgcSceneParser parser    = new TgcSceneParser();
            TgcSceneData   sceneData = parser.parseSceneFromString(File.ReadAllText(scenePath));

            //Separar modelos reales de las luces, segun layer "Lights"
            lights = new List <LightData>();
            List <TgcMeshData> realMeshData = new List <TgcMeshData>();

            for (int i = 0; i < sceneData.meshesData.Length; i++)
            {
                TgcMeshData meshData = sceneData.meshesData[i];

                //Es una luz, no cargar mesh, solo importan sus datos
                if (meshData.layerName == "Lights")
                {
                    //Guardar datos de luz
                    LightData light = new LightData();
                    light.color = Color.FromArgb((int)meshData.color[0], (int)meshData.color[1], (int)meshData.color[2]);
                    light.aabb  = new TgcBoundingBox(TgcParserUtils.float3ArrayToVector3(meshData.pMin), TgcParserUtils.float3ArrayToVector3(meshData.pMax));
                    light.pos   = light.aabb.calculateBoxCenter();
                    lights.Add(light);
                }
                //Es un mesh real, agregar a array definitivo
                else
                {
                    realMeshData.Add(meshData);
                }
            }

            //Reemplazar array original de meshData de sceneData por el definitivo
            sceneData.meshesData = realMeshData.ToArray();

            //Ahora si cargar meshes reales
            TgcSceneLoader loader = new TgcSceneLoader();
            TgcScene       scene  = loader.loadScene(sceneData, mediaPath);

            //Separar meshes con bumpMapping de los comunes
            List <TgcMeshBumpMapping> bumpMeshes = new List <TgcMeshBumpMapping>();

            commonMeshes = new List <TgcMesh>();
            foreach (TgcMesh mesh in scene.Meshes)
            {
                //Mesh con BumpMapping
                if (mesh.Layer == "BumpMap")
                {
                    //Por convencion de este ejemplo el NormalMap se llama igual que el DiffuseMap (y cada mesh tiene una sola)
                    string   path  = mesh.DiffuseMaps[0].FilePath;
                    string[] split = path.Split('.');
                    path = split[0] + "_NormalMap.png";

                    //Convertir TgcMesh a TgcMeshBumpMapping
                    TgcTexture         normalMap      = TgcTexture.createTexture(path);
                    TgcTexture[]       normalMapArray = new TgcTexture[] { normalMap };
                    TgcMeshBumpMapping bumpMesh       = TgcMeshBumpMapping.fromTgcMesh(mesh, normalMapArray);
                    bumpMesh.Effect    = effect;
                    bumpMesh.Technique = "ThreeLightsTechnique";
                    bumpMeshes.Add(bumpMesh);

                    //Liberar original
                    mesh.dispose();
                }
                //Mesh normal
                else
                {
                    commonMeshes.Add(mesh);
                }
            }


            //Pre-calculamos las 3 luces mas cercanas de cada mesh
            meshesWithLight = new List <MeshLightData>();
            foreach (TgcMeshBumpMapping mesh in bumpMeshes)
            {
                MeshLightData meshData = new MeshLightData();
                meshData.mesh = mesh;
                Vector3 meshCeter = mesh.BoundingBox.calculateBoxCenter();
                meshData.lights[0] = getClosestLight(meshCeter, null, null);
                meshData.lights[1] = getClosestLight(meshCeter, meshData.lights[0], null);
                meshData.lights[2] = getClosestLight(meshCeter, meshData.lights[0], meshData.lights[1]);
                meshesWithLight.Add(meshData);
            }



            //Camara en 1ra persona
            GuiController.Instance.FpsCamera.Enable = true;
            GuiController.Instance.FpsCamera.setCamera(new Vector3(0, 50, 100), new Vector3(0, 50, -1));



            //Modifiers
            GuiController.Instance.Modifiers.addBoolean("lightEnable", "lightEnable", true);
            GuiController.Instance.Modifiers.addFloat("reflection", 0, 1, 0.2f);
            GuiController.Instance.Modifiers.addFloat("bumpiness", 0, 2, 1f);
            GuiController.Instance.Modifiers.addFloat("lightIntensity", 0, 150, 20);
            GuiController.Instance.Modifiers.addFloat("lightAttenuation", 0.1f, 2, 0.3f);
            GuiController.Instance.Modifiers.addFloat("specularEx", 0, 20, 9f);

            GuiController.Instance.Modifiers.addColor("mEmissive", Color.Black);
            GuiController.Instance.Modifiers.addColor("mAmbient", Color.White);
            GuiController.Instance.Modifiers.addColor("mDiffuse", Color.White);
            GuiController.Instance.Modifiers.addColor("mSpecular", Color.White);
        }
示例#5
0
        public override void render(float elapsedTime)
        {
            Device device = GuiController.Instance.D3dDevice;

            //Habilitar luz
            bool   lightEnable = (bool)GuiController.Instance.Modifiers["lightEnable"];
            Effect currentShader;
            String currentTechnique;

            if (lightEnable)
            {
                //Shader personalizado de iluminacion
                currentShader    = this.effect;
                currentTechnique = "ThreeLightsTechnique";
            }
            else
            {
                //Sin luz: Restaurar shader default
                currentShader    = GuiController.Instance.Shaders.TgcMeshShader;
                currentTechnique = GuiController.Instance.Shaders.getTgcMeshTechnique(TgcMesh.MeshRenderType.DIFFUSE_MAP);
            }

            //Aplicar a cada mesh el shader actual
            foreach (MeshLightData meshData in meshesWithLight)
            {
                meshData.mesh.Effect    = currentShader;
                meshData.mesh.Technique = currentTechnique;
            }


            Vector3 eyePosition = GuiController.Instance.FpsCamera.getPosition();

            //Renderizar meshes con BumpMapping
            foreach (MeshLightData meshData in meshesWithLight)
            {
                TgcMeshBumpMapping mesh = meshData.mesh;

                if (lightEnable)
                {
                    mesh.Effect.SetValue("eyePosition", TgcParserUtils.vector3ToFloat4Array(eyePosition));
                    mesh.Effect.SetValue("bumpiness", (float)GuiController.Instance.Modifiers["bumpiness"]);
                    mesh.Effect.SetValue("reflection", (float)GuiController.Instance.Modifiers["reflection"]);

                    //Cargar variables de shader del Material
                    mesh.Effect.SetValue("materialEmissiveColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mEmissive"]));
                    mesh.Effect.SetValue("materialAmbientColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mAmbient"]));
                    mesh.Effect.SetValue("materialDiffuseColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mDiffuse"]));
                    mesh.Effect.SetValue("materialSpecularColor", ColorValue.FromColor((Color)GuiController.Instance.Modifiers["mSpecular"]));
                    mesh.Effect.SetValue("materialSpecularExp", (float)GuiController.Instance.Modifiers["specularEx"]);

                    //CubeMap
                    mesh.Effect.SetValue("texCubeMap", cubeMap);

                    //Cargar variables de shader de las 3 luces
                    //Intensidad y atenuacion deberian ser atributos propios de cada luz
                    float lightIntensity   = (float)GuiController.Instance.Modifiers["lightIntensity"];
                    float lightAttenuation = (float)GuiController.Instance.Modifiers["lightAttenuation"];
                    mesh.Effect.SetValue("lightIntensity", new float[] { lightIntensity, lightIntensity, lightIntensity });
                    mesh.Effect.SetValue("lightAttenuation", new float[] { lightAttenuation, lightAttenuation, lightAttenuation });

                    mesh.Effect.SetValue("lightColor", new ColorValue[] { ColorValue.FromColor(meshData.lights[0].color), ColorValue.FromColor(meshData.lights[1].color), ColorValue.FromColor(meshData.lights[2].color) });
                    mesh.Effect.SetValue("lightPosition", new Vector4[] { TgcParserUtils.vector3ToVector4(meshData.lights[0].pos), TgcParserUtils.vector3ToVector4(meshData.lights[1].pos), TgcParserUtils.vector3ToVector4(meshData.lights[2].pos) });
                }


                //Renderizar modelo
                mesh.render();
            }

            //Renderizar meshes comunes
            foreach (TgcMesh mesh in commonMeshes)
            {
                mesh.render();
            }
        }
示例#6
0
        /// <summary>
        /// Crear flechas de debug para normales, tangentes y binormales
        /// </summary>
        private void loadDebugArrows(TgcMeshBumpMapping mesh)
        {
            //Obtener vertexBuffer
            TgcMeshBumpMapping.BumpMappingVertex[] vertexBuffer = (TgcMeshBumpMapping.BumpMappingVertex[])mesh.D3dMesh.LockVertexBuffer(
                        typeof(TgcMeshBumpMapping.BumpMappingVertex), LockFlags.ReadOnly, mesh.D3dMesh.NumberVertices);
            mesh.D3dMesh.UnlockVertexBuffer();

            for (int i = 0; i < vertexBuffer.Length; i++)
            {
                TgcMeshBumpMapping.BumpMappingVertex v = vertexBuffer[i];
                normals.Add(TgcArrow.fromDirection(v.Position, v.Normal * 50, Color.Blue, Color.Yellow, 0.5f, new Vector2(2f, 4f)));
                tangents.Add(TgcArrow.fromDirection(v.Position, v.Tangent * 50, Color.Red, Color.Yellow, 0.5f, new Vector2(2f, 4f)));
                binormals.Add(TgcArrow.fromDirection(v.Position, v.Binormal * 50, Color.Green, Color.Yellow, 0.5f, new Vector2(2f, 4f)));
            }

        }
        public override void init()
        {
            Device d3dDevice = GuiController.Instance.D3dDevice;


            //Cargar textura de CubeMap para Environment Map
            cubeMap = TextureLoader.FromCubeFile(d3dDevice, GuiController.Instance.ExamplesMediaDir + "Shaders\\CubeMap.dds");


            //Crear 3 paredes y un piso con textura comun y textura de normalMap
            TgcTexture diffuseMap = TgcTexture.createTexture(GuiController.Instance.ExamplesMediaDir + "Shaders\\BumpMapping_DiffuseMap.jpg");
            TgcTexture normalMap  = TgcTexture.createTexture(GuiController.Instance.ExamplesMediaDir + "Shaders\\BumpMapping_NormalMap.jpg");

            TgcTexture[] normalMapArray = new TgcTexture[] { normalMap };

            TgcBox paredSur   = TgcBox.fromExtremes(new Vector3(-200, 0, -210), new Vector3(200, 100, -200), diffuseMap);
            TgcBox paredOeste = TgcBox.fromExtremes(new Vector3(-210, 0, -200), new Vector3(-200, 100, 200), diffuseMap);
            TgcBox paredEste  = TgcBox.fromExtremes(new Vector3(200, 0, -200), new Vector3(210, 100, 200), diffuseMap);
            TgcBox piso       = TgcBox.fromExtremes(new Vector3(-200, -1, -200), new Vector3(200, 0, 200), diffuseMap);

            //Convertir TgcBox a TgcMesh
            TgcMesh m1 = paredSur.toMesh("paredSur");
            TgcMesh m2 = paredOeste.toMesh("paredOeste");
            TgcMesh m3 = paredEste.toMesh("paredEste");
            TgcMesh m4 = piso.toMesh("piso");

            //Convertir TgcMesh a TgcMeshBumpMapping (se usa solo por conveniencia, pero el NormalMap de TgcMeshBumpMapping es innecesario para este ejemplo)
            meshes = new List <TgcMeshBumpMapping>();
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m1, normalMapArray));
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m2, normalMapArray));
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m3, normalMapArray));
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m4, normalMapArray));


            //Borrar TgcMesh y TgcBox, ya no se usan
            paredSur.dispose();
            paredOeste.dispose();
            paredEste.dispose();
            piso.dispose();
            m1.dispose();
            m2.dispose();
            m3.dispose();
            m4.dispose();



            //Camara en 1ra persona
            GuiController.Instance.FpsCamera.Enable = true;
            GuiController.Instance.FpsCamera.setCamera(new Vector3(0, 50, 100), new Vector3(0, 50, -1));

            //Cargar Shader de DynamicLights
            effect           = TgcShaders.loadEffect(GuiController.Instance.ExamplesMediaDir + "Shaders\\EnvironmentMap.fx");
            effect.Technique = "SimpleEnvironmentMapTechnique";

            //Cargar shader en meshes
            foreach (TgcMeshBumpMapping m in meshes)
            {
                m.Effect    = effect;
                m.Technique = "SimpleEnvironmentMapTechnique";
            }

            //Mesh para la luz
            lightMesh = TgcBox.fromSize(new Vector3(10, 10, 10), Color.Red);
            GuiController.Instance.Modifiers.addFloat("reflection", 0, 1, 0.35f);
            GuiController.Instance.Modifiers.addVertex3f("lightPos", new Vector3(-200, 0, -200), new Vector3(200, 100, 200), new Vector3(0, 80, 0));
            GuiController.Instance.Modifiers.addColor("lightColor", Color.White);
            GuiController.Instance.Modifiers.addFloat("lightIntensity", 0, 150, 20);
            GuiController.Instance.Modifiers.addFloat("lightAttenuation", 0.1f, 2, 0.3f);
            GuiController.Instance.Modifiers.addFloat("specularEx", 0, 20, 9f);


            GuiController.Instance.Modifiers.addColor("mEmissive", Color.Black);
            GuiController.Instance.Modifiers.addColor("mAmbient", Color.White);
            GuiController.Instance.Modifiers.addColor("mDiffuse", Color.White);
            GuiController.Instance.Modifiers.addColor("mSpecular", Color.White);
        }
示例#8
0
        public override void init()
        {
            Device d3dDevice = GuiController.Instance.D3dDevice;


            //DEBUG: para probar codigo que genera un NormalMap automaticamente. Queda bastante peor que el NormalMap que ya viene hecho
            //createNormalMap(GuiController.Instance.ExamplesMediaDir + "Shaders\\BumpMapping_DiffuseMap.jpg", GuiController.Instance.ExamplesMediaDir + "Shaders\\NormalMap_Prueba.jpg");
            //TgcTexture normalMap = TgcTexture.createTexture(GuiController.Instance.ExamplesMediaDir + "Shaders\\NormalMap_Prueba2.jpg");



            //Crear 3 paredes y un piso con textura comun y textura de normalMap
            TgcTexture diffuseMap = TgcTexture.createTexture(GuiController.Instance.ExamplesMediaDir + "Shaders\\BumpMapping_DiffuseMap.jpg");
            TgcTexture normalMap  = TgcTexture.createTexture(GuiController.Instance.ExamplesMediaDir + "Shaders\\BumpMapping_NormalMap.jpg");

            TgcTexture[] normalMapArray = new TgcTexture[] { normalMap };

            TgcBox paredSur   = TgcBox.fromExtremes(new Vector3(-200, 0, -210), new Vector3(200, 100, -200), diffuseMap);
            TgcBox paredOeste = TgcBox.fromExtremes(new Vector3(-210, 0, -200), new Vector3(-200, 100, 200), diffuseMap);
            TgcBox paredEste  = TgcBox.fromExtremes(new Vector3(200, 0, -200), new Vector3(210, 100, 200), diffuseMap);
            TgcBox piso       = TgcBox.fromExtremes(new Vector3(-200, -1, -200), new Vector3(200, 0, 200), diffuseMap);

            //Convertir TgcBox a TgcMesh
            TgcMesh m1 = paredSur.toMesh("paredSur");
            TgcMesh m2 = paredOeste.toMesh("paredOeste");
            TgcMesh m3 = paredEste.toMesh("paredEste");
            TgcMesh m4 = piso.toMesh("piso");

            //Convertir TgcMesh a TgcMeshBumpMapping
            meshes = new List <TgcMeshBumpMapping>();
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m1, normalMapArray));
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m2, normalMapArray));
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m3, normalMapArray));
            meshes.Add(TgcMeshBumpMapping.fromTgcMesh(m4, normalMapArray));


            //Borrar TgcMesh y TgcBox, ya no se usan
            paredSur.dispose();
            paredOeste.dispose();
            paredEste.dispose();
            piso.dispose();
            m1.dispose();
            m2.dispose();
            m3.dispose();
            m4.dispose();


            //Crear flechas de debug
            tangents  = new List <TgcArrow>();
            normals   = new List <TgcArrow>();
            binormals = new List <TgcArrow>();
            foreach (TgcMeshBumpMapping mesh in meshes)
            {
                loadDebugArrows(mesh);
            }



            //Camara en 1ra persona
            GuiController.Instance.FpsCamera.Enable = true;
            GuiController.Instance.FpsCamera.setCamera(new Vector3(0, 50, 100), new Vector3(0, 50, -1));

            //Cargar Shader de personalizado de BumpMapping. Solo soporta meshes de tipo DiffuseMap
            effect = TgcShaders.loadEffect(GuiController.Instance.ExamplesMediaDir + "Shaders\\BumpMapping.fx");

            //Cargar shader en meshes
            foreach (TgcMeshBumpMapping m in meshes)
            {
                m.Effect    = effect;
                m.Technique = "BumpMappingTechnique";
            }

            //Mesh para la luz
            lightMesh = TgcBox.fromSize(new Vector3(10, 10, 10), Color.Red);
            GuiController.Instance.Modifiers.addFloat("bumpiness", 0, 1, 1f);
            GuiController.Instance.Modifiers.addVertex3f("lightPos", new Vector3(-200, 0, -200), new Vector3(200, 100, 200), new Vector3(0, 80, 0));
            GuiController.Instance.Modifiers.addColor("lightColor", Color.White);
            GuiController.Instance.Modifiers.addFloat("lightIntensity", 0, 150, 20);
            GuiController.Instance.Modifiers.addFloat("lightAttenuation", 0.1f, 2, 0.3f);
            GuiController.Instance.Modifiers.addFloat("specularEx", 0, 20, 9f);
            GuiController.Instance.Modifiers.addBoolean("showNormals", "showNormals", false);
            GuiController.Instance.Modifiers.addBoolean("showTangents", "showTangents", false);
            GuiController.Instance.Modifiers.addBoolean("showBinormals", "showBinormals", false);


            GuiController.Instance.Modifiers.addColor("mEmissive", Color.Black);
            GuiController.Instance.Modifiers.addColor("mAmbient", Color.White);
            GuiController.Instance.Modifiers.addColor("mDiffuse", Color.White);
            GuiController.Instance.Modifiers.addColor("mSpecular", Color.White);
        }