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

📄 ideas.c

📁 嵌入式GUI OpenGL源代码。OpenGL是嵌入式开发中常用的一种GUI系统。
💻 C
📖 第 1 页 / 共 2 页
字号:
   case '4':
      new_speed = SPEED_SUPER_FAST;
      break;
   default:
      if (paused) timejerk = 1;
      paused = ~paused;
      if(paused) {
	 glutIdleFunc(NULL);
      } else {
	 glutIdleFunc(idle);
      }
   }
}

static void
vis(int visible)
{
  if (visible == GLUT_VISIBLE) {
      if(!paused) glutIdleFunc(idle);
      do_post_idle();
  } else {
      if(!paused) glutIdleFunc(NULL);
  }
}

static void display(void)
{
  float x, y, z, c;

    calc_spline(view_from, view_from_spline, current_time);
    calc_spline(view_to, view_to_spline, current_time);
    calc_spline(light_pos, light_pos_spline, current_time);
    light_pos[3] = 0.0;
    calc_spline(logo_pos, logo_pos_spline, current_time);
    calc_spline(logo_rot, logo_rot_spline, current_time);
    
    tmplight[1] = light_pos[X] - logo_pos[X];
    tmplight[2] = light_pos[Y] - logo_pos[Y];
    tmplight[3] = light_pos[Z] - logo_pos[Z];
    
    glNewList(LIGHT_TMP, GL_COMPILE); 
    glMaterialf(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, * tmplight); 
    glEndList();
    
    tv[0][0] = tv[1][1] = tv[2][2] = light_pos[Y];
    
    glColor3ub(0,  0,  0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    
    /*
     * SHADOW
     */
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(view_from[X], view_from[Y], view_from[Z], 
	      view_to[X], view_to[Y], view_to[Z],
	      0.0, 1.0, 0.0);
    
    if (view_from[Y] > 0.0) draw_table();

    glEnable(GL_CULL_FACE); 
    glDisable(GL_DEPTH_TEST); 

    if (logo_pos[Y] < 0.0) {
      
      if (logo_pos[Y]>-0.33) {
	/* We're emerging from the table */
	c = 1.0 - (logo_pos[Y]) / -0.33;
	pca /= 4.0;
	glColor3ub((GLubyte)(128.0*(1.0-c)*0.5 + 255.0*pca*c),
		   (GLubyte)(102.0*(1.0-c)*0.5 + 255.0*pca*c),
		   (GLubyte)(179.0*(1.0-c)*0.5 + 200.0*pca*c));
      } else {
	/* Still under table */
	glColor3ub(128/2,  102/2,  179/2);
      }
      
      glPushMatrix();
      glScalef(0.04,  0.0,  0.04);
      glRotatef(0.1 * (-900), 1.0, 0.0, 0.0);
      glRotatef(0.1 * ((int)(10.0*logo_rot[Z])), 0.0, 0.0, 1.0);
      glRotatef(0.1 * ((int)(10.0*logo_rot[Y])), 0.0, 1.0, 0.0);
      glRotatef(0.1 * ((int)(10.0*logo_rot[X])), 1.0, 0.0, 0.0);
      glRotatef(0.1 * (353), 1.0, 0.0, 0.0);
      glRotatef(0.1 * (450), 0.0, 1.0, 0.0);
      draw_logo_shadow();
      glPopMatrix();
    }
    
    if (logo_pos[Y] > 0.0) {
      glPushMatrix();
      if (logo_pos[Y]<0.33) {
	pca /= 4.0;
	c = 1.0 - (logo_pos[Y])/0.33;
	glColor3ub((GLubyte)(255.0*pca*c),
		   (GLubyte)(255.0*pca*c),
		   (GLubyte)(200.0*pca*c));
      } else {
	glColor3ub(0, 0, 0);
      }
      
      glTranslatef(light_pos[X],  light_pos[Y],  light_pos[Z]);
      glMultMatrixf(&tv[0][0]);
      glTranslatef(-light_pos[X]+logo_pos[X],
		   -light_pos[Y]+logo_pos[Y],
		   -light_pos[Z]+logo_pos[Z]);
      glScalef(0.04,  0.04,  0.04);
      glRotatef (0.1 * (-900), 1.0, 0.0, 0.0);
      glRotatef (0.1 * ((int)(10.0*logo_rot[Z])), 0.0, 0.0, 1.0);
      glRotatef (0.1 * ((int)(10.0*logo_rot[Y])), 0.0, 1.0, 0.0);
      glRotatef (0.1 * ((int)(10.0*logo_rot[X])), 1.0, 0.0, 0.0);
      glRotatef (0.1 * (353), 1.0, 0.0, 0.0);
      glRotatef (0.1 * (450), 0.0, 1.0, 0.0);


      glEnable(GL_POLYGON_STIPPLE);
      glPolygonStipple(stipple);
      draw_logo_shadow();
      glDisable(GL_POLYGON_STIPPLE);
      glPopMatrix();
    }
    /*
     * DONE SHADOW 
     */


    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glEnable(GL_LIGHTING);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(.1*(450),  5.0/4.0,  0.5,  20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    gluLookAt(view_from[X],  view_from[Y],  view_from[Z],
	      view_to[X],  view_to[Y],  view_to[Z], 
	      0.0, 1.0, 0.0);
    
    glCallList( MAT_HOLDER_RINGS); 
    
    glPushMatrix();
    glTranslatef(light_pos[X],  light_pos[Y],  light_pos[Z]);
    glScalef(0.1,  0.1,  0.1);
    
    x = light_pos[X] - logo_pos[X];
    y = light_pos[Y] - logo_pos[Y];
    z = light_pos[Z] - logo_pos[Z];
    
    if (x!=0.0) {
      a3 = -atan2(z, x)*10.0 RAD;
    } else a3 = 0.0;
    
    a4 = -atan2(sqrt(x*x + z*z), y)*10.0 RAD;
    
    glRotatef (0.1 * ((int)a3), 0.0, 1.0, 0.0);
    glRotatef (0.1 * ((int)a4), 0.0, 0.0, 1.0);
    glRotatef (0.1 * (-900), 1.0, 0.0, 0.0);
    
    glEnable(GL_LIGHT2);
    glEnable(GL_LIGHT3);
    glCallList(MAT_HEMISPHERE);
    glEnable(GL_NORMALIZE);
    draw_hemisphere();
    glDisable(GL_NORMALIZE);
    glPopMatrix();

    glDisable(GL_LIGHT2);
    glDisable(GL_LIGHT3); 
    glEnable(GL_LIGHT1);
    glLightfv(GL_LIGHT1, GL_POSITION, light_pos);
    
    if (logo_pos[Y] > -0.33) {

      glCallList(MAT_LOGO);
    
      glPushMatrix();
      glTranslatef(logo_pos[X],  logo_pos[Y],  logo_pos[Z]);
      glScalef(0.04,  0.04,  0.04);
      glRotatef (0.1 * (-900), 1.0, 0.0, 0.0);
      glRotatef (0.1 * ((int)(10.0*logo_rot[Z])), 0.0, 0.0, 1.0);
      glRotatef (0.1 * ((int)(10.0*logo_rot[Y])), 0.0, 1.0, 0.0);
      glRotatef (0.1 * ((int)(10.0*logo_rot[X])), 1.0, 0.0, 0.0);
      glRotatef (0.1 * (353), 1.0, 0.0, 0.0);
      glRotatef (0.1 * (450), 0.0, 1.0, 0.0);
      glEnable(GL_LIGHTING);
      draw_logo();
      glPopMatrix();
    }
    
    if (view_from[Y] < 0.0) draw_under_table();
    
    glutSwapBuffers();

    if(post_idle) do_post_idle();
}

static void do_post_idle(void)
{
  struct timeval current;
  float timediff;	

    /* Time jerked -- adjust clock appropriately */
    if (timejerk) {
      timejerk = 0;
      timeoffset = current_time;
      gettimeofday(&start, NULL);
    }
    
    /* Reset our timer */
    if (resetclock) {
      resetclock = 0;
      paused = 0;
      timeoffset = START_TIME;
      gettimeofday(&start, NULL);
    }
    
    /* Compute new time */
    gettimeofday(&current, NULL);
    timediff = (current.tv_sec - start.tv_sec) + 
      ((double) (current.tv_usec - start.tv_usec)) / 1000000.0;
    if (!paused) {
       current_time = timediff * move_speed + timeoffset;
    }
    
    /* Adjust to new speed */
    if (new_speed != move_speed) {
      move_speed = new_speed;
      timeoffset = current_time;
      gettimeofday(&start, NULL);
    }
    post_idle = 0;
}

static void resize_window(int w, int h) 
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective (45.0, 5.0/4.0, 0.5, 20.0); 
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glViewport(0, 0, w, h);
}

static void initialize(void)
{
    int attr;

    attr = doublebuffer ? RGBA_DB_attributes : RGBA_SB_attributes;
    glutInitDisplayMode(attr);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Ideas");

    if (multisample) glEnable(GL_POLYGON_SMOOTH); 
    
    init_lights();
    init_materials();

    build_table();

    view_from_spline = calc_spline_params(view_from_ctl, TIME);
    view_to_spline = calc_spline_params(view_to_ctl, TIME);
    light_pos_spline = calc_spline_params(light_pos_ctl, TIME);
    logo_pos_spline = calc_spline_params(logo_pos_ctl, TIME);
    logo_rot_spline = calc_spline_params(logo_rot_ctl, TIME);

    glutReshapeFunc(resize_window);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutVisibilityFunc(vis);

    glMatrixMode(GL_MODELVIEW);
}


static void build_table(void) 
{
    float i, j;

    for (j=0.0; j<=TABLERES*1.0; j+=1.0) {
	for (i=0.0; i<=TABLERES*1.0; i+=1.0) {
	    table_points[(int)j][(int)i][Z] = (i-TABLERES*1.0/2.0)/2.0;
	    table_points[(int)j][(int)i][X] = (j-TABLERES*1.0/2.0)/2.0;
	    table_points[(int)j][(int)i][Y] = 0.0;
	}
    }
}


void draw_table(void)
{
    float c;
    int i, j;
    int k, l;
    float ov[3], lv[3];

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);

    ov[X] = light_pos[X]-logo_pos[X];
    ov[Y] = light_pos[Y]-logo_pos[Y];
    ov[Z] = light_pos[Z]-logo_pos[Z];

    normalize(ov);

    for (j=0; j<=TABLERES; j++) {
      for (i=0; i<=TABLERES; i++) {
	lv[X] = light_pos[X] - table_points[j][i][X];
	lv[Y] = light_pos[Y] - table_points[j][i][Y];
	lv[Z] = light_pos[Z] - table_points[j][i][Z];
	normalize(lv);
	if ((c = dot(lv, ov))<0.0) c = 0.0;
	c = c * c * c * lv[Y] * 255.0;
	/* fade */
	if ((current_time>TIME-5.0) && (current_time<TIME-3.0)) 
	  c *= 1.0 - (current_time-(TIME-5.0)) * 0.5;
	
	tablecolors[j][i] = (int)c;
      }
    }
    
    
    for (l=0; l<TABLERES; l++) {
      
      glBegin(GL_TRIANGLE_STRIP);
      for (k=0; k<=TABLERES; k++) {
	glColor3ub(tablecolors[l][k],
		   tablecolors[l][k],
		   tablecolors[l][k]);
	glVertex3fv(table_points[l][k]);

	glColor3ub(tablecolors[l+1][k],
		   tablecolors[l+1][k], 
		   tablecolors[l+1][k]);
	glVertex3fv(table_points[l+1][k]);
	
      }
	glEnd();
    }

    if (logo_pos[Y]>-0.33 && logo_pos[Y]<0.33) {
	glEnable(GL_DEPTH_TEST);
    }

    pca = 0.0;
    glBegin(GL_POLYGON);
    for (i=0; i<4; i++) {
      lv[X] = light_pos[X] - paper_points[i][X];
      lv[Y] = light_pos[Y] - paper_points[i][Y];
      lv[Z] = light_pos[Z] - paper_points[i][Z];
      normalize(lv);
      if ((c = dot(lv, ov))<0.0) c = 0.0;
      c = c * c * c * lv[Y];
      /* fade */
      if ((current_time>TIME-5.0) && (current_time<TIME-3.0)) 
	c *= 1.0 - (current_time-(TIME-5.0)) * 0.5;
      
      pcr = c * 255; pcg = c * 255; pcb = c * 200;
      pca += c;
      glColor3ub((GLubyte)pcr,  (GLubyte)pcg,  (GLubyte)pcb);
      glVertex3fv(paper_points[i]);
    }
    glEnd();

    glPushMatrix();
    glRotatef (0.1 * (-184), 0.0, 1.0, 0.0);
    glTranslatef(-0.3, 0.0, -0.8);
    glRotatef (0.1 * (-900), 1.0, 0.0, 0.0);
    glScalef(0.015, 0.015, 0.015);


    if (current_time>TIME*1.0-5.0) {
	c = (current_time-(TIME*1.0-5.0))/2.0;
	glColor3ub((GLubyte)(c*255.0),  (GLubyte)(c*255.0),  (GLubyte)(c*255.0));
    } else glColor3ub(0,  0,  0);

    glDisable(GL_DEPTH_TEST);

    draw_i();
    glTranslatef(3.0,  0.0,  0.0);

    draw_d();
    glTranslatef(6.0,  0.0,  0.0);

    draw_e();
    glTranslatef(5.0,  0.0,  0.0);

    draw_a();
    glTranslatef(6.0,  0.0,  0.0);

    draw_s();
    glTranslatef(10.0,  0.0,  0.0);

    draw_i();
    glTranslatef(3.0,  0.0,  0.0);

    draw_n();
    glTranslatef(-31.0,  -13.0,  0.0);

    draw_m();
    glTranslatef(10.0,  0.0,  0.0);

    draw_o();
    glTranslatef(5.0,  0.0,  0.0);

    draw_t();
    glTranslatef(4.0,  0.0,  0.0);

    draw_i();
    glTranslatef(3.5,  0.0,  0.0);

    draw_o();
    glTranslatef(5.0,  0.0,  0.0);

    draw_n();

    glPopMatrix();

}



void draw_under_table(void) 
{
    int k, l;

    glDisable(GL_DEPTH_TEST);


    glColor3ub(0,  0,  0);

    for (l=0; l<TABLERES; l++) {

	glBegin(GL_TRIANGLE_STRIP);
	for (k=0; k<=TABLERES; k++) {

	    glVertex3fv(table_points[l][k]);
	    glVertex3fv(table_points[l+1][k]);

	}
	glEnd();
    }

    glEnable(GL_DEPTH_TEST); 

}

static void calc_spline(vector v, parameter *params, float current_time)
{

    float t;
    int ti, i;

    t = current_time - (float)((int)current_time);

    ti = current_time;
    /* XXX Hack so that time will not overflow the params array.
       The size of the spline params array should not be built into
       this routine this way. */
    if (ti >= (TIME - 3)) {
      ti = TIME - 4;
    }

    for (i=0; i<3; i++) {
	v[i] = params[ti][3][i] +
	       params[ti][2][i] * t +
	       params[ti][1][i] * t * t +
	       params[ti][0][i] * t * t * t;
    }

}

static parameter *calc_spline_params(vector *ctl_pts, int n)
{

    int i, j;
    parameter *params;

    if (n<4) {
	fprintf(stderr,
	    "calc_spline_params: not enough control points\n");
	return (NULL);
    }

    params = (parameter *)malloc(sizeof(parameter) * (n-3));

    for (i=0; i<n-3; i++) {

	for (j=0; j<3; j++) {

	    params[i][3][j] = ctl_pts[i+1][j];
	    params[i][2][j] = ctl_pts[i+2][j] - ctl_pts[i][j];
	    params[i][1][j] =  2.0 * ctl_pts[i][j] +
			      -2.0 * ctl_pts[i+1][j] +
			       1.0 * ctl_pts[i+2][j] +
			      -1.0 * ctl_pts[i+3][j];
	    params[i][0][j] = -1.0 * ctl_pts[i][j] +
			       1.0 * ctl_pts[i+1][j] +
			      -1.0 * ctl_pts[i+2][j] +
			       1.0 * ctl_pts[i+3][j];

	}
    }

    return (params);
}


static void normalize(vector v)
{
    float r;

    r = sqrt(v[X]*v[X] + v[Y]*v[Y] + v[Z]*v[Z]);

    v[X] /= r;
    v[Y] /= r;
    v[Z] /= r;
}


static float dot(vector v1, vector v2)
{
    return v1[X]*v2[X]+v1[Y]*v2[Y]+v1[Z]*v2[Z];
}

⌨️ 快捷键说明

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