#include <stdio.h>
#include <stdlib.h>
-#include <GL/glut.h>
+#include <string.h>
+#include <errno.h>
#include <assert.h>
+#include <GL/glut.h>
struct vec3 {
float x, y, z;
void set_proj_matrix(int eye, float ipd);
void reshape(int x, int y);
void keyb(unsigned char key, int x, int y);
+void save_tiles(void);
+int save_tile(const char *fname, unsigned char *pixptr, int xsz, int ysz, int pitch);
static int win_width = 320;
static int win_height = 640;
+static int grid_cols = 10;
+static int grid_rows = 20;
static float aspect;
static int view = 0;
-static float eye_dist = 1.4;
+static float eye_dist = 1.3;
int main(int argc, char **argv)
{
set_view_matrix(-1, eye_dist);
glTranslatef(0, 0, -1);
- draw_field(10, 20);
+ draw_field(grid_cols, grid_rows);
glColorMask(0, 1, 1, 0);
set_view_matrix(1, eye_dist);
glTranslatef(0, 0, -1);
- draw_field(10, 20);
+ draw_field(grid_cols, grid_rows);
glColorMask(1, 1, 1, 1);
draw_tile(j, i, xcells, ycells);
}
}
-
- /*
- glBegin(GL_LINES);
- glColor3f(1, 0, 0);
- glVertex2f(-100, 0);
- glVertex2f(100, 0);
- glColor3f(0, 1, 0);
- glVertex2f(0, -100);
- glVertex2f(0, 100);
- glEnd();
- */
}
#define DX 0.4
-#define DZ 0.3
+#define DZ 0.35
static struct vec3 tileverts[] = {
{-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0},
{1 - DX, 1 - DX, DZ}, {-1 + DX, 1 - DX, DZ}
};
static struct vec3 tilecol[] = {
- {0.8, 0.8, 0.8}, /* left */
+ {0.5, 0.5, 0.5}, /* left */
{0.2, 0.2, 0.2}, /* bottom */
- {0.4, 0.4, 0.4}, /* right */
- {1.0, 1.0, 1.0}, /* top */
- {0.7, 0.7, 0.7}, /* front */
+ {0.5, 0.5, 0.5}, /* right */
+ {0.8, 0.8, 0.8}, /* top */
+ {0.65, 0.65, 0.65}, /* front */
{0, 0, 0}, {0, 0, 0}, {0, 0, 0}
};
static uint16_t tileidx[] = { /* 3 +---------+ 2 */
view = key - '2';
glutPostRedisplay();
break;
+
+ case 's':
+ save_tiles();
+ break;
+
+ case ' ':
+ if(win_height > 160) {
+ glutReshapeWindow(win_width / 4, win_height / 4);
+ } else {
+ glutReshapeWindow(win_width * 4, win_height * 4);
+ }
+ glutPostRedisplay();
+ break;
+ }
+}
+
+
+void save_tiles(void)
+{
+ unsigned char *pixels, *tilepix;
+ int i, j, tilesz = win_height / grid_rows;
+ char fname[256];
+
+ if(!(pixels = malloc(win_width * win_height * 3))) {
+ fprintf(stderr, "failed to allocate pixel buffer\n");
+ return;
}
+ glReadPixels(0, 0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+
+ for(i=0; i<grid_rows; i++) {
+ for(j=0; j<grid_cols; j++) {
+ tilepix = pixels + (i * win_width + j) * tilesz * 3;
+
+ sprintf(fname, "tile-%02d%02d.ppm", j, i);
+ save_tile(fname, tilepix, tilesz, tilesz, win_width * 3);
+ }
+ }
+}
+
+int save_tile(const char *fname, unsigned char *pixptr, int xsz, int ysz, int pitch)
+{
+ int i, j;
+ FILE *fp;
+
+ printf("saving tile: %s\n", fname);
+
+ if(!(fp = fopen(fname, "wb"))) {
+ fprintf(stderr, "failed to open %s for writing: %s\n", fname, strerror(errno));
+ return -1;
+ }
+
+ pixptr += ysz * pitch;
+ fprintf(fp, "P6\n%d %d\n255\n", xsz, ysz);
+ for(i=0; i<ysz; i++) {
+ pixptr -= pitch;
+ for(j=0; j<xsz; j++) {
+ int r = pixptr[j * 3];
+ int g = pixptr[j * 3 + 1];
+ int b = pixptr[j * 3 + 2];
+ fputc(r, fp);
+ fputc(g, fp);
+ fputc(b, fp);
+ }
+ }
+
+ fclose(fp);
+ return 0;
}