static void loadtiles(void) { int lx, rx, ty, by; /* image bounding box */ static int ox = TILES*TILESIZE/2, oy = TILES*TILESIZE/2; /* image origin */ static int ot = 0, os = 0; int dx = 0, dy = 0, nx = -1, ny = -1; float trx, try; #define S_TSIZE (TSIZE-TILESIZE) /* visible portion of texture = TSIZE less one tile for slop */ /* calculate tile #'s at corners of visible region */ lx = x - S_TSIZE/2; rx = lx + S_TSIZE; by = y - S_TSIZE/2; ty = by + S_TSIZE; lx /= TILESIZE; rx /= TILESIZE; by /= TILESIZE; ty /= TILESIZE; dx = ((x - S_TSIZE/2)/TILESIZE) - ((ox - S_TSIZE/2)/TILESIZE); nx = lx; ny = by; if (dx < 0) { /* add on left */ os -= TILESIZE; if (os < 0) os += TSIZE; nx = lx; } else if (dx > 0) { nx = rx; } dy = ((y - S_TSIZE/2) / TILESIZE) - ((oy - S_TSIZE/2) / TILESIZE); if (dy > 0) { /* add on bottom */ ny = ty; } else if (dy < 0) { /* add on top */ ot -= TILESIZE; if (ot < 0) ot += TSIZE; ny = by; } if (dx || dy) printf("dx %d dy %d lx %d rx %d by %d ty %d nx %d ny %d os %d ot %d\n", dx, dy, lx, rx, by, ty, nx, ny, os, ot); if (dx) { int t; for(t = 0; t < TSIZE; t += TILESIZE) { glTexSubImage2D(GL_TEXTURE_2D, 0, os, (t+ot) % TSIZE, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[ny+t/TILESIZE][nx].data); printf("load %d %d %d %d\n", nx, ny+t/TILESIZE, os, (t+ot) % TSIZE); } } if (dy) { int s; for(s = 0; s < TSIZE; s += TILESIZE) { glTexSubImage2D(GL_TEXTURE_2D, 0, (s+os) % TSIZE, ot, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[ny][nx+s/TILESIZE].data); printf("load %d %d %d %d\n", nx+s/TILESIZE, ny, (s+os) % TSIZE, ot); } } if (dx > 0) { os += TILESIZE; if (os >= TSIZE) os -= TSIZE; } if (dy > 0) { ot += TILESIZE; if (ot >= TSIZE) ot -= TSIZE; } ox = x; oy = y; glMatrixMode(GL_TEXTURE); glLoadIdentity(); trx = (float)((x-TILES*TILESIZE/2) % TSIZE)/TSIZE; try = (float)((y-TILES*TILESIZE/2) % TSIZE)/TSIZE; glTranslatef(trx, try, 0.f); glMatrixMode(GL_MODELVIEW); } static void init(char *filename) { int i; mesh0(-1.f,1.f,-1.f,1.f,0.f,1.f,0.f,1.f,0.f,64,64); if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components < 3 || components > 4) { printf("must be RGB or RGBA image\n"); exit(EXIT_FAILURE); } } else { int i, j; components = 4; width = height = TSIZE; image = (unsigned *) malloc(width*height*sizeof(unsigned)); for (j = 0; j < height; j++) for (i = 0; i < width; i++) { if (i & 1) image[i+j*width] = 0xff; else image[i+j*width] = 0xff00; if (j&1) image[i+j*width] |= 0xff0000; } } if (width % TILESIZE || height % TILESIZE) { #define TXSIZE 192 unsigned *newimage = malloc(TXSIZE*TXSIZE*sizeof *newimage); gluScaleImage(GL_RGBA, width, height, GL_UNSIGNED_BYTE, image, TXSIZE, TXSIZE, GL_UNSIGNED_BYTE, newimage); free(image); image = newimage; width = height = TXSIZE; components = 4; } tile_image(image); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TSIZE, TSIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); for(i = 0; i < TILES; i++) { int j; for(j = 0; j < TILES; j++) { glTexSubImage2D(GL_TEXTURE_2D, 0, i*TILESIZE, j*TILESIZE, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[(TILES-TSIZE/TILESIZE)/2+j][(TILES-TSIZE/TILESIZE)/2+i].data); } } glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.,1.,.1,10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-1.0); glLineWidth(3.0); glClearColor(.25, .25, .25, .25); /* start at center of image */ x = TILES*TILESIZE/2; y = TILES*TILESIZE/2; }
static void init(char *filename) { int i; mesh0(-1.f,1.f,-1.f,1.f,0.f,1.f,0.f,1.f,0.f,64,64); if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components < 3 || components > 4) { printf("must be RGB or RGBA image\n"); exit(EXIT_FAILURE); } } else { int i, j; components = 4; width = height = TSIZE; image = (unsigned *) malloc(width*height*sizeof(unsigned)); for (j = 0; j < height; j++) for (i = 0; i < width; i++) { if (i & 1) image[i+j*width] = 0xff; else image[i+j*width] = 0xff00; if (j&1) image[i+j*width] |= 0xff0000; } } if (width % TILESIZE || height % TILESIZE) { #define TXSIZE 192 unsigned *newimage = malloc(TXSIZE*TXSIZE*sizeof *newimage); gluScaleImage(GL_RGBA, width, height, GL_UNSIGNED_BYTE, image, TXSIZE, TXSIZE, GL_UNSIGNED_BYTE, newimage); free(image); image = newimage; width = height = TXSIZE; components = 4; } tile_image(image); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TSIZE, TSIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); for(i = 0; i < TILES; i++) { int j; for(j = 0; j < TILES; j++) { glTexSubImage2D(GL_TEXTURE_2D, 0, i*TILESIZE, j*TILESIZE, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[(TILES-TSIZE/TILESIZE)/2+j][(TILES-TSIZE/TILESIZE)/2+i].data); } } glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.,1.,.1,10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-1.0); glLineWidth(3.0); glClearColor(.25, .25, .25, .25); /* start at center of image */ x = TILES*TILESIZE/2; y = TILES*TILESIZE/2; }