deepstone

view src/main.c @ 28:11d14f688485

added clipping
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 06:38:08 +0300
parents dcfe615c4c5f
children 03a0b307706a
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <signal.h>
5 #include <conio.h>
6 #include "wvga.h"
7 #include "mingl.h"
8 #include "timer.h"
9 #include "mouse.h"
10 #include "texture.h"
11 #include "palman.h"
12 #include "scene.h"
14 #define DEG2RAD(x) (M_PI * (x) / 180.0)
16 static int init(void);
17 static void shutdown(void);
18 static void redraw(void);
19 static int proc_events(void);
20 static int keyb(int key);
21 static void mouse_button(int bn, int x, int y);
22 static void mouse_motion(int x, int y);
23 static void sighandler(int s);
26 static float cam_x, cam_y, cam_z;
27 static float cam_theta, cam_phi;
29 static float walk_speed = 0.5;
30 static float look_speed = 1.0;
31 static int mouse_look = 0;
33 static void *fbuf;
34 static struct scene scn;
37 int main(void)
38 {
39 if(init() == -1) {
40 return 1;
41 }
43 while(proc_events()) {
44 redraw();
45 }
47 shutdown();
48 return 0;
49 }
52 static int init(void)
53 {
54 float vfov, hfov, aspect;
56 aspect = 320.0 / 200.0;
57 vfov = 60.0;
58 hfov = vfov * aspect;
60 init_timer(100);
62 set_video_mode(0x13);
64 signal(SIGINT, sighandler);
65 signal(SIGSEGV, sighandler);
66 signal(SIGFPE, sighandler);
67 signal(SIGILL, sighandler);
68 signal(SIGABRT, sighandler);
70 if(mgl_init(320, 200) == -1) {
71 fprintf(stderr, "mgl init failed\n");
72 return -1;
73 }
74 fbuf = mgl_framebuffer();
76 mgl_enable(MGL_CULL_FACE);
77 mgl_enable(MGL_SMOOTH);
78 mgl_enable(MGL_LIGHTING);
79 mgl_enable(MGL_DEPTH_TEST);
81 mgl_matrix_mode(MGL_PROJECTION);
82 mgl_load_identity();
83 mgl_perspective(vfov, aspect, 0.5, 200.0);
85 #if 0
86 mgl_enable(MGL_CLIP_PLANE0);
87 mgl_enable(MGL_CLIP_PLANE1);
88 mgl_enable(MGL_CLIP_PLANE2);
89 mgl_enable(MGL_CLIP_PLANE3);
90 mgl_clip_plane(MGL_CLIP_PLANE0, -1, 0, -1, 0); /* positive X */
91 mgl_clip_plane(MGL_CLIP_PLANE1, 1, 0, -1, 0); /* negative X */
92 mgl_clip_plane(MGL_CLIP_PLANE2, 0, -1, -0.5, 0); /* positive Y */
93 mgl_clip_plane(MGL_CLIP_PLANE3, 0, 1, -0.5, 0); /* negative Y */
94 #endif
96 /* setup palette */
97 palm_add_color(255, 255, 255);
99 scn_init(&scn);
100 if(scn_load(&scn, "data/hall.obj") == -1) {
101 return -1;
102 }
104 palm_build();
105 {
106 int i, palsz = palm_palette_size();
107 struct palm_color *pal = palm_palette();
109 for(i=0; i<palsz; i++) {
110 set_pal_entry(i, pal[i].r, pal[i].g, pal[i].b);
111 }
112 }
114 mgl_color_range(palm_color_range() - 1);
115 return 0;
116 }
118 static void shutdown(void)
119 {
120 mgl_free();
121 set_video_mode(3);
122 }
124 static void redraw(void)
125 {
126 mgl_clear(0);
127 mgl_clear_depth();
129 mgl_matrix_mode(MGL_MODELVIEW);
130 mgl_load_identity();
131 mgl_rotate(cam_phi, 1, 0, 0);
132 mgl_rotate(cam_theta, 0, 1, 0);
133 mgl_translate(-cam_x, -cam_y, -cam_z);
134 mgl_translate(0, -2.0, 0);
136 mgl_light_intensity(0, 1.0);
137 mgl_light_position(0, 0, 5, 0, 1);
139 /*mgl_torus(0.75, 0.25, 16, 8);*/
140 scn_render(&scn);
142 copy_frame(fbuf);
143 }
145 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
146 void cam_move(float dx, float dy)
147 {
148 float angle = DEG_TO_RAD(cam_theta);
149 cam_x += cos(angle) * dx + sin(angle) * dy;
150 cam_z -= -sin(angle) * dx + cos(angle) * dy;
151 }
153 static int proc_events(void)
154 {
155 static int prev_mx, prev_my, prev_bnmask;
156 int mx, my, bnmask;
158 if(kbhit()) {
159 if(keyb(getch()) == 0) {
160 return 0;
161 }
162 }
164 bnmask = read_mouse(&mx, &my);
165 if(bnmask != prev_bnmask) {
166 mouse_button(bnmask, mx, my);
167 prev_bnmask = bnmask;
168 }
169 if(mx != prev_mx || my != prev_my) {
170 mouse_motion(mx, my);
171 prev_mx = mx;
172 prev_my = my;
173 }
174 return 1;
175 }
177 static int keyb(int key)
178 {
179 switch(key) {
180 case 27:
181 return 0;
183 case 'w':
184 cam_move(0, walk_speed);
185 break;
187 case 's':
188 cam_move(0, -walk_speed);
189 break;
191 case 'a':
192 cam_move(-walk_speed, 0);
193 break;
195 case 'd':
196 cam_move(walk_speed, 0);
197 break;
199 case '`':
200 mouse_look = !mouse_look;
201 break;
203 default:
204 break;
205 }
206 return 1;
207 }
209 static int bnstate;
210 static void mouse_button(int bn, int x, int y)
211 {
212 bnstate = bn;
213 }
215 static void mouse_motion(int x, int y)
216 {
217 static int prev_x, prev_y;
218 int dx = x - prev_x;
219 int dy = y - prev_y;
220 prev_x = x;
221 prev_y = y;
223 if(mouse_look || bnstate) {
224 cam_theta += dx * look_speed;
225 cam_phi += dy * look_speed;
227 if(cam_phi < -90) {
228 cam_phi = -90;
229 }
230 if(cam_phi > 90) {
231 cam_phi = 90;
232 }
233 }
234 }
236 static void sighandler(int s)
237 {
238 set_video_mode(3);
240 switch(s) {
241 case SIGABRT:
242 fprintf(stderr, "abort\n");
243 break;
245 case SIGILL:
246 fprintf(stderr, "illegal operation\n");
247 break;
249 case SIGSEGV:
250 fprintf(stderr, "segmentation fault\n");
251 break;
253 case SIGINT:
254 fprintf(stderr, "interrupted\n");
255 break;
257 case SIGFPE:
258 fprintf(stderr, "floating point exception\n");
259 break;
261 default:
262 fprintf(stderr, "unexpected signal\n");
263 }
265 exit(1);
266 }