Working on bump effect. I have more ideas for it, the way I made this, the lightmap...
[dosdemo] / src / bump.c
1 // Bump effect (not moving yet of course, I have many ideas on this to commit before it's ready)
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <math.h>
7
8 #include "demo.h"
9 #include "screen.h"
10 #include "tinyfps.h"
11
12 static int init(void);
13 static void destroy(void);
14 static void start(long trans_time);
15 static void stop(long trans_time);
16 static void draw(void);
17
18 static struct screen scr = {
19         "bump",
20         init,
21         destroy,
22         start,
23         stop,
24         draw
25 };
26
27 static struct point {
28         int x, y;
29 };
30
31 #define LIGHT_WIDTH 128
32 #define LIGHT_HEIGHT LIGHT_WIDTH
33
34 static unsigned long startingTime;
35
36 static unsigned char *heightmap;
37 static unsigned short *lightmap;
38 static int *bumpOffset;
39
40 static unsigned short *lightR;
41 static unsigned short *lightG;
42 static unsigned short *lightB;
43 static struct point pointR, pointG, pointB;
44
45 //#define FUNKY_COLORS
46
47
48 struct screen *bump_screen(void)
49 {
50         return &scr;
51 }
52
53 static int init(void)
54 {
55         int i, j, x, y, c, r, g, b;
56
57         const int numBlurs = 3;
58         const int lightRadius = LIGHT_WIDTH / 2;
59
60         const int lightSize = LIGHT_WIDTH * LIGHT_HEIGHT;
61         const int fb_size = fb_width * fb_height;
62
63         // Just some parameters to temporary test the colors of 3 lights
64         #ifdef FUNKY_COLORS
65                 // I see some artifacts if I mix channels, not sure if ORing is fine
66                 const float r0 = 1.0f, g0 = 0.6f, b0 = 0.0f;
67                 const float r1 = 0.5f, g1 = 1.0f, b1 = 0.2f;
68                 const float r2 = 0.6f, g2 = 0.4f, b2 = 1.0f;
69         #else
70                 // if every light uses it's own channel bits, it's better
71                 const float r0 = 1.0f, g0 = 0.0f, b0 = 0.0f;
72                 const float r1 = 0.0f, g1 = 1.0f, b1 = 0.0f;
73                 const float r2 = 0.0f, g2 = 0.0f, b2 = 1.0f;
74         #endif
75
76         initFpsFonts();
77
78         heightmap = malloc(sizeof(*heightmap) * fb_size);
79         lightmap = malloc(sizeof(*lightmap) * fb_size);
80         bumpOffset = malloc(sizeof(*bumpOffset) * fb_size);
81
82         lightR = malloc(sizeof(*lightR) * lightSize);
83         lightG = malloc(sizeof(*lightG) * lightSize);
84         lightB = malloc(sizeof(*lightB) * lightSize);
85
86         memset(lightmap, 0, sizeof(*lightmap) * fb_size);
87         memset(bumpOffset, 0, sizeof(*bumpOffset) * fb_size);
88
89         // Create random junk
90         for (i = 0; i < fb_size; i++)
91                 heightmap[i] = rand() & 255;
92
93         // Blur to smooth
94         for (j = 0; j < numBlurs; j++)
95                 for (i = 0; i < fb_size; i++)
96                         heightmap[i] = (heightmap[abs((i - 1) % fb_size)] + heightmap[abs((i + 1) % fb_size)] + heightmap[abs((i - fb_width) % fb_size)] + heightmap[abs((i + fb_width) % fb_size)]) >> 2;
97
98         // Inclination precalculation
99         i = 0;
100         for (y = 0; y < fb_height; y++)
101         {
102                 for (x = 0; x < fb_width; x++)
103                 {
104                         const float offsetPower = 2.0f;
105                         int dx, dy, xp, yp;
106
107                         dx = (int)((heightmap[i] - heightmap[i + 1]) * offsetPower);
108                         dy = (int)((heightmap[i] - heightmap[i + fb_width]) * offsetPower);
109
110                         xp = x + dx;
111                         if (xp < 0) xp = 0;
112                         if (xp > fb_width - 1) xp = fb_width - 1;
113
114                         yp = y + dy;
115                         if (yp < 0) yp = 0;
116                         if (yp > fb_height - 1) yp = fb_height - 1;
117
118                         bumpOffset[i++] = yp * fb_width + xp;
119                 }
120         }
121
122         // Generate three lights
123         i = 0;
124         for (y = 0; y < LIGHT_HEIGHT; y++)
125         {
126                 int yc = y - (LIGHT_HEIGHT / 2);
127                 for (x = 0; x < LIGHT_WIDTH; x++)
128                 {
129                         int xc = x - (LIGHT_WIDTH / 2);
130                         float d = (float)sqrt(xc * xc + yc * yc);
131                         float invDist = ((float)lightRadius - (float)sqrt(xc * xc + yc * yc)) / (float)lightRadius;
132                         if (invDist < 0.0f) invDist = 0.0f;
133
134                         c = (int)(invDist * 63);
135                         r = c >> 1;
136                         g = c;
137                         b = c >> 1;
138
139                         lightR[i] = ((int)(r * r0) << 11) | ((int)(g * g0) << 5) | (int)(b * b0);
140                         lightG[i] = ((int)(r * r1) << 11) | ((int)(g * g1) << 5) | (int)(b * b1);
141                         lightB[i] = ((int)(r * r2) << 11) | ((int)(g * g2) << 5) | (int)(b * b2);
142                         i++;
143                 }
144         }
145
146         return 0;
147 }
148
149 static void destroy(void)
150 {
151 }
152
153 static void start(long trans_time)
154 {
155         startingTime = time_msec;
156 }
157
158 static void stop(long trans_time)
159 {
160 }
161
162
163 static void renderLight(struct point *p, unsigned short *light)
164 {
165         // Check for boundaries is missing atm, will add soon
166         int x, y;
167         unsigned short *dst = (unsigned short*)lightmap + p->y * fb_width + p->x;
168         for (y = 0; y < LIGHT_HEIGHT; y++)
169         {
170                 for (x = 0; x < LIGHT_WIDTH; x++)
171                 {
172                         *dst++ |= *light++;
173                 }
174                 dst += fb_width - LIGHT_WIDTH;
175         }
176 }
177
178
179 static void renderLights()
180 {
181         memset(lightmap, 0, fb_width * fb_height * sizeof(*lightmap));
182         // I will later delete only the regions of lights to speed up
183
184         renderLight(&pointR, lightR);
185         renderLight(&pointG, lightG);
186         renderLight(&pointB, lightB);
187 }
188
189 static void animateLights()
190 {
191         struct point center;
192         float dt = (float)(time_msec - startingTime) / 1000.0f;
193
194         center.x = (fb_width >> 1) - (LIGHT_WIDTH / 2);
195         center.y = (fb_height >> 1) - (LIGHT_HEIGHT / 2);
196
197         pointR.x = center.x + sin(1.2f * dt) * 64.0f;
198         pointR.y = center.y + sin(0.8f * dt) * 48.0f;
199
200         pointG.x = center.x + sin(1.5f * dt) * 56.0f;
201         pointG.y = center.y + sin(1.1f * dt) * 42.0f;
202
203         pointB.x = center.x + sin(2.0f * dt) * 80.0f;
204         pointB.y = center.y + sin(1.3f * dt) * 46.0f;
205 }
206
207 static void renderBump(unsigned short *vram)
208 {
209         int i;
210         for (i = 0; i < fb_width * fb_height; i++)
211         {
212                 unsigned short c = lightmap[bumpOffset[i]];
213                 *vram++ = c;
214         }
215 }
216
217 static void draw(void)
218 {
219         animateLights();
220         renderLights();
221         renderBump((unsigned short*)vmem_back);
222
223         drawFps((unsigned short*)vmem_back);
224
225         swap_buffers(0);
226 }