From f22bed18ebb87b87c6b8e9d31b42f4320bdf34d6 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sat, 4 Feb 2017 04:06:41 +0200 Subject: [PATCH] tilesaving etc --- tools/tilegen/src/main.c | 101 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 20 deletions(-) diff --git a/tools/tilegen/src/main.c b/tools/tilegen/src/main.c index 5468d6c..3d4b316 100644 --- a/tools/tilegen/src/main.c +++ b/tools/tilegen/src/main.c @@ -1,7 +1,9 @@ #include #include -#include +#include +#include #include +#include struct vec3 { float x, y, z; @@ -14,12 +16,16 @@ void set_view_matrix(int eye, float ipd); 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) { @@ -50,7 +56,7 @@ void disp(void) 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); @@ -58,7 +64,7 @@ void disp(void) 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); @@ -75,21 +81,10 @@ void draw_field(int xcells, int ycells) 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}, @@ -97,11 +92,11 @@ static struct vec3 tileverts[] = { {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 */ @@ -175,5 +170,71 @@ void keyb(unsigned char key, int x, int y) 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