deepstone

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