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.17 by root, Fri Oct 29 16:05:21 2004 UTC vs.
Revision 1.64 by root, Mon Jul 18 04:28:40 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
10material::~material ()
11{
12}
13
14void
15simple_material::enable ()
16{
17 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse);
18 glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular);
19 glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission);
20 glMaterialf (GL_FRONT, GL_SHININESS, shininess);
21}
22
23void
24simple_material::disable ()
25{
26}
27 10
28GLuint 11GLuint
29texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) 12texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
30{ 13{
31 GLuint name; 14 GLuint name;
32 int w, h;
33 SDL_Surface *image; 15 SDL_Surface *image;
34 SDL_Rect area; 16 SDL_Rect area;
35 Uint32 saved_flags; 17 Uint32 saved_flags;
36 Uint8 saved_alpha; 18 Uint8 saved_alpha;
37 19
38 /* Use the surface width and height expanded to powers of 2 */ 20 /* Use the surface width and height expanded to powers of 2 */
39 //w = power_of_two (surface->w);
40 //h = power_of_two (surface->h);
41 w = power_of_two (surface->w);
42 h = power_of_two (surface->h);
43 tex2oord[0] = 0.0f; /* Min X */ 21 tex2oord[0] = 0.F; /* Min X */
44 tex2oord[1] = 0.0f; /* Min Y */ 22 tex2oord[1] = 0.F; /* Min Y */
45 tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ 23 tex2oord[2] = 1.F; /* Max X */
46 tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ 24 tex2oord[3] = 1.F; /* Max Y */
47 25
48 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, 26 image = SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, 32,
49#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 27#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
50 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 28 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
51#else 29#else
52 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF 30 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
53#endif 31#endif
77 glGenTextures (1, &name); 55 glGenTextures (1, &name);
78 glBindTexture (GL_TEXTURE_2D, name); 56 glBindTexture (GL_TEXTURE_2D, name);
79 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 57 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80 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);
81 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 59 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
82 glTexImage2D (GL_TEXTURE_2D, 60 glTexImage2D (GL_TEXTURE_2D, 0,
83 0,
84 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);
85 SDL_FreeSurface (image); /* No longer needed */ 62 SDL_FreeSurface (image); /* No longer needed */
86 63
87 return name; 64 return name;
88} 65}
89 66
67material::~material ()
68{
69}
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 a very acceptable way
95 lz = z (vout.position);
96 lz = sign (lz) * (log (abs (lz) + exp (1e0)) - 1e0) / log (1e30);
97 z (vout.position) = lz * w (vout.position);
98 }
99
100 vsh_src = shader::shader_builder::stop ();
101
102 shader::shader_builder::start ();
103
104 if (ctx.pass->l)
105 {
106 ctx.pass->l->fsh ();
107 fsh (ctx);
108 }
109 else
110 // many drivers need both vertex and fragment shader, 'caboom!' otherwise
111 shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.);
112
113 fsh_src = shader::shader_builder::stop ();
114
115 shader::program_object po = shader::get_program (vsh_src, fsh_src);
116 matmap.insert (pass_data::matmap_t::value_type (this, po));
117
118 po->enable ();
119 }
120 else
121 i->second->enable ();
122
123 if (ctx.pass->l)
124 ctx.pass->l->enable (ctx);
125}
126
127void material::disable (view &ctx)
128{
129}
130
90test_material::test_material () 131test_material::test_material ()
91: tex ("textures/osama.jpg"), texvar (tex.name) 132//: tex ("textures/osama.jpg"), texvar (tex.name)
133: tex ("textures/rockwall.jpg"), texvar (tex.name)
134, norm ("textures/rockwall_normal.jpg"), normvar (norm.name)
92{ 135{
136}
137
138void mat_timed::enable (view &ctx)
139{
140 material::enable (ctx);
141
142 time->set (timer::now);
143 sh_colour->set (vec3 (255, 0, 0) * (1.F / 255.F));
144}
145
146void mat_timed::disable (view &ctx)
147{
148 material::disable (ctx);
149}
150
151void mat_timed::vsh (view &ctx)
152{
93 using namespace shader; 153 using namespace shader::compile;
154 std_vsh ();
94 155
95 p.vsh->start (); 156 if (ctx.pass->l)
157 f_normal = normal_matrix * vin.normal;
158}
96 159
97 temp_4f lightpos; 160void mat_timed::fsh (view &ctx)
98 temp_4f wpos; 161{
162 using namespace shader::compile;
99 163
100 //lightpos = shader::vec4 (0, 10, 0, 1); 164 if (ctx.pass->l)
101 vout.position = shader::gl.model_view_projection_matrix * vin.vertex; 165 {
166 temp_1f fac;
167 temp_3f normal;
168
169 normal = f_normal + 0.3 * pow (sin (f_normal * 3.14159 + time), 3);
170
171 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
172
173 xyz (fout.frag_color) = fac * float3 (0.1,0.5,1);//ctx.pass->l->sh_colour * sh_colour * fac;
174 }
175}
176
177void mat_gouraud_shaded::enable (view &ctx)
178{
179 material::enable (ctx);
180
181 sh_colour->set (vec3 (c.r, c.g, c.b) * (1.F / 255.F));
182}
183
184void mat_gouraud_shaded::disable (view &ctx)
185{
186 material::disable (ctx);
187}
188
189void mat_gouraud_shaded::vsh (view &ctx)
190{
191 using namespace shader::compile;
192 std_vsh ();
193
194 if (ctx.pass->l)
195 f_normal = normal_matrix * vin.normal;
196}
197
198void mat_gouraud_shaded::fsh (view &ctx)
199{
200 using namespace shader::compile;
201
202 if (ctx.pass->l)
203 {
204 temp_1f fac;
205
206 fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec));
207
208 xyz (fout.frag_color) = ctx.pass->l->sh_colour * sh_colour * fac;
209 }
210}
211
212static shader::varying_3f f_normal;
213
214void mat_debug::enable (view &ctx)
215{
216 material::enable (ctx);
217
218 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
219 glEnable (GL_BLEND);
220 glDepthMask (1);
221}
222
223void mat_debug::disable (view &ctx)
224{
225 material::disable (ctx);
226
227 glDisable (GL_BLEND);
228 glDepthMask (0);
229}
230
231void mat_debug::vsh (view &ctx)
232{
233 using namespace shader::compile;
234 std_vsh ();
235
236 if (ctx.pass->l)
237 f_normal = normal_matrix * vin.normal;
238}
239
240void mat_debug::fsh (view &ctx)
241{
242 using namespace shader::compile;
243
244 if (ctx.pass->l)
245 {
246 temp_1f fac;
247
248 fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec));
249
250 xyz (fout.frag_color) = float3 (1.,0.,0.);
251 w (fout.frag_color) = 0.1;
252 }
253}
254
255static shader::varying_2f texcoord;
256static shader::varying_3f normal;
257
258void test_material::vsh (view &ctx)
259{
260 using namespace shader::compile;
261
262 std_vsh ();
263
264 if (ctx.pass->l)
265 {
102 vout.tex_coord[0] = vin.tex_coord[0]; 266 texcoord = xy (vin.tex_coord[0]);
103 //vout.tex_coord[1] = normalize (lightpos - wpos); 267 normal = normal_matrix * vin.normal;
104 //vout.tex_coord[2] = normalize (wpos); 268 }
105 //vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal); 269}
106 //vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos);
107 270
108 p.vsh->end (); 271void test_material::fsh (view &ctx)
109 p.vsh->compile (); 272{
273 using namespace shader::compile;
110 274
111 fragment_shader fsh; 275 if (ctx.pass->l)
276 {
277 temp_3f lc;
278 temp_1f fac;
112 279
113 p.fsh->start (); 280 temp_3f rot1, rot2, rot3;
114 281
282 yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F);
283
284 //rot1 = normal_matrix * rot1;
285
286 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
287 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
288
289 normal = mat3 (rot1, rot2, rot3) * normal;
290
291 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
292 fac = max (pow (max (fac, 0.0), 6), 0.3);
115 xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]); 293 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
116 294 * ctx.pass->l->sh_colour;
117 p.fsh->end (); 295 //xyz (fout.frag_color) = lc * fac;
118 p.fsh->compile (); 296 }
119 p.link ();
120} 297}
121 298
122void test_material::enable () 299void test_material::enable (view &ctx)
123{ 300{
124 p.enable (); 301 material::enable (ctx);
125 texvar->enable (); 302 texvar->enable ();
303 normvar->enable ();
126} 304}
127 305
128void test_material::disable () 306void test_material::disable (view &ctx)
129{ 307{
308 normvar->disable ();
130 texvar->disable (); 309 texvar->disable ();
131 p.disable (); 310 material::disable (ctx);
132} 311}
133 312
313test_material *testmat;
314mat_gouraud_shaded *testmat2;
315mat_timed *testmat3;
316mat_debug *debugmat;
317

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines