/***********      .---.         .-"-.      *******************\
* -------- *     /   ._.       / ´ ` \     * ---------------- *
* Author's *     \_  (__\      \_°v°_/     * humus@rogers.com *
*   note   *     //   \\       //   \\     * ICQ #47010716    *
* -------- *    ((     ))     ((     ))    * ---------------- *
*          ****--""---""-------""---""--****                  ********\
* This file is a part of the work done by Humus. You are free to use  *
* the code in any way you like, modified, unmodified or copy'n'pasted *
* into your own work. However, I expect you to respect these points:  *
*  @ If you use this file and its contents unmodified, or use a major *
*    part of this file, please credit the author and leave this note. *
*  @ For use in anything commercial, please request my approval.      *
*  @ Share your work and ideas too as much as you can.                *
\*********************************************************************/

#include "../Framework3/Direct3D/D3DApp.h"
#include "../Framework3/Util/Model.h"
#include "../Framework3/Util/BSP.h"
#include "../Framework3/Util/TexturePacker.h"

#define SAMPLE_COUNT 4
#define LS_COUNT 512

class App : public D3DApp {
public:
	char *getTitle() const { return "Dynamic lightmapping"; }

	void moveCamera(const vec3 &dir);
	void resetCamera();

	bool onMouseButton(const int x, const int y, const MouseButton button, const bool pressed);

	bool init();
	void exit();

	bool load();
	void unload();

	bool onReset();

	void drawLight(const vec3 &dx, const vec3 &dy);
	void drawLightmap();
	void drawFrame();

protected:
	Model *map;
	BSP bsp;

	vec3 lightPos;
	float lightRadius, lightRange;

	uint lmWidth, lmHeight;

	VertexFormatID lightVF, quadVF;

	ShaderID depth;
	TextureID shadowMap, shadowMapDepth;

	ShaderID lightMapping, lightMappingBlend;
	TextureID rtLightMap[2];
	int currRTLightMap;

	SamplerStateID nearestClamp, bilinearClamp, trilinearAniso;
	TextureID base[3], bump[3], positionMap[SAMPLE_COUNT], lightTex;
	ShaderID lighting, light;
	BlendStateID blendAdd;

	float lastMouseTime;
};
