+/* Normal map preprocessing */
+/* Scale normal with depth and unpack R component (horizontal component) */
+static void processNormal() {
+ int scanline;
+ unsigned int *normalmap = (unsigned int*)background;
+ normalmap += NORMALMAP_SCANLINE * backgroundW;
+ unsigned short *dst = normalmap;
+ displacementMap = (short*)dst;
+ short *dst2 = displacementMap;
+ float scale;
+ int i;
+ int x;
+ short maxDisplacement = 0;
+ short minDisplacement = 256;
+
+ for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
+ scale = 2.0f - (float)scanline / ((float)REFLECTION_HEIGHT - 1);
+ scrollModTable[scanline] = (int) (backgroundW / scale + 0.5f);
+ for (i = 0; i < backgroundW; i++) {
+ x = (int)(i * scale + 0.5f);
+ if (x < background) {
+ *dst = (unsigned short)normalmap[x] & 0xFF;
+ if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
+ if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
+ } else {
+ *dst = 0;
+ }
+ dst++;
+ }
+ normalmap += backgroundW;
+ }
+
+ if (maxDisplacement == minDisplacement) {
+ printf("Warning: grise normalmap fucked up\n");
+ return;
+ }
+
+ /* Second pass - subtract half maximum displacement to displace in both directions */
+ for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
+ for (i = 0; i < backgroundW; i++) {
+ /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
+ *dst2 = 2 * MIN_SCROLL * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MIN_SCROLL;
+ dst2++;
+ }
+ }
+}
+
+static void initScrollTables() {
+ int i = 0;
+ float scrollSpeed = FAR_SCROLL_SPEED;
+ float speedIncrement = (NEAR_SCROLL_SPEED - FAR_SCROLL_SPEED) / ((float) (REFLECTION_HEIGHT - 1));
+ for (i = 0; i < REFLECTION_HEIGHT; i++) {
+ scrollSpeedTable[i] = scrollSpeed;
+ scrollSpeed += speedIncrement;
+ scrollTable[i] = 0.0f;
+ scrollTableRounded[i] = 0;
+ }
+}
+