dungeon_crawler

view prototype/src/tile.cc @ 45:dfd3a413ef9e

particle system 1
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 12 Sep 2012 06:04:20 +0300
parents acfe0c0110fc
children f3030df27110
line source
1 #include <stdio.h>
2 #include <map>
3 #include "opengl.h"
4 #include <assimp/cimport.h>
5 #include <assimp/scene.h>
6 #include <assimp/postprocess.h>
7 #include "tile.h"
8 #include "tileset.h"
9 #include "renderer.h"
11 using std::map;
13 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
14 static PointLight *mesh_to_light(Mesh *m);
16 bool ass_obj_hack;
18 Tile::Tile(TileSet *tileset)
19 {
20 tset = tileset;
21 }
23 Tile::~Tile()
24 {
25 for(auto m : meshes) {
26 delete m;
27 }
28 for(auto lt : lights) {
29 delete lt;
30 }
31 for(auto ps : psattr) {
32 psys_destroy_attr(ps);
33 delete ps;
34 }
35 }
37 const struct psys_attributes **get_unique_psys() const
38 {
39 return &psattr[0];
40 }
42 int get_unique_psys_count() const
43 {
44 return (int)psattr.size();
45 }
47 bool Tile::load(const char *fname)
48 {
49 if(!fname) {
50 return false;
51 }
53 char *saved_fname = (char*)alloca(strlen(fname) + 1);
54 strcpy(saved_fname, fname);
56 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
57 aiProcess_CalcTangentSpace |
58 aiProcess_Triangulate |
59 aiProcess_SortByPType |
60 aiProcess_FlipUVs;
62 const aiScene *scn = aiImportFile(fname, proc_flags);
63 if(!scn) {
64 fprintf(stderr, "failed to load tile: %s\n", fname);
65 return -1;
66 }
68 map<aiMesh*, aiNode*> nodemap;
69 build_nodemap(&nodemap, scn, scn->mRootNode);
71 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
72 ass_obj_hack = true;
73 } else {
74 ass_obj_hack = false;
75 }
77 //load_lights(scn);
78 load_meshes(scn, nodemap);
80 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
82 aiReleaseImport(scn);
83 return true;
84 }
86 void Tile::update(unsigned long msec, float dt)
87 {
88 // TODO particle system update
89 }
91 void Tile::draw(unsigned int draw_mask) const
92 {
93 for(size_t i=0; i<meshes.size(); i++) {
94 if(mesh_side[i] & draw_mask) {
95 meshes[i]->draw();
96 }
97 }
98 }
100 void Tile::draw_lights(unsigned int draw_mask) const
101 {
102 for(size_t i=0; i<lights.size(); i++) {
103 if(light_side[i] & draw_mask) {
104 lights[i]->draw();
105 }
106 }
107 }
109 /*
110 int Tile::load_lights(const aiScene *scn)
111 {
112 int count = 0;
114 for(int i=0; i<(int)scn->mNumLights; i++) {
115 Light *lt;
116 aiLight *ailt = scn->mLights[i];
118 switch(ailt->mType) {
119 case aiLightSource_POINT:
120 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
121 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
122 ailt->mAttenuationQuadratic);
123 break;
125 case aiLightSource_DIRECTIONAL:
126 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
127 break;
129 default:
130 continue;
131 }
133 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
135 lights.push_back(lt);
136 count++;
137 }
139 return count;
140 }
141 */
143 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
144 {
145 int count = 0;
147 int attr_loc = rend->get_tangent_location();
148 if(attr_loc == -1) {
149 fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
150 }
152 for(int i=0; i<(int)scn->mNumMeshes; i++) {
153 // ignore any lines or other crap
154 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
155 continue;
156 }
158 Mesh *mesh = new Mesh;
159 if(!mesh->create(scn, scn->mMeshes[i])) {
160 delete mesh;
161 continue;
162 }
163 if(attr_loc != -1) {
164 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
165 }
167 Material mat;
168 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
169 mesh->set_material(mat);
171 // retrieve the node pointer
172 const char *name = "<unknown>";
174 auto iter = nmap.find(scn->mMeshes[i]);
175 if(iter != nmap.end()) {
176 aiNode *node = iter->second;
178 Matrix4x4 xform;
179 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
180 xform = *(Matrix4x4*)&node->mTransformation;
181 mesh->set_xform(xform);
183 name = node->mName.data;
184 mesh->set_name(name);
185 }
187 // find which side is this mesh on
188 unsigned int side = 0;
189 if(strstr(name, "NORTH")) {
190 side |= TILE_NORTH;
191 }
192 if(strstr(name, "SOUTH")) {
193 side |= TILE_SOUTH;
194 }
195 if(strstr(name, "EAST")) {
196 side |= TILE_EAST;
197 }
198 if(strstr(name, "WEST")) {
199 side |= TILE_WEST;
200 }
201 if(!side) {
202 side = TILE_ALL;
203 }
205 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
206 // and destroy the mesh...
207 if(strstr(name, "LIGHT")) {
208 PointLight *lt = mesh_to_light(mesh);
209 if(!lt) {
210 fprintf(stderr, "failed to convert mesh %s to light\n", name);
211 } else {
212 lights.push_back(lt);
213 light_side.push_back(side);
214 }
215 delete mesh;
217 } else {
218 meshes.push_back(mesh);
219 mesh_side.push_back(side);
220 count++;
221 }
222 }
223 return count;
224 }
226 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
227 {
228 unsigned int i;
230 for(i=0; i<node->mNumMeshes; i++) {
231 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
233 (*nmap)[m] = node;
234 }
236 for(i=0; i<node->mNumChildren; i++) {
237 build_nodemap(nmap, scn, node->mChildren[i]);
238 }
239 }
241 static PointLight *mesh_to_light(Mesh *m)
242 {
243 Vector3 center = m->get_bsph_center();
244 float rad = m->get_bsph_radius();
246 PointLight *lt = new PointLight(center);
247 lt->set_radius(rad);
249 lt->set_color(m->get_material().kd);
251 return lt;
252 }