Exemple #1
0
 public void Read(BinaryReader reader)
 {
     interpolation_type = reader.ReadUInt16();
     global_sequence    = reader.ReadUInt16();
     timestamps         = new M2Array(reader, 0);
     values             = new M2Array(reader, 0);
 }
Exemple #2
0
        // Number of cameras not used. Multiple cameras never used in 7.1.5
        static void readCamera(M2Camera cam, BinaryReader reader, CinematicCameraRecord dbcentry)
        {
            List <FlyByCamera> cameras   = new List <FlyByCamera>();
            List <FlyByCamera> targetcam = new List <FlyByCamera>();

            Vector4 dbcData = new Vector4(dbcentry.Origin.X, dbcentry.Origin.Y, dbcentry.Origin.Z, dbcentry.OriginFacing);

            // Read target locations, only so that we can calculate orientation
            for (uint k = 0; k < cam.target_positions.timestamps.number; ++k)
            {
                // Extract Target positions
                reader.BaseStream.Position = cam.target_positions.timestamps.offset_elements;
                M2Array targTsArray = reader.Read <M2Array>();

                reader.BaseStream.Position = targTsArray.offset_elements;
                uint[] targTimestamps = reader.ReadArray <uint>(targTsArray.number);

                reader.BaseStream.Position = cam.target_positions.values.offset_elements;
                M2Array targArray = reader.Read <M2Array>();

                reader.BaseStream.Position = targArray.offset_elements;
                M2SplineKey[] targPositions = new M2SplineKey[targArray.number];
                for (var i = 0; i < targArray.number; ++i)
                {
                    targPositions[i] = new M2SplineKey(reader);
                }

                // Read the data for this set
                for (uint i = 0; i < targTsArray.number; ++i)
                {
                    // Translate co-ordinates
                    Vector3 newPos = translateLocation(dbcData, cam.target_position_base, targPositions[i].p0);

                    // Add to vector
                    FlyByCamera thisCam = new FlyByCamera();
                    thisCam.timeStamp = targTimestamps[i];
                    thisCam.locations = new Vector4(newPos.X, newPos.Y, newPos.Z, 0.0f);
                    targetcam.Add(thisCam);
                }
            }

            // Read camera positions and timestamps (translating first position of 3 only, we don't need to translate the whole spline)
            for (uint k = 0; k < cam.positions.timestamps.number; ++k)
            {
                // Extract Camera positions for this set
                reader.BaseStream.Position = cam.positions.timestamps.offset_elements;
                M2Array posTsArray = reader.Read <M2Array>();

                reader.BaseStream.Position = posTsArray.offset_elements;
                uint[] posTimestamps = reader.ReadArray <uint>(posTsArray.number);

                reader.BaseStream.Position = cam.positions.values.offset_elements;
                M2Array posArray = reader.Read <M2Array>();

                reader.BaseStream.Position = posArray.offset_elements;
                M2SplineKey[] positions = new M2SplineKey[posTsArray.number];
                for (var i = 0; i < posTsArray.number; ++i)
                {
                    positions[i] = new M2SplineKey(reader);
                }

                // Read the data for this set
                for (uint i = 0; i < posTsArray.number; ++i)
                {
                    // Translate co-ordinates
                    Vector3 newPos = translateLocation(dbcData, cam.position_base, positions[i].p0);

                    // Add to vector
                    FlyByCamera thisCam = new FlyByCamera();
                    thisCam.timeStamp = posTimestamps[i];
                    thisCam.locations = new Vector4(newPos.X, newPos.Y, newPos.Z, 0);

                    if (targetcam.Count > 0)
                    {
                        // Find the target camera before and after this camera
                        // Pre-load first item
                        FlyByCamera lastTarget = targetcam[0];
                        FlyByCamera nextTarget = targetcam[0];
                        for (int j = 0; j < targetcam.Count; ++j)
                        {
                            nextTarget = targetcam[j];
                            if (targetcam[j].timeStamp > posTimestamps[i])
                            {
                                break;
                            }

                            lastTarget = targetcam[j];
                        }

                        float x = lastTarget.locations.X;
                        float y = lastTarget.locations.Y;
                        float z = lastTarget.locations.Z;

                        // Now, the timestamps for target cam and position can be different. So, if they differ we interpolate
                        if (lastTarget.timeStamp != posTimestamps[i])
                        {
                            uint  timeDiffTarget = nextTarget.timeStamp - lastTarget.timeStamp;
                            uint  timeDiffThis   = posTimestamps[i] - lastTarget.timeStamp;
                            float xDiff          = nextTarget.locations.X - lastTarget.locations.X;
                            float yDiff          = nextTarget.locations.Y - lastTarget.locations.Y;
                            float zDiff          = nextTarget.locations.Z - lastTarget.locations.Z;
                            x = lastTarget.locations.X + (xDiff * ((float)timeDiffThis / timeDiffTarget));
                            y = lastTarget.locations.Y + (yDiff * ((float)timeDiffThis / timeDiffTarget));
                            z = lastTarget.locations.Z + (zDiff * ((float)timeDiffThis / timeDiffTarget));
                        }
                        float xDiff1 = x - thisCam.locations.X;
                        float yDiff1 = y - thisCam.locations.Y;
                        thisCam.locations.W = (float)Math.Atan2(yDiff1, xDiff1);

                        if (thisCam.locations.W < 0)
                        {
                            thisCam.locations.W += 2 * MathFunctions.PI;
                        }
                    }

                    cameras.Add(thisCam);
                }
            }

            FlyByCameraStorage[dbcentry.Id] = cameras;
        }