|
|
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 | |
|
|
14 | void |
8 | void simple_material::begin () |
15 | simple_material::enable () |
9 | { |
16 | { |
10 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&diffuse); |
17 | glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
11 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&specular); |
18 | glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
12 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&emission); |
19 | glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
13 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
20 | glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
14 | } |
21 | } |
15 | |
22 | |
|
|
23 | void |
16 | void simple_material::end () |
24 | simple_material::disable () |
17 | { |
25 | { |
18 | } |
26 | } |
19 | void osama_material::begin () |
27 | |
|
|
28 | GLuint |
|
|
29 | texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
20 | { |
30 | { |
21 | cgGLEnableProfile (vsh_profile); |
31 | GLuint name; |
22 | cgGLEnableProfile (fsh_profile); |
|
|
23 | cgGLEnableTextureParameter(g_Texture); |
|
|
24 | } |
|
|
25 | void osama_material::end () |
|
|
26 | { |
|
|
27 | cgGLDisableTextureParameter(g_Texture); |
|
|
28 | // cgGLUnbindProgram (vsh_profile); |
|
|
29 | // cgGLUnbindProgram (fsh_profile); |
|
|
30 | cgGLDisableProfile (vsh_profile); |
|
|
31 | cgGLDisableProfile (fsh_profile); |
|
|
32 | } |
|
|
33 | |
|
|
34 | |
|
|
35 | GLuint texture::load_texture (SDL_Surface *surface, GLfloat *tex2oord) |
|
|
36 | { |
|
|
37 | GLuint textur; |
|
|
38 | int w, h; |
32 | int w, h; |
39 | SDL_Surface *image; |
33 | SDL_Surface *image; |
40 | SDL_Rect area; |
34 | SDL_Rect area; |
41 | Uint32 saved_flags; |
35 | Uint32 saved_flags; |
42 | Uint8 saved_alpha; |
36 | Uint8 saved_alpha; |
… | |
… | |
56 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
50 | 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
57 | #else |
51 | #else |
58 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
52 | 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
59 | #endif |
53 | #endif |
60 | ); |
54 | ); |
|
|
55 | |
61 | if (image == NULL) |
56 | if (image == NULL) |
62 | { |
|
|
63 | return 0; |
57 | return 0; |
64 | } |
|
|
65 | |
58 | |
66 | /* Save the alpha blending attributes */ |
59 | /* Save the alpha blending attributes */ |
67 | saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
60 | saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
68 | saved_alpha = surface->format->alpha; |
61 | saved_alpha = surface->format->alpha; |
69 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
62 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
70 | { |
|
|
71 | SDL_SetAlpha (surface, 0, 0); |
63 | SDL_SetAlpha (surface, 0, 0); |
72 | } |
|
|
73 | |
64 | |
74 | /* Copy the surface into the GL texture image */ |
65 | /* Copy the surface into the GL texture image */ |
75 | area.x = 0; |
66 | area.x = 0; |
76 | area.y = 0; |
67 | area.y = 0; |
77 | area.w = surface->w; |
68 | area.w = surface->w; |
78 | area.h = surface->h; |
69 | area.h = surface->h; |
79 | SDL_BlitSurface (surface, &area, image, &area); |
70 | SDL_BlitSurface (surface, &area, image, &area); |
80 | |
71 | |
81 | /* Restore the alpha blending attributes */ |
72 | /* Restore the alpha blending attributes */ |
82 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
73 | if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
83 | { |
|
|
84 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
74 | SDL_SetAlpha (surface, saved_flags, saved_alpha); |
85 | } |
|
|
86 | |
75 | |
87 | /* Create an OpenGL texture for the image */ |
76 | /* Create an OpenGL texture for the image */ |
88 | glGenTextures (1, &textur); |
77 | glGenTextures (1, &name); |
89 | glBindTexture (GL_TEXTURE_2D, textur); |
78 | glBindTexture (GL_TEXTURE_2D, name); |
90 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
79 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
91 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
80 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
|
|
81 | glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
92 | glTexImage2D (GL_TEXTURE_2D, |
82 | glTexImage2D (GL_TEXTURE_2D, |
93 | 0, |
83 | 0, |
94 | 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); |
95 | SDL_FreeSurface (image); /* No longer needed */ |
85 | SDL_FreeSurface (image); /* No longer needed */ |
96 | |
86 | |
97 | return textur; |
87 | return name; |
98 | } |
88 | } |
99 | |
89 | |
|
|
90 | test_material::test_material () |
|
|
91 | : tex ("textures/osama.jpg"), texvar (tex.name) |
|
|
92 | { |
|
|
93 | using namespace shader; |
100 | |
94 | |
101 | CGcontext cgc; |
95 | p.vsh->start (); |
102 | void init_shaders () { |
96 | |
103 | cgc = cgCreateContext (); |
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] + 1; |
|
|
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 (); |
104 | } |
120 | } |
|
|
121 | |
|
|
122 | void test_material::enable () |
|
|
123 | { |
|
|
124 | p.enable (); |
|
|
125 | texvar->enable (); |
|
|
126 | } |
|
|
127 | |
|
|
128 | void test_material::disable () |
|
|
129 | { |
|
|
130 | texvar->disable (); |
|
|
131 | p.disable (); |
|
|
132 | } |
|
|
133 | |