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.43 by root, Sat Nov 6 04:31:01 2004 UTC vs.
Revision 1.56 by root, Mon Feb 7 07:37:00 2005 UTC

69{ 69{
70} 70}
71 71
72void material::enable (view &ctx) 72void material::enable (view &ctx)
73{ 73{
74 pass::matmap_t &matmap = ctx.pass_data->matmap; 74 pass_data::matmap_t &matmap = ctx.pass->matmap;
75 75
76 pass::matmap_t::iterator i = matmap.find (this); 76 pass_data::matmap_t::iterator i = matmap.find (this);
77 77
78 if (i == matmap.end ()) 78 if (i == matmap.end ())
79 { 79 {
80 string vsh_src, fsh_src; 80 string vsh_src, fsh_src;
81 81
82 shader::shader_builder::start (); 82 shader::shader_builder::start ();
83 83
84 if (ctx.pass_data->l) 84 if (ctx.pass->l)
85 ctx.pass_data->l->vsh (); 85 ctx.pass->l->vsh ();
86 86
87 vsh (ctx); 87 vsh (ctx);
88 88
89 // compute logarithmic depth - see the frustum matrix in view.C
89 { 90 {
90 using namespace shader::compile; 91 using namespace shader::compile;
91 92
92 z (vout.position) = (log2 (z (vout.position) + 1.F) * 1.82F - 1); 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
93 //z (vout.position) = z (vout.position) - 1; 105 z (vout.position) = lz * w (vout.position);
94 } 106 }
95 107
96 vsh_src = shader::shader_builder::stop (); 108 vsh_src = shader::shader_builder::stop ();
97 109
98 shader::shader_builder::start (); 110 shader::shader_builder::start ();
99 111
100 if (ctx.pass_data->l) 112 if (ctx.pass->l)
101 { 113 {
102 ctx.pass_data->l->fsh (); 114 ctx.pass->l->fsh ();
103
104 fsh (ctx); 115 fsh (ctx);
105 } 116 }
106 else 117 else
118 // many drivers need both vertex and fragment shader, 'caboom!' otherwise
107 shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.); 119 shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.);
108 120
109 fsh_src = shader::shader_builder::stop (); 121 fsh_src = shader::shader_builder::stop ();
110 122
111 shader::program_object po = shader::get_program (vsh_src, fsh_src); 123 shader::program_object po = shader::get_program (vsh_src, fsh_src);
112 matmap.insert (pass::matmap_t::value_type (this, po)); 124 matmap.insert (pass_data::matmap_t::value_type (this, po));
113 125
114 po->enable (); 126 po->enable ();
115 } 127 }
116 else 128 else
117 i->second->enable (); 129 i->second->enable ();
130
131 if (ctx.pass->l)
132 ctx.pass->l->enable ();
118} 133}
119 134
120void material::disable (view &ctx) 135void material::disable (view &ctx)
121{ 136{
122} 137}
129} 144}
130 145
131static shader::varying_3f normal; 146static shader::varying_3f normal;
132static shader::varying_2f texcoord; 147static shader::varying_2f texcoord;
133 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
134void test_material::vsh (view &ctx) 172void test_material::vsh (view &ctx)
135{ 173{
136 using namespace shader::compile; 174 using namespace shader::compile;
137 175
138 std_vsh (); 176 std_vsh ();
139 177
140 if (ctx.pass_data->l) 178 if (ctx.pass->l)
141 { 179 {
142 texcoord = xy (vin.tex_coord[0]); 180 texcoord = xy (vin.tex_coord[0]);
143 normal = normal_matrix * vin.normal; 181 normal = normal_matrix * vin.normal;
144 } 182 }
145} 183}
146 184
147void test_material::fsh (view &ctx) 185void test_material::fsh (view &ctx)
148{ 186{
149 using namespace shader::compile; 187 using namespace shader::compile;
150 188
151 if (ctx.pass_data->l) 189 if (ctx.pass->l)
152 { 190 {
153 temp_3f lc; 191 temp_3f lc;
154 temp_1f fac; 192 temp_1f fac;
155 193
156 temp_3f rot1, rot2, rot3; 194 temp_3f rot1, rot2, rot3;
162 rot2 = float3 (x(rot1), z(rot1), -y(rot1)); 200 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
163 rot3 = float3 (y(rot1), -x(rot1), z(rot1)); 201 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
164 202
165 normal = mat3 (rot1, rot2, rot3) * normal; 203 normal = mat3 (rot1, rot2, rot3) * normal;
166 204
167 fac = dot (normalize (normal), normalize (ctx.pass_data->l->sh_lightvec)); 205 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
168 fac = max (pow (max (fac, 0.0), 6), 0.3); 206 fac = max (pow (max (fac, 0.0), 6), 0.3);
169 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac 207 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
170 * ctx.pass_data->l->sh_colour; 208 * ctx.pass->l->sh_colour;
171 //xyz (fout.frag_color) = lc * fac; 209 //xyz (fout.frag_color) = lc * fac;
172 } 210 }
173} 211}
174 212
175void test_material::enable (view &ctx) 213void test_material::enable (view &ctx)
185 texvar->disable (); 223 texvar->disable ();
186 material::disable (ctx); 224 material::disable (ctx);
187} 225}
188 226
189test_material *testmat; 227test_material *testmat;
228test_material2 *testmat2;
190 229

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines