ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/material.h
Revision: 1.3
Committed: Sun Oct 10 19:50:37 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +107 -0 lines
Log Message:
first material stuff

File Contents

# User Rev Content
1 root 1.1 #ifndef MATERIAL_H
2     #define MATERIAL_H
3    
4     #include "util.h"
5    
6 root 1.3
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 root 1.1 struct material
27     {
28 root 1.2 virtual void begin () = 0;
29     virtual void end () = 0;
30 root 1.1 virtual ~material ();
31     };
32    
33     struct simple_material : material
34     {
35     colour diffuse, specular, emission;
36     GLfloat shininess;
37    
38 root 1.2 void begin ();
39     void end ();
40 root 1.1
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 root 1.3 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    
80     void check_cg_error (void)
81     {
82     CGerror err = cgGetError ();
83    
84     if (err != CG_NO_ERROR)
85     {
86     printf("CG error: %s\n", cgGetErrorString (err));
87     exit(1);
88     }
89     }
90     shader (const char *vshp, const char *fshp)
91     {
92     vsh_profile = CG_PROFILE_ARBVP1;
93     //if (cgGLIsProfileSupported (CG_PROFILE_VP30)) vsh_profile = CG_PROFILE_VP30;
94     //if (cgGLIsProfileSupported (CG_PROFILE_VP40)) vsh_profile = CG_PROFILE_VP40;
95     cgGLSetOptimalOptions (vsh_profile);
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     cgGLSetOptimalOptions (fsh_profile);
100    
101     vsh = cgCreateProgramFromFile (cgc, CG_SOURCE, vshp, vsh_profile, 0, 0);
102     check_cg_error ();
103     cgGLLoadProgram (vsh);
104     check_cg_error ();
105    
106     fsh = cgCreateProgramFromFile (cgc, CG_SOURCE, fshp, fsh_profile, 0, 0);
107     check_cg_error ();
108     cgGLLoadProgram (fsh);
109     check_cg_error ();
110    
111    
112     // mv = cgGetNamedParameter (vsh, "WorldProj");
113     // mvp = cgGetNamedParameter (vsh, "WorldViewProj");
114     lightpos = cgGetNamedParameter (vsh, "LightPos");
115     cgGLSetParameter4f (lightpos, 10, 10, 0, 1); //camera.p.x, camera.p.y, camera.p.z, 1);
116     }
117    
118    
119     };
120    
121     struct osama_material : material, texture, shader {
122     CGparameter g_Texture; // the texture parameter
123     osama_material ()
124     : shader ("vsh.cg", "fsh.cg"), texture ("textures/osama.jg")
125     {
126     g_Texture = cgGetNamedParameter(fsh, "Texture"); // the texture cg-warper ;)
127     cgGLSetTextureParameter(g_Texture, this->texture_pxls); // Bind the texture number 999 to g_Texture
128     check_cg_error ();
129     }
130    
131     void begin ();
132     void end ();
133    
134     };
135    
136     void init_shaders ();
137 root 1.1 #endif
138    
139