Advertisement
rakoczyn

[OpenGL] Krzywie Bezier'a / Powierzchnie

Apr 26th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.83 KB | None | 0 0
  1. //#include <windows.h>
  2. //#include <GL/glut.h>
  3. //#include <cmath>
  4. //#define M_PI 3.14159265358979323846
  5. //
  6. //float w=600,h=600;
  7. //float alfa=0,beta=0;
  8. //float x,y,z;
  9. //float r=5;
  10. //float poz1=0,poz2=0;
  11. //
  12. //const GLdouble left = -5.0;
  13. //const GLdouble right = 5.0;
  14. //const GLdouble bottom = -5.0;
  15. //const GLdouble top = 5.0;
  16. //const GLdouble nnear = -5.0;
  17. //const GLdouble ffar = 5.0;
  18. //
  19. //int right_button_state = GLUT_UP ;
  20. //int middle_button_state = GLUT_UP ;
  21. //int left_button_state = GLUT_UP ;
  22. //
  23. //int l_button_x, l_button_y, m_button_x, m_button_y, mr_button;
  24. //
  25. //
  26. //void Keyboard( unsigned char key, int x, int y )
  27. //{
  28. //  if ( key == 27 )
  29. //      exit( EXIT_SUCCESS );
  30. //      switch(key)
  31. //      {
  32. //      case 'w':
  33. //              alfa-=5;
  34. //              break;
  35. //      case 's':
  36. //              alfa+=5;
  37. //              break;
  38. //      case 'a':
  39. //              beta-=5;
  40. //              break;
  41. //      case 'd':
  42. //              beta+=5;
  43. //              break;
  44. //      case 'z':
  45. //              r+=0.1;
  46. //              break;
  47. //      case 'x':
  48. //              r-=0.1;
  49. //              break;
  50. //      case 'e':
  51. //              poz1+=0.1;
  52. //              break;
  53. //      case 'q':
  54. //              poz1-=0.1;
  55. //              break;
  56. //      }
  57. //}
  58. //void SpecialKeys(int key, int x, int y)
  59. //{
  60. //      switch(key)
  61. //      {
  62. //      case GLUT_KEY_UP:
  63. //              alfa+=1;
  64. //              break;
  65. //      }
  66. //}
  67. //
  68. //void Init()
  69. //{
  70. //  glClearColor( 0.1f, 0.1f, 0.1f, 1.0f );
  71. //  glMatrixMode( GL_PROJECTION ) ;
  72. //  glLoadIdentity();
  73. //      gluPerspective(60,w/h,0.1,100);
  74. //      glMatrixMode(GL_MODELVIEW);
  75. //      glLoadIdentity();
  76. //      glEnable(GL_DEPTH_TEST);
  77. //     
  78. //  glutKeyboardFunc( Keyboard );
  79. //}
  80. //
  81. //void Camera()
  82. //{
  83. //      x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
  84. //      y=r*sin(alfa*M_PI/180);
  85. //      z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
  86. //}
  87. //
  88. //void MouseButton( int button, int state, int x, int y)
  89. //{
  90. //      if (button == GLUT_LEFT_BUTTON)
  91. //  {
  92. //      left_button_state = state;
  93. //      if (state == GLUT_DOWN)
  94. //      {
  95. //          l_button_x = x;
  96. //                      l_button_y = y;
  97. //      }
  98. //  }
  99. //
  100. //
  101. //  if (button == GLUT_MIDDLE_BUTTON)
  102. //  {
  103. //      middle_button_state = state;
  104. //      if (state == GLUT_DOWN)
  105. //      {
  106. //                      mr_button = y;
  107. //      }
  108. //  }
  109. //
  110. //      if (button == GLUT_RIGHT_BUTTON)
  111. //  {
  112. //              right_button_state = state;
  113. //      if (state == GLUT_DOWN)
  114. //      {
  115. //          m_button_x = x; // by nie wracal do punktu wyjscia
  116. //                      m_button_y = y;
  117. //      }
  118. //  }
  119. //
  120. //}
  121. //
  122. //void MouseMotion( int x, int y)//te zmiennie ile razy obroci sie kosta przy przejechaniu mysza przez pole obiektu.
  123. //{
  124. //  if (left_button_state == GLUT_DOWN)  // zmiana kata
  125. //  {
  126. //      beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
  127. //              l_button_x = x;
  128. //              alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y );  //normalizacja okna, zachowanie proporcji
  129. //              l_button_y = y;
  130. //
  131. //              glutPostRedisplay();
  132. //  }
  133. //
  134. //      if (middle_button_state == GLUT_DOWN)
  135. //  {
  136. //      r += 0.01 * (ffar - nnear) * (y - mr_button); // zmiana promienia
  137. //      mr_button = y;
  138. //              if (r<1)
  139. //                      r=1;
  140. //      glutPostRedisplay();
  141. //  }
  142. //
  143. //      if (right_button_state == GLUT_DOWN)
  144. //  {
  145. //      poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
  146. //              m_button_x = x;
  147. //              poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
  148. //              m_button_y = y;
  149. //
  150. //              glutPostRedisplay();
  151. //  }
  152. //}
  153. //
  154. //float ctrlPoints[6][3]={
  155. //                      0,-0,0,
  156. //                      -5,-2,1,
  157. //                      -7,3,-3,
  158. //                      7,3,2,
  159. //                      4,-1,0,
  160. //                      0,-5,0
  161. //                      };
  162. //
  163. //void Render()
  164. //{
  165. //     
  166. //glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  167. //
  168. //Camera();
  169. //gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0); //zmiana polozenia kamer
  170. //     
  171. //glMap1f(GL_MAP1_VERTEX_3, //do krzywych
  172. //          0, //poczatek
  173. //          1, //koniec
  174. //          3, //odl pomiedzy kolejnymi wsp punktow kontrolnych
  175. //          6,  //punkty kontrolne brane pod uwage podczas robienia krzywej
  176. //          &ctrlPoints[0][0]); //odwolanie
  177. //      glEnable(GL_MAP1_VERTEX_3);
  178. //      glMapGrid1f(20,0,1);   //generowanie siatku punktow
  179. //      glEvalMesh1(GL_LINE,0,20);
  180. //
  181. //
  182. //glLoadIdentity();
  183. //glutSwapBuffers();
  184. //}
  185. //
  186. //void Idle()
  187. //{
  188. //  Render();
  189. //}
  190. //
  191. //int main( int argc, char **argv )
  192. //{
  193. //      glutInitDisplayMode( GLUT_DOUBLE);
  194. //      glutInitWindowSize(w,h);
  195. //      glutInitWindowPosition(50,50);
  196. //      glutCreateWindow( "OpenGL" );
  197. //  Init();
  198. //      glutIdleFunc( Idle);
  199. //  glutDisplayFunc( Render );
  200. //      glutMouseFunc(MouseButton);
  201. //      glutMotionFunc(MouseMotion);
  202. //      glutSpecialFunc(SpecialKeys);
  203. //  glutMainLoop();
  204. //  return EXIT_SUCCESS;
  205. //}
  206.  
  207. #include <windows.h>
  208. #include <GL/glut.h>
  209. //#include <math.h>
  210. #include <cmath>
  211. #define M_PI 3.14159265358979323846
  212.  
  213. float w=600,h=600;
  214. float alfa=0,beta=0;
  215. float x,y,z;
  216. float r=5;
  217. float poz1=0,poz2=0;
  218.  
  219.  
  220. const GLdouble left = -5.0;
  221. const GLdouble right = 5.0;
  222. const GLdouble bottom = -5.0;
  223. const GLdouble top = 5.0;
  224. const GLdouble nnear = -5.0;
  225. const GLdouble ffar = 5.0;
  226.  
  227. int right_button_state = GLUT_UP ;
  228. int middle_button_state = GLUT_UP ;
  229. int left_button_state = GLUT_UP ;
  230.  
  231. int l_button_x, l_button_y, m_button_x, m_button_y, mr_button;
  232.  
  233.  
  234. float ctrlPoint[3][4][3]= {
  235.                             {{-2,-2,0},  //u0   //v0
  236.                             {-1,-2,0},    //u1
  237.                             {1,-2,0},
  238.                             {2,-2,0}},   //u3
  239.  
  240.                             {{-2,-2,3},  //u0   //v1
  241.                             {-1,2,3},    //u1
  242.                             {1,2,3},
  243.                             {2,-2,3}},   //u3
  244.  
  245.                             {{-2,-2,6},  //u0   //v2
  246.                             {-1,-2,6},    //u1
  247.                             {1,-2,6},
  248.                             {2,-2,6}}    //u3
  249.                             };
  250.  
  251.  
  252.  
  253.  
  254. void Keyboard( unsigned char key, int x, int y )
  255. {
  256.     if ( key == 27 )
  257.         exit( EXIT_SUCCESS );
  258.     switch(key)
  259.     {
  260.     case 'w':
  261.         alfa-=5;
  262.         break;
  263.     case 's':
  264.         alfa+=5;
  265.         break;
  266.     case 'a':
  267.         beta-=5;
  268.         break;
  269.     case 'd':
  270.         beta+=5;
  271.         break;
  272.     case 'z':
  273.         r+=0.1;
  274.         break;
  275.     case 'x':
  276.         r-=0.1;
  277.         break;
  278.     case 'e':
  279.         poz1+=0.1;
  280.         break;
  281.     case 'q':
  282.         poz1-=0.1;
  283.         break;
  284.     }
  285. }
  286. void SpecialKeys(int key, int x, int y)
  287. {
  288.     switch(key)
  289.     {
  290.     case GLUT_KEY_UP:
  291.         alfa+=1;
  292.         break;
  293.     }
  294. }
  295.  
  296. void Init()
  297. {
  298.     glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
  299.     glMatrixMode( GL_PROJECTION ) ;
  300.     glLoadIdentity();
  301.     gluPerspective(60,w/h,0.1,100);
  302.     //glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );
  303.     glMatrixMode(GL_MODELVIEW);
  304.     glLoadIdentity();
  305.     glEnable(GL_DEPTH_TEST);
  306.    
  307.     glutKeyboardFunc( Keyboard );
  308. }
  309.  
  310. void Camera()
  311. {
  312.     x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
  313.     y=r*sin(alfa*M_PI/180);
  314.     z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
  315. }
  316.  
  317. void MouseButton( int button, int state, int x, int y)
  318. {
  319.     if (button == GLUT_LEFT_BUTTON)
  320.     {
  321.         left_button_state = state;
  322.         if (state == GLUT_DOWN)
  323.         {
  324.             l_button_x = x;
  325.             l_button_y = y;
  326.         }
  327.     }
  328.  
  329.  
  330.     if (button == GLUT_MIDDLE_BUTTON)
  331.     {
  332.         middle_button_state = state;
  333.         if (state == GLUT_DOWN)
  334.         {
  335.             mr_button = y;
  336.         }
  337.     }
  338.  
  339.     if (button == GLUT_RIGHT_BUTTON)
  340.     {
  341.         right_button_state = state;
  342.         if (state == GLUT_DOWN)
  343.         {
  344.             m_button_x = x;
  345.             m_button_y = y;
  346.         }
  347.     }
  348.  
  349. }
  350.  
  351. void MouseMotion( int x, int y)
  352. {
  353.     if (left_button_state == GLUT_DOWN)
  354.     {
  355.         beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
  356.         l_button_x = x;
  357.         alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y );
  358.         l_button_y = y;
  359.  
  360.         glutPostRedisplay();
  361.     }
  362.  
  363.     if (middle_button_state == GLUT_DOWN)
  364.     {
  365.         r += 0.01 * (ffar - nnear) * (y - mr_button);
  366.         mr_button = y;
  367.         if (r<1)
  368.             r=1;
  369.         glutPostRedisplay();
  370.     }
  371.  
  372.     if (right_button_state == GLUT_DOWN)
  373.     {
  374.         poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
  375.         m_button_x = x;
  376.         poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
  377.         m_button_y = y;
  378.  
  379.         glutPostRedisplay();
  380.     }
  381. }
  382.  
  383. void Render()
  384. {
  385.     glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  386.  
  387.     Camera();
  388.     gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0);
  389.    
  390.     //glutWireCube(1);
  391.     glColor3f(1,0,0);
  392.     glMap2f(GL_MAP2_VERTEX_3,
  393.             0.0f,  //początek przedziału u
  394.             1.0f,  //koniec przedziału u
  395.             3,     //odległość wsp. pktctrl
  396.             4,     //max liczba pkt w danym zbiorze pkt (parametr u)
  397.  
  398.             0.0f,  //początek przedziału v
  399.             1.0f,  //koniec przedziału v
  400.             12,     //odległość wsp. pktctrl
  401.             3,     //max liczba pkt w danym zbiorze pkt (parametr v)
  402.             &ctrlPoint[0][0][0]);
  403.  
  404.     glEnable(GL_MAP2_VERTEX_3);
  405.  
  406.     glMapGrid2f(20,0,1,20,0,1);
  407.     glEvalMesh2(GL_LINE,0,20,0,20);
  408.  
  409.    
  410.     glLoadIdentity();
  411.    
  412.     glutSwapBuffers();
  413. }
  414.  
  415. void Idle()
  416. {
  417.     Render();
  418. }
  419.  
  420. int main( int argc, char **argv )
  421. {
  422.     glutInitDisplayMode( GLUT_DOUBLE);
  423.     glutInitWindowSize(w,h);
  424.     glutInitWindowPosition(50,50);
  425.     glutCreateWindow( "OpenGL" );
  426.     Init();
  427.     glutIdleFunc( Idle);
  428.     glutDisplayFunc( Render );
  429.     glutMouseFunc(MouseButton);
  430.     glutMotionFunc(MouseMotion);
  431.     glutSpecialFunc(SpecialKeys);
  432.     glutMainLoop();
  433.     return EXIT_SUCCESS;
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement