This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
actor.cpp
286 lines (228 loc) · 8.04 KB
/
actor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
#include "engines/stark/actor.h"
#include "engines/stark/skeleton.h"
#include "engines/stark/texture.h"
#include "engines/stark/gfx/coordinate.h"
#include "engines/stark/stark.h"
#include "common/archive.h"
#include "common/stream.h"
#include <SDL_opengl.h> // HACK: I just want to see something
namespace Stark {
Actor::Actor() : _skeleton(NULL), _texture(NULL) {
}
Actor::~Actor() {
for (Common::Array<MaterialNode *>::iterator it = _materials.begin(); it != _materials.end(); ++it)
delete *it;
for (Common::Array<MeshNode *>::iterator it = _meshes.begin(); it != _meshes.end(); ++it)
delete *it;
if (_skeleton)
delete _skeleton;
if (_texture)
delete _texture;
}
bool Actor::readFromStream(Common::ReadStream *stream) {
_id = stream->readUint32LE();
uint32 format = stream->readUint32LE();
uint32 u1 = stream->readUint32LE();
uint32 id2 = stream->readUint32LE();
if (id2 != 0xDEADBABE)
return false;
uint32 u2 = stream->readUint32LE();
uint32 numMaterials = stream->readUint32LE();
for (uint i = 0; i < numMaterials; ++i) {
MaterialNode *node = new MaterialNode();
uint32 len = stream->readUint32LE();
char *ptr = new char[len];
stream->read(ptr, len);
node->_name = Common::String(ptr, len);
uint32 u3 = stream->readUint32LE();
delete[] ptr;
len = stream->readUint32LE();
ptr = new char[len];
stream->read(ptr, len);
node->_texName = Common::String(ptr, len);
delete[] ptr;
ptr = new char[12];
stream->read(ptr, 12);
node->_r = get_float(ptr);
node->_g = get_float(ptr + 4);
node->_b = get_float(ptr + 8);
_materials.push_back(node);
}
uint32 numUnknowns = stream->readUint32LE();
for (uint32 i = 0; i < numUnknowns; ++i) {
UnknownNode *node = new UnknownNode();
char *ptr = new char[16];
stream->read(ptr, 16);
node->_u1 = get_float(ptr);
node->_u2 = get_float(ptr + 4);
node->_u3 = get_float(ptr + 8);
node->_u4 = get_float(ptr + 12);
}
_skeleton = new Skeleton();
_skeleton->readFromStream(stream);
uint32 numMeshes = stream->readUint32LE();
for (uint32 i = 0; i < numMeshes; ++i) {
MeshNode *node = new MeshNode();
uint32 len = stream->readUint32LE();
char *ptr = new char[len];
stream->read(ptr, len);
node->_name = Common::String(ptr, len);
delete[] ptr;
len = stream->readUint32LE();
for (uint32 j = 0; j < len; ++j) {
FaceNode *face = new FaceNode();
face->_matIdx = stream->readUint32LE();
uint32 childCount = stream->readUint32LE();
for (uint32 k = 0; k < childCount; ++k) {
VertNode *vert = new VertNode();
ptr = new char[14 * 4];
stream->read(ptr, 14 * 4);
vert->_pos1 = Graphics::Vector3d(get_float(ptr), get_float(ptr + 4), get_float(ptr + 8));
vert->_pos2 = Graphics::Vector3d(get_float(ptr + 12), get_float(ptr + 16), get_float(ptr + 20));
vert->_normal = Graphics::Vector3d(get_float(ptr + 24), get_float(ptr + 28), get_float(ptr + 32));
vert->_texS = get_float(ptr + 36);
vert->_texT = get_float(ptr + 40);
vert->_bone1 = READ_LE_UINT32(ptr + 44);
vert->_bone2 = READ_LE_UINT32(ptr + 48);
vert->_boneWeight = get_float(ptr + 52);
delete[] ptr;
face->_verts.push_back(vert);
}
childCount = stream->readUint32LE();
for (uint32 k = 0; k < childCount; ++k) {
TriNode *tri = new TriNode();
tri->_vert1 = stream->readUint32LE();
tri->_vert2 = stream->readUint32LE();
tri->_vert3 = stream->readUint32LE();
face->_tris.push_back(tri);
}
node->_faces.push_back(face);
}
_meshes.push_back(node);
}
return true;
}
bool Actor::setAnim(Common::ReadStream *stream)
{
return _skeleton->setAnim(stream);
}
bool Actor::setTexture(Common::ReadStream *stream)
{
if (_texture)
delete _texture;
_texture = new Texture();
return _texture->createFromStream(stream);
}
SceneElementActor *SceneElementActor::load(const Common::Archive *archive, const Common::String &name) {
Common::ReadStream *stream = archive->createReadStreamForMember(name);
if (!stream)
return NULL;
SceneElementActor *cir = new SceneElementActor();
cir->_actor = new Actor();
cir->_actor->readFromStream(stream);
return cir;
}
SceneElementActor::SceneElementActor() : _actor(NULL) {
}
SceneElementActor::~SceneElementActor() {
if (_actor)
delete _actor;
}
bool SceneElementActor::setAnim(const Common::Archive *archive, const Common::String &name) {
Common::ReadStream *stream = archive->createReadStreamForMember(name);
if (!stream)
return false;
return _actor->setAnim(stream);
}
bool SceneElementActor::setTexture(const Common::Archive *archive, const Common::String &name) {
Common::ReadStream *stream = archive->createReadStreamForMember(name);
if (!stream)
return false;
return _actor->setTexture(stream);
}
void SceneElementActor::update(uint32 delta) {
_actor->getSkeleton()->animate(delta);
}
void SceneElementActor::render(Stark::GfxDriver *gfx) {
// Prepare vertex list and push to gfx driver
// HACK: Purely because I just want to see something for now
static double ctr = 0;
ctr += .1;
gfx->set3DMode();
glPushMatrix();
glLoadIdentity();
glScalef(0.005f, .005f, -.005f);
glTranslatef(0, -20.f, 100.f);
glRotatef(20, .3f, 1.f, 0.f);
glRotatef(180, 0.f, 1.f, 0.f);
//glRotatef((ctr * 10.3f), -4.0f, 10.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
Common::Array<BoneNode *> bones = _actor->getSkeleton()->getBones();
Common::Array<MeshNode *> meshes = _actor->getMeshes();
Common::Array<MaterialNode *> mats = _actor->getMaterials();
const Texture *texture = _actor->getTexture();
for (Common::Array<MeshNode *>::iterator mesh = meshes.begin(); mesh != meshes.end(); ++mesh) {
for (Common::Array<FaceNode *>::iterator face = (*mesh)->_faces.begin(); face != (*mesh)->_faces.end(); ++face) {
// For each triangle to draw
uint32 tex = texture->getTexture(mats[(*face)->_matIdx]->_texName);
if (tex)
glColor3f(1.f, 1.f, 1.f);
else
glColor3f(mats[(*face)->_matIdx]->_r, mats[(*face)->_matIdx]->_g, mats[(*face)->_matIdx]->_b);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_TRIANGLES);
for (Common::Array<TriNode *>::iterator tri = (*face)->_tris.begin(); tri != (*face)->_tris.end(); ++tri) {
// Contains 3 vertices
// Each vertex relative to a bone coordinate
// 'move' to join location and rotation, then place the vertex there
for (int vert = 0; vert < 3; ++vert) {
int vertIdx;
if (vert == 0)
vertIdx = (*tri)->_vert1;
else if (vert == 1)
vertIdx = (*tri)->_vert3;
else
vertIdx = (*tri)->_vert2;
Coordinate b1 = (*face)->_verts[vertIdx]->_pos1;
BoneNode *bone = bones[(*face)->_verts[vertIdx]->_bone1];
b1.rotate(bone->_animPos);
b1.translate(bone->_animPos);
Coordinate b2 = (*face)->_verts[vertIdx]->_pos2;
bone = bones[(*face)->_verts[vertIdx]->_bone2];
b2.rotate(bone->_animPos);
b2.translate(bone->_animPos);
float w = (*face)->_verts[vertIdx]->_boneWeight;
Coordinate pos = b1 * w + b2 * (1.f - w);
if (tex)
glTexCoord2f(-(*face)->_verts[vertIdx]->_texS, (*face)->_verts[vertIdx]->_texT);
glVertex3f(pos.x(), pos.y(), -pos.z()); // - is LHS->RHS
}
}
glEnd();
}
}
glPopMatrix();
}
} // End of namespace Stark