… | |
… | |
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 | |
10 | |
11 | GLuint |
11 | GLuint |
12 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
12 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord, int flags) |
13 | { |
13 | { |
14 | GLuint name; |
14 | GLuint name; |
15 | SDL_Surface *image; |
15 | SDL_Surface *image; |
16 | SDL_Rect area; |
16 | SDL_Rect area; |
17 | Uint32 saved_flags; |
17 | Uint32 saved_flags; |
… | |
… | |
53 | |
53 | |
54 | /* Create an OpenGL texture for the image */ |
54 | /* Create an OpenGL texture for the image */ |
55 | glGenTextures (1, &name); |
55 | glGenTextures (1, &name); |
56 | glBindTexture (GL_TEXTURE_2D, name); |
56 | glBindTexture (GL_TEXTURE_2D, name); |
57 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
57 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
58 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
58 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, flags & DISABLE_MIPMAP ? GL_LINEAR : GL_LINEAR_MIPMAP_LINEAR); |
59 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
59 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, flags & DISABLE_MIPMAP ? GL_FALSE : GL_TRUE); |
60 | glTexImage2D (GL_TEXTURE_2D, 0, |
|
|
61 | GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
60 | glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
|
|
61 | |
|
|
62 | if (flags & CLAMP) |
|
|
63 | { |
|
|
64 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
|
65 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
|
66 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); |
|
|
67 | } |
|
|
68 | |
62 | SDL_FreeSurface (image); /* No longer needed */ |
69 | SDL_FreeSurface (image); /* No longer needed */ |
63 | |
70 | |
64 | return name; |
71 | return name; |
65 | } |
72 | } |
66 | |
73 | |
… | |
… | |
106 | ctx.pass->l->fsh (); |
113 | ctx.pass->l->fsh (); |
107 | fsh (ctx); |
114 | fsh (ctx); |
108 | } |
115 | } |
109 | else |
116 | else |
110 | // many drivers need both vertex and fragment shader, 'caboom!' otherwise |
117 | // many drivers need both vertex and fragment shader, 'caboom!' otherwise |
111 | shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.); |
118 | shader::compile::fout.frag_color = shader::compile::vec4 (0., 0., 0., 0.); |
112 | |
119 | |
113 | fsh_src = shader::shader_builder::stop (); |
120 | fsh_src = shader::shader_builder::stop (); |
114 | |
121 | |
115 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
122 | shader::program_object po = shader::get_program (vsh_src, fsh_src); |
116 | matmap.insert (pass_data::matmap_t::value_type (this, po)); |
123 | matmap.insert (pass_data::matmap_t::value_type (this, po)); |
… | |
… | |
133 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
140 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
134 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
141 | , norm ("textures/rockwall_normal.jpg"), normvar (norm.name) |
135 | { |
142 | { |
136 | } |
143 | } |
137 | |
144 | |
|
|
145 | skybox_material::skybox_material () |
|
|
146 | : tex (0) |
|
|
147 | { |
|
|
148 | } |
|
|
149 | |
|
|
150 | void skybox_material::enable (view &ctx) |
|
|
151 | { |
|
|
152 | material::enable (ctx); |
|
|
153 | } |
|
|
154 | |
|
|
155 | void skybox_material::disable (view &ctx) |
|
|
156 | { |
|
|
157 | material::disable (ctx); |
|
|
158 | } |
|
|
159 | |
|
|
160 | static shader::varying_2f skybox_texcoord; |
|
|
161 | |
|
|
162 | void skybox_material::vsh (view &ctx) |
|
|
163 | { |
|
|
164 | using namespace shader::compile; |
|
|
165 | |
|
|
166 | std_vsh (); |
|
|
167 | skybox_texcoord = xy (vin.tex_coord[0]); |
|
|
168 | } |
|
|
169 | |
|
|
170 | void skybox_material::fsh (view &ctx) |
|
|
171 | { |
|
|
172 | using namespace shader::compile; |
|
|
173 | |
|
|
174 | xyz (fout.frag_color) = texture_2d (tex, skybox_texcoord); |
|
|
175 | } |
|
|
176 | |
138 | void mat_timed::enable (view &ctx) |
177 | void mat_timed::enable (view &ctx) |
139 | { |
178 | { |
140 | material::enable (ctx); |
179 | material::enable (ctx); |
141 | |
180 | |
142 | time->set (timer::now); |
181 | time->set (timer::now); |
… | |
… | |
168 | |
207 | |
169 | normal = f_normal + 0.3 * pow (sin (f_normal * 3.14159 + time), 3); |
208 | normal = f_normal + 0.3 * pow (sin (f_normal * 3.14159 + time), 3); |
170 | |
209 | |
171 | fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); |
210 | fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); |
172 | |
211 | |
173 | xyz (fout.frag_color) = fac * float3 (0.1,0.5,1);//ctx.pass->l->sh_colour * sh_colour * fac; |
212 | xyz (fout.frag_color) = fac * shader::compile::vec3 (0.1,0.5,1);//ctx.pass->l->sh_colour * sh_colour * fac; |
174 | } |
213 | } |
175 | } |
214 | } |
176 | |
215 | |
177 | void mat_gouraud_shaded::enable (view &ctx) |
216 | void mat_gouraud_shaded::enable (view &ctx) |
178 | { |
217 | { |
… | |
… | |
245 | { |
284 | { |
246 | temp_1f fac; |
285 | temp_1f fac; |
247 | |
286 | |
248 | fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec)); |
287 | fac = dot (normalize (f_normal), normalize (ctx.pass->l->sh_lightvec)); |
249 | |
288 | |
250 | xyz (fout.frag_color) = float3 (1.,0.,0.); |
289 | xyz (fout.frag_color) = shader::compile::vec3 (1.,0.,0.); |
251 | w (fout.frag_color) = 0.1; |
290 | w (fout.frag_color) = 0.1; |
252 | } |
291 | } |
253 | } |
292 | } |
254 | |
293 | |
255 | static shader::varying_2f texcoord; |
294 | static shader::varying_2f texcoord; |
… | |
… | |
281 | |
320 | |
282 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
321 | yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F); |
283 | |
322 | |
284 | //rot1 = normal_matrix * rot1; |
323 | //rot1 = normal_matrix * rot1; |
285 | |
324 | |
286 | rot2 = float3 (x(rot1), z(rot1), -y(rot1)); |
325 | rot2 = shader::compile::vec3 (x(rot1), z(rot1), -y(rot1)); |
287 | rot3 = float3 (y(rot1), -x(rot1), z(rot1)); |
326 | rot3 = shader::compile::vec3 (y(rot1), -x(rot1), z(rot1)); |
288 | |
327 | |
289 | normal = mat3 (rot1, rot2, rot3) * normal; |
328 | normal = mat3 (rot1, rot2, rot3) * normal; |
290 | |
329 | |
291 | fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); |
330 | fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec)); |
292 | fac = max (pow (max (fac, 0.0), 6), 0.3); |
331 | fac = max (pow (max (fac, 0.0), 6), 0.3); |