Browse Source

x11_random: improvement to the worley noise algorithm and fix bugs

Douglas Andreani 5 years ago
parent
commit
83e96bee5e
2 changed files with 40 additions and 24 deletions
  1. 5 3
      x11-random/Makefile
  2. 35 21
      x11-random/worley_noise.c

+ 5 - 3
x11-random/Makefile

@@ -5,12 +5,14 @@ OBJ=$(OUT)/obj
 OBJS=$(OBJ)/worley_noise.o
 
 
-all: $(OBJS)
+all: bin/worley_noise
+
+bin/worley_noise: $(OBJS)
 	$(CC) $(CFLAGS) $(OBJS) -o $(OUT)/worley_noise
 
-bin/obj/worley_noise.o:
+bin/obj/worley_noise.o: worley_noise.c
 	$(CC) $(CFLAGS) -c worley_noise.c -o $(OBJ)/worley_noise.o
 
 clean:
-	rm bin/* bin/obj/*
+	find ./bin -type f -exec rm {} \;
 

+ 35 - 21
x11-random/worley_noise.c

@@ -8,7 +8,7 @@
 #define HEIGHT 720
 #define SEED 200
 
-#define POINTS 20
+#define POINTS 15
 
 static int seed_offset = 1;
 
@@ -71,32 +71,44 @@ void worley_noise(Display * di, Window wi, GC gc)
 	struct point p[POINTS];
 
 
-	for (x = 0; x < WIDTH; x++) {
-		for (y = 0; y < HEIGHT; y++) {
-			c = (int) (rand_0_1() * 255);
-			c = (c << 16) | (c << 8) | c;
-			noise_space[x][y] = c;
-		}
-	}
-
 	for (i = 0; i < POINTS; i++) {
 		p[i].x = (int) (rand_0_1() * WIDTH);
 		p[i].y = (int) (rand_0_1() * HEIGHT);
 	}
+
 	for (x = 0; x < WIDTH; x++) {
 		for (y = 0; y < HEIGHT; y++) {
 			for (i = 0; i < POINTS; i++) {
 				dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
 				sort(dist);
 			}
-			c = map(dist[0], 0, WIDTH / 4, 255, 20);
+			int d1 = map(dist[0], 0, WIDTH/2, 200, 0);
+			int d2 = map(dist[1], 0, WIDTH/2, 200, 0);
+			int d3 = map(dist[2], 0, WIDTH/2, 200, 0);
+			
+			/* 0x000000 rgb value where 
+			 * 16 r
+			 *  8 g
+			 *  4 b*/
+			c = (d1 << 16) + (d2 << 8) + d3;
+			noise_space[x][y] = c;
+		}
+	}
 
-			c = (c << 16) | (c << 8) | c;
-			draw_pixel(di, wi, gc, x, y, c);
+	for (x = 0; x < WIDTH; x++) {
+		for (y = 0; y < HEIGHT; y++) {
+			draw_pixel(di, wi, gc, x, y, noise_space[x][y]);
 		}
 	}
 }
 
+void redraw(Display * di, Window wi, GC gc) {
+	
+	XClearWindow(di, wi);
+	worley_noise(di, wi, gc);
+	XFlush(di);
+}
+
 int main()
 {
 	Display *di = XOpenDisplay(getenv("DISPLAY"));
@@ -125,21 +137,23 @@ int main()
 	XSelectInput(di, wi, KeyPressMask | ExposureMask);
 	XEvent ev;
 	int quit = 0;
-	int z = 50;
 	while (!quit) {
-		int a = XNextEvent(di, &ev);
-		if (ev.type == KeyPress) {
-			printf("KeyPress %x\n", ev.xkey.keycode);
-			quit = 1;
-		}
-		if (ev.type == Expose) {
-			worley_noise(di, wi, gc);
+		if (XPending(di) > 0) {
+			int a = XNextEvent(di, &ev);
+			if (ev.type == KeyPress) {
+				printf("KeyPress %x\n", ev.xkey.keycode);
+				if (ev.xkey.keycode == 0x18)
+					quit = 1;
+			}
+			if (ev.type == Expose) {
+				redraw(di, wi, gc);
+			}
 		}
+		redraw(di, wi, gc);
 	}
 
 	XFreeGC(di, gc);
 	XDestroyWindow(di, wi);
 	XCloseDisplay(di);
 	return (EXIT_SUCCESS);
-
 }