⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dinospin.c

📁 一个正二十面体通过迭代产生(star)星形的漂亮的工程,需要glut
💻 C
📖 第 1 页 / 共 2 页
字号:
/* six equidistant points lying on the unit sphere */
#define XPLUS {  1,  0,  0 }    /*  X */
#define XMIN  { -1,  0,  0 }    /* -X */
#define YPLUS {  0,  1,  0 }    /*  Y */
#define YMIN  {  0, -1,  0 }    /* -Y */
#define ZPLUS {  0,  0,  1 }    /*  Z */
#define ZMIN  {  0,  0, -1 }    /* -Z */

/* for icosahedron */
#define CZ (0.89442719099991)   /*  2/sqrt(5) */
#define SZ (0.44721359549995)   /*  1/sqrt(5) */
#define C1 (0.95105651629515)        /* cos(18),  */
#define S1 (0.30901699437495)        /* sin(18) */
#define C2 (0.58778525229247)        /* cos(54),  */
#define S2 (0.80901699437495)        /* sin(54) */
#define K  (0.61803398874989)
#define R  (0.38196601125011)
#define r  (1-R)
#define X1 (C1*CZ)
#define Y1 (S1*CZ)
#define X2 (C2*CZ)
#define Y2 (S2*CZ)

#define Ip0     {0.,    0.,     1.}
#define Ip1     {-X2,   -Y2,    SZ}
#define Ip2     {X2,    -Y2,    SZ}
#define Ip3     {X1,    Y1,     SZ}
#define Ip4     {0,     CZ,     SZ}
#define Ip5     {-X1,   Y1,     SZ}

#define Im0     {0.,    0.,     -1.}
#define Im1     {X2,    Y2,     -SZ}
#define Im2     {-X2,   Y2,     -SZ}
#define Im3     {-X1,   -Y1,    -SZ}
#define Im4     {0,     -CZ,    -SZ}
#define Im5     {X1,    -Y1,    -SZ}

/* vertices of a unit icosahedron */
/*static point3 p[12][5]= {
        
        { Ip1, Ip2, Ip3, Ip4, Ip5 },
        { Ip0, Ip5, Im3, Im4, Ip2 },
        { Ip0, Ip1, Im4, Im5, Ip3 },
        { Ip0, Ip2, Im5, Im1, Ip4 },
        { Ip0, Ip3, Im1, Im2, Ip5 },
		{ Ip0, Ip4, Im2, Im3, Ip1 },

        
        { Im1, Im5, Im4, Im3, Im2 },
        { Im0, Im2, Ip4, Ip3, Im5 },
        { Im0, Im3, Ip5, Ip4, Im1 },
        { Im0, Im4, Ip1, Ip5, Im2 },
        { Im0, Im5, Ip2, Ip1, Im3 },
        { Im0, Im1, Ip3, Ip2, Im4 },
        
}; */
static point3 pt[12]={
	Ip0,Ip1,Ip2,Ip3,Ip4,Ip5,
    Im0,Im1,Im2,Im3,Im4,Im5
};

int depth;  //recursion depth

void sub(point3 p2,point3 p1,point3 diff){
// diff = p2 - p1
     int i;
        for(i=0;i<3;i++)
		{
			diff[i]=p2[i]-p1[i];
		}
}

void findNormal(point3 p1,point3 p2,point3 p3,point3 n){
// n = (p2-p1) x (p3-p2)
        point3 u,v;
        sub(p2,p1,u);
        sub(p3,p2,v);
        n[0]=u[1]*v[2]-u[2]*v[1];
        n[1]=u[2]*v[0]-u[0]*v[2];
        n[2]=u[0]*v[1]-u[1]*v[0];
}
void kpoint(point3 p1,point3 p2,point3 kp,float f)
{
	int i;
	for(i=0;i<3;i++)
	{
		kp[i]=(p1[i]+f*p2[i])/(1+f);
	}
}
void rpoint(point3 p,point3 rp,float f)
{
	int i;
	for(i=0;i<3;i++)
	{
		rp[i]=f*p[i];
	}
	
}
void dpoint(point3 p1,point3 p2,point3 dp)
{
	int i;
	for(i=0;i<3;i++)
	{  
		dp[i]=p1[i]+p2[i];
	}
	
}

void normalize(point3 p){
        int i;
        GLfloat x=0;
        for(i=0;i<3;i++)x+=p[i]*p[i];
        for(i=0;i<3;i++)p[i]/=sqrt(x);
}
void triangle(point3 p1,point3 p2,point3 p3)
{
	point3 n;
    findNormal(p1,p2,p3,n);
    glBegin(GL_POLYGON);
    glNormal3fv(n);            
    glVertex3fv(p1);            
    glVertex3fv(p2);            
    glVertex3fv(p3);
    glEnd();
}
void face(point3 p1,point3 p2,point3 p3,point3 p4,point3 p5)
{  
	point3 p11,p22,p33,p44,p55;
    kpoint(p3,p5,p11,K);
	kpoint(p4,p1,p22,K);
	kpoint(p5,p2,p33,K);
	kpoint(p1,p3,p44,K);
	kpoint(p2,p4,p55,K);

	triangle(p1,p44,p33);
	triangle(p2,p55,p44);
	triangle(p3,p11,p55);
	triangle(p4,p22,p11);
	triangle(p5,p33,p22);
	
    
}

void drawFace(int depth,point3 p[12]){
	int h, i,j,k;
	  point3 pot[12],pont[12],pnt[12];

// Draw polygon p1 p2 p3, or recursively subdivide
        if(depth<=0){
                face(p[1],p[2],p[3],p[4],p[5]);
                
				face(p[0],p[5],p[9],p[10],p[2]);
				
				face(p[0],p[1],p[10],p[11],p[3]);

                face(p[0],p[2],p[11],p[7],p[4]);
			
				face(p[0],p[3],p[7],p[8],p[5]);

				face(p[0],p[4],p[8],p[9],p[1]);

				face(p[7],p[11],p[10],p[9],p[8]);

                face(p[6],p[8],p[4],p[3],p[11]);

				face(p[6],p[9],p[5],p[4],p[7]);

				face(p[6],p[10],p[1],p[5],p[8]);

				face(p[6],p[11],p[2],p[1],p[9]);

			    face(p[6],p[7],p[3],p[2],p[10]);

        }else{ drawFace(depth-1,p);

               for(i=0;i<12;i++)
			   {
				   rpoint(p[i],pot[i],R);
			   }

				for(h=0;h<12;h++)
				{
					rpoint(pt[h],pont[h],r);
				}
				

				for( j=0;j<12;j++)
				{
				
					for( k=0;k<12;k++)
					{
						 dpoint(pot[k],pont[j],pnt[k]);
					}
					drawFace(depth-1,pnt);
				  
			                
				}
				
		}
}


//void drawface(int depth,int i1,int i2,int i3,int i4,int i5,int i){
 //to pass indices instead of points on initial call
//        drawFace(depth,p[i][i1],p[i][i2],p[i][i3],p[i][i4],p[i][i5]);
//}

void myinit(void)
{
    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 50.0 };
    GLfloat light_position[] = { 1.0, 0.0, 4.0, 0.0 };
/* For real-world objects, diffuse and ambient reflectance are normally 
the same color. For this reason, OpenGL provides you with a convenient 
way of assigning the same value to both simultaneously with glMaterial*(): 
*/
    GLfloat mat_amb_diff[] = { 0.1, 0.5, 0.8, 1.0 };
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 
        mat_amb_diff);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
    /* set up viewing */

	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluPerspective(40.0, 1.0, 2.0, 200.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //glGetFloatv(GL_MODELVIEW_MATRIX, modelmatrix);

 
    //float w=0.1;
   // glFrustum(-0.1,0.1,-0.1,0.1,.2,10);
}

void drawCube(int depth)
{
        drawFace(depth,pt);
			 
        
}

/* void display( void )
{
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
        glLoadIdentity();
        gluLookAt(0,0,4,0,0,0,0,1,0);
        drawCube(depth);
        glFlush();
} */

void key(unsigned char k,int x, int y){
        if(k==' ')depth++;
        else if(depth>0)depth--;
        glutPostRedisplay();
}

void
redraw(void)
{
  recalcModelView();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  //glutSolidIcosahedron();
  drawCube(depth);
  glutSwapBuffers();
}

/*void
myReshape(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective( 40.0,1.0*w/h,1.0,800.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
}*/
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1., 1., -1. * (GLfloat) h / (GLfloat) w,
1. * (GLfloat) h / (GLfloat) w, -30.0, 30.0);
else
glOrtho(-1. * (GLfloat) w / (GLfloat) h,
1. * (GLfloat) w / (GLfloat) h, -1., 1., -30.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void
mouse(int button, int state, int x, int y)
{
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    spinning = 0;
    glutIdleFunc(NULL);
    moving = 1;
    beginx = x;
    beginy = y;
    if (glutGetModifiers() & GLUT_ACTIVE_SHIFT) {
      scaling = 1;
    } else {
      scaling = 0;
    }
  }
  if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
    moving = 0;
  }
}

void
animate(void)
{
  add_quats(lastquat, curquat, curquat);
  glutPostRedisplay();
}

void
motion(int x, int y)
{
  if (scaling) {
    scalefactor = scalefactor * (1.0 + (((float) (beginy - y)) / H));
    beginx = x;
    beginy = y;
    glutPostRedisplay();
    return;
  }
  if (moving) {
    trackball(lastquat,
      (2.0 * beginx - W) / W,
      (H - 2.0 * beginy) / H,
      (2.0 * x - W) / W,
      (H - 2.0 * y) / H
      );
    beginx = x;
    beginy = y;
    spinning = 1;
    glutIdleFunc(animate);
  }
}
int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  depth=1;
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  trackball(curquat, 0.0, 0.0, 0.0, 0.0);
  glutInitWindowSize(400,400);
  glutInitWindowPosition(200,100);
  glutCreateWindow("spin");
  glutDisplayFunc(redraw);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  //glEnable(GL_CULL_FACE);
  glEnable(GL_DEPTH_TEST);
  //glEnable(GL_LIGHTING);
  //glMatrixMode(GL_PROJECTION);
  //glMatrixMode(GL_MODELVIEW);
  //gluLookAt(0.0, 0.0, 6.0,  /* eye is at (0,0,3) */
    //0.0, 0.0, 0.0,      /* center is at (0,0,0) */
    //0.0, 1.0, 0.);      /* up is in positive Y direction */
  //glPushMatrix();       /* dummy push so we can pop on model*/
  myinit();                       
  /*glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
  glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
  glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
  glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);*/
  glEnable(GL_SMOOTH); /* Enable smooth shading and color interpolation */
  glEnable(GL_NORMALIZE);
  glLineWidth(5.0);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -