Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GLUT Shapes Demo
- *
- * Written by Nigel Stewart November 2003
- *
- * This program is test harness for the sphere, cone
- * and torus shapes in GLUT.
- *
- * Spinning wireframe and smooth shaded shapes are
- * displayed until the ESC or q key is pressed. The
- * number of geometry stacks and slices can be adjusted
- * using the + and - keys.
- */
- #include <GL/gl.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <vector>
- #include <iostream>
- using namespace std;
- const double PI = 3.14159265389;
- /* GLUT callback Handlers */
- int anglex= 0, angley = 0, anglez = 0; //rotation angles
- int window;
- int wired=0;
- int shcpt=1;
- int animat = 0;
- const int L=20;
- const int dgre=3;
- int ncpt=L+1;
- int clikd=0;
- const int nt = 40; //number of slices along x-direction
- const int ntheta = 20;
- GLfloat ctrlpoints[33][3] =
- {
- { 0.0, 0.0, 0.0}, { -0.3, 0.5, 0.0},
- { 0.1, 1.7, 0.0},{ 0.5, 1.5, 0.0},
- {2.5, 1.2, 0.0},
- { 3.0, 0.0, 0.0}, { 2.7, 0.5, 0.0},
- { 3.1, 1.7, 0.0},{ 3.5, 1.5, 0.0},
- {5.5, 1.2, 0.0},{5.5, 2.2, 0.0},{5.5, 3.2, 0.0},{5.5, 3.2, 0.0},{6.5, 0.2, 0.0},
- {4.5, 0.2, 0.0},{7.5, 1.2, 0.0},{ 3.0, 0.0, 0.0}, { 2.7, 0.5, 0.0},
- { 10.1, 1.7, 0.0},{ 10.5, 1.5, 0.0},
- {12.5, 1.2, 0.0},{12.5, 2.2, 0.0},{12.5, 3.2, 0.0},{12.5, 3.2, 0.0},{13.5, 0.2, 0.0},
- {11.5, 0.2, 0.0},{14.5, 1.2, 0.0}
- };
- /*
- , {1.4, 1.4, 0.0},
- {1.8, 0.4, 0.0},{2.2, 0.4, 0.0},
- {2.6, 1.5, 0.0}, {3.0, 1.4, 0.0},
- {3.4, 1.4, 0.0},{3.8, 1.4, 0.0},
- {4.2, 1.0, 0.0},{4.6, 1.0, 0.0},
- {5.0, 1.0, 0.0},{5.4, 1.0, 0.0},
- {5.8, 0.5, 0.0},{6.2, 0.5, 0.0},
- {6.6, 0.5, 0.0},{7.2, 0.2, 0.0},
- {6.8, 0.52, 0.0}
- */
- double ex=0, ey=0, ez=15, lx=0,ly=0,lz=0, hx=0,hy=1,hz=0;
- float wcsClkDn[3],wcsClkUp[3];
- ///////////////////////////////
- class point1
- {
- public:
- point1()
- {
- x=0;
- y=0;
- }
- int x;
- int y;
- } clkpt[2];
- int flag=0;
- GLint viewport[4]; //var to hold the viewport info
- GLdouble modelview[16]; //var to hold the modelview info
- GLdouble projection[16]; //var to hold the projection matrix info
- //////////////////////////
- void scsToWcs(float sx,float sy, float wcsv[3] );
- void processMouse(int button, int state, int x, int y);
- void matColor(float kdr, float kdg, float kdb, float shiny, int frnt_Back=0, float ambFactor=1.0, float specFactor=1.0);
- ///////////////////////////
- void scsToWcs(float sx,float sy, float wcsv[3] )
- {
- GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
- GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates
- //glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
- glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
- glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info
- winX = sx;
- winY = (float)viewport[3] - (float)sy;
- winZ = 0;
- //get the world coordinates from the screen coordinates
- gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
- wcsv[0]=worldX;
- wcsv[1]=worldY;
- wcsv[2]=worldZ;
- }
- void processMouse(int button, int state, int x, int y)
- {
- if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
- {
- if(flag!=1)
- {
- flag=1;
- clkpt[0].x=x;
- clkpt[0].y=y;
- }
- scsToWcs(clkpt[0].x,clkpt[0].y,wcsClkDn);
- cout<<"\nD: "<<x<<" "<<y<<" wcs: "<<wcsClkDn[0]<<" "<<wcsClkDn[1];
- }
- else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
- {
- if (flag==1)
- {
- clkpt[1].x=x;
- clkpt[1].y=y;
- flag=0;
- }
- float wcs[3];
- scsToWcs(clkpt[1].x,clkpt[1].y,wcsClkUp);
- cout<<"\nU: "<<x<<" "<<y<<" wcs: "<<wcsClkUp[0]<<" "<<wcsClkUp[1];
- clikd=!clikd;
- }
- }
- //control points
- long long nCr(int n, int r)
- {
- if(r > n / 2) r = n - r; // because C(n, r) == C(n, n - r)
- long long ans = 1;
- int i;
- for(i = 1; i <= r; i++)
- {
- ans *= n - r + i;
- ans /= i;
- }
- return ans;
- }
- //polynomial interpretation for N points
- void BezierCurve ( double t, float xy[2])
- {
- double y=0;
- double x=0;
- t=t>1.0?1.0:t;
- for(int i=0; i<=L; i++)
- {
- int ncr=nCr(L,i);
- double oneMinusTpow=pow(1-t,double(L-i));
- double tPow=pow(t,double(i));
- double coef=oneMinusTpow*tPow*ncr;
- x+=coef*ctrlpoints[i][0];
- y+=coef*ctrlpoints[i][1];
- }
- xy[0] = float(x);
- xy[1] = float(y);
- //return y;
- }
- ///////////////////////
- void setNormal(GLfloat x1, GLfloat y1,GLfloat z1, GLfloat x2, GLfloat y2,GLfloat z2, GLfloat x3, GLfloat y3,GLfloat z3)
- {
- GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
- Ux = x2-x1;
- Uy = y2-y1;
- Uz = z2-z1;
- Vx = x3-x1;
- Vy = y3-y1;
- Vz = z3-z1;
- Nx = Uy*Vz - Uz*Vy;
- Ny = Uz*Vx - Ux*Vz;
- Nz = Ux*Vy - Uy*Vx;
- glNormal3f(-Nx,-Ny,-Nz);
- }
- void bottleBezier()
- {
- int i, j;
- float x, y, z, r; //current coordinates
- float x1, y1, z1, r1; //next coordinates
- float theta;
- const float startx = 0, endx = ctrlpoints[L][0];
- //number of angular slices
- const float dx = (endx - startx) / nt; //x step size
- const float dtheta = 2*PI / ntheta; //angular step size
- float t=0;
- float dt=1.0/nt;
- float xy[2];
- BezierCurve( t, xy);
- x = xy[0];
- r = xy[1];
- //rotate about z-axis
- float p1x,p1y,p1z,p2x,p2y,p2z;
- for ( i = 0; i < nt; ++i ) //step through x
- {
- theta = 0;
- t+=dt;
- BezierCurve( t, xy);
- x1 = xy[0];
- r1 = xy[1];
- //draw the surface composed of quadrilaterals by sweeping theta
- glBegin( GL_QUAD_STRIP );
- for ( j = 0; j <= ntheta; ++j )
- {
- theta += dtheta;
- double cosa = cos( theta );
- double sina = sin ( theta );
- y = r * cosa;
- y1 = r1 * cosa; //current and next y
- z = r * sina;
- z1 = r1 * sina; //current and next z
- //edge from point at x to point at next x
- glVertex3f (x, y, z);
- if(j>0)
- {
- setNormal(p1x,p1y,p1z,p2x,p2y,p2z,x, y, z);
- }
- else
- {
- p1x=x;
- p1y=y;
- p1z=z;
- p2x=x1;
- p2y=y1;
- p2z=z1;
- }
- glVertex3f (x1, y1, z1);
- //forms quad with next pair of points with incremented theta value
- }
- glEnd();
- x = x1;
- r = r1;
- } //for i
- }
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- // gluPerspective(60,ar, 2.0, 100.0);
- glOrtho(-8.0, 8.0, -8.0*(GLfloat)height/(GLfloat)width, 8.0*(GLfloat)height/(GLfloat)width, 2.0, 25.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- gluLookAt(ex,ey,ez,lx,ly,lz,hx,hy,hz);
- }
- void showControlPoints()
- {
- glPointSize(5.0);
- glColor3f(1.0, 0.0, 1.0);
- glBegin(GL_POINTS);
- for (int i = 0; i <=L; i++)
- glVertex3fv(&ctrlpoints[i][0]);
- glEnd();
- }
- static void display(void)
- {
- const double t = glutGet(GLUT_ELAPSED_TIME) / 5000.0;
- const double a = t*90.0;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if(wired)
- {
- glPolygonMode( GL_FRONT, GL_LINE ) ;
- glPolygonMode( GL_BACK, GL_LINE ) ;
- }
- else
- {
- glPolygonMode( GL_FRONT,GL_FILL ) ;
- glPolygonMode( GL_BACK, GL_FILL ) ;
- }
- glPushMatrix();
- if(animat)
- glRotated(a,0,0,1);
- //else
- //glRotated(135,0,0,1);
- glRotatef( anglex, 1.0, 0.0, 0.0);
- glRotatef( angley, 0.0, 1.0, 0.0); //rotate about y-axis
- glRotatef( anglez, 0.0, 0.0, 1.0);
- glScaled(0.5,0.5,0.5);
- glRotatef( 270, 0.0, 0.0, 1.0);
- glTranslated(-3.5,0,0);
- glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
- matColor(0.9,0.5,0.1,20); // front face color
- matColor(0.0,0.5,0.8,20,1); // back face color
- bottleBezier();
- if(shcpt)
- {
- matColor(0.0,0.0,0.9,20);
- showControlPoints();
- }
- glPopMatrix();
- glutSwapBuffers();
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'A':
- animat=!animat;
- break;
- case 's':
- case 'S':
- shcpt=!shcpt;
- break;
- case 'w':
- case 'W':
- wired=!wired;
- break;
- case 'x':
- anglex = ( anglex + 3 ) % 360;
- break;
- case 'X':
- anglex = ( anglex - 3 ) % 360;
- break;
- case 'y':
- angley = ( angley + 3 ) % 360;
- break;
- case 'Y':
- angley = ( angley - 3 ) % 360;
- break;
- case 'z':
- anglez = ( anglez + 3 ) % 360;
- break;
- case 'Z':
- anglez = ( anglez - 3 ) % 360;
- break;
- case 'q':
- case 27 :
- glutDestroyWindow(window);
- exit(0);
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- const GLfloat light_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
- const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 1.0f };
- void matColor(float kdr, float kdg, float kdb, float shiny, int frnt_Back, float ambFactor, float specFactor)
- {
- const GLfloat mat_ambient[] = { kdr*ambFactor, kdg*ambFactor, kdb*ambFactor, 1.0f };
- const GLfloat mat_diffuse[] = { kdr, kdg, kdb, 1.0f };
- const GLfloat mat_specular[] = { 1.0f*specFactor, 1.0f*specFactor, 1.0f*specFactor, 1.0f };
- const GLfloat high_shininess[] = { shiny };
- if(frnt_Back==0)
- {
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
- }
- else if(frnt_Back==1)
- {
- glMaterialfv(GL_BACK, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_BACK, GL_SHININESS, high_shininess);
- }
- else if(frnt_Back==2)
- {
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, high_shininess);
- }
- }
- /* Program entry point */
- void myInit()
- {
- glClearColor(.1,.1,.1,1);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Curved Surface");
- myInit();
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutMouseFunc(processMouse);
- glutIdleFunc(idle);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement