Пример #1
0
/**
 *	Starts the parsing of a scene description file.
 */
int scn20_parse(
	const char *	fname,
	RaySetupData *	rsd,
	const char *	searchpaths
	)
{
	g_scn20error = SCN_OK;

	if (!error_initialize())
		return SCN_INIT_FAIL;
	FindFileInitialize();
	if (!gettoken_Init(fname))
		return SCN_INIT_FAIL;
	if (!pcontext_init())
		return SCN_INIT_FAIL;

	
	Image_Initialize();
	SCN_SetPaths(&scn_include_paths, searchpaths);
	
	parse(rsd);
//	ScnBuild_CommitScene();

	pcontext_close();
	gettoken_Close();
	FindFileClose();
	error_close();

	return g_error_count ? SCN_ERR_PARSE : SCN_OK;
}
Пример #2
0
///
//Loads and parses a .BMP file constructing an image
//holding it's contents.
//
//WARNING: CURRENTLY ONLY READS 24 BIT BITMAPS!!!!
//
//Parameters:
//	fPath: The filepath of the .bmp file to load
//
//Returns:
//	A pointer to a newly allocated image containing the data from the BMP
struct Image* Loader_Load24BitBMPFile(const char* fPath)
{
	FILE* fp = fopen(fPath, "rb");

	//Make sure file is open
	if (fp == NULL)
	{
		printf("Unable to open %s\n", fPath);
		return NULL;
	}

	//Read the first two unsigned characters
	unsigned char headerInfo[2];
	fread(headerInfo, sizeof(unsigned char), 2, fp);

	//The first two characters in the file represent the file type.
	//Bitmaps should have the magic number 66,77 ('B','M')
	if (headerInfo[0] != 'B' || headerInfo[1] != 'M')
	{
		printf("File %s is not a bitmap file!", fPath);
	}

	//In bitmap files the locationof the start of the bitmap can be found in bytes 10-14
	fseek(fp, 10, 0);
	unsigned int bitmapOffset = 0;
	fread(&bitmapOffset, sizeof(unsigned int), 1, fp);

	unsigned int width;
	unsigned int height;

	//In bmp files the width and height of the image can be found after 4 more bytes
	//18 - 22 for width
	//22 - 26 for height
	fseek(fp, 18, 0);
	fread(&(width), sizeof(unsigned int), 1, fp);
	fread(&(height), sizeof(unsigned int), 1, fp);

	struct Image* img = Image_Allocate();
	Image_Initialize(img, width, height);

	//Seek to beginning of bitmap array
	fseek(fp, bitmapOffset, 0);
	for (unsigned int i = 0; i < img->height; i++)
	{
		for (unsigned int j = 0; j < img->width; j++)
		{
			//Doing it this way swapped my R and B values
			//When I figure out why, change back. I think openGL trnsposes everything when translating information to shader
			//Read the RGB values and store in bitmap
			//fread(img->bitmap + (((i * img->width) + j) * 4), sizeof(unsigned char), 3, fp);

			//So instead
			//Read one at a time and store them in img->bitmap backwards
			unsigned char r, g, b;
			fread(&r, sizeof(unsigned char), 1, fp);
			fread(&g, sizeof(unsigned char), 1, fp);
			fread(&b, sizeof(unsigned char), 1, fp);

			*(img->bitmap + (((i * img->width) + j) * 4) + 0) = b;
			*(img->bitmap + (((i * img->width) + j) * 4) + 1) = g;
			*(img->bitmap + (((i * img->width) + j) * 4) + 2) = r;



			//After the RGB values, store 255 as the alpha value for that pixel.
			*(img->bitmap + (((i * img->width) + j) * 4) + 3) = (unsigned char)255;
			

		}

		//Read padding at the end of each row
		unsigned int padding = 0 - (img->width * 3) % 4;
		padding *= -1;
		if (padding > 0)
		{
			unsigned char* paddingContents = (unsigned char *)malloc(sizeof(unsigned char)* padding + 1);
			fread(paddingContents, sizeof(unsigned char), padding, fp);
			paddingContents[padding] = '\0';
			printf("\nPadding size/contents: %d/%s", padding, paddingContents);
			free(paddingContents);
		}

	}

	//Close the file
	fclose(fp);

	//REturn the image
	return img;
}