#include <stdlib.h>
#include <windows.h>		// Header File For Windows
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library
#include <gl\glut.h>
#include <gl\glext.h>
//#include "glext.h"	
#include <stdio.h>
#include <math.h>
#include <string.h>													// Header File For The String Library

#define MAX_EMBOSS (GLfloat)0.008f	// Maximum Emboss-Translate. Increase To Get Higher Immersion
									// At A Cost Of Lower Quality (More Artifacts Will Occur!)
#define __ARB_ENABLE true											// Used To Disable ARB Extensions Entirely

// #define EXT_INFO													// Do You Want To See Your Extensions At Start-Up?
#define MAX_EXTENSION_SPACE 10240									// Characters for Extension-Strings
#define MAX_EXTENSION_LENGTH 256									// Maximum Of Characters In One Extension-String
bool multitextureSupported=false;									// Flag Indicating Whether Multitexturing Is Supported
bool useMultitexture=true;											// Use It If It Is Supported?
GLint maxTexelUnits=1;												// Number Of Texel-Pipelines. This Is At Least 1.

PFNGLMULTITEXCOORD1FARBPROC		glMultiTexCoord1fARB	= NULL;
PFNGLMULTITEXCOORD2FARBPROC		glMultiTexCoord2fARB	= NULL;
PFNGLMULTITEXCOORD3FARBPROC		glMultiTexCoord3fARB	= NULL;
PFNGLMULTITEXCOORD4FARBPROC		glMultiTexCoord4fARB	= NULL;
PFNGLACTIVETEXTUREARBPROC		glActiveTextureARB		= NULL;
PFNGLCLIENTACTIVETEXTUREARBPROC	glClientActiveTextureARB= NULL;	


HDC			hDC=NULL;		// Private GDI Device Context
HGLRC		hRC=NULL;		// Permanent Rendering Context
HWND		hWnd=NULL;		// Holds Our Window Handle
HINSTANCE	hInstance;		// Holds The Instance Of The Application

GLfloat i = 0.1f;
GLfloat j = 2.0f;
GLfloat k = 0.001f;
GLfloat z=  -15;
//GLfloat y=  -1;
GLfloat x=  -1;
GLfloat forward=0.0;
GLfloat angle = 0.0;

GLuint	filter=1;													// Which Filter To Use
GLuint	texture[3];													// Storage For 3 Textures
GLuint  bump[3];													// Our Bumpmappings
GLuint  invbump[3];													// Inverted Bumpmaps

GLfloat Gray[]= {0.5f,0.5f,0.5f,1.0f};

GLUquadricObj *quad;	// Storage For Our Quadratic Objects
bool	keys[256];			// Array Used For The Keyboard Routine
bool	active=TRUE;		// Window Active Flag Set To TRUE By Default
bool	fullscreen=TRUE;	// Fullscreen Flag Set To Fullscreen Mode By Default
bool	light=FALSE;				// Lighting ON/OFF
bool	lp;					// L Pressed?
UINT TextureArray[2];

const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;

GLfloat	xrot;				// X Rotation
GLfloat	yrot;				// Y Rotation
GLfloat xspeed;				// X Rotation Speed
GLfloat yspeed;				// Y Rotation Speed
GLfloat	zmove=0.0f;			// Depth Into The Screen

GLfloat walkbias = 0;
GLfloat walkbiasangle = 0;
GLfloat lookupdown = 0.0f;

#define __ARB_ENABLE true											// Used To Disable ARB Extensions Entirely

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);	// Declaration For WndProc
//PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB = NULL;
//PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = NULL;

//void BMP_Texture(UINT textureArray[], LPSTR strFileName, int ID)  // for mulittexturing
//{
//	if(!strFileName)   return;
	
//	AUX_RGBImageRec *pBitMap = auxDIBImageLoad(strFileName);

//	if(pBitMap == NULL)	exit(0);

//	glGenTextures(1, &textureArray[ID]);
//	glBindTexture(GL_TEXTURE_2D, textureArray[ID]);
//	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitMap->sizeX, pBitMap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data);
//	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	

//	if (pBitMap)										
//	{
//		if (pBitMap->data)								
//		{
//			free(pBitMap->data);						
//		}
//		free(pBitMap);									
//	}
//}

// Always Check For Extension-Availability During Run-Time!
// Here We Go!

bool isInString(char *string, const char *search) {
	int pos=0;
	int maxpos=strlen(search)-1;
	int len=strlen(string);	
	char *other;
	for (int i=0; i<len; i++) {
		if ((i==0) || ((i>1) && string[i-1]=='\n')) {				// New Extension Begins Here!
			other=&string[i];			
			pos=0;													// Begin New Search
			while (string[i]!='\n') {								// Search Whole Extension-String
				if (string[i]==search[pos]) pos++;					// Next Position
				if ((pos>maxpos) && string[i+1]=='\n') return true; // We Have A Winner!
				i++;
			}			
		}
	}	
	return false;													// Sorry, Not Found!
}

// isMultitextureSupported() Checks At Run-Time If Multitexturing Is Supported
bool initMultitexture(void) {
	char *extensions;	
	extensions=strdup((char *) glGetString(GL_EXTENSIONS));			// Fetch Extension String
	int len=strlen(extensions);
	for (int i=0; i<len; i++)										// Separate It By Newline Instead Of Blank
		if (extensions[i]==' ') extensions[i]='\n';

#ifdef EXT_INFO
	MessageBox(hWnd,extensions,"supported GL extensions",MB_OK | MB_ICONINFORMATION);
#endif

	if (isInString(extensions,"GL_ARB_multitexture")				// Is Multitexturing Supported?
		&& __ARB_ENABLE												// Override-Flag
		&& isInString(extensions,"GL_EXT_texture_env_combine"))		// Is texture_env_combining Supported?
	{	
		glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB,&maxTexelUnits);
		glMultiTexCoord1fARB	= (PFNGLMULTITEXCOORD1FARBPROC)		wglGetProcAddress("glMultiTexCoord1fARB");
		glMultiTexCoord2fARB	= (PFNGLMULTITEXCOORD2FARBPROC)		wglGetProcAddress("glMultiTexCoord2fARB");
		glMultiTexCoord3fARB	= (PFNGLMULTITEXCOORD3FARBPROC)		wglGetProcAddress("glMultiTexCoord3fARB");
		glMultiTexCoord4fARB	= (PFNGLMULTITEXCOORD4FARBPROC)		wglGetProcAddress("glMultiTexCoord4fARB");
		glActiveTextureARB		= (PFNGLACTIVETEXTUREARBPROC)		wglGetProcAddress("glActiveTextureARB");
		glClientActiveTextureARB= (PFNGLCLIENTACTIVETEXTUREARBPROC)	wglGetProcAddress("glClientActiveTextureARB");		
#ifdef EXT_INFO
	MessageBox(hWnd,"The GL_ARB_multitexture extension will be used.","feature supported!",MB_OK | MB_ICONINFORMATION);
#endif
		return true;
	}
	useMultitexture=false;											// We Can't Use It If It Isn't Supported!
	return false;
}


// Using auxDIBImageLoad's Own Error-Handler!
int LoadGLTextures(){												// Load Bitmaps And Convert To Textures
	bool status=true;												// Status Indicator
	AUX_RGBImageRec *Image=NULL;									// Create Storage Space For The Texture
	char *alpha=NULL;

	// Load The Tile-Bitmap For Base-Texture
	if (Image=auxDIBImageLoad("Pic/try.bmp")) {											
		glGenTextures(3, texture);									// Create Three Textures

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
		//                             ========
		// Use GL_RGB8 Instead Of "3" In glTexImage2D. Also Defined By GL: GL_RGBA8 Etc.
		// NEW: Now Creating GL_RGBA8 Textures, Alpha Is 1.0f Where Not Specified By Format.

		// Create Linear Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);

		// Create MipMapped Texture
		glBindTexture(GL_TEXTURE_2D, texture[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, Image->sizeX, Image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
	}
	else status=false;
	if (Image) {													// If Texture Exists
		if (Image->data) delete Image->data;						// If Texture Image Exists
		delete Image;
		Image=NULL;
	}	
	
	// Load The Bumpmaps
	if (Image=auxDIBImageLoad("Pic/Bump.bmp")) {			
		glPixelTransferf(GL_RED_SCALE,0.5f);						// Scale RGB By 50%, So That We Have Only			
		glPixelTransferf(GL_GREEN_SCALE,0.5f);						// Half Intenstity
		glPixelTransferf(GL_BLUE_SCALE,0.5f);

		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);	// No Wrapping, Please!
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
		glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,Gray);

		glGenTextures(3, bump);										// Create Three Textures

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, bump[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
		
		// Create Linear Filtered Texture
		glBindTexture(GL_TEXTURE_2D, bump[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);

		// Create MipMapped Texture
		glBindTexture(GL_TEXTURE_2D, bump[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, Image->sizeX, Image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
		
		for (int i=0; i<3*Image->sizeX*Image->sizeY; i++)		// Invert The Bumpmap
			Image->data[i]=255-Image->data[i];

		glGenTextures(3, invbump);								// Create Three Textures

		// Create Nearest Filtered Texture
		glBindTexture(GL_TEXTURE_2D, invbump[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
		
		// Create Linear Filtered Texture
		glBindTexture(GL_TEXTURE_2D, invbump[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Image->sizeX, Image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Image->data);

		// Create MipMapped Texture
		glBindTexture(GL_TEXTURE_2D, invbump[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, Image->sizeX, Image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, Image->data);
		
		glPixelTransferf(GL_RED_SCALE,1.0f);				// Scale RGB Back To 100% Again		
		glPixelTransferf(GL_GREEN_SCALE,1.0f);			
		glPixelTransferf(GL_BLUE_SCALE,1.0f);
	}
	else status=false;
	if (Image) {											// If Texture Exists
		if (Image->data) delete Image->data;				// If Texture Image Exists
		delete Image;
	}

	return status;											// Return The Status
}


GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
{
	if (height==0)										// Prevent A Divide By Zero By
	{
		height=1;										// Making Height Equal One
	}

	glViewport(0,0,width,height);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix to centre of screen

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}


 bool BitmapLoad(char* Filename, UINT index)
    {
      AUX_RGBImageRec *Bitmap = NULL;     // Pointer To AUX_RGBImageRec
      FILE *FileHandle = NULL;            // File Handle To Use

      FileHandle = fopen(Filename,"r");  // Check To See If The File Exists

      if(FileHandle != NULL)              // If File Exists
      {
        Bitmap = auxDIBImageLoad(Filename);  // Use glaux.h To Load Image
      } else                              // Else If Error Loading File
      {
        fclose(FileHandle);               // Close File Handle
        return(false);                    // Return FALSE
      }

      fclose(FileHandle);                 // Close File Handle

      glGenTextures(1,                    // # Of Texture Names/ID To Create
                    &TextureArray[index]);  // Generated Texture ID

      glBindTexture(GL_TEXTURE_2D,        // Bind A 2D Texture 
                    TextureArray[index]); // Bind Using This Texture ID

	  glTexImage2D(GL_TEXTURE_2D, 
				   0, 
				   3, 
				   Bitmap->sizeX, 
				   Bitmap->sizeY, 
				   0, 
				   GL_RGB, 
				   GL_UNSIGNED_BYTE, 
				   Bitmap->data);

  
      glTexParameteri(GL_TEXTURE_2D,          // Set 2D Texture Parameter
                      GL_TEXTURE_MIN_FILTER,  // Select Minification Filter Type
                      GL_NEAREST);            // GL_NEAREST (Blocky & Fastest)

      glTexParameteri(GL_TEXTURE_2D,          // Set 2D Texture Parameter
                      GL_TEXTURE_MAG_FILTER,  // Select Magnification Filter Type
                      GL_LINEAR);	      // GL_LINEAR (Smoothest & Slowest)

      if (Bitmap)
      {
        if (Bitmap->data)                 // If There Is Texture Data
        {
           free(Bitmap->data);            // Free Texture Data
        }
        free(Bitmap);                     // Free Structure
      }

      return(true);                       // Return TRUE
    }


int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
{
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

//	glEnable(GL_TEXTURE_2D);

// Activate multitexturing, and define the new function pointers
//	glActiveTextureARB		= (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress("glActiveTextureARB");
//	glMultiTexCoord2fARB	= (PFNGLMULTITEXCOORD2FARBPROC)wglGetProcAddress("glMultiTexCoord2fARB");

//	if(!glActiveTextureARB || !glMultiTexCoord2fARB)		// Check if your OpenGL version support Multitexturing
//	{
//		MessageBox(hWnd, "This OpenGL version does not support Multitexturing!", "ERROR", MB_OK);
//		PostQuitMessage(0);									// If not Exit
//	}

//	BMP_Texture(TextureArray, "pic/image1.bmp", 0); // Here we load a normal texture
//	BMP_Texture(TextureArray, "pic/wall.bmp", 1); // Here we load a lightmap
	
	return TRUE;										// Initialization Went OK
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% START DRAWING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



//***************************************CEILING************************************************
void drawceiling() {

glBegin(GL_QUADS);
		for (z=-10;z<10;z+=5){
			
			glNormal3f( 0.0f, -1.0f,0.0f);
			
			glTexCoord2f(1.0f, 0.0f);    glVertex3f( 1.2, 1.2, z+5);		// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( 1.2, 1.2, z);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( -1, 1.2, z);			// Upper Left
			glTexCoord2f(0.0f, 0.0f);	 glVertex3f( -1, 1.2, z+5);			// Lower Left
			
		}
		
	 glEnd();									// Finished Drawing The Ceiling

}

//*******************************END OF CEILING***********************************************

int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	const FLOOR=0;
	const F_WALL =1;
	const CEILING =2;
	const STAR = 3;
	const LOW_LEFT=4;
	const TOP_RWALL=5;

	GLfloat i = 0.1f;
	GLfloat j = 2.0f;
	GLfloat z=  -10;
	GLfloat y=  -1;
	GLfloat x=-1;

	
	glPushMatrix();
	glLoadIdentity();	
	glRotatef(angle, 0.0, 1.0, 0.0);
	glTranslatef(0.0, 0.0, forward);		// moving 0 unit into X, 0 unit into Y, forward(ie 0) deep into the screen.

	glClearColor(0.0,0.0,0.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glClearDepth(1.0);



	 if (!light){ 
		glDisable(GL_LIGHTING);
		glDisable(GL_LIGHT0);
		glDisable(GL_LIGHT1);
		glDisable(GL_LIGHT2);
		glDisable(GL_LIGHT3);
		}
	
	 else {
		GLfloat light_position0[]= {k,k, -8,1.0};		//far back
		GLfloat light_position1[]= {1,1, -3, 1.0};
		GLfloat light_position2[]= {k,k, 2, 1.0};
		GLfloat light_position3[]= {k,k, 8,1.0};		//front
	
		GLfloat light_specular[]= {1.0f,1.0f,1.0f,k};
		GLfloat light_ambient[]= {0.0,0.0,0.0,k};
		GLfloat light_diffuse[]= {0.8f,0.8f,0.8f,k};


 		glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular);
		glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);
		glLightfv(GL_LIGHT0,GL_DIFFUSE, light_diffuse);
		glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);

		glLightfv(GL_LIGHT1,GL_SPECULAR, light_specular);
		glLightfv(GL_LIGHT1,GL_AMBIENT, light_ambient);
		glLightfv(GL_LIGHT1,GL_DIFFUSE, light_diffuse);
		glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
		glEnable(GL_LIGHT1);
	
		glLightfv(GL_LIGHT2,GL_SPECULAR, light_specular);
		glLightfv(GL_LIGHT2,GL_AMBIENT, light_ambient);
		glLightfv(GL_LIGHT2,GL_DIFFUSE, light_diffuse);
		glLightfv(GL_LIGHT2, GL_POSITION, light_position2);
		glEnable(GL_LIGHT2);

		glLightfv(GL_LIGHT3,GL_SPECULAR, light_specular);
		glLightfv(GL_LIGHT3,GL_AMBIENT, light_ambient);
		glLightfv(GL_LIGHT3,GL_DIFFUSE, light_diffuse);
		glLightfv(GL_LIGHT3, GL_POSITION, light_position3);
		glEnable(GL_LIGHT3);

	}	


//*******************************FLOOR********************************************************
	glEnable(GL_TEXTURE_2D);						// Enable Texture Mapping 		
	BitmapLoad("pic\\try.bmp", FLOOR);
	glBindTexture(GL_TEXTURE_2D, TextureArray[FLOOR]);

	glBegin(GL_QUADS);
		for (z=-10;z<10;z+=j){
			glNormal3f( 0.0f, 1.0f,0.0f);
			
			for(x=-1.0f;x<1.2;x+=i){
			glTexCoord2f(1.5f, 0.0f);  	glVertex3f( x+i, -1.2, z+j);	// Texture and Quad Bottom Right
			glTexCoord2f(1.5f, 1.5f);   glVertex3f( x+i, -1.2, z);		// Upper Right
		    glTexCoord2f(0.0f, 1.5f);   glVertex3f( x, -1.2, z);		// Texture Upper Left
			glTexCoord2f(0.0f, 0.0f);   glVertex3f( x, -1.2, z+j);		// Texture Lower Left
			}
	}
		
	glEnd();									// Finished Drawing The Floor
	glDisable(GL_TEXTURE_2D);						// Enable Texture Mapping ( NEW )
//*******************************END OF FLOOR********************************************************

//*******************************FRONT WALL********************************************************

	 glEnable(GL_TEXTURE_2D);						// Enable Texture Mapping ( NEW )
	 BitmapLoad("pic\\wall.bmp", F_WALL );
	 glBindTexture(GL_TEXTURE_2D, TextureArray[F_WALL]);
	 glBegin(GL_QUADS);						// Drawing Using Quads
	
			glNormal3f( 0.0f, 0.0f,1.0f);		
			glTexCoord2f(1.0f, 1.0f);		glVertex3f( 1.2f, 1.2f, -10.0f);		// Upper Right
			glTexCoord2f(1.0f, 0.0f);		glVertex3f( 1.2f,  -1.2f, -10.0f);		// Upper Left
			glTexCoord2f(0.0f, 0.0f);       glVertex3f( -1.0f,  -1.2f, -10.0f);		// Bottom Right
			glTexCoord2f(0.0f, 1.0f);       glVertex3f( -1.0f, 1.2f, -10.0f);		// Bottom Left
		
	 glEnd();									// Finished Drawing The Front Wall

	 	
//*******************************END OF FRONT WALL********************************************************

//*******************************LEFT WALL********************************************************

//	glActiveTextureARB(GL_TEXTURE0_ARB);
//	glEnable(GL_TEXTURE_2D);
//	glBindTexture(GL_TEXTURE_2D, TextureArray[0]);

//	glActiveTextureARB(GL_TEXTURE1_ARB);
//	glEnable(GL_TEXTURE_2D);
//	glBindTexture(GL_TEXTURE_2D, TextureArray[1]);

//	glBegin(GL_QUADS);
	 
//	for (z=-10;z<10;z+=j){			// left_ wall 
//		glNormal3f( 1.0f, 0.0f, 0.0f);
//		for(y=-1.2f;y<1.2;y+=i){
		
		// top left vertex
//			glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0f, 0.0f);
//			glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0f, 0.0f);
//			glVertex3f( -1.0f, y, z+j);

		// bottom left vertex
//			glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0f, 1.0f);
//			glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0f, 1.0f);
//			glVertex3f( -1.0f, y+i, z+j);
	
		// bottom right vertex
//			glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, 1.0f);
//			glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, 1.0f);
//			glVertex3f( -1.0f, y+i, z);

		// top right vertex
//			glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0.0f, 0.0f);
///			glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, 0.0f);
//			glVertex3f( -1.0f, y, z);
//		}
//	}
//	glEnd();

	glEnable(GL_TEXTURE_2D);	
	BitmapLoad("pic\\wall.bmp", F_WALL);
	glBindTexture(GL_TEXTURE_2D, TextureArray[F_WALL]);

	  glBegin(GL_QUADS);					// Drawing Using Quads
	
		 for (z=-10;z<10;z+=j){			// left_ wall 
			glNormal3f( 1.0f, 0.0f, 0.0f);
			for(y=-1.2f;y<1.2;y+=i){
				glTexCoord2f(1.0f, 0.0f);				glVertex3f( -1.0f, y, z+j);
				glTexCoord2f(1.0f, 1.0f);              	glVertex3f( -1.0f, y+i, z+j);		
				glTexCoord2f(0.0f, 1.0f);              	glVertex3f( -1.0f, y+i, z);				
				glTexCoord2f(0.0f, 0.0f);              	glVertex3f( -1.0f, y, z);	
			}
		} 
	 glEnd(); 
		
	 glDisable(GL_TEXTURE_2D);

//*******************************END OF LEFT WALL********************************************************
	 		
//*******************************BOTTOM RIGHT WALL********************************************************


	 glEnable(GL_TEXTURE_2D);	
	BitmapLoad("pic\\rightwall.bmp", F_WALL);
	glBindTexture(GL_TEXTURE_2D, TextureArray[F_WALL]);

//LEFT FACE OF RIGHT WALL
	glBegin(GL_QUADS);						// Drawing Using Quads
	
		for (z=-10;z<10;z+=j){
			glNormal3f( -1.0f, 0.0f, 0.0f);
			
			for(y=-1.2f;y<-0.4;y+=i){
			
			glTexCoord2f(5.0f, 0.0f); 	glVertex3f( 1.1, y, z+j);				// Bottom Right
			glTexCoord2f(5.0f, 5.0f);   glVertex3f( 1.1, y+i, z+j);				// Upper Right
		    glTexCoord2f(0.0f, 5.0f);   glVertex3f( 1.1, y+i, z);				// Upper Left
			glTexCoord2f(0.0f, 0.0f);   glVertex3f( 1.1, y, z);					 // Lower Left
			}
		}
		
	 glEnd();				


//RIGHT FACE OF RIGHT WALL
	glBegin(GL_QUADS);						// Drawing Using Quads
	
		for (z=-10;z<10;z+=j){
			glNormal3f( -1.0f, 0.0f, 0.0f);

			for(y=-1.2f;y<-0.4;y+=i){
			
			glTexCoord2f(1.0f, 0.0f);    glVertex3f( 1.2, y, z+j);				// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( 1.2, y+i, z+j);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( 1.2, y+i, z);				// Upper Left
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( 1.2, y, z);				// Lower Left
			}
		}
		
	 glEnd();							


//TOP RIGHT WALL LEDGE
	 glBegin(GL_QUADS);						// Drawing Using Quads
     for (z=-10;z<10;z+=j){
			glNormal3f( 0.0f, 1.0f,0.0f);

			for(x=1.1f;x<=1.15;x+=0.05){

			glTexCoord2f(1.0f, 0.0f);    glVertex3f( x+0.05, -0.4, z+j);		// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( x+0.05, -0.4, z);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( x, -0.4, z);				// Upper Left	
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( x, -0.4, z+j);				// Lower Left
			}
		}
		
	 glEnd();									// Finished Drawing The Ledge


//BOTTOM RIGHT WALL LEDGE
	 glBegin(GL_QUADS);						// Drawing Using Quads
     for (z=-10;z<10;z+=j){
			glNormal3f( 0.0f, 1.0f,0.0f);

			for(x=1.1f;x<=1.15;x+=0.05){

			glTexCoord2f(1.0f, 0.0f);    glVertex3f( x+0.05, -1.2, z+j);			// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( x+0.05, -1.2, z);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( x, -1.2, z);					// Upper Left
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( x, -1.2, z+j);				// Lower Left
			}
		}
		
	 glEnd();		

	 glDisable(GL_TEXTURE_2D);
//*******************************RIGHT WALL********************************************************
	 

//*******************************TOP RIGHT WALL********************************************************

	glEnable(GL_TEXTURE_2D);	
	BitmapLoad("pic\\rightwall.bmp", F_WALL);
	glBindTexture(GL_TEXTURE_2D, TextureArray[F_WALL]);

//LEFT FACE OF RIGHT WALL
	glBegin(GL_QUADS);						// Drawing Using Quads
	
		for (z=-10;z<10;z+=j){
			glNormal3f( -1.0f, 0.0f, 0.0f);
			
			for(y=0.8f;y<1.2;y+=i){
			
			glTexCoord2f(1.0f, 0.0f); 	glVertex3f( 1.1, y, z+j);				// Bottom Right
			glTexCoord2f(1.0f, 1.0f);   glVertex3f( 1.1, y+i, z+j);				// Upper Right
		    glTexCoord2f(0.0f, 1.0f);   glVertex3f( 1.1, y+i, z);				// Upper Left
			glTexCoord2f(0.0f, 0.0f);   glVertex3f( 1.1, y, z);					 // Lower Left
			}
		}
		
	 glEnd();				


//RIGHT FACE OF RIGHT WALL
	glBegin(GL_QUADS);						// Drawing Using Quads

		for (z=-10;z<10;z+=j){
			glNormal3f( -1.0f, 0.0f, 0.0f);

			for(y=0.8f;y<1.2;y+=i){
			
			glTexCoord2f(1.0f, 0.0f);    glVertex3f( 1.2, y, z+j);				// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( 1.2, y+i, z+j);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( 1.2, y+i, z);				// Upper Left
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( 1.2, y, z);				// Lower Left
			}
		}
		
	 glEnd();							


//BOTTOM RIGHT WALL LEDGE
	 glBegin(GL_QUADS);						// Drawing Using Quads
     for (z=-10;z<10;z+=j){
			glNormal3f( 0.0f, 1.0f,0.0f);

			for(x=1.1f;x<=1.15;x+=0.05){

			glTexCoord2f(1.0f, 0.0f);    glVertex3f( x+0.05, 0.8, z-j);		// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( x+0.05, 0.8, z);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( x, 0.8, z);				// Upper Left	
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( x, 0.8, z-j);				// Lower Left
			}
		}
	
	 glEnd();									// Finished Drawing The Ledge


//TOP RIGHT WALL LEDGE
	 glBegin(GL_QUADS);						// Drawing Using Quads
     for (z=-10;z<10;z+=j){
			glNormal3f( 0.0f, 1.0f,0.0f);

			for(x=1.1f;x<=1.15;x+=0.05){

			glTexCoord2f(1.0f, 0.0f);    glVertex3f( x+0.05, 1.2, z-j);			// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( x+0.05, 1.2, z);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( x, 1.2, z);					// Upper Left
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( x, 1.2, z-j);				// Lower Left
			}
		}
		
	 glEnd();		

	 glDisable(GL_TEXTURE_2D);


//*******************************RIGHT WALL********************************************************

//SKY

	glBegin(GL_QUADS);						// Drawing Using Quads
	
		for (z=-10;z<10;z+=j){
			glNormal3f( 1.0f, 0.0f, 0.0f);

			for(y=-0.4f;y<0.8;y+=i){
			
			glTexCoord2f(1.0f, 0.0f);    glVertex3f( 1.2, y, z+j);				// Bottom Right
			glTexCoord2f(1.0f, 1.0f);    glVertex3f( 1.2, y+i, z+j);			// Upper Right
		    glTexCoord2f(0.0f, 1.0f);    glVertex3f( 1.2, y+i, z);				// Upper Left
			glTexCoord2f(0.0f, 0.0f);    glVertex3f( 1.2, y, z);				// Lower Left
			}
		}
		
	 glEnd();	


	if (!light){ 	 
		glEnable(GL_TEXTURE_2D);	
		BitmapLoad("pic\\ceilinglampday.bmp", CEILING);
		glBindTexture(GL_TEXTURE_2D, TextureArray[CEILING]);	 
	}
	else 
		{
		glEnable(GL_TEXTURE_2D);	
		BitmapLoad("pic\\ceilinglampnight.bmp", CEILING);
		glBindTexture(GL_TEXTURE_2D, TextureArray[CEILING]);	 
	}
		
		drawceiling();

	return TRUE;										// Keep Going
}



//*************************************END OF DRAWING!!!****************************************
//**********************************************************************************************


GLvoid KillGLWindow(GLvoid)								// Properly Kill The Window
{

	gluDeleteQuadric(quad);						// Delete The Quadratic To Free System Resources

	if (fullscreen)										// Are We In Fullscreen Mode?
	{
		ChangeDisplaySettings(NULL,0);					// If So Switch Back To The Desktop
		ShowCursor(TRUE);								// Show Mouse Pointer
	}

	if (hRC)											// Do We Have A Rendering Context?
	{
		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
		{
			MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}

		if (!wglDeleteContext(hRC))						// Are We Able To Delete The RC?
		{
			MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}
		hRC=NULL;										// Set RC To NULL
	}

	if (hDC && !ReleaseDC(hWnd,hDC))					// Are We Able To Release The DC
	{
		MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hDC=NULL;										// Set DC To NULL
	}

	if (hWnd && !DestroyWindow(hWnd))					// Are We Able To Destroy The Window?
	{
		MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hWnd=NULL;										// Set hWnd To NULL
	}

	if (!UnregisterClass("OpenGL",hInstance))			// Are We Able To Unregister Class
	{
		MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hInstance=NULL;									// Set hInstance To NULL
	}
}

/*	This Code Creates Our OpenGL Window.  Parameters Are:					*
 *	title			- Title To Appear At The Top Of The Window				*
 *	width			- Width Of The GL Window Or Fullscreen Mode				*
 *	height			- Height Of The GL Window Or Fullscreen Mode			*
 *	bits			- Number Of Bits To Use For Color (8/16/24/32)			*
 *	fullscreenflag	- Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)	*/
 
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
	GLuint		PixelFormat;			// Holds The Results After Searching For A Match
	WNDCLASS	wc;						// Windows Class Structure
	DWORD		dwExStyle;				// Window Extended Style
	DWORD		dwStyle;				// Window Style
	RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left=(long)0;			// Set Left Value To 0
	WindowRect.right=(long)width;		// Set Right Value To Requested Width
	WindowRect.top=(long)0;				// Set Top Value To 0
	WindowRect.bottom=(long)height;		// Set Bottom Value To Requested Height

	fullscreen=fullscreenflag;			// Set The Global Fullscreen Flag

	hInstance			= GetModuleHandle(NULL);				// Grab An Instance For Our Window
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
	wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
	wc.cbClsExtra		= 0;									// No Extra Window Data
	wc.cbWndExtra		= 0;									// No Extra Window Data
	wc.hInstance		= hInstance;							// Set The Instance
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
	wc.hbrBackground	= NULL;									// No Background Required For GL
	wc.lpszMenuName		= NULL;									// We Don't Want A Menu
	wc.lpszClassName	= "OpenGL";								// Set The Class Name

	if (!RegisterClass(&wc))									// Attempt To Register The Window Class
	{
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;											// Return FALSE
	}
	
	if (fullscreen)												// Attempt Fullscreen Mode?
	{
		DEVMODE dmScreenSettings;								// Device Mode
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
		dmScreenSettings.dmPelsWidth	= width;				// Selected Screen Width
		dmScreenSettings.dmPelsHeight	= height;				// Selected Screen Height
		dmScreenSettings.dmBitsPerPel	= bits;					// Selected Bits Per Pixel
		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

		// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
		if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
		{
			// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
			if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
			{
				fullscreen=FALSE;		// Windowed Mode Selected.  Fullscreen = FALSE
			}
			else
			{
				// Pop Up A Message Box Letting User Know The Program Is Closing.
				MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
				return FALSE;									// Return FALSE
			}
		}
	}

	if (fullscreen)												// Are We Still In Fullscreen Mode?
	{
		dwExStyle=WS_EX_APPWINDOW;								// Window Extended Style
		dwStyle=WS_POPUP;										// Windows Style
		ShowCursor(FALSE);										// Hide Mouse Pointer
	}
	else
	{
		dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style
		dwStyle=WS_OVERLAPPEDWINDOW;							// Windows Style
	}

	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size

	// Create The Window
	if (!(hWnd=CreateWindowEx(	dwExStyle,							// Extended Style For The Window
								"OpenGL",							// Class Name
								title,								// Window Title
								dwStyle |							// Defined Window Style
								WS_CLIPSIBLINGS |					// Required Window Style
								WS_CLIPCHILDREN,					// Required Window Style
								0, 0,								// Window Position
								WindowRect.right-WindowRect.left,	// Calculate Window Width
								WindowRect.bottom-WindowRect.top,	// Calculate Window Height
								NULL,								// No Parent Window
								NULL,								// No Menu
								hInstance,							// Instance
								NULL)))								// Dont Pass Anything To WM_CREATE
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
		1,											// Version Number
		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,							// Must Support Double Buffering
		PFD_TYPE_RGBA,								// Request An RGBA Format
		bits,										// Select Our Color Depth
		0, 0, 0, 0, 0, 0,							// Color Bits Ignored
		0,											// No Alpha Buffer
		0,											// Shift Bit Ignored
		0,											// No Accumulation Buffer
		0, 0, 0, 0,									// Accumulation Bits Ignored
		16,											// 16Bit Z-Buffer (Depth Buffer)  
		0,											// No Stencil Buffer
		0,											// No Auxiliary Buffer
		PFD_MAIN_PLANE,								// Main Drawing Layer
		0,											// Reserved
		0, 0, 0										// Layer Masks Ignored
	};
	
	if (!(hDC=GetDC(hWnd)))							// Did We Get A Device Context?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if(!SetPixelFormat(hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if (!(hRC=wglCreateContext(hDC)))				// Are We Able To Get A Rendering Context?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if(!wglMakeCurrent(hDC,hRC))					// Try To Activate The Rendering Context
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	ShowWindow(hWnd,SW_SHOW);						// Show The Window
	SetForegroundWindow(hWnd);						// Slightly Higher Priority
	SetFocus(hWnd);									// Sets Keyboard Focus To The Window
	ReSizeGLScene(width, height);					// Set Up Our Perspective GL Screen

	if (!InitGL())									// Initialize Our Newly Created GL Window
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	return TRUE;									// Success
}

LRESULT CALLBACK WndProc(	HWND	hWnd,			// Handle For This Window
							UINT	uMsg,			// Message For This Window
							WPARAM	wParam,			// Additional Message Information
							LPARAM	lParam)			// Additional Message Information
{
	switch (uMsg)									// Check For Windows Messages
	{
		case WM_ACTIVATE:							// Watch For Window Activate Message
		{
			if (!HIWORD(wParam))					// Check Minimization State
			{
				active=TRUE;						// Program Is Active
			}
			else
			{
				active=FALSE;						// Program Is No Longer Active
			}

			return 0;								// Return To The Message Loop
		}

		case WM_SYSCOMMAND:
		{
			switch (wParam)
			{
				case SC_SCREENSAVE:
				case SC_MONITORPOWER:
					return 0;
			}
			break;
		}

		case WM_CLOSE:								// Did We Receive A Close Message?
		{
			PostQuitMessage(0);						// Send A Quit Message
			return 0;								// Jump Back
		}

		case WM_KEYDOWN:							// Is A Key Being Held Down?
		{
			keys[wParam] = TRUE;					// If So, Mark It As TRUE
			return 0;								// Jump Back
		}

		case WM_KEYUP:								// Has A Key Been Released?
		{
			keys[wParam] = FALSE;					// If So, Mark It As FALSE
			return 0;								// Jump Back
		}

		case WM_SIZE:								// Resize The OpenGL Window
		{
			ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
			return 0;								// Jump Back
		}
	
	}

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	MSG		msg;									// Windows Message Structure
	BOOL	done=FALSE;								// Bool Variable To Exit Loop

	// Ask The User Which Screen Mode They Prefer
	if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
		fullscreen=FALSE;							// Windowed Mode
	}

	// Create Our OpenGL Window
	if (!CreateGLWindow("Assignment 2",640,480,16,fullscreen))
	{
		return 0;									// Quit If Window Was Not Created
	}

	while(!done)									// Loop That Runs While done=FALSE
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// Have We Received A Quit Message?
			{
				done=TRUE;							// If So done=TRUE
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}

		
		else										// If There Are No Messages
		{
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			if ((active && !DrawGLScene()) || keys[VK_ESCAPE])	// Active?  Was There A Quit Received?
			{
				done=TRUE;							// ESC or DrawGLScene Signalled A Quit
			}
			else									// Not Time To Quit, Update Screen
			{
				SwapBuffers(hDC);					// Swap Buffers (Double Buffering)

				if (keys['D'])
				{
					if (light)
					{
						light=FALSE;
						glDisable(GL_LIGHTING);
					}
				}

				if (keys['N'])
				{
					if (!light)
					{
						light=TRUE;
						glEnable(GL_LIGHTING);
					}
				}
				if (!keys['D'])
				{
					lp=FALSE;
				}
			
				if (keys[VK_UP])
				{
					//xspeed-=0.01f;
					forward+=0.2f;
				}
				if (keys[VK_DOWN])
				{
					//xspeed+=0.01f;
					forward-=0.2f;
				}
				if (keys[VK_RIGHT])
				{
					//yspeed+=0.01f;
					angle+=2.0f;
				}
				if (keys[VK_LEFT])
				{
					//yspeed-=0.01f;
					angle-=2.0f;
				}

				if (keys[VK_F1])						// Is F1 Being Pressed?
				{
					keys[VK_F1]=FALSE;					// If So Make Key FALSE
					KillGLWindow();						// Kill Our Current Window
					fullscreen=!fullscreen;				// Toggle Fullscreen / Windowed Mode
					// Recreate Our OpenGL Window
					if (!CreateGLWindow("NeHe's Textures, Lighting & Keyboard Tutorial",640,480,16,fullscreen))
					{
						return 0;						// Quit If Window Was Not Created
					}
				}
			}
		}
	}

	// Shutdown
	KillGLWindow();									// Kill The Window
	return (msg.wParam);							// Exit The Program
}



