Guest User

Untitled

a guest
Feb 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. /*
  2.  * korytnacka_klavesnica.cpp
  3.  *
  4.  *  Created on: Sep 30, 2011
  5.  *      Author: miro
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11. //#include "glut.h"
  12. #include <GL/glut.h>
  13.  
  14. static int KROK = 10;
  15. static int UHOL = 90;
  16. static int W_WIDTH = 800;
  17. static int W_HEIGHT = 600;
  18.  
  19. int prevx = 0;
  20. int prevy = 0;
  21.  
  22. void initSettings() {
  23.     srand(time(NULL));
  24.  
  25.     glLoadIdentity();
  26.  
  27.     glEnable (GL_DEPTH_TEST);
  28.     glEnable(GL_COLOR_MATERIAL);
  29.     glShadeModel (GL_SMOOTH);
  30.  
  31.     glMatrixMode(GL_MODELVIEW);
  32.  
  33. //  glScalef(SCALE, SCALE, SCALE);
  34. //  glTranslatef(0, -(1/SCALE)+5, 0);
  35. //  glColor3fv(TREE_COLOR);
  36.  
  37. //  glRotatef(70, 1, 0, 0);
  38.     //glRotatef(70, 0, 1, 0);
  39. //  glRotatef(25, 0, 0, 1);
  40. //  glScalef(0.7, 0.7, 0.7);
  41. //  glColor3f(1, 0, 0);
  42.  
  43.         glColor3f(0, 1, 0);
  44.         glScalef(1/100.0, 1/100.0, 1/100.0);
  45. }
  46.  
  47. void forward(float x) {
  48.     glBegin(GL_LINES);
  49.         glVertex3f(0, 0, 0);
  50.         glVertex3f(0, x, 0);
  51.     glEnd();
  52.  
  53.     glTranslatef(0, x, 0);
  54. }
  55.  
  56. void rotate(float angle) {
  57.     glRotatef(angle, 0, 0, 1);
  58. }
  59.  
  60.  
  61. //void rectangle() {
  62. //  forward(40);
  63. //  rotate(90);
  64. //  forward(40);
  65. //  rotate(90);
  66. //  forward(40);
  67. //  rotate(90);
  68. //  forward(40);
  69. //}
  70. //
  71. //void triangle() {
  72. //  rotate(90);
  73. //  forward(40);
  74. //  rotate(60);
  75. //  forward(40);
  76. //}
  77.  
  78.  
  79. void display() {
  80.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.  
  82. //  glColor3f(1, 0, 0);
  83. //  glScalef(1/100.0, 1/100.0, 1);
  84. //  glLineWidth(2);
  85.  
  86. //  rectangle();
  87.  
  88. //  triangle();
  89. //  rectangle();
  90.  
  91.     glFlush();
  92.  
  93. }
  94.  
  95. void keyboard(unsigned char key, int x, int y)
  96. {
  97.     printf("pressed: %c\n",key);
  98.  
  99.     switch (key)
  100.     {
  101.     case 'w':
  102.           forward(KROK);
  103.           break;
  104.     case 's':
  105.           forward(-KROK);
  106.           break;
  107.     case 'a':{
  108. //          rotate(UHOL);
  109.           // skuste si namiesto toho nasledovne:
  110.           // otoc(90); dopredu(krok); otoc(-90);
  111.         rotate(90);
  112.         forward(KROK);
  113.         rotate(-90);
  114.         } break;
  115.     case 'd':{
  116. //          rotate(-UHOL);
  117.           // otoc(-90); dopredu(krok); otoc(90);
  118.         rotate(-90);
  119.         forward(KROK);
  120.         rotate(90);
  121.         } break;
  122.     }
  123.     glFlush();
  124. }
  125.  
  126. // je potrebná zmena súradníc, oknové súradnice majú [0,0] vľavo hore,
  127. // vaša súradnicová sústava máva nulu v strede okna !!
  128. void mouseMotion(int x, int y)
  129. {
  130.     // teraz sa vypisu "oknove" suradnice
  131.     printf("x: %d, y: %d\n",x,y);
  132.     // TODO: prevedte do svojej suradnej sustavy
  133.     // ...
  134.     int g_x = x - W_WIDTH/2;
  135.     int g_y = -(y - W_HEIGHT/2);
  136.  
  137.     printf("g_x: %d, g_y: %d\n", g_x, g_y);
  138. }
  139.  
  140.  
  141. void mouseButton(int button, int state, int x, int y)
  142. {
  143.   if (button == GLUT_LEFT_BUTTON)
  144.     {
  145.         if (state == GLUT_DOWN) {
  146.             int g_x = x - W_WIDTH/2;
  147.             int g_y = -(y - W_HEIGHT/2);
  148.             printf("click: g_x: %d, g_y: %d",g_x,g_y);
  149.             g_x = g_x/4.0;
  150.             g_y = g_y/3.0;
  151.             glBegin(GL_LINES);
  152.               glVertex3d(prevx,prevy,0);
  153. //              glVertex3d(0,0,0);
  154.               glVertex3d(g_x,g_y,0);
  155.             glEnd();
  156.             glFlush();
  157.             prevx = g_x;
  158.             prevy = g_y;
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. int main(int argc, char **argv) {
  165.     glutInit(&argc, argv);
  166.     glutInitWindowSize(W_WIDTH, W_HEIGHT);
  167.     glutCreateWindow("korytnacka - klavesnica");
  168.  
  169.     initSettings();
  170.  
  171.     glutDisplayFunc(display);
  172.  
  173.     glutKeyboardFunc(keyboard);
  174.     glutPassiveMotionFunc(mouseMotion);
  175.     glutMouseFunc(mouseButton);
  176.  
  177.  
  178.     glutMainLoop();
  179.     return 0;
  180. }
Add Comment
Please, Sign In to add comment