World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
BSPModel.cpp
См. документацию.
2 
3 #include <cstdlib>
4 
5 #include "Engine/AssetsManager.h"
6 #include "Engine/Events.h"
8 #include "Engine/Time.h"
9 
10 #pragma pack(push, 1)
11 struct ODMFace_MM7 {
13  int zCalc1;
14  int zCalc2;
15  int zCalc3;
16  unsigned int uAttributes;
30  char field_128;
31  char field_129;
40  char field_132;
41  char field_133;
42 };
43 #pragma pack(pop)
44 
46  static_assert(sizeof(BSPModelData) == 188, "Wrong type size");
47  static_assert(sizeof(BSPNode) == 8, "Wrong type size");
48  static_assert(sizeof(Vec3_int_) == 12, "Wrong type size");
49 
50  uint32_t uNumBModels;
51  memcpy(&uNumBModels, data, 4); // количество BModel'ей
52  data += 4;
53 
54  std::vector<BSPModelData> models;
55  for (unsigned int i = 0; i < uNumBModels; ++i) {
56  BSPModelData model;
57  memcpy(&model, data, sizeof(BSPModelData));
58  data += sizeof(BSPModelData);
59  models.push_back(model);
60  }
61 
62  unsigned int model_index = 0;
63  for (BSPModelData &model : models) {
64  this->emplace_back();
65  BSPModel &new_model = this->back();
66 
67  new_model.index = model_index;
68  new_model.pModelName = model.pModelName;
69  new_model.pModelName2 = model.pModelName2;
70  new_model.field_40 = model.field_40;
71  new_model.sCenterX = model.sCenterX;
72  new_model.sCenterY = model.sCenterY;
73  new_model.vPosition = model.vPosition;
74  new_model.sMinX = model.sMinX;
75  new_model.sMinY = model.sMinY;
76  new_model.sMinZ = model.sMinZ;
77  new_model.sMaxX = model.sMaxX;
78  new_model.sMaxY = model.sMaxY;
79  new_model.sMaxZ = model.sMaxZ;
80  new_model.sSomeOtherMinX = model.sSomeOtherMinX;
81  new_model.sSomeOtherMinY = model.sSomeOtherMinY;
82  new_model.sSomeOtherMinZ = model.sSomeOtherMinZ;
83  new_model.sSomeOtherMaxX = model.sSomeOtherMaxX;
84  new_model.sSomeOtherMaxY = model.sSomeOtherMaxY;
85  new_model.sSomeOtherMaxZ = model.sSomeOtherMaxZ;
86  new_model.vBoundingCenter = model.vBoundingCenter;
87  new_model.sBoundingRadius = model.sBoundingRadius;
88 
89  unsigned int verticesSize = model.uNumVertices * sizeof(Vec3_int_);
90  new_model.pVertices.pVertices = (Vec3_int_ *)malloc(verticesSize);
91  memcpy(new_model.pVertices.pVertices, data, verticesSize);
92  data += verticesSize;
93 
94  for (size_t i = 0; i < model.uNumFaces; i++) {
95  new_model.pFaces.emplace_back();
96  ODMFace &face = new_model.pFaces.back();
97  face.index = i;
98  face.Deserialize((ODMFace_MM7 *)data);
99  data += sizeof(ODMFace_MM7);
100  }
101 
102  unsigned int facesOrderingSize = model.uNumFaces * sizeof(short);
103  new_model.pFacesOrdering = (uint16_t *)malloc(facesOrderingSize);
104  memcpy(new_model.pFacesOrdering, data, facesOrderingSize);
105  data += facesOrderingSize;
106 
107  for (size_t i = 0; i < model.uNumNodes; i++) {
108  BSPNode node;
109  memcpy(&node, data, sizeof(BSPNode));
110  new_model.pNodes.push_back(node);
111  data += sizeof(BSPNode);
112  }
113 
114  const char *textureFilenames =
115  (const char *)malloc(10 * model.uNumFaces);
116  memcpy((char *)textureFilenames, data, 10 * model.uNumFaces);
117  data += 10 * model.uNumFaces;
118  for (unsigned int j = 0; j < model.uNumFaces; ++j) {
119  const char *texFilename = &textureFilenames[j * 10];
120  new_model.pFaces[j].SetTexture(texFilename);
121  if (new_model.pFaces[j].sCogTriggeredID) {
122  if (new_model.pFaces[j].HasEventHint())
123  new_model.pFaces[j].uAttributes |= FACE_HAS_EVENT;
124  else
125  new_model.pFaces[j].uAttributes &= ~FACE_HAS_EVENT;
126  }
127  }
128 
129  free((void *)textureFilenames);
130 
131  model_index++;
132  }
133 
134  return data;
135 }
136 
138  free(this->pVertices.pVertices);
139  this->pVertices.pVertices = 0;
140  free(this->pFacesOrdering);
141  this->pFacesOrdering = nullptr;
142 }
143 
144 // ODMFace
145 
147  if (this->IsTextureFrameTable()) {
150  } else {
151  return (Texture *)this->resource;
152  }
153 }
154 
155 void ODMFace::SetTexture(const String &filename) {
156  if (this->IsTextureFrameTable()) {
157  this->resource = (void *)pTextureFrameTable->FindTextureByName(filename.c_str());
158  if (this->resource != (void *)-1) {
159  return;
160  }
161 
163  }
164 
165  this->resource = assets->GetBitmap(filename);
166 }
167 
169  memcpy(&this->pFacePlane, &mm7->pFacePlane, sizeof(this->pFacePlane));
170  this->zCalc1 = mm7->zCalc1;
171  this->zCalc2 = mm7->zCalc2;
172  this->zCalc3 = mm7->zCalc3;
173  this->uAttributes = mm7->uAttributes;
174  memcpy(this->pVertexIDs, mm7->pVertexIDs, sizeof(this->pVertexIDs));
175  memcpy(this->pTextureUIDs, mm7->pTextureUIDs, sizeof(this->pTextureUIDs));
176  memcpy(this->pTextureVIDs, mm7->pTextureVIDs, sizeof(this->pTextureVIDs));
178  sizeof(this->pXInterceptDisplacements));
180  sizeof(this->pYInterceptDisplacements));
182  sizeof(this->pZInterceptDisplacements));
183  this->resource = nullptr;
184  this->sTextureDeltaU = mm7->sTextureDeltaU;
185  this->sTextureDeltaV = mm7->sTextureDeltaV;
186  memcpy(&this->pBoundingBox, &mm7->pBoundingBox, sizeof(this->pBoundingBox));
187  this->sCogNumber = mm7->sCogNumber;
188  this->sCogTriggeredID = mm7->sCogTriggeredID;
189  this->sCogTriggerType = mm7->sCogTriggerType;
190  this->field_128 = mm7->field_128;
191  this->field_129 = mm7->field_129;
192  this->uGradientVertex1 = mm7->uGradientVertex1;
193  this->uGradientVertex2 = mm7->uGradientVertex2;
194  this->uGradientVertex3 = mm7->uGradientVertex3;
195  this->uGradientVertex4 = mm7->uGradientVertex4;
196  this->uNumVertices = mm7->uNumVertices;
197  this->uPolygonType = mm7->uPolygonType;
198  this->uShadeType = mm7->uShadeType;
199  this->bVisible = mm7->bVisible;
200  this->field_132 = mm7->field_132;
201  this->field_133 = mm7->field_133;
202 
203  return true;
204 }
205 
207  signed int event_index; // eax@1
208  _evt_raw *start_evt;
209  _evt_raw *end_evt;
210 
211  event_index = 0;
212  if ((uLevelEVT_NumEvents - 1) <= 0) return false;
213  while (pLevelEVT_Index[event_index].uEventID != this->sCogTriggeredID) {
214  ++event_index;
215  if (event_index >= (signed int)(uLevelEVT_NumEvents - 1)) return false;
216  }
217  end_evt =
218  (_evt_raw
219  *)&pLevelEVT[pLevelEVT_Index[event_index + 1].uEventOffsetInEVT];
220  start_evt =
221  (_evt_raw *)&pLevelEVT[pLevelEVT_Index[event_index].uEventOffsetInEVT];
222  if ((end_evt->_e_type != EVENT_Exit) ||
223  (start_evt->_e_type != EVENT_MouseOver))
224  return false;
225  else
226  return true;
227 }
ODMFace_MM7::field_128
char field_128
Definition: BSPModel.cpp:30
uint16_t
unsigned __int16 uint16_t
Definition: SDL_config.h:37
BSPModel::sSomeOtherMinX
int32_t sSomeOtherMinX
Definition: BSPModel.h:180
ODMFace::uGradientVertex4
uint8_t uGradientVertex4
Definition: BSPModel.h:154
ODMFace::field_129
char field_129
Definition: BSPModel.h:150
BSPModel::pVertices
struct BSPVertexBuffer pVertices
Definition: BSPModel.h:189
ODMFace_MM7::pTextureUIDs
uint16_t pTextureUIDs[20]
Definition: BSPModel.cpp:18
face
GLenum GLuint GLint GLenum face
Definition: SDL_opengl_glext.h:3022
ODMFace_MM7::zCalc3
int zCalc3
Definition: BSPModel.cpp:15
ODMFace::ToggleIsTextureFrameTable
void ToggleIsTextureFrameTable()
Definition: BSPModel.h:119
int16_t
signed __int16 int16_t
Definition: SDL_config.h:36
ODMFace::zCalc1
int zCalc1
Definition: BSPModel.h:132
ODMFace_MM7::uPolygonType
uint8_t uPolygonType
Definition: BSPModel.cpp:37
ODMFace::pYInterceptDisplacements
int16_t pYInterceptDisplacements[20]
Definition: BSPModel.h:140
EVENT_Exit
@ EVENT_Exit
Definition: Events.h:61
ODMFace::pTextureVIDs
int16_t pTextureVIDs[20]
Definition: BSPModel.h:138
TextureFrameTable::FindTextureByName
int FindTextureByName(const char *Str2)
Definition: Image.cpp:136
BSPModel::sSomeOtherMinY
int32_t sSomeOtherMinY
Definition: BSPModel.h:181
pTextureFrameTable
struct TextureFrameTable * pTextureFrameTable
Definition: Image.cpp:20
BSPModel::vPosition
Vec3_int_ vPosition
Definition: BSPModel.h:173
ODMFace_MM7::uNumVertices
uint8_t uNumVertices
Definition: BSPModel.cpp:36
BSPModel::index
unsigned int index
Definition: BSPModel.h:167
ODMFace_MM7::sCogTriggeredID
int16_t sCogTriggeredID
Definition: BSPModel.cpp:28
ODMFace::IsTextureFrameTable
bool IsTextureFrameTable()
Definition: BSPModel.h:116
BSPModelData
Definition: BSPModel.h:58
BSPModel::sMaxX
int32_t sMaxX
Definition: BSPModel.h:177
BSPModel::sCenterY
int32_t sCenterY
Definition: BSPModel.h:172
Texture
Definition: Texture.h:4
BBox_short_
Definition: VectorTypes.h:113
BSPModel::sBoundingRadius
int32_t sBoundingRadius
Definition: BSPModel.h:187
ODMFace::zCalc3
int zCalc3
Definition: BSPModel.h:134
ODMFace_MM7::uGradientVertex3
uint8_t uGradientVertex3
Definition: BSPModel.cpp:34
BSPModel::sCenterX
int32_t sCenterX
Definition: BSPModel.h:171
BSPModel::sSomeOtherMinZ
int32_t sSomeOtherMinZ
Definition: BSPModel.h:182
_evt_raw::_e_type
unsigned char _e_type
Definition: Events.h:26
ODMFace::sCogTriggeredID
int16_t sCogTriggeredID
Definition: BSPModel.h:147
ODMFace::uPolygonType
uint8_t uPolygonType
Definition: BSPModel.h:156
ODMFace_MM7
Definition: BSPModel.cpp:11
ODMFace_MM7::pBoundingBox
struct BBox_short_ pBoundingBox
Definition: BSPModel.cpp:26
BSPModelList::Load
uint8_t * Load(uint8_t *data)
Definition: BSPModel.cpp:45
BSPNode
Definition: BSPModel.h:42
AssetsManager.h
BSPModel::sMinZ
int32_t sMinZ
Definition: BSPModel.h:176
ODMFace::pTextureUIDs
int16_t pTextureUIDs[20]
Definition: BSPModel.h:137
ODMFace_MM7::zCalc2
int zCalc2
Definition: BSPModel.cpp:14
Image.h
ODMFace_MM7::uTextureID
int16_t uTextureID
Definition: BSPModel.cpp:23
Events.h
ODMFace::sCogTriggerType
int16_t sCogTriggerType
Definition: BSPModel.h:148
ODMFace::pBoundingBox
struct BBox_short_ pBoundingBox
Definition: BSPModel.h:145
ODMFace::sTextureDeltaV
int16_t sTextureDeltaV
Definition: BSPModel.h:144
ODMFace_MM7::uGradientVertex4
uint8_t uGradientVertex4
Definition: BSPModel.cpp:35
BSPModel::pFacesOrdering
uint16_t * pFacesOrdering
Definition: BSPModel.h:191
EVENT_MouseOver
@ EVENT_MouseOver
Definition: Events.h:64
ODMFace_MM7::sTextureDeltaU
int16_t sTextureDeltaU
Definition: BSPModel.cpp:24
BSPModel::sMinY
int32_t sMinY
Definition: BSPModel.h:175
ODMFace::pVertexIDs
uint16_t pVertexIDs[20]
Definition: BSPModel.h:136
ODMFace_MM7::zCalc1
int zCalc1
Definition: BSPModel.cpp:13
ODMFace::pXInterceptDisplacements
int16_t pXInterceptDisplacements[20]
Definition: BSPModel.h:139
BSPModel::pModelName
String pModelName
Definition: BSPModel.h:168
ODMFace_MM7::field_129
char field_129
Definition: BSPModel.cpp:31
ODMFace_MM7::pXInterceptDisplacements
int16_t pXInterceptDisplacements[20]
Definition: BSPModel.cpp:20
pLevelEVT
std::array< char, 9216 > pLevelEVT
Definition: Events.cpp:54
BSPModel::pNodes
std::vector< BSPNode > pNodes
Definition: BSPModel.h:192
ODMFace::uGradientVertex2
uint8_t uGradientVertex2
Definition: BSPModel.h:152
ODMFace_MM7::pFacePlane
struct Plane_int_ pFacePlane
Definition: BSPModel.cpp:12
ODMFace::field_128
char field_128
Definition: BSPModel.h:149
ODMFace_MM7::sCogNumber
int16_t sCogNumber
Definition: BSPModel.cpp:27
ODMFace::uGradientVertex1
uint8_t uGradientVertex1
Definition: BSPModel.h:151
Plane_int_
Definition: VectorTypes.h:106
ODMFace_MM7::bVisible
uint8_t bVisible
Definition: BSPModel.cpp:39
ODMFace_MM7::pZInterceptDisplacements
int16_t pZInterceptDisplacements[20]
Definition: BSPModel.cpp:22
BSPModel::sMinX
int32_t sMinX
Definition: BSPModel.h:174
ODMFace_MM7::uGradientVertex2
uint8_t uGradientVertex2
Definition: BSPModel.cpp:33
ODMFace::uShadeType
uint8_t uShadeType
Definition: BSPModel.h:157
ODMFace::resource
void * resource
Definition: BSPModel.h:142
ODMFace::uNumVertices
uint8_t uNumVertices
Definition: BSPModel.h:155
AssetsManager::GetBitmap
Texture * GetBitmap(const String &name)
Definition: AssetsManager.cpp:126
ODMFace::uGradientVertex3
uint8_t uGradientVertex3
Definition: BSPModel.h:153
ODMFace_MM7::sTextureDeltaV
int16_t sTextureDeltaV
Definition: BSPModel.cpp:25
ODMFace::field_133
char field_133
Definition: BSPModel.h:160
ODMFace::SetTexture
void SetTexture(const String &filename)
Definition: BSPModel.cpp:155
pLevelEVT_Index
std::array< EventIndex, 4400 > pLevelEVT_Index
Definition: Events.cpp:55
ODMFace::sTextureDeltaU
int16_t sTextureDeltaU
Definition: BSPModel.h:143
BSPModel.h
ODMFace_MM7::pVertexIDs
uint16_t pVertexIDs[20]
Definition: BSPModel.cpp:17
ODMFace_MM7::sCogTriggerType
int16_t sCogTriggerType
Definition: BSPModel.cpp:29
ODMFace::Deserialize
bool Deserialize(struct ODMFace_MM7 *)
Definition: BSPModel.cpp:168
BSPModel::sMaxY
int32_t sMaxY
Definition: BSPModel.h:178
uint8_t
unsigned __int8 uint8_t
Definition: SDL_config.h:35
ODMFace_MM7::field_132
char field_132
Definition: BSPModel.cpp:40
ODMFace_MM7::uAttributes
unsigned int uAttributes
Definition: BSPModel.cpp:16
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
ODMFace::HasEventHint
bool HasEventHint()
Definition: BSPModel.cpp:206
BSPModel::sSomeOtherMaxY
int32_t sSomeOtherMaxY
Definition: BSPModel.h:184
ODMFace::GetTexture
Texture * GetTexture()
Definition: BSPModel.cpp:146
assets
AssetsManager * assets
Definition: AssetsManager.cpp:12
BSPModel::pModelName2
String pModelName2
Definition: BSPModel.h:169
ODMFace::field_132
char field_132
Definition: BSPModel.h:159
_evt_raw
Definition: Events.h:20
uLevelEVT_NumEvents
signed int uLevelEVT_NumEvents
Definition: Events.cpp:51
ODMFace_MM7::uShadeType
uint8_t uShadeType
Definition: BSPModel.cpp:38
BSPModel::pFaces
std::vector< ODMFace > pFaces
Definition: BSPModel.h:190
ODMFace
Definition: BSPModel.h:93
BSPModel::vBoundingCenter
Vec3_int_ vBoundingCenter
Definition: BSPModel.h:186
BSPModel::sSomeOtherMaxX
int32_t sSomeOtherMaxX
Definition: BSPModel.h:183
ODMFace::uAttributes
uint32_t uAttributes
Definition: BSPModel.h:135
ODMFace::pFacePlane
struct Plane_int_ pFacePlane
Definition: BSPModel.h:131
BSPVertexBuffer::pVertices
Vec3_int_ * pVertices
Definition: BSPModel.h:53
BSPModel::Release
void Release()
Definition: BSPModel.cpp:137
ODMFace::zCalc2
int zCalc2
Definition: BSPModel.h:133
BSPModel::field_40
int32_t field_40
Definition: BSPModel.h:170
BSPModel
Definition: BSPModel.h:163
BSPModel::sSomeOtherMaxZ
int32_t sSomeOtherMaxZ
Definition: BSPModel.h:185
ODMFace_MM7::uGradientVertex1
uint8_t uGradientVertex1
Definition: BSPModel.cpp:32
ODMFace_MM7::field_133
char field_133
Definition: BSPModel.cpp:41
uint32_t
unsigned __int32 uint32_t
Definition: SDL_config.h:39
Timer::uTotalGameTimeElapsed
unsigned int uTotalGameTimeElapsed
Definition: Time.h:135
String
std::string String
Definition: Strings.h:10
ODMFace::pZInterceptDisplacements
int16_t pZInterceptDisplacements[20]
Definition: BSPModel.h:141
ODMFace_MM7::pYInterceptDisplacements
int16_t pYInterceptDisplacements[20]
Definition: BSPModel.cpp:21
pEventTimer
Timer * pEventTimer
Definition: Time.cpp:8
BSPModel::sMaxZ
int32_t sMaxZ
Definition: BSPModel.h:179
ODMFace::bVisible
uint8_t bVisible
Definition: BSPModel.h:158
ODMFace_MM7::pTextureVIDs
uint16_t pTextureVIDs[20]
Definition: BSPModel.cpp:19
Time.h
ODMFace::sCogNumber
int16_t sCogNumber
Definition: BSPModel.h:146
TextureFrameTable::GetFrameTexture
Texture * GetFrameTexture(int uFrameID, int time)
Definition: Image.cpp:146