1 |
#include <cstdlib> |
2 |
#include <cstring> |
3 |
|
4 |
#include <algorithm> |
5 |
|
6 |
#include "opengl.h" |
7 |
#include "material.h" |
8 |
#include "util.h" |
9 |
|
10 |
material::~material () |
11 |
{ |
12 |
} |
13 |
|
14 |
void |
15 |
simple_material::enable () |
16 |
{ |
17 |
glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse); |
18 |
glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular); |
19 |
glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission); |
20 |
glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
21 |
} |
22 |
|
23 |
void |
24 |
simple_material::disable () |
25 |
{ |
26 |
} |
27 |
|
28 |
GLuint |
29 |
texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) |
30 |
{ |
31 |
GLuint name; |
32 |
int w, h; |
33 |
SDL_Surface *image; |
34 |
SDL_Rect area; |
35 |
Uint32 saved_flags; |
36 |
Uint8 saved_alpha; |
37 |
|
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 */ |
44 |
tex2oord[1] = 0.0f; /* Min Y */ |
45 |
tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ |
46 |
tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ |
47 |
|
48 |
image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
49 |
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
50 |
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
51 |
#else |
52 |
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
53 |
#endif |
54 |
); |
55 |
|
56 |
if (image == NULL) |
57 |
return 0; |
58 |
|
59 |
/* Save the alpha blending attributes */ |
60 |
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
61 |
saved_alpha = surface->format->alpha; |
62 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
63 |
SDL_SetAlpha (surface, 0, 0); |
64 |
|
65 |
/* Copy the surface into the GL texture image */ |
66 |
area.x = 0; |
67 |
area.y = 0; |
68 |
area.w = surface->w; |
69 |
area.h = surface->h; |
70 |
SDL_BlitSurface (surface, &area, image, &area); |
71 |
|
72 |
/* Restore the alpha blending attributes */ |
73 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
74 |
SDL_SetAlpha (surface, saved_flags, saved_alpha); |
75 |
|
76 |
/* Create an OpenGL texture for the image */ |
77 |
glGenTextures (1, &name); |
78 |
glBindTexture (GL_TEXTURE_2D, name); |
79 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
80 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
81 |
glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
82 |
glTexImage2D (GL_TEXTURE_2D, |
83 |
0, |
84 |
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
85 |
SDL_FreeSurface (image); /* No longer needed */ |
86 |
|
87 |
return name; |
88 |
} |
89 |
|
90 |
test_material::test_material () |
91 |
: tex ("textures/osama.jpg"), texvar (tex.name) |
92 |
{ |
93 |
using namespace shader; |
94 |
|
95 |
p.vsh->start (); |
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 () |
123 |
{ |
124 |
p.enable (); |
125 |
texvar->enable (); |
126 |
} |
127 |
|
128 |
void test_material::disable () |
129 |
{ |
130 |
texvar->disable (); |
131 |
p.disable (); |
132 |
} |
133 |
|