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