11218
|
1 #include <assert.h>
|
|
2 #include <string.h>
|
|
3 #include <stdlib.h>
|
|
4 #include <GL/gl.h>
|
|
5 #include <GL/glu.h>
|
|
6 #include <math.h>
|
|
7 #include "util.h"
|
|
8 #include "cc_interface.h"
|
|
9 #include "crazychat.h"
|
|
10 #include <stdio.h>
|
|
11
|
|
12 #include "cc_gtk_gl.h"
|
|
13
|
|
14 #include "face.h"
|
|
15 #include "glm.h"
|
|
16
|
|
17
|
|
18 #define TAN_30 0.577
|
|
19 #define ROOT_3 1.73
|
|
20 //this will be the width of all heads
|
|
21 #define HEAD_WIDTH 2.5
|
|
22 #define HEAD_HEIGHT 2.5 //same for now, this will actually vary from head
|
|
23 #define PERSONS_HEAD 1
|
|
24 #define MOVEMENT .33
|
|
25
|
|
26 GLfloat ambient[] = {1.0, 1.0, 1.0, 1.0};
|
|
27 GLfloat diffuse[] = {.7, .7, .7, 1};
|
|
28 GLfloat specular[] = {1, 1, 1, 1};
|
|
29 GLfloat lightpos[] = {0.0, 0.0, 20, 1};
|
|
30 GLfloat specref[] = {1.0, 1, 1, 1,};
|
|
31
|
|
32 GLfloat xrot, yrot, zrot;
|
|
33 GLfloat x, y, z, mouth_open;
|
|
34 GLint left_eye_frame, right_eye_frame; //between 0 - 8
|
|
35 GLint mouth_type;
|
|
36 GLint mouth_frame;
|
|
37 DIRECTION dir;
|
|
38 BOOL left_open, right_open;
|
|
39 GLfloat window_aspect;
|
|
40
|
|
41 FACE remote_shark_face, remote_dog_face, local_shark_face, local_dog_face;
|
|
42 int count = 0;
|
|
43 int curr_materials[DOG_SHARK_CHANGE]; //these are the materials from output_instance
|
|
44 OUTPUT_MODE curr_mode;
|
|
45 KIND which_face;
|
|
46
|
|
47 static void destroy_cb(GtkWidget *widget, struct output_instance *data);
|
|
48
|
|
49 void Interpolate(struct output_instance* instance){
|
|
50 GLfloat rangeX, rangeY, minZ, adj, angle, temp;
|
|
51 count++;
|
|
52 /* yrot=90;
|
|
53 zrot=0;
|
|
54 z=5;
|
|
55 x=0;
|
|
56 y=0;
|
|
57 left_open = right_open = TRUE;
|
|
58 mouth_open = (float)(count%10)/10;
|
|
59 dir = CONST;
|
|
60 curr_mode = NORMAL;
|
|
61 return;
|
|
62 */
|
|
63
|
|
64 //find z plane from percentage of face
|
|
65 if(instance->features->head_size==0){
|
|
66 z = 5;
|
|
67 }
|
|
68
|
|
69 temp = (GLfloat)instance->features->head_size/40.0;
|
|
70 //printf("head size %d\n", instance->features->head_size);
|
|
71
|
|
72 minZ = ROOT_3;
|
|
73 z = ROOT_3*(PERSONS_HEAD/temp);
|
|
74 if(z < minZ)
|
|
75 z = minZ;
|
|
76
|
|
77 //these calculations are based on a 90 degree viewing angle
|
|
78 rangeX = z*(TAN_30)*2;
|
|
79 rangeY = window_aspect*rangeX;
|
|
80 temp = (GLfloat)instance->features->x;
|
|
81 if(temp>50) { //then its on the left
|
|
82 temp = (temp - 50.0)/50.0;
|
|
83 x = 0 - temp*rangeX/1.0;
|
|
84 }
|
|
85 else {
|
|
86 temp = (50.0-temp)/50.0;
|
|
87 x = 0 + temp*rangeX/1.0;
|
|
88 }
|
|
89
|
|
90 temp = (GLfloat)instance->features->y;
|
|
91
|
|
92 if(temp>50){
|
|
93 temp = (temp-50.0)/50.0;
|
|
94 y = 0 - temp*rangeY/1.0;
|
|
95 }
|
|
96 else {
|
|
97 temp = (50.0-temp)/50.0;
|
|
98 y = 0 + temp*rangeY/1.0;
|
|
99 }
|
|
100
|
|
101 temp = (GLfloat)instance->features->head_y_rot;
|
|
102 yrot = temp - 50;
|
|
103 temp = (GLfloat)instance->features->head_z_rot;
|
|
104 zrot = temp-50;
|
|
105
|
|
106 if(y-instance->past_y < -MOVEMENT)
|
|
107 dir = DOWN;
|
|
108 else if(y-instance->past_y > MOVEMENT)
|
|
109 dir = UP;
|
|
110 else
|
|
111 dir = CONST;
|
|
112 instance->past_y=y;
|
|
113
|
|
114 mouth_open = (float)instance->features->mouth_open/105;
|
|
115 count++;
|
|
116 //mouth_open = (count%10)/(10);
|
|
117
|
|
118 if(instance->features->left_eye_open==0){
|
|
119 left_open = FALSE;
|
|
120 }
|
|
121 else{
|
|
122 left_open = TRUE;
|
|
123 }
|
|
124
|
|
125 if(instance->features->right_eye_open==0)
|
|
126 right_open = FALSE;
|
|
127 else
|
|
128 right_open = TRUE;
|
|
129 //right_open =1 - (count%5)/4;
|
|
130
|
|
131 //set the materials
|
|
132 curr_materials[APPENDAGE]=instance->features->appendage_color;
|
|
133 curr_materials[HEAD]=instance->features->head_color;
|
|
134 curr_materials[LIDS]=instance->features->lid_color;
|
|
135 curr_materials[LEFT_IRIS]=instance->features->left_iris_color;
|
|
136 curr_materials[RIGHT_IRIS]=instance->features->right_iris_color;
|
|
137 // we don't get an x rotation
|
|
138 xrot = 0;
|
|
139 curr_mode=instance->features->mode;
|
|
140 if(instance->features->kind==0)
|
|
141 which_face=DOG;
|
|
142 else
|
|
143 which_face=SHARK;
|
|
144
|
|
145 }
|
|
146
|
|
147
|
|
148
|
|
149 gboolean configure(GtkWidget *widget,
|
|
150 GdkEventConfigure *event, void *data)
|
|
151 {
|
|
152 GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
|
|
153 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
|
|
154
|
|
155 GLfloat w = widget->allocation.width;
|
|
156 GLfloat h = widget->allocation.height;
|
|
157 GLfloat aspect;
|
|
158
|
|
159 Debug("configuring\n");
|
|
160
|
|
161
|
|
162 /*** OpenGL BEGIN ***/
|
|
163 if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
|
|
164 return FALSE;
|
|
165
|
|
166 glEnable(GL_DEPTH_TEST);
|
|
167 glViewport(0, 0, w, h);
|
|
168 glMatrixMode(GL_PROJECTION);
|
|
169 glLoadIdentity();
|
|
170
|
|
171 if (w > h) {
|
|
172 window_aspect = w / h;
|
|
173 } else {
|
|
174 window_aspect = h / w;
|
|
175 }
|
|
176
|
|
177 //glOrtho(-10, 10, -10,10, 0.0001, 1000);
|
|
178 gluPerspective(90.0, window_aspect, 0.0001, 1000.0);
|
|
179
|
|
180 glMatrixMode(GL_MODELVIEW);
|
|
181 glLoadIdentity();
|
|
182
|
|
183 gdk_gl_drawable_gl_end(gldrawable);
|
|
184 /*** OpenGL END ***/
|
|
185
|
|
186 return TRUE;
|
|
187 }
|
|
188
|
|
189 gboolean draw(GtkWidget *widget, GdkEventExpose *event,
|
|
190 void *data)
|
|
191 {
|
|
192 struct output_instance *instance = (struct output_instance*)data;
|
|
193 if(!data) {
|
|
194 fprintf(stderr,"null\n");
|
|
195 }
|
|
196 assert(instance);
|
|
197 Interpolate(instance);
|
|
198
|
|
199 GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
|
|
200 GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
|
|
201
|
|
202 //return TRUE;
|
|
203
|
|
204 assert(gtk_widget_is_gl_capable(widget));
|
|
205
|
|
206 if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext)) {
|
|
207 fprintf(stderr, "We're fucked this time.\n");
|
|
208 return FALSE;
|
|
209 }
|
|
210
|
|
211
|
|
212 glClearColor(1.0, 1.0, 1.0, 0.0);
|
|
213 //glDisable(GL_CULL_FACE);
|
|
214
|
|
215 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
216 glColor3f(1.0, 1.0, 1.0);
|
|
217 glMatrixMode(GL_MODELVIEW);
|
|
218 glLoadIdentity();
|
|
219
|
|
220 glTranslatef(x, y, -z);
|
|
221 if(instance->my_output==LOCAL){
|
|
222 if(which_face==DOG){
|
|
223 change_materials(local_dog_face, curr_materials, DOG_SHARK_CHANGE);
|
|
224 draw_face(local_dog_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
|
|
225 }
|
|
226 else {
|
|
227 change_materials(local_shark_face, curr_materials, DOG_SHARK_CHANGE);
|
|
228 draw_face(local_shark_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
|
|
229 }
|
|
230 }
|
|
231 else{
|
|
232 if(which_face==DOG){
|
|
233 change_materials(remote_dog_face, curr_materials, DOG_SHARK_CHANGE);
|
|
234 draw_face(remote_dog_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
|
|
235 }
|
|
236 else{
|
|
237 change_materials(remote_shark_face, curr_materials, DOG_SHARK_CHANGE);
|
|
238 draw_face(remote_shark_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
|
|
239 }
|
|
240 }
|
|
241 if (gdk_gl_drawable_is_double_buffered (gldrawable))
|
|
242 gdk_gl_drawable_swap_buffers (gldrawable);
|
|
243 else
|
|
244 glFlush ();
|
|
245 return TRUE;
|
|
246 }
|
|
247
|
|
248 void init (GtkWidget *widget, void *data)
|
|
249 {
|
|
250 setupDrawlists(REMOTE);
|
|
251 setupLighting(widget);
|
|
252 }
|
|
253
|
|
254
|
|
255 void setupDrawlists(OUTPUT output)
|
|
256 {
|
|
257 if(output==REMOTE){
|
|
258 remote_shark_face = init_face(SHARK);
|
|
259 remote_dog_face = init_face(DOG);
|
|
260 }
|
|
261 if(output==LOCAL){
|
|
262 local_shark_face = init_face(SHARK);
|
|
263 local_dog_face = init_face(DOG);
|
|
264 }
|
|
265 }
|
|
266
|
|
267
|
|
268 void setupLighting(GtkWidget *widget)
|
|
269 {
|
|
270
|
|
271 GLfloat w = widget->allocation.width;
|
|
272 GLfloat h = widget->allocation.height;
|
|
273 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
274 glEnable(GL_DEPTH_TEST);
|
|
275 glEnable(GL_LIGHTING);
|
|
276 //glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
|
|
277 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
|
|
278 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
|
|
279 glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
|
|
280 glEnable(GL_LIGHT0);
|
|
281 //glEnable(GL_COLOR_MATERIAL);
|
|
282 //glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
|
|
283 glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
|
|
284 glMateriali(GL_FRONT, GL_SHININESS, 128);
|
|
285
|
|
286 //glEnable(GL_CULL_FACE);
|
|
287
|
|
288 glClear(GL_COLOR_BUFFER_BIT);
|
|
289 glMatrixMode(GL_PROJECTION);
|
|
290 glLoadIdentity();
|
|
291 //glOrtho(-2, 2, -2, 2, 0.0001, 1000);
|
|
292
|
|
293 if (w > h) {
|
|
294 window_aspect = w / h;
|
|
295 } else {
|
|
296 window_aspect = h / w;
|
|
297 }
|
|
298
|
|
299 gluPerspective(90.0, window_aspect, 0.0001, 1000.0);
|
|
300 //glFrustum(-100.0, 100.0, -100.0, 100.0, 0, 10);
|
|
301 glMatrixMode(GL_MODELVIEW);
|
|
302 //gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
|
|
303 }
|
|
304
|
|
305 struct output_instance *init_output(struct cc_features *features,
|
|
306 struct cc_session *session)
|
|
307 {
|
|
308 struct draw_info *info;
|
|
309 struct output_instance *instance;
|
|
310 struct window_box ret;
|
|
311
|
|
312 instance = (struct output_instance*)malloc(sizeof(*instance));
|
|
313 assert(instance);
|
|
314 memset(instance, 0, sizeof(*instance));
|
|
315 instance->features = features;
|
|
316 instance->session = session;
|
|
317
|
|
318 info = (struct draw_info*)malloc(sizeof(*info));
|
|
319 memset(info, 0, sizeof(*info));
|
|
320 info->timeout = TRUE;
|
|
321 info->delay_ms = 40;
|
|
322 info->data = instance;
|
|
323
|
|
324
|
|
325 cc_new_gl_window(init, configure, draw, info, &ret);
|
|
326 g_signal_connect(GTK_OBJECT(ret.window), "destroy",
|
|
327 G_CALLBACK(destroy_cb), instance);
|
|
328 gtk_window_set_default_size(ret.window,640,480);
|
|
329 if (session) {
|
|
330 gtk_window_set_title(ret.window, session->name);
|
|
331 }
|
|
332 gtk_widget_show(ret.window);
|
|
333 instance->widget = ret.window;
|
|
334 instance->box = ret.vbox;
|
|
335 return instance;
|
|
336 }
|
|
337
|
|
338 static void destroy_cb(GtkWidget *widget, struct output_instance *data)
|
|
339 {
|
|
340 Debug("Closing output window\n");
|
|
341 if (data->session) {
|
|
342 cc_remove_session(data->session->cc, data->session);
|
|
343 close(data->session->tcp_sock);
|
|
344 close(data->session->udp_sock);
|
|
345 Filter_Destroy(data->session->filter);
|
|
346 destroy_output(data->session->output);
|
|
347 }
|
|
348 }
|
|
349
|
|
350 void destroy_output(struct output_instance *instance)
|
|
351 {
|
|
352 free(instance);
|
|
353 }
|