a4bb0c70fd8f7462638b11c90f6343ddcaeb2942
[demo_prior] / src / demo.c
1 #include <stdio.h>
2 #include "opengl.h"
3 #include "demo.h"
4 #include "part.h"
5 #include "post.h"
6 #include "sdr.h"
7 #include "opt.h"
8
9 void reg_whitted(void);
10
11 int win_width, win_height;
12 float win_aspect;
13 long time_msec;
14
15 static int reshape_pending;
16 static unsigned int sdr_gamma;
17
18 int demo_init(void)
19 {
20         int i;
21
22         if(init_opengl() == -1) {
23                 return -1;
24         }
25
26         glEnable(GL_DEPTH_TEST);
27         glEnable(GL_CULL_FACE);
28         if(opt.srgb) {
29                 glEnable(GL_FRAMEBUFFER_SRGB);
30         }
31         if(opt.msaa) {
32                 glEnable(GL_MULTISAMPLE);
33         }
34
35         post_init();
36
37         if(!(sdr_gamma = create_program_load("sdr/post.v.glsl", "sdr/gamma.p.glsl"))) {
38                 fprintf(stderr, "Warning: failed to load the gamma correction shader\n");
39         }
40
41         reg_whitted();
42
43         for(i=0; i<num_parts; i++) {
44                 if(parts[i]->init() == -1) {
45                         fprintf(stderr, "part %s init failed\n", parts[i]->name);
46                         return -1;
47                 }
48         }
49
50         switch_part(parts[0]);
51         return 0;
52 }
53
54 void demo_cleanup(void)
55 {
56         int i;
57
58         if(sdr_gamma) {
59                 glDeleteProgram(sdr_gamma);
60         }
61         post_cleanup();
62
63         for(i=0; i<num_parts; i++) {
64                 parts[i]->destroy();
65         }
66 }
67
68 void demo_display(void)
69 {
70         long part_time;
71
72         if(reshape_pending) {
73                 post_reshape(win_width, win_height);
74                 reshape_pending = 0;
75         }
76
77         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78
79         if(!cur_part) return;
80
81         part_time = time_msec - cur_part->start_time;
82         if(part_time < cur_part->in_time) {
83                 float t = (float)part_time / cur_part->in_time;
84
85                 if(prev_part) {
86                         long prev_part_time = time_msec - prev_part->start_time;
87                         if(prev_part->draw_out) {
88                                 prev_part->draw_out(prev_part_time, t);
89                         } else {
90                                 prev_part->draw(prev_part_time);
91                         }
92                 }
93
94                 if(cur_part->draw_in) {
95                         cur_part->draw_in(part_time, t);
96                 } else {
97                         cur_part->draw(part_time);
98                 }
99         } else {
100                 prev_part = 0;
101                 cur_part->draw(part_time);
102         }
103
104         /* no-srgb gamma correction fallback */
105         if(!opt.srgb) {
106                 glBindTexture(GL_TEXTURE_2D, post_fbtex[0].id);
107                 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
108
109                 glUseProgram(sdr_gamma);
110                 overlay_tex(post_fbtex, 1.0);
111         }
112 }
113
114 void demo_reshape(int x, int y)
115 {
116         reshape_pending = 1;
117
118         win_width = x;
119         win_height = y;
120         win_aspect = (float)x / (float)y;
121
122         glViewport(0, 0, x, y);
123
124         glMatrixMode(GL_PROJECTION);
125         glLoadIdentity();
126         gluPerspective(50.0, win_aspect, 0.5, 500.0);
127 }
128
129 void demo_keyboard(int key, int st)
130 {
131         if(st && key == 27) {
132                 demo_quit();
133                 return;
134         }
135
136         if(cur_part && cur_part->keyboard) {
137                 cur_part->keyboard(key, st);
138         }
139 }
140
141
142 void demo_mbutton(int bn, int st, int x, int y)
143 {
144         if(cur_part && cur_part->mbutton) {
145                 cur_part->mbutton(bn, st, x, y);
146         }
147 }
148
149 void demo_mmotion(int x, int y)
150 {
151         if(cur_part && cur_part->mmotion) {
152                 cur_part->mmotion(x, y);
153         }
154 }
155
156 void demo_sball_motion(int x, int y, int z)
157 {
158 }
159
160 void demo_sball_rotate(int x, int y, int z)
161 {
162 }
163
164 void demo_sball_button(int bn, int st)
165 {
166 }