11 void draw_field(int xcells, int ycells);
12 void draw_tile(int col, int row, int xcells, int ycells);
13 void set_view_matrix(int eye, float ipd);
14 void set_proj_matrix(int eye, float ipd);
15 void reshape(int x, int y);
16 void keyb(unsigned char key, int x, int y);
18 static int win_width = 320;
19 static int win_height = 640;
22 static float eye_dist = 1.4;
24 int main(int argc, char **argv)
26 glutInit(&argc, argv);
27 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
28 glutInitWindowSize(win_width, win_height);
29 glutCreateWindow("tilegen");
31 glutDisplayFunc(disp);
32 glutReshapeFunc(reshape);
33 glutKeyboardFunc(keyb);
35 glClearColor(0.05, 0.05, 0.05, 1);
36 glEnable(GL_CULL_FACE);
37 glShadeModel(GL_FLAT);
45 glClear(GL_COLOR_BUFFER_BIT);
47 glColorMask(1, 0, 0, 0);
49 set_proj_matrix(-1, eye_dist);
50 set_view_matrix(-1, eye_dist);
51 glTranslatef(0, 0, -1);
55 glColorMask(0, 1, 1, 0);
57 set_proj_matrix(1, eye_dist);
58 set_view_matrix(1, eye_dist);
59 glTranslatef(0, 0, -1);
63 glColorMask(1, 1, 1, 1);
66 assert(glGetError() == GL_NO_ERROR);
69 void draw_field(int xcells, int ycells)
73 for(i=0; i<ycells; i++) {
74 for(j=0; j<xcells; j++) {
75 draw_tile(j, i, xcells, ycells);
94 static struct vec3 tileverts[] = {
95 {-1, -1, 0}, {1, -1, 0}, {1, 1, 0}, {-1, 1, 0},
96 {-1 + DX, -1 + DX, DZ}, {1 - DX, -1 + DX, DZ},
97 {1 - DX, 1 - DX, DZ}, {-1 + DX, 1 - DX, DZ}
99 static struct vec3 tilecol[] = {
100 {0.8, 0.8, 0.8}, /* left */
101 {0.2, 0.2, 0.2}, /* bottom */
102 {0.4, 0.4, 0.4}, /* right */
103 {1.0, 1.0, 1.0}, /* top */
104 {0.7, 0.7, 0.7}, /* front */
105 {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
107 static uint16_t tileidx[] = { /* 3 +---------+ 2 */
108 4, 7, 3, 0, /* |\ 7 6 /| */
109 5, 4, 0, 1, /* | +-----+ | */
110 6, 5, 1, 2, /* | | | | */
111 7, 6, 2, 3, /* | +-----+ | */
112 5, 6, 7, 4 /* |/ 4 5 \| */
113 }; /* 0 +---------+ 1 */
115 void draw_tile(int col, int row, int xcells, int ycells)
117 int ncells = xcells > ycells ? xcells : ycells;
118 float cellsz = 2.0 / ncells;
121 glTranslatef((col - xcells / 2.0 + 0.5) * cellsz, (row - ycells / 2.0 + 0.5) * cellsz, 0);
122 glScalef(cellsz * 0.5, cellsz * 0.5, cellsz * 0.5);
124 glEnableClientState(GL_VERTEX_ARRAY);
125 glEnableClientState(GL_COLOR_ARRAY);
126 glVertexPointer(3, GL_FLOAT, 0, tileverts);
127 glColorPointer(3, GL_FLOAT, 0, tilecol);
128 glDrawElements(GL_QUADS, 20, GL_UNSIGNED_SHORT, tileidx);
129 glDisableClientState(GL_VERTEX_ARRAY);
130 glDisableClientState(GL_COLOR_ARRAY);
135 void set_view_matrix(int eye, float ipd)
137 const float offs[] = {0.5, 0.0, -0.5};
139 glMatrixMode(GL_MODELVIEW);
141 glTranslatef(offs[eye + 1] * ipd, 0, 0);
144 void set_proj_matrix(int eye, float ipd)
146 const float offs[] = {1.0, 0.0, -1.0};
149 float right = aspect * vpsz;
151 float shift = offs[eye + 1] * ipd * 0.25; /* 0.25 -> 0.5 * znear */
153 glMatrixMode(GL_PROJECTION);
155 glFrustum(-right + shift, right + shift, -top, top, 0.5, 500.0);
158 void reshape(int x, int y)
162 aspect = (float)win_width / (float)win_height;
163 glViewport(0, 0, x, y);
166 void keyb(unsigned char key, int x, int y)