generalized pixel format handling in 3d pipeline
[bootcensus] / src / census / census.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <math.h>
4 #include "census.h"
5 #include "logo.h"
6 #include "timer.h"
7 #include "3dgfx.h"
8 #include "panic.h"
9
10 static void draw_disc(float x, float y, float rad, int sub);
11 static void draw_fatline(float x0, float y0, float x1, float y1, float rad);
12
13 static int nverts = 256;
14 static long start_time;
15
16 #define LOOPTIME        1.45f
17
18 void init_census(void *pixels, int xsz, int ysz)
19 {
20         float aspect = (float)xsz / (float)ysz;
21
22         init_logo(0);
23
24         fb_pixels = pixels;
25         fb_width = xsz;
26         fb_height = ysz;
27
28         g3d_init();
29         g3d_framebuffer(xsz, ysz, fb_pixels);
30         g3d_polygon_mode(G3D_FLAT);
31
32         g3d_viewport(0, 0, xsz, ysz);
33         g3d_matrix_mode(G3D_PROJECTION);
34         g3d_load_identity();
35         g3d_scale(1.0f / aspect , 1.0f, 1.0f);
36
37         start_time = TICKS_TO_MSEC(nticks);
38 }
39
40 void draw_census(void)
41 {
42         int i;
43         long msec = TICKS_TO_MSEC(nticks);
44         float t = (float)msec / 1000.0f;
45         float a[2], b[2], dt;
46         float anim, alpha, center_alpha;
47
48         memset(fb_pixels, 0x11, fb_width * fb_height * 4);
49
50         anim = fmod(t / 6.0f, LOOPTIME);
51         alpha = 1.0f - ((anim - (LOOPTIME - 0.075)) / 0.06f);
52         if(alpha < 0.0f) alpha = 0.0f;
53         if(alpha > 1.0f) alpha = 1.0f;
54
55         dt = (anim > 1.0f ? 1.0f : anim) / (float)(nverts - 1);
56
57         g3d_color4f(1, 1, 1, alpha);
58         for(i=0; i<nverts-1; i++) {
59                 float t0 = (float)i * dt;
60                 float t1 = (float)(i + 1) * dt;
61                 eval_logo(a, t0);
62                 eval_logo(b, t1);
63                 draw_fatline(a[0], a[1], b[0], b[1], 0.02);
64         }
65
66         if(anim > 0.0f) {
67                 eval_logo(a, 0);
68                 draw_disc(a[0], a[1], 0.05, 22);
69         }
70         if(anim >= 1.0f) {
71                 eval_logo(b, 1);
72                 draw_disc(b[0], b[1], 0.05, 22);
73         }
74
75
76         center_alpha = 2.0 * (anim - 1.0f) / (LOOPTIME - 1.0f);
77         if(center_alpha > 0.4) {
78                 g3d_color4f(0.8, 0, 0, center_alpha);
79                 draw_disc(0, 0, 0.14, 30);
80         }
81
82 }
83
84 static void draw_disc(float x, float y, float rad, int sub)
85 {
86         int i;
87         float dt = 1.0f / (float)(sub - 1);
88         float da = dt * M_PI * 2.0;
89
90         g3d_begin(G3D_TRIANGLES);
91         for(i=0; i<sub; i++) {
92                 float theta = i * da;
93
94                 g3d_vertex(x, y, 0);
95                 g3d_vertex(cos(theta) * rad + x, sin(theta) * rad + y, 0);
96                 g3d_vertex(cos(theta + da) * rad + x, sin(theta + da) * rad + y, 0);
97         }
98         g3d_end();
99 }
100
101 static void draw_fatline(float x0, float y0, float x1, float y1, float rad)
102 {
103         float dx, dy, rx, ry, len;
104
105         dx = x1 - x0;
106         dy = y1 - y0;
107         len = sqrt(dx * dx + dy * dy);
108
109         rx = rad * dy / len;
110         ry = -rad * dx / len;
111
112         draw_disc(x0, y0, rad, 12);
113         draw_disc(x1, y1, rad, 12);
114
115         g3d_begin(G3D_QUADS);
116         g3d_vertex(x0 + rx, y0 + ry, 0);
117         g3d_vertex(x1 + rx, y1 + ry, 0);
118         g3d_vertex(x1 - rx, y1 - ry, 0);
119         g3d_vertex(x0 - rx, y0 - ry, 0);
120         g3d_end();
121 }
122