summaryrefslogtreecommitdiff
path: root/gtk/plugins/crazychat/cc_output.c
blob: 77a04df2111ca2e5b993d73bc45055860ca9c848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
/*#include "util.h"*/
#include "cc_interface.h"
#include "crazychat.h"
#include <stdio.h>

#include "cc_gtk_gl.h"

#include "face.h"
#include "glm.h"


#define TAN_30 0.577
#define ROOT_3 1.73
//this will be the width of all heads
#define HEAD_WIDTH 2.5
#define HEAD_HEIGHT 2.5 //same for now, this will actually vary from head
#define PERSONS_HEAD 1
#define MOVEMENT .33

GLfloat ambient[] = {1.0, 1.0, 1.0, 1.0};
GLfloat diffuse[] = {.7, .7, .7, 1};
GLfloat specular[] = {1, 1, 1, 1};
GLfloat lightpos[] = {0.0, 0.0, 20, 1};
GLfloat specref[] = {1.0, 1, 1, 1,};

GLfloat xrot, yrot, zrot;
GLfloat x, y,  z, mouth_open;
GLint left_eye_frame, right_eye_frame;		//between 0 - 8
GLint mouth_type;
GLint mouth_frame;
DIRECTION dir;
BOOL left_open, right_open;
GLfloat window_aspect;

FACE remote_shark_face, remote_dog_face, local_shark_face, local_dog_face;
int count = 0;
int curr_materials[DOG_SHARK_CHANGE]; //these are the materials from output_instance
OUTPUT_MODE curr_mode;
KIND which_face;

static void destroy_cb(GtkWidget *widget, struct output_instance *data);

void Interpolate(struct output_instance* instance){
	GLfloat rangeX, rangeY, minZ, adj, angle, temp;
	count++;
/*	yrot=90;
	zrot=0;
	z=5;
	x=0;
	y=0;
	left_open = right_open = TRUE;
	mouth_open = (float)(count%10)/10;
	dir = CONST;
	curr_mode = NORMAL;
	return;
*/
	
	//find z plane from percentage of face
	if(instance->features->head_size==0){
		z = 5;
	}	

	temp = (GLfloat)instance->features->head_size/40.0;
	//printf("head size %d\n", instance->features->head_size);
	
	minZ = ROOT_3;
	z = ROOT_3*(PERSONS_HEAD/temp);
	if(z < minZ)
		z = minZ;
	 	
	//these calculations are based on a 90 degree viewing angle
	rangeX = z*(TAN_30)*2;
	rangeY = window_aspect*rangeX;
	temp = (GLfloat)instance->features->x;
	if(temp>50) { //then its on the left
		temp = (temp - 50.0)/50.0;
		x = 0 - temp*rangeX/1.0;
	}
	else {
		temp = (50.0-temp)/50.0;
		x = 0 + temp*rangeX/1.0;
	}

	temp = (GLfloat)instance->features->y;	

	if(temp>50){
		temp = (temp-50.0)/50.0;
		y = 0 - temp*rangeY/1.0;
	}
	else {
		temp = (50.0-temp)/50.0;
		y = 0 + temp*rangeY/1.0;
	}

	temp = (GLfloat)instance->features->head_y_rot;
	yrot = temp - 50;
	temp = (GLfloat)instance->features->head_z_rot;
	zrot = temp-50;

	if(y-instance->past_y < -MOVEMENT)
	  dir = DOWN;
	else if(y-instance->past_y > MOVEMENT)
	  dir = UP;
	else
	  dir = CONST;
	instance->past_y=y;

	mouth_open = (float)instance->features->mouth_open/105;
	count++;
	//mouth_open = (count%10)/(10);	

	if(instance->features->left_eye_open==0){
		left_open = FALSE;
	}
	else{
		left_open = TRUE;
	}	

	if(instance->features->right_eye_open==0)
		right_open = FALSE;
	else
		right_open = TRUE;
	//right_open =1 - (count%5)/4;

	//set the materials
	curr_materials[APPENDAGE]=instance->features->appendage_color;
	curr_materials[HEAD]=instance->features->head_color;
	curr_materials[LIDS]=instance->features->lid_color;
	curr_materials[LEFT_IRIS]=instance->features->left_iris_color;
	curr_materials[RIGHT_IRIS]=instance->features->right_iris_color;
	// we don't get an x rotation
	xrot = 0;
	curr_mode=instance->features->mode;
	if(instance->features->kind==0)
		which_face=DOG;
	else
		which_face=SHARK;
	
}



gboolean configure(GtkWidget *widget,
				GdkEventConfigure *event, void *data)
{
	GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
	
	GLfloat w = widget->allocation.width;
	GLfloat h = widget->allocation.height;
	GLfloat aspect;

	Debug("configuring\n");


	/*** OpenGL BEGIN ***/
	if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
		return FALSE;

	glEnable(GL_DEPTH_TEST);
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	if (w > h) {
		window_aspect = w / h;
	} else {
		window_aspect = h / w;
	}

	//glOrtho(-10, 10, -10,10, 0.0001, 1000); 
	gluPerspective(90.0, window_aspect, 0.0001, 1000.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gdk_gl_drawable_gl_end(gldrawable);
	/*** OpenGL END ***/

	return TRUE;
}

gboolean draw(GtkWidget *widget, GdkEventExpose *event,
			      void *data)
{
	struct output_instance *instance = (struct output_instance*)data;
	if(!data) {
		fprintf(stderr,"null\n");
	}
	assert(instance);
	Interpolate(instance);	

	GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
	GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

	//return TRUE;

	assert(gtk_widget_is_gl_capable(widget));

	if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext)) {
		fprintf(stderr, "We're fucked this time.\n");
		return FALSE;
	}


	glClearColor(1.0, 1.0, 1.0, 0.0);
	//glDisable(GL_CULL_FACE);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glColor3f(1.0, 1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glTranslatef(x, y, -z);
	if(instance->my_output==LOCAL){
		if(which_face==DOG){
			change_materials(local_dog_face, curr_materials, DOG_SHARK_CHANGE);
			draw_face(local_dog_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
		}
		else {
			change_materials(local_shark_face, curr_materials, DOG_SHARK_CHANGE);
			draw_face(local_shark_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
		}	
	}
	else{
		if(which_face==DOG){
			change_materials(remote_dog_face, curr_materials, DOG_SHARK_CHANGE);
			draw_face(remote_dog_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
		}
		else{
			change_materials(remote_shark_face, curr_materials, DOG_SHARK_CHANGE);
			draw_face(remote_shark_face, zrot, yrot, left_open, right_open, mouth_open, dir, curr_mode);
		}
	}
	if (gdk_gl_drawable_is_double_buffered (gldrawable))
		gdk_gl_drawable_swap_buffers (gldrawable);
	else
		glFlush ();
	return TRUE;
}

void init (GtkWidget *widget, void *data) 
{
	setupDrawlists(REMOTE);
	setupLighting(widget);
}


void setupDrawlists(OUTPUT output)
{
	if(output==REMOTE){
		remote_shark_face = init_face(SHARK);
		remote_dog_face = init_face(DOG);
	}
	if(output==LOCAL){
		local_shark_face = init_face(SHARK);
		local_dog_face = init_face(DOG);
	}
}


void setupLighting(GtkWidget *widget)
{

	GLfloat w = widget->allocation.width;
	GLfloat h = widget->allocation.height;
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        //glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
        glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
        glEnable(GL_LIGHT0);
        //glEnable(GL_COLOR_MATERIAL);
        //glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
        glMateriali(GL_FRONT, GL_SHININESS, 128);
        
        //glEnable(GL_CULL_FACE);
        
        glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glOrtho(-2, 2, -2, 2, 0.0001, 1000);
	
	if (w > h) {
		window_aspect = w / h;
	} else {
		window_aspect = h / w;
	}

	gluPerspective(90.0, window_aspect, 0.0001, 1000.0);
	//glFrustum(-100.0, 100.0, -100.0, 100.0, 0, 10);
	glMatrixMode(GL_MODELVIEW);
	//gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

struct output_instance *init_output(struct cc_features *features,
		struct cc_session *session)
{
	struct draw_info *info;
	struct output_instance *instance;
	struct window_box ret;

	instance = (struct output_instance*)malloc(sizeof(*instance));
	assert(instance);
	memset(instance, 0, sizeof(*instance));
	instance->features = features;
	instance->session = session;

	info = (struct draw_info*)malloc(sizeof(*info));
	memset(info, 0, sizeof(*info));
	info->timeout = TRUE;
	info->delay_ms = 40;
	info->data = instance;


	cc_new_gl_window(init, configure, draw, info, &ret);
	g_signal_connect(GTK_OBJECT(ret.window), "destroy",
			G_CALLBACK(destroy_cb),	instance);
	gtk_window_set_default_size(ret.window,640,480);
	if (session) {
		gtk_window_set_title(ret.window, session->name);
	}
	gtk_widget_show(ret.window);
	instance->widget = ret.window;
	instance->box = ret.vbox;
	return instance;
}

static void destroy_cb(GtkWidget *widget, struct output_instance *data)
{
	Debug("Closing output window\n");
	if (data->session) {
		cc_remove_session(data->session->cc, data->session);
		close(data->session->tcp_sock);
		close(data->session->udp_sock);
		Filter_Destroy(data->session->filter);
		destroy_output(data->session->output);
	}
}

void destroy_output(struct output_instance *instance)
{
	free(instance);
}