~ubuntu-branches/ubuntu/karmic/xscreensaver/karmic

« back to all changes in this revision

Viewing changes to hacks/glx/boxed.c

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Hildebrandt
  • Date: 2006-07-29 23:00:43 UTC
  • mfrom: (1.1.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20060729230043-3ergkicskbntet5n
Tags: 4.24-5
remove /usr/doc/xscreensaver link to finish /usr/doc ->
/usr/share/doc transition (closes: #380394)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* thebox --- 3D bouncing balls that explode */
 
1
/* boxed --- 3D bouncing balls that explode */
2
2
 
3
3
#if 0
4
4
static const char sccsid[] = "@(#)boxed.c       0.9 01/09/26 xlockmore";
23
23
 *       as an OpenGL screensaver for the xscreensaver package.
24
24
 *       Lots of hardcoded values still in place. Also, there are some
25
25
 *       copy/paste leftovers from the gears hack. opts don't work.
 
26
 *
 
27
 * 2005: opts work. added options -balls, -ballsize, -explosion
 
28
 *
26
29
 */
27
30
 
28
31
#include <X11/Intrinsic.h>
42
45
# define boxed_opts     xlockmore_opts
43
46
 
44
47
# define DEF_SPEED      "0.5"
45
 
# define DEFAULTS       "*delay:   20000   \n"  \
46
 
                        "*showFPS: False   \n"  \
 
48
# define DEF_BALLS      "25"
 
49
# define DEF_BALLSIZE   "2.0"
 
50
# define DEF_EXPLOSION  "25.0f"
 
51
# define DEFAULTS       "*delay:     20000   \n" \
 
52
                        "*showFPS:   False   \n" \
 
53
                        "*wireframe: False   \n"
47
54
 
48
55
# include "xlockmore.h"         /* from the xscreensaver distribution */
49
56
#else  /* !STANDALONE */
59
66
#define rnd() (frand(1.0))
60
67
 
61
68
GLfloat speed;  /* jwz -- overall speed factor applied to all motion */
 
69
int cfg_balls;
 
70
GLfloat cfg_ballsize;
 
71
GLfloat cfg_explosion;
62
72
 
63
73
 
64
74
static XrmOptionDescRec opts[] = {
65
75
    {"-speed", ".boxed.speed", XrmoptionSepArg, 0},
 
76
    {"-balls", ".boxed.balls", XrmoptionSepArg, 0},
 
77
    {"-ballsize", ".boxed.ballsize", XrmoptionSepArg, 0},
 
78
    {"-explosion", ".boxed.explosion", XrmoptionSepArg, 0},
66
79
};
67
80
 
68
81
static argtype vars[] = {
69
82
    {&speed, "speed", "Speed", DEF_SPEED, t_Float},
 
83
    {&cfg_balls, "balls", "Balls", DEF_BALLS, t_Int},
 
84
    {&cfg_ballsize, "ballsize", "Ball Size", DEF_BALLSIZE, t_Float},
 
85
    {&cfg_explosion, "explosion", "Exlosion", DEF_BALLSIZE, t_Float},
70
86
};
71
87
 
72
88
ModeSpecOpt boxed_opts = {countof(opts), opts, countof(vars), vars, NULL};
85
101
#define TRUE 1
86
102
#define FALSE 0
87
103
 
88
 
/* rendering defines */
89
 
 
90
 
 
91
 
/* box size */
92
 
#define BOX_SIZE        20.0f
93
 
 
94
104
/* camera */
95
105
#define CAM_HEIGHT      100.0f
96
106
#define CAMDISTANCE_MIN 20.0
102
112
#define SPHERE_VERTICES (2+MESH_SIZE*MESH_SIZE*2)
103
113
#define SPHERE_INDICES  ((MESH_SIZE*4 + MESH_SIZE*4*(MESH_SIZE-1))*3)
104
114
 
105
 
#define EXPLOSION 10.0f
106
 
#define MAXBALLS  50;
107
 
#define NUMBALLS 12;
108
 
#define BALLSIZE 3.0f;
109
 
 
110
115
/*
111
116
**-----------------------------------------------------------------------------
112
117
**      Typedefs
168
173
   float          cam_x_speed, cam_z_speed, cam_y_speed;
169
174
   boxed_config  config;
170
175
   float          tic;
 
176
   float          camtic;
171
177
   vectorf        spherev[SPHERE_VERTICES];
172
178
   GLint          spherei[SPHERE_INDICES];
173
179
   ballman        bman;
351
357
   newball->dir.z = (0.5-rnd())  * speed;
352
358
   newball->offside = 0;
353
359
   newball->bounced = FALSE;
354
 
   newball->radius = BALLSIZE;
355
 
   while (r+g+b < 1.7f ) {
 
360
   newball->radius = cfg_ballsize;
 
361
   while (r+g+b < 1.8f ) {
356
362
      newball->color.x = r=rnd();
357
363
      newball->color.y = g=rnd();
358
364
      newball->color.z = b=rnd();
370
376
 
371
377
   for (b=0;b<bman->num_balls;b++) {
372
378
 
373
 
     GLfloat gravity = 0.15f * speed;
 
379
     GLfloat gravity = 0.30f * speed;
374
380
 
375
381
      /* apply gravity */
376
382
      bman->balls[b].dir.y -= gravity;
378
384
      addvectors(&bman->balls[b].loc,&bman->balls[b].loc,&bman->balls[b].dir);
379
385
      /* boundary check */
380
386
      if (bman->balls[b].loc.y < bman->balls[b].radius) { /* ball onder bodem? (bodem @ y=0) */
381
 
         if ((bman->balls[b].loc.x < -100.0) || 
382
 
             (bman->balls[b].loc.x > 100.0) ||
383
 
             (bman->balls[b].loc.z < -100.0) ||
384
 
             (bman->balls[b].loc.z > 100.0)) {
 
387
         if ((bman->balls[b].loc.x < -95.0) || 
 
388
             (bman->balls[b].loc.x > 95.0) ||
 
389
             (bman->balls[b].loc.z < -95.0) ||
 
390
             (bman->balls[b].loc.z > 95.0)) {
385
391
            if (bman->balls[b].loc.y < -1000.0)
386
392
              createball(&bman->balls[b]);
387
393
         } else {
512
518
      scalevector(&tman->vertices[pos+1],&tman->vertices[pos+1],scale);
513
519
      scalevector(&tman->vertices[pos+2],&tman->vertices[pos+2],scale);
514
520
            
 
521
      tman->vertices[pos+0].x += avgdir.x;
 
522
      tman->vertices[pos+0].y += avgdir.y;
 
523
      tman->vertices[pos+0].z += avgdir.z;
 
524
      tman->vertices[pos+1].x += avgdir.x;
 
525
      tman->vertices[pos+1].y += avgdir.y;
 
526
      tman->vertices[pos+1].z += avgdir.z;
 
527
      tman->vertices[pos+2].x += avgdir.x;
 
528
      tman->vertices[pos+2].y += avgdir.y;
 
529
      tman->vertices[pos+2].z += avgdir.z;
 
530
 
515
531
      /* bereken nieuwe richting */
516
532
      scalevector(&tman->tris[i].dir,&avgdir,explosion);
517
533
      dvect.x = (0.1f - 0.2f*rnd());
539
555
      /* boundary check */
540
556
      if (t->tris[b].far) continue;
541
557
      if (t->tris[b].loc.y < 0) { /* onder bodem ? */
542
 
         if ((t->tris[b].loc.x > -100.0f) &
543
 
             (t->tris[b].loc.x < 100.0f) &
544
 
             (t->tris[b].loc.z > -100.0f) &
545
 
             (t->tris[b].loc.z < 100.0f)) {  /* in veld  */
 
558
         if ((t->tris[b].loc.x > -95.0f) &
 
559
             (t->tris[b].loc.x < 95.0f) &
 
560
             (t->tris[b].loc.z > -95.0f) &
 
561
             (t->tris[b].loc.z < 95.0f)) {  /* in veld  */
546
562
            t->tris[b].dir.y = -(t->tris[b].dir.y);
547
563
            t->tris[b].loc.y = -t->tris[b].loc.y;
548
564
            scalevector(&t->tris[b].dir,&t->tris[b].dir,0.80f); /* dampening */
618
634
 */
619
635
void setdefaultconfig(boxed_config *config) 
620
636
{
621
 
  config->numballs = NUMBALLS;
 
637
  cfg_balls = MAX(3,MIN(40,cfg_balls));
 
638
  cfg_ballsize = MAX(1.0f,MIN(5.0f,cfg_ballsize));
 
639
  cfg_explosion = MAX(0.0f,MIN(50.0f,cfg_explosion));
 
640
  config->numballs = cfg_balls;
622
641
  config->textures = TRUE;
623
642
  config->transparent = FALSE;
624
 
  config->explosion = 25.0f;
625
 
  config->ballsize = BALLSIZE;
 
643
  config->explosion = cfg_explosion;
 
644
  config->ballsize = cfg_ballsize;
626
645
  config->camspeed = 35.0f;
627
646
}
628
647
 
630
649
/*
631
650
 * draw bottom
632
651
 */ 
633
 
static void drawfilledbox(boxedstruct *boxed)
 
652
static void drawfilledbox(boxedstruct *boxed, int wire)
634
653
{   
635
654
   /* draws texture filled box, 
636
655
      top is drawn using the entire texture, 
638
657
    */
639
658
   
640
659
   /* front */
641
 
   glBegin(GL_QUADS);
 
660
   glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
642
661
   glTexCoord2f(0,1);
643
662
   glVertex3f(-1.0,1.0,1.0);
644
663
   glTexCoord2f(1,1);
735
754
/* 
736
755
 * Draw ball
737
756
 */
738
 
static void drawball(boxedstruct *gp, ball *b)
 
757
static void drawball(boxedstruct *gp, ball *b, int wire)
739
758
{
740
759
   int i,pos,cnt;
741
760
   GLint *spherei = gp->spherei;
761
780
      cnt = SPHERE_INDICES/3;
762
781
      for (i=0; i<cnt; i++) {
763
782
         pos = i * 3;
764
 
         glBegin(GL_TRIANGLES);
 
783
         glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
765
784
         glNormal3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
766
785
         glVertex3f(spherev[spherei[pos+0]].x,spherev[spherei[pos+0]].y,spherev[spherei[pos+0]].z);
767
786
         glNormal3f(spherev[spherei[pos+1]].x,spherev[spherei[pos+1]].y,spherev[spherei[pos+1]].z);
783
802
/* 
784
803
 * Draw all triangles in triman
785
804
 */
786
 
static void drawtriman(triman *t) 
 
805
static void drawtriman(triman *t, int wire) 
787
806
{
788
807
   int i,pos;
789
808
   vectorf *spherev = t->vertices;
804
823
      pos = i*3;
805
824
      glPushMatrix();
806
825
      glTranslatef(t->tris[i].loc.x,t->tris[i].loc.y,t->tris[i].loc.z);
807
 
      glBegin(GL_TRIANGLES);
 
826
      glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLES);
808
827
      glNormal3f(t->normals[i].x,t->normals[i].y,t->normals[i].z);
809
828
      glVertex3f(spherev[pos+0].x,spherev[pos+0].y,spherev[pos+0].z);
810
829
      glVertex3f(spherev[pos+1].x,spherev[pos+1].y,spherev[pos+1].z);
878
897
static void draw(ModeInfo * mi)
879
898
{
880
899
   boxedstruct *gp = &boxed[MI_SCREEN(mi)];
 
900
   int wire = MI_IS_WIREFRAME (mi);
881
901
   vectorf v1;
882
902
   GLfloat dcam;
883
903
   int dx, dz;
900
920
   glLoadIdentity();
901
921
   
902
922
   gp->tic += 0.01f;
903
 
 
 
923
   gp->camtic += 0.01f + 0.01f * sin(gp->tic * speed);
 
924
   
904
925
   /* rotate camera around (0,0,0), looking at (0,0,0), up is (0,1,0) */
905
 
   dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->tic/CAMDISTANCE_SPEED) * speed);
906
 
   v1.x = dcam * sin((gp->tic/gp->cam_x_speed) * speed);
907
 
   v1.z = dcam * cos((gp->tic/gp->cam_z_speed) * speed);
908
 
   v1.y = CAM_HEIGHT * sin((gp->tic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
 
926
   dcam = CAMDISTANCE_MIN + (CAMDISTANCE_MAX - CAMDISTANCE_MIN) + (CAMDISTANCE_MAX - CAMDISTANCE_MIN)*cos((gp->camtic/CAMDISTANCE_SPEED) * speed);
 
927
   v1.x = dcam * sin((gp->camtic/gp->cam_x_speed) * speed);
 
928
   v1.z = dcam * cos((gp->camtic/gp->cam_z_speed) * speed);
 
929
   v1.y = CAM_HEIGHT * sin((gp->camtic/gp->cam_y_speed) * speed) + 1.02 * CAM_HEIGHT;
909
930
   gluLookAt(v1.x,v1.y,v1.z,0.0,0.0,0.0,0.0,1.0,0.0); 
910
931
 
911
 
   glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient); 
912
 
   glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse); 
913
 
   glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular); 
914
 
   glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
915
 
   glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient); 
916
 
   glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse); 
917
 
   glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular); 
918
 
   glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
919
 
   glEnable(GL_LIGHT0);
920
 
   glEnable(GL_LIGHT1);
921
 
   
922
 
   glFrontFace(GL_CW);
923
 
   
924
 
   glMaterialfv(GL_FRONT, GL_SPECULAR, black);
925
 
   glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
926
 
   glMaterialfv(GL_FRONT,GL_AMBIENT,black);
927
 
   glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
 
932
   if (!wire) {
 
933
     glLightfv(GL_LIGHT0, GL_AMBIENT, l0_ambient); 
 
934
     glLightfv(GL_LIGHT0, GL_DIFFUSE, l0_diffuse); 
 
935
     glLightfv(GL_LIGHT0, GL_SPECULAR, l0_specular); 
 
936
     glLightfv(GL_LIGHT0, GL_POSITION, l0_position);
 
937
     glLightfv(GL_LIGHT1, GL_AMBIENT, l1_ambient); 
 
938
     glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diffuse); 
 
939
     glLightfv(GL_LIGHT1, GL_SPECULAR, l1_specular); 
 
940
     glLightfv(GL_LIGHT1, GL_POSITION, l1_position);
 
941
     glEnable(GL_LIGHT0);
 
942
     glEnable(GL_LIGHT1);
 
943
   
 
944
     glFrontFace(GL_CW);
 
945
   
 
946
     glMaterialfv(GL_FRONT, GL_SPECULAR, black);
 
947
     glMaterialfv(GL_FRONT, GL_EMISSION, lblue);
 
948
     glMaterialfv(GL_FRONT,GL_AMBIENT,black);
 
949
     glMaterialf(GL_FRONT, GL_SHININESS, 5.0);
 
950
   }
928
951
   
929
952
   
930
953
   /* draw ground grid */
943
966
   
944
967
   /* Set drawing mode for the boxes */
945
968
   glEnable(GL_DEPTH_TEST);
946
 
   glEnable(GL_TEXTURE_2D);
 
969
   if (!wire) glEnable(GL_TEXTURE_2D);
947
970
   glPushMatrix();
948
971
   glColor3f(1.0,1.0,1.0);
949
972
   glScalef(20.0,0.25,20.0);
950
973
   glTranslatef(0.0,2.0,0.0);
951
 
   drawfilledbox(gp);
 
974
   drawfilledbox(gp, wire);
952
975
   glPopMatrix();
953
976
   glDisable(GL_TEXTURE_2D);
954
977
 
980
1003
   drawbox(gp);
981
1004
   glPopMatrix();
982
1005
 
983
 
   glEnable(GL_LIGHTING);
 
1006
   if (!wire) {
 
1007
     glEnable(GL_LIGHTING);
984
1008
   
985
 
   glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
986
 
   glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
 
1009
     glMaterialfv(GL_FRONT, GL_DIFFUSE, dgray);
 
1010
     glMaterialfv(GL_FRONT, GL_EMISSION, black); /* turn it off before painting the balls */
 
1011
   }
987
1012
 
988
1013
   /* move the balls and shrapnel */
989
1014
   updateballs(&gp->bman);
1001
1026
            updatetris(&gp->tman[i]);
1002
1027
         }
1003
1028
         glDisable(GL_CULL_FACE);
1004
 
         drawtriman(&gp->tman[i]);
1005
 
         glEnable(GL_CULL_FACE);
 
1029
         drawtriman(&gp->tman[i], wire);
 
1030
         if (!wire) glEnable(GL_CULL_FACE);
1006
1031
      } else {
1007
 
         drawball(gp, &gp->bman.balls[i]);
 
1032
         drawball(gp, &gp->bman.balls[i], wire);
1008
1033
      }
1009
1034
   }
1010
1035
      
1035
1060
pinit(ModeInfo * mi)
1036
1061
{
1037
1062
   boxedstruct *gp = &boxed[MI_SCREEN(mi)];
 
1063
   int wire = MI_IS_WIREFRAME (mi);
1038
1064
   ballman *bman;
1039
1065
   int i,texpixels;
1040
1066
   char *texpixeldata;
1069
1095
 
1070
1096
   generatesphere();
1071
1097
   
1072
 
   glEnable(GL_CULL_FACE);
1073
 
   glEnable(GL_LIGHTING);
 
1098
   if (!wire) {
 
1099
     glEnable(GL_CULL_FACE);
 
1100
     glEnable(GL_LIGHTING);
 
1101
   }
1074
1102
 
1075
1103
   /* define cam path */
1076
1104
   gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0));
1078
1106
   gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0));
1079
1107
   if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed;
1080
1108
   if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed;
1081
 
   
1082
 
   
 
1109
 
 
1110
   /* define initial cam position */
 
1111
   gp->tic = gp->camtic = rnd() * 100.0f;
 
1112
   
 
1113
   /* define tex1 (bottom plate) */
1083
1114
   gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint));
1084
1115
   texpixels = 256*256; /*width*height;*/
1085
1116
   texpixeldata = header_data;