Advertisement
rakoczyn

openGL nurbs

Jun 12th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1.  #include <windows.h>
  2. #include <GL/glut.h>
  3. //#include <math.h>
  4. #include <cmath>
  5. #define M_PI 3.14159265358979323846
  6. float w=600,h=600;
  7. float alfa=0,beta=0;
  8. float x,y,z;
  9. float r=30;
  10. float poz1=0,poz2=0;
  11. const GLdouble left = -10.0;
  12. const GLdouble right = 10.0;
  13. const GLdouble bottom = -10.0;
  14. const GLdouble top = 10.0;
  15. const GLdouble nnear = -10.0;
  16. const GLdouble ffar = 10.0;
  17. int right_button_state = GLUT_UP ;
  18. int middle_button_state = GLUT_UP ;
  19. int left_button_state = GLUT_UP ;
  20. int l_button_x, l_button_y, m_button_x, m_button_y, mr_button;
  21. float a = sqrtf(2)/2;
  22. // n - liczba pkt kontrolnych
  23. // m - rzฤ…d powierzchni
  24. // k - stopien powierzchni
  25. // t - rozmiar wektora wฤ™zล‚รณw
  26. // t = k + n + 1
  27. // m = k + 1
  28.  
  29. float PunktyKontrolne [4][9][4] = {             //v0
  30.                                     {   2,      0,      0,      1//u0
  31.                                         2*a,    2*a,    0*a,    a,  //u1
  32.                                         0,      2,      0,      1//u2
  33.                                         -2*a,   2*a,    0*a,    a,  //u3
  34.                                         -2,     0,      0,      1//u4
  35.                                         -2*a,  -2*a,    0*a,    a,  //u5
  36.                                         0,     -2,      0,      1//u6
  37.                                         2*a,    -2*a,   0,      a,  //u7
  38.                                         2,      0,      0,      1   //u8
  39.                                     },     
  40.  
  41.                                                 //v1
  42.                                     {   4,      0,      2,      1//u0
  43.                                         4*a,    4*a,    2*a,    a,
  44.                                         0,      4,      2,      1,
  45.                                         -4*a,   4*a,    2*a,    a,
  46.                                         -4,     0,      2,      1,
  47.                                         -4*a,   -4*a,   2*a,    a,
  48.                                         0,      -4,     2,      1,
  49.                                         4*a,    -4*a,   2*a,    a,
  50.                                         4,      0,      2,      1   //u8
  51.                                     },     
  52.                                                
  53.                                                 //v2
  54.                                     {   2,      0,      4,      1//u0   
  55.                                         2*a,    2*a,    4*a,    a,
  56.                                         0,      2,      4,      1,
  57.                                         -2*a,   2*a,    4*a,    a,
  58.                                         -2,     0,      4,      1,
  59.                                         -2*a,   -2*a,   4*a,    a,
  60.                                         0,      -2,     4,      1,
  61.                                         2*a,    -2*a,   4*a,    a,
  62.                                         2,      0,      4,      1   //u8
  63.                                     },     
  64.  
  65.                                                 //v3
  66.                                     {   2,      0,      10,     1//u0   
  67.                                         2*a,    1.5*a,  10*a,   a,
  68.                                         0,      1.5,    10,     1,
  69.                                         -2*a,   1.5*a,  10*a,   a,
  70.                                         -2,     0,      10,     1,
  71.                                         -2*a,   -1.5*a, 10*a,   a,
  72.                                         0,      -1.5,   10,     1,
  73.                                         2*a,    -1.5*a, 10*a,   a,
  74.                                         2,      0,      10,     1   //u8
  75.                                     }      
  76.  
  77.                                 };
  78.  
  79. float OstrzePktKtrl[1][4][3] = {
  80.                                     {
  81.                                         220
  82.                                         -2, 20
  83.                                         -2, -2, 0
  84.                                         2-2, 0
  85.                                     }/*
  86.                                     {
  87.                                         2,  2,  2,  1,
  88.                                         -2, 2,  2,  1,
  89.                                         -2, -2, 2,  1,
  90.                                         2,  -2, 2,  1
  91.                                     }*/
  92.                                 };
  93.  
  94. float knot_u[12] = {0,0,0, 0.1,0.1, 0.5,0.5, 0.9,0.9, 1,1,1};
  95. float knot_v[7] = {0,0,0, 0.6, 1,1,1};
  96.  
  97. void Keyboard( unsigned char key, int x, int y )
  98. {
  99.     if ( key == 27 )
  100.         exit( EXIT_SUCCESS );
  101.     switch(key)
  102.     {
  103.     case 'w':
  104.         alfa-=5;
  105.         break;
  106.     case 's':
  107.         alfa+=5;
  108.         break;
  109.     case 'a':
  110.         beta-=5;
  111.         break;
  112.     case 'd':
  113.         beta+=5;
  114.         break;
  115.     case 'z':
  116.         r+=0.1;
  117.         break;
  118.     case 'x':
  119.         r-=0.1;
  120.         break;
  121.     case 'e':
  122.         poz1+=0.1;
  123.         break;
  124.     case 'q':
  125.         poz1-=0.1;
  126.         break;
  127.     }
  128. }
  129. void SpecialKeys(int key, int x, int y)
  130. {
  131.     switch(key)
  132.     {
  133.     case GLUT_KEY_UP:
  134.         alfa+=1;
  135.         break;
  136.     }
  137. }
  138. void Init()
  139. {
  140.     glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
  141.     glMatrixMode( GL_PROJECTION ) ;
  142.     glLoadIdentity();
  143.     gluPerspective(60,w/h,0.1,100);
  144.     //glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );
  145.     glMatrixMode(GL_MODELVIEW);
  146.     glLoadIdentity();
  147.     glEnable(GL_DEPTH_TEST);
  148.    
  149.     glutKeyboardFunc( Keyboard );
  150. }
  151. void Camera()
  152. {
  153.     x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
  154.     y=r*sin(alfa*M_PI/180);
  155.     z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
  156. }
  157. void MouseButton( int button, int state, int x, int y)
  158. {
  159.     if (button == GLUT_LEFT_BUTTON)
  160.     {
  161.         left_button_state = state;
  162.         if (state == GLUT_DOWN)
  163.         {
  164.             l_button_x = x;
  165.             l_button_y = y;
  166.         }
  167.     }
  168.     if (button == GLUT_MIDDLE_BUTTON)
  169.     {
  170.         middle_button_state = state;
  171.         if (state == GLUT_DOWN)
  172.         {
  173.             mr_button = y;
  174.         }
  175.     }
  176.     if (button == GLUT_RIGHT_BUTTON)
  177.     {
  178.         right_button_state = state;
  179.         if (state == GLUT_DOWN)
  180.         {
  181.             m_button_x = x;
  182.             m_button_y = y;
  183.         }
  184.     }
  185. }
  186. void MouseMotion( int x, int y)
  187. {
  188.     if (left_button_state == GLUT_DOWN)
  189.     {
  190.         beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
  191.         l_button_x = x;
  192.         alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y );
  193.         l_button_y = y;
  194.         /*if (alfa<0)
  195.             alfa=0;
  196.         if (alfa>180)
  197.             alfa=180;*/
  198.         glutPostRedisplay();
  199.     }
  200.     if (middle_button_state == GLUT_DOWN)
  201.     {
  202.         r += 0.01 * (ffar - nnear) * (y - mr_button);
  203.         mr_button = y;
  204.         glutPostRedisplay();
  205.     }
  206.     if (right_button_state == GLUT_DOWN)
  207.     {
  208.         poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
  209.         m_button_x = x;
  210.         poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
  211.         m_button_y = y;
  212.         /*if (poz2<-0.55)
  213.             poz2=-0.55;*/
  214.         glutPostRedisplay();
  215.     }
  216. }
  217. void Render()
  218. {
  219. glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  220. Camera();
  221. gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0);
  222.  
  223. glColor3f(1,0,0);
  224.  
  225. float knots_u[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 };
  226. float knots_v[6] = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
  227.  
  228. GLUnurbsObj * nurb;
  229. nurb = gluNewNurbsRenderer();
  230.  
  231. gluNurbsProperty(nurb,GLU_SAMPLING_TOLERANCE,20);
  232. gluNurbsProperty(nurb,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
  233.  
  234. float ctlpoints[2][4][4] = {
  235.     {
  236.          -4.0, -4.0, 0.0, 1,
  237.          -2.0, 4.0, 0.0, 1,
  238.         2.0, -4.0, 0.0, 1,
  239.         4.0, 4.0, 0.0, 1
  240.     },
  241.     {
  242.          -4.0, -4.0, 10.0, 1,
  243.          -2.0, 4.0, 10.0, 1,
  244.         2.0, -4.0, 10.0, 1,
  245.         4.0, 4.0, 10.0, 1
  246.     }
  247. };
  248.  
  249.  
  250. gluBeginSurface(nurb);
  251. gluNurbsSurface(
  252.     nurb,
  253.     8, knots_u,
  254.     6, knots_v,
  255.     4, 16,
  256.     &ctlpoints[0][0][0],
  257.     3, 3,
  258.     GL_MAP2_VERTEX_3
  259.     );
  260. gluEndSurface(nurb);
  261.  
  262. glPointSize(3);
  263. glColor3f(0,0,0);
  264. glBegin(GL_POINTS);
  265.     for(int i = 0; i < 2; i++)
  266.         for(int j=0; j < 4; j++)
  267.             glVertex3f(ctlpoints[i][j][0],
  268.                        ctlpoints[i][j][1],
  269.                        ctlpoints[i][j][2]);
  270.  
  271. glEnd();
  272.  
  273.  
  274.  
  275. glLoadIdentity();
  276. glutSwapBuffers();
  277. }
  278. void Idle()
  279. {
  280.     Render();
  281. }
  282. int main( int argc, char **argv )
  283. {
  284.     glutInitDisplayMode( GLUT_DOUBLE);
  285.     glutInitWindowSize(w,h);
  286.     glutInitWindowPosition(50,50);
  287.     glutCreateWindow( "OpenGL" );
  288.     Init();
  289.     glutIdleFunc( Idle);
  290.     glutDisplayFunc( Render );
  291.     glutMouseFunc(MouseButton);
  292.     glutMotionFunc(MouseMotion);
  293.     glutSpecialFunc(SpecialKeys);
  294.     glutMainLoop();
  295.     return EXIT_SUCCESS;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement