/*  CS 354 Computer Graphics Sample Program
 */

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/times.h>
#include <unistd.h>
#include <math.h>

#include "main.h"


void PrintglError(int error)
{
   switch (error) {
      case GL_NO_ERROR :
	       printf ("GL_NO_ERROR\n"); break;
	  case GL_INVALID_ENUM :
	       printf ("GL_INVALID_ENUM\n"); break;
      case GL_INVALID_VALUE :
	       printf ("GL_INVALID_VALUE\n"); break;
      case GL_INVALID_OPERATION :
	       printf ("GL_INVALID_OPERATION\n"); break;
      case GL_STACK_OVERFLOW :
	       printf ("GL_STACK_OVERFLOW\n"); break;
      case GL_STACK_UNDERFLOW :
	       printf ("GL_STACK_UNDERFLOW\n"); break;
      case GL_OUT_OF_MEMORY :
	       printf ("GL_OUT_OF_MEMORY\n"); break;
      case GL_TABLE_TOO_LARGE:
	       printf ("GL_TABLE_TOO_LARGE\n"); break;
	  default :
	       printf ("Unknown Error\n"); break;
   }


}


// Uniform Radom Number Generator from 0 to 1
float ranf()
{
	float	RetRandomValue;
	double	UniformRandomNum;

	UniformRandomNum = (double)rand();
	UniformRandomNum /= (double)RAND_MAX;
	return (float)UniformRandomNum; // from 0 to 1
}


// Uniform Radom Number Generator from Minf to Maxf
float ranf(float Minf, float Maxf)
{
	float	RetRandomValue;
	double	UniformRandomNum;

	UniformRandomNum = (double)rand();
	UniformRandomNum /= (double)RAND_MAX;
	UniformRandomNum *= (Maxf - Minf);
	UniformRandomNum += Minf;
	
	return (float)UniformRandomNum; // from 0 to 1
}



//
// Normal Random Variate Generator
// Mean=m, Standard deviation=S
//
float GaussianRandomNum(float m, float s)     
{                                       
	float x1, x2, w, y1;
	static float y2;
	static int use_last = 0;

	// use value from previous call
	if (use_last) {
		y1 = y2;
		use_last = 0;
	}
	else {
		do {
			x1 = 2.0 * ranf() - 1.0;
			x2 = 2.0 * ranf() - 1.0;
			w = x1 * x1 + x2 * x2;
		} while ( w >= 1.0 );

		w = sqrt( (-2.0 * log( w ) ) / w );
		y1 = x1 * w;
		y2 = x2 * w;
		use_last = 1;
	}

	return( m + y1 * s );
}



unsigned char* LoadTexture(char* filename, int& Width, int& Height)
{
	FILE			*texstream;
	int				Max;
	char			str[10];
	unsigned char	*TexImage;
	
	
	printf ("FileName = %s\n", filename);
	texstream = fopen(filename, "r");
	if (texstream==NULL) printf ("%s cannot be open\n", filename);
	else {
		fscanf (texstream, "%s", str);
		fscanf (texstream, "%d %d", &Width, &Height);
		fscanf (texstream, "%d", &Max);
	}
	printf ("Width = %d, Height = %d, Max = %d\n", Width, Height, Max);
	
	TexImage = new unsigned char[Width*Height*3];
	for (int i=0; i<Width*Height*3; i++) {
		fscanf (texstream, "%d", &TexImage[i]);
	}
	
//	for (int i=0; i<10*3; i+=3) {
//		printf ("%d %d %d\n", TexImage[i], TexImage[i+1], TexImage[i+2]);
//	}
	fclose (texstream);
	return TexImage;
}

// P1[3], P2[3], P3[3] are vertices as input parameters
// Normal[3] is output
void CalculateNormalVector(float *P1, float *P2, float *P3, float *Normal)
{
	int		i, j;
	float	V1[3], V2[3];

	for (i=0; i<3; i++) {
		V1[i] = P3[i] - P2[i];
		V2[i] = P1[i] - P2[i];
	}

	// Cross Product
	Normal[0] = V1[1]*V2[2] - V1[2]*V2[1];
	Normal[1] = V1[2]*V2[0] - V1[0]*V2[2];
	Normal[2] = V1[0]*V2[1] - V1[1]*V2[0];

}














