fixed incorrect checking of the existence of GLX_EXT_swap_control and friends
[demo_prior] / src / part_whitted.c
1 #include "opengl.h"
2 #include "demo.h"
3 #include "part.h"
4 #include "sdr.h"
5 #include "texture.h"
6 #include "post.h"
7 #include "imtk.h"
8
9 static int init(void);
10 static void destroy(void);
11 static void start(void);
12 static void stop(void);
13 static void draw(long tm);
14 static void mbutton(int bn, int st, int x, int y);
15 static void mmotion(int x, int y);
16
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 static float cam_theta, cam_phi, cam_dist = 6;
30
31 static int bnstate[8];
32 static int mouse_x, mouse_y;
33
34 static unsigned int sdr;
35 static int uloc_aspect;
36
37 static struct texture *dbgtex;
38 static float dbg_alpha;
39
40
41 void reg_whitted(void)
42 {
43         add_part(&part);
44 }
45
46
47 static int init(void)
48 {
49         if(!(sdr = create_program_load("sdr/whitted.v.glsl", "sdr/whitted.p.glsl"))) {
50                 return -1;
51         }
52         uloc_aspect = get_uniform_loc(sdr, "aspect");
53
54         dbgtex = load_texture("data/dbg_whitted.jpg");
55
56         return 0;
57 }
58
59 static void destroy(void)
60 {
61         free_texture(dbgtex);
62 }
63
64 static void start(void)
65 {
66 }
67
68 static void stop(void)
69 {
70 }
71
72 static void draw(long tm)
73 {
74         static float vgn_offset = 0.47;
75         static float vgn_sharp = 3.1;
76
77         glDisable(GL_DEPTH_TEST);
78
79         glMatrixMode(GL_MODELVIEW);
80         glLoadIdentity();
81         /*
82         glTranslatef(0, 0, -cam_dist);
83         glRotatef(cam_phi, 1, 0, 0);
84         glRotatef(cam_theta, 0, 1, 0);
85         */
86         glRotatef(-cam_theta, 0, 1, 0);
87         glRotatef(-cam_phi, 1, 0, 0);
88         glTranslatef(0, 0, cam_dist);
89
90         glUseProgram(sdr);
91         glUniform1f(uloc_aspect, win_aspect);
92
93         glBegin(GL_QUADS);
94         glTexCoord2f(0, 0);
95         glVertex2f(-1, -1);
96         glTexCoord2f(1, 0);
97         glVertex2f(1, -1);
98         glTexCoord2f(1, 1);
99         glVertex2f(1, 1);
100         glTexCoord2f(0, 1);
101         glVertex2f(-1, 1);
102         glEnd();
103
104         vignette(0.15, 0.05, 0.15, vgn_offset, vgn_sharp);
105
106         if(dbgtex && dbg_alpha > 0.0) {
107                 glUseProgram(0);
108                 glMatrixMode(GL_TEXTURE);
109                 glLoadIdentity();
110                 glScalef(1, -1, 1);
111                 overlay_tex(dbgtex, dbg_alpha);
112                 glMatrixMode(GL_TEXTURE);
113                 glLoadIdentity();
114         }
115
116         if(dbgui) {
117                 glUseProgram(0);
118
119                 imtk_begin();
120                 imtk_layout_start(10, 20);
121                 imtk_layout_spacing(10);
122
123                 imtk_layout_dir(IMTK_HORIZONTAL);
124
125                 imtk_label("offset:", IMTK_AUTO, IMTK_AUTO);
126                 vgn_offset = imtk_slider(IMUID, vgn_offset, 0.0, 1.5, IMTK_AUTO, IMTK_AUTO);
127                 imtk_layout_newline();
128
129                 imtk_label("sharpness:", IMTK_AUTO, IMTK_AUTO);
130                 vgn_sharp = imtk_slider(IMUID, vgn_sharp, 0, 10, IMTK_AUTO, IMTK_AUTO);
131                 imtk_layout_newline();
132                 imtk_end();
133         }
134 }
135
136 static void mbutton(int bn, int st, int x, int y)
137 {
138         bnstate[bn] = st;
139         mouse_x = x;
140         mouse_y = y;
141
142         switch(bn) {
143         case 0:
144                 if(imtk_layout_contains(x, y)) {
145                         imtk_inp_mouse(bn, st);
146                 }
147                 break;
148         case 3:
149                 dbg_alpha += 0.1;
150                 if(dbg_alpha > 1.0) dbg_alpha = 1.0;
151                 break;
152         case 4:
153                 dbg_alpha -= 0.1;
154                 if(dbg_alpha < 0.0) dbg_alpha = 0.0;
155         }
156 }
157
158 static void mmotion(int x, int y)
159 {
160         int dx = x - mouse_x;
161         int dy = y - mouse_y;
162         mouse_x = x;
163         mouse_y = y;
164
165         if(!(dx | dy)) return;
166
167         if(imtk_layout_contains(x, y)) {
168                 imtk_inp_motion(x, y);
169                 return;
170         }
171
172         if(bnstate[0]) {
173                 cam_theta += dx * 0.5;
174                 cam_phi += dy * 0.5;
175                 if(cam_phi < -90) cam_phi = -90;
176                 if(cam_phi > 90) cam_phi = 90;
177         }
178         if(bnstate[2]) {
179                 cam_dist += dy * 0.1;
180                 if(cam_dist < 0) cam_dist = 0;
181         }
182 }