+plot(unsigned char *buffer, int x, int y, int r, int g, int b, int a) {
+ int c;
+ if (x < 0 || x >= TEXTURE_SIZE || y < 0 || y >= TEXTURE_SIZE) {
+ return;
+ }
+ c = ((x * TEXTURE_SIZE) + y) * 4;
+ /*printf("(%d,%d)[%d]\n", x, y, c);*/
+ buffer[c + 0] = r;
+ buffer[c + 1] = g;
+ buffer[c + 2] = b;
+ buffer[c + 3] = a;
+}
+
+#if 0
+static void
+plot2(unsigned char *buffer, int x, int y, int r, int g, int b, int a) {
+ int c;
+ if (x < 0 || x >= TEXTURE_SIZE || y < 0 || y >= TEXTURE_SIZE) {
+ return;
+ }
+ c = ((x * TEXTURE_SIZE) + y) * 4;
+ /*printf("(%d,%d)[%d]\n", x, y, c);*/
+ buffer[c + 0] = r;
+ buffer[c + 1] = g;
+ buffer[c + 2] = b;
+ buffer[c + 3] = a;
+
+ if (y + 1 < TEXTURE_SIZE) {
+ buffer[c + 4] = r;
+ buffer[c + 5] = g;
+ buffer[c + 6] = b;
+ buffer[c + 7] = a;
+ }
+
+ if (x + 1 < TEXTURE_SIZE) {
+ c += (TEXTURE_SIZE * 4);
+ buffer[c + 0] = r;
+ buffer[c + 1] = g;
+ buffer[c + 2] = b;
+ buffer[c + 3] = a;
+ if (y + 1 < TEXTURE_SIZE) {
+ buffer[c + 4] = r;
+ buffer[c + 5] = g;
+ buffer[c + 6] = b;
+ buffer[c + 7] = a;
+ }
+ }
+}
+#endif
+
+/* draw geometric shapes to texture */
+/* modifies passed in buffer */
+static void
+draw_shapes (unsigned char *buffer) {
+ int a = 0xff;
+ int x, y, w, h;
+ int i, j;
+ int s;
+ float left, right;
+
+ for (i = 0 ; i < TEXTURE_SIZE * TEXTURE_SIZE * 4 ; i += 4) {
+ buffer[i + 0] = 0x00;
+ buffer[i + 1] = 0x00;
+ buffer[i + 2] = 0x00;
+ buffer[i + 3] = 0xff;
+ }
+
+ for (s = 0 ; s < 25 ; s++) {
+ int shape = random() % 3;
+
+ /* 8 bits */
+ int r = (random() & 0xff);
+ int g = (random() & 0xff);
+ int b = (random() & 0xff);
+
+ switch (shape) {
+ case 0:
+ /* rectangle */
+ x = (random() % TEXTURE_SIZE) - (TEXTURE_SIZE / 4); /* top left */
+ y = (random() % TEXTURE_SIZE) - (TEXTURE_SIZE / 4);
+ w = 10 + random() % (TEXTURE_SIZE / 4); /* size */
+ h = 10 + random() % (TEXTURE_SIZE / 4);
+#ifdef DEBUG
+ printf("Rectangle: (%d, %d)(%d, %d)\n", x, y, w, h);
+#endif
+ if (x < 0) {
+ x = 0;
+ }
+ if (y < 0) {
+ y = 0;
+ }
+ for (i = x ; i < x + w && i < TEXTURE_SIZE; i++) {
+ for (j = y ; j < y + h && j < TEXTURE_SIZE; j++) {
+ plot(buffer, i, j, r, g, b, a);
+ }
+ }
+ break;
+
+ case 1:
+ /* circle */
+ x = random() % TEXTURE_SIZE; /* centre */
+ y = random() % TEXTURE_SIZE;
+ h = 10 + random() % (TEXTURE_SIZE / 8); /* radius */
+#ifdef DEBUG
+ printf("Circle: %d, %d, %d\n", x, y, h);
+#endif
+ for (i = 0 ; i < h ; i++) {
+ int xdist = i * i;
+ for (j = 0 ; j < h ; j++) {
+ int ydist = j * j;
+ /*
+ printf("xdist: %d\n", xdist);
+ printf("ydist: %d\n", ydist);
+ printf("radius: %d\n", h * h);
+ */
+ if ((xdist + ydist) < (h * h)) {
+ plot(buffer, x + i, y + j, r, b, g, a);
+ /* check we haven't already done these */
+ if (j != 0) {
+ plot(buffer, x + i, y - j, r, b, g, a);
+ }
+ if (i != 0) {
+ plot(buffer, x - i, y + j, r, b, g, a);
+ if (j != 0) {
+ plot(buffer, x - i, y - j, r, b, g, a);
+ }
+ }
+ }
+ }
+ }
+ break;
+
+ case 2:
+ /* triangle */
+ x = random() % TEXTURE_SIZE; /* top */
+ y = random() % TEXTURE_SIZE;
+ h = 10 + random() % (TEXTURE_SIZE / 4); /* height */
+#ifdef DEBUG
+ printf("Triangle: %d, %d, %d\n", x, y, h);
+#endif
+ left = x;
+ right = x;
+ for (i = 0 ; i < h ; i++) {
+ for (j = left ; j < right ; j++) {
+ plot(buffer, j, y + i, r, g, b, a);
+ }
+ left -= .5;
+ right += .5;
+ }
+ break;
+ }
+ }
+}
+
+static void
+setup_random_texture (ModeInfo *mi, texture *texture)
+{
+ int width = 0, height = 0;
+ char buf[1024];
+ unsigned char *my_data = NULL;
+#if 0
+ int i, j, c;
+ int style;
+ int r0, g0, b0, a0, r1, g1, b1, a1;
+#endif
+
+#ifdef DEBUG
+ printf("RandomTexture\n");
+#endif
+
+ /* use this texture */
+ glBindTexture(GL_TEXTURE_2D, texture->id);
+
+ clear_gl_error();
+
+ /*
+ * code for various generated patterns - noise, stripes, checks etc.
+ * random geometric shapes looked the best.
+ */
+
+#if 0
+ style = random() & 0x3;
+ r0 = random() & 0xff;
+ g0 = random() & 0xff;
+ b0 = random() & 0xff;
+ a0 = 0xff;
+ r1 = random() & 0xff;
+ g1 = random() & 0xff;
+ b1 = random() & 0xff;
+ a1 = 0xff;
+
+ switch (style) {
+ case 0: /* random */
+ printf("Random0\n");
+ height = width = TEXTURE_SIZE;
+ my_data = (void *)malloc(width * height * 4);
+ for (i = 0 ; i < width ; i += 2) {
+ for (j = 0 ; j < height ; j += 2) {
+ r0 = random() & 0xff;
+ g0 = random() & 0xff;
+ b0 = random() & 0xff;
+ a0 = 0xff;
+ plot2(my_data, i, j, r0, g0, b0, a0);
+ }
+ }
+ break;
+
+ case 1: /* shapes */
+#endif
+#ifdef DEBUG
+ printf("Shapes\n");
+#endif
+ height = width = TEXTURE_SIZE;
+ my_data = (void *)malloc(width * height * 4);
+ draw_shapes(my_data);
+#if 0
+ break;
+
+ case 2: /* check */
+ printf("Check\n");
+ height = width = TEXTURE_SIZE;
+ my_data = (void *)malloc(width * height * 4);
+ for (i = 0 ; i < height ; i += 2) {
+ for (j = 0 ; j < width ; j += 2) {
+ if (((i + j) & 0x3) == 0) {
+ plot2(my_data, i, j, r0, g0, b0, a0);
+ } else {
+ plot2(my_data, i, j, r1, g1, b1, a1);
+ }
+ }
+ }
+ break;
+
+ case 3: /* random stripes */
+ printf("Stripes 2\n");
+ height = width = TEXTURE_SIZE;
+ my_data = (void *)malloc(width * height * 4);
+ for (i = 0 ; i < height ; i += 2) {
+ r0 = random() & 0xff;
+ g0 = random() & 0xff;
+ b0 = random() & 0xff;
+ a0 = 0xff;
+ for (j = 0 ; j < width ; j += 2) {
+ plot2(my_data, i, j, r0, g0, b0, a0);
+ }
+ }
+ break;
+ }
+#endif
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
+ width, height, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, my_data);
+ sprintf (buf, "random texture: (%dx%d)",
+ width, height);
+ check_gl_error(buf);
+
+ /* setup parameters for texturing */
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
+
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ if (random() & 0x1) {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ } else {
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ }
+
+ if (my_data != NULL) {
+ free(my_data);
+ my_data = NULL;
+ }
+
+ /* use full texture */
+ /* acd - was 1.0 */
+ texture->min_tx = 0.0;
+ texture->max_tx = 2.0;
+ texture->min_ty = 0.0;
+ texture->max_ty = 2.0;
+ texture->start_time = time((time_t *)0);
+}
+
+static Bool
+setup_file_texture (ModeInfo *mi, char *filename, texture *texture)