rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@46
|
2 #include <ctype.h>
|
nuclear@7
|
3 #include <map>
|
nuclear@1
|
4 #include "opengl.h"
|
nuclear@7
|
5 #include <assimp/cimport.h>
|
nuclear@7
|
6 #include <assimp/scene.h>
|
nuclear@7
|
7 #include <assimp/postprocess.h>
|
nuclear@1
|
8 #include "tile.h"
|
nuclear@11
|
9 #include "tileset.h"
|
nuclear@35
|
10 #include "renderer.h"
|
nuclear@46
|
11 #include "datapath.h"
|
nuclear@1
|
12
|
nuclear@7
|
13 using std::map;
|
nuclear@7
|
14
|
nuclear@7
|
15 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
|
nuclear@21
|
16 static PointLight *mesh_to_light(Mesh *m);
|
nuclear@7
|
17
|
nuclear@31
|
18 bool ass_obj_hack;
|
nuclear@31
|
19
|
nuclear@11
|
20 Tile::Tile(TileSet *tileset)
|
nuclear@11
|
21 {
|
nuclear@11
|
22 tset = tileset;
|
nuclear@46
|
23 last_upd = LONG_MIN;
|
nuclear@48
|
24
|
nuclear@48
|
25 memset(samples, 0, sizeof samples);
|
nuclear@11
|
26 }
|
nuclear@7
|
27
|
nuclear@38
|
28 Tile::~Tile()
|
nuclear@38
|
29 {
|
nuclear@38
|
30 for(auto m : meshes) {
|
nuclear@38
|
31 delete m;
|
nuclear@38
|
32 }
|
nuclear@38
|
33 for(auto lt : lights) {
|
nuclear@38
|
34 delete lt;
|
nuclear@38
|
35 }
|
nuclear@46
|
36 for(auto psa : psattr) {
|
nuclear@46
|
37 psys_free_attr(psa);
|
nuclear@46
|
38 }
|
nuclear@46
|
39 for(auto ps : psys_global) {
|
nuclear@46
|
40 psys_free(ps);
|
nuclear@38
|
41 }
|
nuclear@38
|
42 }
|
nuclear@38
|
43
|
nuclear@48
|
44 AudioSample *Tile::get_sample(int sidx) const
|
nuclear@48
|
45 {
|
nuclear@48
|
46 if(sidx >= 0 && sidx < MAX_TILE_SAMPLES) {
|
nuclear@48
|
47 return samples[sidx];
|
nuclear@48
|
48 }
|
nuclear@48
|
49 return 0;
|
nuclear@48
|
50 }
|
nuclear@48
|
51
|
nuclear@46
|
52 const struct psys_attributes * const *Tile::get_unique_psys() const
|
nuclear@45
|
53 {
|
nuclear@45
|
54 return &psattr[0];
|
nuclear@45
|
55 }
|
nuclear@45
|
56
|
nuclear@46
|
57 int Tile::get_unique_psys_count() const
|
nuclear@45
|
58 {
|
nuclear@45
|
59 return (int)psattr.size();
|
nuclear@45
|
60 }
|
nuclear@45
|
61
|
nuclear@1
|
62 bool Tile::load(const char *fname)
|
nuclear@1
|
63 {
|
nuclear@5
|
64 if(!fname) {
|
nuclear@5
|
65 return false;
|
nuclear@5
|
66 }
|
nuclear@5
|
67
|
nuclear@14
|
68 char *saved_fname = (char*)alloca(strlen(fname) + 1);
|
nuclear@14
|
69 strcpy(saved_fname, fname);
|
nuclear@14
|
70
|
nuclear@3
|
71 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
|
nuclear@35
|
72 aiProcess_CalcTangentSpace |
|
nuclear@7
|
73 aiProcess_Triangulate |
|
nuclear@7
|
74 aiProcess_SortByPType |
|
nuclear@7
|
75 aiProcess_FlipUVs;
|
nuclear@4
|
76
|
nuclear@3
|
77 const aiScene *scn = aiImportFile(fname, proc_flags);
|
nuclear@3
|
78 if(!scn) {
|
nuclear@3
|
79 fprintf(stderr, "failed to load tile: %s\n", fname);
|
nuclear@3
|
80 return -1;
|
nuclear@3
|
81 }
|
nuclear@3
|
82
|
nuclear@7
|
83 map<aiMesh*, aiNode*> nodemap;
|
nuclear@7
|
84 build_nodemap(&nodemap, scn, scn->mRootNode);
|
nuclear@7
|
85
|
nuclear@31
|
86 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
|
nuclear@31
|
87 ass_obj_hack = true;
|
nuclear@31
|
88 } else {
|
nuclear@31
|
89 ass_obj_hack = false;
|
nuclear@31
|
90 }
|
nuclear@31
|
91
|
nuclear@21
|
92 //load_lights(scn);
|
nuclear@7
|
93 load_meshes(scn, nodemap);
|
nuclear@3
|
94
|
nuclear@22
|
95 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
|
nuclear@38
|
96
|
nuclear@38
|
97 aiReleaseImport(scn);
|
nuclear@48
|
98
|
nuclear@48
|
99 // XXX get the default audio samples for now
|
nuclear@48
|
100 SampleSet *sampleset = tset->get_samples();
|
nuclear@48
|
101 samples[TILE_SAMPLE_WALK] = sampleset->get("walk_stone.ogg");
|
nuclear@48
|
102 samples[TILE_SAMPLE_RUN] = sampleset->get("run_stone.ogg");
|
nuclear@1
|
103 return true;
|
nuclear@1
|
104 }
|
nuclear@1
|
105
|
nuclear@38
|
106 void Tile::update(unsigned long msec, float dt)
|
nuclear@38
|
107 {
|
nuclear@46
|
108 // update particle systems
|
nuclear@46
|
109 for(auto ps : psys_global) {
|
nuclear@46
|
110 psys_update(ps, (float)msec / 1000.0f);
|
nuclear@46
|
111 }
|
nuclear@38
|
112 }
|
nuclear@38
|
113
|
nuclear@5
|
114 void Tile::draw(unsigned int draw_mask) const
|
nuclear@1
|
115 {
|
nuclear@4
|
116 for(size_t i=0; i<meshes.size(); i++) {
|
nuclear@4
|
117 if(mesh_side[i] & draw_mask) {
|
nuclear@5
|
118 meshes[i]->draw();
|
nuclear@4
|
119 }
|
nuclear@4
|
120 }
|
nuclear@4
|
121 }
|
nuclear@1
|
122
|
nuclear@23
|
123 void Tile::draw_lights(unsigned int draw_mask) const
|
nuclear@23
|
124 {
|
nuclear@23
|
125 for(size_t i=0; i<lights.size(); i++) {
|
nuclear@23
|
126 if(light_side[i] & draw_mask) {
|
nuclear@23
|
127 lights[i]->draw();
|
nuclear@23
|
128 }
|
nuclear@23
|
129 }
|
nuclear@23
|
130 }
|
nuclear@23
|
131
|
nuclear@46
|
132 void Tile::draw_post(unsigned int draw_mask) const
|
nuclear@46
|
133 {
|
nuclear@46
|
134 // draw global particle systems (simulated once)
|
nuclear@46
|
135 for(size_t i=0; i<psys_global.size(); i++) {
|
nuclear@46
|
136 if(psys_side[i] & draw_mask) {
|
nuclear@46
|
137 psys_draw(psys_global[i]);
|
nuclear@46
|
138 }
|
nuclear@46
|
139 }
|
nuclear@46
|
140 }
|
nuclear@46
|
141
|
nuclear@21
|
142 /*
|
nuclear@4
|
143 int Tile::load_lights(const aiScene *scn)
|
nuclear@4
|
144 {
|
nuclear@4
|
145 int count = 0;
|
nuclear@4
|
146
|
nuclear@4
|
147 for(int i=0; i<(int)scn->mNumLights; i++) {
|
nuclear@4
|
148 Light *lt;
|
nuclear@4
|
149 aiLight *ailt = scn->mLights[i];
|
nuclear@4
|
150
|
nuclear@4
|
151 switch(ailt->mType) {
|
nuclear@4
|
152 case aiLightSource_POINT:
|
nuclear@4
|
153 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
|
nuclear@4
|
154 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
|
nuclear@4
|
155 ailt->mAttenuationQuadratic);
|
nuclear@4
|
156 break;
|
nuclear@4
|
157
|
nuclear@4
|
158 case aiLightSource_DIRECTIONAL:
|
nuclear@4
|
159 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
|
nuclear@4
|
160 break;
|
nuclear@4
|
161
|
nuclear@4
|
162 default:
|
nuclear@4
|
163 continue;
|
nuclear@4
|
164 }
|
nuclear@4
|
165
|
nuclear@4
|
166 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
|
nuclear@4
|
167
|
nuclear@4
|
168 lights.push_back(lt);
|
nuclear@4
|
169 count++;
|
nuclear@4
|
170 }
|
nuclear@4
|
171
|
nuclear@4
|
172 return count;
|
nuclear@1
|
173 }
|
nuclear@21
|
174 */
|
nuclear@4
|
175
|
nuclear@7
|
176 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
|
nuclear@4
|
177 {
|
nuclear@4
|
178 int count = 0;
|
nuclear@4
|
179
|
nuclear@41
|
180 int attr_loc = rend->get_tangent_location();
|
nuclear@35
|
181 if(attr_loc == -1) {
|
nuclear@35
|
182 fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
|
nuclear@35
|
183 }
|
nuclear@35
|
184
|
nuclear@4
|
185 for(int i=0; i<(int)scn->mNumMeshes; i++) {
|
nuclear@37
|
186 // ignore any lines or other crap
|
nuclear@37
|
187 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
|
nuclear@37
|
188 continue;
|
nuclear@37
|
189 }
|
nuclear@37
|
190
|
nuclear@4
|
191 Mesh *mesh = new Mesh;
|
nuclear@4
|
192 if(!mesh->create(scn, scn->mMeshes[i])) {
|
nuclear@4
|
193 delete mesh;
|
nuclear@4
|
194 continue;
|
nuclear@4
|
195 }
|
nuclear@35
|
196 if(attr_loc != -1) {
|
nuclear@35
|
197 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
|
nuclear@35
|
198 }
|
nuclear@4
|
199
|
nuclear@11
|
200 Material mat;
|
nuclear@11
|
201 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
|
nuclear@11
|
202 mesh->set_material(mat);
|
nuclear@11
|
203
|
nuclear@7
|
204 // retrieve the node pointer
|
nuclear@7
|
205 const char *name = "<unknown>";
|
nuclear@7
|
206
|
nuclear@7
|
207 auto iter = nmap.find(scn->mMeshes[i]);
|
nuclear@7
|
208 if(iter != nmap.end()) {
|
nuclear@7
|
209 aiNode *node = iter->second;
|
nuclear@7
|
210
|
nuclear@7
|
211 Matrix4x4 xform;
|
nuclear@7
|
212 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
|
nuclear@7
|
213 xform = *(Matrix4x4*)&node->mTransformation;
|
nuclear@7
|
214 mesh->set_xform(xform);
|
nuclear@7
|
215
|
nuclear@7
|
216 name = node->mName.data;
|
nuclear@7
|
217 mesh->set_name(name);
|
nuclear@7
|
218 }
|
nuclear@7
|
219
|
nuclear@4
|
220 // find which side is this mesh on
|
nuclear@7
|
221 unsigned int side = 0;
|
nuclear@7
|
222 if(strstr(name, "NORTH")) {
|
nuclear@7
|
223 side |= TILE_NORTH;
|
nuclear@7
|
224 }
|
nuclear@7
|
225 if(strstr(name, "SOUTH")) {
|
nuclear@7
|
226 side |= TILE_SOUTH;
|
nuclear@7
|
227 }
|
nuclear@7
|
228 if(strstr(name, "EAST")) {
|
nuclear@7
|
229 side |= TILE_EAST;
|
nuclear@7
|
230 }
|
nuclear@7
|
231 if(strstr(name, "WEST")) {
|
nuclear@7
|
232 side |= TILE_WEST;
|
nuclear@7
|
233 }
|
nuclear@7
|
234 if(!side) {
|
nuclear@4
|
235 side = TILE_ALL;
|
nuclear@4
|
236 }
|
nuclear@4
|
237
|
nuclear@21
|
238 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
|
nuclear@21
|
239 // and destroy the mesh...
|
nuclear@21
|
240 if(strstr(name, "LIGHT")) {
|
nuclear@21
|
241 PointLight *lt = mesh_to_light(mesh);
|
nuclear@21
|
242 if(!lt) {
|
nuclear@21
|
243 fprintf(stderr, "failed to convert mesh %s to light\n", name);
|
nuclear@21
|
244 } else {
|
nuclear@21
|
245 lights.push_back(lt);
|
nuclear@21
|
246 light_side.push_back(side);
|
nuclear@21
|
247 }
|
nuclear@21
|
248 delete mesh;
|
nuclear@21
|
249
|
nuclear@46
|
250 // ... ALSO add a fire particle system :) save me jebus
|
nuclear@46
|
251 struct psys_emitter *ps = psys_create();
|
nuclear@46
|
252 if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys")) == 0) {
|
nuclear@46
|
253 Vector3 lpos = lt->get_position();
|
nuclear@46
|
254 psys_set_pos(ps, v3_cons(lpos.x, lpos.y, lpos.z), 0);
|
nuclear@46
|
255 psys_global.push_back(ps);
|
nuclear@46
|
256 psys_side.push_back(side);
|
nuclear@46
|
257 } else {
|
nuclear@46
|
258 fprintf(stderr, "failed to create global particle system\n");
|
nuclear@46
|
259 }
|
nuclear@46
|
260
|
nuclear@21
|
261 } else {
|
nuclear@21
|
262 meshes.push_back(mesh);
|
nuclear@21
|
263 mesh_side.push_back(side);
|
nuclear@21
|
264 count++;
|
nuclear@21
|
265 }
|
nuclear@4
|
266 }
|
nuclear@4
|
267 return count;
|
nuclear@4
|
268 }
|
nuclear@7
|
269
|
nuclear@7
|
270 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
|
nuclear@7
|
271 {
|
nuclear@7
|
272 unsigned int i;
|
nuclear@7
|
273
|
nuclear@7
|
274 for(i=0; i<node->mNumMeshes; i++) {
|
nuclear@7
|
275 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
|
nuclear@7
|
276
|
nuclear@7
|
277 (*nmap)[m] = node;
|
nuclear@7
|
278 }
|
nuclear@7
|
279
|
nuclear@7
|
280 for(i=0; i<node->mNumChildren; i++) {
|
nuclear@7
|
281 build_nodemap(nmap, scn, node->mChildren[i]);
|
nuclear@7
|
282 }
|
nuclear@7
|
283 }
|
nuclear@21
|
284
|
nuclear@21
|
285 static PointLight *mesh_to_light(Mesh *m)
|
nuclear@21
|
286 {
|
nuclear@21
|
287 Vector3 center = m->get_bsph_center();
|
nuclear@21
|
288 float rad = m->get_bsph_radius();
|
nuclear@21
|
289
|
nuclear@21
|
290 PointLight *lt = new PointLight(center);
|
nuclear@21
|
291 lt->set_radius(rad);
|
nuclear@21
|
292
|
nuclear@21
|
293 lt->set_color(m->get_material().kd);
|
nuclear@21
|
294
|
nuclear@21
|
295 return lt;
|
nuclear@21
|
296 }
|