Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <GL/glut.h>
- //#include <math.h>
- #include <cmath>
- #define M_PI 3.14159265358979323846
- float w=600,h=600;
- float alfa=0,beta=0;
- float x,y,z ;
- float r=30;
- float poz1=0,poz2=0;
- const GLdouble left = -10.0;
- const GLdouble right = 10.0;
- const GLdouble bottom = -10.0;
- const GLdouble top = 10.0;
- const GLdouble nnear = -10.0;
- const GLdouble ffar = 10.0;
- int right_button_state = GLUT_UP ;
- int middle_button_state = GLUT_UP ;
- int left_button_state = GLUT_UP ;
- int l_button_x, l_button_y, m_button_x, m_button_y, mr_button;
- float a = sqrtf(2)/2;
- // n - liczba pkt kontrolnych
- // m - rząd powierzchni
- // k - stopien powierzchni
- // t - rozmiar wektora węzłów
- // t = k + n + 1
- // m = k + 1
- float PunktyKontrolne[9][11][4]={
- {
- 0, 11, 0, 1,
- -2, 11, 0, 1,
- -2.3, 9, 0, 1,
- -2.7, 1, 0, 1,
- -2, -5, 0, 1,
- 0, -5, 0, 1,
- 2, -5, 0, 1,
- 2.7, 1, 0, 1,
- 2.3, 9, 0, 1,
- 2, 11, 0, 1,
- 0, 11, 0, 1
- },
- {
- 0, 11, 2, 1,
- -2, 11, 2, 1,
- -2.3, 9, 2, 1,
- -2.7, 1, 2, 1,
- -2, -5, 2, 1,
- 0, -5, 2, 1,
- 2, -5, 2, 1,
- 2.7, 1, 2, 1,
- 2.3, 9, 2, 1,
- 2, 11, 2, 1,
- 0, 11, 2, 1
- },
- {
- 0, 10, 2, 1,
- -1, 10, 2, 1,
- -1.2, 9, 2, 1,
- -1.4, 3, 2, 1,
- -1, 1, 2, 1,
- 0, 1, 2, 1,
- 1, 1, 2, 1,
- 1.4, 3, 2, 1,
- 1.2, 9, 2, 1,
- 1, 10, 2, 1,
- 0, 10, 2, 1
- },
- {
- 0, 9, 2, 1,
- -0.8, 9, 2, 1,
- -1, 8, 2, 1,
- -1.2, 3, 2, 1,
- -0.8, 2, 2, 1,
- 0, 2, 2, 1,
- 0.8, 2, 2, 1,
- 1.2, 3, 2, 1,
- 1.0, 8, 2, 1,
- 0.8, 9, 2, 1,
- 0, 9, 2, 1
- },
- {
- 0, 9, 0, 1,
- -0.8, 9, 0, 1,
- -1, 8, 0, 1,
- -1.2, 3, 0, 1,
- -0.8, 2, 0, 1,
- 0, 2, 0, 1,
- 0.8, 2, 0, 1,
- 1.2, 3, 0, 1,
- 1.0, 8, 0, 1,
- 0.8, 9, 0, 1,
- 0, 9, 0, 1
- },
- /////////
- {
- 0, 9, -2, 1,
- -0.8, 9, -2, 1,
- -1, 8, -2, 1,
- -1.2, 3, -2, 1,
- -0.8, 2, -2, 1,
- 0, 2, -2, 1,
- 0.8, 2, -2, 1,
- 1.2, 3, -2, 1,
- 1.0, 8, -2, 1,
- 0.8, 9, -2, 1,
- 0, 9, -2, 1
- },
- {
- 0, 10, -2, 1,
- -1, 10, -2, 1,
- -1.2, 9, -2, 1,
- -1.4, 3, -2, 1,
- -1, 1, -2, 1,
- 0, 1, -2, 1,
- 1, 1, -2, 1,
- 1.4, 3, -2, 1,
- 1.2, 9, -2, 1,
- 1, 10, -2, 1,
- 0, 10, -2, 1
- },
- {
- 0, 11, -2, 1,
- -2, 11, -2, 1,
- -2.3, 9, -2, 1,
- -2.7, 1, -2, 1,
- -2, -5, -2, 1,
- 0, -5, -2, 1,
- 2, -5, -2, 1,
- 2.7, 1, -2, 1,
- 2.3, 9, -2, 1,
- 2, 11, -2, 1,
- 0, 11, -2, 1
- },
- {
- 0, 11, 0, 1,
- -2, 11, 0, 1,
- -2.3, 9, 0, 1,
- -2.7, 1, 0, 1,
- -2, -5, 0, 1,
- 0, -5, 0, 1,
- 2, -5, 0, 1,
- 2.7, 1, 0, 1,
- 2.3, 9, 0, 1,
- 2, 11, 0, 1,
- 0, 11, 0, 1
- },
- };
- 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};
- float knot_v[9]={0,0,0,0, 0.5, 1,1,1,1};
- //float PktKtrlOstrze[][][3]
- void Keyboard( unsigned char key, int x, int y )
- {
- if ( key == 27 )
- exit( EXIT_SUCCESS );
- switch(key)
- {
- case 'w':
- alfa-=5;
- break;
- case 's':
- alfa+=5;
- break;
- case 'a':
- beta-=5;
- break;
- case 'd':
- beta+=5;
- break;
- case 'z':
- r+=0.1;
- break;
- case 'x':
- r-=0.1;
- break;
- case 'e':
- poz1+=0.1;
- break;
- case 'q':
- poz1-=0.1;
- break;
- }
- }
- void SpecialKeys(int key, int x, int y)
- {
- switch(key)
- {
- case GLUT_KEY_UP:
- alfa+=1;
- break;
- }
- }
- void Init()
- {
- glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
- glMatrixMode( GL_PROJECTION ) ;
- glLoadIdentity();
- gluPerspective(60,w/h,0.1,100);
- //glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 );
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glEnable(GL_DEPTH_TEST);
- glutKeyboardFunc( Keyboard );
- }
- void Camera()
- {
- x=-r*cos(alfa*M_PI/180)*sin(beta*M_PI/180);
- y=r*sin(alfa*M_PI/180);
- z=r*cos(alfa*M_PI/180)*cos(beta*M_PI/180);
- }
- void MouseButton( int button, int state, int x, int y)
- {
- if (button == GLUT_LEFT_BUTTON)
- {
- left_button_state = state;
- if (state == GLUT_DOWN)
- {
- l_button_x = x;
- l_button_y = y;
- }
- }
- if (button == GLUT_MIDDLE_BUTTON)
- {
- middle_button_state = state;
- if (state == GLUT_DOWN)
- {
- mr_button = y;
- }
- }
- if (button == GLUT_RIGHT_BUTTON)
- {
- right_button_state = state;
- if (state == GLUT_DOWN)
- {
- m_button_x = x;
- m_button_y = y;
- }
- }
- }
- void MouseMotion( int x, int y)
- {
- if (left_button_state == GLUT_DOWN)
- {
- beta += 50 *(top - bottom) / glutGet( GLUT_WINDOW_WIDTH ) *( x - l_button_x );
- l_button_x = x;
- alfa += 50 *(right - left) / glutGet( GLUT_WINDOW_HEIGHT) *( y - l_button_y );
- l_button_y = y;
- /*if (alfa<0)
- alfa=0;
- if (alfa>180)
- alfa=180;*/
- glutPostRedisplay();
- }
- if (middle_button_state == GLUT_DOWN)
- {
- r += 0.01 * (ffar - nnear) * (y - mr_button);
- mr_button = y;
- glutPostRedisplay();
- }
- if (right_button_state == GLUT_DOWN)
- {
- poz1 += 2 *(right - left) / glutGet( GLUT_WINDOW_WIDTH ) *( x - m_button_x );
- m_button_x = x;
- poz2 += 2 *(top - bottom) / glutGet( GLUT_WINDOW_HEIGHT ) *( m_button_y - y );
- m_button_y = y;
- /*if (poz2<-0.55)
- poz2=-0.55;*/
- glutPostRedisplay();
- }
- }
- void Light()
- {
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_AUTO_NORMAL);
- glShadeModel(GL_SMOOTH);
- glEnable(GL_NORMALIZE);
- //glEnable(GL_CULL_FACE);
- float LightPosition[]={30,30,30,1};
- float Ambient[]={0.0,0.0,0.0,1.0};
- float Diffuse[]={1.0,1.0,1.0,1.0};
- float Diffuse_mat[]={0.0,0.0,0.0,1.0};
- float Specular[]={1.0,1.0,1.0,1.0};
- float Shine[]={20};
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
- glLightfv(GL_LIGHT0,GL_AMBIENT,Ambient);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,Diffuse);
- glLightfv(GL_LIGHT0,GL_POSITION,LightPosition);
- glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
- glMaterialfv(GL_FRONT,GL_SPECULAR,Specular);
- glMaterialfv(GL_FRONT,GL_SHININESS,Shine);
- // glMaterialfv(GL_BACK,GL_DIFFUSE,Diffuse_mat);
- glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
- // glColorMaterial(GL_BACK,GL_AMBIENT_AND_DIFFUSE);
- }
- void Render()
- {
- Light();
- glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- Camera();
- gluLookAt(x+poz1,y,z,poz1,poz2,0,0,cos(alfa*M_PI/180),0);
- GLUnurbsObj*cnurbs; //wskaźnik
- cnurbs = gluNewNurbsRenderer();
- gluNurbsProperty(cnurbs,GLU_SAMPLING_TOLERANCE,18); // gladkosc imn mniej tym lepiej( niewydajniej)
- //gluNurbsProperty(cnurbs,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
- glColor3f(1,0,1);
- gluBeginSurface(cnurbs); //powierzchnia nurbsa
- gluNurbsSurface(cnurbs, //wskaźnik
- 15, //t(u) rozmiar wektora w kier. u
- knot_u, // tablica wekt. u
- 9, // t(v) rozmiar wektora w kier. v
- knot_v, // tablica wekt. v
- 4, // stride u
- 88, // stride v
- &PunktyKontrolne[0][0][0], //tablica punktów kontrolnych
- 4, // rzad wielkosci w kier u (2+1)
- 4, // rzad wielkosci w kier v (2+1)
- GL_MAP2_VERTEX_4
- );
- gluEndSurface(cnurbs);
- glColor3f(0.3,0.3,0.5);
- glBegin(GL_QUAD_STRIP);
- glVertex3f(-0.7,-10,0.2);
- glVertex3f(-1.0,-4,0.2);
- glVertex3f(1.5,-10,0.2);
- glVertex3f(1.4,-4,0.2);
- glVertex3f(1.5,-10,-0.1);
- glVertex3f(1.4,-4,-0.1);
- glVertex3f(-0.7,-10,-0.1);
- glVertex3f(-1.0,-4,-0.1);
- glVertex3f(-0.7,-10,0.2);
- glVertex3f(-1.0,-4,0.2);
- glEnd();
- glBegin(GL_TRIANGLE_STRIP);
- glVertex3f(0.5,-20,0.2);
- glVertex3f(-0.7,-10,0.2);
- glVertex3f(1.0,-20,0.2);
- glVertex3f(1.0,-10,0.2);
- glVertex3f(1.5,-20,-0.05);
- glVertex3f(1.5,-10,-0.05);
- glVertex3f(1.5,-20,-0.1);
- glVertex3f(1.5,-10,-0.1);
- glVertex3f(0.5,-20,-0.1);
- glVertex3f(-0.7,-10,-0.1);
- glVertex3f(0.5,-20,0.2);
- glVertex3f(-0.7,-10,0.2);
- glEnd();
- glBegin(GL_TRIANGLE_STRIP);
- glVertex3f(0.5,-20,-0.1);
- glVertex3f(0.5,-20,0.2);
- glVertex3f(1.0,-20,-0.1);
- glVertex3f(1.0,-20,0.2);
- glVertex3f(1.5,-20,-0.05);
- glVertex3f(1.5,-20,-0.1);
- glVertex3f(1.0,-20,-0.1);
- glEnd();
- glLoadIdentity();
- glutSwapBuffers();
- }
- void Idle()
- {
- Render();
- }
- int main( int argc, char **argv )
- {
- glutInitDisplayMode( GLUT_DOUBLE);
- glutInitWindowSize(w,h);
- glutInitWindowPosition(50,50);
- glutCreateWindow( "OpenGL" );
- Init();
- glutIdleFunc( Idle);
- glutDisplayFunc( Render );
- glutMouseFunc(MouseButton);
- glutMotionFunc(MouseMotion);
- glutSpecialFunc(SpecialKeys);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement