dos3d
view firstp/firstp.c @ 23:f2c2e45e8edd
reverted the mistaken push from the deepstone branch
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 21 Sep 2013 18:22:11 +0300 |
parents | 1e9f0b3616fa |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <signal.h>
5 #include <conio.h>
6 #include "vga.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 init_timer(100);
56 set_video_mode(0x13);
58 signal(SIGINT, sighandler);
59 signal(SIGSEGV, sighandler);
60 signal(SIGFPE, sighandler);
61 signal(SIGILL, sighandler);
62 signal(SIGABRT, sighandler);
64 if(mgl_init(320, 200) == -1) {
65 fprintf(stderr, "mgl init failed\n");
66 return -1;
67 }
68 fbuf = mgl_framebuffer();
70 mgl_enable(MGL_CULL_FACE);
71 mgl_enable(MGL_SMOOTH);
72 mgl_enable(MGL_LIGHTING);
73 mgl_enable(MGL_DEPTH_TEST);
75 mgl_matrix_mode(MGL_PROJECTION);
76 mgl_load_identity();
77 mgl_perspective(45.0, 320.0 / 200.0, 0.5, 200.0);
79 /* setup palette */
80 palm_add_color(255, 255, 255);
82 scn_init(&scn);
83 if(scn_load(&scn, "data/hall.obj") == -1) {
84 return -1;
85 }
87 palm_build();
88 {
89 int i, palsz = palm_palette_size();
90 struct palm_color *pal = palm_palette();
92 for(i=0; i<palsz; i++) {
93 set_palette(i, pal[i].r, pal[i].g, pal[i].b);
94 }
95 }
97 mgl_color_range(palm_color_range() - 1);
98 return 0;
99 }
101 static void shutdown(void)
102 {
103 mgl_free();
104 set_video_mode(3);
105 }
107 static void redraw(void)
108 {
109 mgl_clear(0);
110 mgl_clear_depth();
112 mgl_matrix_mode(MGL_MODELVIEW);
113 mgl_load_identity();
114 mgl_rotate(cam_phi, 1, 0, 0);
115 mgl_rotate(cam_theta, 0, 1, 0);
116 mgl_translate(-cam_x, -cam_y, -cam_z);
117 mgl_translate(0, -2.0, 0);
119 mgl_light_intensity(0, 1.0);
120 mgl_light_position(0, 0, 5, 0, 1);
122 /*mgl_torus(0.75, 0.25, 16, 8);*/
123 scn_render(&scn);
125 copy_frame(fbuf);
126 }
128 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
129 void cam_move(float dx, float dy)
130 {
131 float angle = DEG_TO_RAD(cam_theta);
132 cam_x += cos(angle) * dx + sin(angle) * dy;
133 cam_z -= -sin(angle) * dx + cos(angle) * dy;
134 }
136 static int proc_events(void)
137 {
138 static int prev_mx, prev_my, prev_bnmask;
139 int mx, my, bnmask;
141 if(kbhit()) {
142 if(keyb(getch()) == 0) {
143 return 0;
144 }
145 }
147 bnmask = read_mouse(&mx, &my);
148 if(bnmask != prev_bnmask) {
149 mouse_button(bnmask, mx, my);
150 prev_bnmask = bnmask;
151 }
152 if(mx != prev_mx || my != prev_my) {
153 mouse_motion(mx, my);
154 prev_mx = mx;
155 prev_my = my;
156 }
157 return 1;
158 }
160 static int keyb(int key)
161 {
162 switch(key) {
163 case 27:
164 return 0;
166 case 'w':
167 cam_move(0, walk_speed);
168 break;
170 case 's':
171 cam_move(0, -walk_speed);
172 break;
174 case 'a':
175 cam_move(-walk_speed, 0);
176 break;
178 case 'd':
179 cam_move(walk_speed, 0);
180 break;
182 case '`':
183 mouse_look = !mouse_look;
184 break;
186 default:
187 break;
188 }
189 return 1;
190 }
192 static int bnstate;
193 static void mouse_button(int bn, int x, int y)
194 {
195 bnstate = bn;
196 }
198 static void mouse_motion(int x, int y)
199 {
200 static int prev_x, prev_y;
201 int dx = x - prev_x;
202 int dy = y - prev_y;
203 prev_x = x;
204 prev_y = y;
206 if(mouse_look || bnstate) {
207 cam_theta += dx * look_speed;
208 cam_phi += dy * look_speed;
210 if(cam_phi < -90) {
211 cam_phi = -90;
212 }
213 if(cam_phi > 90) {
214 cam_phi = 90;
215 }
216 }
217 }
219 static void sighandler(int s)
220 {
221 set_video_mode(3);
223 switch(s) {
224 case SIGABRT:
225 fprintf(stderr, "abort\n");
226 break;
228 case SIGILL:
229 fprintf(stderr, "illegal operation\n");
230 break;
232 case SIGSEGV:
233 fprintf(stderr, "segmentation fault\n");
234 break;
236 case SIGINT:
237 fprintf(stderr, "interrupted\n");
238 break;
240 case SIGFPE:
241 fprintf(stderr, "floating point exception\n");
242 break;
244 default:
245 fprintf(stderr, "unexpected signal\n");
246 }
248 exit(1);
249 }