… | |
… | |
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 | |
|
|
15 | void |
|
|
16 | simple_material::enable (view &ctx) |
|
|
17 | { |
|
|
18 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
|
|
19 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
|
|
20 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
|
|
21 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
|
|
22 | } |
|
|
23 | |
|
|
24 | void |
|
|
25 | simple_material::disable (view &ctx) |
|
|
26 | { |
|
|
27 | } |
|
|
28 | |
10 | |
29 | GLuint |
11 | GLuint |
30 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
12 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
31 | { |
13 | { |
32 | GLuint name; |
14 | GLuint name; |
… | |
… | |
81 | SDL_FreeSurface (image); /* No longer needed */ |
63 | SDL_FreeSurface (image); /* No longer needed */ |
82 | |
64 | |
83 | return name; |
65 | return name; |
84 | } |
66 | } |
85 | |
67 | |
|
|
68 | material::~material () |
|
|
69 | { |
|
|
70 | } |
|
|
71 | |
|
|
72 | void material::enable (view &ctx) |
|
|
73 | { |
|
|
74 | pass_data::matmap_t &matmap = ctx.pass->matmap; |
|
|
75 | |
|
|
76 | pass_data::matmap_t::iterator i = matmap.find (this); |
|
|
77 | |
|
|
78 | if (i == matmap.end ()) |
|
|
79 | { |
|
|
80 | string vsh_src, fsh_src; |
|
|
81 | |
|
|
82 | shader::shader_builder::start (); |
|
|
83 | |
|
|
84 | if (ctx.pass->l) |
|
|
85 | ctx.pass->l->vsh (); |
|
|
86 | |
|
|
87 | vsh (ctx); |
|
|
88 | |
|
|
89 | // compute logarithmic depth - see the frustum matrix in view.C |
|
|
90 | { |
|
|
91 | using namespace shader::compile; |
|
|
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) = (ifelse (lz < 0, lz, log (lz) * (2 / log (1e10))) - 1) * w (vout.position); |
|
|
98 | } |
|
|
99 | |
|
|
100 | vsh_src = shader::shader_builder::stop (); |
|
|
101 | |
|
|
102 | shader::shader_builder::start (); |
|
|
103 | |
|
|
104 | if (ctx.pass->l) |
|
|
105 | { |
|
|
106 | ctx.pass->l->fsh (); |
|
|
107 | |
|
|
108 | fsh (ctx); |
|
|
109 | } |
|
|
110 | else |
|
|
111 | shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.); |
|
|
112 | |
|
|
113 | fsh_src = shader::shader_builder::stop (); |
|
|
114 | |
|
|
115 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
|
|
116 | matmap.insert (pass_data::matmap_t::value_type (this, po)); |
|
|
117 | |
|
|
118 | po->enable (); |
|
|
119 | } |
|
|
120 | else |
|
|
121 | i->second->enable (); |
|
|
122 | |
|
|
123 | if (ctx.pass->l) |
|
|
124 | ctx.pass->l->enable (); |
|
|
125 | } |
|
|
126 | |
|
|
127 | void material::disable (view &ctx) |
|
|
128 | { |
|
|
129 | } |
|
|
130 | |
86 | test_material::test_material () |
131 | test_material::test_material () |
87 | : tex ("textures/osama.jpg"), texvar (tex.name) |
132 | //: tex ("textures/osama.jpg"), texvar (tex.name) |
|
|
133 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
|
|
134 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
88 | { |
135 | { |
89 | using namespace shader; |
136 | } |
90 | |
137 | |
91 | p.vsh->start (); |
138 | static shader::varying_3f normal; |
|
|
139 | static shader::varying_2f texcoord; |
92 | |
140 | |
93 | temp_4f wpos; |
141 | void test_material::vsh (view &ctx) |
94 | |
142 | { |
95 | wpos = shader::gl.model_view_projection_matrix * vin.vertex; |
143 | using namespace shader::compile; |
96 | |
144 | |
97 | vout.position = wpos; |
145 | std_vsh (); |
98 | vout.tex_coord[0] = vin.tex_coord[0]; |
|
|
99 | vout.tex_coord[1] = shader::gl.model_view_matrix * shader::vec4 (x (vin.normal), y(vin.normal), z(vin.normal), 0); |
|
|
100 | |
146 | |
101 | p.vsh->end (); |
147 | if (ctx.pass->l) |
102 | p.vsh->compile (); |
148 | { |
|
|
149 | texcoord = xy (vin.tex_coord[0]); |
|
|
150 | normal = normal_matrix * vin.normal; |
|
|
151 | } |
|
|
152 | } |
103 | |
153 | |
104 | p.fsh->start (); |
154 | void test_material::fsh (view &ctx) |
|
|
155 | { |
|
|
156 | using namespace shader::compile; |
105 | |
157 | |
|
|
158 | if (ctx.pass->l) |
|
|
159 | { |
|
|
160 | temp_3f lc; |
106 | temp_1f fac; |
161 | temp_1f fac; |
107 | |
|
|
108 | fac = max (dot (fin.tex_coord[1], lightpos), 0.0); |
|
|
109 | fac = pow (fac, 3); |
|
|
110 | xyz (fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]) * (fac + 0.3); |
|
|
111 | |
162 | |
112 | p.fsh->end (); |
163 | temp_3f rot1, rot2, rot3; |
113 | p.fsh->compile (); |
164 | |
114 | p.link (); |
165 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
|
|
166 | |
|
|
167 | //rot1 = normal_matrix * rot1; |
|
|
168 | |
|
|
169 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
|
|
170 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
|
|
171 | |
|
|
172 | normal = mat3 (rot1, rot2, rot3) * normal; |
|
|
173 | |
|
|
174 | fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); |
|
|
175 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
|
|
176 | xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac |
|
|
177 | * ctx.pass->l->sh_colour; |
|
|
178 | //xyz (fout.frag_color) = lc * fac; |
|
|
179 | } |
115 | } |
180 | } |
116 | |
181 | |
117 | void test_material::enable (view &ctx) |
182 | void test_material::enable (view &ctx) |
118 | { |
183 | { |
119 | p.enable (); |
184 | material::enable (ctx); |
120 | lightpos->set (-ctx.d); |
|
|
121 | texvar->enable (); |
185 | texvar->enable (); |
|
|
186 | normvar->enable (); |
122 | } |
187 | } |
123 | |
188 | |
124 | void test_material::disable (view &ctx) |
189 | void test_material::disable (view &ctx) |
125 | { |
190 | { |
|
|
191 | normvar->disable (); |
126 | texvar->disable (); |
192 | texvar->disable (); |
127 | p.disable (); |
193 | material::disable (ctx); |
128 | } |
194 | } |
129 | |
195 | |
|
|
196 | test_material *testmat; |
|
|
197 | |