Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. #include <OpenGL/gl.h>
  6. #include <GLUT/GLUT.h>
  7.  
  8. #define CX_HEIGHT 5
  9. #define CX_WIDTH 5 // tamanho da caixa de clique
  10.  
  11. using namespace std;
  12.  
  13. const int WINDOW_W = 500;
  14. const int WINDOW_H = 500;
  15.  
  16. void display(); // funcao de impressao na tela
  17. void handleKeypress(unsigned char key, int x, int y);
  18. void handleMouseClick(int button, int state, int x, int y);
  19. void reshape(int w, int h);
  20. void drag(int x, int y);
  21.  
  22. // funcoes auxiliares
  23.  
  24. unsigned long long factorial(int n);
  25. unsigned long long comb(int n, int p);
  26. bool are_same(double a, double b);
  27.  
  28. int vertices = 0;
  29. int a_vertices[30][3] = {0}; // array que ira conter os vertices
  30.  
  31. int indice = -1; // indice do vetor de vertices [caso vertice seja encontrado]
  32.  
  33. int main(int argc, char ** argv)
  34. {
  35. //Inicalizate GLUT and OPENGL
  36. glutInit(&argc, argv);
  37. glutInitWindowPosition(0, 0);
  38. glutInitWindowSize(WINDOW_W, WINDOW_H);
  39. glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
  40.  
  41. // Create Window
  42. glutCreateWindow("Curva de Bezier");
  43.  
  44. glMatrixMode(GL_MODELVIEW);
  45. glLoadIdentity();
  46.  
  47. //Setup GLUT callback Functions
  48. glutDisplayFunc(display);
  49. glutKeyboardFunc(handleKeypress);
  50. glutMouseFunc(handleMouseClick);
  51. glutMotionFunc(drag);
  52. glutReshapeFunc(reshape);
  53.  
  54. //Enter main loop
  55. glutMainLoop();
  56.  
  57. return 0;
  58. }
  59.  
  60. void display()
  61. {
  62. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  63. glLoadIdentity();
  64.  
  65. // glMap1f(GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, vertices, &a_vertices[0][0]);
  66. // glEnable(GL_MAP1_VERTEX_3);
  67. //
  68. // glBegin(GL_LINE_STRIP);
  69. // glColor3f(1.0f, 1.0f, 1.0f);
  70. // for (int i = 0; i <= 30; i++)
  71. // {
  72. // glEvalCoord1f((GLfloat) i/30);
  73. // }
  74. // glEnd();
  75.  
  76. glBegin(GL_LINE_STRIP);
  77. glColor3f(1.0f, 1.0f, 1.0f);
  78.  
  79. double x, y;
  80.  
  81. if (vertices > 1)
  82. {
  83. double t = 0.0;
  84.  
  85. for (t = 0; t <= 1.0; t += 0.01)
  86. {
  87. x = y = 0.0;
  88.  
  89. for (int i = 0; i < vertices; i++)
  90. {
  91. x += comb(vertices-1, i) * pow((1 - t), vertices-1-i) * pow(t, i) * a_vertices[i][0];
  92. y += comb(vertices-1, i) * pow((1 - t), vertices-1-i) * pow(t, i) * a_vertices[i][1];
  93. }
  94.  
  95. glVertex3d(x, y, 0);
  96. //cout << "Ponto2 (" << x << ", " << y << ")" << endl;
  97. //cout << "t: " << t << endl;
  98. }
  99.  
  100. glVertex3d(a_vertices[vertices - 1][0], a_vertices[vertices - 1][1], 0);
  101. }
  102. else
  103. {
  104. x = (double)a_vertices[0][1];
  105. y = (double)a_vertices[0][1];
  106.  
  107. glVertex3d(x, y, 0);
  108. }
  109.  
  110. glEnd();
  111.  
  112. // imprime os pontos de controle
  113.  
  114. glPointSize(5.0f);
  115. glBegin(GL_POINTS);
  116. glColor3f(1.0f, 1.0f, 0.0f);
  117. for (int i = 0; i < vertices; i++)
  118. {
  119. glVertex3d(a_vertices[i][0], a_vertices[i][1], 0);
  120. }
  121. glEnd();
  122.  
  123. // imprime retas base
  124. glBegin(GL_LINE_STRIP);
  125. glColor3f(1.0f, 1.0f, 1.0f);
  126. for (int i = 0; i < vertices; i++)
  127. {
  128. glVertex3d(a_vertices[i][0], a_vertices[i][1], 0);
  129. }
  130. glEnd();
  131.  
  132. glFlush();
  133. }
  134.  
  135. void reshape(int w, int h)
  136. {
  137. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  138.  
  139. glMatrixMode(GL_PROJECTION);
  140. glLoadIdentity();
  141. glOrtho(0.0f, WINDOW_W, WINDOW_H, 0.0f, -5.0, 5.0);
  142. glMatrixMode(GL_MODELVIEW);
  143. glLoadIdentity();
  144. }
  145.  
  146. void handleKeypress(unsigned char key, int x, int y)
  147. {
  148. switch (key)
  149. {
  150. case 27:
  151. exit(0); // fecha o programa quando aperta ESC
  152. }
  153. }
  154.  
  155. void handleMouseClick(int button, int state, int x, int y)
  156. {
  157. if (button == GLUT_LEFT_BUTTON)
  158. {
  159. bool achou = false;
  160.  
  161. if (state == GLUT_DOWN)
  162. {
  163. for (int i = 0; i < vertices; i++)
  164. {
  165. if ((a_vertices[i][0] <= x + CX_WIDTH && a_vertices[i][0] >= x - CX_WIDTH) &&
  166. ((a_vertices[i][1] <= y + CX_HEIGHT) && a_vertices[i][1] >= y - CX_HEIGHT))
  167. {
  168. achou = true;
  169. indice = i;
  170. }
  171. }
  172.  
  173. if (!achou)
  174. {
  175. // coloca ponto na tela
  176. a_vertices[vertices][0] = (float)x;
  177. a_vertices[vertices][1] = (float)y;
  178. a_vertices[vertices][2] = 0;
  179.  
  180. vertices++;
  181.  
  182. glutPostRedisplay(); // avisa que a janela atual deve ser reimpressa
  183. }
  184. }
  185. }
  186. }
  187.  
  188. void drag(int x, int y)
  189. {
  190. a_vertices[indice][0] = x;
  191. a_vertices[indice][1] = y;
  192.  
  193. glutPostRedisplay();
  194. }
  195.  
  196. // funcoes auxiliares
  197.  
  198. bool are_same(double a, double b)
  199. {
  200. return (fabs(a - b) < 0.00001);
  201. }
  202.  
  203. unsigned long long factorial(int n)
  204. {
  205. long long fat;
  206.  
  207. if (n <= 1)
  208. {
  209. fat = 1;
  210. }
  211. else
  212. {
  213. fat = n;
  214.  
  215. while (n > 1)
  216. {
  217. fat *= (n - 1);
  218. n--;
  219. }
  220. }
  221.  
  222. return fat;
  223. }
  224.  
  225. unsigned long long comb(int n, int p)
  226. {
  227. unsigned long long ret = factorial(n)/(factorial(p) * factorial(n - p));
  228.  
  229. return ret;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement