1 |
#include "opengl.h" |
2 |
#include "material.h" |
3 |
|
4 |
material::~material () |
5 |
{ |
6 |
} |
7 |
|
8 |
void simple_material::begin () |
9 |
{ |
10 |
glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&diffuse); |
11 |
glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&specular); |
12 |
glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&emission); |
13 |
glMaterialf (GL_FRONT, GL_SHININESS, shininess); |
14 |
} |
15 |
|
16 |
void simple_material::end () |
17 |
{ |
18 |
} |
19 |
void osama_material::begin () |
20 |
{ |
21 |
cgGLEnableProfile (vsh_profile); |
22 |
cgGLEnableProfile (fsh_profile); |
23 |
cgGLBindProgram (vsh); |
24 |
cgGLBindProgram (fsh); |
25 |
cgGLEnableTextureParameter(g_Texture); |
26 |
} |
27 |
void osama_material::end () |
28 |
{ |
29 |
cgGLDisableTextureParameter(g_Texture); |
30 |
cgGLUnbindProgram (vsh_profile); |
31 |
cgGLUnbindProgram (fsh_profile); |
32 |
cgGLDisableProfile (vsh_profile); |
33 |
cgGLDisableProfile (fsh_profile); |
34 |
} |
35 |
|
36 |
|
37 |
GLuint texture::load_texture (SDL_Surface *surface, GLfloat *texcoord) |
38 |
{ |
39 |
GLuint texture; |
40 |
int w, h; |
41 |
SDL_Surface *image; |
42 |
SDL_Rect area; |
43 |
Uint32 saved_flags; |
44 |
Uint8 saved_alpha; |
45 |
|
46 |
/* Use the surface width and height expanded to powers of 2 */ |
47 |
//w = power_of_two (surface->w); |
48 |
//h = power_of_two (surface->h); |
49 |
w = power_of_two (surface->w); |
50 |
h = power_of_two (surface->h); |
51 |
texcoord[0] = 0.0f; /* Min X */ |
52 |
texcoord[1] = 0.0f; /* Min Y */ |
53 |
texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
54 |
texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
55 |
|
56 |
image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
57 |
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
58 |
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
59 |
#else |
60 |
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
61 |
#endif |
62 |
); |
63 |
if (image == NULL) |
64 |
{ |
65 |
return 0; |
66 |
} |
67 |
|
68 |
/* Save the alpha blending attributes */ |
69 |
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
70 |
saved_alpha = surface->format->alpha; |
71 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
72 |
{ |
73 |
SDL_SetAlpha (surface, 0, 0); |
74 |
} |
75 |
|
76 |
/* Copy the surface into the GL texture image */ |
77 |
area.x = 0; |
78 |
area.y = 0; |
79 |
area.w = surface->w; |
80 |
area.h = surface->h; |
81 |
SDL_BlitSurface (surface, &area, image, &area); |
82 |
|
83 |
/* Restore the alpha blending attributes */ |
84 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
85 |
{ |
86 |
SDL_SetAlpha (surface, saved_flags, saved_alpha); |
87 |
} |
88 |
|
89 |
/* Create an OpenGL texture for the image */ |
90 |
glGenTextures (1, &texture); |
91 |
glBindTexture (GL_TEXTURE_2D, texture); |
92 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
93 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
94 |
glTexImage2D (GL_TEXTURE_2D, |
95 |
0, |
96 |
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
97 |
SDL_FreeSurface (image); /* No longer needed */ |
98 |
|
99 |
return texture; |
100 |
} |
101 |
|
102 |
|
103 |
CGcontext cgc; |
104 |
void init_shaders () { |
105 |
cgc = cgCreateContext (); |
106 |
} |