Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

second

  • Loading branch information...
commit ae483f1c6d5032c763e63f74cfca3fc1bde10027 1 parent dc48056
Si Lam authored

Showing 1 changed file with 431 additions and 25 deletions. Show diff stats Hide diff stats

  1. 456  USTNoir4.cpp
456  USTNoir4.cpp
@@ -43,6 +43,34 @@ double z_distance;
43 43
 GLfloat tx, ty, tz;
44 44
 GLfloat rx, ry, rz;
45 45
 
  46
+//////////////////////////////////////////////////
  47
+// // turn the wheel left/right angle
  48
+//////////////////////////////////////////////////
  49
+GLfloat turnAngle;
  50
+GLfloat turnCarAngle = 0;
  51
+
  52
+GLfloat maxTurnWheel = 45.00f;
  53
+
  54
+//////////////////////////////////////////////////
  55
+// current position of the car
  56
+//////////////////////////////////////////////////
  57
+GLfloat currentX;
  58
+GLfloat currentZ;
  59
+/////////////////////////
  60
+// move car
  61
+/////////////////////////
  62
+//GLfloat moveForward;
  63
+GLfloat moveStepZ = 0.0005;
  64
+GLfloat rollangle;
  65
+GLfloat turnEyeAngle;
  66
+
  67
+GLfloat moveStepX, vectorLen;
  68
+
  69
+GLboolean  bmoveForward;
  70
+
  71
+GLint switchcamera;
  72
+GLint turnEyeRight;
  73
+
46 74
 //////////////////////////
47 75
 GLboolean  pointCameraAt;
48 76
 GLfloat lenszoom;
@@ -378,13 +406,25 @@ int generateSphere(float radius, int subdiv){
378 406
 
379 407
 void setupHeadLight()
380 408
 {
381  
-	glUniform4fv(headlight_position, 1, mv*vec4(0, -0.9, 0, 1)); //1));
  409
+	vec4 rightlampSource = vec4(currentX,-0.90,currentZ + 0.05,1); 
  410
+	vec3 rightlampDest   = vec3(currentX,-0.99,currentZ + 0.05); 
  411
+
  412
+	stack.push(mv);
  413
+
  414
+		//mv = mv * Translate(currentX, 0, currentZ);
  415
+		//mv = mv * RotateY(turnCarAngle);
  416
+
  417
+		glUniform4fv(headlight_position, 1, mv*rightlampSource); //1));
  418
+		glUniform4fv(headspot_direction, 1, mv*rightlampDest);//vec4(0, -10, 0));
  419
+
  420
+	mv = stack.pop();
  421
+
382 422
 	glUniform4fv(headlight_color, 1, vec4(1,.8f,.4f,1));
383 423
 	glUniform4fv(headdiffuse_color, 1, vec4(0.8,.8f,.4f,1));
384 424
 	glUniform4fv(headspecular_color, 1, vec4(0.8,.8f,.4f,1));
385 425
 	glUniform4fv(headambient_light, 1, vec4(.2, .2, .2, 1));
386 426
 
387  
-	glUniform4fv(headspot_direction, 1, mv*vec4(0, -10, 0));
  427
+	
388 428
 	glUniform1f(headspot_cutoff, 30);
389 429
 	glUniform1f(headspot_exponent, 10);
390 430
 
@@ -430,18 +470,32 @@ void displayStage(void)
430 470
    
431 471
 }
432 472
 
  473
+void moveHeadLight()
  474
+{
  475
+	//stack.push(mv);
  476
+		
  477
+	
  478
+	vec4 rightlampSource = vec4(currentX,-0.93,currentZ,1); 
  479
+	vec3 rightlampDest   = vec3(currentX,-0.99,currentZ + 0.05); 
  480
+	
  481
+	glUniform4fv(headlight_position, 1, mv*rightlampSource); 
  482
+	glUniform4fv(headspot_direction, 1, mv*rightlampDest);
  483
+	
  484
+	//mv = stack.pop();
  485
+}
433 486
 /////////////////////////////////////////
434 487
 // displayCar
435 488
 /////////////////////////////////////////
436 489
 void displayCar(void)
437 490
 {
438 491
 	stack.push(mv);
439  
-		
440 492
 	
441  
-	//mv = mv * Translate(currentX, 0, currentZ);
442  
-	//mv = mv * RotateY(turnCarAngle);
  493
+	
  494
+	
  495
+	mv = mv * Translate(currentX, 0, currentZ);
  496
+	mv = mv * RotateY(turnCarAngle);
443 497
 	 
444  
-	mv = mv * Translate(0, -0.93, 0.0005); //0.025); // 0.05
  498
+	mv = mv * Translate(0, -0.93, 0.0005); 
445 499
 	//mv = mv * Scale(1.0,0.5,3);
446 500
 	mv = mv * Scale(0.5,0.25,1.5 );
447 501
 	
@@ -451,11 +505,6 @@ void displayCar(void)
451 505
 	glVertexAttrib4fv(vSpecularColor, vec4(0.5f,0.5f,1.0f,1.0f));
452 506
 	glVertexAttrib1f(vSpecularExponent, 10.0);
453 507
 
454  
-	/*glUniform4fv(vCarAmbientColor,1, car.ambient);
455  
-	glUniform4fv(vCarDiffuseColor, 1, car.diffuse);
456  
-	glUniform4fv(vCarSpecularColor, 1, car.specular);
457  
-	glUniform1i(vCarShininess, car.shininess);
458  
-*/
459 508
 
460 509
 	DrawTriagle(carvao, 36);
461 510
 
@@ -491,9 +540,11 @@ void display(void)
491 540
 
492 541
 
493 542
 		displayStage();
494  
-		
  543
+		moveHeadLight();
495 544
 		displayCar();
496 545
 
  546
+		
  547
+
497 548
 	}else{
498 549
 		glBindVertexArray(0);
499 550
 		glutSolidTeapot(2); //not very bandwidth efficient
@@ -610,23 +661,131 @@ void setupStageShader(GLuint prog)
610 661
 	glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
611 662
 }
612 663
 
  664
+/////////////////////////////////////////
  665
+// myIdle
  666
+/////////////////////////////////////////
  667
+void myIdle()
  668
+{
  669
+
  670
+	GLint moveF = 1;
  671
+	if ( bmoveForward == true )
  672
+	{
  673
+		moveF = 1;
  674
+	}
  675
+	else
  676
+	{
  677
+		moveF = -1;
  678
+	}
  679
+
  680
+	
  681
+
  682
+	//chasecamlookdirection = RotateY((moveF)*0.2*(turnAngle)/maxTurnWheel) * chasecamlookdirection;
  683
+	//viewpointcam = RotateY((moveF)*0.2*(turnAngle)/maxTurnWheel) * viewpointcam;
  684
+
  685
+		
  686
+	if ( pointCameraAt == true ) // point the stage
  687
+	{
  688
+		atX = 0;
  689
+		atZ = 0;
  690
+	}
  691
+	else // point at the car
  692
+	{
  693
+		atX = currentX;
  694
+		atZ = currentZ;
  695
+	}
  696
+
  697
+	if ( currentX > -1 && currentX < 1 &&
  698
+		 currentZ > -1 && currentZ < 1 ) 
  699
+	{
  700
+		if ( bmoveForward == true )
  701
+		{
  702
+		
  703
+			if ( turnAngle < 0)
  704
+			{
  705
+				turnCarAngle -= 0.2*abs(turnAngle)/maxTurnWheel;
  706
+			}
  707
+			else if ( turnAngle > 0)
  708
+			{
  709
+				turnCarAngle += 0.2*abs(turnAngle)/maxTurnWheel;
  710
+			}
  711
+			
  712
+	
  713
+			moveStepX = vectorLen * sin(turnCarAngle*M_PI/180);
  714
+			moveStepZ  = vectorLen * cos(turnCarAngle*M_PI/180);
  715
+
  716
+			currentX = currentX + moveStepX;
  717
+			currentZ = currentZ + moveStepZ;
  718
+
  719
+			if ( currentX < -1.0 )
  720
+			{
  721
+				currentX = -1.00f;
  722
+			}
  723
+			else if ( currentX > 1.0 )
  724
+			{
  725
+				currentX = 1.00f;
  726
+			}
  727
+			else if ( currentZ > 1.0 )
  728
+			{
  729
+				currentZ = 1.00f;
  730
+			}
  731
+			else if ( currentZ < -1.0 )
  732
+			{
  733
+				currentZ = -1.00f;
  734
+			}
  735
+			else
  736
+			{
  737
+			
  738
+			}
  739
+
  740
+			rollangle += (vectorLen * 360)/(0.05*M_PI);
  741
+		}
  742
+		else
  743
+		{
  744
+			if ( turnAngle < 0)
  745
+			{
  746
+				turnCarAngle += 0.2*abs(turnAngle)/maxTurnWheel;;
  747
+			}
  748
+			else if ( turnAngle > 0)
  749
+			{
  750
+				turnCarAngle -= 0.2*abs(turnAngle)/maxTurnWheel;;
  751
+			}
  752
+		
  753
+			moveStepX = vectorLen * sin(turnCarAngle*M_PI/180);
  754
+			moveStepZ  = vectorLen * cos(turnCarAngle*M_PI/180);
  755
+
  756
+			currentX = currentX - moveStepX;
  757
+			currentZ = currentZ - moveStepZ;
  758
+
  759
+			if ( currentX < -1.0 )
  760
+			{
  761
+				currentX = -1.00f;
  762
+			}
  763
+			else if ( currentX > 1.0 )
  764
+			{
  765
+				currentX = 1.00f;
  766
+			}
  767
+			else if ( currentZ > 1.0 )
  768
+			{
  769
+				currentZ = 1.00f;
  770
+			}
  771
+			else if ( currentZ < -1.0 )
  772
+			{
  773
+				currentZ = -1.00f;
  774
+			}
  775
+		
  776
+
  777
+			rollangle -= (vectorLen * 360)/(0.05*M_PI);
  778
+		}
  779
+
  780
+		glutPostRedisplay();
  781
+	}
  782
+} 
613 783
 void Keyboard(unsigned char key, int x, int y) {
614 784
 	/*exit when the escape key is pressed*/
615 785
 	if (key == 27)
616 786
 		exit(0);
617 787
 
618  
-	if (key == 'r' || key == 'R' )
619  
-	{
620  
-		 ///////////////////
621  
-		  // assighment 3
622  
-		  ///////////////////
623  
-		  atX = atZ = 0;
624  
-		  dollyzoom = 20.0f;
625  
-		  lenszoom = 45.0;
626  
-
627  
-		  reshape(ww, wh);
628  
-	}
629  
-	else if (key == 'w' || key == 'W' ) // dolly in
  788
+	if (key == 'w' || key == 'W' ) // dolly in
630 789
 	{
631 790
 		dollyzoom += 1;
632 791
 	}
@@ -652,6 +811,139 @@ void Keyboard(unsigned char key, int x, int y) {
652 811
 	if (key == 't'){
653 812
 		mode = 1;
654 813
 	}
  814
+	if (key == 'r' || key == 'R' )
  815
+	{
  816
+		 ///////////////////
  817
+		  // assighment 3
  818
+		  ///////////////////
  819
+		  atX = atZ = 0;
  820
+		  dollyzoom = 20.0f;
  821
+		  lenszoom = 45.0;
  822
+
  823
+		  reshape(ww, wh);
  824
+	}
  825
+	else if (key == 'c' || key == 'C' )
  826
+	{
  827
+		/*if ( switchcamera == 0 )
  828
+		{
  829
+			p = Perspective(45.0, (float)ww/(float)wh, 1.0, 100.0);
  830
+			switchcamera = 1;
  831
+		}
  832
+		else if ( switchcamera == 1 )
  833
+		{
  834
+			p = Perspective(45.0, (float)ww/(float)wh, 1.0, 100.0);
  835
+			switchcamera = 2;
  836
+		}
  837
+		else if ( switchcamera == 2 )
  838
+		{
  839
+			reshape(ww, wh);
  840
+			switchcamera = 0;
  841
+		}
  842
+*/
  843
+	}
  844
+	else if (key == 'f' || key == 'F' ) // toggle b/t look at origin or current car postion
  845
+	{
  846
+		if ( pointCameraAt == true )
  847
+		{
  848
+			pointCameraAt = false;
  849
+			
  850
+		}
  851
+		else
  852
+		{
  853
+			pointCameraAt = true;
  854
+			
  855
+		}
  856
+	}
  857
+	else if (key == 's' || key == 'S' ) // to zoom out
  858
+	{
  859
+
  860
+
  861
+		/*if ( switchcamera == 0)
  862
+		{
  863
+			lenszoom += 1;
  864
+			reshape(ww, wh);
  865
+		}
  866
+		else
  867
+			p = Perspective(45.0, (float)ww/(float)wh, 1.0, 100.0);*/
  868
+			
  869
+	}
  870
+	else if (key == 'a' || key == 'A' ) // to zoom in
  871
+	{
  872
+		
  873
+		/*if ( switchcamera == 0)
  874
+		{
  875
+			lenszoom -= 1;
  876
+			reshape(ww, wh);
  877
+		}
  878
+		else
  879
+			p = Perspective(45.0, (float)ww/(float)wh, 1.0, 100.0);*/
  880
+	}
  881
+	else if (key == 'w' || key == 'W' ) // dolly in
  882
+	{
  883
+		dollyzoom += 1;
  884
+	}
  885
+	else if (key == 'q' || key == 'Q' ) // to dolly out in
  886
+	{
  887
+		dollyzoom -= 1;
  888
+	}
  889
+	if ( key == 'b' || key == 'B' ) // to start the car
  890
+	{
  891
+		glutIdleFunc(myIdle);
  892
+	}
  893
+
  894
+	if ( key == 32 ) // to stop car moving
  895
+	{
  896
+		glutIdleFunc(NULL);
  897
+	}
  898
+	else if(key == 'z')
  899
+	{
  900
+		//turnEyeRight = 1;
  901
+		//turnEyeAngle += 1;
  902
+	}
  903
+	else if(key == 'x')
  904
+	{
  905
+		//turnEyeRight = -1;
  906
+		//turnEyeAngle -= 1;
  907
+	}
  908
+	else if(key == 'i')
  909
+	{
  910
+		rx += 5;
  911
+		if(rx > 360)
  912
+			rx -= 360;
  913
+	}
  914
+	else if(key == 'j'){
  915
+		rx -= 5;
  916
+		if(rx < 0)
  917
+			rx += 360;
  918
+	}
  919
+	else if(key == 'o'){
  920
+		ry += 5;
  921
+		if(ry > 360)
  922
+			ry -= 360;
  923
+	}
  924
+	else if(key == 'k'){
  925
+		ry -= 5;
  926
+		if(ry < 0)
  927
+			ry += 360;
  928
+	}else if(key == 'p'){
  929
+		rz += 5;
  930
+		if(rz > 360)
  931
+			rz -= 360;
  932
+	}else if(key == 'l'){
  933
+		rz -= 5;
  934
+		if(rz < 0)
  935
+			rz += 360;
  936
+	}
  937
+
  938
+	printf("rx = %f\n", rx);
  939
+	printf("ry = %f\n", ry);
  940
+	printf("rz = %f\n", rz);
  941
+
  942
+
  943
+	printf("tx = %f\n", tx);
  944
+	printf("ty = %f\n", ty);
  945
+	printf("tz = %f\n", tz);
  946
+
655 947
 	reshape(ww,wh);
656 948
 	glutPostRedisplay();
657 949
 
@@ -693,6 +985,7 @@ void mouse(int button, int state, int x, int y) {
693 985
 	}
694 986
 }
695 987
 
  988
+
696 989
 void init() {
697 990
 
698 991
   /*select clearing (background) color*/
@@ -709,6 +1002,23 @@ void init() {
709 1002
    rx = 30;
710 1003
   ty = 0.8;
711 1004
   tz = 19.2;
  1005
+  // TRUE: point camear at the center, 
  1006
+  // False: point at the car
  1007
+  pointCameraAt = true;
  1008
+  switchcamera = 0; //
  1009
+  ///////////////////
  1010
+  // assighment 2
  1011
+  ///////////////////
  1012
+  bmoveForward = true;
  1013
+  moveStepZ = 0.0005;
  1014
+  currentX = currentZ = 0;
  1015
+  turnCarAngle = 0;
  1016
+  turnAngle = 0;
  1017
+  rollangle = 0;
  1018
+  turnEyeAngle = 0;
  1019
+    // caculate movestepX and vector len for moving car
  1020
+  moveStepX = tan(M_PI/180) * moveStepZ;
  1021
+  vectorLen = sqrt(moveStepX*moveStepX + moveStepZ*moveStepZ);
712 1022
 
713 1023
   //populate our arrays
714 1024
   spherevertcount = generateSphere(2, 30);
@@ -729,6 +1039,101 @@ void init() {
729 1039
 	glEnable(GL_DEPTH_TEST);
730 1040
 }
731 1041
 
  1042
+/////////////////////////////////////////
  1043
+// special keys
  1044
+/////////////////////////////////////////
  1045
+void special(int key, int x, int y){
  1046
+
  1047
+	// restart the game
  1048
+	if (key == GLUT_KEY_F2)
  1049
+	{
  1050
+		init();
  1051
+		glutIdleFunc(NULL);
  1052
+	}
  1053
+	else if(key == GLUT_KEY_UP) // car move forward
  1054
+	{
  1055
+		bmoveForward = true;
  1056
+				
  1057
+		if ( currentZ >= 1 )
  1058
+		{
  1059
+			currentZ = 0.999;
  1060
+		}
  1061
+		else if ( currentZ <= -1 )
  1062
+		{
  1063
+			currentZ = -0.999;
  1064
+		}
  1065
+
  1066
+		if ( currentX >= 1 )
  1067
+		{
  1068
+			currentX = 0.999;
  1069
+		}
  1070
+		else if ( currentX <= -1 )
  1071
+		{
  1072
+			currentX = -0.999;
  1073
+		}
  1074
+
  1075
+	}
  1076
+	else if(key == GLUT_KEY_DOWN) // car moves backward
  1077
+	{
  1078
+		bmoveForward = false;
  1079
+		if ( currentZ >= 1 )
  1080
+		{
  1081
+			currentZ = 0.999;
  1082
+		}
  1083
+		else if ( currentZ <= -1 )
  1084
+		{
  1085
+			currentZ = -0.999;
  1086
+		}
  1087
+
  1088
+		if ( currentX >= 1 )
  1089
+		{
  1090
+			currentX = 0.999;
  1091
+		}
  1092
+		else if ( currentX <= -1 )
  1093
+		{
  1094
+			currentX = -0.999;
  1095
+		}
  1096
+	}
  1097
+	else if(key == GLUT_KEY_LEFT) // move wheels to left
  1098
+	{
  1099
+		if ( turnAngle >= maxTurnWheel )
  1100
+		{
  1101
+			turnAngle = maxTurnWheel;
  1102
+		}
  1103
+		else
  1104
+		{
  1105
+			turnAngle += 1;
  1106
+		}
  1107
+		
  1108
+	}
  1109
+	else if(key == GLUT_KEY_RIGHT) // move wheels to right
  1110
+	{
  1111
+	
  1112
+		if ( turnAngle <= -maxTurnWheel )
  1113
+		{
  1114
+			turnAngle = -maxTurnWheel;
  1115
+		}
  1116
+		else
  1117
+		{
  1118
+			turnAngle -= 1;
  1119
+		}
  1120
+
  1121
+		
  1122
+	}
  1123
+	
  1124
+	printf("=============================\n");
  1125
+	printf("moveStepX : %f\n", moveStepX);
  1126
+	printf("moveStepZ : %f\n", moveStepZ);
  1127
+	printf("vectorLen : %f\n", vectorLen);
  1128
+	printf("currentX : %f\n", currentX);
  1129
+	printf("currentZ : %f\n", currentZ);
  1130
+	printf("turnCarAngle : %f\n", turnCarAngle);
  1131
+	printf("turnAngle : %f\n", turnAngle);
  1132
+ 	
  1133
+
  1134
+	//don't forget to request a new frame since parameters have changed
  1135
+	glutPostRedisplay();
  1136
+}
732 1137
 int main(int argc, char **argv)
733 1138
 {
734 1139
   /*set up window for display*/
@@ -736,7 +1141,7 @@ int main(int argc, char **argv)
736 1141
   glutInitWindowPosition(0, 0); 
737 1142
   glutInitWindowSize(ww, wh);
738 1143
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
739  
-  glutCreateWindow("Lighting Exercise");  
  1144
+  glutCreateWindow("UST Noir");  
740 1145
 
741 1146
   glewExperimental = GL_TRUE;
742 1147
 
@@ -745,6 +1150,7 @@ int main(int argc, char **argv)
745 1150
 
746 1151
   glutDisplayFunc(display);
747 1152
   glutKeyboardFunc(Keyboard);
  1153
+  glutSpecialFunc(special);
748 1154
   glutReshapeFunc(reshape);
749 1155
   //glutIdleFunc(idle);
750 1156
   glutMouseFunc(mouse);

0 notes on commit ae483f1

Please sign in to comment.
Something went wrong with that request. Please try again.