part system
[demo_prior] / src / part_whitted.c
diff --git a/src/part_whitted.c b/src/part_whitted.c
new file mode 100644 (file)
index 0000000..7154abf
--- /dev/null
@@ -0,0 +1,94 @@
+#include "opengl.h"
+#include "demo.h"
+#include "part.h"
+
+static int init(void);
+static void destroy(void);
+static void start(void);
+static void stop(void);
+static void draw(long tm);
+static void mbutton(int bn, int st, int x, int y);
+static void mmotion(int x, int y);
+
+static float cam_theta, cam_phi, cam_dist = 8;
+
+static int bnstate[8];
+static int mouse_x, mouse_y;
+
+static struct demo_part part = {
+       "whitted",
+       0, 0,
+       init, destroy,
+       start, stop,
+       draw,
+       0, 0,
+       0, 0,
+       mbutton, mmotion
+};
+
+void reg_whitted(void)
+{
+       add_part(&part);
+}
+
+
+static int init(void)
+{
+       return 0;
+}
+
+static void destroy(void)
+{
+}
+
+static void start(void)
+{
+}
+
+static void stop(void)
+{
+}
+
+static void draw(long tm)
+{
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity();
+       glTranslatef(0, 0, -cam_dist);
+       glRotatef(cam_phi, 1, 0, 0);
+       glRotatef(cam_theta, 0, 1, 0);
+
+       glEnable(GL_LIGHTING);
+       glEnable(GL_LIGHT0);
+
+       glFrontFace(GL_CW);
+       glutSolidTeapot(1.0);
+       glFrontFace(GL_CCW);
+}
+
+static void mbutton(int bn, int st, int x, int y)
+{
+       bnstate[bn] = st;
+       mouse_x = x;
+       mouse_y = y;
+}
+
+static void mmotion(int x, int y)
+{
+       int dx = x - mouse_x;
+       int dy = y - mouse_y;
+       mouse_x = x;
+       mouse_y = y;
+
+       if(!(dx | dy)) return;
+
+       if(bnstate[0]) {
+               cam_theta += dx * 0.5;
+               cam_phi += dy * 0.5;
+               if(cam_phi < -90) cam_phi = -90;
+               if(cam_phi > 90) cam_phi = 90;
+       }
+       if(bnstate[2]) {
+               cam_dist += dy * 0.1;
+               if(cam_dist < 0) cam_dist = 0;
+       }
+}