… | |
… | |
3 | |
3 | |
4 | #include <algorithm> |
4 | #include <algorithm> |
5 | |
5 | |
6 | #include "opengl.h" |
6 | #include "opengl.h" |
7 | #include "material.h" |
7 | #include "material.h" |
|
|
8 | #include "view.h" |
8 | #include "util.h" |
9 | #include "util.h" |
9 | |
10 | |
10 | material::~material () |
11 | material::~material () |
11 | { |
12 | { |
12 | } |
13 | } |
13 | |
14 | |
14 | void |
15 | void |
15 | simple_material::enable () |
16 | simple_material::enable (view &ctx) |
16 | { |
17 | { |
17 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
18 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
18 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
19 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
19 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
20 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
20 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
21 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
21 | } |
22 | } |
22 | |
23 | |
23 | void |
24 | void |
24 | simple_material::disable () |
25 | simple_material::disable (view &ctx) |
25 | { |
26 | { |
26 | } |
27 | } |
27 | |
28 | |
28 | GLuint |
29 | GLuint |
29 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
30 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
30 | { |
31 | { |
31 | GLuint name; |
32 | GLuint name; |
32 | int w, h; |
|
|
33 | SDL_Surface *image; |
33 | SDL_Surface *image; |
34 | SDL_Rect area; |
34 | SDL_Rect area; |
35 | Uint32 saved_flags; |
35 | Uint32 saved_flags; |
36 | Uint8 saved_alpha; |
36 | Uint8 saved_alpha; |
37 | |
37 | |
38 | /* Use the surface width and height expanded to powers of 2 */ |
38 | /* Use the surface width and height expanded to powers of 2 */ |
39 | //w = power_of_two (surface->w); |
|
|
40 | //h = power_of_two (surface->h); |
|
|
41 | w = power_of_two (surface->w); |
|
|
42 | h = power_of_two (surface->h); |
|
|
43 | tex2oord[0] = 0.0f; /* Min X */ |
39 | tex2oord[0] = 0.F; /* Min X */ |
44 | tex2oord[1] = 0.0f; /* Min Y */ |
40 | tex2oord[1] = 0.F; /* Min Y */ |
45 | tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ |
41 | tex2oord[2] = 1.F; /* Max X */ |
46 | tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ |
42 | tex2oord[3] = 1.F; /* Max Y */ |
47 | |
43 | |
48 | image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
44 | image = SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, 32, |
49 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
45 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
50 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
46 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
51 | #else |
47 | #else |
52 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
48 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
53 | #endif |
49 | #endif |
… | |
… | |
79 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
75 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
80 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
76 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
81 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
77 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
82 | glTexImage2D (GL_TEXTURE_2D, |
78 | glTexImage2D (GL_TEXTURE_2D, |
83 | 0, |
79 | 0, |
84 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
80 | GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
85 | SDL_FreeSurface (image); /* No longer needed */ |
81 | SDL_FreeSurface (image); /* No longer needed */ |
86 | |
82 | |
87 | return name; |
83 | return name; |
88 | } |
84 | } |
89 | |
85 | |
90 | test_material::test_material () |
86 | test_material::test_material () |
91 | : tex ("textures/osama.jpg"), texvar (tex.name) |
87 | //: tex ("textures/osama.jpg"), texvar (tex.name) |
|
|
88 | : tex ("textures/rockwall.jpg"), texvar (tex.name) |
|
|
89 | , norm ("textures/rockwall_height.jpg"), normvar (norm.name) |
92 | { |
90 | { |
93 | using namespace shader; |
91 | using namespace shader::compile; |
94 | |
92 | |
95 | p.vsh->start (); |
93 | p.vsh->start (); |
96 | |
94 | |
97 | temp_4f lightpos; |
|
|
98 | temp_4f wpos; |
95 | temp_4f wpos; |
|
|
96 | varying_1f camdist, zpos; |
|
|
97 | varying_2f texcoord; |
|
|
98 | varying_3f lightvec, normal; |
|
|
99 | |
|
|
100 | wpos = model_view_matrix * vin.vertex; |
99 | |
101 | |
100 | //lightpos = shader::vec4 (0, 10, 0, 1); |
102 | temp_1f wz; |
101 | vout.position = shader::gl.model_view_projection_matrix * vin.vertex; |
103 | //wz = log2 (abs (wz)) * sign (wz); |
102 | vout.tex_coord[0] = vin.tex_coord[0]; |
|
|
103 | //vout.tex_coord[1] = normalize (lightpos - wpos); |
|
|
104 | //vout.tex_coord[2] = normalize (wpos); |
|
|
105 | //vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal); |
|
|
106 | //vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos); |
|
|
107 | |
104 | |
108 | p.vsh->end (); |
105 | vout.position = projection_matrix * wpos; |
109 | p.vsh->compile (); |
106 | texcoord = xy (vin.tex_coord[0]); |
|
|
107 | normal = normal_matrix * vin.normal; |
|
|
108 | lightvec = xyz (lightpos - model_view_matrix * vin.vertex); |
|
|
109 | camdist = 1 - max (length (lightvec) / 10000., 0); |
110 | |
110 | |
111 | fragment_shader fsh; |
111 | p.vsh->stop (); |
112 | |
112 | |
113 | p.fsh->start (); |
113 | p.fsh->start (); |
114 | |
114 | |
115 | xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]); |
115 | temp_1f fac; |
|
|
116 | temp_2f disp; |
|
|
117 | temp_1f dx, dy; |
|
|
118 | temp_1f bumpnormal; |
|
|
119 | temp_1f dist; |
116 | |
120 | |
117 | p.fsh->end (); |
121 | fac = dot (normalize (normal), normalize (lightvec)); |
|
|
122 | //fac = dot (shader::compile::vec3 (normalize (fin.tex_coord[1]) + normalize (fin.tex_coord[2])) * 0.5, bumpnormal); |
|
|
123 | //fac = dot (bumpnormal, normalize (fin.tex_coord[2])); |
|
|
124 | fac = pow (max (fac, 0.0), 2); |
|
|
125 | xyz (fout.frag_color) = texture_2d (texvar, texcoord) * (fac + 0.4) * camdist; |
|
|
126 | |
118 | p.fsh->compile (); |
127 | p.fsh->stop (); |
119 | p.link (); |
128 | p.link (); |
120 | } |
129 | } |
121 | |
130 | |
122 | void test_material::enable () |
131 | void test_material::enable (view &ctx) |
123 | { |
132 | { |
124 | p.enable (); |
133 | p.enable (); |
|
|
134 | lightpos->set (vec3 (0, 0, 0)); |
125 | texvar->enable (); |
135 | texvar->enable (); |
|
|
136 | normvar->enable (); |
126 | } |
137 | } |
127 | |
138 | |
128 | void test_material::disable () |
139 | void test_material::disable (view &ctx) |
129 | { |
140 | { |
|
|
141 | normvar->disable (); |
130 | texvar->disable (); |
142 | texvar->disable (); |
131 | p.disable (); |
143 | p.disable (); |
132 | } |
144 | } |
133 | |
145 | |