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

Comparing cvsroot/libgender/material.C (file contents):
Revision 1.13 by root, Sat Oct 23 02:27:51 2004 UTC vs.
Revision 1.58 by root, Mon Feb 7 08:51:18 2005 UTC

3 3
4#include <algorithm> 4#include <algorithm>
5 5
6#include "opengl.h" 6#include "opengl.h"
7#include "material.h" 7#include "material.h"
8#include "view.h"
8#include "util.h" 9#include "util.h"
9
10namespace shader {
11
12 refcounted::~refcounted ()
13 {
14#if 0
15 if (refcnt)
16 abort ();
17#endif
18 }
19
20 const char str_float [] = "float";
21 const char str_vec2 [] = "vec2";
22 const char str_vec3 [] = "vec3";
23 const char str_vec4 [] = "vec4";
24 const char str_mat2 [] = "mat2";
25 const char str_mat3 [] = "mat3";
26 const char str_mat4 [] = "mat4";
27
28 const char str_sampler_1d [] = "sampler1D";
29 const char str_sampler_1d_shadow [] = "sampler1DShadow";
30 const char str_sampler_2d [] = "sampler2D";
31 const char str_sampler_2d_shadow [] = "sampler2DShadow";
32 const char str_sampler_2d_rect [] = "sampler2DRect";
33 const char str_sampler_2d_rect_shadow [] = "sampler2DRectShadow";
34 const char str_sampler_3d [] = "sampler3D";
35 const char str_sampler_3d_rect [] = "sampler3DRect";
36 const char str_sampler_cube [] = "samplerCube";
37
38 unsigned int var_i::next_id = 0;
39
40 var_i::var_i (CGtype cgtype, const char *typestr)
41 : typestr (typestr)
42 {
43 //param = cgCreateParameter (cg_context, cgtype);
44 }
45
46 var_i::~var_i ()
47 {
48 //cgDestroyParameter (param);
49 }
50
51 temporary_i::temporary_i (CGtype cgtype, const char *strtype)
52 : var_i (cgtype, strtype)
53 {
54 sprintf (name, "T%lx_%d", ((long)this >> 4) & 0xfff, ++next_id);
55 }
56
57 uniform_i::uniform_i (CGtype cgtype, const char *strtype)
58 : var_i (cgtype, strtype), dirty (true)
59 {
60 sprintf (name, "U%lx_%d", ((long)this >> 4) & 0xfff, ++next_id);
61 }
62
63 ////////////////////////////////////////////////////////////////////////////
64
65 void var_i::build_decl (ostringstream &b)
66 {
67 b << typestr << ' ' << name;
68 }
69
70 void uniform_i::build_decl (ostringstream &b)
71 {
72 b << "uniform " << typestr << ' ' << name;
73 }
74
75 void stream_i::build_decl (ostringstream &b)
76 {
77 b << typestr << ' ' << name;
78 }
79
80 ////////////////////////////////////////////////////////////////////////////
81
82 void glvar_i::build (shader_builder &b)
83 {
84 b << name;
85 }
86
87 void var_i::build (shader_builder &b)
88 {
89 b << name;
90 }
91
92 void uniform_i::build (shader_builder &b)
93 {
94 var_i::build (b);
95
96 if (find (b.refs.begin (), b.refs.end (), uniform (*this)) == b.refs.end ())
97 b.refs.push_back (*this);
98 }
99
100 void stream_i::build (shader_builder &b)
101 {
102 var_i::build (b);
103
104 if (find (b.streams.begin (), b.streams.end (), var (*this)) == b.streams.end ())
105 b.streams.push_back (*this);
106 }
107
108 void temporary_i::build (shader_builder &b)
109 {
110 var_i::build (b);
111
112 if (find (b.temps.begin (), b.temps.end (), var (*this)) == b.temps.end ())
113 b.temps.push_back (*this);
114 }
115
116 void fragment_vector_i::build (shader_builder &b)
117 {
118 for (vector<fragment>::iterator i = begin (); i != end (); i++)
119 (*i)->build (b);
120 }
121
122 void fragment_const_string_i::build (shader_builder &b)
123 {
124 b << str;
125 }
126
127 void statement_i::build (shader_builder &b)
128 {
129 b << " ";
130 fragment_vector_i::build (b);
131 b << ";\n";
132 }
133
134 ////////////////////////////////////////////////////////////////////////////
135
136 struct vin vin;
137
138 varying_3f vin::position_3f ("gl_Vertex");
139 varying_4f vin::position_4f ("gl_Vertex");
140 varying_3f vin::normal_3f ("gl_Normal");
141 varying_3f vin::color_3f ("gl_Color");
142 varying_4f vin::color_4f ("gl_Color");
143 varying_3f vin::color2_3f ("gl_SecondaryColor");
144 varying_4f vin::color2_4f ("gl_SecondaryColor");
145 varying_1f vin::texcoord_1f[8] =
146 {
147 varying_1f ("gl_TexCoord[0]"), varying_1f ("gl_TexCoord[1]"), varying_1f ("gl_TexCoord[2]"), varying_1f ("gl_TexCoord[3]"),
148 varying_1f ("gl_TexCoord[4]"), varying_1f ("gl_TexCoord[5]"), varying_1f ("gl_TexCoord[6]"), varying_1f ("gl_TexCoord[7]"),
149 };
150 varying_2f vin::texcoord_2f[8] =
151 {
152 varying_2f ("gl_TexCoord[0]"), varying_2f ("gl_TexCoord[1]"), varying_2f ("gl_TexCoord[2]"), varying_2f ("gl_TexCoord[3]"),
153 varying_2f ("gl_TexCoord[4]"), varying_2f ("gl_TexCoord[5]"), varying_2f ("gl_TexCoord[6]"), varying_2f ("gl_TexCoord[7]"),
154 };
155 varying_3f vin::texcoord_3f[8] =
156 {
157 varying_3f ("gl_TexCoord[0]"), varying_3f ("gl_TexCoord[1]"), varying_3f ("gl_TexCoord[2]"), varying_3f ("gl_TexCoord[3]"),
158 varying_3f ("gl_TexCoord[4]"), varying_3f ("gl_TexCoord[5]"), varying_3f ("gl_TexCoord[6]"), varying_3f ("gl_TexCoord[7]"),
159 };
160 varying_4f vin::texcoord_4f[8] =
161 {
162 varying_4f ("gl_TexCoord[0]"), varying_4f ("gl_TexCoord[1]"), varying_4f ("gl_TexCoord[2]"), varying_4f ("gl_TexCoord[3]"),
163 varying_4f ("gl_TexCoord[4]"), varying_4f ("gl_TexCoord[5]"), varying_4f ("gl_TexCoord[6]"), varying_4f ("gl_TexCoord[7]"),
164 };
165 varying_1f vin::psize_1f ("PSIZE");
166
167 glvar vout::position ("gl_Position");
168 glvar vout::point_size ("gl_PointSize");
169 glvar vout::clip_vertex ("gl_ClipVertex");
170 glvar vout::front_color ("gl_FontColor");
171 glvar vout::back_color ("gl_BackColor");
172 glvar vout::front_secondary_color ("gl_FrontSecondaryColor");
173 glvar vout::back_secondary_color ("gl_BackSecondaryColor");
174 glvar vout::texcoord[8] =
175 {
176 glvar ("gl_TexCoord[0]"), glvar ("gl_TexCoord[1]"), glvar ("gl_TexCoord[2]"), glvar ("gl_TexCoord[3]"),
177 glvar ("gl_TexCoord[4]"), glvar ("gl_TexCoord[5]"), glvar ("gl_TexCoord[6]"), glvar ("gl_TexCoord[7]"),
178 };
179 glvar vout::fog_frag_coord ("gl_FogFragCoord");
180
181 glvar fin::frag_coord ("gl_FragCoord");
182 glvar fin::color ("gl_Color");
183 glvar fin::secondary_color ("gl_SecondaryColor");
184 glvar fin::texcoord[8] =
185 {
186 glvar ("gl_TexCoord[0]"), glvar ("gl_TexCoord[1]"), glvar ("gl_TexCoord[2]"), glvar ("gl_TexCoord[3]"),
187 glvar ("gl_TexCoord[4]"), glvar ("gl_TexCoord[5]"), glvar ("gl_TexCoord[6]"), glvar ("gl_TexCoord[7]"),
188 };
189 glvar fin::fog_frag_coord ("gl_FogFragCoord");
190
191 glvar fout::frag_color ("gl_FragColor");
192 glvar fout::frag_depth ("gl_FragDepth");
193 glvar fout::frag_data[2] = { glvar ("gl_FragData[0]"), glvar ("gl_FragData[1]") };
194
195#if 0
196 varying_4f fin::position_4f ("gl_Position");
197 varying_4f fin::color_4f ("gl_Color");
198 varying_4f fin::color2_4f ("gl_SecondaryColor");
199 varying_4f fin::texcoord_4f[8] =
200 {
201 varying_4f ("gl_TexCoord[0]"), varying_4f ("gl_TexCoord[1]"), varying_4f ("gl_TexCoord[2]"), varying_4f ("gl_TexCoord[3]"),
202 varying_4f ("gl_TexCoord[4]"), varying_4f ("gl_TexCoord[5]"), varying_4f ("gl_TexCoord[6]"), varying_4f ("gl_TexCoord[7]"),
203 };
204#endif
205
206 struct fout fout;
207
208#if 0
209 varying_4f fout::color_4f ("gl_FragColor");
210 varying_1f fout::depth_1f ("gl_FragDepth");
211#endif
212
213 ////////////////////////////////////////////////////////////////////////////
214
215 shader_object_i::shader_object_i (GLenum type)
216 : type (type)
217 {
218 id = glCreateShaderObjectARB (type);
219 }
220
221 shader_object_i::~shader_object_i ()
222 {
223 glDeleteObjectARB (id);
224 }
225
226 string shader_object_i::source ()
227 {
228 shader_builder b;
229 build (b);
230 ostringstream os;
231
232 for (vector<uniform>::iterator i = b.refs.begin (); i != b.refs.end (); i++)
233 {
234 (*i)->build_decl (os);
235 os << ";\n";
236 }
237
238#if 0
239 // not neccessary right now, as GLSL is rich on predefinitions
240 for (vector<var>::iterator i = b.streams.begin (); i != b.streams.end (); i++)
241 {
242 if (vout.is (*i) || fout.is (*i))
243 os << "varying ";
244 else if (vin.is (*i))
245 os << "attribute ";
246
247 (*i)->build_decl (os);
248 os << ";\n";
249 }
250#endif
251
252 os << "\nvoid main (void)\n{\n";
253
254 for (vector<var>::iterator i = b.temps.begin (); i != b.temps.end (); i++)
255 {
256 os << " ";
257 (*i)->build_decl (os);
258 os << ";\n";
259 }
260
261 os << "\n";
262 os << b.source.str ();
263 os << "}\n";
264
265 return os.str ();
266 }
267
268 void shader_object_i::compile ()
269 {
270 string src = source ();
271 const char *sptr = src.data ();
272 const int slen = src.size ();
273
274 printf ("SOURCE<%s>\n", src.c_str ());
275
276 glShaderSourceARB (id, 1, &sptr, &slen);
277 glCompileShaderARB (id);
278
279 GLint compiled;
280 glGetObjectParameterivARB (id, GL_OBJECT_COMPILE_STATUS_ARB, &compiled);
281
282 if (!compiled)
283 {
284 char infolog[8192];
285 glGetInfoLogARB (id, 8192, NULL, infolog);
286 printf ("INFOLOG<%s>\n", infolog);
287 abort ();
288 }
289 }
290
291void debdebdebdebug ()//D
292{
293 fragment_shader p;
294 temp_4f t1, t2;
295 sampler_2d s2d (1);
296
297 //p << t2 << " = texture2D (" << s2d << ", " << vin.position_4f << ".xy)";
298 //p << t1 << " = mul (" << vin.position_4f << "," << mvp << ")";
299 p << fout.frag_color << " = vec4(1,1,1,0)";
300
301 p->compile ();
302}
303
304}
305
306material::~material ()
307{
308}
309
310void
311simple_material::begin ()
312{
313 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse);
314 glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular);
315 glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission);
316 glMaterialf (GL_FRONT, GL_SHININESS, shininess);
317}
318
319void
320simple_material::end ()
321{
322}
323
324void
325osama_material::begin ()
326{
327 cgGLEnableProfile (vsh_profile);
328 cgGLEnableProfile (fsh_profile);
329 cgGLEnableTextureParameter (g_Texture);
330}
331
332void
333osama_material::end ()
334{
335 cgGLDisableTextureParameter (g_Texture);
336 // cgGLUnbindProgram (vsh_profile);
337 // cgGLUnbindProgram (fsh_profile);
338 cgGLDisableProfile (vsh_profile);
339 cgGLDisableProfile (fsh_profile);
340}
341 10
342GLuint 11GLuint
343texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) 12texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
344{ 13{
345 GLuint textur; 14 GLuint name;
346 int w, h;
347 SDL_Surface *image; 15 SDL_Surface *image;
348 SDL_Rect area; 16 SDL_Rect area;
349 Uint32 saved_flags; 17 Uint32 saved_flags;
350 Uint8 saved_alpha; 18 Uint8 saved_alpha;
351 19
352 /* Use the surface width and height expanded to powers of 2 */ 20 /* Use the surface width and height expanded to powers of 2 */
353 //w = power_of_two (surface->w);
354 //h = power_of_two (surface->h);
355 w = power_of_two (surface->w);
356 h = power_of_two (surface->h);
357 tex2oord[0] = 0.0f; /* Min X */ 21 tex2oord[0] = 0.F; /* Min X */
358 tex2oord[1] = 0.0f; /* Min Y */ 22 tex2oord[1] = 0.F; /* Min Y */
359 tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ 23 tex2oord[2] = 1.F; /* Max X */
360 tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ 24 tex2oord[3] = 1.F; /* Max Y */
361 25
362 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, 26 image = SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, 32,
363#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 27#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
364 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 28 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
365#else 29#else
366 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF 30 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
367#endif 31#endif
386 /* Restore the alpha blending attributes */ 50 /* Restore the alpha blending attributes */
387 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) 51 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
388 SDL_SetAlpha (surface, saved_flags, saved_alpha); 52 SDL_SetAlpha (surface, saved_flags, saved_alpha);
389 53
390 /* Create an OpenGL texture for the image */ 54 /* Create an OpenGL texture for the image */
391 glGenTextures (1, &textur); 55 glGenTextures (1, &name);
392 glBindTexture (GL_TEXTURE_2D, textur); 56 glBindTexture (GL_TEXTURE_2D, name);
393 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 57 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
394 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);
395 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); // GENERATE_MIPMAP_SGIS 59 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
396 glTexImage2D (GL_TEXTURE_2D, 60 glTexImage2D (GL_TEXTURE_2D, 0,
397 0,
398 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); 61 GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
399 SDL_FreeSurface (image); /* No longer needed */ 62 SDL_FreeSurface (image); /* No longer needed */
400 63
401 return textur; 64 return name;
402} 65}
403 66
404CGcontext cgc; 67material::~material ()
405
406void
407init_shaders ()
408{ 68{
409 cgc = cgCreateContext ();
410} 69}
411 70
71void material::enable (view &ctx)
72{
73 pass_data::matmap_t &matmap = ctx.pass->matmap;
74
75 pass_data::matmap_t::iterator i = matmap.find (this);
76
77 if (i == matmap.end ())
78 {
79 string vsh_src, fsh_src;
80
81 shader::shader_builder::start ();
82
83 if (ctx.pass->l)
84 ctx.pass->l->vsh ();
85
86 vsh (ctx);
87
88 // compute logarithmic depth - see the frustum matrix in view.C
89 {
90 using namespace shader::compile;
91
92 temp_1f lz;
93
94 // TODO: negative z is not calculated in an acceptable way, clipping does horrible things(?)
95 lz = z (vout.position);
96#if 0
97 lz = (log (max (lz, 0) + 1) / log (1e30)) - 1;
98#else
99 lz = ifelse (lz <= 0,
100 0,
101 log (lz + 1) / log (1e30)
102 ) - 1;
103#endif
104 z (vout.position) = lz * w (vout.position);
105 }
106
107 vsh_src = shader::shader_builder::stop ();
108
109 shader::shader_builder::start ();
110
111 if (ctx.pass->l)
112 {
113 ctx.pass->l->fsh ();
114 fsh (ctx);
115 }
116 else
117 // many drivers need both vertex and fragment shader, 'caboom!' otherwise
118 shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.);
119
120 fsh_src = shader::shader_builder::stop ();
121
122 shader::program_object po = shader::get_program (vsh_src, fsh_src);
123 matmap.insert (pass_data::matmap_t::value_type (this, po));
124
125 po->enable ();
126 }
127 else
128 i->second->enable ();
129
130 if (ctx.pass->l)
131 ctx.pass->l->enable (ctx);
132}
133
134void material::disable (view &ctx)
135{
136}
137
138test_material::test_material ()
139//: tex ("textures/osama.jpg"), texvar (tex.name)
140: tex ("textures/rockwall.jpg"), texvar (tex.name)
141, norm ("textures/rockwall_normal.jpg"), normvar (norm.name)
142{
143}
144
145void mat_gouraud_shaded::enable (view &ctx)
146{
147 material::enable (ctx);
148
149 sh_colour->set (vec3 (c.r, c.g, c.b) * (1.F / 255.F));
150}
151
152void mat_gouraud_shaded::disable (view &ctx)
153{
154 material::disable (ctx);
155}
156
157void mat_gouraud_shaded::vsh (view &ctx)
158{
159 using namespace shader::compile;
160 std_vsh ();
161
162 if (ctx.pass->l)
163 f_normal = normal_matrix * vin.normal;
164}
165
166void mat_gouraud_shaded::fsh (view &ctx)
167{
168 using namespace shader::compile;
169
170 if (ctx.pass->l)
171 {
172 temp_1f fac;
173
174 fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec));
175
176 xyz (fout.frag_color) = ctx.pass->l->sh_colour * sh_colour * fac;
177 }
178}
179
180static shader::varying_2f texcoord;
181static shader::varying_3f normal;
182
183void test_material::vsh (view &ctx)
184{
185 using namespace shader::compile;
186
187 std_vsh ();
188
189 if (ctx.pass->l)
190 {
191 texcoord = xy (vin.tex_coord[0]);
192 normal = normal_matrix * vin.normal;
193 }
194}
195
196void test_material::fsh (view &ctx)
197{
198 using namespace shader::compile;
199
200 if (ctx.pass->l)
201 {
202 temp_3f lc;
203 temp_1f fac;
204
205 temp_3f rot1, rot2, rot3;
206
207 yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F);
208
209 //rot1 = normal_matrix * rot1;
210
211 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
212 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
213
214 normal = mat3 (rot1, rot2, rot3) * normal;
215
216 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
217 fac = max (pow (max (fac, 0.0), 6), 0.3);
218 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
219 * ctx.pass->l->sh_colour;
220 //xyz (fout.frag_color) = lc * fac;
221 }
222}
223
224void test_material::enable (view &ctx)
225{
226 material::enable (ctx);
227 texvar->enable ();
228 normvar->enable ();
229}
230
231void test_material::disable (view &ctx)
232{
233 normvar->disable ();
234 texvar->disable ();
235 material::disable (ctx);
236}
237
238test_material *testmat;
239mat_gouraud_shaded *testmat2;
240

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines