finished the tile generator master
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sun, 5 Feb 2017 04:40:30 +0000 (06:40 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sun, 5 Feb 2017 04:40:30 +0000 (06:40 +0200)
tools/tilegen/src/main.c
tools/tilegen/src/shaft.c [new file with mode: 0644]

index 3d4b316..f63171d 100644 (file)
@@ -10,6 +10,7 @@ struct vec3 {
 };
 
 void disp(void);
+void draw_scene(int eye);
 void draw_field(int xcells, int ycells);
 void draw_tile(int col, int row, int xcells, int ycells);
 void set_view_matrix(int eye, float ipd);
@@ -18,17 +19,30 @@ 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);
+void draw_grid(void);
 
-static int win_width = 320;
-static int win_height = 640;
+void draw_shaft(void);
+
+static int win_width = 1280;
+static int win_height = 960;
 static int grid_cols = 10;
 static int grid_rows = 20;
 static float aspect;
 static int view = 0;
-static float eye_dist = 1.3;
+static float eye_dist_well = 0.5;
+static float eye_dist_tiles = 1.3;
+
+static int show_blocks = 0;
+static int show_stereo = 0;
+static int show_grid = 1;
 
 int main(int argc, char **argv)
 {
+       printf("press 't' to toggle block tiles\n");
+       printf("press 's' to toggle stereo\n");
+       printf("press 'g' to toggle guide grid\n");
+       printf("press space to resize to 8x8 tiles\n");
+
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutInitWindowSize(win_width, win_height);
@@ -48,43 +62,56 @@ int main(int argc, char **argv)
 
 void disp(void)
 {
-       glClear(GL_COLOR_BUFFER_BIT);
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-       glColorMask(1, 0, 0, 0);
+       if(show_stereo) {
+               glColorMask(1, 0, 0, 0);
+               draw_scene(-1);
 
-       set_proj_matrix(-1, eye_dist);
-       set_view_matrix(-1, eye_dist);
-       glTranslatef(0, 0, -1);
+               glColorMask(0, 1, 1, 0);
+               draw_scene(1);
 
-       draw_field(grid_cols, grid_rows);
+               glColorMask(1, 1, 1, 1);
+       } else {
+               draw_scene(0);
+       }
 
-       glColorMask(0, 1, 1, 0);
+       if(show_grid) {
+               draw_grid();
+       }
+
+       glutSwapBuffers();
+       assert(glGetError() == GL_NO_ERROR);
+}
 
-       set_proj_matrix(1, eye_dist);
-       set_view_matrix(1, eye_dist);
+void draw_scene(int eye)
+{
+       set_proj_matrix(eye, eye_dist_well);
+       set_view_matrix(eye, eye_dist_well);
        glTranslatef(0, 0, -1);
+       draw_shaft();
 
+       set_proj_matrix(eye, eye_dist_tiles);
+       set_view_matrix(eye, eye_dist_tiles);
+       glTranslatef(0, 0, -1);
        draw_field(grid_cols, grid_rows);
-
-       glColorMask(1, 1, 1, 1);
-
-       glutSwapBuffers();
-       assert(glGetError() == GL_NO_ERROR);
 }
 
 void draw_field(int xcells, int ycells)
 {
        int i, j;
 
-       for(i=0; i<ycells; i++) {
-               for(j=0; j<xcells; j++) {
-                       draw_tile(j, i, xcells, ycells);
+       if(show_blocks) {
+               for(i=0; i<ycells; i++) {
+                       for(j=0; j<xcells; j++) {
+                               draw_tile(j, i, xcells, ycells);
+                       }
                }
        }
 }
 
-#define DX     0.4
-#define DZ     0.35
+#define DX     0.75
+#define DZ     0.45
 
 static struct vec3 tileverts[] = {
        {-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0},
@@ -139,7 +166,7 @@ void set_view_matrix(int eye, float ipd)
 void set_proj_matrix(int eye, float ipd)
 {
        const float offs[] = {1.0, 0.0, -1.0};
-       float vpsz = 0.5;
+       float vpsz = 0.5 / (2.0 / 3.0);
 
        float right = aspect * vpsz;
        float top = vpsz;
@@ -172,7 +199,8 @@ void keyb(unsigned char key, int x, int y)
                break;
 
        case 's':
-               save_tiles();
+               show_stereo = !show_stereo;
+               glutPostRedisplay();
                break;
 
        case ' ':
@@ -183,6 +211,16 @@ void keyb(unsigned char key, int x, int y)
                }
                glutPostRedisplay();
                break;
+
+       case 't':
+               show_blocks = !show_blocks;
+               glutPostRedisplay();
+               break;
+
+       case 'g':
+               show_grid = !show_grid;
+               glutPostRedisplay();
+               break;
        }
 }
 
@@ -238,3 +276,34 @@ int save_tile(const char *fname, unsigned char *pixptr, int xsz, int ysz, int pi
        fclose(fp);
        return 0;
 }
+
+void draw_grid(void)
+{
+       int x, y;
+
+       glMatrixMode(GL_PROJECTION);
+       glPushMatrix();
+       glLoadIdentity();
+       glOrtho(0, win_width, 0, win_height, -1, 1);
+
+       glMatrixMode(GL_MODELVIEW);
+       glPushMatrix();
+       glLoadIdentity();
+
+       glBegin(GL_LINES);
+       glColor3f(0.5, 0.2, 0.1);
+       for(y=0; y<win_height; y += 32) {
+               glVertex2f(0, y);
+               glVertex2f(win_width, y);
+       }
+       for(x=0; x<win_width; x += 32) {
+               glVertex2f(x, 0);
+               glVertex2f(x, win_height);
+       }
+       glEnd();
+
+       glMatrixMode(GL_PROJECTION);
+       glPopMatrix();
+       glMatrixMode(GL_MODELVIEW);
+       glPopMatrix();
+}
diff --git a/tools/tilegen/src/shaft.c b/tools/tilegen/src/shaft.c
new file mode 100644 (file)
index 0000000..b35ef5a
--- /dev/null
@@ -0,0 +1,78 @@
+#include <GL/gl.h>
+
+#define GAP_Y  1.0
+#define GAP_X  0.5
+#define WALL_WIDTH     0.2
+
+#define XOFFS_OUT      (GAP_X + WALL_WIDTH)
+#define XOFFS_IN       GAP_X
+#define YOFFS_OUT      (GAP_Y + WALL_WIDTH)
+#define YOFFS_IN       GAP_Y
+#define ZMAX           0.5
+
+enum {
+       COL_FRONT,
+       COL_LEFT,
+       COL_RIGHT,
+       COL_BOT,
+       NUM_COLORS
+};
+
+#define LLEFT  0.8
+#define LRIGHT 1.2
+#define LBOT   1.5
+
+static const float col[NUM_COLORS][3] = {
+       {0.75, 0.72, 0.8},
+       {0.75 * LLEFT, 0.72 * LLEFT, 0.8 * LLEFT},
+       {0.75 * LRIGHT, 0.72 * LRIGHT, 0.8 * LRIGHT},
+       {0.75 * LBOT, 0.72 * LBOT, 0.8 * LBOT}
+};
+
+void draw_shaft(void)
+{
+       glPushAttrib(GL_ENABLE_BIT);
+
+       glBegin(GL_QUADS);
+       /* tunnel-left */
+       glColor3fv(col[COL_LEFT]);
+       glVertex3f(-XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(-XOFFS_IN, -YOFFS_IN, -ZMAX);
+       glVertex3f(-XOFFS_IN, YOFFS_OUT, -ZMAX);
+       glVertex3f(-XOFFS_IN, YOFFS_OUT, 0);
+
+       /* tunnel-bottom */
+       glColor3fv(col[COL_BOT]);
+       glVertex3f(-XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(XOFFS_IN, -YOFFS_IN, -ZMAX);
+       glVertex3f(-XOFFS_IN, -YOFFS_IN, -ZMAX);
+
+       /* tunnel-right */
+       glColor3fv(col[COL_RIGHT]);
+       glVertex3f(XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(XOFFS_IN, YOFFS_OUT, 0);
+       glVertex3f(XOFFS_IN, YOFFS_OUT, -ZMAX);
+       glVertex3f(XOFFS_IN, -YOFFS_IN, -ZMAX);
+
+       glColor3fv(col[COL_FRONT]);
+       /* left front face */
+       glVertex3f(-XOFFS_OUT, -YOFFS_OUT, 0);
+       glVertex3f(-XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(-XOFFS_IN, YOFFS_OUT, 0);
+       glVertex3f(-XOFFS_OUT, YOFFS_OUT, 0);
+       /* bottom front face */
+       glVertex3f(-XOFFS_OUT, -YOFFS_OUT, 0);
+       glVertex3f(XOFFS_OUT, -YOFFS_OUT, 0);
+       glVertex3f(XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(-XOFFS_IN, -YOFFS_IN, 0);
+       /* right front face */
+       glVertex3f(XOFFS_IN, -YOFFS_IN, 0);
+       glVertex3f(XOFFS_OUT, -YOFFS_OUT, 0);
+       glVertex3f(XOFFS_OUT, YOFFS_OUT, 0);
+       glVertex3f(XOFFS_IN, YOFFS_OUT, 0);
+
+       glEnd();
+
+       glPopAttrib();
+}