… | |
… | |
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 | vsh (ctx); |
|
|
84 | |
83 | |
85 | if (ctx.pass_data->l) |
84 | if (ctx.pass_data->l) |
86 | ctx.pass_data->l->vsh (); |
85 | ctx.pass_data->l->vsh (); |
87 | |
86 | |
|
|
87 | vsh (ctx); |
|
|
88 | |
|
|
89 | // compute logarithmic depth - see the frustum matrix in view.C |
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 | z (vout.position) = (log2 (max (z (vout.position), 0)) * (2 / log2 (1e10)) - 1) * w (vout.position); |
92 | } |
94 | } |
93 | |
95 | |
94 | vsh_src = shader::shader_builder::stop (); |
96 | vsh_src = shader::shader_builder::stop (); |
95 | |
97 | |
|
|
98 | shader::shader_builder::start (); |
|
|
99 | |
96 | if (ctx.pass_data->l || 1) |
100 | if (ctx.pass_data->l) |
97 | { |
101 | { |
98 | shader::shader_builder::start (); |
102 | ctx.pass_data->l->fsh (); |
99 | shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.); |
103 | |
100 | fsh (ctx); |
104 | fsh (ctx); |
101 | |
|
|
102 | fsh_src = shader::shader_builder::stop (); |
|
|
103 | } |
105 | } |
|
|
106 | else |
|
|
107 | shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.); |
|
|
108 | |
|
|
109 | { |
|
|
110 | using namespace shader::compile; |
|
|
111 | //x (fout.frag_color) = Z;//D |
|
|
112 | //y (fout.frag_color) = Z;//D |
|
|
113 | //z (fout.frag_color) = Z;//D |
|
|
114 | } |
|
|
115 | |
|
|
116 | fsh_src = shader::shader_builder::stop (); |
104 | |
117 | |
105 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
118 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
106 | matmap.insert (pass::matmap_t::value_type (this, po)); |
119 | matmap.insert (pass::matmap_t::value_type (this, po)); |
107 | |
120 | |
108 | po->enable (); |
121 | po->enable (); |
109 | } |
122 | } |
110 | else |
123 | else |
111 | i->second->enable (); |
124 | i->second->enable (); |
|
|
125 | |
|
|
126 | if (ctx.pass_data->l) |
|
|
127 | ctx.pass_data->l->enable (); |
112 | } |
128 | } |
113 | |
129 | |
114 | void material::disable (view &ctx) |
130 | void material::disable (view &ctx) |
115 | { |
131 | { |
116 | } |
132 | } |
… | |
… | |
120 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
136 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
121 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
137 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
122 | { |
138 | { |
123 | } |
139 | } |
124 | |
140 | |
125 | static shader::varying_3f normal, lightvec; |
141 | static shader::varying_3f normal; |
126 | static shader::varying_2f texcoord; |
142 | static shader::varying_2f texcoord; |
127 | |
143 | |
128 | void test_material::vsh (view &ctx) |
144 | void test_material::vsh (view &ctx) |
129 | { |
145 | { |
130 | using namespace shader::compile; |
146 | using namespace shader::compile; |
… | |
… | |
133 | |
149 | |
134 | if (ctx.pass_data->l) |
150 | if (ctx.pass_data->l) |
135 | { |
151 | { |
136 | texcoord = xy (vin.tex_coord[0]); |
152 | texcoord = xy (vin.tex_coord[0]); |
137 | normal = normal_matrix * vin.normal; |
153 | normal = normal_matrix * vin.normal; |
138 | lightvec = xyz (ctx.pass_data->l->lightpos - model_view_matrix * vin.vertex); |
|
|
139 | } |
154 | } |
140 | } |
155 | } |
141 | |
156 | |
142 | void test_material::fsh (view &ctx) |
157 | void test_material::fsh (view &ctx) |
143 | { |
158 | { |
… | |
… | |
146 | if (ctx.pass_data->l) |
161 | if (ctx.pass_data->l) |
147 | { |
162 | { |
148 | temp_3f lc; |
163 | temp_3f lc; |
149 | temp_1f fac; |
164 | temp_1f fac; |
150 | |
165 | |
151 | lc = (*ctx.pass_data->l)(); |
|
|
152 | |
|
|
153 | temp_3f rot1, rot2, rot3; |
166 | temp_3f rot1, rot2, rot3; |
154 | |
167 | |
155 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
168 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
156 | |
169 | |
157 | //rot1 = normal_matrix * rot1; |
170 | //rot1 = normal_matrix * rot1; |
… | |
… | |
159 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
172 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
160 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
173 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
161 | |
174 | |
162 | normal = mat3 (rot1, rot2, rot3) * normal; |
175 | normal = mat3 (rot1, rot2, rot3) * normal; |
163 | |
176 | |
164 | fac = dot (normalize (normal), normalize (lightvec)); |
177 | fac = dot (normalize (normal), normalize (ctx.pass_data->l->sh_lightvec)); |
165 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
178 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
166 | xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * lc * fac; |
179 | xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac |
|
|
180 | * ctx.pass_data->l->sh_colour; |
167 | //xyz (fout.frag_color) = lc * fac; |
181 | //xyz (fout.frag_color) = lc * fac; |
168 | } |
182 | } |
169 | } |
183 | } |
170 | |
184 | |
171 | void test_material::enable (view &ctx) |
185 | void test_material::enable (view &ctx) |