dos3d

view firstp/firstp.c @ 14:be61704c4cc8

added initial firstp
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 29 Nov 2011 07:22:49 +0200
parents
children 1e9f0b3616fa
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 = 10;
27 static float cam_theta, cam_phi;
29 static float walk_speed = 0.1;
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_theta, 0, 1, 0);
115 mgl_rotate(cam_phi, 1, 0, 0);
116 mgl_translate(-cam_x, -cam_y, -cam_z);
118 mgl_light_intensity(0, 1.0);
119 mgl_light_position(0, 0, 5, 0, 1);
121 /*mgl_torus(0.75, 0.25, 16, 8);*/
122 scn_render(&scn);
124 copy_frame(fbuf);
125 }
127 static int proc_events(void)
128 {
129 static int prev_mx, prev_my, prev_bnmask;
130 int mx, my, bnmask;
132 if(kbhit()) {
133 if(keyb(getch()) == 0) {
134 return 0;
135 }
136 }
138 bnmask = read_mouse(&mx, &my);
139 if(bnmask != prev_bnmask) {
140 mouse_button(bnmask, mx, my);
141 prev_bnmask = bnmask;
142 }
143 if(mx != prev_mx || my != prev_my) {
144 mouse_motion(mx, my);
145 prev_mx = mx;
146 prev_my = my;
147 }
148 return 1;
149 }
151 static int keyb(int key)
152 {
153 float dir_x, dir_y, right_x, right_y;
155 dir_x = sin(DEG2RAD(cam_theta)) * walk_speed;
156 dir_y = cos(DEG2RAD(cam_theta)) * walk_speed;
157 right_x = dir_y;
158 right_y = -dir_x;
160 switch(key) {
161 case 27:
162 return 0;
164 case 'w':
165 cam_x += dir_x;
166 cam_z -= dir_y;
167 break;
169 case 's':
170 cam_x -= dir_x;
171 cam_z += dir_y;
172 break;
174 case 'a':
175 cam_x -= right_x;
176 cam_z += right_y;
177 break;
179 case 'd':
180 cam_x += right_x;
181 cam_z -= right_y;
182 break;
184 case '`':
185 mouse_look = !mouse_look;
186 break;
188 default:
189 break;
190 }
191 return 1;
192 }
194 static int bnstate;
195 static void mouse_button(int bn, int x, int y)
196 {
197 bnstate = bn;
198 }
200 static void mouse_motion(int x, int y)
201 {
202 static int prev_x, prev_y;
203 int dx = x - prev_x;
204 int dy = y - prev_y;
205 prev_x = x;
206 prev_y = y;
208 if(mouse_look || bnstate) {
209 cam_theta += dx * look_speed;
210 cam_phi += dy * look_speed;
212 if(cam_phi < -90) {
213 cam_phi = -90;
214 }
215 if(cam_phi > 90) {
216 cam_phi = 90;
217 }
218 }
219 }
221 static void sighandler(int s)
222 {
223 set_video_mode(3);
225 switch(s) {
226 case SIGABRT:
227 fprintf(stderr, "abort\n");
228 break;
230 case SIGILL:
231 fprintf(stderr, "illegal operation\n");
232 break;
234 case SIGSEGV:
235 fprintf(stderr, "segmentation fault\n");
236 break;
238 case SIGINT:
239 fprintf(stderr, "interrupted\n");
240 break;
242 case SIGFPE:
243 fprintf(stderr, "floating point exception\n");
244 break;
246 default:
247 fprintf(stderr, "unexpected signal\n");
248 }
250 exit(1);
251 }