Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include <windows.h>
  5. #include <glut.h>
  6.  
  7. #define pi (2*acos(0.0))
  8.  
  9. int drawgrid;
  10. int drawaxes;
  11.  
  12. struct point
  13. {
  14. double x,y,z;
  15. };
  16.  
  17. struct point pos;
  18. struct point left;
  19. struct point right;
  20. struct point up;
  21.  
  22. double limit;
  23. double angleX1;
  24. double angleY1;
  25. double angleX2;
  26. double angleY2;
  27.  
  28. double gunlength1;
  29. double gunlength2;
  30. double gunradius1;
  31. double gunradius2;
  32. double walldistance;
  33. double walllength;
  34.  
  35. int numberOfBullets;
  36. struct point bullets[200];
  37.  
  38. bool isRightButtonDown;
  39.  
  40. void drawAxes()
  41. {
  42. if(drawaxes==1)
  43. {
  44. glColor3f(1.0, 1.0, 1.0);
  45. glBegin(GL_LINES);{
  46. glVertex3f( 200,0,0);
  47. glVertex3f(-200,0,0);
  48.  
  49. glVertex3f(0,-200,0);
  50. glVertex3f(0, 200,0);
  51.  
  52. glVertex3f(0,0, 200);
  53. glVertex3f(0,0,-200);
  54. }glEnd();
  55. }
  56. }
  57.  
  58. void drawGun1(double height, double radius, double slices, double stacks) {
  59. glPushMatrix();
  60. glRotatef(90,0,1,0);
  61. glTranslatef(0,0,-radius);
  62.  
  63. struct point points[100][100];
  64. int i,j;
  65. double h,r;
  66.  
  67. for(i=0;i<=stacks;i++)
  68. {
  69. h=radius*sin(((double)i/(double)stacks)*(pi/2));
  70. r=radius*cos(((double)i/(double)stacks)*(pi/2));
  71. for(j=0;j<=slices;j++)
  72. {
  73. points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  74. points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  75. points[i][j].z=h;
  76. }
  77. }
  78.  
  79. for(i=0;i<stacks;i++) {
  80. for(j=0;j<slices;j++) {
  81. if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
  82. else glColor3f(0, 0, 0);
  83.  
  84. glBegin(GL_QUADS);
  85. glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  86. glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  87. glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  88. glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  89. glEnd();
  90. }
  91. }
  92.  
  93. for(j=0;j<slices;j++) {
  94. if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
  95. else glColor3f(0, 0, 0);
  96.  
  97. glBegin(GL_QUADS);
  98. glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z);
  99. glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z);
  100. glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z - height);
  101. glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z - height);
  102. glEnd();
  103. }
  104.  
  105. for(i=0;i<stacks;i++) {
  106. for(j=0;j<slices;j++) {
  107. if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
  108. else glColor3f(0, 0, 0);
  109.  
  110. glBegin(GL_QUADS);
  111. glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z - height);
  112. glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z - height);
  113. glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z - height);
  114. glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z - height);
  115. glEnd();
  116. }
  117. }
  118.  
  119. glPopMatrix();
  120. }
  121.  
  122. void drawGun2(double height, double radius, double slices, double stacks) {
  123. glPushMatrix();
  124. glRotatef(90,0,1,0);
  125. glTranslatef(0,0,-radius);
  126.  
  127. struct point points[100][100];
  128. int i,j;
  129. double h,r;
  130. //generate points
  131. for(i=0;i<=stacks;i++)
  132. {
  133. h=radius*sin(((double)i/(double)stacks)*(pi/2));
  134. r=radius*cos(((double)i/(double)stacks)*(pi/2));
  135. for(j=0;j<=slices;j++)
  136. {
  137. points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  138. points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  139. points[i][j].z=h;
  140. }
  141. }
  142.  
  143. for(i=0;i<stacks;i++) {
  144. for(j=0;j<slices;j++) {
  145. if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
  146. else glColor3f(0, 0, 0);
  147.  
  148. glBegin(GL_QUADS);
  149. glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  150. glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  151. glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  152. glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  153. glEnd();
  154. }
  155. }
  156.  
  157. for(j=0;j<slices;j++) {
  158. if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
  159. else glColor3f(0, 0, 0);
  160.  
  161. glBegin(GL_QUADS);
  162. glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z);
  163. glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z);
  164. glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z - height);
  165. glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z - height);
  166. glEnd();
  167. }
  168.  
  169. for(i=0;i<=stacks;i++) {
  170. h=radius*sin(((double)i/(double)stacks)*(pi/2));
  171. r=2*radius - radius*cos(((double)i/(double)stacks)*(pi/2));
  172. for(j=0;j<=slices;j++)
  173. {
  174. points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  175. points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  176. points[i][j].z=h;
  177. }
  178. }
  179.  
  180. for(i=0;i<stacks;i++) {
  181. for(j=0;j<slices;j++) {
  182. if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
  183. else glColor3f(0, 0, 0);
  184.  
  185. glBegin(GL_QUADS);
  186. glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z - height);
  187. glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z - height);
  188. glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z - height);
  189. glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z - height);
  190. glEnd();
  191. }
  192. }
  193.  
  194. glPopMatrix();
  195. }
  196.  
  197. void drawWall(double distance, double length) {
  198. glColor3f(0.5,0.5,0.5);
  199. glBegin(GL_QUADS);
  200. glVertex3f(distance, length/2, length/2);
  201. glVertex3f(distance, -length/2, length/2);
  202. glVertex3f(distance, -length/2, -length/2);
  203. glVertex3f(distance, length/2, -length/2);
  204. glEnd();
  205. }
  206.  
  207. void drawGulis() {
  208. int i;
  209. glColor3f(1.0,0,0);
  210. for(i=0;i<numberOfBullets;i++) {
  211. glBegin(GL_QUADS);
  212. glVertex3f(-bullets[i].x, -(bullets[i].y+3), bullets[i].z+3);
  213. glVertex3f(-bullets[i].x, -(bullets[i].y-3), bullets[i].z+3);
  214. glVertex3f(-bullets[i].x, -(bullets[i].y-3), bullets[i].z-3);
  215. glVertex3f(-bullets[i].x, -(bullets[i].y+3), bullets[i].z-3);
  216. glEnd();
  217. }
  218. }
  219.  
  220. point calculateIntersect() {
  221. double a = gunlength1 + 2*gunradius1;
  222. double b = gunlength2 + 2*gunradius2;
  223. double c = walldistance - 10;
  224.  
  225. point p;
  226. double t = (c - a*cos(angleY1 * pi/180)*cos(angleX1 * pi/180)) / (b*cos(angleY1 * pi/180+angleY2 * pi/180)*cos(angleX1 * pi/180));
  227. p.x = c;
  228. p.y = a*cos(angleY1 * pi/180)*sin(angleX1 * pi/180) + t * b*cos(angleY1 * pi/180+angleY2 * pi/180)*sin(angleX1 * pi/180);
  229. p.z = a*sin(angleY1 * pi/180) + t * b*sin(angleY1 * pi/180+angleY2 * pi/180);
  230.  
  231. if(p.y < -walllength/2 || p.y > walllength/2 || p.z < -walllength/2 || p.z > walllength/2) p.x = 0;
  232. return p;
  233. }
  234.  
  235. point lastpointofgun2() {
  236. double a = gunlength1 + 2*gunradius1;
  237. double b = gunlength2 + 2*gunradius2;
  238.  
  239. point p;
  240. p.x = a*cos(angleY1 * pi/180)*cos(angleX1 * pi/180) + b*cos(angleY1 * pi/180+angleY2 * pi/180)*cos(angleX1 * pi/180);
  241. p.y = a*cos(angleY1 * pi/180)*sin(angleX1 * pi/180) + b*cos(angleY1 * pi/180+angleY2 * pi/180)*sin(angleX1 * pi/180);
  242. p.z = a*sin(angleY1 * pi/180) + b*sin(angleY1 * pi/180+angleY2 * pi/180);
  243.  
  244. return p;
  245. }
  246.  
  247. point getRotatedVector(point a, point b, double angle, bool pls) {
  248. if(!pls) angle = -angle;
  249. point val;
  250. val.x = a.x * cos(angle) + b.x * sin(angle);
  251. val.y = a.y * cos(angle) + b.y * sin(angle);
  252. val.z = a.z * cos(angle) + b.z * sin(angle);
  253. return val;
  254. }
  255.  
  256.  
  257. void rotateVector(int type, int rot) {
  258. double cameraAngle;
  259. if(rot == 1) cameraAngle = 2.0 * (pi / 180);
  260. else if(rot == -1) cameraAngle = -2.0 * (pi / 180);
  261.  
  262. if(type == 1) {
  263. point l = getRotatedVector(left, right, cameraAngle, true);
  264. point r = getRotatedVector(right, left, cameraAngle, false);
  265. left = l;
  266. right = r;
  267. }
  268. else if(type == 2) {
  269. point l = getRotatedVector(left, up, cameraAngle, true);
  270. point u = getRotatedVector(up, left, cameraAngle, false);
  271. left = l;
  272. up = u;
  273. }
  274. else {
  275. point r = getRotatedVector(right, up, cameraAngle, true);
  276. point u = getRotatedVector(up, right, cameraAngle, false);
  277. right = r;
  278. up = u;
  279. }
  280. }
  281.  
  282. void keyboardListener(unsigned char key, int x,int y){
  283. switch(key){
  284.  
  285. case '1':
  286. rotateVector(1,-1);
  287. break;
  288. case '2':
  289. rotateVector(1,1);
  290. break;
  291. case '3':
  292. rotateVector(2,1);
  293. break;
  294. case '4':
  295. rotateVector(2,-1);
  296. break;
  297. case '5':
  298. rotateVector(3,-1);
  299. break;
  300. case '6':
  301. rotateVector(3,1);
  302. break;
  303.  
  304. case 'q':
  305. if(angleX1 < limit)
  306. angleX1 += 2;
  307. break;
  308. case 'w':
  309. if(angleX1 > -limit)
  310. angleX1 -= 2;
  311. break;
  312. case 'e':
  313. if(angleY1 < limit)
  314. angleY1 += 2;
  315. break;
  316. case 'r':
  317. if(angleY1 > -limit)
  318. angleY1 -= 2;
  319. break;
  320. case 'a':
  321. if(angleY2 < limit)
  322. angleY2 += 2;
  323. break;
  324. case 's':
  325. if(angleY2 > -limit)
  326. angleY2 -= 2;
  327. break;
  328. case 'd':
  329. if(angleX2 < limit)
  330. angleX2 += 2;
  331. break;
  332. case 'f':
  333. if(angleX2 > -limit)
  334. angleX2 -= 2;
  335. break;
  336.  
  337. default:
  338. break;
  339. }
  340. }
  341.  
  342.  
  343. void specialKeyListener(int key, int x,int y){
  344. switch(key){
  345. case GLUT_KEY_DOWN:
  346. pos.x-=(left.x*2);
  347. pos.y-=(left.y*2);
  348. pos.z-=(left.z*2);
  349. break;
  350. case GLUT_KEY_UP:
  351. pos.x+=(left.x*2);
  352. pos.y+=(left.y*2);
  353. pos.z+=(left.z*2);
  354. break;
  355.  
  356. case GLUT_KEY_RIGHT:
  357. pos.x+=(right.x*2);
  358. pos.y+=(right.y*2);
  359. pos.z+=(right.z*2);
  360. break;
  361. case GLUT_KEY_LEFT:
  362. pos.x-=(right.x*2);
  363. pos.y-=(right.y*2);
  364. pos.z-=(right.z*2);
  365. break;
  366.  
  367. case GLUT_KEY_PAGE_UP:
  368. pos.x+=(up.x*2);
  369. pos.y+=(up.y*2);
  370. pos.z+=(up.z*2);
  371. break;
  372. case GLUT_KEY_PAGE_DOWN:
  373. pos.x-=(up.x*2);
  374. pos.y-=(up.y*2);
  375. pos.z-=(up.z*2);
  376. break;
  377.  
  378. case GLUT_KEY_INSERT:
  379. break;
  380.  
  381. case GLUT_KEY_HOME:
  382. break;
  383. case GLUT_KEY_END:
  384. break;
  385.  
  386. default:
  387. break;
  388. }
  389. }
  390.  
  391. void add_bullets() {
  392. point p = calculateIntersect();
  393. if(p.x != 0) {
  394. bullets[numberOfBullets++] = p;
  395. }
  396. }
  397.  
  398. void mouseListener(int button, int state, int x, int y){ //x, y is the x-y of the screen (2D)
  399. switch(button){
  400. case GLUT_LEFT_BUTTON:
  401. if(state == GLUT_DOWN){ // 2 times?? in ONE click? -- solution is checking DOWN or UP
  402. add_bullets();
  403. }
  404. break;
  405.  
  406. case GLUT_RIGHT_BUTTON:
  407. if(state == GLUT_DOWN){ // 2 times?? in ONE click? -- solution is checking DOWN or UP
  408. isRightButtonDown = TRUE;
  409. }
  410. else {
  411. isRightButtonDown = FALSE;
  412. }
  413. break;
  414.  
  415. case GLUT_MIDDLE_BUTTON:
  416. //........
  417. break;
  418.  
  419. default:
  420. break;
  421. }
  422. }
  423.  
  424. void scope() {
  425. point p = lastpointofgun2();
  426. point q = calculateIntersect();
  427. if(q.x != 0) {
  428. gluLookAt(-p.x, -p.y, p.z, -q.x, -q.y, q.z, 0,0,1);
  429. //printf("p : %lf , %lf, %lf\n",p.x,p.y,p.z);
  430. //printf("q : %lf , %lf, %lf\n",q.x,q.y,q.z);
  431.  
  432. glColor3f(1.0, 1.0, 1.0);
  433. glBegin(GL_LINES);
  434. {
  435. glVertex3f(-q.x,-q.y,q.z + 5);
  436. glVertex3f(-q.x,-q.y,q.z - 5);
  437.  
  438. glVertex3f(-q.x,-q.y - 5,q.z);
  439. glVertex3f(-q.x,-q.y + 5,q.z);
  440. }
  441. glEnd();
  442. }
  443. }
  444.  
  445. void display(){
  446. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  447. glClearColor(0,0,0,0);
  448. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  449.  
  450. glMatrixMode(GL_MODELVIEW);
  451. glLoadIdentity();
  452.  
  453. if(isRightButtonDown) {
  454. scope();
  455. }
  456. else {
  457. gluLookAt(pos.x, pos.y, pos.z, pos.x+left.x, pos.y+left.y, pos.z+left.z, up.x,up.y,up.z);
  458. //printf("pos : %lf , %lf, %lf\n",pos.x,pos.y,pos.z);
  459. //printf("pos+ left : %lf , %lf, %lf\n",pos.x+left.x, pos.y+left.y, pos.z+left.z);
  460. }
  461.  
  462. glMatrixMode(GL_MODELVIEW);
  463.  
  464. drawAxes();
  465. drawWall(-walldistance,walllength);
  466. drawGulis();
  467.  
  468. glRotatef(angleX1,0,0,1);
  469. glRotatef(angleY1,0,1,0);
  470. drawGun1(gunlength1,gunradius1,50,50);
  471.  
  472. glTranslatef(-(gunlength1 + 2*gunradius1),0,0);
  473.  
  474. glRotatef(angleY2,0,1,0);
  475. glRotatef(angleX2,1,0,0);
  476. drawGun2(gunlength2,gunradius2,50,50);
  477.  
  478. glutSwapBuffers();
  479. }
  480.  
  481.  
  482. void animate(){
  483. glutPostRedisplay();
  484. }
  485.  
  486. void init(){
  487. drawgrid=0;
  488. drawaxes=1;
  489. pos.x = 100;
  490. pos.y = 100;
  491. pos.z = 20;
  492. left.x = -1/sqrt(2);
  493. left.y = -1/sqrt(2);
  494. left.z = 0;
  495. right.x = -1/sqrt(2);
  496. right.y = 1/sqrt(2);
  497. right.z = 0;
  498. up.x = 0;
  499. up.y = 0;
  500. up.z = 1;
  501.  
  502. limit = 45;
  503. angleX1 = 0;
  504. angleY1 = 0;
  505. angleX2 = 0;
  506. angleY2 = 0;
  507.  
  508. gunlength1 = 20;
  509. gunlength2 = 40;
  510. gunradius1 = 10;
  511. gunradius2 = 5;
  512. walldistance = 200;
  513. walllength = 150;
  514. numberOfBullets = 0;
  515. isRightButtonDown = FALSE;
  516.  
  517. glClearColor(0,0,0,0);
  518. glMatrixMode(GL_PROJECTION);
  519. glLoadIdentity();
  520. gluPerspective(80, 1, 1, 1000.0);
  521. }
  522.  
  523. int main(int argc, char **argv){
  524. glutInit(&argc,argv);
  525. glutInitWindowSize(500, 500);
  526. glutInitWindowPosition(0, 0);
  527. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
  528.  
  529. glutCreateWindow("My OpenGL First Offline");
  530.  
  531. init();
  532. glEnable(GL_DEPTH_TEST);
  533.  
  534. glutDisplayFunc(display);
  535. glutIdleFunc(animate);
  536. glutKeyboardFunc(keyboardListener);
  537. glutSpecialFunc(specialKeyListener);
  538. glutMouseFunc(mouseListener);
  539.  
  540. glutMainLoop();
  541.  
  542. return 0;
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement