bool SceneImporter<real>::Import( const std::string& filename, Scene<real>& oScene )
{
	std::ifstream file( filename.c_str() );

	mCurrentLine = 0;
	mCurrentScene = &oScene;

	// Open the file
	if ( !file )
	{
		Log::Get() << "SceneImporter error: Scene file not found, " << filename << Log::EndLine;
		return false;
	}

	// Read the scene
	try
	{
		mCurrentLine = 1;
		ReadScene( file, oScene );
	}
	catch( const char* exception )
	{
		Log::Get() << "SceneImporter error, line " << mCurrentLine << ": " << std::string( exception ) << Log::EndLine;
		return false;
	}

	Log::Get() << "SceneImporter: Scene successfully parsed" << Log::EndLine;

	return true;
}
int 
main(int argc, char **argv)
{
  // Parse program arguments
  if (!ParseArgs(argc, argv)) exit(1);

  // Initialize GLUT
  GLUTInit(&argc, argv);

  // Read scene
  scene = ReadScene(input_scene_name);
  if (!scene) exit(-1);

  // Run GLUT interface
  GLUTMainLoop();

  // Return success 
  return 0;
}
Exemple #3
0
int main(int argc, char **argv)
{
  // Parse program arguments
  if (!ParseArgs(argc, argv)) exit(-1);

  // Read scene
  scene = ReadScene(input_scene_name);
  if (!scene) exit(-1);

  // Check output image file
  if (output_image_name) {
    // Set scene viewport
    scene->SetViewport(R2Viewport(0, 0, render_image_width, render_image_height));

    // Render image
    R2Image *image = RenderImage(scene, render_image_width, render_image_height, print_verbose);
    if (!image) exit(-1);

    // Write image
    if (!WriteImage(image, output_image_name)) exit(-1);

    // Delete image
    delete image;
  }
  else {
    // Initialize GLUT
    GLUTInit(&argc, argv);

    // Create viewer
    viewer = new R3Viewer(scene->Viewer());
    if (!viewer) exit(-1);

    // Run GLUT interface
    GLUTMainLoop();

    // Delete viewer (doesn't ever get here)
    delete viewer;
  }

  // Return success
  return 0;
}
Exemple #4
0
void ColladaParser::Parse(const char* colladaFileData)
{
	XmlParser p;
	p.Parse(colladaFileData);

	if (p.root.tagName != "COLLADA")
		throw "expected COLLADA as root element";

	if (!p.root.HasAttribute("xmlns", "http://www.collada.org/2005/11/COLLADASchema") || !p.root.HasAttribute("version", "1.4.1"))
		throw "expected COLLADA as root element";

	XmlElement* root = &p.root;

	{
		auto v = root->Child("library_geometries")->Childs("geometry");

		for (int ia = 0; ia < v.size(); ia++)
			ReadGeometry(v[ia]);
	}

	if (root->HasChild("library_animations"))
	{
		auto v = root->Child("library_animations")->Childs("animation");

		for (int ia = 0; ia < v.size(); ia++)
			ReadAnimations(v[ia]);
	}

	{
		auto v = root->Child("library_visual_scenes")->Childs("visual_scene");

		for (int ia = 0; ia < v.size(); ia++)
		{
			ReadScene(v[ia]);
		}
	}

	auto sceneId = ReadId(root->Child("scene")->Child("instance_visual_scene")->Attribute("url"));
	currentScene = &scenes[sceneId];
	int x = 2;
}
Exemple #5
0
bool bgParserASE::Read()
{
	bool hr = true;

	TCHAR szWordArray[2][MAX_PATH * 4];

	// m_ASE 데이터 초기화
	ZeroMemory(&m_pModel->m_Scene, sizeof(SceneInfo));
	m_pModel->m_MaterialList.clear();
	m_pModel->m_ObjectList.clear();

	// 파일 앞부분 1회만 등장하는 섹션 읽기
	IF_FALSE_RETURN(FindWord(_T("*SCENE")));
	IF_FALSE_RETURN(ReadScene());
	IF_FALSE_RETURN(FindWord(_T("*MATERIAL_LIST")));
	IF_FALSE_RETURN(ReadMaterial());

	// 여러번 등장하는 섹션 반복해서 읽기
	_tcscpy(szWordArray[0], _T("*GEOMOBJECT"));
	_tcscpy(szWordArray[1], _T("*HELPEROBJECT"));
	while (!feof(m_pFile))
	{
		switch (FindWordArray(szWordArray, 2))
		{
		case 0:	IF_FALSE_RETURN(ReadGeomObject());		break;
		case 1:	IF_FALSE_RETURN(ReadHelperObject());	break;
		case -1:	// 찾는 단어 없음 (파일의 끝)
		{
			ConvertToModel();	// 읽은 데이터를 모델용 데이터로 컨버팅
			LinkNode();			// 노드 관계 연결
			OperationTM();		// 노드 관계에 따른 행렬 연산
		}
		break;
		default:	// 나머지 (배열 요소가 2개이므로 나올 수 없음)
			return false;
			break;
		}
	}

	return hr;
}
int 
main(int argc, char **argv)
{
  // Look for help
  for (int i = 0; i < argc; i++) {
    if (!strcmp(argv[i], "-help")) {
      ShowUsage();
    }
  }

  // Read input and output filenames
  if (argc < 3)  ShowUsage();
  argv++, argc--; // First argument is program name
  char *input_scene_name = *argv; argv++, argc--; 
  char *output_image_name = *argv; argv++, argc--; 

  // Initialize arguments to default values
  int width = 256;
  int height = 256;
  int max_depth = 0;
  int num_distributed_rays_per_intersection = 0;
  int num_primary_rays_per_pixel = 1;
	bool hard_shadow = false;

  // Parse arguments 
  while (argc > 0) {
    if (!strcmp(*argv, "-width")) {
      CheckOption(*argv, argc, 2);
      width = atoi(argv[1]);
      argv += 2, argc -= 2;
    }
    else if (!strcmp(*argv, "-height")) {
      CheckOption(*argv, argc, 2);
      height = atoi(argv[1]);
      argv += 2, argc -= 2;
    }
    else if (!strcmp(*argv, "-max_depth")) {
      CheckOption(*argv, argc, 2);
      max_depth = atoi(argv[1]);
      argv += 2, argc -= 2;
    }
    else if (!strcmp(*argv, "-antialias")) {
      CheckOption(*argv, argc, 2);
      num_primary_rays_per_pixel = atoi(argv[1]);
      argv += 2, argc -= 2;
    }
    else if (!strcmp(*argv, "-distribute")) {
      CheckOption(*argv, argc, 2);
      num_distributed_rays_per_intersection = atoi(argv[1]);
      argv += 2, argc -= 2;
    }
		else if (!strcmp(*argv, "-hard_shadow")) {
			hard_shadow = true;
			argv += 1, argc -= 1;
		}
    else {
      // Unrecognized program argument
      fprintf(stderr,  "meshpro: invalid option: %s\n", *argv);
      ShowUsage();
    }
  }

  // Read scene
  R3Scene *scene = ReadScene(input_scene_name, width, height);
  if (!scene) {
    fprintf(stderr, "Unable to read scene from %s\n", input_scene_name);
    exit(-1);
  }

  // Render image
  R2Image *image = RenderImage(scene, width, height, max_depth, 
    num_primary_rays_per_pixel, num_distributed_rays_per_intersection, hard_shadow);
  if (!image) {
    fprintf(stderr, "Did not render image from scene\n");
    exit(-1);
  }

  // Transfer the image to sRGB color space: for Windows + Linux and Mac OS X
  // 10.6+ (for earlier MAC OS X it will look slightly too bright, but not as
  // much as it would be too dark otherwise. This function also clamps the 
  // image values; however, it does not scale the brightness and also does not
  // perform any more complicated tone mapping
  image->TosRGB();

  // Write output image
  if (!image->Write(output_image_name)) {
    fprintf(stderr, "Did not write image to %s\n", output_image_name);
    exit(-1);
  }

  // Delete everything
  delete scene;
  delete image;

  // Return success
  return EXIT_SUCCESS;
}
Exemple #7
0
CBool CScene::Load( CChar * fileName, CBool reportWarningsAndErrors, CBool readFromBuffer )
{
	if ( fileName == NULL )
		return CFalse; 
	CChar * nameOnly = GetAfterPath( fileName );
	SetName( nameOnly );

	// Instantiate the reference implementation
	m_collada = new DAE;
	PrintInfo( "\n" );
	PrintInfo( _T("\n========Importing new external scene========"), COLOR_BLUE ); 
	numErrors = numWarnings = 0;
	domCOLLADA *dom;
	if( readFromBuffer )
	{
		/////////////////////////////////////////////////////////////////
		//zip path
		CChar zipPath[MAX_NAME_SIZE];
		Cpy( zipPath, fileName );
		GetWithoutDot( zipPath );
		Append(zipPath, ".zip" );

		//file name inside zip file
		CChar fileNameInZip[MAX_NAME_SIZE];
		Cpy( fileNameInZip, GetAfterPath( fileName ) );
		//Uncompress zip file
		std::string buffer = ReadZipFile( zipPath, fileNameInZip );
		//res = m_collada->load( "", buffer.c_str() );
		dom = m_collada->openFromMemory( "", buffer.c_str() );
		///////////////////////////////////////////////////////////////
	}
	else
	{
		// load with full path 
		//res = m_collada->load( fileName );
		dom = m_collada->open(fileName);
	}

	//if (res != DAE_OK)
	//{
	//	PrintInfo(_T("\nCScene::Load > Error loading the COLLADA file:") + CString(fileName), COLOR_RED );
	//	delete m_collada;
	//	m_collada = NULL;

		//if( reportWarningsAndErrors )
		//{
		//	char tempReport[MAX_NAME_SIZE];;
		//	sprintf( tempReport, "%s - fatal error (s)", nameOnly );
		//	PrintInfo2( tempReport, COLOR_RED ); 
		//}

	//	return CFalse;
	//}
	PrintInfo( _T("\nCOLLADA_DOM Runtime database initialized from" ) );
	PrintInfo( _T("'") +  CString( fileName ), COLOR_RED_GREEN );
	//PrintInfo( _T("nameOnly: '") + (CString)nameOnly + _T( "'\n" ) );

	//domCOLLADA *dom = m_collada->getDom(nameOnly);
	//if ( !dom )
	//	dom = m_collada->getDom( fileName); 
	if ( !dom )
	{
		PrintInfo( _T("\nCScene::Load > COLLADA File loaded to the dom, but query for the dom assets failed "), COLOR_RED );
		PrintInfo( _T("\nCScene::Load > COLLADA Load Aborted! "), COLOR_RED );
		delete m_collada;	
		m_collada = NULL;
		if( reportWarningsAndErrors )
		{
			char tempReport[MAX_NAME_SIZE];;
			sprintf( tempReport, "\n%s - Fatal error (s)", nameOnly );
			PrintInfo( tempReport, COLOR_RED ); 
		}
		return CFalse; 
	}

	CInt ret = 0;
	//PrintInfo("Begin Conditioning\n");
	//ret = kmzcleanup(m_collada, true);
	//if (ret)
	//	PrintInfo("kmzcleanup complete\n");
	ret = Triangulate(m_collada);
	if (ret)
		PrintInfo("\nTriangulate complete");
	//ret = deindexer(m_collada);
	//if (ret)
	//	PrintInfo("deindexer complete\n");

	//PrintInfo("Finish Conditioning\n");

	// Need to now get the asset tag which will determine what vector x y or z is up.  Typically y or z. 
	if ( dom->getAsset()->getUp_axis() )
	{
		domAsset::domUp_axis *up = dom->getAsset()->getUp_axis();
		switch( up->getValue() )
		{
			case UPAXISTYPE_X_UP:
				PrintInfo(_T("\nWarning!X is Up Data and Hiearchies must be converted!") ); 
				PrintInfo(_T("\nConversion to X axis Up isn't currently supported!") ); 
				PrintInfo(_T("\nCOLLADA defaulting to Y Up") ); 
				numWarnings +=1;
				//CRender.SetUpAxis(eCYUp); 
				break; 
			case UPAXISTYPE_Y_UP:
				PrintInfo( _T("\nY Axis is Up for this file...COLLADA set to Y Up ") ); 
				//CRender.SetUpAxis(eCYUp); 
				break;
			case UPAXISTYPE_Z_UP:
				PrintInfo( _T("\nZ Axis is Up for this file ") ); 
				PrintInfo( _T("\nAll Geometries and Hiearchies converted!"), COLOR_YELLOW ) ; 
				numWarnings +=1;
				//CRender.SetUpAxis(eCZUp); 
				break; 
			default:
				break; 
		}
	}
	strcpy( m_fileName, fileName ); 
	strcpy( m_pureFileName, nameOnly ); 

	// Load all the image libraries
	//for ( CUInt i = 0; i < dom->getLibrary_images_array().getCount(); i++)
	//{
	//	ReadImageLibrary( dom->getLibrary_images_array()[i] );			
	//}

	CBool success = CFalse;

	/*
	CChar *cfxBinFilename = ReadCfxBinaryFilename( dom->getExtra_array() );
	
	if ( cfxBinFilename != NULL ) 
	{
		cfxLoader::setBinaryLoadRemotePath( BasePath );
		success = (CBool) cfxLoader::loadMaterialsAndEffectsFromBinFile(cfxBinFilename, cfxMaterials, cfxEffects, cgContext);
		assert(success);
	}
	else
	{
	*/
		//success = ( CBool ) cfxLoader::loadMaterialsAndEffects( m_collada, m_cfxMaterials, m_cfxEffects, m_cgContext );
		//assert(success);
	/*}*/
	
	// Load all the effect libraries
	//for ( CUInt i = 0; i < dom->getLibrary_effects_array().getCount(); i++)
	//{
	//	ReadEffectLibrary( dom->getLibrary_effects_array()[i] );			
	//}

	//// Load all the material libraries
	//for ( CUInt i = 0; i < dom->getLibrary_materials_array().getCount(); i++)
	//{
 //		ReadMaterialLibrary( dom->getLibrary_materials_array()[i] );			
	//}

	// Load all the animation libraries
	for ( CUInt i = 0; i < dom->getLibrary_animations_array().getCount(); i++)
	{
		ReadAnimationLibrary( dom->getLibrary_animations_array()[i] );		
		m_hasAnimation = CTrue;
	}
	//Load all animation clips
	for ( CUInt i = 0; i < dom->getLibrary_animation_clips_array().getCount(); i++)
	{
		ReadAnimationClipLibrary( dom->getLibrary_animation_clips_array()[i] );			
	}

	//If there's no clip in COLLADA file, try to create a default clip
	if( m_numClips == 0 && dom->getLibrary_animations_array().getCount() > 0 )
	{
		//Create a default clip and attach all the animations to this clip
		PrintInfo( "\nAdding default animation clip..." );
		CAnimationClip * newAnimClip = CNew(CAnimationClip); 
		//CAssert("No memory\n", newAnimClip!=NULL);
		newAnimClip->SetName( "defaultClip" );
		newAnimClip->SetIndex(0);
		newAnimClip->SetStart(0.0);
		CFloat endTime = 0.0;
		for(CUInt i=0; i<m_animations.size(); i++)
		{
			CAnimation * anim = m_animations[i];

			PrintInfo( "\nAttaching animation ' ", COLOR_WHITE );PrintInfo( anim->GetName(), COLOR_RED_GREEN );
			PrintInfo( " ' to the default animation clip", COLOR_WHITE  );
			
			anim->SetClipTarget( newAnimClip );
			newAnimClip->m_animations.push_back( anim );
			if( anim->GetEndTime() > endTime )
				endTime = anim->GetEndTime();
		}
		newAnimClip->SetEnd((CDouble)endTime);
		m_numClips = 1;
		m_animationClips.push_back( newAnimClip );
	}

	// Find the scene we want
	daeElement* defaultScene = dom->getScene()->getInstance_visual_scene()->getUrl().getElement();
	domAsset::domUp_axis *up = dom->getAsset()->getUp_axis();

	switch( up->getValue() )
	{
		case UPAXISTYPE_X_UP:
			upAxis = eCXUp;
			break; 
		case UPAXISTYPE_Y_UP:
			upAxis = eCYUp;
			break;
		case UPAXISTYPE_Z_UP:
			upAxis = eCZUp;
			break; 
		default:
			break; 
	}

	if(defaultScene)
		ReadScene( (domVisual_scene *)defaultScene, upAxis );
	
	if (m_collada)
	{
		delete m_collada;
		m_collada = 0;
	}
	if( reportWarningsAndErrors )
	{
		char tempReport[MAX_NAME_SIZE];
		COLORREF color;
		if( numErrors > 0 )
			color = COLOR_RED;
		else if (numWarnings > 0 )
			color = COLOR_YELLOW;
		else
			color = COLOR_GREEN;
		sprintf( tempReport, "\n%s - %i error (s), %i warning (s)", nameOnly, numErrors, numWarnings );

		PrintInfo( tempReport, color );

		totalErrors += numErrors;
		totalWarnings += numWarnings;
	}
	return CTrue;
}