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 [4][9][4] = { //v0
- { 2, 0, 0, 1, //u0
- 2*a, 2*a, 0*a, a, //u1
- 0, 2, 0, 1, //u2
- -2*a, 2*a, 0*a, a, //u3
- -2, 0, 0, 1, //u4
- -2*a, -2*a, 0*a, a, //u5
- 0, -2, 0, 1, //u6
- 2*a, -2*a, 0, a, //u7
- 2, 0, 0, 1 //u8
- },
- //v1
- { 4, 0, 2, 1, //u0
- 4*a, 4*a, 2*a, a,
- 0, 4, 2, 1,
- -4*a, 4*a, 2*a, a,
- -4, 0, 2, 1,
- -4*a, -4*a, 2*a, a,
- 0, -4, 2, 1,
- 4*a, -4*a, 2*a, a,
- 4, 0, 2, 1 //u8
- },
- //v2
- { 2, 0, 4, 1, //u0
- 2*a, 2*a, 4*a, a,
- 0, 2, 4, 1,
- -2*a, 2*a, 4*a, a,
- -2, 0, 4, 1,
- -2*a, -2*a, 4*a, a,
- 0, -2, 4, 1,
- 2*a, -2*a, 4*a, a,
- 2, 0, 4, 1 //u8
- },
- //v3
- { 2, 0, 10, 1, //u0
- 2*a, 1.5*a, 10*a, a,
- 0, 1.5, 10, 1,
- -2*a, 1.5*a, 10*a, a,
- -2, 0, 10, 1,
- -2*a, -1.5*a, 10*a, a,
- 0, -1.5, 10, 1,
- 2*a, -1.5*a, 10*a, a,
- 2, 0, 10, 1 //u8
- }
- };
- float OstrzePktKtrl[1][4][3] = {
- {
- 2, 2, 0,
- -2, 2, 0,
- -2, -2, 0,
- 2, -2, 0,
- }/*
- {
- 2, 2, 2, 1,
- -2, 2, 2, 1,
- -2, -2, 2, 1,
- 2, -2, 2, 1
- }*/
- };
- float knot_u[12] = {0,0,0, 0.1,0.1, 0.5,0.5, 0.9,0.9, 1,1,1};
- float knot_v[7] = {0,0,0, 0.6, 1,1,1};
- 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 Render()
- {
- 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);
- glColor3f(1,0,0);
- float knots_u[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 };
- float knots_v[6] = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
- GLUnurbsObj * nurb;
- nurb = gluNewNurbsRenderer();
- gluNurbsProperty(nurb,GLU_SAMPLING_TOLERANCE,20);
- gluNurbsProperty(nurb,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
- float ctlpoints[2][4][4] = {
- {
- -4.0, -4.0, 0.0, 1,
- -2.0, 4.0, 0.0, 1,
- 2.0, -4.0, 0.0, 1,
- 4.0, 4.0, 0.0, 1
- },
- {
- -4.0, -4.0, 10.0, 1,
- -2.0, 4.0, 10.0, 1,
- 2.0, -4.0, 10.0, 1,
- 4.0, 4.0, 10.0, 1
- }
- };
- gluBeginSurface(nurb);
- gluNurbsSurface(
- nurb,
- 8, knots_u,
- 6, knots_v,
- 4, 16,
- &ctlpoints[0][0][0],
- 3, 3,
- GL_MAP2_VERTEX_3
- );
- gluEndSurface(nurb);
- glPointSize(3);
- glColor3f(0,0,0);
- glBegin(GL_POINTS);
- for(int i = 0; i < 2; i++)
- for(int j=0; j < 4; j++)
- glVertex3f(ctlpoints[i][j][0],
- ctlpoints[i][j][1],
- ctlpoints[i][j][2]);
- 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