Represents 4x4 mathematical matrix. Immutable.
Ejemplo n.º 1
0
        public void Setup()
        {
            // Set camera optics
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();

            CameraOpticsTechniqueCommonPerspective perspective = this.Optics.TechniqueCommon.Item as CameraOpticsTechniqueCommonPerspective;
            if (perspective != null) {
                double yFov, aspectRatio, zNear, zFar;
                perspective.GetCameraParameters(out yFov, out aspectRatio, out zNear, out zFar);
                if (aspectRatio < GlobalSettings.ViewportAspectRatio) {
                    yFov *= (GlobalSettings.ViewportAspectRatio / aspectRatio);
                }
                aspectRatio = GlobalSettings.ViewportAspectRatio;
                Glu.gluPerspective(yFov, 1 / aspectRatio, zNear, zFar);
            }

            CameraOpticsTechniqueCommonOrthographic orthographic = this.Optics.TechniqueCommon.Item as CameraOpticsTechniqueCommonOrthographic;
            if (orthographic != null) {
                double xMag, yMag, zNear, zFar;
                orthographic.GetCameraParameters(out xMag, out yMag, out zNear, out zFar);
                Gl.glOrtho(-xMag, xMag, -yMag, yMag, zNear, zFar);
            }

            // Set camera location and orientation
            double[] elements = new double[16];
            Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, elements);
            Math.Matrix modelView = new Math.Matrix(elements);

            Math.Vector cameraPosition = modelView.Multiply(Math.Vector.Origin);
            Math.Vector targetPosition = modelView.Multiply(new Math.Vector(0, 0, -1, 1));
            Math.Vector upDirection = modelView.Multiply(new Math.Vector(0, 1, 0, 0));

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Glu.gluLookAt(cameraPosition.X, cameraPosition.Y, cameraPosition.Z,
                          targetPosition.X, targetPosition.Y, targetPosition.Z,
                          upDirection.X, upDirection.Y, upDirection.Z);
        }
Ejemplo n.º 2
0
        public void Animate()
        {
            foreach(object item in this.Items) {
                Channel channel = item as Channel;
                if (channel != null) {
                    Sampler sampler = Sampler.IDs[channel.Source.Remove(0,1)];
                    float[] input = null;
                    float[] output = null;
                    float[] inTangent = null;
                    float[] outTangent = null;
                    string[] interpolation = null;
                    foreach(InputLocal samplerInput in sampler.Input) {
                        Source source = Source.IDs[samplerInput.Source.Remove(0,1)];
                        switch(samplerInput.Semantic) {
                            case "INPUT": input = ((FloatArray)source.Item).ValuesAsFloats; break;
                            case "OUTPUT": output = ((FloatArray)source.Item).ValuesAsFloats; break;
                            case "IN_TANGENT": inTangent = ((FloatArray)source.Item).ValuesAsFloats; break;
                            case "OUT_TANGENT": outTangent = ((FloatArray)source.Item).ValuesAsFloats; break;
                            case "INTERPOLATION": interpolation = ((NameArray)source.Item).Values; break;
                        }
                    }

                    float time = (float)GlobalSettings.RunningTime.TotalSeconds;
                    time = time % input[input.Length - 1];
                    int index = -1;
                    for(int i = 0; i < input.Length - 1; i++) {
                        if (input[i] <= time && time < input[i+1]) {
                            index = i;
                            break;
                        }
                    }
                    if (index != -1) {
                        // Bezier interpolation. See https://collada.org/public_forum/viewtopic.php?t=559
                        float s = (time - input[index]) / (input[index+1] - input[index]);
                        Math.Vector S = new Math.Vector(s*s*s, s*s, s, 1);
                        Math.Matrix M = new Math.Matrix(-1,  3, -3,  1,
                                                         3, -6,  3,  0,
                                                        -3,  3,  0,  0,
                                                         1,  0,  0,  0);
                        Math.Vector C = new Math.Vector(output[index],
                                                        output[index] + outTangent[index] / 3,
                                                        output[index+1] - inTangent[index+1] / 3,
                                                        output[index+1]);
                        double interpolatedValue = S * (M * C);

                        string[] target = channel.Target.Split(new char[] {'/', '.'});
                        string id = target[0];
                        string sid = target[1];
                        string property = target[2];
                        if (Node.IDs.ContainsKey(id)) {
                            Node node = Node.IDs[id];
                            foreach(object transform in node.Items) {
                                if (transform is Rotate && ((Rotate)transform).Sid == sid) {
                                    double[] values = ((Rotate)transform).Values;
                                    values[3] = interpolatedValue;
                                    ((Rotate)transform).Text = "";
                                    foreach(double val in values) {
                                        ((Rotate)transform).Text += val.ToString(System.Globalization.CultureInfo.InvariantCulture) + " ";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }