Esempio n. 1
0
        /**
         * If any changes have been made to the camera since the last time this function was called,
         * then commands for updating those values on the server will be added to the given command sequence.
         */
        public void UpdateCamera(IAddCommand seq)
        {
            if (CameraName == null || CameraName.Length == 0 ||
                CameraInstanceName == null || CameraInstanceName.Length == 0)
            {
                Logger.Log("error", "Cannot update camera until names have been set.");
                return;
            }

            if (changedValues.Count == 0)
            {
                return;
            }

            if (HasChanged("matrix"))
            {
                seq.AddCommand(new RSCommand("instance_set_world_to_obj",
                                             "instance_name", CameraInstanceName,
                                             "transform", TransformMatrix.GetMatrixForRS()
                                             ));
            }

            if (HasChanged("resX") || HasChanged("resY"))
            {
                seq.AddCommand(new RSCommand("camera_set_aspect",
                                             "camera_name", CameraName,
                                             "aspect", Aspect
                                             ));

                seq.AddCommand(new RSCommand("camera_set_resolution",
                                             "camera_name", CameraName,
                                             "resolution", new Hashtable()
                {
                    { "x", ResolutionX },
                    { "y", ResolutionY }
                }
                                             ));
            }

            if (HasChanged("ortho"))
            {
                seq.AddCommand(new RSCommand("camera_set_orthographic",
                                             "camera_name", CameraName,
                                             "orthographic", Orthographic
                                             ));
            }

            // As the aperture is used by both orthographic and perspective cameras
            // got slightly different things, we need to update the aperture if
            // the orthographic value has changed.
            if ((HasChanged("ortho") && Orthographic) || HasChanged("orthoSize"))
            {
                seq.AddCommand(new RSCommand("camera_set_aperture",
                                             "camera_name", CameraName,
                                             "aperture", OrthographicSize * 2.0f * Aspect
                                             ));
            }
            else if ((HasChanged("ortho") && !Orthographic) || HasChanged("fov"))
            {
                // As we currently do not load any camera values from the
                // server, we need to choose an aperture to work with.
                double aperture = 1.417;
                double deg2rad  = Math.PI / 180.0;
                double focal    = ((aperture / Aspect) / 2.0f)
                                  / Math.Tan(FieldOfView / 2.0f * deg2rad);
                seq.AddCommand(new RSCommand("camera_set_aperture",
                                             "camera_name", CameraName,
                                             "aperture", aperture
                                             ));
                seq.AddCommand(new RSCommand("camera_set_focal",
                                             "camera_name", CameraName,
                                             "focal", focal
                                             ));
            }

            changedValues.Clear();
            _hasNewChanges = false;
        }