… | |
… | |
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::begin () |
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::end () |
25 | simple_material::disable (view &ctx) |
25 | { |
26 | { |
26 | } |
|
|
27 | |
|
|
28 | void |
|
|
29 | osama_material::begin () |
|
|
30 | { |
|
|
31 | cgGLEnableProfile (vsh_profile); |
|
|
32 | cgGLEnableProfile (fsh_profile); |
|
|
33 | cgGLEnableTextureParameter (g_Texture); |
|
|
34 | } |
|
|
35 | |
|
|
36 | void |
|
|
37 | osama_material::end () |
|
|
38 | { |
|
|
39 | cgGLDisableTextureParameter (g_Texture); |
|
|
40 | // cgGLUnbindProgram (vsh_profile); |
|
|
41 | // cgGLUnbindProgram (fsh_profile); |
|
|
42 | cgGLDisableProfile (vsh_profile); |
|
|
43 | cgGLDisableProfile (fsh_profile); |
|
|
44 | } |
27 | } |
45 | |
28 | |
46 | GLuint |
29 | GLuint |
47 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
30 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
48 | { |
31 | { |
49 | GLuint textur; |
32 | GLuint name; |
50 | int w, h; |
33 | int w, h; |
51 | SDL_Surface *image; |
34 | SDL_Surface *image; |
52 | SDL_Rect area; |
35 | SDL_Rect area; |
53 | Uint32 saved_flags; |
36 | Uint32 saved_flags; |
54 | Uint8 saved_alpha; |
37 | Uint8 saved_alpha; |
… | |
… | |
90 | /* Restore the alpha blending attributes */ |
73 | /* Restore the alpha blending attributes */ |
91 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
74 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
92 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
75 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
93 | |
76 | |
94 | /* Create an OpenGL texture for the image */ |
77 | /* Create an OpenGL texture for the image */ |
95 | glGenTextures (1, &textur); |
78 | glGenTextures (1, &name); |
96 | glBindTexture (GL_TEXTURE_2D, textur); |
79 | glBindTexture (GL_TEXTURE_2D, name); |
97 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
80 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
98 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
81 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
99 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); // GENERATE_MIPMAP_SGIS |
82 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
100 | glTexImage2D (GL_TEXTURE_2D, |
83 | glTexImage2D (GL_TEXTURE_2D, |
101 | 0, |
84 | 0, |
102 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
85 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
103 | SDL_FreeSurface (image); /* No longer needed */ |
86 | SDL_FreeSurface (image); /* No longer needed */ |
104 | |
87 | |
105 | return textur; |
88 | return name; |
106 | } |
89 | } |
107 | |
90 | |
108 | CGcontext cgc; |
91 | test_material::test_material () |
|
|
92 | : tex ("textures/osama.jpg"), texvar (tex.name) |
|
|
93 | { |
|
|
94 | using namespace shader; |
109 | |
95 | |
110 | void |
96 | p.vsh->start (); |
111 | init_shaders () |
97 | |
112 | { |
98 | temp_4f wpos; |
113 | cgc = cgCreateContext (); |
99 | |
|
|
100 | wpos = shader::gl.model_view_projection_matrix * vin.vertex; |
|
|
101 | |
|
|
102 | vout.position = wpos; |
|
|
103 | vout.tex_coord[0] = vin.tex_coord[0]; |
|
|
104 | vout.tex_coord[1] = shader::gl.model_view_matrix * shader::vec4 (x (vin.normal), y(vin.normal), z(vin.normal), 0); |
|
|
105 | |
|
|
106 | p.vsh->end (); |
|
|
107 | p.vsh->compile (); |
|
|
108 | |
|
|
109 | p.fsh->start (); |
|
|
110 | |
|
|
111 | temp_1f fac; |
|
|
112 | |
|
|
113 | fac = max (dot (fin.tex_coord[1], lightpos), 0.0); |
|
|
114 | fac = pow (fac, 3); |
|
|
115 | xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]) * (fac + 0.3); |
|
|
116 | |
|
|
117 | p.fsh->end (); |
|
|
118 | p.fsh->compile (); |
|
|
119 | p.link (); |
114 | } |
120 | } |
115 | |
121 | |
|
|
122 | void test_material::enable (view &ctx) |
|
|
123 | { |
|
|
124 | p.enable (); |
|
|
125 | lightpos->set (-ctx.d); |
|
|
126 | texvar->enable (); |
|
|
127 | } |
|
|
128 | |
|
|
129 | void test_material::disable (view &ctx) |
|
|
130 | { |
|
|
131 | texvar->disable (); |
|
|
132 | p.disable (); |
|
|
133 | } |
|
|
134 | |