bool BBWater::init() { Lib3dsFile* lib3dsFile = lib3ds_file_open(CCFileUtils::sharedFileUtils()->fullPathForFilename("effects/sea.3DS").c_str()); if (lib3dsFile) { for (int i=0; i < lib3dsFile->nmeshes; i++) { Lib3dsMesh* mesh = lib3dsFile->meshes[i]; m_faceCount += mesh->nfaces; for (int j=0; j < mesh->nfaces; j++) { // if (j >= 50) { // break; // } Lib3dsFace& face = mesh->faces[j]; for (int k=0; k < 3; k++) { int p0 = face.index[0]; int p1 = face.index[1]; int p2 = face.index[2]; vertexArray.push_back(mesh->vertices[p0][0]); vertexArray.push_back(mesh->vertices[p0][1]); vertexArray.push_back(mesh->vertices[p0][2]); vertexArray.push_back(mesh->vertices[p1][0]); vertexArray.push_back(mesh->vertices[p1][1]); vertexArray.push_back(mesh->vertices[p1][2]); vertexArray.push_back(mesh->vertices[p2][0]); vertexArray.push_back(mesh->vertices[p2][1]); vertexArray.push_back(mesh->vertices[p2][2]); } } } // glGenBuffers(1, &m_vertexVbo); // glBindBuffer(GL_ARRAY_BUFFER, m_vertexVbo); // glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertexArray.size(), &vertexArray[0], GL_STATIC_DRAW); } m_sprite = CCSprite::create("effects/water_normals.png"); m_sprite->retain(); setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor)); setContentSize(CCSizeMake(1024, 1024)); setPosition(CCPointMake(-500, -1000)); return true; }
int main(int argc, char **argv) { Lib3dsFile *f; parse_args(argc, argv); f = lib3ds_file_open(input); if (!f) { fprintf(stderr, "***ERROR***\nLoading file failed: %s\n", input); exit(1); } if (mtl_file) { FILE *mtl = fopen(mtl_file, "wt"); if (!mtl) { fprintf(stderr, "***ERROR***\nCreating output file failed: %s\n", mtl_file); exit(1); } write_mtl(mtl, f); fclose(mtl); } { FILE *obj = fopen(obj_file, "wt"); if (!obj) { fprintf(stderr, "***ERROR***\nCreating output file failed: %s\n", obj_file); exit(1); } if (!f->nodes) lib3ds_file_create_nodes_for_meshes(f); lib3ds_file_eval(f, 0); fprintf(obj, "# Wavefront OBJ file\n"); fprintf(obj, "# Converted by 3ds2obj\n"); fprintf(obj, "# http://www.lib3ds.org\n\n"); if (mtl_file) { fprintf(obj, "mtllib %s\n", mtl_file); } write_nodes(obj, f, f->nodes); fclose(obj); } lib3ds_file_free(f); return 0; }
Pointer<Scene> Loader3Ds::read(const char *path) { Pointer<Scene> pScene; char pathBuf[100]; strcpy(pathBuf,path); char *const lastSlash = strrchr(pathBuf,'/'); const char* modPath = Core::Inst()->mResMan->modPath(path); Lib3dsFile *const f3d = lib3ds_file_open(modPath); if(!f3d) return pScene; int i; LoaderTGA loadTGA; pScene.alloc(); for(i=0; i<f3d->nmaterials; ++i) { Pointer<Material> pMat = new Material(); pScene->mMaterials.insert(pMat); const Lib3dsMaterial &m = *f3d->materials[i]; pMat->diffuse = glm::vec4( m.diffuse[0], m.diffuse[1], m.diffuse[2], 1.f ); pMat->specular = glm::vec4( m.specular[0],m.specular[1],m.specular[2],1.f ); pMat->glossiness = 100.f * m.shininess; if( m.texture1_map.name[0] ) { strcpy((lastSlash ? lastSlash+1 : pathBuf), m.texture1_map.name ); pMat->pTexture = loadTGA.read(pathBuf); } if(! pMat->pTexture ) pMat->pTexture = defColorTex; } for(i=0; i<f3d->nmeshes; ++i) { Lib3dsMesh *const lm = f3d->meshes[i]; normalize(*lm); qsort( lm->faces, lm->nfaces, sizeof(Lib3dsFace), Comparator::faces ); const Pointer< Array<VertexData> > pUnrolled = unroll_vertices(lm); Pointer<Mesh> pMesh, pOrig = (doSquash ? upload_data_squashed(*pUnrolled) : upload_data(*pUnrolled)); if(doTransform) pOrig->pNode.alloc()->local = *reinterpret_cast<glm::mat4*>( lm->matrix ); //read face data int j, prev = -1,start=0; for(j=0; j<lm->nfaces; ++j) { const int cur = lm->faces[j].material; if(cur == prev) continue; if( pMesh.get() ) pMesh->setup(GL_TRIANGLES,3*start,3*j); List< Pointer<Material> > *pl = &pScene->mMaterials; for(int counter=cur; pl && --counter>=0; pl=pl->next.get()); assert(pl); pMesh = new Mesh(*pOrig); pScene->mMeshes.insert(pMesh); pMesh->pMaterial = pl->data; start=j; prev=cur; } if(pMesh.get()) pMesh->setup(GL_TRIANGLES,3*start,3*j); } for(i=0; i<f3d->ncameras; ++i) { Pointer<Camera> pCam = new Camera(); pScene->mCameras.insert(pCam); const Lib3dsCamera &cam = *f3d->cameras[i]; pCam->near = cam.near_range; if(pCam->near < 0.01f) { //invalid near plane value! pCam->near = 1.f; } pCam->far = cam.far_range; pCam->fov = cam.fov; const Pointer<Node> &pn = pCam->pNode = new Node(); pn->local = makeSpatial( cam.position, cam.target, cam.roll ); } for(i=0; i<f3d->nlights; ++i) { Pointer<Light> pLit = new Light(); pScene->mLights.insert(pLit); const Lib3dsLight &lit = *f3d->lights[i]; pLit->near = lit.inner_range; pLit->far = lit.outer_range; pLit->fov = lit.falloff; pLit->hotspot = lit.hotspot / lit.falloff; pLit->attenuation = lit.attenuation; pLit->color = lit.multiplier * glm::vec4( lit.color[0], lit.color[1], lit.color[2], 1.f ); const Pointer<Node> &pn = pLit->pNode = new Node(); pn->local = makeSpatial( lit.position, lit.target, lit.roll ); } return pScene; }
/*! * Load the model from .3ds file. */ static void load_model(void) { file = lib3ds_file_open(filepath); if (!file) { puts("3dsplayer: Error: Loading 3DS file failed.\n"); exit(1); } /* No nodes? Fabricate nodes to display all the meshes. */ if (!file->nodes) { Lib3dsNode *node; int i; for (i = 0; i < file->nmeshes; ++i) { Lib3dsMesh *mesh = file->meshes[i]; node = lib3ds_node_new(LIB3DS_NODE_MESH_INSTANCE); strcpy(node->name, mesh->name); lib3ds_file_insert_node(file, node, NULL); } } lib3ds_file_eval(file, 0.0f); lib3ds_file_bounding_box_of_nodes(file, 1, 0, 0, bmin, bmax, NULL); sx = bmax[0] - bmin[0]; sy = bmax[1] - bmin[1]; sz = bmax[2] - bmin[2]; size = MAX(sx, sy); size = MAX(size, sz); cx = (bmin[0] + bmax[0]) / 2; cy = (bmin[1] + bmax[1]) / 2; cz = (bmin[2] + bmax[2]) / 2; /* No cameras in the file? Add four */ if (!file->ncameras) { /* Add some cameras that encompass the bounding box */ Lib3dsCamera *camera = lib3ds_camera_new("Camera_X"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + 1.5 * MAX(sy, sz); camera->near_range = (camera->position[0] - bmax[0]) * .5; camera->far_range = (camera->position[0] - bmin[0]) * 2; lib3ds_file_insert_camera(file, camera, -1); /* Since lib3ds considers +Y to be into the screen, we'll put * this camera on the -Y axis, looking in the +Y direction. */ camera = lib3ds_camera_new("Camera_Y"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[1] = bmin[1] - 1.5 * MAX(sx, sz); camera->near_range = (bmin[1] - camera->position[1]) * .5; camera->far_range = (bmax[1] - camera->position[1]) * 2; lib3ds_file_insert_camera(file, camera, -1); camera = lib3ds_camera_new("Camera_Z"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[2] = bmax[2] + 1.5 * MAX(sx, sy); camera->near_range = (camera->position[2] - bmax[2]) * .5; camera->far_range = (camera->position[2] - bmin[2]) * 2; lib3ds_file_insert_camera(file, camera, -1); camera = lib3ds_camera_new("Camera_ISO"); camera->target[0] = cx; camera->target[1] = cy; camera->target[2] = cz; memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + .75 * size; camera->position[1] = bmin[1] - .75 * size; camera->position[2] = bmax[2] + .75 * size; camera->near_range = (camera->position[0] - bmax[0]) * .5; camera->far_range = (camera->position[0] - bmin[0]) * 3; lib3ds_file_insert_camera(file, camera, -1); } /* No lights in the file? Add some. */ if (!file->nlights) { Lib3dsLight *light; light = lib3ds_light_new("light0"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .6; light->position[0] = cx + size * .75; light->position[1] = cy - size * 1.; light->position[2] = cz + size * 1.5; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light, -1); light = lib3ds_light_new("light1"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx - size; light->position[1] = cy - size; light->position[2] = cz + size * .75; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light, -1); light = lib3ds_light_new("light2"); light->spot_light = 0; light->see_cone = 0; light->color[0] = light->color[1] = light->color[2] = .3; light->position[0] = cx; light->position[1] = cy + size; light->position[2] = cz + size; light->position[3] = 0.; light->outer_range = 100; light->inner_range = 10; light->multiplier = 1; lib3ds_file_insert_light(file, light, -1); } camera = file->cameras[0]->name; lib3ds_file_eval(file, 0); }