Guest User

Untitled

a guest
Feb 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1.  
  2. #include <GL/glut.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. // Define a constant for the value of PI
  8. #define GL_PI 3.1415f
  9.  
  10. static int MenuID, IdleMenu;
  11. static int IdlePrint = 0;
  12.  
  13. GLfloat xRot = 0.0f;
  14. GLfloat yRot = 0.0f;
  15. GLfloat rot = 0;
  16. GLfloat speed = 1;
  17.  
  18. void Csonka(double radius)
  19. {
  20. /*
  21. n - hany oldalu szabalyos sokszoggel kozelitunk (n legalább 3)
  22. radius - a sokszog kore irhato kor sugara
  23. x, y - a sokszog kore irhato kor kozeppontjanak koordinatai
  24. */
  25.  
  26. int i;
  27. GLfloat angle;
  28. int szin = 1;
  29.  
  30. glBegin(GL_QUAD_STRIP);
  31. for(i = 0, angle = 0.0; i <= 16; i++, angle += (GL_PI / 8), szin++)
  32. {
  33. if (szin%2 == 0)
  34. glColor3f(1,0,0);
  35. else
  36. glColor3f(0,0,1);
  37.  
  38. glVertex3f((radius-4) * cos(angle), (radius-4) * sin(angle), 10);
  39. glVertex3f(radius * cos(angle), radius * sin(angle), 0);
  40. }
  41. glEnd();
  42.  
  43.  
  44. glBegin(GL_POLYGON);
  45. for(i = 0, angle = 0.0; i <= 16; i++, angle += (GL_PI / 8))
  46. {
  47. glColor3f(1,1,0);
  48. glVertex3f((radius-4) * cos(angle), (radius-4) * sin(angle), 10);
  49. }
  50. glEnd();
  51. }
  52.  
  53. void BitmapText( GLfloat x, GLfloat y, char *string )
  54. {
  55. int len, i;
  56. glRasterPos2f( x, y );
  57. len = (int) strlen ( string );
  58. for ( i = 0; i < len; i++ ){
  59. glutBitmapCharacter ( GLUT_BITMAP_HELVETICA_18, string[i] );
  60. }
  61. }
  62.  
  63.  
  64.  
  65.  
  66. // Called to draw scene
  67. void RenderScene(void)
  68. {
  69. // Clear the window with current clearing color
  70. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  71.  
  72. glPushMatrix();
  73. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  74. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  75.  
  76. // >> Modellezo programresz
  77.  
  78. //Porond
  79. glShadeModel(GL_FLAT);
  80. Csonka (10);
  81.  
  82. //Kocka
  83. glColor3f(0,1,0);
  84. glPushMatrix();
  85. glRotatef(rot, 0,0,1);
  86. glPushMatrix();
  87.  
  88. glTranslatef(0,0,14);
  89. glRotatef(45,1,1,0);
  90. glutWireCube(5);
  91. glPopMatrix();
  92. glPopMatrix();
  93.  
  94. BitmapText(-10, 20, "Balazsfalvi Eva");
  95. BitmapText(-20, -15, "+ a gyorsitashoz");
  96. BitmapText(-20, -20, "- a lassitashoz");
  97. // << Modellezo programresz
  98.  
  99. glPopMatrix();
  100.  
  101. // Flush drawing commands
  102. glutSwapBuffers();
  103. }
  104.  
  105. // This function does any needed initialization on the rendering
  106. // context.
  107. void SetupRC()
  108. {
  109. // Black background
  110. glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  111. glEnable(GL_DEPTH_TEST);
  112. }
  113.  
  114. void SpecialKeys(int key, int x, int y)
  115. {
  116. // ...
  117. int state;
  118.  
  119. if(key == GLUT_KEY_UP)
  120. xRot-= 5.0f;
  121.  
  122. if(key == GLUT_KEY_DOWN)
  123. xRot += 5.0f;
  124.  
  125. if(key == GLUT_KEY_LEFT)
  126. yRot -= 5.0f;
  127.  
  128. if(key == GLUT_KEY_RIGHT)
  129. yRot += 5.0f;
  130.  
  131. if(xRot > 356.0f)
  132. xRot = 0.0f;
  133.  
  134. if(xRot < 0.0f)
  135. xRot = 355.0f;
  136.  
  137. if(yRot > 356.0f)
  138. yRot = 0.0f;
  139.  
  140. if(yRot < -1.0f)
  141. yRot = 355.0f;
  142.  
  143. printf("Funkciobillentyu lenyomva, kodja %d, pozicio (%d,%d). ", key, x, y);
  144. state = glutGetModifiers();
  145. if(state & GLUT_ACTIVE_SHIFT)
  146. printf("SHIFT lenyomva. ");
  147. if(state & GLUT_ACTIVE_CTRL)
  148. printf("CTRL lenyomva. ");
  149. if(state & GLUT_ACTIVE_ALT)
  150. printf("ALT lenyomva. ");
  151. printf("\n");
  152. fflush(stdout);
  153.  
  154. // Refresh the Window
  155. glutPostRedisplay();
  156. }
  157.  
  158. void Keyboard(unsigned char key, int x, int y)
  159. {
  160. // ...
  161. int state;
  162.  
  163. printf("Billentyu lenyomva, kodja %c, pozicio (%d,%d). ", key, x, y);
  164. state = glutGetModifiers();
  165. if(state & GLUT_ACTIVE_SHIFT)
  166. printf("SHIFT lenyomva. ");
  167. if(state & GLUT_ACTIVE_CTRL)
  168. printf("CTRL lenyomva. ");
  169. if(state & GLUT_ACTIVE_ALT)
  170. printf("ALT lenyomva. ");
  171. if((key == 27) || (key == 'q')){
  172. printf("\n");
  173. exit(0);
  174. }
  175.  
  176. if(key == '+'){
  177. speed +=0.5;
  178. }
  179.  
  180. if(key == '-'){
  181. speed -=0.5;
  182. }
  183. printf("\n");
  184. fflush(stdout);
  185.  
  186. glutPostRedisplay();
  187. }
  188.  
  189. void Timer(int value)
  190. {
  191. printf("Timer esemeny (%d)\n", value);
  192.  
  193. glutPostRedisplay();
  194. glutTimerFunc(100, Timer, value + 1);
  195.  
  196. if(rot == 360){
  197. rot = 10*speed;
  198. }else{
  199. rot = rot + (10*speed);
  200. }
  201. }
  202.  
  203. void Idle()
  204. {
  205. if(IdlePrint)
  206. printf("Idle esemeny.\n");
  207.  
  208. glutPostRedisplay();
  209. }
  210.  
  211. void ChangeSizeOrtho(int w, int h)
  212. {
  213. GLfloat nRange = 25.0f;
  214.  
  215. // Prevent a divide by zero
  216. if(h == 0)
  217. h = 1;
  218.  
  219. // Set Viewport to window dimensions
  220. glViewport(0, 0, w, h);
  221.  
  222. // Reset projection matrix stack
  223. glMatrixMode(GL_PROJECTION);
  224. glLoadIdentity();
  225.  
  226. // Establish clipping volume (left, right, bottom, top, near, far)
  227. if (w <= h)
  228. glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
  229. else
  230. glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
  231.  
  232. // Reset Model view matrix stack
  233. glMatrixMode(GL_MODELVIEW);
  234. glLoadIdentity();
  235. }
  236.  
  237. void ChangeSizePerspective(GLsizei w, GLsizei h)
  238. {
  239. GLfloat fAspect;
  240.  
  241. // Prevent a divide by zero
  242. if(h == 0)
  243. h = 1;
  244.  
  245. // Set Viewport to window dimensions
  246. glViewport(0, 0, w, h);
  247.  
  248. fAspect = (GLfloat)w/(GLfloat)h;
  249.  
  250. // Reset coordinate system
  251. glMatrixMode(GL_PROJECTION);
  252. glLoadIdentity();
  253.  
  254. // Produce the perspective projection
  255. gluPerspective(60.0f, // fovy
  256. fAspect, // aspect
  257. 10.0, // zNear
  258. 100.0 // zFar
  259. );
  260. gluLookAt(0.0, 0.0, 50.0, // eye
  261. 0.0, 0.0, 0.0, // center
  262. 0.0, 1.0, 0.0 // up
  263. );
  264.  
  265. glMatrixMode(GL_MODELVIEW);
  266. glLoadIdentity();
  267. }
  268.  
  269. void ProcessMenu(int value)
  270. {
  271. switch(value)
  272. {
  273. case 1:
  274. printf("1. menupont kivalasztva.\n");
  275. break;
  276.  
  277. case 2:
  278. printf("2. menupont kivalasztva.\n");
  279. break;
  280.  
  281. case 3:
  282. printf("Idle kiiratas bekapcsolva.\n");
  283. IdlePrint = 1;
  284. break;
  285.  
  286. case 4:
  287. printf("Idle kiiratas kikapcsolva.\n");
  288. IdlePrint = 0;
  289. break;
  290.  
  291. case 5:
  292. exit(0);
  293.  
  294. default:
  295. break;
  296. }
  297.  
  298. glutPostRedisplay();
  299. }
  300.  
  301. int main(int argc, char* argv[])
  302. {
  303. // >> Inicializalas
  304.  
  305. glutInit(&argc, argv);
  306. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  307. //glutInitWindowSize(300, 300);
  308. glutCreateWindow("GLUT Alap");
  309.  
  310. // << Inicializalas
  311.  
  312. // >> Callback fuggvenyek
  313.  
  314. glutReshapeFunc(ChangeSizeOrtho); // Parhuzamos vetites
  315. //glutReshapeFunc(ChangeSizePerspective); // Perspektiv vetites
  316.  
  317. glutSpecialFunc(SpecialKeys);
  318. glutKeyboardFunc(Keyboard);
  319. glutDisplayFunc(RenderScene);
  320. glutTimerFunc(1000, Timer, 1); // 1 mp mulva meghivodik a Timer() fuggveny
  321. //glutIdleFunc(Idle); // Idle(), ha nem tortenik semmi
  322.  
  323. // << Callback fuggvenyek
  324.  
  325. // >> Menu
  326.  
  327. IdleMenu = glutCreateMenu(ProcessMenu);
  328. glutAddMenuEntry("Idle kiiratas bekapcsolasa", 3);
  329. glutAddMenuEntry("Idle kiiratas kikapcsolasa", 4);
  330.  
  331. MenuID = glutCreateMenu(ProcessMenu);
  332. glutAddMenuEntry("1. menupont", 1);
  333. glutAddMenuEntry("2. menupont", 2);
  334. glutAddSubMenu("Idle fuggveny", IdleMenu);
  335. glutAddMenuEntry("Kilepes", 5);
  336. glutAttachMenu(GLUT_RIGHT_BUTTON);
  337.  
  338. // << Menu
  339.  
  340. SetupRC();
  341. glutMainLoop();
  342.  
  343. return 0;
  344. }
Add Comment
Please, Sign In to add comment