VGearsCameraMatrixFileSerializer.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreLogManager.h>
  16. #include <OgreException.h>
  17. #include "data/VGearsCameraMatrixFileSerializer.h"
  18. namespace VGears{
  19. CameraMatrixFileSerializer::CameraMatrixFileSerializer() : Serializer(){}
  20. CameraMatrixFileSerializer::~CameraMatrixFileSerializer(){}
  21. void CameraMatrixFileSerializer::ImportCameraMatrixFile(
  22. Ogre::DataStreamPtr &stream, CameraMatrixFile* dest
  23. ){
  24. // Read matrix.
  25. sint16 matrix_sint16[CAMERA_MATRIX_ROW_COUNT][CAMERA_MATRIX_COL_COUNT];
  26. ReadInt16(stream, matrix_sint16[0][0]);
  27. ReadInt16(stream, matrix_sint16[1][0]);
  28. ReadInt16(stream, matrix_sint16[2][0]);
  29. ReadInt16(stream, matrix_sint16[0][1]);
  30. ReadInt16(stream, matrix_sint16[1][1]);
  31. ReadInt16(stream, matrix_sint16[2][1]);
  32. ReadInt16(stream, matrix_sint16[0][2]);
  33. ReadInt16(stream, matrix_sint16[1][2]);
  34. ReadInt16(stream, matrix_sint16[2][2]);
  35. stream->skip(2); // Unused.
  36. // Array to matrix.
  37. Ogre::Matrix3 camera_matrix;
  38. camera_matrix[0][0] = static_cast<float>(matrix_sint16[0][0]) / 4096.0f;
  39. camera_matrix[1][0] = static_cast<float>(matrix_sint16[1][0]) / 4096.0f;
  40. camera_matrix[2][0] = static_cast<float>(matrix_sint16[2][0]) / 4096.0f;
  41. camera_matrix[0][1] = static_cast<float>(matrix_sint16[0][1]) / 4096.0f;
  42. camera_matrix[1][1] = static_cast<float>(matrix_sint16[1][1]) / 4096.0f;
  43. camera_matrix[2][1] = static_cast<float>(matrix_sint16[2][1]) / 4096.0f;
  44. camera_matrix[0][2] = static_cast<float>(matrix_sint16[0][2]) / 4096.0f;
  45. camera_matrix[1][2] = static_cast<float>(matrix_sint16[1][2]) / 4096.0f;
  46. camera_matrix[2][2] = static_cast<float>(matrix_sint16[2][2]) / 4096.0f;
  47. // Read POS
  48. sint32 pos[CAMERA_MATRIX_ROW_COUNT];
  49. readInts(
  50. stream, reinterpret_cast<uint32*>(pos), CAMERA_MATRIX_ROW_COUNT
  51. );
  52. // Array to vector.
  53. Ogre::Vector3 position;
  54. for (int i = 0; i < 3; i ++) position[i] = -Ogre::Real(pos[i]);
  55. Ogre::Quaternion orientation;
  56. orientation.FromRotationMatrix(camera_matrix);
  57. position = orientation * (position / 128.0f);
  58. // Matrix fix up.
  59. //camera_matrix[0][0] = camera_matrix[0][0];
  60. //camera_matrix[1][0] = camera_matrix[1][0];
  61. //camera_matrix[2][0] = camera_matrix[2][0];
  62. camera_matrix[0][1] = -camera_matrix[0][1];
  63. camera_matrix[1][1] = -camera_matrix[1][1];
  64. camera_matrix[2][1] = -camera_matrix[2][1];
  65. camera_matrix[0][2] = -camera_matrix[0][2];
  66. camera_matrix[1][2] = -camera_matrix[1][2];
  67. camera_matrix[2][2] = -camera_matrix[2][2];
  68. dest->SetMatrix(camera_matrix);
  69. dest->SetPosition(position);
  70. Pixel offset;
  71. readObject(stream, offset);
  72. dest->SetOffset(offset);
  73. uint16 focal_length;
  74. ReadShort(stream, focal_length);
  75. dest->SetFocalLength(focal_length);
  76. size_t count(1);
  77. while (!stream->eof()){
  78. stream->skip(TOTAL_DATA_SIZE);
  79. ++ count;
  80. }
  81. dest->SetCount(count);
  82. }
  83. }