/*-
 * Copyright (c) 1999 Qube Software, Ltd.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of the source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Any redistribution solely in binary form must conspicuously
 *    reproduce the following disclaimer in documentation provided with the
 *    binary redistribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'', WITHOUT ANY WARRANTIES, EXPRESS
 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  LICENSOR SHALL
 * NOT BE LIABLE FOR ANY LOSS OR DAMAGES RESULTING FROM THE USE OF THIS
 * SOFTWARE, EITHER ALONE OR IN COMBINATION WITH ANY OTHER SOFTWARE.
 *
 *	$Id: genteapot.cpp,v 1.10 2000/04/11 14:06:06 nps Exp $
 */

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <math.h>
 
#include "patch.h"

#define M_PI            3.14159265358979323846
#define scale		1.0f
using namespace Utils;

void GenModelPov(Patch::Model* model, int noShaders)
{
    
    int i,j,ret,pnum;
    unsigned char r,g,b;
    float x,y,z;
    float min,max;    
    FILE *stream;
    Patch::BicubicData bdat;
    
    stream = fopen("utahteapot.txt","r");

    if(stream == NULL)
	throw Problem("Teapot data not found\n");
    
    j = 0;
    min = 0;
    max = 0;
    pnum = 1;
    while(1) {
	
	//find next patch
	
	for(i = 0; i < 16; i++) {
	    
	    ret = fscanf(stream,"%f %f %f",&x,&y,&z);
	    
            if(ret != 3)
                return;

	    //read in the point
	    
	    r = (unsigned char)(z * 2);
	    g = (unsigned char)(z * 2);
	    b = (unsigned char)(z * 2);

	    bdat.points[i].x = x * scale;
	    bdat.points[i].y = y * scale;
	    bdat.points[i].z = z * scale;

	    bdat.colors[i].x = 255;//r;
	    bdat.colors[i].y = 255;//g;
	    bdat.colors[i].z = 255;//b;

	    bdat.uv[i].x = float(i % 4) / 3.0f;
	    bdat.uv[i].y = float(i / 4) / 3.0f;
	    
	}

	if(pnum < 13)
	    bdat.shaderIndex = 0;
	else if(pnum < 21)
	    bdat.shaderIndex = 2;
		else
		    bdat.shaderIndex = 1;

	pnum++;
	
	model->add(bdat);
	j++;
    }



}

void makePatch(Patch::Model* model, Draw::Shader* shader)
{

    int i,j,k;
    Patch::BicubicData test;

    for(k = 0; k < 2; k++){
	for(i = 0; i < 4; i++){
	    for(j = 0; j < 4; j++){
		test.points[i*4+j].x = (float)(j*20 - 30) + (k*60);
		test.points[i*4+j].y = (float)(i*20 - 30);
		test.points[i*4+j].z = 0;
		
		test.colors[i*4+j].x = 255;
		test.colors[i*4+j].y = 255;
		test.colors[i*4+j].z = 255;
		
		test.uv[i*4+j].x = (float)j / 3.0f;
		test.uv[i*4+j].y = (float)i / 3.0f;
	    }
	}
	//test.shader = shader;

	model->add(test);
    }
    
}
