dos3d

annotate firstp/firstp.c @ 19:c10f62b2bd56

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Nov 2011 07:17:09 +0200
parents 1e9f0b3616fa
children
rev   line source
nuclear@14 1 #include <stdio.h>
nuclear@14 2 #include <stdlib.h>
nuclear@14 3 #include <math.h>
nuclear@14 4 #include <signal.h>
nuclear@14 5 #include <conio.h>
nuclear@14 6 #include "vga.h"
nuclear@14 7 #include "mingl.h"
nuclear@14 8 #include "timer.h"
nuclear@14 9 #include "mouse.h"
nuclear@14 10 #include "texture.h"
nuclear@14 11 #include "palman.h"
nuclear@14 12 #include "scene.h"
nuclear@14 13
nuclear@14 14 #define DEG2RAD(x) (M_PI * (x) / 180.0)
nuclear@14 15
nuclear@14 16 static int init(void);
nuclear@14 17 static void shutdown(void);
nuclear@14 18 static void redraw(void);
nuclear@14 19 static int proc_events(void);
nuclear@14 20 static int keyb(int key);
nuclear@14 21 static void mouse_button(int bn, int x, int y);
nuclear@14 22 static void mouse_motion(int x, int y);
nuclear@14 23 static void sighandler(int s);
nuclear@14 24
nuclear@14 25
nuclear@17 26 static float cam_x, cam_y, cam_z;
nuclear@14 27 static float cam_theta, cam_phi;
nuclear@14 28
nuclear@19 29 static float walk_speed = 0.5;
nuclear@14 30 static float look_speed = 1.0;
nuclear@14 31 static int mouse_look = 0;
nuclear@14 32
nuclear@14 33 static void *fbuf;
nuclear@14 34 static struct scene scn;
nuclear@14 35
nuclear@14 36
nuclear@14 37 int main(void)
nuclear@14 38 {
nuclear@14 39 if(init() == -1) {
nuclear@14 40 return 1;
nuclear@14 41 }
nuclear@14 42
nuclear@14 43 while(proc_events()) {
nuclear@14 44 redraw();
nuclear@14 45 }
nuclear@14 46
nuclear@14 47 shutdown();
nuclear@14 48 return 0;
nuclear@14 49 }
nuclear@14 50
nuclear@14 51
nuclear@14 52 static int init(void)
nuclear@14 53 {
nuclear@14 54 init_timer(100);
nuclear@14 55
nuclear@14 56 set_video_mode(0x13);
nuclear@14 57
nuclear@14 58 signal(SIGINT, sighandler);
nuclear@14 59 signal(SIGSEGV, sighandler);
nuclear@14 60 signal(SIGFPE, sighandler);
nuclear@14 61 signal(SIGILL, sighandler);
nuclear@14 62 signal(SIGABRT, sighandler);
nuclear@14 63
nuclear@14 64 if(mgl_init(320, 200) == -1) {
nuclear@14 65 fprintf(stderr, "mgl init failed\n");
nuclear@14 66 return -1;
nuclear@14 67 }
nuclear@14 68 fbuf = mgl_framebuffer();
nuclear@14 69
nuclear@14 70 mgl_enable(MGL_CULL_FACE);
nuclear@14 71 mgl_enable(MGL_SMOOTH);
nuclear@14 72 mgl_enable(MGL_LIGHTING);
nuclear@14 73 mgl_enable(MGL_DEPTH_TEST);
nuclear@14 74
nuclear@14 75 mgl_matrix_mode(MGL_PROJECTION);
nuclear@14 76 mgl_load_identity();
nuclear@14 77 mgl_perspective(45.0, 320.0 / 200.0, 0.5, 200.0);
nuclear@14 78
nuclear@14 79 /* setup palette */
nuclear@14 80 palm_add_color(255, 255, 255);
nuclear@14 81
nuclear@14 82 scn_init(&scn);
nuclear@14 83 if(scn_load(&scn, "data/hall.obj") == -1) {
nuclear@14 84 return -1;
nuclear@14 85 }
nuclear@14 86
nuclear@14 87 palm_build();
nuclear@14 88 {
nuclear@14 89 int i, palsz = palm_palette_size();
nuclear@14 90 struct palm_color *pal = palm_palette();
nuclear@14 91
nuclear@14 92 for(i=0; i<palsz; i++) {
nuclear@14 93 set_palette(i, pal[i].r, pal[i].g, pal[i].b);
nuclear@14 94 }
nuclear@14 95 }
nuclear@14 96
nuclear@14 97 mgl_color_range(palm_color_range() - 1);
nuclear@14 98 return 0;
nuclear@14 99 }
nuclear@14 100
nuclear@14 101 static void shutdown(void)
nuclear@14 102 {
nuclear@14 103 mgl_free();
nuclear@14 104 set_video_mode(3);
nuclear@14 105 }
nuclear@14 106
nuclear@14 107 static void redraw(void)
nuclear@14 108 {
nuclear@14 109 mgl_clear(0);
nuclear@14 110 mgl_clear_depth();
nuclear@14 111
nuclear@14 112 mgl_matrix_mode(MGL_MODELVIEW);
nuclear@14 113 mgl_load_identity();
nuclear@17 114 mgl_rotate(cam_phi, 1, 0, 0);
nuclear@14 115 mgl_rotate(cam_theta, 0, 1, 0);
nuclear@14 116 mgl_translate(-cam_x, -cam_y, -cam_z);
nuclear@17 117 mgl_translate(0, -2.0, 0);
nuclear@14 118
nuclear@14 119 mgl_light_intensity(0, 1.0);
nuclear@14 120 mgl_light_position(0, 0, 5, 0, 1);
nuclear@14 121
nuclear@14 122 /*mgl_torus(0.75, 0.25, 16, 8);*/
nuclear@14 123 scn_render(&scn);
nuclear@14 124
nuclear@14 125 copy_frame(fbuf);
nuclear@14 126 }
nuclear@14 127
nuclear@17 128 #define DEG_TO_RAD(x) (M_PI * (x) / 180.0)
nuclear@17 129 void cam_move(float dx, float dy)
nuclear@17 130 {
nuclear@17 131 float angle = DEG_TO_RAD(cam_theta);
nuclear@17 132 cam_x += cos(angle) * dx + sin(angle) * dy;
nuclear@17 133 cam_z -= -sin(angle) * dx + cos(angle) * dy;
nuclear@17 134 }
nuclear@17 135
nuclear@14 136 static int proc_events(void)
nuclear@14 137 {
nuclear@14 138 static int prev_mx, prev_my, prev_bnmask;
nuclear@14 139 int mx, my, bnmask;
nuclear@14 140
nuclear@14 141 if(kbhit()) {
nuclear@14 142 if(keyb(getch()) == 0) {
nuclear@14 143 return 0;
nuclear@14 144 }
nuclear@14 145 }
nuclear@14 146
nuclear@14 147 bnmask = read_mouse(&mx, &my);
nuclear@14 148 if(bnmask != prev_bnmask) {
nuclear@14 149 mouse_button(bnmask, mx, my);
nuclear@14 150 prev_bnmask = bnmask;
nuclear@14 151 }
nuclear@14 152 if(mx != prev_mx || my != prev_my) {
nuclear@14 153 mouse_motion(mx, my);
nuclear@14 154 prev_mx = mx;
nuclear@14 155 prev_my = my;
nuclear@14 156 }
nuclear@14 157 return 1;
nuclear@14 158 }
nuclear@14 159
nuclear@14 160 static int keyb(int key)
nuclear@14 161 {
nuclear@14 162 switch(key) {
nuclear@14 163 case 27:
nuclear@14 164 return 0;
nuclear@14 165
nuclear@14 166 case 'w':
nuclear@17 167 cam_move(0, walk_speed);
nuclear@14 168 break;
nuclear@14 169
nuclear@14 170 case 's':
nuclear@17 171 cam_move(0, -walk_speed);
nuclear@14 172 break;
nuclear@14 173
nuclear@14 174 case 'a':
nuclear@17 175 cam_move(-walk_speed, 0);
nuclear@14 176 break;
nuclear@14 177
nuclear@14 178 case 'd':
nuclear@17 179 cam_move(walk_speed, 0);
nuclear@14 180 break;
nuclear@14 181
nuclear@14 182 case '`':
nuclear@14 183 mouse_look = !mouse_look;
nuclear@14 184 break;
nuclear@14 185
nuclear@14 186 default:
nuclear@14 187 break;
nuclear@14 188 }
nuclear@14 189 return 1;
nuclear@14 190 }
nuclear@14 191
nuclear@14 192 static int bnstate;
nuclear@14 193 static void mouse_button(int bn, int x, int y)
nuclear@14 194 {
nuclear@14 195 bnstate = bn;
nuclear@14 196 }
nuclear@14 197
nuclear@14 198 static void mouse_motion(int x, int y)
nuclear@14 199 {
nuclear@14 200 static int prev_x, prev_y;
nuclear@14 201 int dx = x - prev_x;
nuclear@14 202 int dy = y - prev_y;
nuclear@14 203 prev_x = x;
nuclear@14 204 prev_y = y;
nuclear@14 205
nuclear@14 206 if(mouse_look || bnstate) {
nuclear@14 207 cam_theta += dx * look_speed;
nuclear@14 208 cam_phi += dy * look_speed;
nuclear@14 209
nuclear@14 210 if(cam_phi < -90) {
nuclear@14 211 cam_phi = -90;
nuclear@14 212 }
nuclear@14 213 if(cam_phi > 90) {
nuclear@14 214 cam_phi = 90;
nuclear@14 215 }
nuclear@14 216 }
nuclear@14 217 }
nuclear@14 218
nuclear@14 219 static void sighandler(int s)
nuclear@14 220 {
nuclear@14 221 set_video_mode(3);
nuclear@14 222
nuclear@14 223 switch(s) {
nuclear@14 224 case SIGABRT:
nuclear@14 225 fprintf(stderr, "abort\n");
nuclear@14 226 break;
nuclear@14 227
nuclear@14 228 case SIGILL:
nuclear@14 229 fprintf(stderr, "illegal operation\n");
nuclear@14 230 break;
nuclear@14 231
nuclear@14 232 case SIGSEGV:
nuclear@14 233 fprintf(stderr, "segmentation fault\n");
nuclear@14 234 break;
nuclear@14 235
nuclear@14 236 case SIGINT:
nuclear@14 237 fprintf(stderr, "interrupted\n");
nuclear@14 238 break;
nuclear@14 239
nuclear@14 240 case SIGFPE:
nuclear@14 241 fprintf(stderr, "floating point exception\n");
nuclear@14 242 break;
nuclear@14 243
nuclear@14 244 default:
nuclear@14 245 fprintf(stderr, "unexpected signal\n");
nuclear@14 246 }
nuclear@14 247
nuclear@14 248 exit(1);
nuclear@14 249 }