… | |
… | |
79 | |
79 | |
80 | //////////////////////////////////////////////////////////////////////////// |
80 | //////////////////////////////////////////////////////////////////////////// |
81 | |
81 | |
82 | void var_i::operator ()() const |
82 | void var_i::operator ()() const |
83 | { |
83 | { |
84 | compile::cur->code << name; |
84 | shader_builder::cur->code << name; |
85 | |
85 | |
86 | if (compile::cur->first (this)) |
86 | if (shader_builder::cur->first (this)) |
87 | { |
87 | { |
88 | if (domainstr) |
88 | if (domainstr) |
89 | compile::cur->global << domainstr << ' ' << typestr << ' ' << name << ";\n"; |
89 | shader_builder::cur->global << domainstr << ' ' << typestr << ' ' << name << ";\n"; |
90 | else |
90 | else |
91 | compile::cur->local << " " << typestr << ' ' << name << ";\n"; |
91 | shader_builder::cur->local << " " << typestr << ' ' << name << ";\n"; |
92 | } |
92 | } |
93 | } |
93 | } |
94 | |
94 | |
95 | //////////////////////////////////////////////////////////////////////////// |
95 | //////////////////////////////////////////////////////////////////////////// |
96 | |
96 | |
… | |
… | |
129 | } while (b < s.size ()); |
129 | } while (b < s.size ()); |
130 | |
130 | |
131 | return o.str (); |
131 | return o.str (); |
132 | } |
132 | } |
133 | |
133 | |
134 | void shader_object_i::start () |
|
|
135 | { |
|
|
136 | compile::cur = new compile::shader_builder; |
|
|
137 | } |
|
|
138 | |
|
|
139 | void shader_object_i::stop () |
|
|
140 | { |
|
|
141 | compile::shader_builder &b = *compile::cur; |
|
|
142 | |
|
|
143 | ostringstream os; |
|
|
144 | |
|
|
145 | os << b.global.str () |
|
|
146 | << "\nvoid main (void)\n" |
|
|
147 | << "{\n" |
|
|
148 | << b.local.str () |
|
|
149 | << "\n" |
|
|
150 | << b.code.str () |
|
|
151 | << "}\n"; |
|
|
152 | |
|
|
153 | delete cur; |
|
|
154 | cur = 0; |
|
|
155 | |
|
|
156 | string src = os.str (); |
|
|
157 | const char *sptr = src.data (); |
|
|
158 | const int slen = src.size (); |
|
|
159 | |
|
|
160 | printf ("%s\n", linify (src).c_str ()); |
|
|
161 | glShaderSourceARB (id, 1, &sptr, &slen); |
|
|
162 | glCompileShaderARB (id); |
|
|
163 | |
|
|
164 | GLint compiled; |
|
|
165 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
|
|
166 | |
|
|
167 | if (!compiled) |
|
|
168 | { |
|
|
169 | char infolog[8192]; |
|
|
170 | glGetInfoLogARB (id, 8192, NULL, infolog); |
|
|
171 | printf ("%s\n", linify (src).c_str ()); |
|
|
172 | printf ("%s\n", infolog); |
|
|
173 | abort (); |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | //////////////////////////////////////////////////////////////////////////// |
134 | //////////////////////////////////////////////////////////////////////////// |
178 | |
135 | |
179 | GLint uniform_i::location () |
136 | GLint uniform_i::location () |
180 | { |
137 | { |
181 | assert (program_object_i::cur); |
138 | assert (program_object_i::cur); |
… | |
… | |
186 | rid = glGetUniformLocationARB (program_object_i::cur->id, name); |
143 | rid = glGetUniformLocationARB (program_object_i::cur->id, name); |
187 | |
144 | |
188 | return rid; |
145 | return rid; |
189 | } |
146 | } |
190 | |
147 | |
191 | program_object_i *program_object_i::cur; // currently bound program |
148 | program_object_i *program_object_i::cur; |
192 | |
149 | |
193 | program_object_i::program_object_i () |
150 | program_object_i::program_object_i () |
194 | { |
151 | { |
195 | id = glCreateProgramObjectARB (); |
152 | id = glCreateProgramObjectARB (); |
196 | assert (id); |
153 | assert (id); |
… | |
… | |
255 | sl_string<100> s; |
212 | sl_string<100> s; |
256 | sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
213 | sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w); |
257 | return s; |
214 | return s; |
258 | } |
215 | } |
259 | |
216 | |
260 | namespace compile { |
217 | shader_builder *shader_builder::cur = 0; |
261 | |
218 | |
262 | shader_builder *cur = 0; |
|
|
263 | |
|
|
264 | bool shader_builder::first (const void *p) |
219 | bool shader_builder::first (const void *p) |
265 | { |
220 | { |
266 | if (seen.find (p) == seen.end ()) |
221 | if (seen.find (p) == seen.end ()) |
267 | { |
222 | { |
268 | seen.insert (p); |
223 | seen.insert (p); |
269 | return true; |
224 | return true; |
270 | } |
225 | } |
271 | |
226 | |
272 | return false; |
227 | return false; |
|
|
228 | } |
|
|
229 | |
|
|
230 | void shader_builder::start () |
|
|
231 | { |
|
|
232 | cur = new shader_builder; |
|
|
233 | } |
|
|
234 | |
|
|
235 | string shader_builder::stop () |
|
|
236 | { |
|
|
237 | ostringstream os; |
|
|
238 | |
|
|
239 | os << cur->global.str () |
|
|
240 | << "\nvoid main (void)\n" |
|
|
241 | << "{\n" |
|
|
242 | << cur->local.str () |
|
|
243 | << "\n" |
|
|
244 | << cur->code.str () |
|
|
245 | << "}\n"; |
|
|
246 | |
|
|
247 | delete cur; |
|
|
248 | cur = 0; |
|
|
249 | |
|
|
250 | return os.str (); |
|
|
251 | } |
|
|
252 | |
|
|
253 | void shader_object_i::compile (const string &source) |
|
|
254 | { |
|
|
255 | const char *sptr = source.data (); |
|
|
256 | const int slen = source.size (); |
|
|
257 | |
|
|
258 | printf ("%s\n", linify (source).c_str ()); |
|
|
259 | glShaderSourceARB (id, 1, &sptr, &slen); |
|
|
260 | glCompileShaderARB (id); |
|
|
261 | |
|
|
262 | GLint compiled; |
|
|
263 | glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); |
|
|
264 | |
|
|
265 | if (!compiled) |
|
|
266 | { |
|
|
267 | char infolog[8192]; |
|
|
268 | glGetInfoLogARB (id, 8192, NULL, infolog); |
|
|
269 | printf ("%s\n", linify (source).c_str ()); |
|
|
270 | printf ("%s\n", infolog); |
|
|
271 | abort (); |
273 | } |
272 | } |
274 | } |
|
|
275 | |
|
|
276 | void debdebdebdebug ()//D |
|
|
277 | { |
|
|
278 | return; |
|
|
279 | |
|
|
280 | using namespace compile; |
|
|
281 | |
|
|
282 | vertex_shader vsh; |
|
|
283 | |
|
|
284 | vsh->start (); |
|
|
285 | |
|
|
286 | temp_4f lightpos; |
|
|
287 | temp_4f wpos; |
|
|
288 | |
|
|
289 | lightpos = vec4 (0, 10, 0, 1); |
|
|
290 | wpos = model_view_matrix * vin.vertex; |
|
|
291 | vout.position = vin.vertex * model_view_matrix_inverse; |
|
|
292 | vout.tex_coord[0] = vin.tex_coord[0]; |
|
|
293 | vout.tex_coord[1] = normalize (lightpos - wpos); |
|
|
294 | vout.tex_coord[2] = normalize (wpos); |
|
|
295 | //vout.tex_coord[3] = normalize (xyz (model_view_matrix_inverse_transpose) * vin.normal); |
|
|
296 | //vout.tex_coord[4] = normalize (xyz (projection_matrix_inverse_transpose) - wpos); |
|
|
297 | |
|
|
298 | vsh->stop (); |
|
|
299 | |
|
|
300 | fragment_shader fsh; |
|
|
301 | |
|
|
302 | fsh->start (); |
|
|
303 | |
|
|
304 | xyz (fout.frag_color) = noise3 (x (fin.frag_coord) * y (fin.frag_coord)); |
|
|
305 | |
|
|
306 | temp_1f spec_expon; |
|
|
307 | spec_expon = 200; |
|
|
308 | |
|
|
309 | fsh->stop (); |
|
|
310 | |
|
|
311 | //abort (); |
|
|
312 | } |
273 | } |
313 | |
274 | |
314 | } |
275 | } |
315 | |
276 | |