Advertisement
rakoczyn

Untitled

Jun 11th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.84 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.  
  23. // n - liczba pkt kontrolnych
  24. // m - rząd powierzchni
  25. // k - stopien powierzchni
  26. // t - rozmiar wektora węzłów
  27. // t = k + n + 1
  28. // m = k + 1
  29.  
  30. float PunktyKontrolne[9][11][4]={
  31.                                                                
  32.                                                                 {
  33.                                                                         0,   11,   0,   1,
  34.                                                                    -2,   11,   0,   1,
  35.                                                                    -2.3,  9,   0,   1,
  36.                                                                    -2.7,  1,   0,   1,
  37.                                                                    -2,    -5,   0,  1,
  38.                                                                     0,    -5,   0,  1,
  39.                                                                         2,    -5,   0,  1,
  40.                                                                         2.7,  1,   0,   1,
  41.                                                                         2.3,  9,   0,   1,
  42.                                                                         2,    11,  0,   1,
  43.                                                                         0,    11,  0,   1
  44.                                                                 },
  45.  
  46.                                                                 {
  47.                                                                         0,   11,   2,   1,
  48.                                                                    -2,   11,   2,   1,
  49.                                                                    -2.3,  9,   2,   1,
  50.                                                                    -2.7,  1,   2,   1,
  51.                                                                    -2,    -5,   2,  1,
  52.                                                                     0,    -5,   2,  1,
  53.                                                                         2,    -5,   2,  1,
  54.                                                                         2.7,  1,   2,   1,
  55.                                                                         2.3,  9,   2,   1,
  56.                                                                         2,    11,  2,   1,
  57.                                                                         0,    11,  2,   1
  58.                                                                 },
  59.  
  60.                                                                 {
  61.                                                                         0,   10,   2,   1,
  62.                                                                    -1,   10,   2,   1,
  63.                                                                    -1.2,  9,   2,   1,
  64.                                                                    -1.4,  3,   2,   1,
  65.                                                                    -1,    1,   2,   1,
  66.                                                                     0,    1,   2,   1,
  67.                                                                         1,    1,   2,   1,
  68.                                                                         1.4,  3,   2,   1,
  69.                                                                         1.2,  9,   2,   1,
  70.                                                                         1,    10,  2,   1,
  71.                                                                         0,    10,  2,   1
  72.                                                                 },
  73.  
  74.                                                                 {
  75.                                                                         0,   9,   2,   1,
  76.                                                                    -0.8,   9,   2,   1,
  77.                                                                    -1,  8,   2,   1,
  78.                                                                    -1.2,  3,   2,   1,
  79.                                                                    -0.8,    2,   2,   1,
  80.                                                                     0,    2,   2,   1,
  81.                                                                         0.8,    2,   2,   1,
  82.                                                                         1.2,  3,   2,   1,
  83.                                                                         1.0,  8,   2,   1,
  84.                                                                         0.8,    9,  2,   1,
  85.                                                                         0,    9,  2,   1
  86.                                                                 },
  87.  
  88.                                                                 {
  89.                                                                         0,   9,   0,   1,
  90.                                                                    -0.8,   9,   0,   1,
  91.                                                                    -1,  8,   0,   1,
  92.                                                                    -1.2,  3,   0,   1,
  93.                                                                    -0.8,    2,   0,   1,
  94.                                                                     0,    2,   0,   1,
  95.                                                                         0.8,    2,   0,   1,
  96.                                                                         1.2,  3,   0,   1,
  97.                                                                         1.0,  8,   0,   1,
  98.                                                                         0.8,    9,  0,   1,
  99.                                                                         0,    9,  0,   1
  100.                                                                 },
  101.                                                                 /////////
  102.                                                              {
  103.                                                                         0,   9,   -2,   1,
  104.                                                                    -0.8,   9,   -2,   1,
  105.                                                                    -1,  8,   -2,   1,
  106.                                                                    -1.2,  3,   -2,   1,
  107.                                                                    -0.8,    2,   -2,   1,
  108.                                                                     0,    2,   -2,   1,
  109.                                                                         0.8,    2,  -2,   1,
  110.                                                                         1.2,  3,   -2,   1,
  111.                                                                         1.0,  8,   -2,   1,
  112.                                                                         0.8,    9,  -2,   1,
  113.                                                                         0,    9,  -2,   1
  114.                                                                 },
  115.                                                                 {
  116.                                                                         0,   10,   -2,   1,
  117.                                                                    -1,   10,   -2,   1,
  118.                                                                    -1.2,  9,   -2,   1,
  119.                                                                    -1.4,  3,   -2,   1,
  120.                                                                    -1,    1,   -2,   1,
  121.                                                                     0,    1,   -2,   1,
  122.                                                                         1,    1,   -2,   1,
  123.                                                                         1.4,  3,   -2,   1,
  124.                                                                         1.2,  9,   -2,   1,
  125.                                                                         1,    10,  -2,   1,
  126.                                                                         0,    10,  -2,   1
  127.                                                                 },
  128.  
  129.                                                                 {
  130.                                                                         0,   11,   -2,   1,
  131.                                                                    -2,   11,   -2,   1,
  132.                                                                    -2.3,  9,   -2,   1,
  133.                                                                    -2.7,  1,   -2,   1,
  134.                                                                    -2,    -5,   -2,  1,
  135.                                                                     0,    -5,   -2,  1,
  136.                                                                         2,    -5,   -2,  1,
  137.                                                                         2.7,  1,   -2,   1,
  138.                                                                         2.3,  9,   -2,   1,
  139.                                                                         2,    11,  -2,   1,
  140.                                                                         0,    11,  -2,   1
  141.                                                                 },
  142.  
  143.                                                                 {
  144.                                                                         0,   11,   0,   1,
  145.                                                                    -2,   11,   0,   1,
  146.                                                                    -2.3,  9,   0,   1,
  147.                                                                    -2.7,  1,   0,   1,
  148.                                                                    -2,    -5,   0,  1,
  149.                                                                     0,    -5,   0,  1,
  150.                                                                         2,    -5,   0,  1,
  151.                                                                         2.7,  1,   0,   1,
  152.                                                                         2.3,  9,   0,   1,
  153.                                                                         2,    11,  0,   1,
  154.                                                                         0,    11,  0,   1
  155.                                                                 },
  156.  
  157.  
  158.                                                            
  159.  
  160.                                                                
  161.                                                            
  162.  
  163.                                                                 };
  164.  
  165.  
  166. float knot_u[15]={0,0,0,0, 0.1,0.2,0.3,0.5,0.7,0.8,0.9, 1,1,1,1};
  167.  
  168. float knot_v[9]={0,0,0,0, 0.5, 1,1,1,1};
  169.  
  170. //float PktKtrlOstrze[][][3]
  171.  
  172. void Keyboard( unsigned char key, int x, int y )
  173. {
  174.     if ( key == 27 )
  175.         exit( EXIT_SUCCESS );
  176.     switch(key)
  177.     {
  178.     case 'w':
  179.         alfa-=5;
  180.         break;
  181.     case 's':
  182.         alfa+=5;
  183.         break;
  184.     case 'a':
  185.         beta-=5;
  186.         break;
  187.     case 'd':
  188.         beta+=5;
  189.         break;
  190.     case 'z':
  191.         r+=0.1;
  192.         break;
  193.     case 'x':
  194.         r-=0.1;
  195.         break;
  196.     case 'e':
  197.         poz1+=0.1;
  198.         break;
  199.     case 'q':
  200.         poz1-=0.1;
  201.         break;
  202.     }
  203. }
  204. void SpecialKeys(int key, int x, int y)
  205. {
  206.     switch(key)
  207.     {
  208.     case GLUT_KEY_UP:
  209.         alfa+=1;
  210.         break;
  211.     }
  212. }
  213. void Init()
  214. {
  215.     glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
  216.     glMatrixMode( GL_PROJECTION ) ;
  217.     glLoadIdentity();
  218.     gluPerspective(60,w/h,0.1,100);
  219.     //glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );
  220.     glMatrixMode(GL_MODELVIEW);
  221.     glLoadIdentity();
  222.     glEnable(GL_DEPTH_TEST);
  223.    
  224.     glutKeyboardFunc( Keyboard );
  225. }
  226. void Camera()
  227. {
  228.     x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
  229.     y=r*sin(alfa*M_PI/180);
  230.     z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
  231. }
  232. void MouseButton( int button, int state, int x, int y)
  233. {
  234.     if (button == GLUT_LEFT_BUTTON)
  235.     {
  236.         left_button_state = state;
  237.         if (state == GLUT_DOWN)
  238.         {
  239.             l_button_x = x;
  240.             l_button_y = y;
  241.         }
  242.     }
  243.     if (button == GLUT_MIDDLE_BUTTON)
  244.     {
  245.         middle_button_state = state;
  246.         if (state == GLUT_DOWN)
  247.         {
  248.             mr_button = y;
  249.         }
  250.     }
  251.     if (button == GLUT_RIGHT_BUTTON)
  252.     {
  253.         right_button_state = state;
  254.         if (state == GLUT_DOWN)
  255.         {
  256.             m_button_x = x;
  257.             m_button_y = y;
  258.         }
  259.     }
  260. }
  261. void MouseMotion( int x, int y)
  262. {
  263.     if (left_button_state == GLUT_DOWN)
  264.     {
  265.         beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
  266.         l_button_x = x;
  267.         alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y );
  268.         l_button_y = y;
  269.         /*if (alfa<0)
  270.             alfa=0;
  271.         if (alfa>180)
  272.             alfa=180;*/
  273.         glutPostRedisplay();
  274.     }
  275.     if (middle_button_state == GLUT_DOWN)
  276.     {
  277.         r += 0.01 * (ffar - nnear) * (y - mr_button);
  278.         mr_button = y;
  279.         glutPostRedisplay();
  280.     }
  281.     if (right_button_state == GLUT_DOWN)
  282.     {
  283.         poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
  284.         m_button_x = x;
  285.         poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
  286.         m_button_y = y;
  287.         /*if (poz2<-0.55)
  288.             poz2=-0.55;*/
  289.         glutPostRedisplay();
  290.     }
  291. }
  292.  
  293.  
  294. void Light()
  295. {
  296.                 glEnable(GL_LIGHTING);
  297.         glEnable(GL_LIGHT0);
  298.         glEnable(GL_COLOR_MATERIAL);
  299.         glEnable(GL_AUTO_NORMAL);
  300.                 glShadeModel(GL_SMOOTH);
  301.                 glEnable(GL_NORMALIZE);
  302.                 //glEnable(GL_CULL_FACE);
  303.  
  304.  
  305.         float LightPosition[]={30,30,30,1};
  306.         float Ambient[]={0.0,0.0,0.0,1.0};
  307.         float Diffuse[]={1.0,1.0,1.0,1.0};
  308.  
  309.         float Diffuse_mat[]={0.0,0.0,0.0,1.0};
  310.         float Specular[]={1.0,1.0,1.0,1.0};
  311.         float Shine[]={20};
  312.        
  313.  
  314.         glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  315.  
  316.  
  317.         glLightfv(GL_LIGHT0,GL_AMBIENT,Ambient);
  318.         glLightfv(GL_LIGHT0,GL_DIFFUSE,Diffuse);
  319.         glLightfv(GL_LIGHT0,GL_POSITION,LightPosition);
  320.         glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
  321.  
  322.         glMaterialfv(GL_FRONT,GL_SPECULAR,Specular);
  323.         glMaterialfv(GL_FRONT,GL_SHININESS,Shine);
  324.        // glMaterialfv(GL_BACK,GL_DIFFUSE,Diffuse_mat);
  325.  
  326.        
  327.  
  328.  
  329.         glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  330.        // glColorMaterial(GL_BACK,GL_AMBIENT_AND_DIFFUSE);
  331.  
  332.        
  333. }
  334.  
  335.  
  336.  
  337. void Render()
  338. {
  339.  
  340.     Light();
  341.     glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  342.     Camera();
  343.     gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0);
  344.  
  345.     GLUnurbsObj*cnurbs; //wskaźnik
  346.  
  347.     cnurbs = gluNewNurbsRenderer();
  348.  
  349.     gluNurbsProperty(cnurbs,GLU_SAMPLING_TOLERANCE,18); // gladkosc imn mniej tym lepiej( niewydajniej)
  350.     //gluNurbsProperty(cnurbs,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
  351.  
  352.     glColor3f(1,0,1);
  353.  
  354.     gluBeginSurface(cnurbs); //powierzchnia nurbsa
  355.  
  356.         gluNurbsSurface(cnurbs, //wskaźnik
  357.                         15,     //t(u) rozmiar wektora w kier. u
  358.                         knot_u, // tablica wekt. u
  359.                         9,      // t(v) rozmiar wektora w kier. v
  360.                         knot_v, // tablica wekt. v
  361.                         4,      // stride u
  362.                         88,     // stride v
  363.                         &PunktyKontrolne[0][0][0], //tablica punktów kontrolnych
  364.                         4, // rzad wielkosci w kier u (2+1)
  365.                         4, // rzad wielkosci w kier v (2+1)
  366.                         GL_MAP2_VERTEX_4
  367.                         );
  368.                      
  369.     gluEndSurface(cnurbs);
  370.     glColor3f(0.3,0.3,0.5);
  371.     glBegin(GL_QUAD_STRIP);
  372.  
  373.         glVertex3f(-0.7,-10,0.2);
  374.         glVertex3f(-1.0,-4,0.2);
  375.         glVertex3f(1.5,-10,0.2);
  376.         glVertex3f(1.4,-4,0.2);
  377.  
  378.         glVertex3f(1.5,-10,-0.1);
  379.         glVertex3f(1.4,-4,-0.1);
  380.         glVertex3f(-0.7,-10,-0.1);
  381.         glVertex3f(-1.0,-4,-0.1);
  382.  
  383.         glVertex3f(-0.7,-10,0.2);
  384.         glVertex3f(-1.0,-4,0.2);
  385.        
  386.  
  387.  
  388.     glEnd();
  389.  
  390.     glBegin(GL_TRIANGLE_STRIP);
  391.  
  392.         glVertex3f(0.5,-20,0.2);
  393.         glVertex3f(-0.7,-10,0.2);
  394.         glVertex3f(1.0,-20,0.2);
  395.         glVertex3f(1.0,-10,0.2);
  396.        
  397.         glVertex3f(1.5,-20,-0.05);
  398.         glVertex3f(1.5,-10,-0.05);
  399.  
  400.         glVertex3f(1.5,-20,-0.1);
  401.         glVertex3f(1.5,-10,-0.1);
  402.  
  403.         glVertex3f(0.5,-20,-0.1);
  404.         glVertex3f(-0.7,-10,-0.1);
  405.  
  406.         glVertex3f(0.5,-20,0.2);
  407.         glVertex3f(-0.7,-10,0.2);
  408.  
  409.  
  410.     glEnd();
  411.  
  412.     glBegin(GL_TRIANGLE_STRIP);
  413.  
  414.         glVertex3f(0.5,-20,-0.1);
  415.         glVertex3f(0.5,-20,0.2);
  416.         glVertex3f(1.0,-20,-0.1);
  417.         glVertex3f(1.0,-20,0.2);
  418.         glVertex3f(1.5,-20,-0.05);
  419.         glVertex3f(1.5,-20,-0.1);
  420.         glVertex3f(1.0,-20,-0.1);
  421.        
  422.  
  423.     glEnd();
  424.    
  425.    
  426.    
  427.  
  428.     glLoadIdentity();
  429.     glutSwapBuffers();
  430. }
  431. void Idle()
  432. {
  433.     Render();
  434. }
  435. int main( int argc, char **argv )
  436. {
  437.     glutInitDisplayMode( GLUT_DOUBLE);
  438.     glutInitWindowSize(w,h);
  439.     glutInitWindowPosition(50,50);
  440.     glutCreateWindow( "OpenGL" );
  441.     Init();
  442.     glutIdleFunc( Idle);
  443.     glutDisplayFunc( Render );
  444.     glutMouseFunc(MouseButton);
  445.     glutMotionFunc(MouseMotion);
  446.     glutSpecialFunc(SpecialKeys);
  447.     glutMainLoop();
  448.     return EXIT_SUCCESS;
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement