ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/material.C
(Generate patch)

Comparing libgender/material.C (file contents):
Revision 1.11 by root, Thu Oct 21 22:28:42 2004 UTC vs.
Revision 1.56 by root, Mon Feb 7 07:37:00 2005 UTC

1#include <cstdlib>
2#include <cstring>
3
4#include <algorithm>
5
1#include "opengl.h" 6#include "opengl.h"
2#include "material.h" 7#include "material.h"
3 8#include "view.h"
4#include <cstdlib> 9#include "util.h"
5#include <cstring>
6
7#include <algorithm>
8
9namespace shader {
10
11 refcounted::~refcounted ()
12 {
13#if 0
14 if (refcnt)
15 abort ();
16#endif
17 }
18
19 const char str_float [] = "float";
20 const char str_float2 [] = "float2";
21 const char str_float3 [] = "float3";
22 const char str_float4 [] = "float4";
23 const char str_float4x4 [] = "float4x4";
24
25 const char str_sampler1d [] = "sampler1d";
26 const char str_sampler2d [] = "sampler2d";
27 const char str_sampler3d [] = "sampler3d";
28 const char str_samplerCUBE [] = "samplerCUBE";
29 const char str_samplerRECT [] = "samplerRECT";
30
31 static CGcontext cg_context = cgCreateContext ();
32
33 int var_i::next_name = 0;
34
35 var_i::var_i (CGtype cgtype, const char *typestr)
36 : typestr (typestr)
37 {
38 name = ++next_name;
39 param = cgCreateParameter (cg_context, cgtype);
40 }
41
42 var_i::~var_i ()
43 {
44 cgDestroyParameter (param);
45 }
46
47 void var_i::build_decl (ostringstream &b)
48 {
49 b << typestr << ' ' << "V" << name;
50 }
51
52 void uniform_i::build_decl (ostringstream &b)
53 {
54 b << "uniform " << typestr << ' ' << "V" << name;
55 }
56
57 void stream_i::build_decl (ostringstream &b)
58 {
59 b << typestr << ' ' << "V" << name;
60 b << " : " << binding;
61 }
62
63 void var_i::build (shader_builder &b)
64 {
65 b << "V" << name;
66 }
67
68 void uniform_i::build (shader_builder &b)
69 {
70 var_i::build (b);
71
72 if (find (b.refs.begin (), b.refs.end (), uniform (*this)) == b.refs.end ())
73 b.refs.push_back (*this);
74 }
75
76 void stream_i::build (shader_builder &b)
77 {
78 var_i::build (b);
79
80 if (find (b.streams.begin (), b.streams.end (), var (*this)) == b.streams.end ())
81 b.streams.push_back (*this);
82 }
83
84 void temporary_i::build (shader_builder &b)
85 {
86 var_i::build (b);
87
88 if (find (b.temps.begin (), b.temps.end (), var (*this)) == b.temps.end ())
89 b.temps.push_back (*this);
90 }
91
92 void fragment_const_string_i::build (shader_builder &b)
93 {
94 b << str;
95 }
96
97 struct vin vin;
98
99 // MUST update is-function below
100 varying_3f vin::position_3f ("POSITION");
101 varying_4f vin::position_4f ("POSITION");
102 varying_3f vin::normal_3f ("NORMAL");
103 varying_4f vin::normal_4f ("NORMAL");
104 varying_3f vin::color0_3f ("COLOR0");
105 varying_4f vin::color0_4f ("COLOR0");
106 varying_3f vin::color1_3f ("COLOR1");
107 varying_4f vin::color1_4f ("COLOR1");
108 varying_1f vin::texcoord_1f[8] =
109 {
110 varying_1f ("TEXCOORD0"), varying_1f ("TEXCOORD1"), varying_1f ("TEXCOORD2"), varying_1f ("TEXCOORD3"),
111 varying_1f ("TEXCOORD4"), varying_1f ("TEXCOORD5"), varying_1f ("TEXCOORD6"), varying_1f ("TEXCOORD7"),
112 };
113 varying_2f vin::texcoord_2f[8] =
114 {
115 varying_2f ("TEXCOORD0"), varying_2f ("TEXCOORD1"), varying_2f ("TEXCOORD2"), varying_2f ("TEXCOORD3"),
116 varying_2f ("TEXCOORD4"), varying_2f ("TEXCOORD5"), varying_2f ("TEXCOORD6"), varying_2f ("TEXCOORD7"),
117 };
118 varying_3f vin::texcoord_3f[8] =
119 {
120 varying_3f ("TEXCOORD0"), varying_3f ("TEXCOORD1"), varying_3f ("TEXCOORD2"), varying_3f ("TEXCOORD3"),
121 varying_3f ("TEXCOORD4"), varying_3f ("TEXCOORD5"), varying_3f ("TEXCOORD6"), varying_3f ("TEXCOORD7"),
122 };
123 varying_4f vin::texcoord_4f[8] =
124 {
125 varying_4f ("TEXCOORD0"), varying_4f ("TEXCOORD1"), varying_4f ("TEXCOORD2"), varying_4f ("TEXCOORD3"),
126 varying_4f ("TEXCOORD4"), varying_4f ("TEXCOORD5"), varying_4f ("TEXCOORD6"), varying_4f ("TEXCOORD7"),
127 };
128 varying_1f vin::psize_1f ("PSIZE");
129 varying_1f vin::attr6_1f ("ATTR6");
130 varying_1f vin::attr7_1f ("ATTR7");
131 varying_2f vin::attr6_2f ("ATTR6");
132 varying_2f vin::attr7_2f ("ATTR7");
133 varying_3f vin::attr6_3f ("ATTR6");
134 varying_3f vin::attr7_3f ("ATTR7");
135 varying_4f vin::attr6_4f ("ATTR6");
136 varying_4f vin::attr7_4f ("ATTR7");
137
138 bool vin::is (const var &r)
139 {
140 return r == vin::position_3f
141 || r == vin::position_4f
142 || r == vin::normal_3f
143 || r == vin::normal_4f
144 || r == vin::color0_3f
145 || r == vin::color0_4f
146 || r == vin::color1_3f
147 || r == vin::color1_4f
148 || r == vin::texcoord_1f[0] || r == vin::texcoord_1f[1] || r == vin::texcoord_1f[2] || r == vin::texcoord_1f[3]
149 || r == vin::texcoord_1f[4] || r == vin::texcoord_1f[5] || r == vin::texcoord_1f[6] || r == vin::texcoord_1f[7]
150 || r == vin::texcoord_2f[0] || r == vin::texcoord_2f[1] || r == vin::texcoord_2f[2] || r == vin::texcoord_2f[3]
151 || r == vin::texcoord_2f[4] || r == vin::texcoord_2f[5] || r == vin::texcoord_2f[6] || r == vin::texcoord_2f[7]
152 || r == vin::texcoord_3f[0] || r == vin::texcoord_3f[1] || r == vin::texcoord_3f[2] || r == vin::texcoord_3f[3]
153 || r == vin::texcoord_3f[4] || r == vin::texcoord_3f[5] || r == vin::texcoord_3f[6] || r == vin::texcoord_3f[7]
154 || r == vin::texcoord_4f[0] || r == vin::texcoord_4f[1] || r == vin::texcoord_4f[2] || r == vin::texcoord_4f[3]
155 || r == vin::texcoord_4f[4] || r == vin::texcoord_4f[5] || r == vin::texcoord_4f[6] || r == vin::texcoord_4f[7]
156 || r == vin::psize_1f
157 || r == vin::attr6_1f
158 || r == vin::attr7_1f
159 || r == vin::attr6_2f
160 || r == vin::attr7_2f
161 || r == vin::attr6_3f
162 || r == vin::attr7_3f
163 || r == vin::attr6_4f
164 || r == vin::attr7_4f;
165 }
166
167 struct fin fin;
168 struct fin &vout = fin;
169
170 // MUST update is-function below
171 varying_4f fin::position_4f ("HPOS");
172 varying_4f fin::color0_4f ("COLOR0");
173 varying_4f fin::color1_4f ("COLOR1");
174 varying_4f fin::texcoord_4f[8] =
175 {
176 varying_4f ("TEXCOORD0"), varying_4f ("TEXCOORD1"), varying_4f ("TEXCOORD2"), varying_4f ("TEXCOORD3"),
177 varying_4f ("TEXCOORD4"), varying_4f ("TEXCOORD5"), varying_4f ("TEXCOORD6"), varying_4f ("TEXCOORD7"),
178 };
179
180 bool fin::is (const var &r)
181 {
182 return r == fin::position_4f
183 || r == fin::color0_4f
184 || r == fin::color1_4f
185 || r == fin::texcoord_4f[0]
186 || r == fin::texcoord_4f[1]
187 || r == fin::texcoord_4f[2]
188 || r == fin::texcoord_4f[3]
189 || r == fin::texcoord_4f[4]
190 || r == fin::texcoord_4f[5]
191 || r == fin::texcoord_4f[6]
192 || r == fin::texcoord_4f[7];
193 }
194
195 struct fout fout;
196
197 // MUST update is-function below
198 varying_4f fout::color0_4f ("COLOR0");
199 varying_3f fout::color0_3f ("COLOR0");
200 varying_4f fout::color1_4f ("COLOR1");
201 varying_3f fout::color1_3f ("COLOR1");
202 varying_1f fout::depth_1f ("DEPTH");
203
204 bool fout::is (const var &r)
205 {
206 return r == fout::color0_4f
207 || r == fout::color0_3f
208 || r == fout::color1_4f
209 || r == fout::color1_3f
210 || r == fout::depth_1f;
211 }
212
213 uniform_matrix_f mvp, mv, proj;
214
215 void fragment_vector_i::build (shader_builder &b)
216 {
217 for (vector<fragment>::iterator i = begin (); i != end (); i++)
218 (*i)->build (b);
219 }
220
221 void shader_program::print ()
222 {
223 shader_builder b;
224 build (b);
225 ostringstream os;
226
227 os << "void main (\n";
228 for (vector<var>::iterator i = b.streams.begin (); i != b.streams.end (); i++)
229 {
230 os << " ";
231
232 if (is_vertex)
233 {
234 if (vin.is (*i)) os << "in ";
235 if (vout.is (*i)) os << "out ";
236 }
237 else
238 {
239 if (fin.is (*i)) os << "in ";
240 if (fout.is (*i)) os << "out ";
241 }
242
243 (*i)->build_decl (os);
244 os << ",\n";
245 }
246
247 for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++)
248 {
249 os << " ";
250 (*i)->build_decl (os);
251 os << ",\n";
252 }
253
254 os << " uniform float unused_cg_broken_syntax\n";
255 os << ")\n{\n";
256
257 for (vector<var>::iterator i = b.temps.begin (); i != b.temps.end (); i++)
258 {
259 (*i)->build_decl (os);
260 os << ";\n";
261 }
262
263 os << "\n";
264 os << b.source.str ();
265 os << "\n}\n";
266
267 printf ("%s\n", os.str ().c_str ());
268
269 }
270
271void debdebdebdebug ()//D
272{
273 vertex_program p;
274 temp_4f t1, t2;
275 sampler2d s2d (1);
276
277 p << t2 << " = tex2D (" << s2d << ", " << vin.position_4f << ".xy);\n";
278 p << t1 << " = mul (" << vin.position_4f << "," << mvp << ");\n";
279 p << vout.position_4f << " = " << t1 << ";\n";
280
281 p.print ();
282 //exit (0);
283}
284
285}
286
287material::~material ()
288{
289}
290
291void
292simple_material::begin ()
293{
294 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse);
295 glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular);
296 glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission);
297 glMaterialf (GL_FRONT, GL_SHININESS, shininess);
298}
299
300void
301simple_material::end ()
302{
303}
304
305void
306osama_material::begin ()
307{
308 cgGLEnableProfile (vsh_profile);
309 cgGLEnableProfile (fsh_profile);
310 cgGLEnableTextureParameter (g_Texture);
311}
312
313void
314osama_material::end ()
315{
316 cgGLDisableTextureParameter (g_Texture);
317 // cgGLUnbindProgram (vsh_profile);
318 // cgGLUnbindProgram (fsh_profile);
319 cgGLDisableProfile (vsh_profile);
320 cgGLDisableProfile (fsh_profile);
321}
322 10
323GLuint 11GLuint
324texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) 12texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
325{ 13{
326 GLuint textur; 14 GLuint name;
327 int w, h;
328 SDL_Surface *image; 15 SDL_Surface *image;
329 SDL_Rect area; 16 SDL_Rect area;
330 Uint32 saved_flags; 17 Uint32 saved_flags;
331 Uint8 saved_alpha; 18 Uint8 saved_alpha;
332 19
333 /* Use the surface width and height expanded to powers of 2 */ 20 /* Use the surface width and height expanded to powers of 2 */
334 //w = power_of_two (surface->w);
335 //h = power_of_two (surface->h);
336 w = power_of_two (surface->w);
337 h = power_of_two (surface->h);
338 tex2oord[0] = 0.0f; /* Min X */ 21 tex2oord[0] = 0.F; /* Min X */
339 tex2oord[1] = 0.0f; /* Min Y */ 22 tex2oord[1] = 0.F; /* Min Y */
340 tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ 23 tex2oord[2] = 1.F; /* Max X */
341 tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ 24 tex2oord[3] = 1.F; /* Max Y */
342 25
343 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, 26 image = SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, 32,
344#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 27#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
345 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 28 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
346#else 29#else
347 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF 30 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
348#endif 31#endif
367 /* Restore the alpha blending attributes */ 50 /* Restore the alpha blending attributes */
368 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) 51 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
369 SDL_SetAlpha (surface, saved_flags, saved_alpha); 52 SDL_SetAlpha (surface, saved_flags, saved_alpha);
370 53
371 /* Create an OpenGL texture for the image */ 54 /* Create an OpenGL texture for the image */
372 glGenTextures (1, &textur); 55 glGenTextures (1, &name);
373 glBindTexture (GL_TEXTURE_2D, textur); 56 glBindTexture (GL_TEXTURE_2D, name);
374 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 57 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
375 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 58 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
376 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); // GENERATE_MIPMAP_SGIS 59 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
377 glTexImage2D (GL_TEXTURE_2D, 60 glTexImage2D (GL_TEXTURE_2D,
378 0, 61 0,
379 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); 62 GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
380 SDL_FreeSurface (image); /* No longer needed */ 63 SDL_FreeSurface (image); /* No longer needed */
381 64
382 return textur; 65 return name;
383} 66}
384 67
385CGcontext cgc; 68material::~material ()
386
387void
388init_shaders ()
389{ 69{
390 cgc = cgCreateContext ();
391} 70}
392 71
72void material::enable (view &ctx)
73{
74 pass_data::matmap_t &matmap = ctx.pass->matmap;
75
76 pass_data::matmap_t::iterator i = matmap.find (this);
77
78 if (i == matmap.end ())
79 {
80 string vsh_src, fsh_src;
81
82 shader::shader_builder::start ();
83
84 if (ctx.pass->l)
85 ctx.pass->l->vsh ();
86
87 vsh (ctx);
88
89 // compute logarithmic depth - see the frustum matrix in view.C
90 {
91 using namespace shader::compile;
92
93 temp_1f lz;
94
95 // TODO: negative z is not calculated in an acceptable way, clipping does horrible things(?)
96 lz = z (vout.position);
97#if 0
98 lz = (log (max (lz, 0) + 1) / log (1e30)) - 1;
99#else
100 lz = ifelse (lz <= 0,
101 0,
102 log (lz + 1) / log (1e30)
103 ) - 1;
104#endif
105 z (vout.position) = lz * w (vout.position);
106 }
107
108 vsh_src = shader::shader_builder::stop ();
109
110 shader::shader_builder::start ();
111
112 if (ctx.pass->l)
113 {
114 ctx.pass->l->fsh ();
115 fsh (ctx);
116 }
117 else
118 // many drivers need both vertex and fragment shader, 'caboom!' otherwise
119 shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.);
120
121 fsh_src = shader::shader_builder::stop ();
122
123 shader::program_object po = shader::get_program (vsh_src, fsh_src);
124 matmap.insert (pass_data::matmap_t::value_type (this, po));
125
126 po->enable ();
127 }
128 else
129 i->second->enable ();
130
131 if (ctx.pass->l)
132 ctx.pass->l->enable ();
133}
134
135void material::disable (view &ctx)
136{
137}
138
139test_material::test_material ()
140//: tex ("textures/osama.jpg"), texvar (tex.name)
141: tex ("textures/rockwall.jpg"), texvar (tex.name)
142, norm ("textures/rockwall_normal.jpg"), normvar (norm.name)
143{
144}
145
146static shader::varying_3f normal;
147static shader::varying_2f texcoord;
148
149void test_material2::vsh (view &ctx)
150{
151 using namespace shader::compile;
152 std_vsh ();
153
154 if (ctx.pass->l)
155 {
156 normal = normal_matrix * vin.normal;
157 }
158}
159
160void test_material2::fsh (view &ctx)
161{
162 using namespace shader::compile;
163
164 if (ctx.pass->l)
165 {
166 temp_1f fac;
167 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
168 xyz (fout.frag_color) = ctx.pass->l->sh_colour * fac;//normalize (ctx.pass->l->sh_lightvec);
169 }
170}
171
172void test_material::vsh (view &ctx)
173{
174 using namespace shader::compile;
175
176 std_vsh ();
177
178 if (ctx.pass->l)
179 {
180 texcoord = xy (vin.tex_coord[0]);
181 normal = normal_matrix * vin.normal;
182 }
183}
184
185void test_material::fsh (view &ctx)
186{
187 using namespace shader::compile;
188
189 if (ctx.pass->l)
190 {
191 temp_3f lc;
192 temp_1f fac;
193
194 temp_3f rot1, rot2, rot3;
195
196 yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F);
197
198 //rot1 = normal_matrix * rot1;
199
200 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
201 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
202
203 normal = mat3 (rot1, rot2, rot3) * normal;
204
205 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
206 fac = max (pow (max (fac, 0.0), 6), 0.3);
207 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
208 * ctx.pass->l->sh_colour;
209 //xyz (fout.frag_color) = lc * fac;
210 }
211}
212
213void test_material::enable (view &ctx)
214{
215 material::enable (ctx);
216 texvar->enable ();
217 normvar->enable ();
218}
219
220void test_material::disable (view &ctx)
221{
222 normvar->disable ();
223 texvar->disable ();
224 material::disable (ctx);
225}
226
227test_material *testmat;
228test_material2 *testmat2;
229

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines