Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include<stdio.h>
  3. #include<math.h>
  4. #include<algorithm>
  5.  
  6. double x=0, y=0, d = .3, z = 0, r, g, b, n1=1, n2=1, n3=1, t1=0,t2=0,t3=0;
  7. bool fiQ, sQ, tQ, fQ;
  8.  
  9. void drawRectangle(){
  10.  
  11. glBegin(GL_QUADS);
  12. glVertex3f(x+d,y+d,z);
  13. glVertex3f(x+d,y-d,z);
  14. glVertex3f(x-d,y-d,z);
  15. glVertex3f(x-d,y+d,z);
  16. glEnd();
  17. }
  18.  
  19. void changeColor(){
  20. if(fiQ) glColor3f(1,1,0);
  21. if(sQ) glColor3f(1,0,1);
  22. if(tQ) glColor3f(0,1,1);
  23. if(fQ) glColor3f(.7,.3,.2);
  24. }
  25.  
  26. void display (void) {
  27.  
  28. glClearColor (0.0,0.0,0.0,1.0);
  29. glClear (GL_COLOR_BUFFER_BIT);
  30. glLoadIdentity();
  31. gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  32.  
  33. glScalef(n1,n2,n3);
  34. glTranslatef(t1,t2,t3);
  35. changeColor();
  36. drawRectangle();
  37.  
  38. glFlush();
  39. glutSwapBuffers();
  40. }
  41.  
  42. void reshape (int w, int h) {
  43.  
  44. glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  45. glMatrixMode (GL_PROJECTION);
  46. glLoadIdentity ();
  47. gluPerspective (30, 1, 1.0, 100.0);
  48. glMatrixMode (GL_MODELVIEW);
  49.  
  50. }
  51.  
  52.  
  53. void keyboard(unsigned char key, int x, int y){
  54. if(key=='R'||key=='r'){
  55. glColor3f(1,0,0);
  56. }
  57. if(key=='G'||key=='g'){
  58. glColor3f(0,1,0);
  59. }
  60. if(key=='B'||key=='b'){
  61. glColor3f(0,0,1);
  62. }
  63. if(key=='E'||key=='e'){
  64. n1 += .1;
  65. n2 += .1;
  66. if(n1>2) n1=2;
  67. if(n2>2) n2 = 2;
  68. }
  69. if(key=='S'||key=='s'){
  70. n1 -= .1;
  71. n2 -= .1;
  72. if(n1<0) n1 = 0;
  73. if(n2<0) n2 = 0;
  74. }
  75.  
  76. //glutPostRedisplay();
  77. }
  78.  
  79. void specialKeyboard(int key, int x, int y){
  80. if(key==GLUT_KEY_UP){
  81. t2+=.1;
  82. if(t2>2)t2=2;
  83. }
  84.  
  85. if(key==GLUT_KEY_DOWN){
  86. t2-=.1;
  87. if(t2<-2)t2=-2;
  88. }
  89. if(key==GLUT_KEY_LEFT){
  90. t1-=.1;
  91. if(t1<-2)t1=-2;
  92. }
  93.  
  94. if(key==GLUT_KEY_RIGHT){
  95. t1+=.1;
  96. if(t1>2)t1=2;
  97. }
  98.  
  99.  
  100. fiQ=sQ=tQ=fQ=false;
  101. if(x<=400){
  102. if(y<=400){
  103. sQ=true;
  104. }
  105. else tQ=true;
  106. }
  107. else{
  108. if(y<=400) fiQ=true;
  109. else fQ=true;
  110. }
  111.  
  112. printf("%d %d\n",x,y);
  113. }
  114.  
  115. void main (int argc, char **argv)
  116. {
  117. glutInit(&argc, argv);
  118. glutInitDisplayMode(GLUT_SINGLE);
  119. glutInitWindowSize(800,800);
  120. glutInitWindowPosition(250,30);
  121. glutCreateWindow("Rotation");
  122. glutDisplayFunc(display);
  123. glutReshapeFunc(reshape);
  124.  
  125. glutIdleFunc(display);
  126. glutKeyboardFunc(keyboard);
  127. glutSpecialFunc(specialKeyboard);
  128.  
  129. glutMainLoop();
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement