- added libdrawtext
[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         glUseProgram(sdr);
87         glUniform1f(uloc_aspect, win_aspect);
88
89         glBegin(GL_QUADS);
90         glTexCoord2f(0, 0);
91         glVertex2f(-1, -1);
92         glTexCoord2f(1, 0);
93         glVertex2f(1, -1);
94         glTexCoord2f(1, 1);
95         glVertex2f(1, 1);
96         glTexCoord2f(0, 1);
97         glVertex2f(-1, 1);
98         glEnd();
99
100         vignette(0.43, 0.38, 0.45, 0.8, 1.0);
101
102         if(dbgtex && dbg_alpha > 0.0) {
103                 glUseProgram(0);
104                 glMatrixMode(GL_TEXTURE);
105                 glLoadIdentity();
106                 glScalef(1, -1, 1);
107                 overlay_tex(dbgtex, dbg_alpha);
108                 glMatrixMode(GL_TEXTURE);
109                 glLoadIdentity();
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 }