options/config
[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 = 15, cam_dist = 6;
27
28 static int bnstate[8];
29 static int mouse_x, mouse_y;
30
31 static unsigned int sdr;
32 static int uloc_aspect;
33
34
35 void reg_whitted(void)
36 {
37         add_part(&part);
38 }
39
40
41 static int init(void)
42 {
43         if(!(sdr = create_program_load("sdr/whitted.v.glsl", "sdr/whitted.p.glsl"))) {
44                 return -1;
45         }
46         uloc_aspect = get_uniform_loc(sdr, "aspect");
47         return 0;
48 }
49
50 static void destroy(void)
51 {
52 }
53
54 static void start(void)
55 {
56 }
57
58 static void stop(void)
59 {
60 }
61
62 static void draw(long tm)
63 {
64         glMatrixMode(GL_MODELVIEW);
65         glLoadIdentity();
66         /*
67         glTranslatef(0, 0, -cam_dist);
68         glRotatef(cam_phi, 1, 0, 0);
69         glRotatef(cam_theta, 0, 1, 0);
70         */
71         glRotatef(-cam_theta, 0, 1, 0);
72         glRotatef(-cam_phi, 1, 0, 0);
73         glTranslatef(0, 0, cam_dist);
74
75         glUseProgram(sdr);
76         glUniform1f(uloc_aspect, win_aspect);
77
78         glBegin(GL_QUADS);
79         glTexCoord2f(0, 0);
80         glVertex2f(-1, -1);
81         glTexCoord2f(1, 0);
82         glVertex2f(1, -1);
83         glTexCoord2f(1, 1);
84         glVertex2f(1, 1);
85         glTexCoord2f(0, 1);
86         glVertex2f(-1, 1);
87         glEnd();
88 }
89
90 static void mbutton(int bn, int st, int x, int y)
91 {
92         bnstate[bn] = st;
93         mouse_x = x;
94         mouse_y = y;
95 }
96
97 static void mmotion(int x, int y)
98 {
99         int dx = x - mouse_x;
100         int dy = y - mouse_y;
101         mouse_x = x;
102         mouse_y = y;
103
104         if(!(dx | dy)) return;
105
106         if(bnstate[0]) {
107                 cam_theta += dx * 0.5;
108                 cam_phi += dy * 0.5;
109                 if(cam_phi < -90) cam_phi = -90;
110                 if(cam_phi > 90) cam_phi = 90;
111         }
112         if(bnstate[2]) {
113                 cam_dist += dy * 0.1;
114                 if(cam_dist < 0) cam_dist = 0;
115         }
116 }