part system
[demo_prior] / src / part_whitted.c
1 #include "opengl.h"
2 #include "demo.h"
3 #include "part.h"
4
5 static int init(void);
6 static void destroy(void);
7 static void start(void);
8 static void stop(void);
9 static void draw(long tm);
10 static void mbutton(int bn, int st, int x, int y);
11 static void mmotion(int x, int y);
12
13 static float cam_theta, cam_phi, cam_dist = 8;
14
15 static int bnstate[8];
16 static int mouse_x, mouse_y;
17
18 static struct demo_part part = {
19         "whitted",
20         0, 0,
21         init, destroy,
22         start, stop,
23         draw,
24         0, 0,
25         0, 0,
26         mbutton, mmotion
27 };
28
29 void reg_whitted(void)
30 {
31         add_part(&part);
32 }
33
34
35 static int init(void)
36 {
37         return 0;
38 }
39
40 static void destroy(void)
41 {
42 }
43
44 static void start(void)
45 {
46 }
47
48 static void stop(void)
49 {
50 }
51
52 static void draw(long tm)
53 {
54         glMatrixMode(GL_MODELVIEW);
55         glLoadIdentity();
56         glTranslatef(0, 0, -cam_dist);
57         glRotatef(cam_phi, 1, 0, 0);
58         glRotatef(cam_theta, 0, 1, 0);
59
60         glEnable(GL_LIGHTING);
61         glEnable(GL_LIGHT0);
62
63         glFrontFace(GL_CW);
64         glutSolidTeapot(1.0);
65         glFrontFace(GL_CCW);
66 }
67
68 static void mbutton(int bn, int st, int x, int y)
69 {
70         bnstate[bn] = st;
71         mouse_x = x;
72         mouse_y = y;
73 }
74
75 static void mmotion(int x, int y)
76 {
77         int dx = x - mouse_x;
78         int dy = y - mouse_y;
79         mouse_x = x;
80         mouse_y = y;
81
82         if(!(dx | dy)) return;
83
84         if(bnstate[0]) {
85                 cam_theta += dx * 0.5;
86                 cam_phi += dy * 0.5;
87                 if(cam_phi < -90) cam_phi = -90;
88                 if(cam_phi > 90) cam_phi = 90;
89         }
90         if(bnstate[2]) {
91                 cam_dist += dy * 0.1;
92                 if(cam_dist < 0) cam_dist = 0;
93         }
94 }