… | |
… | |
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::matmap_t &matmap = ctx.pass_data->matmap; |
75 | |
75 | |
76 | pass::matmap_t::iterator i = matmap.find (this); |
76 | pass::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 (); |
84 | vsh (ctx); |
83 | vsh (ctx); |
85 | |
84 | |
86 | if (ctx.pass_data->l) |
85 | if (ctx.pass_data->l) |
87 | ctx.pass_data->l->vsh (); |
86 | ctx.pass_data->l->vsh (); |
88 | |
87 | |
89 | po->vsh->compile (shader::shader_builder::stop ()); |
88 | { |
|
|
89 | using namespace shader::compile; |
90 | |
90 | |
91 | shader::shader_builder::start (); |
91 | z (vout.position) = log2 (z (vout.position) / w (vout.position)) * w (vout.position); |
92 | shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.); |
92 | } |
93 | fsh (ctx); |
|
|
94 | po->fsh->compile (shader::shader_builder::stop ()); |
|
|
95 | |
93 | |
96 | po->link (); |
94 | vsh_src = shader::shader_builder::stop (); |
97 | |
95 | |
|
|
96 | if (ctx.pass_data->l || 1) |
|
|
97 | { |
|
|
98 | shader::shader_builder::start (); |
|
|
99 | shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.); |
|
|
100 | fsh (ctx); |
|
|
101 | |
|
|
102 | fsh_src = shader::shader_builder::stop (); |
|
|
103 | } |
|
|
104 | |
|
|
105 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
98 | matmap.insert (pass::matmap_t::value_type (this, po)); |
106 | matmap.insert (pass::matmap_t::value_type (this, po)); |
99 | |
107 | |
100 | p = &po; |
108 | po->enable (); |
101 | } |
109 | } |
102 | else |
110 | else |
103 | p = &i->second; |
111 | i->second->enable (); |
104 | |
|
|
105 | (*p)->enable (); |
|
|
106 | } |
112 | } |
107 | |
113 | |
108 | void material::disable (view &ctx) |
114 | void material::disable (view &ctx) |
109 | { |
115 | { |
110 | } |
116 | } |
111 | |
117 | |
112 | test_material::test_material () |
118 | test_material::test_material () |
113 | //: tex ("textures/osama.jpg"), texvar (tex.name) |
119 | //: tex ("textures/osama.jpg"), texvar (tex.name) |
114 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
120 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
115 | , norm ("textures/rockwall_height.jpg"), normvar (norm.name) |
121 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
116 | { |
122 | { |
117 | } |
123 | } |
118 | |
124 | |
119 | static shader::varying_3f normal, lightvec; |
125 | static shader::varying_3f normal, lightvec; |
120 | static shader::varying_2f texcoord; |
126 | static shader::varying_2f texcoord; |
… | |
… | |
142 | temp_3f lc; |
148 | temp_3f lc; |
143 | temp_1f fac; |
149 | temp_1f fac; |
144 | |
150 | |
145 | lc = (*ctx.pass_data->l)(); |
151 | lc = (*ctx.pass_data->l)(); |
146 | |
152 | |
|
|
153 | temp_3f rot1, rot2, rot3; |
|
|
154 | |
|
|
155 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
|
|
156 | |
|
|
157 | //rot1 = normal_matrix * rot1; |
|
|
158 | |
|
|
159 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
|
|
160 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
|
|
161 | |
|
|
162 | normal = mat3 (rot1, rot2, rot3) * normal; |
|
|
163 | |
147 | fac = dot (normalize (normal), normalize (lightvec)); |
164 | fac = dot (normalize (normal), normalize (lightvec)); |
148 | fac = pow (max (fac, 0.0), 2); |
165 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
149 | xyz (fout.frag_color) = texture_2d (texvar, texcoord) * lc * fac; |
166 | xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * lc * fac; |
|
|
167 | //xyz (fout.frag_color) = lc * fac; |
150 | } |
168 | } |
151 | } |
169 | } |
152 | |
170 | |
153 | void test_material::enable (view &ctx) |
171 | void test_material::enable (view &ctx) |
154 | { |
172 | { |