Esempio n. 1
0
/*!
* Load the model from .3ds file.
*/
static void
load_model(void)
{
  file=lib3ds_file_load(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 )
  {
    Lib3dsMesh *mesh;
    Lib3dsNode *node;

    for(mesh = file->meshes; mesh != NULL; mesh = mesh->next)
    {
      node = lib3ds_node_new_object();
      strcpy(node->name, mesh->name);
      node->parent_id = LIB3DS_NO_PARENT;
      lib3ds_file_insert_node(file, node);
    }
  }

  lib3ds_file_eval(file, 1.0f);
  lib3ds_file_bounding_box_of_nodes(file, LIB3DS_TRUE, LIB3DS_FALSE, LIB3DS_FALSE, bmin, bmax);
  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->cameras ) {

    /* 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);

    /* 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);

    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);

    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);
  }


  /* No lights in the file?  Add some. */

  if (file->lights == NULL)
  {
    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);

    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);

    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);

  }

  if (!file->cameras) {
    fputs("3dsplayer: Error: No Camera found.\n", stderr);
    lib3ds_file_free(file);
    file=0;
    exit(1);
  }
  if (!camera) {
    camera=file->cameras->name;
  }

  lib3ds_file_eval(file,0.);
}
Esempio n. 2
0
// load the model, and if the texture has textures, then apply them on the geometric primitives
void Model::LoadFile(const char *name)
{
	if(glIsEnabled(GL_LIGHTING))
	 lightEnabled = true;
	 else
	 lightEnabled = false;
	filename = name;
	// load file
	file = lib3ds_file_load(filename);
	if(!file) // if we were not able to load the file
	{
		// give some errors
		string event = "Error loading: ";
		event.append(filename);
		cout << event << endl;
		exit(1);
	}
	
//Node:
	/* No nodes?  Fabricate nodes to display all the meshes. */  
	if( !file->nodes )  
	{  
		cout << "No node found"<< endl;
		Lib3dsMesh *mesh;  
		Lib3dsNode *node;  
		
		for(mesh = file->meshes; mesh != NULL; mesh = mesh->next)  
		{  
			node = lib3ds_node_new_object();  
			strcpy(node->name, mesh->name);  
			node->parent_id = LIB3DS_NO_PARENT;  
			lib3ds_file_insert_node(file, node);  
		} 
		//goto Node;
	}  
	
	static Lib3dsVector bmin, bmax;  
	static float    sx, sy, sz, size;   /* bounding box dimensions */  
	static float    cx, cy, cz;     /* bounding box center */  
	
 
	lib3ds_file_bounding_box_of_nodes(file, LIB3DS_TRUE, LIB3DS_FALSE, LIB3DS_FALSE, bmin, bmax);  
	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;  
	
	
	
	
	lib3ds_file_eval(file, 0); // set current frame to 0
	// apply texture to all meshes that have texels
	
	
	if (file->lights == NULL)  
	{  
		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);  
		
		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);  
		
		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);  
		
	}  
	
	
	
	
	
	
	
	
	Lib3dsMesh *mesh;
	for(mesh = file->meshes;mesh != 0;mesh = mesh->next)
	{
		if(mesh->texels) //if there's texels for the mesh
			ApplyTexture(mesh); //then apply texture to it
	}
	if(file->lights) //if we have lights in our model
		CreateLightList();
}
Esempio n. 3
0
/*!
* 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);
}