vpost_plugins

view hullsdr/hullsdr_post.c @ 0:7f7342bfbb25

vpost plugin for that video: http://www.youtube.com/watch?v=txt6SGL483E
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Oct 2011 06:23:32 +0300
parents
children
line source
1 #include <math.h>
2 #include <assert.h>
3 #include <GL/glut.h>
4 #include <GL/freeglut_ext.h>
5 #include <imago2.h>
6 #include <drawtext.h>
7 #include <dsys.h>
9 static void disp(void);
10 static void draw_text(void);
11 static void reshape(int x, int y);
13 static struct img_pixmap *curfrm;
14 static int nfrm;
15 static int xsz = -1, ysz;
16 static float tsec;
18 static struct dsys_demo *script;
20 static struct dtx_font *font;
22 static struct {
23 const char *name, *caption;
24 struct dsys_event *ev;
25 } events[] = {
26 {"diffuse", "diffuse only - 1 light", 0},
27 {"diffuse2", "diffuse only - 2 lights", 0},
28 {"vmanip", "vertex manipulation", 0},
29 {"2tone", "two-tone paint", 0},
30 {"specular2", "full phong - 2 lights", 0},
31 {"celshade", "cel shading", 0},
32 {"sphmap", "reflection - spherical env. map", 0},
33 {"cubemap", "reflection - cubic env. map", 0},
34 {"refract", "relfection & refraction - cubic env. map w/fresnel", 0},
35 {"distort1", "image distortion", 0},
36 {"blur", "blur", 0},
37 {"distort2", "image distortion (again)", 0},
38 {0, 0, 0}
39 };
42 int init(void)
43 {
44 int i, argc = 1;
45 char *argv[] = {"./vpost", 0};
47 if(!(font = dtx_open_font("linux-libertine.ttf", 32))) {
48 fprintf(stderr, "failed to open font\n");
49 return -1;
50 }
52 if(!(script = dsys_open("script"))) {
53 fprintf(stderr, "failed to open script\n");
54 return -1;
55 }
57 for(i=0; events[i].name; i++) {
58 events[i].ev = dsys_event(script, events[i].name);
59 }
61 glutInit(&argc, argv);
62 glutInitWindowSize(32, 32);
63 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
65 glutCreateWindow("foo");
67 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
69 glutDisplayFunc(disp);
70 glutReshapeFunc(reshape);
71 return 0;
72 }
74 void shutdown(void)
75 {
76 printf("shutdown called\n");
77 }
79 int process(struct img_pixmap *frame, int frm_num, float sec)
80 {
81 curfrm = frame;
82 nfrm = frm_num;
83 tsec = sec;
85 if(frame->width != xsz || frame->height != ysz) {
86 glutReshapeWindow(frame->width, frame->height);
87 }
88 glutPostRedisplay();
90 while(curfrm) {
91 glutMainLoopEvent();
92 }
93 return 0;
94 }
96 static void disp(void)
97 {
98 assert(curfrm);
99 glClear(GL_COLOR_BUFFER_BIT);
101 dsys_update(script, dsys_msec_to_dtime(nfrm));
103 glDrawPixels(xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE, curfrm->pixels);
105 draw_text();
107 glFlush();
108 glReadPixels(0, 0, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE, curfrm->pixels);
110 glutSwapBuffers();
111 curfrm = 0;
113 assert(glGetError() == GL_NO_ERROR);
114 }
116 static float fade(float x)
117 {
118 x = 1.0 - (cos((x) * M_PI * 2.0) * 0.5 + 0.5);
119 x *= 10.0f;
121 return x > 1.0 ? 1.0 : x;
122 }
124 static void caption(const char *text, struct dsys_event *ev)
125 {
126 float t = fade(dsys_event_value(ev));
128 if(t < (1.0f / 255.0f)) {
129 return;
130 }
132 glMatrixMode(GL_MODELVIEW);
133 glPushMatrix();
134 glTranslatef(0, 40 * t - 20, 0);
136 glBegin(GL_QUADS);
137 glColor4f(0.7, 0.2, 0.1, t);
138 glVertex2f(0, -10);
139 glVertex2f(xsz, -10);
140 glVertex2f(xsz, 30);
141 glVertex2f(0, 30);
142 glEnd();
144 glTranslatef(20, 0, 0);
146 glColor4f(1, 1, 1, t);
147 dtx_string(text);
149 glPopMatrix();
150 }
153 static void draw_text(void)
154 {
155 int i;
156 dtx_use_font(font, 32);
158 glPushAttrib(GL_ENABLE_BIT);
159 glEnable(GL_BLEND);
161 for(i=0; events[i].name; i++) {
162 caption(events[i].caption, events[i].ev);
163 }
165 glPopAttrib();
166 }
168 static void reshape(int x, int y)
169 {
170 xsz = x;
171 ysz = y;
173 glViewport(0, 0, x, y);
174 glMatrixMode(GL_PROJECTION);
175 glLoadIdentity();
176 glOrtho(0, x, y, 0, -1, 1);
177 }