vpost_plugins
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/hullsdr/hullsdr_post.c Wed Oct 19 06:23:32 2011 +0300 1.3 @@ -0,0 +1,177 @@ 1.4 +#include <math.h> 1.5 +#include <assert.h> 1.6 +#include <GL/glut.h> 1.7 +#include <GL/freeglut_ext.h> 1.8 +#include <imago2.h> 1.9 +#include <drawtext.h> 1.10 +#include <dsys.h> 1.11 + 1.12 +static void disp(void); 1.13 +static void draw_text(void); 1.14 +static void reshape(int x, int y); 1.15 + 1.16 +static struct img_pixmap *curfrm; 1.17 +static int nfrm; 1.18 +static int xsz = -1, ysz; 1.19 +static float tsec; 1.20 + 1.21 +static struct dsys_demo *script; 1.22 + 1.23 +static struct dtx_font *font; 1.24 + 1.25 +static struct { 1.26 + const char *name, *caption; 1.27 + struct dsys_event *ev; 1.28 +} events[] = { 1.29 + {"diffuse", "diffuse only - 1 light", 0}, 1.30 + {"diffuse2", "diffuse only - 2 lights", 0}, 1.31 + {"vmanip", "vertex manipulation", 0}, 1.32 + {"2tone", "two-tone paint", 0}, 1.33 + {"specular2", "full phong - 2 lights", 0}, 1.34 + {"celshade", "cel shading", 0}, 1.35 + {"sphmap", "reflection - spherical env. map", 0}, 1.36 + {"cubemap", "reflection - cubic env. map", 0}, 1.37 + {"refract", "relfection & refraction - cubic env. map w/fresnel", 0}, 1.38 + {"distort1", "image distortion", 0}, 1.39 + {"blur", "blur", 0}, 1.40 + {"distort2", "image distortion (again)", 0}, 1.41 + {0, 0, 0} 1.42 +}; 1.43 + 1.44 + 1.45 +int init(void) 1.46 +{ 1.47 + int i, argc = 1; 1.48 + char *argv[] = {"./vpost", 0}; 1.49 + 1.50 + if(!(font = dtx_open_font("linux-libertine.ttf", 32))) { 1.51 + fprintf(stderr, "failed to open font\n"); 1.52 + return -1; 1.53 + } 1.54 + 1.55 + if(!(script = dsys_open("script"))) { 1.56 + fprintf(stderr, "failed to open script\n"); 1.57 + return -1; 1.58 + } 1.59 + 1.60 + for(i=0; events[i].name; i++) { 1.61 + events[i].ev = dsys_event(script, events[i].name); 1.62 + } 1.63 + 1.64 + glutInit(&argc, argv); 1.65 + glutInitWindowSize(32, 32); 1.66 + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 1.67 + 1.68 + glutCreateWindow("foo"); 1.69 + 1.70 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.71 + 1.72 + glutDisplayFunc(disp); 1.73 + glutReshapeFunc(reshape); 1.74 + return 0; 1.75 +} 1.76 + 1.77 +void shutdown(void) 1.78 +{ 1.79 + printf("shutdown called\n"); 1.80 +} 1.81 + 1.82 +int process(struct img_pixmap *frame, int frm_num, float sec) 1.83 +{ 1.84 + curfrm = frame; 1.85 + nfrm = frm_num; 1.86 + tsec = sec; 1.87 + 1.88 + if(frame->width != xsz || frame->height != ysz) { 1.89 + glutReshapeWindow(frame->width, frame->height); 1.90 + } 1.91 + glutPostRedisplay(); 1.92 + 1.93 + while(curfrm) { 1.94 + glutMainLoopEvent(); 1.95 + } 1.96 + return 0; 1.97 +} 1.98 + 1.99 +static void disp(void) 1.100 +{ 1.101 + assert(curfrm); 1.102 + glClear(GL_COLOR_BUFFER_BIT); 1.103 + 1.104 + dsys_update(script, dsys_msec_to_dtime(nfrm)); 1.105 + 1.106 + glDrawPixels(xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE, curfrm->pixels); 1.107 + 1.108 + draw_text(); 1.109 + 1.110 + glFlush(); 1.111 + glReadPixels(0, 0, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE, curfrm->pixels); 1.112 + 1.113 + glutSwapBuffers(); 1.114 + curfrm = 0; 1.115 + 1.116 + assert(glGetError() == GL_NO_ERROR); 1.117 +} 1.118 + 1.119 +static float fade(float x) 1.120 +{ 1.121 + x = 1.0 - (cos((x) * M_PI * 2.0) * 0.5 + 0.5); 1.122 + x *= 10.0f; 1.123 + 1.124 + return x > 1.0 ? 1.0 : x; 1.125 +} 1.126 + 1.127 +static void caption(const char *text, struct dsys_event *ev) 1.128 +{ 1.129 + float t = fade(dsys_event_value(ev)); 1.130 + 1.131 + if(t < (1.0f / 255.0f)) { 1.132 + return; 1.133 + } 1.134 + 1.135 + glMatrixMode(GL_MODELVIEW); 1.136 + glPushMatrix(); 1.137 + glTranslatef(0, 40 * t - 20, 0); 1.138 + 1.139 + glBegin(GL_QUADS); 1.140 + glColor4f(0.7, 0.2, 0.1, t); 1.141 + glVertex2f(0, -10); 1.142 + glVertex2f(xsz, -10); 1.143 + glVertex2f(xsz, 30); 1.144 + glVertex2f(0, 30); 1.145 + glEnd(); 1.146 + 1.147 + glTranslatef(20, 0, 0); 1.148 + 1.149 + glColor4f(1, 1, 1, t); 1.150 + dtx_string(text); 1.151 + 1.152 + glPopMatrix(); 1.153 +} 1.154 + 1.155 + 1.156 +static void draw_text(void) 1.157 +{ 1.158 + int i; 1.159 + dtx_use_font(font, 32); 1.160 + 1.161 + glPushAttrib(GL_ENABLE_BIT); 1.162 + glEnable(GL_BLEND); 1.163 + 1.164 + for(i=0; events[i].name; i++) { 1.165 + caption(events[i].caption, events[i].ev); 1.166 + } 1.167 + 1.168 + glPopAttrib(); 1.169 +} 1.170 + 1.171 +static void reshape(int x, int y) 1.172 +{ 1.173 + xsz = x; 1.174 + ysz = y; 1.175 + 1.176 + glViewport(0, 0, x, y); 1.177 + glMatrixMode(GL_PROJECTION); 1.178 + glLoadIdentity(); 1.179 + glOrtho(0, x, y, 0, -1, 1); 1.180 +}