1 /* -*- Mode: C; tab-width: 2 -*- */
2 /* glcells --- Cells growing on your screen */
5 * Cells growing on your screen
7 * Copyright (c) 2007 by Matthias Toussaint
9 * Permission to use, copy, modify, and distribute this software and its
10 * documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and that
12 * both that copyright notice and this permission notice appear in
13 * supporting documentation.
15 * This file is provided AS IS with no warranties of any kind. The author
16 * shall have no liability with respect to the infringement of copyrights,
17 * trade secrets or any patents by this file or any part thereof. In no
18 * event will the author be liable for any lost revenue or profits or
19 * other special, indirect and consequential damages.
21 * 2007: Written by Matthias Toussaint
23 * 0.2 Bugfixes (threading) and code cleanup by Jamie Zawinski
24 * Window scaling bug + performance bug in tick()
27 #include <sys/time.h> /* gettimeofday */
29 #include "xlockmore.h"
32 /**********************************
34 **********************************/
36 #define INDEX_OFFSET 100000
37 #define NUM_CELL_SHAPES 10
39 #define refresh_glcells 0
40 #define glcells_handle_event 0
42 #define DEF_DELAY "20000"
43 #define DEF_MAXCELLS "800"
44 #define DEF_RADIUS "40"
46 #define DEF_QUALITY "3"
47 #define DEF_KEEPOLD "False"
48 #define DEF_MINFOOD "5"
49 #define DEF_MAXFOOD "20"
50 #define DEF_DIVIDEAGE "20"
51 #define DEF_MINDIST "1.4"
52 #define DEF_PAUSE "50"
54 #define DEFAULTS "*delay: 30000 \n" \
55 "*showFPS: False \n" \
56 "*wireframe: False \n" \
59 #define countof(x) (sizeof((x))/sizeof((*x)))
61 #ifndef HAVE_JWZGLES /* glDrawElements unimplemented... */
62 # define USE_VERTEX_ARRAY
67 /**********************************
69 **********************************/
71 typedef struct /* a 3-D vector */
73 double x, y, z; /* 3-D coordinates (we don't need w here) */
76 typedef struct /* a triangle (indexes of vertexes in some list) */
78 int i[3]; /* the three indexes for the triangle corners */
89 typedef struct /* an 3-D object without normal vectors */
91 Vector *vertex; /* the vertexes */
92 Triangle *triangle; /* triangle list */
93 int num_vertex; /* number of vertexes */
94 int num_triangle; /* number of triangles */
97 typedef struct /* an 3-D object with smooth normal vectors */
99 Vector *vertex; /* the vertexes */
100 Vector *normal; /* the vertex normal vectors */
101 Triangle *triangle; /* triangle list */
102 int num_vertex; /* number of vertexes */
103 int num_triangle; /* number of triangles */
106 typedef struct /* Cell */
108 double x, y; /* position */
109 double vx, vy; /* movement vector */
110 int age; /* cells age */
111 double min_dist; /* minimum distance to other cells */
112 int energy; /* health */
113 double rotation; /* random rot, so they don't look all the same */
114 double radius; /* current size of cell */
115 double growth; /* current growth rate. might be <1.0 while dividing,
116 >1.0 when finished dividing and food is available
117 and 1.0 when grown up */
120 typedef struct /* hacks state */
122 GLXContext *glx_context;
123 int width, height; /* current size of viewport */
124 double screen_scale; /* we scale content with window size */
125 int num_cells; /* current number of cell in list */
126 Cell *cell; /* array of cells */
128 GLfloat color[4]; /* current cell color */
129 double radius; /* cell radius */
130 int move_dist; /* min distance from neighbours for forking */
131 int max_cells; /* maximum number of cells */
132 int num_seeds; /* number of initial seeds */
133 int keep_old_cells; /* draw dead cells? */
134 int divide_age; /* min age for division */
135 /* display lists for the cell stages */
136 int cell_list[NUM_CELL_SHAPES];
138 int minfood; /* minimum amount of food per area unit */
139 int maxfood; /* maximum amount of food per area unit */
140 int pause; /* pause at end (all cells dead) */
142 int wire; /* draw wireframe? */
143 Object *sphere; /* the raw undisturbed sphere */
144 double *disturbance; /* disturbance values for the vertexes */
145 int *food; /* our petri dish (e.g. screen) */
146 GLubyte *texture; /* texture data for nucleus */
147 GLuint texture_name; /* texture name for binding */
150 /**********************************
152 **********************************/
154 static State *sstate = NULL;
156 static XrmOptionDescRec opts[] = {
157 { "-maxcells", ".maxcells", XrmoptionSepArg, 0 },
158 { "-radius", ".radius", XrmoptionSepArg, 0 },
159 { "-seeds", ".seeds", XrmoptionSepArg, 0 },
160 { "-quality", ".quality", XrmoptionSepArg, 0 },
161 { "-minfood", ".minfood", XrmoptionSepArg, 0 },
162 { "-maxfood", ".maxfood", XrmoptionSepArg, 0 },
163 { "-divideage", ".divideage", XrmoptionSepArg, 0 },
164 { "-mindist", ".mindist", XrmoptionSepArg, 0 },
165 { "-pause", ".pause", XrmoptionSepArg, 0 },
166 { "-keepold", ".keepold", XrmoptionNoArg, "True" }
169 static int s_maxcells;
172 static int s_quality;
173 static int s_minfood;
174 static int s_maxfood;
175 static int s_divideage;
177 static float s_min_dist;
178 static Bool s_keepold;
180 static argtype vars[] = {
181 {&s_maxcells, "maxcells", "Max Cells", DEF_MAXCELLS, t_Int},
182 {&s_radius, "radius", "Radius", DEF_RADIUS, t_Int},
183 {&s_seeds, "seeds", "Seeds", DEF_SEEDS, t_Int},
184 {&s_quality, "quality", "Quality", DEF_QUALITY, t_Int},
185 {&s_minfood, "minfood", "Min Food", DEF_MINFOOD, t_Int},
186 {&s_maxfood, "maxfood", "Max Food", DEF_MAXFOOD, t_Int},
187 {&s_pause, "pause", "Pause at end", DEF_PAUSE, t_Int},
188 {&s_divideage, "divideage", "Age for duplication (Ticks)", DEF_DIVIDEAGE, t_Int},
189 {&s_min_dist, "mindist", "Minimum preferred distance to other cells", DEF_MINDIST, t_Float},
190 {&s_keepold, "keepold", "Keep old cells", DEF_KEEPOLD, t_Bool}
193 /**********************************
195 **********************************/
198 static int render( State *st );
199 /* create initial cells and fill petri dish with food */
200 static void create_cells( State * );
201 /* do one animation step */
202 static void tick( State *st );
203 /* draw a single cell */
204 static void draw_cell( State *st, int shape );
205 /* draw cells nucleus */
206 static void draw_nucleus( State *st );
207 /* return randum number in the interval min-max */
208 static int random_interval( int min, int max );
209 /* retunr random number in the interval 0-max */
210 static int random_max( int max );
211 /* create display list for given disturbance weighting factor */
212 static int create_list( State *st, double fac );
213 /* return length of vector */
214 static double vector_length( Vector * );
215 /* normalize vector */
216 static void vector_normalize( Vector * );
218 static void vector_add( Vector *a, Vector *b );
220 static void vector_sub( Vector *a, Vector *b );
222 static void vector_mul( Vector *a, double fac );
223 /* a.x = a.y = a.z = 0 */
224 static void vector_clear( Vector *a );
225 /* return crossproduct a*b in out */
226 static void vector_crossprod( Vector *a, Vector *b, Vector *out );
227 /* return 1 if vectors are equal (epsilon compare) otherwise 0 */
228 static int vector_compare( Vector *a, Vector *b );
229 /* compute normal vector of given triangle and return in out */
230 static void triangle_normal( Vector *a, Vector *b, Vector *c, Vector *out );
231 /* take an Object and create an ObjectSmooth out of it */
232 static ObjectSmooth *create_ObjectSmooth( Object * );
233 /* Subdivide the Object once (assuming it's supposed to be a shpere */
234 static Object *subdivide( Object *obj );
236 static void free_Object( Object * );
237 /* free an ObjectSmooth */
238 static void free_ObjectSmooth( ObjectSmooth * );
239 /* scale an Object. return pointer to the object */
240 /*static Object *scale_Object( Object *obj, double scale );*/
241 /* create a perfect sphere refining with divisions */
242 static Object *create_sphere( State *st, int divisions );
243 /* make a copy of the given Object */
244 static Object *clone_Object( Object * );
245 /* return 1 if cell is capable to divide */
246 static int can_divide( State *st, Cell *cell );
247 #ifdef USE_VERTEX_ARRAY
248 static VertexArray *array_from_ObjectSmooth( ObjectSmooth * );
250 static void create_nucleus_texture( State *st );
252 ENTRYPOINT ModeSpecOpt glcells_opts = { countof(opts), opts, countof(vars), vars,
256 /**********************************
258 **********************************/
259 /* create random numbers
261 static inline int random_interval( int min, int max )
265 return min+(random()%n);
268 static inline int random_max( int max )
277 static inline void vector_add( Vector *a, Vector *b )
285 static inline void vector_sub( Vector *a, Vector *b )
293 static inline void vector_mul( Vector *a, double v )
301 static inline void vector_clear( Vector *vec )
303 vec->x = vec->y = vec->z = 0;
306 /* return vector length */
307 static inline double vector_length( Vector *vec )
309 return sqrt( vec->x*vec->x + vec->y*vec->y + vec->z*vec->z );
312 /* normalize vector */
313 static inline void vector_normalize( Vector *vec )
315 double len = vector_length( vec );
318 vector_mul( vec, 1.0 / len );
323 static inline void vector_crossprod( Vector *a, Vector *b, Vector *out )
325 out->x = a->y*b->z - a->z*b->y;
326 out->y = a->z*b->x - a->x*b->z;
327 out->z = a->x*b->y - a->y*b->x;
330 /* epsilon compare of two vectors */
331 static inline int vector_compare( Vector *a, Vector *b )
333 const double epsilon = 0.0000001;
336 vector_sub( &delta, b );
337 if (fabs(delta.x) < epsilon &&
338 fabs(delta.y) < epsilon &&
339 fabs(delta.z) < epsilon) {
346 /* check if given cell is capable of dividing
347 needs space, must be old enough, grown up and healthy
349 static inline int can_divide( State *st, Cell *cell )
351 if (cell->min_dist > st->move_dist &&
352 cell->age >= st->divide_age &&
353 cell->radius > 0.99 * st->radius &&
361 /**********************************
363 **********************************/
365 /* compute normal vector of given
366 triangle spanned by the points a, b, c
368 static void triangle_normal( Vector *a, Vector *b, Vector *c, Vector *out )
373 vector_sub( &v1, b );
374 vector_sub( &v2, c );
375 vector_crossprod( &v1, &v2, out );
379 static void free_Object( Object *obj )
382 free( obj->triangle );
386 static void free_ObjectSmooth( ObjectSmooth *obj )
389 free( obj->triangle );
394 /* scale the given Object */
396 static Object *scale_Object( Object *obj, double scale )
400 for (v=0; v<obj->num_vertex; ++v) {
401 vector_mul( &obj->vertex[v], scale );
408 /* create a copy of the given Object */
409 static Object *clone_Object( Object *obj )
412 Object *ret = (Object *) malloc( sizeof( Object ) );
415 (Vector *) malloc( obj->num_vertex*sizeof(Vector) );
417 (Triangle *) malloc( obj->num_triangle*sizeof(Triangle) );
418 ret->num_vertex = obj->num_vertex;
419 ret->num_triangle = obj->num_triangle;
421 memcpy( ret->vertex, obj->vertex,
422 obj->num_vertex*sizeof(Vector) );
423 memcpy( ret->triangle, obj->triangle,
424 obj->num_triangle*sizeof(Triangle) );
429 #ifdef USE_VERTEX_ARRAY
430 static VertexArray *array_from_ObjectSmooth( ObjectSmooth *obj )
433 VertexArray *array = (VertexArray *) malloc( sizeof( VertexArray ) );
435 array->vertex = (float *) malloc( 3*sizeof(float)*obj->num_vertex );
436 array->normal = (float *) malloc( 3*sizeof(float)*obj->num_vertex );
437 array->index = (unsigned *) malloc( 3*sizeof(unsigned)*obj->num_triangle );
438 array->num_index = obj->num_triangle*3;
440 for (i=0, j=0; i<obj->num_vertex; ++i) {
441 array->vertex[j] = obj->vertex[i].x;
442 array->normal[j++] = obj->normal[i].x;
443 array->vertex[j] = obj->vertex[i].y;
444 array->normal[j++] = obj->normal[i].y;
445 array->vertex[j] = obj->vertex[i].z;
446 array->normal[j++] = obj->normal[i].z;
449 for (i=0, j=0; i<obj->num_triangle; ++i) {
450 array->index[j++] = obj->triangle[i].i[0];
451 array->index[j++] = obj->triangle[i].i[1];
452 array->index[j++] = obj->triangle[i].i[2];
457 #endif /* USE_VERTEX_ARRAY */
460 /* create a smoothed version of the given Object
461 by computing average normal vectors for the vertexes
463 static ObjectSmooth *create_ObjectSmooth( Object *obj )
467 (Vector *) malloc( obj->num_triangle*sizeof(Vector) );
469 (ObjectSmooth *) malloc( sizeof( ObjectSmooth ) );
471 /* fill in vertexes and triangles */
472 ret->num_vertex = obj->num_vertex;
473 ret->num_triangle = obj->num_triangle;
475 (Vector *) malloc( obj->num_vertex * sizeof( Vector ) );
477 (Vector *) malloc( obj->num_vertex * sizeof( Vector ) );
479 (Triangle *) malloc( obj->num_triangle * sizeof( Triangle ) );
481 for (v=0; v<obj->num_vertex; ++v) {
482 ret->vertex[v] = obj->vertex[v];
485 for (t=0; t<obj->num_triangle; ++t) {
486 ret->triangle[t] = obj->triangle[t];
489 /* create normals (triangles) */
490 for (t=0; t<ret->num_triangle; ++t) {
491 triangle_normal( &ret->vertex[ret->triangle[t].i[0]],
492 &ret->vertex[ret->triangle[t].i[1]],
493 &ret->vertex[ret->triangle[t].i[2]],
497 /* create normals (vertex) by averaging triangle
500 for (v=0; v<ret->num_vertex; ++v) {
501 vector_clear( &ret->normal[v] );
502 for (t=0; t<ret->num_triangle; ++t) {
503 for (i=0; i<3; ++i) {
504 if (ret->triangle[t].i[i] == v) {
505 vector_add( &ret->normal[v], &t_normal[t] );
509 /* as we have only a half sphere we force the
510 normals at the bortder to be perpendicular to z.
511 the simple algorithm above makes an error here.
513 if (fabs(ret->vertex[v].z) < 0.0001) {
514 ret->normal[v].z = 0.0;
517 vector_normalize( &ret->normal[v] );
525 /* subdivide the triangles of the object once
526 The order of this algorithm is probably something like O(n^42) :)
527 but I can't think of something smarter at the moment
529 static Object *subdivide( Object *obj )
531 /* create for worst case (which I dont't know) */
533 int index_list[1000];
534 int index_cnt, index_found;
535 Object *tmp = (Object *)malloc( sizeof(Object) );
536 Object *ret = (Object *)malloc( sizeof(Object) );
540 (Vector *)malloc( 100*obj->num_vertex*sizeof( Vector ) );
542 (Triangle *)malloc( 4*obj->num_triangle*sizeof( Triangle ) );
544 tmp->num_triangle = 0;
546 (Vector *)malloc( 100*obj->num_vertex*sizeof( Vector ) );
548 (Triangle *)malloc( 4*obj->num_triangle*sizeof( Triangle ) );
550 ret->num_triangle = 0;
552 fprintf( stderr, "in v=%d t=%d\n",
553 obj->num_vertex, obj->num_triangle );
555 /* for each triangle create 3 new vertexes and the 4
556 corresponding triangles
558 for (t=0; t<obj->num_triangle; ++t) {
559 /* copy the three original vertexes */
560 for (i=0; i<3; ++i) {
561 tmp->vertex[tmp->num_vertex++] =
562 obj->vertex[obj->triangle[t].i[i]];
566 tmp->vertex[tmp->num_vertex] =
567 obj->vertex[obj->triangle[t].i[0]];
568 vector_add( &tmp->vertex[tmp->num_vertex],
569 &obj->vertex[obj->triangle[t].i[1]] );
570 vector_mul( &tmp->vertex[tmp->num_vertex++], 0.5 );
572 tmp->vertex[tmp->num_vertex] =
573 obj->vertex[obj->triangle[t].i[1]];
574 vector_add( &tmp->vertex[tmp->num_vertex],
575 &obj->vertex[obj->triangle[t].i[2]] );
576 vector_mul( &tmp->vertex[tmp->num_vertex++], 0.5 );
578 tmp->vertex[tmp->num_vertex] =
579 obj->vertex[obj->triangle[t].i[2]];
580 vector_add( &tmp->vertex[tmp->num_vertex],
581 &obj->vertex[obj->triangle[t].i[0]] );
582 vector_mul( &tmp->vertex[tmp->num_vertex++], 0.5 );
584 /* create triangles */
585 start = tmp->num_vertex-6;
587 tmp->triangle[tmp->num_triangle].i[0] = start;
588 tmp->triangle[tmp->num_triangle].i[1] = start+3;
589 tmp->triangle[tmp->num_triangle++].i[2] = start+5;
591 tmp->triangle[tmp->num_triangle].i[0] = start+3;
592 tmp->triangle[tmp->num_triangle].i[1] = start+1;
593 tmp->triangle[tmp->num_triangle++].i[2] = start+4;
595 tmp->triangle[tmp->num_triangle].i[0] = start+5;
596 tmp->triangle[tmp->num_triangle].i[1] = start+4;
597 tmp->triangle[tmp->num_triangle++].i[2] = start+2;
599 tmp->triangle[tmp->num_triangle].i[0] = start+3;
600 tmp->triangle[tmp->num_triangle].i[1] = start+4;
601 tmp->triangle[tmp->num_triangle++].i[2] = start+5;
604 /* compress object eliminating double vertexes
605 (welcome to the not so smart section)
607 /* copy original triangle list */
608 for (t=0; t<tmp->num_triangle; ++t) {
609 ret->triangle[t] = tmp->triangle[t];
611 ret->num_triangle = tmp->num_triangle;
613 /* copy unique vertexes and correct triangle list */
614 for (v=0; v<tmp->num_vertex; ++v) {
615 /* create list of vertexes that are the same */
617 for (i=0; i<tmp->num_vertex; ++i) {
618 /* check if i and v are the same
619 first in the list is the smallest index
621 if (vector_compare( &tmp->vertex[v], &tmp->vertex[i] )) {
622 index_list[index_cnt++] = i;
626 /* check if vertex unknown so far */
628 for (i=0; i<ret->num_vertex; ++i) {
629 if (vector_compare( &ret->vertex[i],
630 &tmp->vertex[index_list[0]] )) {
637 ret->vertex[ret->num_vertex] = tmp->vertex[index_list[0]];
640 (we add an offset to the index, so we can tell them apart)
642 for (t=0; t<ret->num_triangle; ++t) {
643 for (i=0; i<index_cnt; ++i) {
644 if (ret->triangle[t].i[0] == index_list[i]) {
645 ret->triangle[t].i[0] = ret->num_vertex+INDEX_OFFSET;
647 if (ret->triangle[t].i[1] == index_list[i]) {
648 ret->triangle[t].i[1] = ret->num_vertex+INDEX_OFFSET;
650 if (ret->triangle[t].i[2] == index_list[i]) {
651 ret->triangle[t].i[2] = ret->num_vertex+INDEX_OFFSET;
661 /* correct index offset */
662 for (t=0; t<ret->num_triangle; ++t) {
663 ret->triangle[t].i[0] -= INDEX_OFFSET;
664 ret->triangle[t].i[1] -= INDEX_OFFSET;
665 ret->triangle[t].i[2] -= INDEX_OFFSET;
668 /* normalize vertexes */
669 for (v=0; v<ret->num_vertex; ++v) {
670 vector_normalize( &ret->vertex[v] );
673 fprintf( stderr, "out v=%d t=%d\n",
674 ret->num_vertex, ret->num_triangle );
676 /* shrink the arrays by cloning */
677 c_ret = clone_Object( ret );
683 static int render( State *st )
686 struct timeval tv1, tv2;
689 GLfloat LightAmbient[]= { 0.1f, 0.1f, 0.1f, 1.0f };
690 GLfloat LightPosition[]= { -20.0f, -10.0f, -100.0f, 0.0f };
694 if (0 == st->food) return 0;
696 gettimeofday( &tv1, NULL );
698 /* life goes on... */
701 gettimeofday( &tv2, NULL );
702 usec = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec);
703 fprintf( stderr, "tick %d\n", usec );
704 gettimeofday( &tv1, NULL );
707 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
708 glDepthFunc(GL_LESS);
709 glEnable(GL_DEPTH_TEST);
710 glLightfv( GL_LIGHT0, GL_AMBIENT, LightAmbient );
711 glLightfv( GL_LIGHT0, GL_DIFFUSE, st->color );
712 glLightfv( GL_LIGHT0, GL_POSITION, LightPosition );
714 /* prepare lighting vs. wireframe */
716 glEnable( GL_LIGHT0 );
717 glEnable( GL_LIGHTING );
718 glEnable( GL_NORMALIZE );
719 glPolygonMode( GL_FRONT, GL_FILL );
721 glPolygonMode( GL_FRONT, GL_LINE );
724 /* draw the dead cells if choosen */
725 if (st->keep_old_cells) {
726 for (b=0; b<st->num_cells; ++b) {
727 if (st->cell[b].energy <= 0) {
730 glTranslatef( st->cell[b].x, st->cell[b].y, 0.0 );
731 glRotatef( st->cell[b].rotation, 0.0, 0.0, 1.0 );
732 glScalef( st->cell[b].radius, st->cell[b].radius, st->cell[b].radius );
739 /* draw the living cells */
740 for (b=0; b<st->num_cells; ++b) {
741 if (st->cell[b].energy >0) {
742 double fac = (double)st->cell[b].energy / 50.0;
744 if (fac < 0.0) fac = 0.0;
745 if (fac > 1.0) fac = 1.0;
747 shape = (int)(9.0*fac);
749 /*glColor3f( fac, fac, fac );*/
752 glTranslatef( st->cell[b].x, st->cell[b].y, 0.0 );
753 glRotatef( st->cell[b].rotation, 0.0, 0.0, 1.0 );
754 glScalef( st->cell[b].radius, st->cell[b].radius, st->cell[b].radius );
755 draw_cell( st, 9-shape );
760 /* draw cell nuclei */
763 glDisable( GL_LIGHT0 );
764 glDisable( GL_LIGHTING );
766 glEnable( GL_BLEND );
767 glDisable( GL_DEPTH_TEST );
768 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
769 glEnable( GL_TEXTURE_2D );
770 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
771 glBindTexture( GL_TEXTURE_2D, st->texture_name );
773 for (b=0; b<st->num_cells; ++b) {
774 if (st->cell[b].energy>0 || st->keep_old_cells) {
776 glTranslatef( st->cell[b].x, st->cell[b].y, 0.0 );
777 glScalef( st->cell[b].radius, st->cell[b].radius, st->cell[b].radius );
783 glDisable( GL_TEXTURE_2D );
784 glDisable( GL_BLEND );
788 gettimeofday( &tv2, NULL );
789 usec = (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec);
790 fprintf( stderr, "OpenGL %d\n", usec );
792 return num_paint * st->cell_polys;
795 /* this creates the initial subdivided half-dodecaedron */
796 static Object *create_sphere( State *st, int divisions )
799 int num_triangle = 10;
801 double a, aStep = (double)M_PI / 3.0;
803 int vi[30] = { 0, 7, 1, 1, 7, 2, 2, 8, 3, 3, 8, 4, 4, 6, 5,
804 5, 6, 0, 0, 6, 7, 2, 7, 8, 4, 8, 6, 6, 8, 7 };
805 Object *obj = (Object *)malloc( sizeof( Object ) );
807 obj->vertex = (Vector *)malloc( num_vertex*sizeof( Vector ) );
809 (Triangle *)malloc( num_triangle*sizeof( Triangle ) );
810 obj->num_vertex = num_vertex;
811 obj->num_triangle = num_triangle;
813 /* create vertexes for dodecaedron */
815 for (v=0; v<6; ++v) {
816 obj->vertex[v].x = sin( a );
817 obj->vertex[v].y = -cos( a );
818 obj->vertex[v].z = 0.0;
823 a = -60.0/180.0*(double)M_PI;
824 e = 58.2825/180.0 * (double)M_PI;
826 obj->vertex[v].x = sin( a )*cos( e );
827 obj->vertex[v].y = -cos( a )*cos( e );
828 obj->vertex[v].z = -sin( e );
833 /* create triangles */
834 for (t=0; t<obj->num_triangle; ++t) {
835 obj->triangle[t].i[0] = vi[3*t];
836 obj->triangle[t].i[1] = vi[3*t+1];
837 obj->triangle[t].i[2] = vi[3*t+2];
840 /* subdivide as specified */
841 for (i=0; i<divisions; ++i) {
842 Object *newObj = subdivide( obj );
847 st->cell_polys = obj->num_triangle;
852 static int create_list( State *st, double fac )
855 Object *obj = clone_Object( st->sphere );
856 ObjectSmooth *smooth;
857 #ifdef USE_VERTEX_ARRAY
858 VertexArray *vertex_array;
862 int list = glGenLists(1);
864 /* apply wrinckle factor */
865 for (v=0; v<obj->num_vertex; ++v) {
866 vector_mul( &obj->vertex[v], 1.0+fac*st->disturbance[v] );
869 /* compute normals */
870 smooth = create_ObjectSmooth( obj );
873 /* Create display list */
874 glNewList( list, GL_COMPILE );
875 #ifdef USE_VERTEX_ARRAY
876 vertex_array = array_from_ObjectSmooth( smooth );
877 glEnableClientState( GL_VERTEX_ARRAY );
878 glEnableClientState( GL_NORMAL_ARRAY );
879 glVertexPointer( 3, GL_FLOAT, 0, vertex_array->vertex );
880 glNormalPointer( GL_FLOAT, 0, vertex_array->normal );
881 glDrawElements( GL_TRIANGLES, vertex_array->num_index,
882 GL_UNSIGNED_INT, vertex_array->index );
883 free( vertex_array );
885 glBegin( GL_TRIANGLES );
887 for (t=0; t<smooth->num_triangle; ++t) {
888 for (i=0; i<3; ++i) {
889 glNormal3f( smooth->normal[smooth->triangle[t].i[i]].x,
890 smooth->normal[smooth->triangle[t].i[i]].y,
891 smooth->normal[smooth->triangle[t].i[i]].z );
892 glVertex3f( smooth->vertex[smooth->triangle[t].i[i]].x,
893 smooth->vertex[smooth->triangle[t].i[i]].y,
894 smooth->vertex[smooth->triangle[t].i[i]].z );
902 free_ObjectSmooth( smooth );
907 static void draw_cell( State *st, int shape )
909 if (-1 == st->cell_list[shape]) {
910 st->cell_list[shape] = create_list( st, (double)shape/10.0 );
913 glCallList( st->cell_list[shape] );
916 static void create_nucleus_texture( State *st )
922 st->texture = (GLubyte *) malloc( 4*TEX_SIZE*TEX_SIZE );
924 for (y=0; y<TEX_SIZE; ++y) {
925 for (x=0; x<TEX_SIZE; ++x) {
926 float r2 = ((x-w2)*(x-w2)+(y-w2)*(y-w2));
927 float v = 120.0 * expf( -(r2) / s );
928 st->texture[4*(x+y*TEX_SIZE)] = (GLubyte)0;
929 st->texture[4*(x+y*TEX_SIZE)+1] = (GLubyte)0;
930 st->texture[4*(x+y*TEX_SIZE)+2] = (GLubyte)0;
931 st->texture[4*(x+y*TEX_SIZE)+3] = (GLubyte)v;
935 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
936 glGenTextures( 1, &st->texture_name );
937 glBindTexture( GL_TEXTURE_2D, st->texture_name );
939 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
940 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
941 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
942 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
943 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0,
944 GL_RGBA, GL_UNSIGNED_BYTE, st->texture );
947 static void draw_nucleus( State *st )
949 if (-1 == st->nucleus_list) {
952 st->nucleus_list = glGenLists( 1 );
953 glNewList( st->nucleus_list, GL_COMPILE );
955 glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -r, -r, z );
956 glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -r, r, z );
957 glTexCoord2f( 1.0f, 1.0f ); glVertex3f( r, r, z );
958 glTexCoord2f( 1.0f, 0.0f ); glVertex3f( r, -r, z );
963 glCallList( st->nucleus_list );
966 static void create_cells( State *st )
968 int border = (int)(200.0 * st->screen_scale);
970 int w = st->width-2*border;
971 int h = st->height-2*border;
973 st->color[0] = 0.5 + random_max( 1000 ) * 0.0005;
974 st->color[1] = 0.5 + random_max( 1000 ) * 0.0005;
975 st->color[2] = 0.5 + random_max( 1000 ) * 0.0005;
978 /* allocate if startup */
980 st->cell = (Cell *) malloc( st->max_cells * sizeof(Cell));
983 /* fill the screen with random food for our little critters */
984 foodcnt = (st->width*st->height)/16;
985 for (i=0; i<foodcnt; ++i) {
986 st->food[i] = random_interval( st->minfood, st->maxfood );
989 /* create the requested seed-cells */
990 st->num_cells = st->num_seeds;
992 for (i=0; i<st->num_cells; ++i) {
993 st->cell[i].x = border + random_max( w );
994 st->cell[i].y = border + random_max( h );
995 st->cell[i].vx = 0.0;
996 st->cell[i].vy = 0.0;
997 st->cell[i].age = random_max( 0x0f );
998 st->cell[i].min_dist = 500.0;
999 st->cell[i].energy = random_interval( 5, 5+0x3f );
1000 st->cell[i].rotation = ((double)random()/(double)RAND_MAX)*360.0;
1001 st->cell[i].radius = st->radius;
1002 st->cell[i].growth = 1.0;
1006 /* all this is rather expensive :( */
1007 static void tick( State *st )
1009 int new_num_cells, num_cells=0;
1011 int x, y, w4=st->width/4, h4=st->height/4, offset;
1015 const double check_dist = 0.75*st->move_dist;
1016 const double grow_dist = 0.75*st->radius;
1017 const double adult_radius = st->radius;
1019 /* find number of cells capable of division
1020 and count living cells
1022 for (b=0; b<st->num_cells; ++b) {
1023 if (st->cell[b].energy > 0) num_living++;
1024 if (can_divide( st, &st->cell[b] )) num_cells++;
1026 new_num_cells = st->num_cells + num_cells;
1028 /* end of simulation ? */
1029 if (0 == num_living || new_num_cells >= st->max_cells) {
1030 if (st->pause_counter > 0) st->pause_counter--;
1031 if (st->pause_counter > 0) return;
1033 st->pause_counter = st->pause;
1034 } else if (num_cells) { /* any fertile candidates ? */
1035 for (b=0, j=st->num_cells; b<st->num_cells; ++b) {
1036 if (can_divide( st, &st->cell[b] )) {
1037 st->cell[b].vx = random_interval( -50, 50 ) * 0.01;
1038 st->cell[b].vy = random_interval( -50, 50 ) * 0.01;
1039 st->cell[b].age = random_max( 0x0f );
1040 /* half energy for both plus some bonus for forking */
1041 st->cell[b].energy =
1042 st->cell[b].energy/2 + random_max( 0x0f );
1043 /* forking makes me shrink */
1044 st->cell[b].growth = 0.995;
1046 /* this one initially goes into the oposite direction */
1047 st->cell[j].vx = -st->cell[b].vx;
1048 st->cell[j].vy = -st->cell[b].vy;
1050 st->cell[j].x = st->cell[b].x;
1051 st->cell[j].y = st->cell[b].y;
1052 st->cell[j].age = random_max( 0x0f );
1053 st->cell[j].energy = (st->cell[b].energy);
1054 st->cell[j].rotation =
1055 ((double)random()/(double)RAND_MAX)*360.0;
1056 st->cell[j].growth = st->cell[b].growth;
1057 st->cell[j].radius = st->cell[b].radius;
1060 st->cell[b].vx = 0.0;
1061 st->cell[b].vy = 0.0;
1065 st->num_cells = new_num_cells;
1068 /* for each find a direction to escape */
1069 if (st->num_cells > 1) {
1070 for (b=0; b<st->num_cells; ++b) {
1071 if (st->cell[b].energy > 0) {
1076 /* grow or shrink */
1077 st->cell[b].radius *= st->cell[b].growth;
1078 /* find closest neighbour */
1079 min_dist = 100000.0;
1081 for (j=0; j<st->num_cells; ++j) {
1083 const double dx = st->cell[b].x - st->cell[j].x;
1084 const double dy = st->cell[b].y - st->cell[j].y;
1086 if (fabs(dx) < check_dist || fabs(dy) < check_dist) {
1087 const double dist = dx*dx+dy*dy;
1088 /*const double dist = sqrt( dx*dx+dy*dy );*/
1089 if (dist<min_dist) {
1096 /* escape step is away from closest normalized with distance */
1097 vx = st->cell[b].x - st->cell[min_index].x;
1098 vy = st->cell[b].y - st->cell[min_index].y;
1099 len = sqrt( vx*vx + vy*vy );
1101 st->cell[b].vx = vx/len;
1102 st->cell[b].vy = vy/len;
1104 st->cell[b].min_dist = len;
1105 /* if not adult (radius too small) */
1106 if (st->cell[b].radius < adult_radius) {
1107 /* if too small 60% stop shrinking */
1108 if (st->cell[b].radius < adult_radius * 0.6) {
1109 st->cell[b].growth = 1.0;
1111 /* at safe distance we start growing again */
1112 if (len > grow_dist) {
1113 if (st->cell[b].energy > 30) {
1114 st->cell[b].growth = 1.005;
1117 } else { /* else keep size */
1118 st->cell[b].growth = 1.0;
1123 st->cell[0].min_dist = 2*st->move_dist;
1126 /* now move em, snack and burn energy */
1127 for (b=0; b<st->num_cells; ++b) {
1128 /* if still alive */
1129 if (st->cell[b].energy > 0) {
1130 /* agility depends on amount of energy */
1131 double fac = (double)st->cell[b].energy / 50.0;
1132 if (fac < 0.0) fac = 0.0;
1133 if (fac > 1.0) fac = 1.0;
1135 st->cell[b].x += fac*(2.0 -
1136 (4.0*(double)random() / (double)RAND_MAX) +
1138 st->cell[b].y += fac*(2.0 -
1139 (4.0*(double)random() / (double)RAND_MAX) +
1142 /* get older and burn energy */
1143 if (st->cell[b].energy > 0) {
1145 st->cell[b].energy--;
1149 x = ((int)st->cell[b].x)/4;
1150 if (x<0) x=0; if (x>=w4) x = w4-1;
1151 y = ((int)st->cell[b].y)/4;
1152 if (y<0) y=0; if (y>=h4) y = h4-1;
1156 /* don't eat if already satisfied */
1157 if (st->cell[b].energy < 100 &&
1158 st->food[offset] > 0) {
1160 st->cell[b].energy++;
1161 /* if you are hungry, eat more */
1162 if (st->cell[b].energy < 50 &&
1163 st->food[offset] > 0) {
1165 st->cell[b].energy++;
1173 reshape_glcells( ModeInfo *mi, int width, int height )
1175 State *st = &sstate[MI_SCREEN(mi)];
1176 st->height = height;
1178 st->screen_scale = (double)width / 1600.0;
1180 st->radius = s_radius;
1181 if (st->radius < 5) st->radius = 5;
1182 if (st->radius > 200) st->radius = 200;
1183 st->radius *= st->screen_scale;
1185 st->move_dist = s_min_dist;
1186 if (st->move_dist < 1.0) st->move_dist = 1.0;
1187 if (st->move_dist > 3.0) st->move_dist = 3.0;
1188 st->move_dist *= st->radius;
1190 glViewport (0, 0, (GLint) width, (GLint) height);
1192 glMatrixMode(GL_PROJECTION);
1194 glOrtho( 0, width, height, 0, 200, 0 );
1195 glMatrixMode(GL_MODELVIEW);
1198 if (st->food) free( st->food );
1199 st->food = (int *)malloc( ((width*height)/16)*sizeof(int) );
1200 /* create_cells( st );*/
1204 init_glcells( ModeInfo *mi )
1211 calloc( MI_NUM_SCREENS(mi), sizeof(State) );
1213 fprintf( stderr, "%s: out of memory\n", progname );
1217 st = &sstate[MI_SCREEN(mi)];
1219 st->glx_context = init_GL(mi);
1222 st->wire = MI_IS_WIREFRAME(mi);
1224 # ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
1229 st->max_cells = s_maxcells;;
1230 if (st->max_cells < 50) st->max_cells = 50;
1231 if (st->max_cells > 10000) st->max_cells = 10000;
1233 st->pause = s_pause;
1234 if (st->pause < 0) st->pause = 0;
1235 if (st->pause > 400) st->pause = 400;
1236 st->pause_counter = st->pause;
1238 st->radius = s_radius;
1239 if (st->radius < 5) st->radius = 5;
1240 if (st->radius > 200) st->radius = 200;
1242 divisions = s_quality;
1243 if (divisions < 0) divisions = 0;
1244 if (divisions > 5) divisions = 5;
1246 st->num_seeds = s_seeds;
1247 if (st->num_seeds < 1) st->num_seeds = 1;
1248 if (st->num_seeds > 16) st->num_seeds = 16;
1250 st->minfood = s_minfood;
1251 if (st->minfood < 0) st->minfood = 0;
1252 if (st->minfood > 1000) st->minfood = 1000;
1254 st->maxfood = s_maxfood;
1255 if (st->maxfood < 0) st->maxfood = 0;
1256 if (st->maxfood > 1000) st->maxfood = 1000;
1258 if (st->maxfood < st->minfood) st->maxfood = st->minfood+1;
1260 st->keep_old_cells = s_keepold;
1262 st->divide_age = s_divideage;
1263 if (st->divide_age < 1) st->divide_age = 1;
1264 if (st->divide_age > 1000) st->divide_age = 1000;
1266 st->move_dist = s_min_dist;
1267 if (st->move_dist < 1.0) st->move_dist = 1.0;
1268 if (st->move_dist > 3.0) st->move_dist = 3.0;
1269 st->move_dist *= st->radius;
1271 for (i=0; i<NUM_CELL_SHAPES; ++i) st->cell_list[i] = -1;
1272 st->nucleus_list = -1;
1275 st->sphere = create_sphere( st, divisions );
1277 (double *) malloc( st->sphere->num_vertex*sizeof(double) );
1278 for (i=0; i<st->sphere->num_vertex; ++i) {
1279 st->disturbance[i] =
1280 0.05-((double)random()/(double)RAND_MAX*0.1);
1283 create_nucleus_texture( st );
1285 reshape_glcells (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
1289 draw_glcells( ModeInfo *mi )
1291 State *st = &sstate[MI_SCREEN(mi)];
1292 Display *dpy = MI_DISPLAY(mi);
1293 Window window = MI_WINDOW(mi);
1295 if (!st->glx_context) return;
1297 glXMakeCurrent( MI_DISPLAY(mi), MI_WINDOW(mi),
1298 *(st->glx_context) );
1300 mi->polygon_count = render( st );
1302 if (mi->fps_p) do_fps (mi);
1305 glXSwapBuffers( dpy, window );
1309 release_glcells( ModeInfo *mi )
1312 State *st = &sstate[MI_SCREEN(mi)];
1314 /* nuke everything before exit */
1315 if (st->sphere) free_Object( st->sphere );
1316 if (st->food) free( st->food );
1317 for (i=0; i<NUM_CELL_SHAPES; ++i) {
1318 if (st->cell_list[i] != -1) {
1319 glDeleteLists( st->cell_list[i], 1 );
1322 if (st->cell) free( st->cell );
1323 free( st->disturbance );
1324 glDeleteTextures( 1, &st->texture_name );
1325 free( st->texture );
1328 XSCREENSAVER_MODULE( "GLCells", glcells )