textures, overlay images, libimago
[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         glMatrixMode(GL_MODELVIEW);
74         glLoadIdentity();
75         /*
76         glTranslatef(0, 0, -cam_dist);
77         glRotatef(cam_phi, 1, 0, 0);
78         glRotatef(cam_theta, 0, 1, 0);
79         */
80         glRotatef(-cam_theta, 0, 1, 0);
81         glRotatef(-cam_phi, 1, 0, 0);
82         glTranslatef(0, 0, cam_dist);
83
84         glUseProgram(sdr);
85         glUniform1f(uloc_aspect, win_aspect);
86
87         glBegin(GL_QUADS);
88         glTexCoord2f(0, 0);
89         glVertex2f(-1, -1);
90         glTexCoord2f(1, 0);
91         glVertex2f(1, -1);
92         glTexCoord2f(1, 1);
93         glVertex2f(1, 1);
94         glTexCoord2f(0, 1);
95         glVertex2f(-1, 1);
96         glEnd();
97
98         if(dbgtex) {
99                 glUseProgram(0);
100                 overlay_tex(dbgtex, dbg_alpha);
101         }
102 }
103
104 static void mbutton(int bn, int st, int x, int y)
105 {
106         bnstate[bn] = st;
107         mouse_x = x;
108         mouse_y = y;
109
110         switch(bn) {
111         case 3:
112                 dbg_alpha += 0.1;
113                 if(dbg_alpha > 1.0) dbg_alpha = 1.0;
114                 break;
115         case 4:
116                 dbg_alpha -= 0.1;
117                 if(dbg_alpha < 0.0) dbg_alpha = 0.0;
118         }
119 }
120
121 static void mmotion(int x, int y)
122 {
123         int dx = x - mouse_x;
124         int dy = y - mouse_y;
125         mouse_x = x;
126         mouse_y = y;
127
128         if(!(dx | dy)) return;
129
130         if(bnstate[0]) {
131                 cam_theta += dx * 0.5;
132                 cam_phi += dy * 0.5;
133                 if(cam_phi < -90) cam_phi = -90;
134                 if(cam_phi > 90) cam_phi = 90;
135         }
136         if(bnstate[2]) {
137                 cam_dist += dy * 0.1;
138                 if(cam_dist < 0) cam_dist = 0;
139         }
140 }