00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include <fstream>
00012 #include <string>
00013 #include "gl.h"
00014 #include "glext.h"
00015 #include "fragmentprog.h"
00016
00017 using namespace std;
00018
00019
00020 unsigned int fp::sphereShadowOnRings = 0;
00021 unsigned int fp::eclipseShadow1 = 0;
00022 unsigned int fp::eclipseShadow2 = 0;
00023 unsigned int fp::texDiffuse = 0;
00024 unsigned int fp::texDiffuseBump = 0;
00025 unsigned int fp::texSpecular = 0;
00026 unsigned int fp::texSpecularAlpha = 0;
00027
00028
00029 class FragmentProcessorNV : public FragmentProcessor
00030 {
00031 public:
00032 FragmentProcessorNV();
00033 virtual ~FragmentProcessorNV();
00034
00035 virtual void enable();
00036 virtual void disable();
00037 virtual void use(unsigned int);
00038 virtual void parameter(fp::Parameter, float, float, float, float);
00039 virtual void parameter(fp::Parameter, const float*);
00040 };
00041
00042 class FragmentProcessorARB : public FragmentProcessor
00043 {
00044 public:
00045 FragmentProcessorARB();
00046 virtual ~FragmentProcessorARB();
00047
00048 virtual void enable();
00049 virtual void disable();
00050 virtual void use(unsigned int);
00051 virtual void parameter(fp::Parameter, float, float, float, float);
00052 virtual void parameter(fp::Parameter, const float*);
00053 };
00054
00055
00056
00057 static string* ReadTextFromFile(const string& filename)
00058 {
00059 ifstream textFile(filename.c_str(), ios::in);
00060 if (!textFile.good())
00061 return NULL;
00062
00063 string* s = new string();
00064
00065 char c;
00066 while (textFile.get(c))
00067 *s += c;
00068
00069 return s;
00070 }
00071
00072
00073 static int findLineNumber(const string& s, unsigned int index)
00074 {
00075 if (index >= s.length())
00076 return -1;
00077
00078 int lineno = 1;
00079 for (unsigned int i = 0; i < index; i++)
00080 {
00081 if (s[i] == '\n')
00082 lineno++;
00083 }
00084
00085 return lineno;
00086 }
00087
00088
00089 static bool LoadNvFragmentProgram(const string& filename, unsigned int& id)
00090 {
00091 cout << _("Loading NV fragment program: ") << filename << '\n';
00092
00093 string* source = ReadTextFromFile(filename);
00094 if (source == NULL)
00095 {
00096 cout << _("Error loading NV fragment program: ") << filename << '\n';
00097 return false;
00098 }
00099
00100 glx::glGenProgramsNV(1, (GLuint*) &id);
00101 glx::glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV,
00102 id,
00103 source->length(),
00104 reinterpret_cast<const GLubyte*>(source->c_str()));
00105
00106 delete source;
00107
00108 GLenum err = glGetError();
00109 if (err != GL_NO_ERROR)
00110 {
00111 GLint errPos = 0;
00112 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &errPos);
00113 cout << _("Error in fragment program ") << filename << ' ' <<
00114 glGetString(GL_PROGRAM_ERROR_STRING_NV) << '\n';
00115 return false;
00116 }
00117
00118 return true;
00119 }
00120
00121
00122 FragmentProcessor* fp::initNV()
00123 {
00124 cout << _("Initializing NV fragment programs . . .\n");
00125 if (!LoadNvFragmentProgram("shaders/shadow_on_rings_nv.fp", sphereShadowOnRings))
00126 return NULL;
00127 if (!LoadNvFragmentProgram("shaders/eclipse1_nv.fp", eclipseShadow1))
00128 return NULL;
00129 if (!LoadNvFragmentProgram("shaders/eclipse2_nv.fp", eclipseShadow2))
00130 return NULL;
00131 if (!LoadNvFragmentProgram("shaders/diffuse_nv.fp", texDiffuse))
00132 return NULL;
00133 if (!LoadNvFragmentProgram("shaders/bumpdiffuse_nv.fp", texDiffuseBump))
00134 return NULL;
00135 if (!LoadNvFragmentProgram("shaders/texphong_nv.fp", texSpecular))
00136 return NULL;
00137 if (!LoadNvFragmentProgram("shaders/texphong_alpha_nv.fp", texSpecularAlpha))
00138 return NULL;
00139
00140 cout << _("All NV fragment programs loaded successfully.\n");
00141
00142 return new FragmentProcessorNV();
00143 }
00144
00145
00146 FragmentProcessor* fp::initARB()
00147 {
00148 cout << _("Initializing ARB fragment programs . . .\n");
00149
00150 return new FragmentProcessorARB();
00151 }
00152
00153
00154 FragmentProcessor::FragmentProcessor()
00155 {
00156 }
00157
00158 FragmentProcessor::~FragmentProcessor()
00159 {
00160 }
00161
00162 void FragmentProcessor::parameter(fp::Parameter param, const Vec3f& v)
00163 {
00164 parameter(param, v.x, v.y, v.z, 0.0f);
00165 }
00166
00167 void FragmentProcessor::parameter(fp::Parameter param, const Point3f& p)
00168 {
00169 parameter(param, p.x, p.y, p.z, 0.0f);
00170 }
00171
00172 void FragmentProcessor::parameter(fp::Parameter param, const Color& c)
00173 {
00174 parameter(param, c.red(), c.green(), c.blue(), c.alpha());
00175 }
00176
00177
00178
00179
00180
00181 FragmentProcessorNV::FragmentProcessorNV()
00182 {
00183 }
00184
00185 FragmentProcessorNV::~FragmentProcessorNV()
00186 {
00187 }
00188
00189 void FragmentProcessorNV::enable()
00190 {
00191 glEnable(GL_FRAGMENT_PROGRAM_NV);
00192 }
00193
00194 void FragmentProcessorNV::disable()
00195 {
00196 glDisable(GL_FRAGMENT_PROGRAM_NV);
00197 }
00198
00199 void FragmentProcessorNV::use(unsigned int prog)
00200 {
00201 glx::glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, prog);
00202 }
00203
00204 void FragmentProcessorNV::parameter(fp::Parameter param,
00205 float x, float y, float z, float w)
00206 {
00207 glx::glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_NV,
00208 param, x, y, z, w);
00209 }
00210
00211 void FragmentProcessorNV::parameter(fp::Parameter param, const float* fv)
00212 {
00213 glx::glProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_NV, param, fv);
00214 }
00215
00216
00217
00218
00219 FragmentProcessorARB::FragmentProcessorARB()
00220 {
00221 }
00222
00223 FragmentProcessorARB::~FragmentProcessorARB()
00224 {
00225 }
00226
00227 void FragmentProcessorARB::enable()
00228 {
00229
00230 }
00231
00232 void FragmentProcessorARB::disable()
00233 {
00234
00235 }
00236
00237 void FragmentProcessorARB::use(unsigned int prog)
00238 {
00239
00240 }
00241
00242 void FragmentProcessorARB::parameter(fp::Parameter param,
00243 float x, float y, float z, float w)
00244 {
00245
00246 }
00247
00248 void FragmentProcessorARB::parameter(fp::Parameter param, const float* fv)
00249 {
00250
00251 }