Fixed normal calculation at edges, more UI changes
[ted.git] / src / main.c
blobe4adf47fc499fe260d877271a9abfd12f71dfd3c
1 #include "backend/video.h"
2 #include "backend/input.h"
3 #include "backend/shapes.h"
4 #include "terrain.h"
5 #include <stdlib.h>
6 #include <math.h>
7 #include <time.h>
9 #define MESH_SIZE 128
11 void terrain_demo(struct terrain *t)
13 int size = t->size;
14 float *height = t->height;
16 int i, j;
17 for (i=0; i<size; i++)
19 for (j=0; j<size; j++)
21 float z = (cos(i*0.5) + sin(j*0.5)) * 2.0;
22 height[i*size+j] = z;
23 if (z > t->max_height)
24 t->max_height = z;
25 if (z < t->min_height)
26 t->min_height = z;
31 void setup_gl();
33 int main(int argc, char **argv)
35 v_init();
36 v_closable(1); // can close the window when it's running
38 v_setup(800, 600, 0);
40 i_init();
41 keystate_t *key;
42 mousestate_t *mouse;
44 shapes_init();
46 struct terrain *mesh = terrain_create(MESH_SIZE);
48 terrain_demo(mesh);
49 terrain_bake(mesh);
51 setup_gl();
53 float zoom = 1.0;
54 float zoom_min = 1.0;
55 float zoom_max = 10.0;
57 float rot[] = {45.0, -45.0, 0.0};
59 float mouse_sensitivity = 0.2;
61 int done = 0;
62 while (!done)
64 i_pump();
65 key = i_keystate();
66 mouse = i_mousestate();
68 if (key[K_ESCAPE]) done = 1;
69 if (key[K_SPACE])
71 mesh->seed = time(0);
72 terrain_clear(mesh);
73 terrain_fractal(mesh, MESH_SIZE);
74 terrain_bake(mesh);
76 if (key[K_p])
78 mesh->seed = time(0);
79 terrain_plasma(mesh);
80 terrain_bake(mesh);
82 if (key[K_s])
84 terrain_smooth(mesh, 0.90);
85 terrain_bake(mesh);
87 if (key[K_f])
89 mesh->drawmode = FLAT;
90 mesh->dirty = 1;
92 if (key[K_n])
94 terrain_bake(mesh);
95 mesh->drawmode = NORMAL;
96 mesh->dirty = 1;
98 if (key[K_d])
100 mesh->drawmode = NORMAL_DEBUG;
101 mesh->dirty = 1;
104 if (mouse->button & M_BUTTON(M_MIDDLE))
106 zoom -= mouse->rel_y * mouse_sensitivity * 0.1 * zoom;
107 if (zoom < zoom_min) zoom = zoom_min;
108 if (zoom > zoom_max) zoom = zoom_max;
110 if (mouse->button & M_BUTTON(M_RIGHT))
112 rot[1] += mouse->rel_x * mouse_sensitivity;
113 rot[0] += mouse->rel_y * mouse_sensitivity;
116 glMatrixMode(GL_MODELVIEW);
117 glLoadIdentity();
119 glTranslatef(0.0, 0.0, -1000.0); // remember: this is an ortho projection
120 glScalef(1.0/zoom, 1.0/zoom, 1.0/zoom);
121 glRotatef(rot[0], 1,0,0);
122 glRotatef(rot[1], 0,1,0);
123 glRotatef(rot[2], 0,0,1);
125 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
127 terrain_draw(mesh);
129 glPushMatrix();
131 glScalef(zoom, zoom, zoom);
133 glDisable(GL_LIGHTING);
134 glDisable(GL_DEPTH_TEST);
136 glBegin(GL_LINES);
138 glColor3f(1.0, 0.0, 0.0); // x
139 glVertex3f(0.0, 0.0, 0.0);
140 glVertex3f(1.0, 0.0, 0.0);
142 glColor3f(0.0, 1.0, 0.0); // y
143 glVertex3f(0.0, 0.0, 0.0);
144 glVertex3f(0.0, 1.0, 0.0);
146 glColor3f(0.0, 0.0, 1.0); // z
147 glVertex3f(0.0, 0.0, 0.0);
148 glVertex3f(0.0, 0.0, 1.0);
150 glEnd();
152 glEnable(GL_DEPTH_TEST);
153 glEnable(GL_LIGHTING);
155 glPopMatrix();
157 v_flip();
160 terrain_destroy(mesh);
162 return 0;
165 void setup_gl()
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
170 int width = v_info()->width;
171 int height = v_info()->height;
173 float ratio = (float)width/height;
174 float scale = 10.0;
176 glOrtho(-scale*ratio, scale*ratio, -scale, scale, -10000.0, 10000.0);
178 glShadeModel(GL_SMOOTH);
180 glEnable(GL_DEPTH_TEST);
181 glEnable(GL_NORMALIZE);
183 glEnable(GL_LIGHTING);
184 glEnable(GL_LIGHT0);