#include <iostream>
#include <fstream>

using namespace std;

struct RGBA32F {
	float r;
	float g;
	float b;
	float a;
};

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

int main() {
	ifstream transmittance("transmittance.dat", std::ifstream::binary);
	RGBA32F trans[256][64];
	int count = 0;
	while (transmittance) {
		RGBA32F i;
		transmittance.read(reinterpret_cast<char*>(&i), 4*4);
		trans[count/64][count%64] = i;
		count++;
	}
	
	ifstream irradiance("irradiance.dat", std::ifstream::binary);
	RGBA32F irr[64][16];
	count = 0;
	while (irradiance) {
		RGBA32F i;
		irradiance.read(reinterpret_cast<char*>(&i), 4*4);
		irr[count/16][count%16] = i;
		count++;
	}
	
	ifstream scattering("scattering.dat", std::ifstream::binary);
	RGBA32F* scat = new RGBA32F[256*128*33];
	count = 0;
	while (scattering) {
		RGBA32F i;
		scattering.read(reinterpret_cast<char*>(&i), 4*4);
		scat[count] = i;
		count++;
	}
	
	
	ofstream myFile ("data.bin", ios::out | ios::binary);
	
	for (int i = 0; i < 256*128*32; ++i) {
		myFile.write((char*)(scat + i), 4*4);
	}
	
	for (int i = 256*128*32; i < 256*128*33; ++i) {
		int j = i - 256*128*32;
		
		int x = (j % 256);
		int y = (j / 256);
		
		RGBA32F ebin = {0.0, 0.0, 0.0, 1.0};
		
		if (j / 256 < 64) {
			x = j % 64;
			y = j / 64;
			ebin.r = trans[j / 64][j % 64].r;
			ebin.g = trans[j / 64][j % 64].g;
			ebin.b = trans[j / 64][j % 64].b;
		} else if (y >= 64 && y < 64 + 16 && x < 64) {
			j = (j - 64*256);
		//	x = x % 64;
		//	y = y / 64;
		//	ebin.r = irr[y][x].r;
		//	ebin.g = irr[y][x].g;
		//	ebin.b = irr[y][x].b;
			
			ebin.r = x / 64.0;
			ebin.g = y / 16.0;
			ebin.b = 0.0;
			
			j = j - (j / 256)*256 + (j / 256) * 64;
			
			ebin.r = irr[j / 16][j % 16].r;
			ebin.g = irr[j / 16][j % 16].g;
			ebin.b = irr[j / 16][j % 16].b;
			
		//	ebin.r = j / (64.0 * 16.0);
		//	ebin.g = 0;
		//	ebin.b = 0;
		}
		
		myFile.write((char*)(&ebin), 4*4);
	}
	
	return 0;
}