nuclear@1: #include nuclear@7: #include nuclear@1: #include "opengl.h" nuclear@7: #include nuclear@7: #include nuclear@7: #include nuclear@1: #include "tile.h" nuclear@11: #include "tileset.h" nuclear@1: nuclear@7: using std::map; nuclear@7: nuclear@7: static void build_nodemap(map *nmap, const aiScene *scn, aiNode *node); nuclear@7: nuclear@11: Tile::Tile(TileSet *tileset) nuclear@11: { nuclear@11: tset = tileset; nuclear@11: } nuclear@7: nuclear@1: bool Tile::load(const char *fname) nuclear@1: { nuclear@5: if(!fname) { nuclear@5: return false; nuclear@5: } nuclear@5: nuclear@3: unsigned int proc_flags = aiProcess_JoinIdenticalVertices | nuclear@7: aiProcess_Triangulate | nuclear@7: aiProcess_SortByPType | nuclear@7: aiProcess_FlipUVs; nuclear@4: nuclear@3: const aiScene *scn = aiImportFile(fname, proc_flags); nuclear@3: if(!scn) { nuclear@3: fprintf(stderr, "failed to load tile: %s\n", fname); nuclear@3: return -1; nuclear@3: } nuclear@3: nuclear@7: map nodemap; nuclear@7: build_nodemap(&nodemap, scn, scn->mRootNode); nuclear@7: nuclear@3: load_lights(scn); nuclear@7: load_meshes(scn, nodemap); nuclear@3: nuclear@3: printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights); nuclear@1: return true; nuclear@1: } nuclear@1: nuclear@5: void Tile::draw(unsigned int draw_mask) const nuclear@1: { nuclear@4: for(size_t i=0; idraw(); nuclear@4: } nuclear@4: } nuclear@4: } nuclear@1: nuclear@4: int Tile::load_lights(const aiScene *scn) nuclear@4: { nuclear@4: int count = 0; nuclear@4: nuclear@4: for(int i=0; i<(int)scn->mNumLights; i++) { nuclear@4: Light *lt; nuclear@4: aiLight *ailt = scn->mLights[i]; nuclear@4: nuclear@4: switch(ailt->mType) { nuclear@4: case aiLightSource_POINT: nuclear@4: lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z)); nuclear@4: ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear, nuclear@4: ailt->mAttenuationQuadratic); nuclear@4: break; nuclear@4: nuclear@4: case aiLightSource_DIRECTIONAL: nuclear@4: lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z)); nuclear@4: break; nuclear@4: nuclear@4: default: nuclear@4: continue; nuclear@4: } nuclear@4: nuclear@4: lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0)); nuclear@4: nuclear@4: lights.push_back(lt); nuclear@4: count++; nuclear@4: } nuclear@4: nuclear@4: return count; nuclear@1: } nuclear@4: nuclear@7: int Tile::load_meshes(const aiScene *scn, const std::map &nmap) nuclear@4: { nuclear@4: int count = 0; nuclear@4: nuclear@4: for(int i=0; i<(int)scn->mNumMeshes; i++) { nuclear@4: Mesh *mesh = new Mesh; nuclear@4: if(!mesh->create(scn, scn->mMeshes[i])) { nuclear@4: delete mesh; nuclear@4: continue; nuclear@4: } nuclear@4: nuclear@11: Material mat; nuclear@11: mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures()); nuclear@11: mesh->set_material(mat); nuclear@11: nuclear@7: // retrieve the node pointer nuclear@7: const char *name = ""; nuclear@7: nuclear@7: auto iter = nmap.find(scn->mMeshes[i]); nuclear@7: if(iter != nmap.end()) { nuclear@7: aiNode *node = iter->second; nuclear@7: nuclear@7: Matrix4x4 xform; nuclear@7: //xform.rotate(Vector3(-M_PI / 2.0, 0, 0)); nuclear@7: xform = *(Matrix4x4*)&node->mTransformation; nuclear@7: mesh->set_xform(xform); nuclear@7: nuclear@7: name = node->mName.data; nuclear@7: mesh->set_name(name); nuclear@7: } nuclear@4: meshes.push_back(mesh); nuclear@4: nuclear@9: printf(" mesh: \"%s\"", name); nuclear@7: nuclear@4: // find which side is this mesh on nuclear@7: unsigned int side = 0; nuclear@7: if(strstr(name, "NORTH")) { nuclear@7: side |= TILE_NORTH; nuclear@7: } nuclear@7: if(strstr(name, "SOUTH")) { nuclear@7: side |= TILE_SOUTH; nuclear@7: } nuclear@7: if(strstr(name, "EAST")) { nuclear@7: side |= TILE_EAST; nuclear@7: } nuclear@7: if(strstr(name, "WEST")) { nuclear@7: side |= TILE_WEST; nuclear@7: } nuclear@7: if(!side) { nuclear@4: side = TILE_ALL; nuclear@4: } nuclear@9: printf(" (0x0%x)\n", side); nuclear@4: mesh_side.push_back(side); nuclear@4: nuclear@4: count++; nuclear@4: } nuclear@4: return count; nuclear@4: } nuclear@7: nuclear@7: static void build_nodemap(map *nmap, const aiScene *scn, aiNode *node) nuclear@7: { nuclear@7: unsigned int i; nuclear@7: nuclear@7: for(i=0; imNumMeshes; i++) { nuclear@7: aiMesh *m = scn->mMeshes[node->mMeshes[i]]; nuclear@7: nuclear@7: (*nmap)[m] = node; nuclear@7: } nuclear@7: nuclear@7: for(i=0; imNumChildren; i++) { nuclear@7: build_nodemap(nmap, scn, node->mChildren[i]); nuclear@7: } nuclear@7: }