ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/material.h
Revision: 1.11
Committed: Sat Oct 23 21:43:27 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1 -597 lines
Log Message:
*** empty log message ***

File Contents

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