#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 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);
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);
+void draw_grid(void);
+
+void draw_shaft(void);
-static int win_width = 320;
-static int win_height = 640;
+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.4;
+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);
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);
-
- draw_field(10, 20);
-
- glColorMask(0, 1, 1, 0);
-
- 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(10, 20);
+ glColorMask(1, 1, 1, 1);
+ } else {
+ draw_scene(0);
+ }
- glColorMask(1, 1, 1, 1);
+ if(show_grid) {
+ draw_grid();
+ }
glutSwapBuffers();
assert(glGetError() == GL_NO_ERROR);
}
+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);
+}
+
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);
+ }
}
}
-
- /*
- 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 DX 0.75
+#define DZ 0.45
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 */
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;
view = key - '2';
glutPostRedisplay();
break;
+
+ case 's':
+ show_stereo = !show_stereo;
+ glutPostRedisplay();
+ break;
+
+ case ' ':
+ if(win_height > 160) {
+ glutReshapeWindow(win_width / 4, win_height / 4);
+ } else {
+ glutReshapeWindow(win_width * 4, win_height * 4);
+ }
+ glutPostRedisplay();
+ break;
+
+ case 't':
+ show_blocks = !show_blocks;
+ glutPostRedisplay();
+ break;
+
+ case 'g':
+ show_grid = !show_grid;
+ 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;
+}
+
+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();
+}