nuclear@1: #include nuclear@1: #include "opengl.h" nuclear@4: #include nuclear@4: #include nuclear@4: #include nuclear@1: #include "tile.h" nuclear@1: 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@3: aiProcess_PreTransformVertices | aiProcess_Triangulate | nuclear@3: aiProcess_GenNormals | aiProcess_SortByPType | 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@3: load_lights(scn); nuclear@3: nuclear@3: load_meshes(scn); 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@4: int Tile::load_meshes(const aiScene *scn) 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@4: meshes.push_back(mesh); nuclear@4: nuclear@4: // find which side is this mesh on nuclear@4: const char *name = mesh->get_name(); nuclear@5: printf("name: %s ... ", name); nuclear@4: unsigned int side; nuclear@4: nuclear@4: if(strstr(name, "NORTH") == name) { nuclear@4: side = TILE_NORTH; nuclear@4: } else if(strstr(name, "SOUTH") == name) { nuclear@4: side = TILE_SOUTH; nuclear@4: } else if(strstr(name, "EAST") == name) { nuclear@4: side = TILE_EAST; nuclear@4: } else if(strstr(name, "WEST") == name) { nuclear@4: side = TILE_WEST; nuclear@4: } else { nuclear@4: side = TILE_ALL; nuclear@4: } nuclear@5: printf("side: %x\n", side); nuclear@4: mesh_side.push_back(side); nuclear@4: nuclear@4: count++; nuclear@4: } nuclear@4: return count; nuclear@4: }