dungeon_crawler

view prototype/src/tile.cc @ 41:acfe0c0110fc

- cleaned up the renderer - implemented fallback (non-deferred renderer)
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Aug 2012 05:35:00 +0300
parents 862461b686f4
children dfd3a413ef9e
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 bool Tile::load(const char *fname)
38 {
39 if(!fname) {
40 return false;
41 }
43 char *saved_fname = (char*)alloca(strlen(fname) + 1);
44 strcpy(saved_fname, fname);
46 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
47 aiProcess_CalcTangentSpace |
48 aiProcess_Triangulate |
49 aiProcess_SortByPType |
50 aiProcess_FlipUVs;
52 const aiScene *scn = aiImportFile(fname, proc_flags);
53 if(!scn) {
54 fprintf(stderr, "failed to load tile: %s\n", fname);
55 return -1;
56 }
58 map<aiMesh*, aiNode*> nodemap;
59 build_nodemap(&nodemap, scn, scn->mRootNode);
61 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
62 ass_obj_hack = true;
63 } else {
64 ass_obj_hack = false;
65 }
67 //load_lights(scn);
68 load_meshes(scn, nodemap);
70 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
72 aiReleaseImport(scn);
73 return true;
74 }
76 void Tile::update(unsigned long msec, float dt)
77 {
78 // TODO particle system update
79 }
81 void Tile::draw(unsigned int draw_mask) const
82 {
83 for(size_t i=0; i<meshes.size(); i++) {
84 if(mesh_side[i] & draw_mask) {
85 meshes[i]->draw();
86 }
87 }
88 }
90 void Tile::draw_lights(unsigned int draw_mask) const
91 {
92 for(size_t i=0; i<lights.size(); i++) {
93 if(light_side[i] & draw_mask) {
94 lights[i]->draw();
95 }
96 }
97 }
99 /*
100 int Tile::load_lights(const aiScene *scn)
101 {
102 int count = 0;
104 for(int i=0; i<(int)scn->mNumLights; i++) {
105 Light *lt;
106 aiLight *ailt = scn->mLights[i];
108 switch(ailt->mType) {
109 case aiLightSource_POINT:
110 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
111 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
112 ailt->mAttenuationQuadratic);
113 break;
115 case aiLightSource_DIRECTIONAL:
116 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
117 break;
119 default:
120 continue;
121 }
123 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
125 lights.push_back(lt);
126 count++;
127 }
129 return count;
130 }
131 */
133 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
134 {
135 int count = 0;
137 int attr_loc = rend->get_tangent_location();
138 if(attr_loc == -1) {
139 fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
140 }
142 for(int i=0; i<(int)scn->mNumMeshes; i++) {
143 // ignore any lines or other crap
144 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
145 continue;
146 }
148 Mesh *mesh = new Mesh;
149 if(!mesh->create(scn, scn->mMeshes[i])) {
150 delete mesh;
151 continue;
152 }
153 if(attr_loc != -1) {
154 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
155 }
157 Material mat;
158 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
159 mesh->set_material(mat);
161 // retrieve the node pointer
162 const char *name = "<unknown>";
164 auto iter = nmap.find(scn->mMeshes[i]);
165 if(iter != nmap.end()) {
166 aiNode *node = iter->second;
168 Matrix4x4 xform;
169 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
170 xform = *(Matrix4x4*)&node->mTransformation;
171 mesh->set_xform(xform);
173 name = node->mName.data;
174 mesh->set_name(name);
175 }
177 // find which side is this mesh on
178 unsigned int side = 0;
179 if(strstr(name, "NORTH")) {
180 side |= TILE_NORTH;
181 }
182 if(strstr(name, "SOUTH")) {
183 side |= TILE_SOUTH;
184 }
185 if(strstr(name, "EAST")) {
186 side |= TILE_EAST;
187 }
188 if(strstr(name, "WEST")) {
189 side |= TILE_WEST;
190 }
191 if(!side) {
192 side = TILE_ALL;
193 }
195 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
196 // and destroy the mesh...
197 if(strstr(name, "LIGHT")) {
198 PointLight *lt = mesh_to_light(mesh);
199 if(!lt) {
200 fprintf(stderr, "failed to convert mesh %s to light\n", name);
201 } else {
202 lights.push_back(lt);
203 light_side.push_back(side);
204 }
205 delete mesh;
207 } else {
208 meshes.push_back(mesh);
209 mesh_side.push_back(side);
210 count++;
211 }
212 }
213 return count;
214 }
216 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
217 {
218 unsigned int i;
220 for(i=0; i<node->mNumMeshes; i++) {
221 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
223 (*nmap)[m] = node;
224 }
226 for(i=0; i<node->mNumChildren; i++) {
227 build_nodemap(nmap, scn, node->mChildren[i]);
228 }
229 }
231 static PointLight *mesh_to_light(Mesh *m)
232 {
233 Vector3 center = m->get_bsph_center();
234 float rad = m->get_bsph_radius();
236 PointLight *lt = new PointLight(center);
237 lt->set_radius(rad);
239 lt->set_color(m->get_material().kd);
241 return lt;
242 }