예제 #1
0
        public void Run()
        {
            m_cam = new Camera();

            WindowCreateInfo      windowInfo = new WindowCreateInfo(100, 100, 640, 480, WindowState.Normal, "Quack! - AssimpNet Sample");
            GraphicsDeviceOptions options    = new GraphicsDeviceOptions(false, PixelFormat.B8_G8_R8_A8_UNorm, true, ResourceBindingModel.Default, true, true);

            options.SwapchainDepthFormat = PixelFormat.D24_UNorm_S8_UInt;

            m_window = VeldridStartup.CreateWindow(ref windowInfo);

            //If on windows...use D3D11...if not...use OpenGL. Vulkan did not seem to play nice and haven't tested Metal
            m_graphicsDevice = VeldridStartup.CreateGraphicsDevice(m_window, options, IsWindows() ? GraphicsBackend.Direct3D11 : GraphicsBackend.OpenGL);

            m_cmdList = m_graphicsDevice.ResourceFactory.CreateCommandList();

            //NOTICE: This is the duck model we load for the sample, replace this line with a path to your own model to see it imported!
            String fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Assets", "duck.dae");

            //Veldrid defaults to Clockwise winding order, assimp defaults to CCW. We also do some processing + generate normals if missing.
            SimpleModel model = SimpleModel.LoadFromFile(fileName, m_graphicsDevice, PostProcessPreset.TargetRealTimeQuality | PostProcessSteps.FlipWindingOrder,
                                                         new NormalSmoothingAngleConfig(66f));

            if (model == null)
            {
                return;
            }

            m_targetModel = model;
            SetupCamera(m_targetModel);
            m_window.Resized += Window_Resized;

            RenderLoop();

            m_graphicsDevice.WaitForIdle();

            m_targetModel.Dispose();
            m_graphicsDevice.Dispose();
        }
예제 #2
0
        public static SimpleModel LoadFromFile(String filePath, GraphicsDevice gd, PostProcessSteps ppSteps, params PropertyConfig[] configs)
        {
            if (!File.Exists(filePath) || gd == null)
            {
                return(null);
            }

            AssimpContext importer = new AssimpContext();

            if (configs != null)
            {
                foreach (PropertyConfig config in configs)
                {
                    importer.SetConfig(config);
                }
            }

            Scene scene = importer.ImportFile(filePath, ppSteps);

            if (scene == null)
            {
                return(null);
            }

            SimpleModel model = new SimpleModel();

            model.WorldMatrix = SN.Matrix4x4.Identity;
            if (!model.CreateVertexBuffer(scene, gd, Path.GetDirectoryName(filePath)))
            {
                return(null);
            }

            model.ComputeBoundingBox(scene);
            model.AdjustModelScale();

            return(model);
        }
예제 #3
0
        private void SetupCamera(SimpleModel modelToLookAt)
        {
            float aspectRatio = (float)m_window.Width / (float)m_window.Height;

            //Using the imported scene's bounding volume, we place the camera at the maximum point looking down at the scene center. The clip planes are sized so it fits the scene completely.
            //The model gets scaled down when its imported so we shouldn't run into precision issues with the clip planes being too wide apart.

            SN.Vector3 min      = modelToLookAt.SceneMin;
            SN.Vector3 max      = modelToLookAt.SceneMax;
            SN.Vector3 center   = modelToLookAt.SceneCenter;
            float      diagonal = SN.Vector3.Distance(center, max) * 3.5f;

            SN.Vector3 dir    = max - center;
            SN.Vector3 camPos = SN.Vector3.Normalize(dir) * diagonal;

            diagonal = SN.Vector3.Distance(camPos, min);

            SN.Matrix4x4 proj = SN.Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 4, aspectRatio, 0.5f, diagonal);
            SN.Matrix4x4 view = SN.Matrix4x4.CreateLookAt(camPos, center, SN.Vector3.UnitY);

            m_cam.ViewProjection        = view * proj;
            m_cam.Position              = camPos;
            m_targetModel.LightPosition = camPos;
        }