ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/material.C
Revision: 1.13
Committed: Sat Oct 23 02:27:51 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +125 -78 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <cstdlib>
2 #include <cstring>
3
4 #include <algorithm>
5
6 #include "opengl.h"
7 #include "material.h"
8 #include "util.h"
9
10 namespace 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
291 void 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
306 material::~material ()
307 {
308 }
309
310 void
311 simple_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
319 void
320 simple_material::end ()
321 {
322 }
323
324 void
325 osama_material::begin ()
326 {
327 cgGLEnableProfile (vsh_profile);
328 cgGLEnableProfile (fsh_profile);
329 cgGLEnableTextureParameter (g_Texture);
330 }
331
332 void
333 osama_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
342 GLuint
343 texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
344 {
345 GLuint textur;
346 int w, h;
347 SDL_Surface *image;
348 SDL_Rect area;
349 Uint32 saved_flags;
350 Uint8 saved_alpha;
351
352 /* 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 */
358 tex2oord[1] = 0.0f; /* Min Y */
359 tex2oord[2] = (GLfloat) surface->w / w; /* Max X */
360 tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */
361
362 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
363 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
364 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
365 #else
366 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
367 #endif
368 );
369
370 if (image == NULL)
371 return 0;
372
373 /* Save the alpha blending attributes */
374 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
375 saved_alpha = surface->format->alpha;
376 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
377 SDL_SetAlpha (surface, 0, 0);
378
379 /* Copy the surface into the GL texture image */
380 area.x = 0;
381 area.y = 0;
382 area.w = surface->w;
383 area.h = surface->h;
384 SDL_BlitSurface (surface, &area, image, &area);
385
386 /* Restore the alpha blending attributes */
387 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
388 SDL_SetAlpha (surface, saved_flags, saved_alpha);
389
390 /* Create an OpenGL texture for the image */
391 glGenTextures (1, &textur);
392 glBindTexture (GL_TEXTURE_2D, textur);
393 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
394 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
396 glTexImage2D (GL_TEXTURE_2D,
397 0,
398 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
399 SDL_FreeSurface (image); /* No longer needed */
400
401 return textur;
402 }
403
404 CGcontext cgc;
405
406 void
407 init_shaders ()
408 {
409 cgc = cgCreateContext ();
410 }
411