Mzsmunna
10/16/2019 - 3:21 PM

BoxCricketV0.5


#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include<GL/gl.h>
#include <GL/glut.h>
#include<math.h>
#include<cstring>
#include <stdio.h>
#include <string.h>

using namespace std;

//------ BOX CRICKET VARIABLES -------//

float batsMan_Movement = 0.0f;
float batsMan_Handy = 0.0f;


//------ BOX CRICKET VARIABLES -------//

// actual vector representing the camera's direction
float lx=-0.0f,ly=1.0f,lz=-0.5f;
//float lx=-0.71f,ly=0.0f,lz=-0.69f;

// XZ position of the camera
float x=0.0f,z=10.0f;
//float x=2.73f,z=2.75f;

// all variables initialized to 1.0, meaning
// the triangle will initially be white
float red=1.0f, blue=1.0f, green=1.0f;

// angle for rotating triangle
float angle = 0.0f;
//float angle = -0.8f;
float _angle1 = 15.0f;
float speed = 0.0f;
bool con = false;

//int dummy = 5;

//Lighting values
//GLfloat  whiteLight[] = { 1.0f, 0.8f, 0.8f, 1.0f };
//GLfloat  sourceLight[] = { 1.0f, 0.8f, 0.8f, 1.0f };
//GLfloat	 lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };

//TEXTURE MAPPING Global variables

//FPS
GLint gFramesPerSecond = 0;

void FPS(void) {
  static GLint Frames = 0;         // frames averaged over 1000mS
  static GLuint Clock;             // [milliSeconds]
  static GLuint PreviousClock = 0; // [milliSeconds]
  static GLuint NextClock = 0;     // [milliSeconds]

  ++Frames;
  Clock = glutGet(GLUT_ELAPSED_TIME); //has limited resolution, so average over 1000mS
  if ( Clock < NextClock ) return;

  gFramesPerSecond = Frames/1; // store the averaged number of frames per second

  PreviousClock = Clock;
  NextClock = Clock+1000; // 1000mS=1S in the future
  Frames=0;
}

void idle() {
  #define REGULATE_FPS
  #ifdef REGULATE_FPS
  static GLuint PreviousClock=glutGet(GLUT_ELAPSED_TIME);
  static GLuint Clock=glutGet(GLUT_ELAPSED_TIME);
  static GLfloat deltaT;

  Clock = glutGet(GLUT_ELAPSED_TIME);
  deltaT=Clock-PreviousClock;
  if (deltaT < 35) {return;} else {PreviousClock=Clock;}
  #endif

  //put your specific idle code here
  //... this code will run at FPS
  cout<<".";
  //end your specific idle code here

  FPS(); //only call once per frame loop
  glutPostRedisplay();
}

// END OF FPS

//TEXTURE MAPPING


//END OF TEXTURE MAPPING

void changeSize(int w, int h)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;

// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset Matrix
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(15.0f, ratio, 0.1, 300.0f);

// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}

void WicketStamp()
{
    GLUquadric *obj = gluNewQuadric();
//gluQuadricDrawStyle(obj, GLU_LINE);
//glColor3f(0.2f, 0.9, 0.1f);
glColor3ub(255,15,15);
//glPushMatrix();
glRotatef(-90, 1.0,0.0,0.0);
//glScaled(1,2,1);
//gluCylinder(obj,0.1,0.1-(0.1*0.2),0.5, 20,20);
gluCylinder(obj,0.025,0.025,0.8, 20,20);

//glPopMatrix();
//glutSwapBuffers();
}

void WicketBell()
{
    GLUquadric *obj = gluNewQuadric();
//gluQuadricDrawStyle(obj, GLU_LINE);
//glColor3f(0.2f, 0.9, 0.1f);
glColor3ub(155,55,195);
//glPushMatrix();
glRotatef(-90, 0.0,1.0,0.0);
//glScaled(1,2,1);
//gluCylinder(obj,0.1,0.1-(0.1*0.2),0.5, 20,20);
gluCylinder(obj,0.015,0.015,0.1, 20,20);
//glColor3ub(255,15,15);
glTranslated(0,0.0,-0.01);
gluCylinder(obj,0.005,0.005,0.12, 20,20);

//glPopMatrix();
//glutSwapBuffers();
}

void Bat()
{

    //BAT HOLDER
    glColor3ub(255,55,195);
    glPushMatrix();
    glTranslated(-0.25,1.1,-10);
    //glRotated(90,0.0,1.0,0.0);
    glRotated(20,0.0,0.0,1.0);
    glScaled(0.1,1,0.1);
    glutSolidCube(0.3);
    glPopMatrix();

    //BAT BODY
    glColor3ub(255,155,195);
    glPushMatrix();
    glTranslated(-0.36,1.4,-10);
    //glRotated(90,0.0,1.0,0.0);
    glRotated(20,0.0,0.0,1.0);
    glScaled(0.1,1.2,0.3);
    glutSolidCube(0.4);
    glPopMatrix();

}

void BatsMan()
{
    //FACE
    glColor3ub(255,55,95);
    glPushMatrix();
    glTranslated(-0.05,1.35,-10);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.4,0.4,0.3);
    glutSolidCube(0.4);
    glPopMatrix();

    //LEFT SHOULDER
    glColor3ub(55,55,195);
    glPushMatrix();
    glTranslated(0.05,1.10,-9.85);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.2,0.8,0.2);
    glutSolidCube(0.4);
    glPopMatrix();

    //LEFT HAND
    glColor3ub(255,155,115);
    glPushMatrix();
    glTranslated(-0.08,0.97,-9.88);
    //glRotated(90,0.0,1.0,0.0);
    glRotated(25,0.0,-1.0,0.0);
    glScaled(1,0.3,0.35);
    glutSolidCube(0.2);
    glPopMatrix();

    //LEFT GLOVS
    glColor3ub(255,255,15);
    glPushMatrix();
    glTranslated(-0.20,0.97,-10.07);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.3,0.3,0.5);
    glutSolidCube(0.2);
    glPopMatrix();

    //RIGHT GLOVS
    glColor3ub(255,255,15);
    glPushMatrix();
    glTranslated(-0.20,0.97,-9.93);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.3,0.3,0.5);
    glutSolidCube(0.2);
    glPopMatrix();

    //Right HAND
    glColor3ub(255,155,115);
    glPushMatrix();
    glTranslated(-0.08,0.97,-10.12);
    //glRotated(90,0.0,1.0,0.0);
    glRotated(25,0.0,1.0,0.0);
    glScaled(1,0.3,0.35);
    glutSolidCube(0.2);
    glPopMatrix();

    //Right SHOULDER
    glColor3ub(55,55,195);
    glPushMatrix();
    glTranslated(0.05,1.10,-10.15);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.2,0.8,0.2);
    glutSolidCube(0.4);
    glPopMatrix();

    //BODY
    glColor3ub(155,155,195);
    glPushMatrix();
    glTranslated(0.16,1.05,-10.0);
    glRotated(90,0.0,1.0,0.0);
    glRotated(30,-1.0,0.0,0.0);
    glScaled(0.5,1.2,0.2);
    glutSolidCube(0.5);
    glPopMatrix();

    //RIGHT THAI
    glColor3ub(155,155,95);
    glPushMatrix();
    glTranslated(0.2,0.7,-10.08);
    glRotated(90,0.0,1.0,0.0);
    glRotated(30,1.0,0.0,0.0);
    glScaled(0.2,0.7,0.2);
    glutSolidCube(0.5);
    glPopMatrix();

    //LEFT LEG
    glColor3ub(55,55,195);
    glPushMatrix();
    glTranslated(0.12,0.3,-10.08);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.2,1.2,0.2);
    glutSolidCube(0.5);
    glPopMatrix();

    //LEFT THAI
    glColor3ub(155,155,95);
    glPushMatrix();
    glTranslated(0.2,0.7,-9.92);
    glRotated(90,0.0,1.0,0.0);
    glRotated(30,1.0,0.0,0.0);
    glScaled(0.2,0.7,0.2);
    glutSolidCube(0.5);
    glPopMatrix();

    //LEFT LEG
    glColor3ub(55,55,195);
    glPushMatrix();
    glTranslated(0.12,0.3,-9.92);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(0.2,1.2,0.2);
    glutSolidCube(0.5);
    glPopMatrix();

    //LEFT BOOT
    glColor3ub(255,255,15);
    glPushMatrix();
    glTranslated(0.08,0.15,-9.92);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(1,0.3,0.6);
    glutSolidCube(0.2);
    glPopMatrix();

    //RIGHT BOOT
    glColor3ub(255,255,15);
    glPushMatrix();
    glTranslated(0.08,0.15,-10.08);
    //glRotated(90,0.0,1.0,0.0);
    //glRotated(30,1.0,0.0,0.0);
    glScaled(1,0.3,0.6);
    glutSolidCube(0.2);
    glPopMatrix();

    glPushMatrix();
        //glRotated(60,0.0,0.0,1.0);
        //glTranslated(0.28,0.85,0.28);
        Bat();
    glPopMatrix();
}

void renderScene(void)
{

// Clear Color and Depth Buffers

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
glLoadIdentity();
// Set the camera
//ACTUAL GONNA BE
gluLookAt(	x, ly, z,
x+lx, ly, z+lz,
0.0f, ly, 0.0f);

glColor3ub(195,155,195);
glBegin(GL_QUADS);
glVertex3f(-150.0f, 0.0f, -150.0f);
glVertex3f(-150.0f, 0.0f, 150.0f);
glVertex3f( 150.0f, 0.0f, 150.0f);
glVertex3f( 150.0f, 0.0f, -150.0f);
glEnd();


//LIGHTING
/*
	glPushMatrix();

	// Set light position before viewing transformation
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

	// Translate the whole scene out and into view
	glTranslatef(50.0f, 100.0f, 80.0f);

	// Set material color, Red
	// Sun
	glColor3ub(255, 255, 0);
	glutSolidSphere(5.0f, 20, 20);

	// Move the light after we draw the sun!
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

	// Restore the matrix state
	glPopMatrix();	// Modelview matrix
*/
//END OF LIGHTING

glLineWidth(3.0f);
    glBegin(GL_LINES);
        //X-Axis
        glColor3ub(255,0,0);
        glVertex3f(0,0,0);
        glVertex3f(1,0,0);
        //Y-Axis -UP
        glColor3ub(255,255,0);
        glVertex3f(0,0,0);
        glVertex3f(0,1,0);
        //Z-Axis - To wards Camera
        glColor3ub(0,0,255);
        glVertex3f(0,0,0);
        glVertex3f(0,0,1);
    glEnd();
glPushMatrix();

    glTranslated(0,0.11,0);
    glRotated(90,1,0,0);
    glColor3ub(25,255,10);
	glBegin(GL_POLYGON);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=80;
		float x = r * cos(A);
		float y = r * sin(A);

		glVertex2f(x,y );
	}
	glEnd();

glPopMatrix();

glPushMatrix();
    glTranslated(0,0.12,0);
    glRotated(90,1,0,0);
    //30 YARD CIRCLE
    glColor3ub(255,255,255);
	glBegin(GL_LINES);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=30;
		float x = r * cos(A);
		float y = r * sin(A);

		glVertex2f(x,y );
	}
	glEnd();

	//BOUNDARY LINE
    glColor3ub(255,255,255);
	glBegin(GL_LINE_LOOP);
	for(int i=0;i<200;i++)
	{
		float pi=3.1416;
		float A=(i*2*pi)/50 ;
		float r=75;
		float x = r * cos(A);
		float y = r * sin(A);

		glVertex2f(x,y );
	}
	glEnd();

glPopMatrix();

glPushMatrix();
    glTranslated(0,0.12,0);
    //22 YARD PITCH
    glColor3ub(224,203,103);
    glBegin(GL_QUADS);
    glVertex3f(-1.8f, 0.0f, -11.0f);
    glVertex3f(-1.8f, 0.0f, 11.0f);
    glVertex3f( 1.8f, 0.0f, 11.0f);
    glVertex3f( 1.8f, 0.0f, -11.0f);
    glEnd();

glPopMatrix();

glPushMatrix();

    //CREASE LINE
    glLineWidth(3.0f);
    glBegin(GL_LINES);
        //BATSMAN END
        glColor3ub(255,255,255);
        glVertex3f(2.0f,0.13f,-10.0f);
        glVertex3f(-2.0f,0.13f,-10.0f);
        //BOWLER END
        glColor3ub(255,255,255);
        glVertex3f(2.0f,0.13f,10.0f);
        glVertex3f(-2.0f,0.13f,10.0f);
    glEnd();

    //WIDE LINE
    glLineWidth(3.0f);
    glBegin(GL_LINES);
        //BATSMAN END
        glColor3ub(255,255,255);
        glVertex3f(1.4f,0.13f,-9.8f);
        glVertex3f(1.4f,0.13f,-11.5f);
        //BATSMAN END
        glColor3ub(255,255,255);
        glVertex3f(-1.4f,0.13f,-9.8f);
        glVertex3f(-1.4f,0.13f,-11.5f);
        //BOWLER END
        glColor3ub(255,255,255);
        glVertex3f(1.4f,0.13f,9.8f);
        glVertex3f(1.4f,0.13f,11.5f);
        //BOWLER END
        glColor3ub(255,255,255);
        glVertex3f(-1.4f,0.13f,9.8f);
        glVertex3f(-1.4f,0.13f,11.5f);
    glEnd();

glPopMatrix();

//1st Stamp - BATSMAN SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(-0.1,0,-10.8);
    WicketStamp();
glPopMatrix();

//2nd Stamp - BATSMAN SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(0,0,-10.8);
    WicketStamp();
glPopMatrix();

//3rd Stamp - BATSMAN SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(0.1,0,-10.8);
    WicketStamp();
glPopMatrix();

//1st Stamp - BOWLER SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(-0.1,0,10.8);
    WicketStamp();
glPopMatrix();

//2nd Stamp - BOWLER SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(0,0,10.8);
    WicketStamp();
glPopMatrix();

//3rd Stamp - BOWLER SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(0.1,0,10.8);
    WicketStamp();
glPopMatrix();

//1st BELL - BATSMAN SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(0.1,0.81,-10.8);
    //glRotated(45,0.0,0.0,-1.0);
    WicketBell();
glPopMatrix();

//2nd BELL - BATSMAN SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(-0.01,0.81,-10.8);
    //glRotated(45,0.0,0.0,-1.0);
    WicketBell();
glPopMatrix();

//1st BELL - BOWLER SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(0.1,0.81,10.8);
    //glRotated(45,0.0,0.0,-1.0);
    WicketBell();
glPopMatrix();

//2nd BELL - BOWLER SIDE
glPushMatrix();
    //glCallList(WicketStamp);
    glTranslated(-0.01,0.81,10.8);
    //glRotated(45,0.0,0.0,-1.0);
    WicketBell();
glPopMatrix();

//1ST BATSMAN
glPushMatrix();

    glTranslated(batsMan_Movement,0.0f,0.0f);
    //glRotated(batsMan_Handy,0.0f,1.0f,0.0f);
    if(batsMan_Handy==180.f)
    {
        glRotated(batsMan_Handy,0.0f,1.0f,0.0f);
        glTranslatef(0.0f,0.0f,19.5f);
    }
    BatsMan();

glPopMatrix();

glutSwapBuffers();

}
//MZS ADDED
void update(int value)
{
    //gluLookAt(5.0,camPosy,5.0,0.0,0.0,0.0,0.0,1.0,0.0glMatrixMode(GL_MODELVIEW);
    /*_angle1+=1.0f;
    if(speed>1 || speed<0)
    {
        //cout<<speed<<endl;
        speed = 0.0f;
    }
	_angle1 += speed;
	if (_angle1 > 360) {
		_angle1 -= 360;
	}*/

	//cout<<"_angle : "<<_angle1<<endl;

    glLoadIdentity();
    //gluLookAt(camPosX,camPosy,camPosZ,0.0,0.0,0.0,1.0,0.0,0.0);
    //MZS ADDED
    gluLookAt(	x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f);
    glutPostRedisplay();
    glutTimerFunc(25,update,0);
}

void processNormalKeys(unsigned char key, int x, int y)
{

if (key == 27)
{
   exit(0);
}
else if((key == 'w')||(key == 'W'))
{

}
else if((key == 's')||(key == 'S'))
{

}
else if((key == 'a')||(key == 'A'))
{
    if(batsMan_Movement>-0.8)
    {
        batsMan_Movement-=0.1f;
    }
}
else if((key == 'd')||(key == 'D'))
{
    if(batsMan_Movement<0.8)
    {
        batsMan_Movement+=0.1f;
    }
}
else if((key == 'n')||(key == 'N'))
{

}
else if((key == 'h')||(key == 'H'))
{
    if(batsMan_Handy==0.0f)
    {
        batsMan_Handy=180.0f;
    }
    else if(batsMan_Handy==180.0f)
    {
        batsMan_Handy=0.0f;
    }
}
}

void processSpecialKeys(int key, int xx, int yy)
{

//float fraction = 0.1f;
float fraction = 0.5f;

switch (key) {
case GLUT_KEY_LEFT :
//angle -= 0.01f;
angle -= 0.1f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_RIGHT :
//angle += 0.01f;
angle += 0.1f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_UP :
x += lx * fraction;
z += lz * fraction;
break;
case GLUT_KEY_DOWN :
x -= lx * fraction;
z -= lz * fraction;
break;
}

cout<<" ----------------------------------- "<<endl;
cout<<"Angle : "<<angle<<endl;
cout<<"Fraction : "<<fraction<<endl;

cout<<"X: "<<x<<endl;
//cout<<"Y: "<<y<<endl;
cout<<"Z: "<<z<<endl;
cout<<"lx: "<<lx<<endl;
//cout<<"ly: "<<ly<<endl;
cout<<"lz: "<<lz<<endl;
cout<<" ----------------------------------- "<<endl;
cout<<"FPS: "<<gFramesPerSecond<<endl;


}

void init(void)
{
glClearColor(0,0,0,1.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);

//LIGHTING PURPOSE
/*
	glFrontFace(GL_CCW);		// Counter clock-wise polygons face out

	// Enable lighting
	glEnable(GL_LIGHTING);

	// Setup and enable light 0
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
	glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
	glEnable(GL_LIGHT0);

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);

	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
*/
//LIGHTING PURPOSE ENDED

//TEXTURE STUFFS
   // Create texture index array.
   /*glGenTextures(2, texture);

   // Load external texture and generate and load procedural texture.
   loadExternalTextures();
   createChessboard();
   loadProceduralTextures();

   // Turn on OpenGL texturing.
   glEnable(GL_TEXTURE_2D);

   // Specify how texture values combine with current surface color values.
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//TEXTURE STUFFS ENDED
*/
}

int main(int argc, char **argv)
{

// init GLUT and create window

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1180,620);
glutCreateWindow("Lighthouse3D - GLUT Tutorial");


//ADDED FOR FPS
//glutIdleFunc(idle);
//END OF ADDED FOR FPS

// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
//ADDED FOR TREE
init();
//END OF ADDED FOR TREE

glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);

// OpenGL init
//glEnable(GL_DEPTH_TEST);

//Mzs added
//glutTimerFunc(5,update,0);
// enter GLUT event processing cycle
glutMainLoop();

return 1;
}