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