Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DriverType driverType;

            if (!AskUserForDriver(out driverType))
            {
                return;
            }

            useHighLevelShaders = AskUserForHighLevelShaders(driverType);

            if (useHighLevelShaders)
            {
                useCgShaders = AskUserForCgShaders(driverType);
            }

            device = IrrlichtDevice.CreateDevice(driverType, new Dimension2Di(640, 480));
            if (device == null)
            {
                return;
            }

            VideoDriver  driver = device.VideoDriver;
            SceneManager smgr   = device.SceneManager;

            string vsFileName = null;             // filename for the vertex shader
            string psFileName = null;             // filename for the pixel shader

            switch (driverType)
            {
            case DriverType.Direct3D8:
                psFileName = "../../media/d3d8.psh";
                vsFileName = "../../media/d3d8.vsh";
                break;

            case DriverType.Direct3D9:
                if (useHighLevelShaders)
                {
                    // Cg can also handle this syntax
                    psFileName = "../../media/d3d9.hlsl";
                    vsFileName = psFileName;                             // both shaders are in the same file
                }
                else
                {
                    psFileName = "../../media/d3d9.psh";
                    vsFileName = "../../media/d3d9.vsh";
                }
                break;

            case DriverType.OpenGL:
                if (useHighLevelShaders)
                {
                    if (useCgShaders)
                    {
                        // Use HLSL syntax for Cg
                        psFileName = "../../media/d3d9.hlsl";
                        vsFileName = psFileName;                                 // both shaders are in the same file
                    }
                    else
                    {
                        psFileName = "../../media/opengl.frag";
                        vsFileName = "../../media/opengl.vert";
                    }
                }
                else
                {
                    psFileName = "../../media/opengl.psh";
                    vsFileName = "../../media/opengl.vsh";
                }
                break;
            }

            if (!driver.QueryFeature(VideoDriverFeature.PixelShader_1_1) &&
                !driver.QueryFeature(VideoDriverFeature.ARB_FragmentProgram_1))
            {
                device.Logger.Log("WARNING: Pixel shaders disabled because of missing driver/hardware support.");
            }

            if (!driver.QueryFeature(VideoDriverFeature.VertexShader_1_1) &&
                !driver.QueryFeature(VideoDriverFeature.ARB_VertexProgram_1))
            {
                device.Logger.Log("WARNING: Vertex shaders disabled because of missing driver/hardware support.");
            }

            // create materials

            GPUProgrammingServices gpu = driver.GPUProgrammingServices;
            MaterialType           newMaterialType1 = MaterialType.Solid;
            MaterialType           newMaterialType2 = MaterialType.TransparentAddColor;

            if (gpu != null)
            {
                gpu.OnSetConstants += new GPUProgrammingServices.SetConstantsHandler(gpu_OnSetConstants);

                // create the shaders depending on if the user wanted high level or low level shaders

                if (useHighLevelShaders)
                {
                    GPUShadingLanguage shadingLanguage = useCgShaders
                                                ? GPUShadingLanguage.Cg
                                                : GPUShadingLanguage.Default;

                    newMaterialType1 = gpu.AddHighLevelShaderMaterialFromFiles(
                        vsFileName, "vertexMain", VertexShaderType.VS_1_1,
                        psFileName, "pixelMain", PixelShaderType.PS_1_1,
                        MaterialType.Solid, 0, shadingLanguage);

                    newMaterialType2 = gpu.AddHighLevelShaderMaterialFromFiles(
                        vsFileName, "vertexMain", VertexShaderType.VS_1_1,
                        psFileName, "pixelMain", PixelShaderType.PS_1_1,
                        MaterialType.TransparentAddColor, 0, shadingLanguage);
                }
                else
                {
                    // create material from low level shaders (asm or arb_asm)

                    newMaterialType1 = gpu.AddShaderMaterialFromFiles(vsFileName,
                                                                      psFileName, MaterialType.Solid);

                    newMaterialType2 = gpu.AddShaderMaterialFromFiles(vsFileName,
                                                                      psFileName, MaterialType.TransparentAddColor);
                }
            }

            if ((int)newMaterialType1 == -1)
            {
                newMaterialType1 = MaterialType.Solid;
            }

            if ((int)newMaterialType2 == -1)
            {
                newMaterialType2 = MaterialType.TransparentAddColor;
            }

            // create test scene node 1, with the new created material type 1

            SceneNode node = smgr.AddCubeSceneNode(50);

            node.Position = new Vector3Df(0);
            node.SetMaterialTexture(0, driver.GetTexture("../../media/wall.bmp"));
            node.SetMaterialFlag(MaterialFlag.Lighting, false);
            node.SetMaterialType(newMaterialType1);

            smgr.AddTextSceneNode(device.GUIEnvironment.BuiltInFont, "PS & VS & EMT_SOLID", new Color(255, 255, 255), node);

            SceneNodeAnimator anim = smgr.CreateRotationAnimator(new Vector3Df(0, 0.3f, 0));

            node.AddAnimator(anim);
            anim.Drop();

            // create test scene node 2, with the new created material type 2

            node          = smgr.AddCubeSceneNode(50);
            node.Position = new Vector3Df(0, -10, 50);
            node.SetMaterialTexture(0, driver.GetTexture("../../media/wall.bmp"));
            node.SetMaterialFlag(MaterialFlag.Lighting, false);
            node.SetMaterialFlag(MaterialFlag.BlendOperation, true);
            node.SetMaterialType(newMaterialType2);

            smgr.AddTextSceneNode(device.GUIEnvironment.BuiltInFont, "PS & VS & EMT_TRANSPARENT", new Color(255, 255, 255), node);

            anim = smgr.CreateRotationAnimator(new Vector3Df(0, 0.3f, 0));
            node.AddAnimator(anim);
            anim.Drop();

            // create test scene node 3, with no shader

            node          = smgr.AddCubeSceneNode(50);
            node.Position = new Vector3Df(0, 50, 25);
            node.SetMaterialTexture(0, driver.GetTexture("../../media/wall.bmp"));
            node.SetMaterialFlag(MaterialFlag.Lighting, false);

            smgr.AddTextSceneNode(device.GUIEnvironment.BuiltInFont, "NO SHADER", new Color(255, 255, 255), node);

            // add a nice skybox

            driver.SetTextureCreationFlag(TextureCreationFlag.CreateMipMaps, false);

            SceneNode skybox = smgr.AddSkyBoxSceneNode(
                "../../media/irrlicht2_up.jpg",
                "../../media/irrlicht2_dn.jpg",
                "../../media/irrlicht2_lf.jpg",
                "../../media/irrlicht2_rt.jpg",
                "../../media/irrlicht2_ft.jpg",
                "../../media/irrlicht2_bk.jpg");

            driver.SetTextureCreationFlag(TextureCreationFlag.CreateMipMaps, true);

            // add a camera and disable the mouse cursor

            CameraSceneNode cam = smgr.AddCameraSceneNodeFPS();

            cam.Position = new Vector3Df(-100, 50, 100);
            cam.Target   = new Vector3Df(0);

            device.CursorControl.Visible = false;

            // draw everything

            int lastFPS = -1;

            while (device.Run())
            {
                if (device.WindowActive)
                {
                    driver.BeginScene(true, true, new Color(0));
                    smgr.DrawAll();
                    driver.EndScene();

                    int fps = driver.FPS;
                    if (lastFPS != fps)
                    {
                        device.SetWindowCaption(String.Format(
                                                    "Vertex and pixel shader example - Irrlicht Engine [{0}] fps: {1}",
                                                    driver.Name, fps));

                        lastFPS = fps;
                    }
                }
            }

            device.Drop();
        }
Exemplo n.º 2
0
        static void Main()
        {
            DriverType?driverType = AskForDriver();

            if (!driverType.HasValue)
            {
                return;
            }

            IrrlichtDevice device = IrrlichtDevice.CreateDevice(driverType.Value, new Dimension2Di(640, 480));

            if (device == null)
            {
                return;
            }

            VideoDriver    driver = device.VideoDriver;
            SceneManager   smgr   = device.SceneManager;
            GUIEnvironment env    = device.GUIEnvironment;

            // load and display animated fairy mesh
            AnimatedMeshSceneNode fairy = smgr.AddAnimatedMeshSceneNode(smgr.GetMesh("../../media/faerie.md2"));

            if (fairy != null)
            {
                fairy.SetMaterialTexture(0, driver.GetTexture("../../media/faerie2.bmp")); // set diffuse texture
                fairy.SetMaterialFlag(MaterialFlag.Lighting, true);                        // enable dynamic lighting
                fairy.GetMaterial(0).Shininess = 20.0f;                                    // set size of specular highlights
                fairy.Position = new Vector3Df(-10, 0, -100);
                fairy.SetMD2Animation(AnimationTypeMD2.Stand);
            }

            // add white light
            smgr.AddLightSceneNode(null, new Vector3Df(-15, 5, -105), new Colorf(1, 1, 1));

            // set ambient light
            smgr.AmbientLight = new Colorf(0.25f, 0.25f, 0.25f);

            // add fps camera
            CameraSceneNode fpsCamera = smgr.AddCameraSceneNodeFPS();

            fpsCamera.Position = new Vector3Df(-50, 50, -150);

            // disable mouse cursor
            device.CursorControl.Visible = false;

            // create test cube
            SceneNode test = smgr.AddCubeSceneNode(60);

            // let the cube rotate and set some light settings
            SceneNodeAnimator anim = smgr.CreateRotationAnimator(new Vector3Df(0.3f, 0.3f, 0));

            test.Position = new Vector3Df(-100, 0, -100);
            test.SetMaterialFlag(MaterialFlag.Lighting, false);             // disable dynamic lighting
            test.AddAnimator(anim);
            anim.Drop();

            // create render target
            Texture         rt       = null;
            CameraSceneNode fixedCam = null;

            if (driver.QueryFeature(VideoDriverFeature.RenderToTarget))
            {
                rt = driver.AddRenderTargetTexture(new Dimension2Di(256), "RTT1");
                test.SetMaterialTexture(0, rt);                 // set material of cube to render target

                // add fixed camera
                fixedCam = smgr.AddCameraSceneNode(null, new Vector3Df(10, 10, -80), new Vector3Df(-10, 10, -100));
            }
            else
            {
                // create problem text
                GUIFont font = env.GetFont("../../media/fonthaettenschweiler.bmp");
                if (font != null)
                {
                    env.Skin.SetFont(font);
                }

                GUIStaticText text = env.AddStaticText(
                    "Your hardware or this renderer is not able to use the " +
                    "render to texture feature. RTT Disabled.",
                    new Recti(150, 20, 470, 60));

                text.OverrideColor = new Color(255, 255, 255, 100);
            }

            int lastFPS = -1;

            while (device.Run())
            {
                if (device.WindowActive)
                {
                    driver.BeginScene(ClearBufferFlag.All, new Color(0));

                    if (rt != null)
                    {
                        // draw scene into render target

                        // set render target texture
                        driver.SetRenderTarget(rt, ClearBufferFlag.All, new Color(0, 0, 255));

                        // make cube invisible and set fixed camera as active camera
                        test.Visible      = false;
                        smgr.ActiveCamera = fixedCam;

                        // draw whole scene into render buffer
                        smgr.DrawAll();

                        // set back old render target
                        // The buffer might have been distorted, so clear it
                        driver.SetRenderTarget(null, ClearBufferFlag.All, new Color(0));

                        // make the cube visible and set the user controlled camera as active one
                        test.Visible      = true;
                        smgr.ActiveCamera = fpsCamera;
                    }

                    // draw scene normally
                    smgr.DrawAll();
                    env.DrawAll();

                    driver.EndScene();

                    // display frames per second in window title
                    int fps = driver.FPS;
                    if (lastFPS != fps)
                    {
                        device.SetWindowCaption(String.Format(
                                                    "Render to Texture and Specular Highlights example - Irrlicht Engine [{0}] fps: {1}",
                                                    driver.Name, fps));

                        lastFPS = fps;
                    }
                }
            }

            device.Drop();
        }