eb0fe5b01bac803c8f28b27b83f143e1b3b99952
[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
8 static int init(void);
9 static void destroy(void);
10 static void start(void);
11 static void stop(void);
12 static void draw(long tm);
13 static void mbutton(int bn, int st, int x, int y);
14 static void mmotion(int x, int y);
15
16
17 static struct demo_part part = {
18         "whitted",
19         0, 0,
20         init, destroy,
21         start, stop,
22         draw,
23         0, 0,
24         0, 0,
25         mbutton, mmotion
26 };
27
28 static float cam_theta, cam_phi, cam_dist = 6;
29
30 static int bnstate[8];
31 static int mouse_x, mouse_y;
32
33 static unsigned int sdr;
34 static int uloc_aspect;
35
36 static struct texture *dbgtex;
37 static float dbg_alpha;
38
39
40 void reg_whitted(void)
41 {
42         add_part(&part);
43 }
44
45
46 static int init(void)
47 {
48         if(!(sdr = create_program_load("sdr/whitted.v.glsl", "sdr/whitted.p.glsl"))) {
49                 return -1;
50         }
51         uloc_aspect = get_uniform_loc(sdr, "aspect");
52
53         dbgtex = load_texture("data/dbg_whitted.jpg");
54
55         return 0;
56 }
57
58 static void destroy(void)
59 {
60         free_texture(dbgtex);
61 }
62
63 static void start(void)
64 {
65 }
66
67 static void stop(void)
68 {
69 }
70
71 static void draw(long tm)
72 {
73         glDisable(GL_DEPTH_TEST);
74
75         glMatrixMode(GL_MODELVIEW);
76         glLoadIdentity();
77         /*
78         glTranslatef(0, 0, -cam_dist);
79         glRotatef(cam_phi, 1, 0, 0);
80         glRotatef(cam_theta, 0, 1, 0);
81         */
82         glRotatef(-cam_theta, 0, 1, 0);
83         glRotatef(-cam_phi, 1, 0, 0);
84         glTranslatef(0, 0, cam_dist);
85
86         glBindFramebuffer(GL_FRAMEBUFFER, post_fbo[0]);
87         glClear(GL_COLOR_BUFFER_BIT);
88
89         glUseProgram(sdr);
90         glUniform1f(uloc_aspect, win_aspect);
91
92         glBegin(GL_QUADS);
93         glTexCoord2f(0, 0);
94         glVertex2f(-1, -1);
95         glTexCoord2f(1, 0);
96         glVertex2f(1, -1);
97         glTexCoord2f(1, 1);
98         glVertex2f(1, 1);
99         glTexCoord2f(0, 1);
100         glVertex2f(-1, 1);
101         glEnd();
102
103         glBindFramebuffer(GL_FRAMEBUFFER, 0);
104         glUseProgram(post_sdr[POST_OLDFIG]);
105         overlay_tex(post_fbtex, 1.0);
106
107         if(dbgtex) {
108                 glUseProgram(0);
109                 overlay_tex(dbgtex, dbg_alpha);
110         }
111 }
112
113 static void mbutton(int bn, int st, int x, int y)
114 {
115         bnstate[bn] = st;
116         mouse_x = x;
117         mouse_y = y;
118
119         switch(bn) {
120         case 3:
121                 dbg_alpha += 0.1;
122                 if(dbg_alpha > 1.0) dbg_alpha = 1.0;
123                 break;
124         case 4:
125                 dbg_alpha -= 0.1;
126                 if(dbg_alpha < 0.0) dbg_alpha = 0.0;
127         }
128 }
129
130 static void mmotion(int x, int y)
131 {
132         int dx = x - mouse_x;
133         int dy = y - mouse_y;
134         mouse_x = x;
135         mouse_y = y;
136
137         if(!(dx | dy)) return;
138
139         if(bnstate[0]) {
140                 cam_theta += dx * 0.5;
141                 cam_phi += dy * 0.5;
142                 if(cam_phi < -90) cam_phi = -90;
143                 if(cam_phi > 90) cam_phi = 90;
144         }
145         if(bnstate[2]) {
146                 cam_dist += dy * 0.1;
147                 if(cam_dist < 0) cam_dist = 0;
148         }
149 }