#include "Club.h"
#include <cmath>
#include <stdio.h>
//#include <windows.h>
#include <GL/glut.h>

#define PI 3.14159
//#define BITMAP_ID 0x4D42		// the universal bitmap ID

// Prototype
//static unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

const double Club::DEF_SLOPE = 0.5;
const double Club::SCALE = 1.0;

Club::Club(double slope)
: hClub(0),
  hTexture(0),
  slope_(slope)
{
	// Nothing (more) to do
}

void Club::initializeGraphics()
{
	////  Read in the texture.
	////  The file name is hard-coded
	////  is hard-coded as club.bmp
	//BITMAPINFOHEADER	bitmapInfoHeader;	// bitmap info header
	//unsigned char*		bitmapData;		// the bitmap data
	//bitmapData = LoadBitmapFile("club.bmp", &bitmapInfoHeader);

	//// generate texture object
	//unsigned int hTexture;
	//glGenTextures(1,&hTexture);

	//// create texture
	//glEnable(GL_TEXTURE_2D);
	//glBindTexture(GL_TEXTURE_2D, hTexture);

	//// the following two commands tell openGL how to interpolate texture
	//// values between the sampled values in your texture bitmap
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	//// the following two commands tell openGL how to extend the texture
	//// when needed
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 //	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	//// replace color of textured object
	//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	//// now give it the texture map
	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
	//	          bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
	//			  bitmapData);

	// Define a few handy colors
	GLfloat gray9[] = {0.9, 0.9, 0.9, 1.0};
	GLfloat gray8[] = {0.8, 0.8, 0.8, 1.0};
	GLfloat gray5[] = {0.5, 0.5, 0.5, 1.0};
	GLfloat gray4[] = {0.4, 0.4, 0.4, 1.0};
	GLfloat gray3[] = {0.3, 0.3, 0.3, 1.0};
	GLfloat brown[] = {0.5, 0.4, 0.2, 1.0};

	// initialize the club display list
	// the geometry is created once at initialization
	hClub = glGenLists(1);
	glNewList(hClub, GL_COMPILE);

//	// draw the shaft of the club with its texture
//	// enable & bind the texture
//	glEnable(GL_TEXTURE_2D);
//	glBindTexture(GL_TEXTURE_2D, hTexture);

	// position and orient the shaft
	glPushMatrix();
	glTranslatef(SCALE * 0.1f, SCALE * 0.4f, SCALE * 0.0f);
	glRotatef(10.0f,0.0f,0.0f,1.0f);

	// create the cylinder
	int numStrips=10;
	float topRadius=SCALE * 0.2;
	float bottomRadius=SCALE * 0.1;
	float clubHeight=SCALE * 5.0;
	glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,brown);
	glBegin(GL_QUAD_STRIP);
	for (int i=0;i<numStrips;i++) {
		double d=(double) i/(double) (numStrips-1);

		glTexCoord2d(d,0);
		glVertex3d(cos(2.0*PI*d)*(double) topRadius,
				   (double) clubHeight,
				   sin( 2*PI*d) * (double) topRadius);
		glTexCoord2d(d,1);
		glVertex3d(cos(2*PI*d)*(double) bottomRadius,
				   0,
				   sin(2*PI*d)*(double) bottomRadius);
	}
	glEnd();
//	glDisable(GL_TEXTURE_2D);

	// draw top of the shaft
	glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,brown);
	glBegin(GL_POLYGON);
	for (int i=0;i<numStrips;i++) {
		double d=2.0*PI*(double) i/(double) (numStrips-1);
		glVertex3d(cos(d)*(double) topRadius,
				   (double) clubHeight,
				   sin(d) * (double) topRadius);
	}
	glEnd();
	glPopMatrix();

	// draw the head

	// draw back face
	glPushMatrix();
	glTranslatef(SCALE * 0.0f, SCALE * 0.0f, SCALE * 0.1f);
	glBegin(GL_QUAD_STRIP);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray9);
		glVertex3f(SCALE * 0.1f, SCALE * 0.5f, SCALE * 0.0f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.5f, SCALE * 0.0f);
		glVertex3f(SCALE * 0.0f, SCALE * 0.4f, SCALE * 0.0f);
		glVertex3f(SCALE * 1.0f, SCALE * 0.4f, SCALE * 0.0f);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray4);
		glVertex3f(SCALE * 0.1f, SCALE * 0.0f, SCALE * -0.05f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.0f, SCALE * -0.05f);
	glEnd();
	glPopMatrix();


	// draw front face
	glPushMatrix();
	glTranslatef(SCALE * 0.0f, SCALE * 0.0f, SCALE * -0.1f);
	glBegin(GL_QUAD_STRIP);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray9);
		glVertex3f(SCALE * 0.1f, SCALE * 0.5f, SCALE * 0.0f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.5f, SCALE * 0.0f);
		glVertex3f(SCALE * 0.0f, SCALE * 0.4f, SCALE * 0.0f);
		glVertex3f(SCALE * 1.0f, SCALE * 0.4f, SCALE * 0.0f);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray5);
		glVertex3f(SCALE * 0.1f, SCALE * 0.0f, SCALE * 0.01f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.0f, SCALE * 0.01f);
	glEnd();
	glPopMatrix();

	// draw sides
	glBegin(GL_QUAD_STRIP);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray8);
		glVertex3f(SCALE * 0.1f, SCALE * 0.5f, SCALE * -0.1f);
		glVertex3f(SCALE * 0.1f, SCALE * 0.5f, SCALE * 0.1f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.5f, SCALE * -0.1f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.5f, SCALE * 0.1f);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray9);
		glVertex3f(SCALE * 1.0f, SCALE * 0.4f, SCALE * -0.1f);
		glVertex3f(SCALE * 1.0f, SCALE * 0.4f, SCALE * 0.1f);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray3);
		glVertex3f(SCALE * 0.9f, SCALE * 0.0f, SCALE * -0.09f);
		glVertex3f(SCALE * 0.9f, SCALE * 0.0f, SCALE * 0.05f);
		glVertex3f(SCALE * 0.1f, SCALE * 0.0f, SCALE * -0.09f);
		glVertex3f(SCALE * 0.1f, SCALE * 0.0f, SCALE * 0.05f);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray9);
		glVertex3f(SCALE * 0.0f, SCALE * 0.4f, SCALE * -0.1f);
		glVertex3f(SCALE * 0.0f, SCALE * 0.4f, SCALE * 0.1f);
		glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,gray8);
		glVertex3f(SCALE * 0.1f, SCALE * 0.5f, SCALE * -0.1f);
		glVertex3f(SCALE * 0.1f, SCALE * 0.5f, SCALE * 0.1f);
	glEnd();

	// end the list
	glEndList();
}


Club::~Club()
{
	glDeleteLists(1,hClub);
}

void Club::draw()
{
	glCallList(hClub);
}



//// LoadBitmapFile
//// desc: Returns a pointer to the bitmap image of the bitmap specified
////       by filename. Also returns the bitmap header information.
////		 No support for 8-bit bitmaps.
//static unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
//{
//	FILE *filePtr;								// the file pointer
//	BITMAPFILEHEADER	bitmapFileHeader;		// bitmap file header
//	unsigned char		*bitmapImage;			// bitmap image data
//	unsigned int		imageIdx = 0;			// image index counter
//	unsigned char		tempRGB;				// swap variable
//
//	// open filename in "read binary" mode
//	filePtr = fopen(filename, "rb");
//	if (filePtr == NULL)
//		return NULL;
//
//	// read the bitmap file header
//	fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
//
//	// verify that this is a bitmap by checking for the universal bitmap id
//	if (bitmapFileHeader.bfType != BITMAP_ID)
//	{
//		fclose(filePtr);
//		return NULL;
//	}
//
//	// read the bitmap information header
//	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
//
//	// move file pointer to beginning of bitmap data
//	fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
//
//	// allocate enough memory for the bitmap image data
//	bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
//
//	// verify memory allocation
//	if (!bitmapImage)
//	{
//		free(bitmapImage);
//		fclose(filePtr);
//		return NULL;
//	}
//
//	// read in the bitmap image data
//	fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
//
//	// make sure bitmap image data was read
//	if (bitmapImage == NULL)
//	{
//		fclose(filePtr);
//		return NULL;
//	}
//
//	// swap the R and B values to get RGB since the bitmap color format is in BGR
//	for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
//	{
//		tempRGB = bitmapImage[imageIdx];
//		bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
//		bitmapImage[imageIdx + 2] = tempRGB;
//	}
//
//	// close the file and return the bitmap image data
//	fclose(filePtr);
//	return bitmapImage;
//}
