Mercurial > mplayer.hg
comparison libvo/matrixview.c @ 30140:12a49e9ca30e
Finally add matrixview vo.
Heavily cleaned up/fixed etc. by me, improvements are still possible though.
Patch originally by Pigeon <pigeon at pigeond.net>
author | reimar |
---|---|
date | Sun, 03 Jan 2010 14:54:51 +0000 |
parents | |
children | 4e0755091f55 |
comparison
equal
deleted
inserted
replaced
30139:47b031706571 | 30140:12a49e9ca30e |
---|---|
1 /* | |
2 * Copyright (C) 2003 Alex Zolotov <nightradio@knoppix.ru> | |
3 * Mucked with by Tugrul Galatali <tugrul@galatali.com> | |
4 * | |
5 * MatrixView is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 2 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * MatrixView is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
18 | |
19 /** | |
20 * Ported to an MPlayer video out plugin by Pigeon <pigeon at pigeond.net> | |
21 * August 2006 | |
22 */ | |
23 | |
24 #include <math.h> | |
25 #include <stdio.h> | |
26 #include <stdint.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include "gl_common.h" | |
30 #include "matrixview.h" | |
31 #include "matrixview_font.h" | |
32 | |
33 static float matrix_contrast = 1.5; | |
34 static float matrix_brightness = 1.0; | |
35 | |
36 // Settings for our light. Try playing with these (or add more lights). | |
37 static float Light_Ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f }; | |
38 static float Light_Diffuse[] = { 1.2f, 1.2f, 1.2f, 1.0f }; | |
39 static float Light_Position[] = { 2.0f, 2.0f, 0.0f, 1.0f }; | |
40 | |
41 static const uint8_t flare[4][4] = { | |
42 { 0, 0, 0, 0}, | |
43 { 0, 180, 0, 0}, | |
44 { 0, 0, 0, 0}, | |
45 { 0, 0, 0, 0} | |
46 }; | |
47 | |
48 #define MAX_TEXT_X 0x4000 | |
49 #define MAX_TEXT_Y 0x4000 | |
50 static int text_x = 0; | |
51 static int text_y = 0; | |
52 #define _text_x text_x/2 | |
53 #define _text_y text_y/2 | |
54 | |
55 // Scene position | |
56 #define Z_Off -128.0f | |
57 #define Z_Depth 8 | |
58 | |
59 static uint8_t *speed; | |
60 static uint8_t *text; | |
61 static uint8_t *text_light; | |
62 static float *text_depth; | |
63 | |
64 static float *bump_pic; | |
65 | |
66 static void draw_char(int num, float light, float x, float y, float z) | |
67 { | |
68 float tx, ty; | |
69 int num2, num3; | |
70 | |
71 num &= 63; | |
72 //light = light / 255; //light=7-light;num+=(light*60); | |
73 light = light / 255 * matrix_brightness; | |
74 num2 = num / 10; | |
75 num3 = num - (num2 * 10); | |
76 ty = (float)num2 / 7; | |
77 tx = (float)num3 / 10; | |
78 Normal3f(0.0f, 0.0f, 1.0f); // Needed for lighting | |
79 Color4f(0.0, 1.0, 0.0, light); // Basic polygon color | |
80 | |
81 TexCoord2f(tx, ty); | |
82 Vertex3f(x, y, z); | |
83 TexCoord2f(tx + 0.1, ty); | |
84 Vertex3f(x + 1, y, z); | |
85 TexCoord2f(tx + 0.1, ty + 0.166); | |
86 Vertex3f(x + 1, y - 1, z); | |
87 TexCoord2f(tx, ty + 0.166); | |
88 Vertex3f(x, y - 1, z); | |
89 } | |
90 | |
91 static void draw_illuminatedchar(int num, float x, float y, float z) | |
92 { | |
93 float tx, ty; | |
94 int num2, num3; | |
95 | |
96 num2 = num / 10; | |
97 num3 = num - (num2 * 10); | |
98 ty = (float)num2 / 7; | |
99 tx = (float)num3 / 10; | |
100 Normal3f(0.0f, 0.0f, 1.0f); // Needed for lighting | |
101 Color4f(1.0, 1.0, 1.0, .5); // Basic polygon color | |
102 | |
103 TexCoord2f(tx, ty); | |
104 Vertex3f(x, y, z); | |
105 TexCoord2f(tx + 0.1, ty); | |
106 Vertex3f(x + 1, y, z); | |
107 TexCoord2f(tx + 0.1, ty + 0.166); | |
108 Vertex3f(x + 1, y - 1, z); | |
109 TexCoord2f(tx, ty + 0.166); | |
110 Vertex3f(x, y - 1, z); | |
111 } | |
112 | |
113 static void draw_flare(float x, float y, float z) //flare | |
114 { | |
115 Normal3f(0.0f, 0.0f, 1.0f); // Needed for lighting | |
116 Color4f(1.0, 1.0, 1.0, .8); // Basic polygon color | |
117 | |
118 TexCoord2f(0, 0); | |
119 Vertex3f(x - 1, y + 1, z); | |
120 TexCoord2f(0.75, 0); | |
121 Vertex3f(x + 2, y + 1, z); | |
122 TexCoord2f(0.75, 0.75); | |
123 Vertex3f(x + 2, y - 2, z); | |
124 TexCoord2f(0, 0.75); | |
125 Vertex3f(x - 1, y - 2, z); | |
126 } | |
127 | |
128 static void draw_text(uint8_t *pic) | |
129 { | |
130 int x, y; | |
131 int p = 0; | |
132 int c, c_pic; | |
133 int pic_fade = 255; | |
134 | |
135 for (y = _text_y; y > -_text_y; y--) { | |
136 for (x = -_text_x; x < _text_x; x++) { | |
137 c = text_light[p] - (text[p] >> 1); | |
138 c += pic_fade; | |
139 if (c > 255) | |
140 c = 255; | |
141 | |
142 if (pic) { | |
143 // Original code | |
144 //c_pic = pic[p] * matrix_contrast - (255 - pic_fade); | |
145 | |
146 c_pic = (255 - pic[p]) * matrix_contrast - (255 - pic_fade); | |
147 | |
148 if (c_pic < 0) | |
149 c_pic = 0; | |
150 | |
151 c -= c_pic; | |
152 | |
153 if (c < 0) | |
154 c = 0; | |
155 | |
156 bump_pic[p] = (255.0f - c_pic) / (256 / Z_Depth); | |
157 } else { | |
158 bump_pic[p] = Z_Depth; | |
159 } | |
160 | |
161 if (text[p] && c > 10) | |
162 draw_char(text[p] + 1, c, x, y, text_depth[p] + bump_pic[p]); | |
163 | |
164 if (text_depth[p] < 0.1) | |
165 text_depth[p] = 0; | |
166 else | |
167 text_depth[p] /= 1.1; | |
168 | |
169 if (text_light[p] > 128 && text_light[p + text_x] < 10) | |
170 draw_illuminatedchar(text[p] + 1, x, y, text_depth[p] + bump_pic[p]); | |
171 | |
172 p++; | |
173 } | |
174 } | |
175 } | |
176 | |
177 static void draw_flares(void) | |
178 { | |
179 float x, y; | |
180 int p = 0; | |
181 | |
182 for (y = _text_y; y > -_text_y; y--) { | |
183 for (x = -_text_x; x < _text_x; x++) { | |
184 if (text_light[p] > 128 && text_light[p + text_x] < 10) | |
185 draw_flare(x, y, text_depth[p] + bump_pic[p]); | |
186 p++; | |
187 } | |
188 } | |
189 } | |
190 | |
191 static void scroll(double dCurrentTime) | |
192 { | |
193 int a, s, polovina; | |
194 //static double dLastCycle = -1; | |
195 static double dLastMove = -1; | |
196 | |
197 if (dCurrentTime - dLastMove > 1.0 / (text_y / 1.5)) { | |
198 dLastMove = dCurrentTime; | |
199 | |
200 polovina = text_x * text_y / 2; | |
201 s = 0; | |
202 for (a = text_x * text_y + text_x - 1; a >= text_x; a--) { | |
203 if (speed[s]) | |
204 text_light[a] = text_light[a - text_x]; //scroll light table down | |
205 s++; | |
206 if (s >= text_x) | |
207 s = 0; | |
208 } | |
209 memmove(text_light + text_x, text_light, text_x * text_y); | |
210 memset(text_light, 253, text_x); | |
211 | |
212 s = 0; | |
213 for (a = polovina; a < text_x * text_y; a++) { | |
214 if (text_light[a] == 255) | |
215 text_light[s] = text_light[s + text_x] >> 1; //make black bugs in top line | |
216 | |
217 s++; | |
218 | |
219 if (s >= text_x) | |
220 s = 0; | |
221 } | |
222 } | |
223 } | |
224 | |
225 static void make_change(double dCurrentTime) | |
226 { | |
227 int r = rand() % text_x * text_y; | |
228 | |
229 text[r] += 133; //random bugs | |
230 | |
231 r = rand() % (4 * text_x); | |
232 if (r < text_x && text_light[r]) | |
233 text_light[r] = 255; //white bugs | |
234 | |
235 scroll (dCurrentTime); | |
236 } | |
237 | |
238 | |
239 static void make_text(void) | |
240 { | |
241 int a; | |
242 | |
243 for (a = 0; a < text_x * text_y; a++) | |
244 text[a] = rand() >> 8; // avoid the lowest bits of rand() | |
245 | |
246 for (a = 0; a < text_x; a++) | |
247 speed[a] = rand() >= RAND_MAX / 2; | |
248 } | |
249 | |
250 static void ourBuildTextures(void) | |
251 { | |
252 TexImage2D(GL_TEXTURE_2D, 0, 1, 128, 64, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, font_texture); | |
253 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
254 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
255 | |
256 BindTexture(GL_TEXTURE_2D, 1); | |
257 TexImage2D(GL_TEXTURE_2D, 0, 1, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, flare); | |
258 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
259 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
260 | |
261 // Some pretty standard settings for wrapping and filtering. | |
262 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
263 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
264 BindTexture(GL_TEXTURE_2D, 0); | |
265 } | |
266 | |
267 void matrixview_init(int w, int h) | |
268 { | |
269 make_text(); | |
270 | |
271 ourBuildTextures(); | |
272 | |
273 // Color to clear color buffer to. | |
274 ClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
275 | |
276 // Depth to clear depth buffer to; type of test. | |
277 ClearDepth(1.0); | |
278 DepthFunc(GL_LESS); | |
279 | |
280 // Enables Smooth Color Shading; try GL_FLAT for (lack of) fun. | |
281 ShadeModel(GL_SMOOTH); | |
282 | |
283 // Set up a light, turn it on. | |
284 Lightfv(GL_LIGHT1, GL_POSITION, Light_Position); | |
285 Lightfv(GL_LIGHT1, GL_AMBIENT, Light_Ambient); | |
286 Lightfv(GL_LIGHT1, GL_DIFFUSE, Light_Diffuse); | |
287 Enable(GL_LIGHT1); | |
288 | |
289 // A handy trick -- have surface material mirror the color. | |
290 ColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); | |
291 Enable(GL_COLOR_MATERIAL); | |
292 | |
293 // Allow adjusting of texture color via glColor | |
294 TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | |
295 | |
296 matrixview_reshape(w, h); | |
297 } | |
298 | |
299 | |
300 void matrixview_reshape(int w, int h) | |
301 { | |
302 Viewport(0, 0, w, h); | |
303 | |
304 MatrixMode(GL_PROJECTION); | |
305 LoadIdentity(); | |
306 Frustum(-_text_x, _text_x, -_text_y, _text_y, -Z_Off - Z_Depth, -Z_Off); | |
307 | |
308 MatrixMode(GL_MODELVIEW); | |
309 } | |
310 | |
311 | |
312 void matrixview_draw(int w, int h, double currentTime, float frameTime, uint8_t *data) | |
313 { | |
314 Enable(GL_BLEND); | |
315 Enable(GL_TEXTURE_2D); | |
316 | |
317 Disable(GL_LIGHTING); | |
318 BlendFunc(GL_SRC_ALPHA, GL_ONE); | |
319 Disable(GL_DEPTH_TEST); | |
320 | |
321 MatrixMode(GL_MODELVIEW); | |
322 LoadIdentity(); | |
323 Translated(0.0f, 0.0f, Z_Off); | |
324 | |
325 // Clear the color and depth buffers. | |
326 Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
327 | |
328 // OK, let's start drawing our planer quads. | |
329 Begin(GL_QUADS); | |
330 draw_text(data); | |
331 End(); | |
332 | |
333 BindTexture(GL_TEXTURE_2D, 1); | |
334 Begin(GL_QUADS); | |
335 draw_flares(); | |
336 End(); | |
337 BindTexture(GL_TEXTURE_2D, 0); | |
338 | |
339 make_change(currentTime); | |
340 | |
341 LoadIdentity(); | |
342 MatrixMode(GL_PROJECTION); | |
343 } | |
344 | |
345 void matrixview_contrast_set(float contrast) | |
346 { | |
347 matrix_contrast = contrast; | |
348 } | |
349 | |
350 void matrixview_brightness_set(float brightness) | |
351 { | |
352 matrix_brightness = brightness; | |
353 } | |
354 | |
355 | |
356 void matrixview_matrix_resize(int w, int h) | |
357 { | |
358 int elems; | |
359 free(speed); | |
360 speed = NULL; | |
361 free(text); | |
362 text = NULL; | |
363 free(text_light); | |
364 text_light = NULL; | |
365 free(text_depth); | |
366 text_depth = NULL; | |
367 if (w > MAX_TEXT_X || h > MAX_TEXT_Y) | |
368 return; | |
369 elems = w * (h + 1); | |
370 speed = calloc(w, sizeof(*speed)); | |
371 text = calloc(elems, sizeof(*text)); | |
372 text_light = calloc(elems, sizeof(*text_light)); | |
373 text_depth = calloc(elems, sizeof(*text_depth)); | |
374 bump_pic = calloc(elems, sizeof(*bump_pic)); | |
375 text_x = w; | |
376 text_y = h; | |
377 make_text(); | |
378 } |