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.41 by root, Sat Nov 6 01:56:20 2004 UTC vs.
Revision 1.52 by root, Sat Nov 27 03:33:51 2004 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) / w (vout.position)) * w (vout.position); 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 lz = ifelse (lz <= 0,
98 lz,
99 log (lz + 1) / log (1e10 + 1)
100 ) - 1;
101 z (vout.position) = lz * w (vout.position);
93 } 102 }
94 103
95 vsh_src = shader::shader_builder::stop (); 104 vsh_src = shader::shader_builder::stop ();
96 105
97 shader::shader_builder::start (); 106 shader::shader_builder::start ();
98 107
99 if (ctx.pass_data->l) 108 if (ctx.pass->l)
100 { 109 {
101 ctx.pass_data->l->fsh (); 110 ctx.pass->l->fsh ();
102
103 fsh (ctx); 111 fsh (ctx);
104 } 112 }
105 else 113 else
114 // many drivers need both vertex and fragment shader, 'caboom!' otherwise
106 shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.); 115 shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.);
107 116
108 fsh_src = shader::shader_builder::stop (); 117 fsh_src = shader::shader_builder::stop ();
109 118
110 shader::program_object po = shader::get_program (vsh_src, fsh_src); 119 shader::program_object po = shader::get_program (vsh_src, fsh_src);
111 matmap.insert (pass::matmap_t::value_type (this, po)); 120 matmap.insert (pass_data::matmap_t::value_type (this, po));
112 121
113 po->enable (); 122 po->enable ();
114 } 123 }
115 else 124 else
116 i->second->enable (); 125 i->second->enable ();
126
127 if (ctx.pass->l)
128 ctx.pass->l->enable ();
117} 129}
118 130
119void material::disable (view &ctx) 131void material::disable (view &ctx)
120{ 132{
121} 133}
134{ 146{
135 using namespace shader::compile; 147 using namespace shader::compile;
136 148
137 std_vsh (); 149 std_vsh ();
138 150
139 if (ctx.pass_data->l) 151 if (ctx.pass->l)
140 { 152 {
141 texcoord = xy (vin.tex_coord[0]); 153 texcoord = xy (vin.tex_coord[0]);
142 normal = normal_matrix * vin.normal; 154 normal = normal_matrix * vin.normal;
143 } 155 }
144} 156}
145 157
146void test_material::fsh (view &ctx) 158void test_material::fsh (view &ctx)
147{ 159{
148 using namespace shader::compile; 160 using namespace shader::compile;
149 161
150 if (ctx.pass_data->l) 162 if (ctx.pass->l)
151 { 163 {
152 temp_3f lc; 164 temp_3f lc;
153 temp_1f fac; 165 temp_1f fac;
154 166
155 temp_3f rot1, rot2, rot3; 167 temp_3f rot1, rot2, rot3;
161 rot2 = float3 (x(rot1), z(rot1), -y(rot1)); 173 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
162 rot3 = float3 (y(rot1), -x(rot1), z(rot1)); 174 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
163 175
164 normal = mat3 (rot1, rot2, rot3) * normal; 176 normal = mat3 (rot1, rot2, rot3) * normal;
165 177
166 fac = dot (normalize (normal), normalize (ctx.pass_data->l->sh_lightvec)); 178 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
167 fac = max (pow (max (fac, 0.0), 6), 0.3); 179 fac = max (pow (max (fac, 0.0), 6), 0.3);
168 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac 180 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
169 * ctx.pass_data->l->sh_colour; 181 * ctx.pass->l->sh_colour;
170 //xyz (fout.frag_color) = lc * fac; 182 //xyz (fout.frag_color) = lc * fac;
171 } 183 }
172} 184}
173 185
174void test_material::enable (view &ctx) 186void test_material::enable (view &ctx)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines