From 7b554099ac39caf1ff135615d5453135d5c3dab4 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Fri, 3 Feb 2017 07:31:30 +0200 Subject: [PATCH] tile generator --- .gitignore | 1 + tools/tilegen/Makefile | 13 ++++ tools/tilegen/src/main.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 tools/tilegen/Makefile create mode 100644 tools/tilegen/src/main.c diff --git a/.gitignore b/.gitignore index 499a618..c85b0ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.o *.swp +tilegen diff --git a/tools/tilegen/Makefile b/tools/tilegen/Makefile new file mode 100644 index 0000000..84a1e68 --- /dev/null +++ b/tools/tilegen/Makefile @@ -0,0 +1,13 @@ +src = $(wildcard src/*.c) +obj = $(src:.c=.o) +bin = tilegen + +CFLAGS = -pedantic -Wall -g +LDFLAGS = -lGL -lGLU -lglut + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff --git a/tools/tilegen/src/main.c b/tools/tilegen/src/main.c new file mode 100644 index 0000000..5468d6c --- /dev/null +++ b/tools/tilegen/src/main.c @@ -0,0 +1,179 @@ +#include +#include +#include +#include + +struct vec3 { + float x, y, z; +}; + +void disp(void); +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); + +static int win_width = 320; +static int win_height = 640; +static float aspect; +static int view = 0; +static float eye_dist = 1.4; + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutInitWindowSize(win_width, win_height); + glutCreateWindow("tilegen"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + + glClearColor(0.05, 0.05, 0.05, 1); + glEnable(GL_CULL_FACE); + glShadeModel(GL_FLAT); + + glutMainLoop(); + return 0; +} + +void disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glColorMask(1, 0, 0, 0); + + 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); + + draw_field(10, 20); + + 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 ? xcells : ycells; + float cellsz = 2.0 / ncells; + + glPushMatrix(); + glTranslatef((col - xcells / 2.0 + 0.5) * cellsz, (row - ycells / 2.0 + 0.5) * cellsz, 0); + glScalef(cellsz * 0.5, cellsz * 0.5, cellsz * 0.5); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, tileverts); + glColorPointer(3, GL_FLOAT, 0, tilecol); + glDrawElements(GL_QUADS, 20, GL_UNSIGNED_SHORT, tileidx); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + + glPopMatrix(); +} + +void set_view_matrix(int eye, float ipd) +{ + const float offs[] = {0.5, 0.0, -0.5}; + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(offs[eye + 1] * ipd, 0, 0); +} + +void set_proj_matrix(int eye, float ipd) +{ + const float offs[] = {1.0, 0.0, -1.0}; + float vpsz = 0.5; + + float right = aspect * vpsz; + float top = vpsz; + float shift = offs[eye + 1] * ipd * 0.25; /* 0.25 -> 0.5 * znear */ + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-right + shift, right + shift, -top, top, 0.5, 500.0); +} + +void reshape(int x, int y) +{ + win_width = x; + win_height = y; + aspect = (float)win_width / (float)win_height; + glViewport(0, 0, x, y); +} + +void keyb(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + + case '1': + case '2': + case '3': + view = key - '2'; + glutPostRedisplay(); + break; + } +} -- 1.7.10.4