Ejemplo n.º 1
0
bool slFileObj::Load() {
	Reset();
	ScenePtr = mScene;

	FILE*  infile = fopen(mFilename, "r");
	FileLineNumber = 0;

	if (!infile) {
		fprintf(stderr, "LoadObjFile: Unable to open file: %s\n", mFilename);
		return false;
	}

	char inbuffer[1026];
	while (true) {
		if (!fgets(inbuffer, 1026, infile)) {
			fclose(infile);
			PrintCmdNotSupportedErrors(stderr);
			return true;
		}

		FileLineNumber++;

		char*  findStart = Preparse(inbuffer);
		if (findStart == 0 || (*findStart) == '#') {
			continue;	//Ignore if a comment or a blank line
		}

		bool parseErrorOccurred = false;

		char theCommand[17];
		int scanCode = sscanf(inbuffer, "%16s", theCommand);
		if (scanCode != 1) {
			parseErrorOccurred = true;
		}

		int cmdNum = GetCommandNumber(theCommand);
		if (cmdNum == -1) {
			AddUnsupportedCmd(theCommand);
			continue;
		}

		char*  args = ScanForSecondField(findStart);
		bool ok = true;
		switch (cmdNum) {
			case 0: //'v' command
				{
					vec4*  vertData = Vertices.Add();
					ok = ReadVectorR4Hg(args, vertData);
				}
				break;
			case 1: //"vt" command
				{
					vec2*  texData = TextureCoords.Add();
					ok = ReadTexCoords(args, texData);
				}
				break;
			case 2: //The 'f' command
				{
					ok = ProcessFace(args , mDefaultMat);
				}
				break;
			case 3: //'l' command
				UnsupportedLines();
				break;
			default:
				parseErrorOccurred = true;
				break;
		}

		if (!ok) {
			parseErrorOccurred = true;
		}
	}
}
Ejemplo n.º 2
0
bool NffFileLoader::Load( const char* filename, SceneDescription& theScene )
{
	Reset();
	ScenePtr = &theScene;

	FILE* infile = fopen( filename, "r" );
	FileLineNumber = 0;

	if ( !infile ) {
		fprintf(stderr, "LoadNffFile: Unable to open file: %s\n", filename);
		return false;
	}

	const Material* curMaterial = &Material::Default;

	// Information for view ("v") command
	int viewCmdStatus = false;		// True if currently handling a "v" command
	VectorR3 viewPos;
	VectorR3 lookAtPos;
	VectorR3 upVector;
	double fovy;		// Field of view angle (in radians)
	int screenWidth, screenHeight;
	double hither;

	char inbuffer[1026];
	while ( true ) {
		if ( !fgets( inbuffer, 1026, infile ) ) {
			if ( viewCmdStatus ) {
				SetCameraViewInfo( theScene.GetCameraView(),
						viewPos, lookAtPos, upVector, fovy, 
						screenWidth, screenHeight, hither );
			}
			fclose( infile );
			PrintCmdNotSupportedErrors(stderr);
			return true;
		}
		FileLineNumber++;

		char *findStart = PreparseNff( inbuffer );
		if ( findStart==0 ) {
			// Ignore if a comment or a blank line
			if ( viewCmdStatus ) {
				SetCameraViewInfo( theScene.GetCameraView(),
						viewPos, lookAtPos, upVector, fovy, 
						screenWidth, screenHeight, hither );
				viewCmdStatus = false;
			}
			continue;				
		}

		bool parseErrorOccurred = false;		

		char theCommand[17];
		int scanCode = sscanf( inbuffer, "%16s", theCommand );
		if ( scanCode!=1 ) {
			parseErrorOccurred = true;
		}
		int cmdNum = GetCommandNumber( theCommand );		
		if ( cmdNum==-1 ) {
			AddUnsupportedCmd( theCommand );
			continue;
		}
		if ( viewCmdStatus && cmdNum<8 ) {
			SetCameraViewInfo( theScene.GetCameraView(),
						viewPos, lookAtPos, upVector, fovy, 
						screenWidth, screenHeight, hither );
			viewCmdStatus = false;
		}


		char* args = ObjFileLoader::ScanForSecondField( findStart );
		bool ok = true;
		switch ( cmdNum ) {
		case 0:   // 'v' command
			viewCmdStatus = true;
			break;
		case 1:   // 'b' command - background color
			{
				VectorR3 bgColor;
				scanCode = sscanf( args, "%lf %lf %lf", &(bgColor.x), &(bgColor.y), &(bgColor.z) );
				if ( scanCode!=3 ) {
					ok = false;
					break;
				}
				theScene.SetBackGroundColor( bgColor );
			}
			break;
		case 2:	// 'l' command - positional light
			{
				VectorR3 lightPos, lightColor;
				scanCode = sscanf( args, "%lf %lf %lf %lf %lf %lf", 
											&(lightPos.x), &(lightPos.y), &(lightPos.z),
											&(lightColor.x), &(lightColor.y), &(lightColor.z) );
				if ( scanCode==3 || scanCode==6 ) {
					Light* aLight = new Light();
					aLight->SetPosition( lightPos );
					if ( scanCode==6 ) {
						aLight->SetColor( lightColor );
					}
					theScene.AddLight( aLight );
				}
				else {
					ok = false;
				}
			}
			break;
		case 3:		// 'f' command - material properties
			{
				VectorR3 color;			// Material color
				double Kd, Ks;			// Diffuse and specular components
				double shininess;
				double transmission;	// Transmission coefficient
				double indexOfRefraction;
				scanCode = sscanf( args, "%lf %lf %lf %lf %lf %lf %lf %lf", 
									&color.x, &color.y, &color.z, &Kd, &Ks,
									&shininess, &transmission, &indexOfRefraction );
				if ( scanCode==8 ) {
					Material* mat = new Material();
					theScene.AddMaterial( mat );				// theScene can take of deleting this material
					mat->SetColorAmbientDiffuse( Kd*color );
					mat->SetColorSpecular( Ks*color );
					mat->SetShininess( shininess );
					if ( transmission>0.0 ) {
						mat->SetColorTransmissive( transmission, transmission, transmission );
						mat->SetIndexOfRefraction( indexOfRefraction );
					}
					curMaterial = mat;
				}
				else {
					ok = false;
				}
			}
			break;
		case 4:		// 'c' command - cylinder or cone or truncated cone
			{
				VectorR3 baseCenter;
				VectorR3 topCenter;
				double baseRadius;
				double topRadius;
				scanCode = sscanf( args, "%lf %lf %lf %lf %lf %lf %lf %lf",
									&baseCenter.x, &baseCenter.y, &baseCenter.z, &baseRadius,
									&topCenter.x, &topCenter.y, &topCenter.z, &topRadius );
				if ( scanCode==8 ) {
					ProcessConeCylNFF( baseCenter, baseRadius, topCenter, topRadius );
				}
				else { 
					ok = false;
				}
			}
		case 5:		// 's' command - sphere
			{
				VectorR3 sphereCenter;
				double radius;
				scanCode = sscanf( args, "%lf %lf %lf %lf", &sphereCenter.x, &sphereCenter.y, &sphereCenter.z, &radius );
				if ( scanCode==4 && radius>0.0 ) {
					ViewableSphere* vs = new ViewableSphere( sphereCenter, radius, curMaterial );
					theScene.AddViewable( vs );
				}
				else {
					ok = false;
				}
			}
			break;
		case 7:		// 'pp' command - normals will be ignored
			UnsupportedNormals();
			// Fall thru to 'p' command.
		case 6:		// 'p' command
			{
				int numVerts;
				const int maxNumVerts = 256;
				scanCode = sscanf( args, "%d", &numVerts );
				if (scanCode!=1 || numVerts<3 ) {
					ok = false;
				}
				else if ( numVerts>maxNumVerts ) {
					UnsupportedTooManyVerts( maxNumVerts );
				}
				else {
					ProcessFaceNFF( numVerts, curMaterial, infile );
				}
			}
			break;
		case 8:		// 'from' command
			{
				scanCode = sscanf( args, "%lf %lf %lf", &(viewPos.x), &(viewPos.y), &(viewPos.z) );
				if ( scanCode!=3 || !viewCmdStatus ) {
					ok = false;
					viewCmdStatus = false;
				}
				break;
			}
		case 9:		// 'lookat' command
			{
				scanCode = sscanf( args, "%lf %lf %lf", &(lookAtPos.x), &(lookAtPos.y), &(lookAtPos.z) );
				if ( scanCode!=3 || !viewCmdStatus ) {
					ok = false;
					viewCmdStatus = false;
				}
				break;
			}
		case 10:		// 'up' command
			{
				scanCode = sscanf( args, "%lf %lf %lf", &(upVector.x), &(upVector.y), &(upVector.z) );
				if ( scanCode!=3 || !viewCmdStatus ) {
					ok = false;
					viewCmdStatus = false;
				}
				break;
			}
		case 11:		// 'angle' command
			{
				scanCode = sscanf( args, "%lf", &fovy );
				if ( scanCode!=1 || !viewCmdStatus ) {
					ok = false;
					viewCmdStatus = false;
				}
				else {
					fovy *= PI/180.0;		// Convert to radians
				}
				break;
			}
		case 12:		// 'hither' command
			{
				scanCode = sscanf( args, "%lf", &hither );
				if ( scanCode!=1 || !viewCmdStatus ) {
					ok = false;
					viewCmdStatus = false;
				}
				break;
			}
		case 13:		// 'resolution' command
			{
				scanCode = sscanf( args, "%d %d", &screenWidth, &screenHeight );
				if ( scanCode!=2 || !viewCmdStatus ) {
					ok = false;
					viewCmdStatus = false;
				}
				break;
			}
		default:
			parseErrorOccurred  = true;
			ok = false;
			break;
		}

		if ( !ok ) {
			fprintf(stderr, "Parse error in NFF file, line %ld: %40s.\n", FileLineNumber, inbuffer );
			parseErrorOccurred = true;
		}

	}
}