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