10 /* APPROX. 170 FPS Minimum */
12 #define BG_FILENAME "data/grise.png"
13 #define GROBJ_01_FILENAME "data/grobj_01.png"
15 #define BB_SIZE 512 /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
17 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
18 #define PIXEL_PADDING 32
20 #define MIN_SCROLL PIXEL_PADDING
21 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
23 #define FAR_SCROLL_SPEED 50.0f
24 #define NEAR_SCROLL_SPEED 400.0f
26 #define HORIZON_HEIGHT 100
27 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
29 #define NORMALMAP_SCANLINE 372
31 static int init(void);
32 static void destroy(void);
33 static void start(long trans_time);
34 static void stop(long trans_time);
35 static void draw(void);
37 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount);
38 static void processNormal();
39 static void initScrollTables();
40 static void updateScrollTables(float dt);
42 static void rleEncode(unsigned char *pixels, unsigned int w, unsigned int h);
44 static unsigned short *background = 0;
45 static unsigned int backgroundW = 0;
46 static unsigned int backgroundH = 0;
48 static unsigned int lastFrameTime = 0;
49 static float lastFrameDuration = 0.0f;
51 static short *displacementMap;
53 static unsigned short *backBuffer;
55 static float scrollScaleTable[REFLECTION_HEIGHT];
56 static float scrollTable[REFLECTION_HEIGHT];
57 static int scrollTableRounded[REFLECTION_HEIGHT];
58 static int scrollModTable[REFLECTION_HEIGHT];
59 static float nearScrollAmount = 0.0f;
61 static struct screen scr = {
70 struct screen *grise_screen(void)
78 unsigned char *reflectedObject;
79 int reflectedObjectW, reflectedObjectH;
81 /* Allocate back buffer */
82 backBuffer = (unsigned short*) malloc(BB_SIZE * BB_SIZE * sizeof(unsigned short));
84 /* grise.png contains the background (horizon), baked reflection and normalmap for displacement */
85 if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
86 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
90 /* Convert to 16bpp */
91 convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
93 /* Load reflected objects */
94 if (!(reflectedObject = img_load_pixels(GROBJ_01_FILENAME, &reflectedObjectW, &reflectedObjectH, IMG_FMT_GREY8))) {
95 fprintf(stderr, "failed to load image " GROBJ_01_FILENAME "\n");
99 rleEncode(reflectedObject, reflectedObjectW, reflectedObjectH);
101 img_free_pixels(reflectedObject);
114 static void destroy(void)
119 img_free_pixels(background);
122 static void start(long trans_time)
124 lastFrameTime = time_msec;
127 static void stop(long trans_time)
131 static void draw(void)
133 int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
134 unsigned short *dst = backBuffer + PIXEL_PADDING;
135 unsigned short *src = background + scroll;
141 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
142 lastFrameTime = time_msec;
144 /* First, render the horizon */
145 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
146 memcpy(dst, src, fb_width * 2);
151 /* Create scroll opffsets for all scanlines of the normalmap */
152 updateScrollTables(lastFrameDuration);
154 /* Then, render the reflection under the horizon */
155 /* dst is already in place */
156 src = background + HORIZON_HEIGHT * backgroundW;
157 dispScanline = displacementMap;
158 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
159 for (i = 0; i < fb_width; i++) {
160 d = dispScanline[(i + scrollTableRounded[scanline]) % scrollModTable[scanline]];
161 *dst++ = src[i + scroll + d];
164 dst += BB_SIZE - fb_width;
165 dispScanline += backgroundW;
168 /* Blit effect to framebuffer */
169 src = backBuffer + PIXEL_PADDING;
171 for (scanline = 0; scanline < fb_height; scanline++) {
172 memcpy(dst, src, fb_width * 2);
178 /* src and dst can be the same */
179 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
183 *dst16++ = ((p << 8) & 0xF800) /* R */
184 | ((p >> 5) & 0x07E0) /* G */
185 | ((p >> 19) & 0x001F); /* B */
190 /* Normal map preprocessing */
191 /* Scale normal with depth and unpack R component (horizontal component) */
192 static void processNormal() {
196 short maxDisplacement = 0;
197 short minDisplacement = 256;
200 unsigned int *normalmap = (unsigned int*)background;
201 normalmap += NORMALMAP_SCANLINE * backgroundW;
202 dst = (unsigned short*)normalmap;
203 displacementMap = (short*)dst;
204 dst2 = displacementMap;
206 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
207 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
208 for (i = 0; i < backgroundW; i++) {
209 x = (int)(i * scrollScaleTable[scanline] + 0.5f);
210 if (x < backgroundW) {
211 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
212 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
213 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
219 normalmap += backgroundW;
222 if (maxDisplacement == minDisplacement) {
223 printf("Warning: grise normalmap fucked up\n");
227 /* Second pass - subtract half maximum displacement to displace in both directions */
228 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
229 for (i = 0; i < backgroundW; i++) {
230 /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
231 *dst2 = 2 * MIN_SCROLL * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MIN_SCROLL;
232 *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
238 static float distanceScale(int scanline) {
240 farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
241 t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
242 return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
245 static void initScrollTables() {
247 for (i = 0; i < REFLECTION_HEIGHT; i++) {
248 scrollScaleTable[i] = distanceScale(i);
249 scrollTable[i] = 0.0f;
250 scrollTableRounded[i] = 0;
255 static void updateScrollTables(float dt) {
258 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
259 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
261 for (i = 0; i < REFLECTION_HEIGHT; i++) {
262 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
263 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
267 static void rleEncode(unsigned char *pixels, unsigned int w, unsigned int h) {
272 for (scanline = 0; scanline < h; scanline++) {
273 for (i = 0; i < w; i++) {