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