📄 shapes.c
字号:
for (j=0; j<slices; j++) { glVertex3d(cost[j+0]*r0, sint[j+0]*r0, z0 ); glVertex3d(0, 0, height); glNormal3d(cost[j+1]*sinn, sint[j+1]*sinn, cosn ); glVertex3d(cost[j+1]*r0, sint[j+1]*r0, z0 ); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost);}/* * Draws a wire cone */void GLUTAPIENTRY glutWireCone( GLdouble base, GLdouble height, GLint slices, GLint stacks){ int i,j; /* Step in z and radius as stacks are drawn. */ double z = 0.0; double r = base; const double zStep = height/stacks; const double rStep = base/stacks; /* Scaling factors for vertex normals */ const double cosn = ( height / sqrt ( height * height + base * base )); const double sinn = ( base / sqrt ( height * height + base * base )); /* Pre-computed circle */ double *sint,*cost; circleTable(&sint,&cost,-slices); /* Draw the stacks... */ for (i=0; i<stacks; i++) { glBegin(GL_LINE_LOOP); for( j=0; j<slices; j++ ) { glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn); glVertex3d(cost[j]*r, sint[j]*r, z ); } glEnd(); z += zStep; r -= rStep; } /* Draw the slices */ r = base; glBegin(GL_LINES); for (j=0; j<slices; j++) { glNormal3d(cost[j]*sinn, sint[j]*sinn, cosn ); glVertex3d(cost[j]*r, sint[j]*r, 0.0 ); glVertex3d(0.0, 0.0, height); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost);}/* * Draws a solid cylinder */void GLUTAPIENTRY glutSolidCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks){ int i,j; /* Step in z and radius as stacks are drawn. */ double z0,z1; const double zStep = height/stacks; /* Pre-computed circle */ double *sint,*cost; circleTable(&sint,&cost,-slices); /* Cover the base and top */ glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 0.0, -1.0 ); glVertex3d(0.0, 0.0, 0.0 ); for (j=0; j<=slices; j++) glVertex3d(cost[j]*radius, sint[j]*radius, 0.0); glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 0.0, 1.0 ); glVertex3d(0.0, 0.0, height); for (j=slices; j>=0; j--) glVertex3d(cost[j]*radius, sint[j]*radius, height); glEnd(); /* Do the stacks */ z0 = 0.0; z1 = zStep; for (i=1; i<=stacks; i++) { if (i==stacks) z1 = height; glBegin(GL_QUAD_STRIP); for (j=0; j<=slices; j++ ) { glNormal3d(cost[j], sint[j], 0.0 ); glVertex3d(cost[j]*radius, sint[j]*radius, z0 ); glVertex3d(cost[j]*radius, sint[j]*radius, z1 ); } glEnd(); z0 = z1; z1 += zStep; } /* Release sin and cos tables */ free(sint); free(cost);}/* * Draws a wire cylinder */void GLUTAPIENTRY glutWireCylinder(GLdouble radius, GLdouble height, GLint slices, GLint stacks){ int i,j; /* Step in z and radius as stacks are drawn. */ double z = 0.0; const double zStep = height/stacks; /* Pre-computed circle */ double *sint,*cost; circleTable(&sint,&cost,-slices); /* Draw the stacks... */ for (i=0; i<=stacks; i++) { if (i==stacks) z = height; glBegin(GL_LINE_LOOP); for( j=0; j<slices; j++ ) { glNormal3d(cost[j], sint[j], 0.0); glVertex3d(cost[j]*radius, sint[j]*radius, z ); } glEnd(); z += zStep; } /* Draw the slices */ glBegin(GL_LINES); for (j=0; j<slices; j++) { glNormal3d(cost[j], sint[j], 0.0 ); glVertex3d(cost[j]*radius, sint[j]*radius, 0.0 ); glVertex3d(cost[j]*radius, sint[j]*radius, height); } glEnd(); /* Release sin and cos tables */ free(sint); free(cost);}/* * */void GLUTAPIENTRY glutWireTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings ){ double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi; double *vertex, *normal; int i, j; double spsi, cpsi, sphi, cphi ; /* * Allocate the vertices array */ vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings ); normal = (double *)calloc( sizeof(double), 3 * nSides * nRings ); glPushMatrix(); dpsi = 2.0 * M_PI / (double)nRings ; dphi = -2.0 * M_PI / (double)nSides ; psi = 0.0; for( j=0; j<nRings; j++ ) { cpsi = cos ( psi ) ; spsi = sin ( psi ) ; phi = 0.0; for( i=0; i<nSides; i++ ) { int offset = 3 * ( j * nSides + i ) ; cphi = cos ( phi ) ; sphi = sin ( phi ) ; *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ; *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ; *(vertex + offset + 2) = sphi * iradius ; *(normal + offset + 0) = cpsi * cphi ; *(normal + offset + 1) = spsi * cphi ; *(normal + offset + 2) = sphi ; phi += dphi; } psi += dpsi; } for( i=0; i<nSides; i++ ) { glBegin( GL_LINE_LOOP ); for( j=0; j<nRings; j++ ) { int offset = 3 * ( j * nSides + i ) ; glNormal3dv( normal + offset ); glVertex3dv( vertex + offset ); } glEnd(); } for( j=0; j<nRings; j++ ) { glBegin(GL_LINE_LOOP); for( i=0; i<nSides; i++ ) { int offset = 3 * ( j * nSides + i ) ; glNormal3dv( normal + offset ); glVertex3dv( vertex + offset ); } glEnd(); } free ( vertex ) ; free ( normal ) ; glPopMatrix();}/* * */void GLUTAPIENTRY glutSolidTorus( GLdouble dInnerRadius, GLdouble dOuterRadius, GLint nSides, GLint nRings ){ double iradius = dInnerRadius, oradius = dOuterRadius, phi, psi, dpsi, dphi; double *vertex, *normal; int i, j; double spsi, cpsi, sphi, cphi ; /* * Increment the number of sides and rings to allow for one more point than surface */ nSides ++ ; nRings ++ ; /* * Allocate the vertices array */ vertex = (double *)calloc( sizeof(double), 3 * nSides * nRings ); normal = (double *)calloc( sizeof(double), 3 * nSides * nRings ); glPushMatrix(); dpsi = 2.0 * M_PI / (double)(nRings - 1) ; dphi = -2.0 * M_PI / (double)(nSides - 1) ; psi = 0.0; for( j=0; j<nRings; j++ ) { cpsi = cos ( psi ) ; spsi = sin ( psi ) ; phi = 0.0; for( i=0; i<nSides; i++ ) { int offset = 3 * ( j * nSides + i ) ; cphi = cos ( phi ) ; sphi = sin ( phi ) ; *(vertex + offset + 0) = cpsi * ( oradius + cphi * iradius ) ; *(vertex + offset + 1) = spsi * ( oradius + cphi * iradius ) ; *(vertex + offset + 2) = sphi * iradius ; *(normal + offset + 0) = cpsi * cphi ; *(normal + offset + 1) = spsi * cphi ; *(normal + offset + 2) = sphi ; phi += dphi; } psi += dpsi; } glBegin( GL_QUADS ); for( i=0; i<nSides-1; i++ ) { for( j=0; j<nRings-1; j++ ) { int offset = 3 * ( j * nSides + i ) ; glNormal3dv( normal + offset ); glVertex3dv( vertex + offset ); glNormal3dv( normal + offset + 3 ); glVertex3dv( vertex + offset + 3 ); glNormal3dv( normal + offset + 3 * nSides + 3 ); glVertex3dv( vertex + offset + 3 * nSides + 3 ); glNormal3dv( normal + offset + 3 * nSides ); glVertex3dv( vertex + offset + 3 * nSides ); } } glEnd(); free ( vertex ) ; free ( normal ) ; glPopMatrix();}/* * */void GLUTAPIENTRY glutWireDodecahedron( void ){ /* Magic Numbers: It is possible to create a dodecahedron by attaching two pentagons to each face of * of a cube. The coordinates of the points are: * (+-x,0, z); (+-1, 1, 1); (0, z, x ) * where x = 0.61803398875 and z = 1.61803398875. */ glBegin ( GL_LINE_LOOP ) ; glNormal3d ( 0.0, 0.525731112119, 0.850650808354 ) ; glVertex3d ( 0.0, 1.61803398875, 0.61803398875 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ; glEnd () ; glBegin ( GL_LINE_LOOP ) ; glNormal3d ( 0.0, 0.525731112119, -0.850650808354 ) ; glVertex3d ( 0.0, 1.61803398875, -0.61803398875 ) ; glVertex3d ( 1.0, 1.0, -1.0 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( -1.0, 1.0, -1.0 ) ; glEnd () ; glBegin ( GL_LINE_LOOP ) ; glNormal3d ( 0.0, -0.525731112119, 0.850650808354 ) ; glVertex3d ( 0.0, -1.61803398875, 0.61803398875 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ; glEnd () ; glBegin ( GL_LINE_LOOP ) ; glNormal3d ( 0.0, -0.525731112119, -0.850650808354 ) ; glVertex3d ( 0.0, -1.61803398875, -0.61803398875 ) ; glVertex3d ( -1.0, -1.0, -1.0 ) ; glVertex3d ( -0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 0.61803398875, 0.0, -1.61803398875 ) ; glVertex3d ( 1.0, -1.0, -1.0 ) ; glEnd () ; glBegin ( GL_LINE_LOOP ) ; glNormal3d ( 0.850650808354, 0.0, 0.525731112119 ) ; glVertex3d ( 0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( 1.0, -1.0, 1.0 ) ; glVertex3d ( 1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( 1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( 1.0, 1.0, 1.0 ) ; glEnd () ; glBegin ( GL_LINE_LOOP ) ; glNormal3d ( -0.850650808354, 0.0, 0.525731112119 ) ; glVertex3d ( -0.61803398875, 0.0, 1.61803398875 ) ; glVertex3d ( -1.0, 1.0, 1.0 ) ; glVertex3d ( -1.61803398875, 0.61803398875, 0.0 ) ; glVertex3d ( -1.61803398875, -0.61803398875, 0.0 ) ; glVertex3d ( -1.0, -1.0, 1.0 ) ; glEnd () ; glBegin ( GL_LINE_LOOP ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -