SCM Repository
Annotation of /examples/boids/main.c
Parent Directory
|
Revision Log
Revision 2128 - (view) (download) (as text)
1 : | lamonts | 2125 | #include <stdlib.h> |
2 : | #include "util.h" | ||
3 : | lamonts | 2128 | #include "boids.h" |
4 : | lamonts | 2125 | #include <unistd.h> |
5 : | #ifdef __APPLE__ | ||
6 : | # include <GLUT/glut.h> | ||
7 : | #else | ||
8 : | # include <GL/glut.h> | ||
9 : | #endif | ||
10 : | |||
11 : | #define PI 3.1415926535897932384626433832795 | ||
12 : | |||
13 : | /* pixel dimensions of output */ | ||
14 : | #define WIDTH 800 | ||
15 : | #define HEIGHT 600 | ||
16 : | |||
17 : | #define NUMBOIDS | ||
18 : | |||
19 : | /* Wrapping dimensions and indices */ | ||
20 : | #define NORTH 0 | ||
21 : | #define SOUTH 1 | ||
22 : | #define WEST 2 | ||
23 : | #define EAST 3 | ||
24 : | #define STEP_SIZE 1 | ||
25 : | #define SLEEP_MS 250 | ||
26 : | |||
27 : | int wrapDimensions[4] = {-10, | ||
28 : | 600 + 10, | ||
29 : | -10, | ||
30 : | 800 + 10}; | ||
31 : | |||
32 : | BOID_World_t * wrld; | ||
33 : | |||
34 : | bool isInitial = true; | ||
35 : | int count = 0; | ||
36 : | // nrrd for getting computational state | ||
37 : | Nrrd *nData; | ||
38 : | |||
39 : | void setUpView(void) | ||
40 : | { | ||
41 : | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | ||
42 : | glMatrixMode(GL_PROJECTION); | ||
43 : | glLoadIdentity(); | ||
44 : | glOrtho(0, // left | ||
45 : | WIDTH, // right | ||
46 : | HEIGHT, // bottom | ||
47 : | 0, // top | ||
48 : | 0, // zNear | ||
49 : | 1 // zFar | ||
50 : | ); | ||
51 : | } | ||
52 : | void shouldWrap(float * b) | ||
53 : | { | ||
54 : | if(b[0] > wrapDimensions[WEST]) | ||
55 : | b[0] = wrapDimensions[EAST]; | ||
56 : | else if(b[0] > wrapDimensions[EAST]) | ||
57 : | b[0] = wrapDimensions[WEST]; | ||
58 : | |||
59 : | if(b[1] < wrapDimensions[NORTH]) | ||
60 : | b[1] = wrapDimensions[SOUTH]; | ||
61 : | else if(b[1] > wrapDimensions[SOUTH]) | ||
62 : | b[1] = wrapDimensions[NORTH]; | ||
63 : | } | ||
64 : | void DrawBoids(Nrrd *positions) | ||
65 : | { | ||
66 : | glMatrixMode(GL_MODELVIEW); | ||
67 : | int r = 5; // "radius" of the triangle | ||
68 : | |||
69 : | float *boids = (float *)(positions->data); | ||
70 : | unsigned int nBoids = positions->axis[1].size; | ||
71 : | |||
72 : | for(int i = 0; i < nBoids; i++) | ||
73 : | { | ||
74 : | int idx = i * 4; | ||
75 : | |||
76 : | float theta = (-1 * atan2(-1 * boids[idx + 2], boids[idx + 3])) + (PI/2.0); | ||
77 : | float degTheta = (theta * 180/PI); | ||
78 : | glColor3f(0.0f, 0.0f, 1.0f); | ||
79 : | glLoadIdentity(); | ||
80 : | glPushMatrix(); | ||
81 : | glTranslatef(boids[idx],boids[idx+1],0.0f); | ||
82 : | glRotatef(degTheta,0.0f,0.0f,1.0f); | ||
83 : | glBegin(GL_TRIANGLES); | ||
84 : | glVertex2f(0, -1 * r * 2); | ||
85 : | glVertex2f(-1 * r, r * 2); | ||
86 : | glVertex2f(r, r * 2); | ||
87 : | glEnd(); | ||
88 : | glPopMatrix(); | ||
89 : | } | ||
90 : | |||
91 : | } | ||
92 : | void GetData (BOID_World_t *wrld, Nrrd *nData) | ||
93 : | { | ||
94 : | // get snapshot of state | ||
95 : | if (BOID_Snapshot_boidInfo(wrld, nData)) { | ||
96 : | // error | ||
97 : | fprintf(stderr, "Error getting nrrd data\n"); | ||
98 : | exit(1); | ||
99 : | } | ||
100 : | } | ||
101 : | void flock (int value) | ||
102 : | { | ||
103 : | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | ||
104 : | glClear(GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer | ||
105 : | setUpView(); | ||
106 : | |||
107 : | // get and render initial state | ||
108 : | if(isInitial){ | ||
109 : | GetData (wrld, nData); | ||
110 : | DrawBoids(nData); | ||
111 : | isInitial = false; | ||
112 : | glutSwapBuffers(); | ||
113 : | }else { | ||
114 : | if(BOID_NumActive(wrld) > 0){ | ||
115 : | // step the computation | ||
116 : | BOID_Run(wrld, STEP_SIZE); | ||
117 : | // get and render the state | ||
118 : | GetData (wrld, nData); | ||
119 : | DrawBoids(nData); | ||
120 : | glutSwapBuffers(); | ||
121 : | } | ||
122 : | } | ||
123 : | glutTimerFunc(50,flock, 0); | ||
124 : | } | ||
125 : | void Display(void) | ||
126 : | { | ||
127 : | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | ||
128 : | glClear(GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer | ||
129 : | setUpView(); | ||
130 : | } | ||
131 : | int main(int argc, char** argv) | ||
132 : | { | ||
133 : | glutInit(&argc, argv); | ||
134 : | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); | ||
135 : | glutInitWindowSize(WIDTH, HEIGHT); | ||
136 : | glutCreateWindow("Flocking Diderot Program"); | ||
137 : | glutDisplayFunc(&Display); | ||
138 : | |||
139 : | // initialize the Diderot program | ||
140 : | wrld = BOID_Init(); | ||
141 : | nData = nrrdNew(); | ||
142 : | BOID_InVarSet_width (wrld, WIDTH); | ||
143 : | BOID_InVarSet_height(wrld, HEIGHT); | ||
144 : | BOID_Initially (wrld); | ||
145 : | |||
146 : | glutTimerFunc(50,flock, 0); | ||
147 : | |||
148 : | glutMainLoop(); | ||
149 : | |||
150 : | return 0; | ||
151 : | } |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |