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

📄 ideas.c

📁 嵌入式GUI OpenGL源代码。OpenGL是嵌入式开发中常用的一种GUI系统。
💻 C
📖 第 1 页 / 共 2 页
字号:

/* Copyright (c) Mark J. Kilgard, 1995. */

/*
 * (c) Copyright 1993, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED 
 * Permission to use, copy, modify, and distribute this software for 
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that 
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. 
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * US Government Users Restricted Rights 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
 */
#include <math.h>
#ifdef _WIN32
#include <windows.h>
#include <winsock.h>
#include <sys/timeb.h>
#define gettimeofday(_x, _y)          \
{                                     \
  struct timeb _t;                    \
  ftime(&_t);                         \
  (_x)->tv_sec = _t.time;             \
  (_x)->tv_usec = _t.millitm * 1000;  \
}
#else
#include <sys/time.h>
#endif
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <stdio.h>
#include <stdlib.h>
#include "objects.h"
#include <GL/glut.h>

#define X 0
#define Y 1
#define Z 2

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#define DEG *M_PI/180.0
#define RAD *180.0/M_PI

float move_speed;		/* Spline distance per second */

int multisample = 0;		/* Antialias polygons? */
int doublebuffer = 1;		/* Doublebuffer? */


#define SPEED_SLOW		0.2	/* Spline distances per second */
#define SPEED_MEDIUM		0.4
#define SPEED_FAST		0.7
#define SPEED_SUPER_FAST	1.0

#define O_NOMS		7
#define O_4MS		8
#define O_8MS		9
#define O_16MS		10

static int RGBA_SB_attributes = GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE;

static int RGBA_DB_attributes = GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE;

float light1_ambient[] = { 0.0,0.0,0.0,1.0 };
float light1_lcolor[] = { 1.0,1.0,1.0,1.0 };
float light1_position[] = { 0.0,1.0,0.0,0.0 };

float light2_ambient[] = { 0.0,0.0,0.0,1.0 };
float light2_lcolor[] = { 0.3,0.3,0.5,1.0 };
float light2_position[] = { -1.0,0.0,0.0,0.0 };

float light3_ambient[] = { 0.2,0.2,0.2,1.0 };
float light3_lcolor[] = { 0.2,0.2,0.2,1.0 };
float light3_position[] = { 0.0,-1.0,0.0,0.0 };

float lmodel_LVW[] = { 0.0 };
float lmodel_ambient[] = { 0.3,0.3,0.3,1.0 };
float lmodel_TWO[] = { GL_TRUE };

float mat_logo_ambient[] = {0.1, 0.1, 0.1, 1.0};
float mat_logo_diffuse[] = {0.5, 0.4, 0.7, 1.0};
float mat_logo_specular[] = {1.0, 1.0, 1.0, 1.0};
float mat_logo_shininess[] = {30.0};

float mat_holder_base_ambient[] = {0.0, 0.0, 0.0, 1.0};
float mat_holder_base_diffuse[] = {0.6, 0.6, 0.6, 1.0};
float mat_holder_base_specular[] = {0.8, 0.8, 0.8, 1.0};
float mat_holder_base_shininess[] = {30.0};

float mat_holder_rings_ambient[] = { 0.0,0.0,0.0,1.0 };
float mat_holder_rings_diffuse[] = { 0.9,0.8,0.0,1.0 };
float mat_holder_rings_specular[] = { 1.0,1.0,1.0,1.0 };
float mat_holder_rings_shininess[] = { 30.0 };

float mat_hemisphere_ambient[] = {0.0, 0.0, 0.0,1.0 };
float mat_hemisphere_diffuse[] = {1.0, 0.2, 0.2,1.0 };
float mat_hemisphere_specular[] = {0.5, 0.5, 0.5,1.0 };
float mat_hemisphere_shininess[] = {20.0};

GLubyte stipple[32*32];

typedef float vector[3];
typedef float vector4[4];
typedef vector parameter[4];

/*
 * Function definitions
 */
static void initialize(void);
static void resize_window(int w, int h);
static void build_table(void);
static parameter *calc_spline_params(vector *ctl_pts, int n);
static void calc_spline(vector v, parameter *params, float current_time);
static void normalize(vector v);
static float dot(vector v1, vector v2);
void draw_table(void);
void draw_logo_shadow(void);
void draw_hemisphere(void);
void draw_logo(void);
void draw_under_table(void);
void draw_i(void);
void draw_d(void);
void draw_e(void);
void draw_a(void);
void draw_s(void);
void draw_n(void);
void draw_m(void);
void draw_o(void);
void draw_t(void);

int post_idle = 0;
static void idle(void);
static void do_post_idle(void);
static void display(void);
static void mouse(int b, int s, int x, int y);
static void keyboard(unsigned char c, int x, int y);
static void vis(int);

static void init_materials(void) {
  int x, y;

  /* Stipple pattern */
  for (y = 0; y < 32; y++)
    for (x = 0; x < 4; x++) 
      stipple[y * 4 + x] = (y % 2) ? 0xaa : 0x55;

    glNewList(MAT_LOGO, GL_COMPILE); 
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_logo_ambient); 
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_logo_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_logo_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_logo_shininess);
    glEndList(); 

    glNewList( MAT_HOLDER_BASE, GL_COMPILE);
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_holder_base_ambient); 
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_holder_base_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_holder_base_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_holder_base_shininess);
    glEndList();

    glNewList(MAT_HOLDER_RINGS, GL_COMPILE); 
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_holder_rings_ambient); 
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_holder_rings_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_holder_rings_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_holder_rings_shininess);
    glEndList();

    glNewList(MAT_HEMISPHERE, GL_COMPILE); 
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_hemisphere_ambient); 
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_hemisphere_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_hemisphere_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_hemisphere_shininess);
    glEndList();

}

void init_lights(void) {
  static float ambient[] = { 0.1, 0.1, 0.1, 1.0 };
  static float diffuse[] = { 0.5, 1.0, 1.0, 1.0 };
  static float position[] = { 90.0, 90.0, 150.0, 0.0 };
  
  glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, position);

  glLightfv (GL_LIGHT1, GL_AMBIENT, light1_ambient);
  glLightfv (GL_LIGHT1, GL_SPECULAR, light1_lcolor);
  glLightfv (GL_LIGHT1, GL_DIFFUSE, light1_lcolor);
  glLightfv (GL_LIGHT1, GL_POSITION, light1_position);
    
  glLightfv (GL_LIGHT2, GL_AMBIENT, light2_ambient);
  glLightfv (GL_LIGHT2, GL_SPECULAR, light2_lcolor);
  glLightfv (GL_LIGHT2, GL_DIFFUSE, light2_lcolor);
  glLightfv (GL_LIGHT2, GL_POSITION, light2_position);

  glLightfv (GL_LIGHT3, GL_AMBIENT, light3_ambient);
  glLightfv (GL_LIGHT3, GL_SPECULAR, light3_lcolor);
  glLightfv (GL_LIGHT3, GL_DIFFUSE, light3_lcolor);
  glLightfv (GL_LIGHT3, GL_POSITION, light3_position);
  
  glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_LVW);
  glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
}

short dev, val;

float current_time=0.0;
float hold_time=0.0;		/* Used when auto-running */

float tmplight[] = {
    GL_POSITION, 0.0, 0.0, 0.0, 0.0, 
};

GLfloat tv[4][4] = {
  {1.0, 0.0, 0.0, 0.0},
  {0.0, 1.0, 0.0, -1.0},
  {0.0, 0.0, 1.0, 0.0},
  {0.0, 0.0, 0.0, 0.0},
};

#define TABLERES 12

float pcr, pcg, pcb, pca;

vector table_points[TABLERES+1][TABLERES+1];
GLubyte tablecolors[TABLERES+1][TABLERES+1];

vector paper_points[4] = {
    {-0.8, 0.0, 0.4},
    {-0.2, 0.0, -1.4},
    {1.0, 0.0, -1.0},
    {0.4, 0.0, 0.8},
};

float dot(vector, vector);

#define TIME 15
#define START_TIME 0.6

vector light_pos_ctl[] = {

    {0.0, 1.8, 0.0},
    {0.0, 1.8, 0.0},
    {0.0, 1.6, 0.0},

    {0.0, 1.6, 0.0},
    {0.0, 1.6, 0.0},
    {0.0, 1.6, 0.0},
    {0.0, 1.4, 0.0},

    {0.0, 1.3, 0.0},
    {-0.2, 1.5, 2.0},
    {0.8, 1.5, -0.4},
    {-0.8, 1.5, -0.4},

    {0.8, 2.0, 1.0},
    {1.8, 5.0, -1.8},
    {8.0, 10.0, -4.0},
    {8.0, 10.0, -4.0},
    {8.0, 10.0, -4.0},
};

vector logo_pos_ctl[] = {

    {0.0, -0.5, 0.0},

    {0.0, -0.5, 0.0},
    {0.0, -0.5, 0.0},

    {0.0, -0.5, 0.0},
    {0.0, -0.5, 0.0},
    {0.0, -0.5, 0.0},
    {0.0, 0.0, 0.0},

    {0.0, 0.6, 0.0},
    {0.0, 0.75, 0.0},
    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},

    {0.0, 0.5, 0.0},
    {0.0, 0.5, 0.0},
    {0.0, 0.5, 0.0},
    {0.0, 0.5, 0.0},
    {0.0, 0.5, 0.0},
};


vector logo_rot_ctl[] = {

    {0.0, 0.0, -18.4},

    {0.0, 0.0, -18.4},
    {0.0, 0.0, -18.4},

    {0.0, 0.0, -18.4},
    {0.0, 0.0, -18.4},
    {0.0, 0.0, -18.4},
    {0.0, 0.0, -18.4},
    {0.0, 0.0, -18.4},

/*    {90.0, 0.0, -90.0},
    {180.0, 180.0, 90.0}, */
    {240.0, 360.0, 180.0},
    {90.0, 180.0, 90.0},

    {11.9, 0.0, -18.4},
    {11.9, 0.0, -18.4},
    {11.9, 0.0, -18.4},
    {11.9, 0.0, -18.4},
    {11.9, 0.0, -18.4},
};


vector view_from_ctl[] = {

    {-1.0, 1.0, -4.0},

    {-1.0, -3.0, -4.0},	/* 0 */
    {-3.0, 1.0, -3.0},	/* 1 */

    {-1.8, 2.0, 5.4},	/* 2 */
    {-0.4, 2.0, 1.2},	/* 3 */
    {-0.2, 1.5, 0.6},	/* 4 */
    {-0.2, 1.2, 0.6},	/* 5 */

    {-0.8, 1.0, 2.4},	/* 6 */
    {-1.0, 2.0, 3.0},	/* 7 */
    {0.0, 4.0, 3.6},	/* 8 */
    {-0.8, 4.0, 1.2},	/* 9 */

    {-0.2, 3.0, 0.6},	/* 10 */
    {-0.1, 2.0, 0.3},	/* 11 */
    {-0.1, 2.0, 0.3},	/* 12 */
    {-0.1, 2.0, 0.3},	/* 13 */
    {-0.1, 2.0, 0.3},	/* 13 */


};

vector view_to_ctl[] = {

    {-1.0, 1.0, 0.0},

    {-1.0, -3.0, 0.0},
    {-1.0, 1.0, 0.0},

    {0.1, 0.0, -0.3},
    {0.1, 0.0, -0.3},
    {0.1, 0.0, -0.3},
    {0.0, 0.2, 0.0},

    {0.0, 0.6, 0.0},
    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},

    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},
    {0.0, 0.8, 0.0},

};


vector view_from, view_to, logo_pos, logo_rot;
vector4 light_pos;

parameter *view_from_spline, *view_to_spline,
	  *light_pos_spline, *logo_pos_spline,
	  *logo_rot_spline;

double a3, a4;

void ideas_usage(void)
{
  fprintf(stderr, "Usage: ideas [-a] [-m] [-d] -s{1-4}\n");
  fprintf(stderr, "Press ESC to quit, 1-4 to control speed, any other key\n");
  fprintf(stderr, "to pause.\n");
}

  int auto_run;		/* If set, then automatically run forever */
  float new_speed;	/* Set new animation speed? */
  int timejerk;		/* Set to indicate time jerked! (menu pulled down) */
  int paused = 0;	/* Paused? */
  int right = 0;	/* Draw right eye? */
  int resetclock;	/* Reset the clock? */
  float timeoffset;	/* Used to compute timing */
  struct timeval start;

int main(int argc, char **argv)
{
  int i;

  glutInit(&argc, argv);

  auto_run = 0;	/* Don't automatically run forever */
  /* .4 spline distance per second by default */
  move_speed = SPEED_MEDIUM;
  new_speed = SPEED_MEDIUM;
  timeoffset = START_TIME;
  
  for (i = 1; i < argc; i++) {
    if (argv[i][0] != '-') {
      break;
    }
    
    switch(argv[i][1]) {
    case 'a':	/* Keep running forever */
      auto_run = 1;
      break;
    case 'm':	/* Multisample */
      multisample = 1;
      break;
    case 'd':	/* Single buffer */
      doublebuffer = 0;
      break;
    case 's':
      switch(argv[i][2]) {
      case '1':
	move_speed = new_speed = SPEED_SLOW;
	break;
      case '2':
	move_speed = new_speed = SPEED_MEDIUM;
	break;
      case '3':
	move_speed = new_speed = SPEED_FAST;
	break;
      case '4':
	move_speed = new_speed = SPEED_SUPER_FAST;
	break;
      }
      break;
    default:
      ideas_usage();
      break;
    }
  }
  
  initialize();
  
  current_time = timeoffset;
  resetclock = 1;
  timejerk = 0;
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}

static void idle(void) 
{
    if ((current_time) > (TIME*1.0)-3.0) {
      if (auto_run) {
	hold_time += current_time - (TIME - 3.001);
	if (hold_time > 3.0) {	/* 3 second hold */
	  hold_time = 0.0;
	  resetclock = 1;
	}
      } else {
        if(!resetclock) glutIdleFunc(NULL);
      }
      current_time = (TIME*1.0)-3.001;
    } else {
       post_idle = 1;
    }
    glutPostRedisplay();
}

/* ARGSUSED2 */
static void
mouse(int b, int s, int x, int y)
{
   if(b == GLUT_LEFT_BUTTON && s == GLUT_DOWN) {
      resetclock = 1;
      paused = 0;
      glutIdleFunc(idle);
   }
}

/* ARGSUSED1 */
static void
keyboard(unsigned char c, int x, int y)
{
   switch(c) {
   case 27:
      exit(0);
      break;
   case '1':
      new_speed = SPEED_SLOW;
      break;
   case '2':
      new_speed = SPEED_MEDIUM;
      break;
   case '3':
      new_speed = SPEED_FAST;
      break;

⌨️ 快捷键说明

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