… | |
… | |
5 | |
5 | |
6 | #include "opengl.h" |
6 | #include "opengl.h" |
7 | #include "material.h" |
7 | #include "material.h" |
8 | #include "view.h" |
8 | #include "view.h" |
9 | #include "util.h" |
9 | #include "util.h" |
10 | |
|
|
11 | material::~material () |
|
|
12 | { |
|
|
13 | } |
|
|
14 | |
10 | |
15 | GLuint |
11 | GLuint |
16 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
12 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
17 | { |
13 | { |
18 | GLuint name; |
14 | GLuint name; |
… | |
… | |
67 | SDL_FreeSurface (image); /* No longer needed */ |
63 | SDL_FreeSurface (image); /* No longer needed */ |
68 | |
64 | |
69 | return name; |
65 | return name; |
70 | } |
66 | } |
71 | |
67 | |
|
|
68 | material::~material () |
|
|
69 | { |
|
|
70 | } |
|
|
71 | |
72 | void material::enable (view &ctx) |
72 | void 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 | shader::program_object *p; |
|
|
78 | |
77 | |
79 | if (i == matmap.end ()) |
78 | if (i == matmap.end ()) |
80 | { |
79 | { |
81 | shader::program_object po; |
80 | string vsh_src, fsh_src; |
82 | |
81 | |
83 | shader::shader_builder::start (); |
82 | shader::shader_builder::start (); |
|
|
83 | |
|
|
84 | if (ctx.pass->l) |
|
|
85 | ctx.pass->l->vsh (); |
|
|
86 | |
84 | vsh (ctx); |
87 | vsh (ctx); |
85 | |
88 | |
86 | if (ctx.pass_data->l) |
89 | // compute logarithmic depth - see the frustum matrix in view.C |
87 | ctx.pass_data->l->vsh (); |
|
|
88 | |
|
|
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 | 0, |
|
|
99 | log (lz + 1) / log (1e18) |
|
|
100 | ) - 1; |
|
|
101 | z (vout.position) = lz * w (vout.position); |
93 | } |
102 | } |
94 | |
103 | |
95 | po->vsh->compile (shader::shader_builder::stop ()); |
104 | vsh_src = shader::shader_builder::stop (); |
96 | |
105 | |
97 | shader::shader_builder::start (); |
106 | shader::shader_builder::start (); |
98 | shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.); |
107 | |
|
|
108 | if (ctx.pass->l) |
|
|
109 | { |
|
|
110 | ctx.pass->l->fsh (); |
99 | fsh (ctx); |
111 | fsh (ctx); |
|
|
112 | } |
|
|
113 | else |
|
|
114 | // many drivers need both vertex and fragment shader, 'caboom!' otherwise |
|
|
115 | shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.); |
|
|
116 | |
100 | po->fsh->compile (shader::shader_builder::stop ()); |
117 | fsh_src = shader::shader_builder::stop (); |
101 | |
118 | |
102 | po->link (); |
119 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
103 | |
|
|
104 | matmap.insert (pass::matmap_t::value_type (this, po)); |
120 | matmap.insert (pass_data::matmap_t::value_type (this, po)); |
105 | |
121 | |
106 | p = &po; |
122 | po->enable (); |
107 | } |
123 | } |
108 | else |
124 | else |
109 | p = &i->second; |
125 | i->second->enable (); |
110 | |
126 | |
111 | (*p)->enable (); |
127 | if (ctx.pass->l) |
|
|
128 | ctx.pass->l->enable (); |
112 | } |
129 | } |
113 | |
130 | |
114 | void material::disable (view &ctx) |
131 | void material::disable (view &ctx) |
115 | { |
132 | { |
116 | } |
133 | } |
… | |
… | |
120 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
137 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
121 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
138 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
122 | { |
139 | { |
123 | } |
140 | } |
124 | |
141 | |
125 | static shader::varying_3f normal, lightvec; |
142 | static shader::varying_3f normal; |
126 | static shader::varying_2f texcoord; |
143 | static shader::varying_2f texcoord; |
127 | |
144 | |
128 | void test_material::vsh (view &ctx) |
145 | void test_material::vsh (view &ctx) |
129 | { |
146 | { |
130 | using namespace shader::compile; |
147 | using namespace shader::compile; |
131 | |
148 | |
132 | std_vsh (); |
149 | std_vsh (); |
133 | |
150 | |
134 | if (ctx.pass_data->l) |
151 | if (ctx.pass->l) |
135 | { |
152 | { |
136 | texcoord = xy (vin.tex_coord[0]); |
153 | texcoord = xy (vin.tex_coord[0]); |
137 | normal = normal_matrix * vin.normal; |
154 | normal = normal_matrix * vin.normal; |
138 | lightvec = xyz (ctx.pass_data->l->lightpos - model_view_matrix * vin.vertex); |
|
|
139 | } |
155 | } |
140 | } |
156 | } |
141 | |
157 | |
142 | void test_material::fsh (view &ctx) |
158 | void test_material::fsh (view &ctx) |
143 | { |
159 | { |
144 | using namespace shader::compile; |
160 | using namespace shader::compile; |
145 | |
161 | |
146 | if (ctx.pass_data->l) |
162 | if (ctx.pass->l) |
147 | { |
163 | { |
148 | temp_3f lc; |
164 | temp_3f lc; |
149 | temp_1f fac; |
165 | temp_1f fac; |
150 | |
166 | |
151 | lc = (*ctx.pass_data->l)(); |
|
|
152 | |
|
|
153 | temp_3f rot1, rot2, rot3; |
167 | temp_3f rot1, rot2, rot3; |
154 | |
168 | |
155 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
169 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
156 | |
170 | |
157 | //rot1 = normal_matrix * rot1; |
171 | //rot1 = normal_matrix * rot1; |
… | |
… | |
159 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
173 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
160 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
174 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
161 | |
175 | |
162 | normal = mat3 (rot1, rot2, rot3) * normal; |
176 | normal = mat3 (rot1, rot2, rot3) * normal; |
163 | |
177 | |
164 | fac = dot (normalize (normal), normalize (lightvec)); |
178 | fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); |
165 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
179 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
166 | xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * lc * fac; |
180 | xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac |
|
|
181 | * ctx.pass->l->sh_colour; |
167 | //xyz (fout.frag_color) = lc * fac; |
182 | //xyz (fout.frag_color) = lc * fac; |
168 | } |
183 | } |
169 | } |
184 | } |
170 | |
185 | |
171 | void test_material::enable (view &ctx) |
186 | void test_material::enable (view &ctx) |