dungeon_crawler
view prototype/src/tile.cc @ 37:84a56fb24850
fuck that uninitialized variable
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 29 Aug 2012 01:04:01 +0300 |
parents | d0e93b4d9ec9 |
children | 862461b686f4 |
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 bool Tile::load(const char *fname)
24 {
25 if(!fname) {
26 return false;
27 }
29 char *saved_fname = (char*)alloca(strlen(fname) + 1);
30 strcpy(saved_fname, fname);
32 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
33 aiProcess_CalcTangentSpace |
34 aiProcess_Triangulate |
35 aiProcess_SortByPType |
36 aiProcess_FlipUVs;
38 const aiScene *scn = aiImportFile(fname, proc_flags);
39 if(!scn) {
40 fprintf(stderr, "failed to load tile: %s\n", fname);
41 return -1;
42 }
44 map<aiMesh*, aiNode*> nodemap;
45 build_nodemap(&nodemap, scn, scn->mRootNode);
47 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
48 ass_obj_hack = true;
49 } else {
50 ass_obj_hack = false;
51 }
53 //load_lights(scn);
54 load_meshes(scn, nodemap);
56 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
57 return true;
58 }
60 void Tile::draw(unsigned int draw_mask) const
61 {
62 for(size_t i=0; i<meshes.size(); i++) {
63 if(mesh_side[i] & draw_mask) {
64 meshes[i]->draw();
65 }
66 }
67 }
69 void Tile::draw_lights(unsigned int draw_mask) const
70 {
71 for(size_t i=0; i<lights.size(); i++) {
72 if(light_side[i] & draw_mask) {
73 lights[i]->draw();
74 }
75 }
76 }
78 /*
79 int Tile::load_lights(const aiScene *scn)
80 {
81 int count = 0;
83 for(int i=0; i<(int)scn->mNumLights; i++) {
84 Light *lt;
85 aiLight *ailt = scn->mLights[i];
87 switch(ailt->mType) {
88 case aiLightSource_POINT:
89 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
90 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
91 ailt->mAttenuationQuadratic);
92 break;
94 case aiLightSource_DIRECTIONAL:
95 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
96 break;
98 default:
99 continue;
100 }
102 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
104 lights.push_back(lt);
105 count++;
106 }
108 return count;
109 }
110 */
112 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
113 {
114 int count = 0;
116 int attr_loc = get_tangent_location();
117 if(attr_loc == -1) {
118 fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
119 }
121 for(int i=0; i<(int)scn->mNumMeshes; i++) {
122 // ignore any lines or other crap
123 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
124 continue;
125 }
127 Mesh *mesh = new Mesh;
128 if(!mesh->create(scn, scn->mMeshes[i])) {
129 delete mesh;
130 continue;
131 }
132 if(attr_loc != -1) {
133 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
134 }
136 Material mat;
137 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
138 mesh->set_material(mat);
140 // retrieve the node pointer
141 const char *name = "<unknown>";
143 auto iter = nmap.find(scn->mMeshes[i]);
144 if(iter != nmap.end()) {
145 aiNode *node = iter->second;
147 Matrix4x4 xform;
148 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
149 xform = *(Matrix4x4*)&node->mTransformation;
150 mesh->set_xform(xform);
152 name = node->mName.data;
153 mesh->set_name(name);
154 }
156 // find which side is this mesh on
157 unsigned int side = 0;
158 if(strstr(name, "NORTH")) {
159 side |= TILE_NORTH;
160 }
161 if(strstr(name, "SOUTH")) {
162 side |= TILE_SOUTH;
163 }
164 if(strstr(name, "EAST")) {
165 side |= TILE_EAST;
166 }
167 if(strstr(name, "WEST")) {
168 side |= TILE_WEST;
169 }
170 if(!side) {
171 side = TILE_ALL;
172 }
174 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
175 // and destroy the mesh...
176 if(strstr(name, "LIGHT")) {
177 PointLight *lt = mesh_to_light(mesh);
178 if(!lt) {
179 fprintf(stderr, "failed to convert mesh %s to light\n", name);
180 } else {
181 lights.push_back(lt);
182 light_side.push_back(side);
183 }
184 delete mesh;
186 } else {
187 meshes.push_back(mesh);
188 mesh_side.push_back(side);
189 count++;
190 }
191 }
192 return count;
193 }
195 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
196 {
197 unsigned int i;
199 for(i=0; i<node->mNumMeshes; i++) {
200 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
202 (*nmap)[m] = node;
203 }
205 for(i=0; i<node->mNumChildren; i++) {
206 build_nodemap(nmap, scn, node->mChildren[i]);
207 }
208 }
210 static PointLight *mesh_to_light(Mesh *m)
211 {
212 Vector3 center = m->get_bsph_center();
213 float rad = m->get_bsph_radius();
215 PointLight *lt = new PointLight(center);
216 lt->set_radius(rad);
218 lt->set_color(m->get_material().kd);
220 return lt;
221 }