… | |
… | |
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) |
92 | { |
88 | { |
93 | using namespace shader; |
89 | using namespace shader::compile; |
94 | |
90 | |
95 | p.vsh->start (); |
91 | p.vsh->start (); |
96 | |
92 | |
97 | temp_4f lightpos; |
|
|
98 | temp_4f wpos; |
93 | temp_4f wpos; |
|
|
94 | |
|
|
95 | wpos = model_view_projection_matrix * vin.vertex; |
99 | |
96 | |
100 | //lightpos = shader::vec4 (0, 10, 0, 1); |
97 | vout.position = wpos; |
101 | vout.position = shader::gl.model_view_projection_matrix * vin.vertex; |
|
|
102 | vout.tex_coord[0] = vin.tex_coord[0] + 1; |
98 | vout.tex_coord[0] = vin.tex_coord[0]; |
103 | //vout.tex_coord[1] = normalize (lightpos - wpos); |
99 | vout.tex_coord[1] = model_view_matrix * shader::compile::vec4 (x(vin.normal), y(vin.normal), z(vin.normal), 0); |
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 | |
100 | |
108 | p.vsh->end (); |
101 | p.vsh->end (); |
109 | p.vsh->compile (); |
102 | p.vsh->compile (); |
110 | |
103 | |
111 | fragment_shader fsh; |
|
|
112 | |
|
|
113 | p.fsh->start (); |
104 | p.fsh->start (); |
114 | |
105 | |
|
|
106 | temp_1f fac; |
|
|
107 | |
|
|
108 | fac = max (dot (fin.tex_coord[1], lightpos), 0.0); |
|
|
109 | fac = pow (fac, 3); |
115 | xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]); |
110 | xyz (fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]) * (fac + 0.3); |
116 | |
111 | |
117 | p.fsh->end (); |
112 | p.fsh->end (); |
118 | p.fsh->compile (); |
113 | p.fsh->compile (); |
119 | p.link (); |
114 | p.link (); |
120 | } |
115 | } |
121 | |
116 | |
122 | void test_material::enable () |
117 | void test_material::enable (view &ctx) |
123 | { |
118 | { |
124 | p.enable (); |
119 | p.enable (); |
|
|
120 | lightpos->set (-ctx.d); |
125 | texvar->enable (); |
121 | texvar->enable (); |
126 | } |
122 | } |
127 | |
123 | |
128 | void test_material::disable () |
124 | void test_material::disable (view &ctx) |
129 | { |
125 | { |
130 | texvar->disable (); |
126 | texvar->disable (); |
131 | p.disable (); |
127 | p.disable (); |
132 | } |
128 | } |
133 | |
129 | |