/*
 * (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.
 */

#include <string.h>
#include <stdio.h>

#include <GL/glew.h>
#include "shader.h"
#include "glsl_glue.h"
#include "gl_error.h"

glsl_shader_program::~glsl_shader_program(void)
{
    glsl_uniform *u;
    glsl_uniform *next;


    for (u = uniforms; u != NULL; u = next) {
        next = u->next;

        if (u->name) {
            delete u->name;
            u->name = NULL;
        }

        u->next = NULL;
        u->handle = 0;
        delete u;
    }
}


void glsl_shader_program::activate(void)
{
    glUseProgramObjectARB(program);
}


glsl_uniform *glsl_shader_program::add_uniform(const char *name)
{
    const size_t len = strlen(name);
    glsl_uniform *u = new glsl_uniform;

    if (u != NULL) {
	u->name = new char[len + 1];
	if (u->name != NULL) {
	    (void) memcpy(u->name, name, len + 1);

	    u->handle = glGetUniformLocationARB(program, u->name);
	    if (u->handle == -1) {
		delete u;
		u = NULL;
	    }
	}
	else {
	    delete u;
	    u = NULL;
	}
    }
    return u;
}


glsl_uniform *glsl_shader_program::find_uniform(const char *name)
{
    glsl_uniform *u;
    glsl_uniform *next;


    for (u = uniforms; u != NULL; u = next) {
        next = u->next;

        if (strcmp(u->name, name) == 0) {
            return u;
        }
    }


    return NULL;
}


int glsl_shader_program::get_info_log(char *log_buffer, size_t size)
{
    GLsizei info_size;

    glGetInfoLogARB(program, GLsizei(size), & info_size, log_buffer);
    return info_size;
}


void glsl_shader_program::print_log(bool only_if_error, const char *file,
                                    int line)
{
    bool do_print = false;

    if (only_if_error) {
        GLenum err = glGetError();
        if (err) {
            printf("%s,%d: Error: 0x%04x\n", file, line, err);

            do_print = true;
        }
    }
    else {
        do_print = true;
    }


    if (do_print) {
        GLcharARB info_log[1024];

        if (get_info_log(info_log, sizeof(info_log)) > 0) {
            printf("Info log:\n%s\n", info_log);
        }
    }
}


void glsl_shader_program::uniform(const char *name, int x)
{
    glsl_uniform *u = find_uniform(name);

    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform1iARB(u->handle, x);
    return;
}


void glsl_shader_program::uniform(const char *name, float x)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform1fARB(u->handle, x);
    return;
}


void glsl_shader_program::uniform(const char *name, float x, float y)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform2fARB(u->handle, x, y);
    return;
}


void glsl_shader_program::uniform(const char *name, float x, float y, float z)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform3fARB(u->handle, x, y, z);
    return;
}


void glsl_shader_program::uniform(const char *name, float x, float y, float z,
                                  float w)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform4fARB(u->handle, x, y, z, w);
    return;
}


void glsl_shader_program::uniform2(const char *name, unsigned count,
                                   const float *v)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform2fvARB(u->handle, count, v);
    return;
}


void glsl_shader_program::uniform3(const char *name, unsigned count,
                                   const float *v)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform3fvARB(u->handle, count, v);
    return;
}


void glsl_shader_program::uniform4(const char *name, unsigned count,
                                   const float *v)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniform4fvARB(u->handle, count, v);
    return;
}


void glsl_shader_program::uniform_matrix4(const char *name, unsigned count,
					  bool transpose, const float *v)
{
    glsl_uniform *u = find_uniform(name);
    
    if (u == NULL) {
        u = add_uniform(name);
        if (u == NULL) {
            return;
        }
    }

    glUniformMatrix4fvARB(u->handle, count, transpose, v);
    return;
}
