/*
This function is called before entering the main rendering loop.
Use it for all your initialisation stuff
*/
void init(GLWrapper *glw)
{
	printInstructions();
	textureLoad = textureLoader();
	/* Set the object transformation controls to their initial values */
	x = 0;
	y = 0;
	z = 0;
	angle_x = 0;
	angle_y = angle_z = 0;
	angle_inc_x = angle_inc_y = angle_inc_z = 0;
	scale = 0.33f;
	aspect_ratio = 1.3333f;
	colourmode = 0;
	weather = 0;

	px, py, pz = 0;

	// Generate index (name) for one vertex array object
	glGenVertexArrays(1, &vao);

	// Create the vertex array object and make it current
	glBindVertexArray(vao);

	/* Define the Blending function */
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	GLfloat vertexColours[] =
	{
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f
	};

	/* Create the colours buffer for the cube */
	glGenBuffers(1, &colourObject);
	glBindBuffer(GL_ARRAY_BUFFER, colourObject);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColours), vertexColours, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);


	/* Load and create our monkey object*/
	birchTree.load_obj("birch_tree.obj");
	walls.createRoom(roomLength, roomHeight);
	walls.setTexture(textureLoad.loadTexture("images/brickwork_texture.png", program));
	walls.setNormalsMap(textureLoad.loadNormals("images/brickwork_normal_map.png", program));

	flooring.createFloor(roomLength, roomHeight);
	flooring.setTexture(textureLoad.loadTexture("images/masonry_wall_texture.png", program));
	flooring.setNormalsMap(textureLoad.loadNormals("images/masonry_wall_normal_map.png", program));

	//sky.createSky();
	createPlanters();


	/* Calculate vertex normals using cross products from the surrounding faces*/
	/* A better way to do this would be to generate the vertex normals in Blender and
	/* then extract the vertex normals from the face definitions and use that to create an
	/* accurate array of veretx normals */
	birchTree.smoothNormals();
	birchTree.createObject();


	tree2 = tree3 = tree4 = tree5 = tree6 = tree6 = tree8 = tree9 = birchTree;
	
	/* Load and build the vertex and fragment shaders */
	try
	{
		particle_program = glw->LoadShader("particle_object.vert", "particle_object.frag");
		program = glw->LoadShader("terrain.vert", "terrain.frag");
	}
	catch (std::exception &e)
	{
		std::cout << "Caught exception: " << e.what() << std::endl;
		std::cin.ignore();
		exit(0);
	}

	/* Define uniforms to send to vertex shader */
	modelID = glGetUniformLocation(program, "model");
	colourmodeID = glGetUniformLocation(particle_program, "weather");
	viewID = glGetUniformLocation(program, "view");
	projectionID = glGetUniformLocation(program, "projection");

	

	/* Define the texture behaviour parameters */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

	particleObject.create(particle_program);
	/* Create the heightfield object */
	
	octaves = 1;
	perlin_scale = 1.0f;
	perlin_frequency = 50.0f;
	land_size = 15.0f;
	snow = new terrain_object(octaves, perlin_frequency, perlin_scale);
	snow->createTerrain(1000, 1000, land_size, land_size);
	snow->createObject();
	snow->setTexture(textureLoad.loadTexture("images/snow.png", program));
}