/*
 * (C) Copyright Ian D. Romanick 2007
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# define _USE_MATH_DEFINES
# include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>

#include "primitives.h"
#include "vector.h"
#include "shader.h"
#include "gl_error.h"

#include "shadow.h"

//#define DO_SHADOWS

void
draw_scene(struct entity *entities, unsigned num_entities,
	   const idr_vec4 &light)
{
    unsigned i;
    static const GLubyte volume_front_color[4] = {
	0x00, 0xff, 0x00, 0x80
    };

    static const GLubyte volume_back_color[4] = {
	0x00, 0x00, 0xff, 0x80
    };

    /* Draw the objects
     */
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glMatrixMode(GL_MODELVIEW);

    /* First pass: draw the object with only ambient lighting.
     */
	glEnableVertexAttribArrayARB(1);

    for (i = 0; i < num_entities; i++) {
        glPushMatrix();
        glMultMatrixf(entities[i].transform);

		entities[i].ambient_shader->activate();
        glColor4ubv(entities[i].color);
        entities[i].object->draw(1);

        glPopMatrix();
    }

	/* Second pass: draw shadow volume back faces with stencil decrement
     */
	glDepthMask(GL_FALSE);
	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
	glDisableVertexAttribArrayARB(1);
	glEnable(GL_STENCIL_TEST);
	glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP);
    for (i = 0; i < num_entities; i++) {
        glPushMatrix();
        glMultMatrixf(entities[i].transform);

		entities[i].sv_shader->activate();
        glColor4ubv(entities[i].color);
        entities[i].object->draw_shadow_volume();

        glPopMatrix();
    }

    /* Third pass: draw shadow volume front faces with stencil increment
     */
	glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP);
	glCullFace(GL_FRONT);
    for (i = 0; i < num_entities; i++) {
        glPushMatrix();
        glMultMatrixf(entities[i].transform);

		entities[i].sv_shader->activate();
        glColor4ubv(entities[i].color);
        entities[i].object->draw_shadow_volume();

        glPopMatrix();
    }
	glCullFace(GL_BACK);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	glDepthMask(GL_TRUE);

	/* Fourth pass: draw the object with regular lighting.
     */
	glEnableVertexAttribArrayARB(1);

	glStencilFunc(GL_EQUAL, 0, ~0); 
	glDepthFunc(GL_LEQUAL);
	glEnableVertexAttribArrayARB(1);

    for (i = 0; i < num_entities; i++) {
        glPushMatrix();
        glMultMatrixf(entities[i].transform);

		entities[i].lit_shader->activate();
        glColor4ubv(entities[i].color);
        entities[i].object->draw(1);

        glPopMatrix();
    }
	glDisable(GL_STENCIL_TEST);
	glDisableVertexAttribArrayARB(1);
	glDepthFunc(GL_LESS);
	glStencilFunc(GL_ALWAYS, 0, ~0); 

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);

    glDisable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
    glUseProgramObjectARB(0);
}
