Пример #1
0
        public static Model FromScene(Scene scene, Device device)
        {
            VertexDeclaration vertexDeclaration = new VertexDeclaration(device,
                VertexPositionNormalTexture.VertexElements);
            Model result = new Model(scene, device, vertexDeclaration);
            foreach (Mesh mesh in scene.Meshes)
            {
                VertexBuffer vertexBuffer = new VertexBuffer(device,
                    mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes,
                    Usage.WriteOnly, VertexFormat.None, Pool.Default);
                DataStream vertexDataStream = vertexBuffer.Lock(0,
                    mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes,
                    LockFlags.None);
                VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.Positions.Count];
                for (int i = 0; i < vertices.Length; ++i)
                    vertices[i] = new VertexPositionNormalTexture(mesh.Positions[i], (mesh.Normals.Count > i) ? mesh.Normals[i] : Vector3D.Zero, Point2D.Zero);
                vertexDataStream.WriteRange(vertices);
                vertexBuffer.Unlock();

                IndexBuffer indexBuffer = new IndexBuffer(device, mesh.Indices.Count * sizeof(int),
                    Usage.WriteOnly, Pool.Default, false);
                DataStream indexDataStream = indexBuffer.Lock(0, mesh.Indices.Count * sizeof(int), LockFlags.None);
                indexDataStream.WriteRange(mesh.Indices.ToArray());
                indexBuffer.Unlock();

                ModelMesh modelMesh = new ModelMesh(mesh, device, vertexBuffer,
                    mesh.Positions.Count, indexBuffer, mesh.PrimitiveCount,
                    Matrix3D.Identity, mesh.Material);
                result.Meshes.Add(modelMesh);
            }
            return result;
        }
 private Model GetModel()
 {
     if (_model == null)
     {
         ModelEditorViewModel vm = (ModelEditorViewModel) DataContext;
         _model = ModelConverter.FromScene(vm.Scene.Scene, _graphicsDeviceService.Device);
     }
     return _model;
 }
Пример #3
0
        public Renderer(Device device, Model model, int width, int height, Transform3D cameraTransform)
        {
            _device = device;
            _model = model;
            _cameraTransform = cameraTransform;

            const float fov = MathUtility.PI_OVER_4;

            AxisAlignedBox3D bounds = _model.SourceScene.Bounds;
            Vector3D max = bounds.Size;
            float radius = System.Math.Max(max.X, System.Math.Max(max.Y, max.Z));

            _projection = Matrix3D.CreatePerspectiveFieldOfView(
                fov,
                width / (float) height,
                1.0f, radius * 10);

            float dist = radius / MathUtility.Sin(fov / 2);

            _view = Matrix3D.CreateLookAt(
                bounds.Center + Vector3D.Backward * dist,
                Vector3D.Forward,
                Vector3D.Up);
        }