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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines