|
|
1 | #include <cstdlib> |
|
|
2 | #include <cstring> |
|
|
3 | |
|
|
4 | #include <algorithm> |
|
|
5 | |
1 | #include "opengl.h" |
6 | #include "opengl.h" |
2 | #include "material.h" |
7 | #include "material.h" |
|
|
8 | #include "util.h" |
3 | |
9 | |
4 | material::~material () |
10 | material::~material () |
5 | { |
11 | { |
6 | } |
12 | } |
7 | |
13 | |
8 | void |
14 | void |
9 | simple_material::begin () |
15 | simple_material::enable () |
10 | { |
16 | { |
11 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
17 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
12 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
18 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
13 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
19 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
14 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
20 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
15 | } |
21 | } |
16 | |
22 | |
17 | void |
23 | void |
18 | simple_material::end () |
24 | simple_material::disable () |
19 | { |
25 | { |
20 | } |
|
|
21 | |
|
|
22 | void |
|
|
23 | osama_material::begin () |
|
|
24 | { |
|
|
25 | cgGLEnableProfile (vsh_profile); |
|
|
26 | cgGLEnableProfile (fsh_profile); |
|
|
27 | cgGLEnableTextureParameter (g_Texture); |
|
|
28 | } |
|
|
29 | |
|
|
30 | void |
|
|
31 | osama_material::end () |
|
|
32 | { |
|
|
33 | cgGLDisableTextureParameter (g_Texture); |
|
|
34 | // cgGLUnbindProgram (vsh_profile); |
|
|
35 | // cgGLUnbindProgram (fsh_profile); |
|
|
36 | cgGLDisableProfile (vsh_profile); |
|
|
37 | cgGLDisableProfile (fsh_profile); |
|
|
38 | } |
26 | } |
39 | |
27 | |
40 | GLuint |
28 | GLuint |
41 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
29 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
42 | { |
30 | { |
43 | GLuint textur; |
31 | GLuint name; |
44 | int w, h; |
32 | int w, h; |
45 | SDL_Surface *image; |
33 | SDL_Surface *image; |
46 | SDL_Rect area; |
34 | SDL_Rect area; |
47 | Uint32 saved_flags; |
35 | Uint32 saved_flags; |
48 | Uint8 saved_alpha; |
36 | Uint8 saved_alpha; |
… | |
… | |
84 | /* Restore the alpha blending attributes */ |
72 | /* Restore the alpha blending attributes */ |
85 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
73 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
86 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
74 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
87 | |
75 | |
88 | /* Create an OpenGL texture for the image */ |
76 | /* Create an OpenGL texture for the image */ |
89 | glGenTextures (1, &textur); |
77 | glGenTextures (1, &name); |
90 | glBindTexture (GL_TEXTURE_2D, textur); |
78 | glBindTexture (GL_TEXTURE_2D, name); |
91 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
79 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
92 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
80 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
93 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE); // GENERATE_MIPMAP_SGIS |
81 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
94 | glTexImage2D (GL_TEXTURE_2D, |
82 | glTexImage2D (GL_TEXTURE_2D, |
95 | 0, |
83 | 0, |
96 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
84 | GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
97 | SDL_FreeSurface (image); /* No longer needed */ |
85 | SDL_FreeSurface (image); /* No longer needed */ |
98 | |
86 | |
99 | return textur; |
87 | return name; |
100 | } |
88 | } |
101 | |
89 | |
102 | CGcontext cgc; |
90 | test_material::test_material () |
|
|
91 | : tex ("textures/osama.jpg"), texvar (tex.name) |
|
|
92 | { |
|
|
93 | using namespace shader; |
103 | |
94 | |
104 | void |
95 | p.vsh->start (); |
105 | init_shaders () |
96 | |
|
|
97 | temp_4f lightpos; |
|
|
98 | temp_4f wpos; |
|
|
99 | |
|
|
100 | //lightpos = shader::vec4 (0, 10, 0, 1); |
|
|
101 | vout.position = shader::gl.model_view_projection_matrix * vin.vertex; |
|
|
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 | |
|
|
108 | p.vsh->end (); |
|
|
109 | p.vsh->compile (); |
|
|
110 | |
|
|
111 | fragment_shader fsh; |
|
|
112 | |
|
|
113 | p.fsh->start (); |
|
|
114 | |
|
|
115 | xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]); |
|
|
116 | |
|
|
117 | p.fsh->end (); |
|
|
118 | p.fsh->compile (); |
|
|
119 | p.link (); |
|
|
120 | } |
|
|
121 | |
|
|
122 | void test_material::enable () |
106 | { |
123 | { |
107 | cgc = cgCreateContext (); |
124 | p.enable (); |
|
|
125 | texvar->enable (); |
108 | } |
126 | } |
|
|
127 | |
|
|
128 | void test_material::disable () |
|
|
129 | { |
|
|
130 | texvar->disable (); |
|
|
131 | p.disable (); |
|
|
132 | } |
|
|
133 | |