Shadow mapping with Traction engine
-----------------------------------

// TODO: Add info about blurring the depth buffer

Steps to get shadows into your scene:
1. Render scene from lights point of view 
2. Render scene from camera point of view

-----------------------------------

Steps explained:

1. Render scene from lights point of view 

	// -- Render 
	glExt.bindDepthFBO();	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
	// Add offsets to remove z-fight
	glPolygonOffset(2.0f, 2.0f);
	glEnable(GL_POLYGON_OFFSET_FILL);

	gluLookAt( LOOK FROM LIGHT POSITION );

	// Store needed matrices for step 2
	Matrix4 light_mat_proj;
	glGetFloatv(GL_PROJECTION_MATRIX, light_mat_proj);
	Matrix4 light_mat_modelview;
	glGetFloatv(GL_MODELVIEW_MATRIX, light_mat_modelview);

	// Draw your scene here
	drawObjects();

	// -- End rendering to buffer
	glDisable(GL_POLYGON_OFFSET_FILL);
	glExt.unbindFBO();

2. Render scene from camera point of view

	gluLookAt( LOOK FROM CAMERA POSITION );

	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();  
	glTranslatef(0.5, 0.5, 0.5); 	// [0.0, 1.0], not [-1.0, 1.0]
	glScalef(0.5, 0.5, 0.5);
	glMultMatrixf(light_mat_proj); 	// multiply texture matrix
	glMultMatrixf(light_mat_modelview);
	// finally, multiply by the *inverse* of the *current* modelview matrix
	Matrix4 s_mat_m;
	glGetFloatv(GL_MODELVIEW_MATRIX, s_mat_m);

	// Invert matrix
	s_mat_m = s_mat_m.makeInverseTranspose();
	s_mat_m = s_mat_m.makeTranspose();

	glMultMatrixf(s_mat_m);
	glMatrixMode(GL_MODELVIEW);

	// Bind your shadowmapping shader
	shaders.shadowMap->bind();

	// Bind shadow texture & pass it to shader
	glActiveTextureARB(GL_TEXTURE0_ARB);
	 glEnable(GL_TEXTURE_2D);
	 glBindTexture(GL_TEXTURE_2D, glExt.shadowMap);
	 shaders.jytky->setUniform1i("shadowMap", 0);  

	// Draw your scene here with lighting and stuff
	drawObjects();
	
	shaders.unbind();	
	
	// Restore normal texture matrix and stuff
	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        glMatrixMode(GL_MODELVIEW);

-----------------------------------

If post procession is wanted on the shadowed scene
apply it after the texture matrix is restored. Otherwise
things will get messy :-)

-----------------------------------

Check the facts and maths from:
http://en.wikipedia.org/wiki/Shadow_mapping

-----------------------------------

 - rale 12.3.2007
