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