Mercurial > mplayer.hg
annotate libvo/matrixview.c @ 30160:f91c628dc701
Refer to MatrixView instead of MPlayer in code taken from MatrixView.
author | diego |
---|---|
date | Mon, 04 Jan 2010 13:05:05 +0000 |
parents | 4e0755091f55 |
children | ab027cdbba31 |
rev | line source |
---|---|
30140 | 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 | |
30160
f91c628dc701
Refer to MatrixView instead of MPlayer in code taken from MatrixView.
diego
parents:
30153
diff
changeset
|
15 * with MatrixView; if not, write to the Free Software Foundation, Inc., |
30140 | 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 | |
30153 | 33 static float matrix_contrast = 1.5; |
30140 | 34 static float matrix_brightness = 1.0; |
35 | |
36 // Settings for our light. Try playing with these (or add more lights). | |
30153 | 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 }; | |
30140 | 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++) { | |
30153 | 137 c = text_light[p] - (text[p] >> 1); |
30140 | 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) | |
30153 | 170 draw_illuminatedchar(text[p] + 1, x, y, |
171 text_depth[p] + bump_pic[p]); | |
30140 | 172 |
173 p++; | |
174 } | |
175 } | |
176 } | |
177 | |
178 static void draw_flares(void) | |
179 { | |
180 float x, y; | |
181 int p = 0; | |
182 | |
183 for (y = _text_y; y > -_text_y; y--) { | |
184 for (x = -_text_x; x < _text_x; x++) { | |
185 if (text_light[p] > 128 && text_light[p + text_x] < 10) | |
30153 | 186 draw_flare(x, y, text_depth[p] + bump_pic[p]); |
30140 | 187 p++; |
188 } | |
189 } | |
190 } | |
191 | |
192 static void scroll(double dCurrentTime) | |
193 { | |
194 int a, s, polovina; | |
195 //static double dLastCycle = -1; | |
196 static double dLastMove = -1; | |
197 | |
198 if (dCurrentTime - dLastMove > 1.0 / (text_y / 1.5)) { | |
199 dLastMove = dCurrentTime; | |
200 | |
201 polovina = text_x * text_y / 2; | |
202 s = 0; | |
203 for (a = text_x * text_y + text_x - 1; a >= text_x; a--) { | |
204 if (speed[s]) | |
205 text_light[a] = text_light[a - text_x]; //scroll light table down | |
206 s++; | |
207 if (s >= text_x) | |
208 s = 0; | |
209 } | |
210 memmove(text_light + text_x, text_light, text_x * text_y); | |
211 memset(text_light, 253, text_x); | |
212 | |
213 s = 0; | |
214 for (a = polovina; a < text_x * text_y; a++) { | |
215 if (text_light[a] == 255) | |
216 text_light[s] = text_light[s + text_x] >> 1; //make black bugs in top line | |
217 | |
218 s++; | |
219 | |
220 if (s >= text_x) | |
221 s = 0; | |
222 } | |
223 } | |
224 } | |
225 | |
226 static void make_change(double dCurrentTime) | |
227 { | |
228 int r = rand() % text_x * text_y; | |
229 | |
230 text[r] += 133; //random bugs | |
231 | |
232 r = rand() % (4 * text_x); | |
233 if (r < text_x && text_light[r]) | |
234 text_light[r] = 255; //white bugs | |
235 | |
236 scroll (dCurrentTime); | |
237 } | |
238 | |
239 | |
240 static void make_text(void) | |
241 { | |
242 int a; | |
243 | |
244 for (a = 0; a < text_x * text_y; a++) | |
245 text[a] = rand() >> 8; // avoid the lowest bits of rand() | |
246 | |
247 for (a = 0; a < text_x; a++) | |
248 speed[a] = rand() >= RAND_MAX / 2; | |
249 } | |
250 | |
251 static void ourBuildTextures(void) | |
252 { | |
30153 | 253 TexImage2D(GL_TEXTURE_2D, 0, 1, 128, 64, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, |
254 font_texture); | |
30140 | 255 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
256 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
257 | |
258 BindTexture(GL_TEXTURE_2D, 1); | |
30153 | 259 TexImage2D(GL_TEXTURE_2D, 0, 1, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, |
260 flare); | |
30140 | 261 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
262 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
263 | |
264 // Some pretty standard settings for wrapping and filtering. | |
265 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
266 TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
267 BindTexture(GL_TEXTURE_2D, 0); | |
268 } | |
269 | |
270 void matrixview_init(int w, int h) | |
271 { | |
272 make_text(); | |
273 | |
274 ourBuildTextures(); | |
275 | |
276 // Color to clear color buffer to. | |
277 ClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
278 | |
279 // Depth to clear depth buffer to; type of test. | |
280 ClearDepth(1.0); | |
281 DepthFunc(GL_LESS); | |
282 | |
283 // Enables Smooth Color Shading; try GL_FLAT for (lack of) fun. | |
284 ShadeModel(GL_SMOOTH); | |
285 | |
286 // Set up a light, turn it on. | |
287 Lightfv(GL_LIGHT1, GL_POSITION, Light_Position); | |
288 Lightfv(GL_LIGHT1, GL_AMBIENT, Light_Ambient); | |
289 Lightfv(GL_LIGHT1, GL_DIFFUSE, Light_Diffuse); | |
290 Enable(GL_LIGHT1); | |
291 | |
292 // A handy trick -- have surface material mirror the color. | |
293 ColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); | |
294 Enable(GL_COLOR_MATERIAL); | |
295 | |
296 // Allow adjusting of texture color via glColor | |
297 TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | |
298 | |
299 matrixview_reshape(w, h); | |
300 } | |
301 | |
302 | |
303 void matrixview_reshape(int w, int h) | |
304 { | |
305 Viewport(0, 0, w, h); | |
306 | |
307 MatrixMode(GL_PROJECTION); | |
308 LoadIdentity(); | |
309 Frustum(-_text_x, _text_x, -_text_y, _text_y, -Z_Off - Z_Depth, -Z_Off); | |
310 | |
311 MatrixMode(GL_MODELVIEW); | |
312 } | |
313 | |
314 | |
30153 | 315 void matrixview_draw(int w, int h, double currentTime, float frameTime, |
316 uint8_t *data) | |
30140 | 317 { |
318 Enable(GL_BLEND); | |
319 Enable(GL_TEXTURE_2D); | |
320 | |
321 Disable(GL_LIGHTING); | |
322 BlendFunc(GL_SRC_ALPHA, GL_ONE); | |
323 Disable(GL_DEPTH_TEST); | |
324 | |
325 MatrixMode(GL_MODELVIEW); | |
326 LoadIdentity(); | |
327 Translated(0.0f, 0.0f, Z_Off); | |
328 | |
329 // Clear the color and depth buffers. | |
330 Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
331 | |
332 // OK, let's start drawing our planer quads. | |
333 Begin(GL_QUADS); | |
334 draw_text(data); | |
335 End(); | |
336 | |
337 BindTexture(GL_TEXTURE_2D, 1); | |
338 Begin(GL_QUADS); | |
339 draw_flares(); | |
340 End(); | |
341 BindTexture(GL_TEXTURE_2D, 0); | |
342 | |
343 make_change(currentTime); | |
344 | |
345 LoadIdentity(); | |
346 MatrixMode(GL_PROJECTION); | |
347 } | |
348 | |
349 void matrixview_contrast_set(float contrast) | |
350 { | |
351 matrix_contrast = contrast; | |
352 } | |
353 | |
354 void matrixview_brightness_set(float brightness) | |
355 { | |
356 matrix_brightness = brightness; | |
357 } | |
358 | |
359 | |
360 void matrixview_matrix_resize(int w, int h) | |
361 { | |
362 int elems; | |
363 free(speed); | |
364 speed = NULL; | |
365 free(text); | |
366 text = NULL; | |
367 free(text_light); | |
368 text_light = NULL; | |
369 free(text_depth); | |
370 text_depth = NULL; | |
371 if (w > MAX_TEXT_X || h > MAX_TEXT_Y) | |
372 return; | |
373 elems = w * (h + 1); | |
374 speed = calloc(w, sizeof(*speed)); | |
375 text = calloc(elems, sizeof(*text)); | |
376 text_light = calloc(elems, sizeof(*text_light)); | |
377 text_depth = calloc(elems, sizeof(*text_depth)); | |
378 bump_pic = calloc(elems, sizeof(*bump_pic)); | |
379 text_x = w; | |
380 text_y = h; | |
381 make_text(); | |
382 } |