1 |
#ifndef MATERIAL_H |
2 |
#define MATERIAL_H |
3 |
|
4 |
#include "util.h" |
5 |
|
6 |
|
7 |
struct texture_execption |
8 |
{ |
9 |
texture_execption (string s) : msg(s) { } |
10 |
string msg; |
11 |
}; |
12 |
|
13 |
inline int power_of_two (int input) |
14 |
{ |
15 |
int value = 1; |
16 |
|
17 |
while (value < input) |
18 |
{ |
19 |
value <<= 1; |
20 |
} |
21 |
return value; |
22 |
} |
23 |
|
24 |
|
25 |
|
26 |
struct material |
27 |
{ |
28 |
virtual void begin () = 0; |
29 |
virtual void end () = 0; |
30 |
virtual ~material (); |
31 |
}; |
32 |
|
33 |
struct simple_material : material |
34 |
{ |
35 |
colour diffuse, specular, emission; |
36 |
GLfloat shininess; |
37 |
|
38 |
void begin (); |
39 |
void end (); |
40 |
|
41 |
simple_material () |
42 |
: diffuse(1, 0, 1, 1) |
43 |
, specular(1, 0, 1, 1) |
44 |
, emission(1, 0, 1, 1) |
45 |
, shininess(1.) |
46 |
{ |
47 |
} |
48 |
}; |
49 |
|
50 |
struct texture |
51 |
{ |
52 |
SDL_Surface *image; |
53 |
GLuint texture_pxls; |
54 |
GLfloat texcoord[4]; |
55 |
|
56 |
GLuint load_texture (SDL_Surface *surface, GLfloat *texcoord); |
57 |
|
58 |
texture(const char *f) |
59 |
{ |
60 |
image = IMG_Load (f); |
61 |
|
62 |
if (!image) |
63 |
throw (texture_execption ("Couldn't load " + (string) f + ": " + (string) IMG_GetError ())); |
64 |
|
65 |
texture_pxls = load_texture (image, (GLfloat*)&texcoord); |
66 |
if (texture_pxls == 0) |
67 |
throw (texture_execption ("Couldn't make GL Texture, failed to create new RGB SWSurface")); |
68 |
} |
69 |
}; |
70 |
|
71 |
extern CGcontext cgc; |
72 |
|
73 |
struct shader |
74 |
{ |
75 |
CGprogram vsh, fsh; |
76 |
CGparameter lightpos; |
77 |
// mv, mvp |
78 |
CGprofile vsh_profile, fsh_profile; |
79 |
CGparameter g_Texture; // the texture parameter |
80 |
|
81 |
void check_cg_error (void) |
82 |
{ |
83 |
CGerror err = cgGetError (); |
84 |
|
85 |
if (err != CG_NO_ERROR) |
86 |
{ |
87 |
printf("CG error: %s\n", cgGetErrorString (err)); |
88 |
exit(1); |
89 |
} |
90 |
} |
91 |
shader (const char *vshp, const char *fshp) |
92 |
{ |
93 |
vsh_profile = CG_PROFILE_ARBVP1; |
94 |
//if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30; |
95 |
//if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40; |
96 |
fsh_profile = CG_PROFILE_ARBFP1; |
97 |
//if (cgGLIsProfileSupported (CG_PROFILE_FP30)) fsh_profile = CG_PROFILE_FP30; |
98 |
//if (cgGLIsProfileSupported (CG_PROFILE_FP40)) fsh_profile = CG_PROFILE_FP40; |
99 |
|
100 |
vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, vshp, vsh_profile, 0, 0); |
101 |
check_cg_error (); |
102 |
cgGLLoadProgram (vsh); |
103 |
check_cg_error (); |
104 |
|
105 |
fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, fshp, fsh_profile, 0, 0); |
106 |
check_cg_error (); |
107 |
cgGLLoadProgram (fsh); |
108 |
check_cg_error (); |
109 |
|
110 |
cgGLBindProgram (vsh); |
111 |
cgGLBindProgram (fsh); |
112 |
|
113 |
// mv = cgGetNamedParameter (vsh, "WorldProj"); |
114 |
// mvp = cgGetNamedParameter (vsh, "WorldViewProj"); |
115 |
//lightpos = cgGetNamedParameter (vsh, "LightPos"); |
116 |
//cgGLSetParameter4f (lightpos, 10, 10, 0, 1); //camera.p.x, camera.p.y, camera.p.z, 1); |
117 |
|
118 |
check_cg_error (); |
119 |
} |
120 |
}; |
121 |
|
122 |
struct osama_material : material, texture, shader { |
123 |
osama_material () |
124 |
: shader ("vsh.cg", "fsh.cg"), texture ("textures/osama.jpg") |
125 |
{ |
126 |
g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;) |
127 |
cgGLSetTextureParameter(g_Texture, texture_pxls); // Bind the texture number 999 to g_Texture |
128 |
} |
129 |
|
130 |
void begin (); |
131 |
void end (); |
132 |
|
133 |
}; |
134 |
|
135 |
void init_shaders (); |
136 |
#endif |
137 |
|
138 |
|