00001
00002
00003
00004 #include "gtex/gl_extensions.H"
00005 #include "msld.H"
00006 #include "gtex/glsl_normal.H"
00007 #include "gtex/ref_image.H"
00008
00009 static bool debug = Config::get_var_bool("DEBUG_MSLD", false);
00010
00011 inline GTexture*
00012 get_normal_shader(Patch* p)
00013 {
00014 GLSLNormalShader* ret = new GLSLNormalShader(p);
00015
00016 return ret;
00017 }
00018
00019
00020
00021
00022
00023
00024 class MSLDStripCB : public GLStripCB {
00025 public:
00026 MSLDStripCB() {}
00027
00028 void set_locs(GLint loc[5]) {
00029 pdir1_attrib_loc = loc[0];
00030 pdir2_attrib_loc = loc[1];
00031 k1_attrib_loc = loc[2];
00032 k2_attrib_loc = loc[3];
00033 dcurv_tensor_attrib_loc = loc[4];
00034 }
00035
00036 virtual void faceCB(CBvert* v, CBface*);
00037
00038 private:
00039
00040 GLint pdir1_attrib_loc, pdir2_attrib_loc;
00041 GLint k1_attrib_loc, k2_attrib_loc;
00042 GLint dcurv_tensor_attrib_loc;
00043 };
00044
00045 void
00046 MSLDStripCB::faceCB(CBvert* v, CBface* f)
00047 {
00048 assert(v && f);
00049
00050
00051
00052 Wvec pdir1 = v->pdir1();
00053 Wvec pdir2 = v->pdir2();
00054
00055 glVertexAttrib3fARB(pdir1_attrib_loc,
00056 static_cast<GLfloat>(pdir1[0]),
00057 static_cast<GLfloat>(pdir1[1]),
00058 static_cast<GLfloat>(pdir1[2]));
00059 glVertexAttrib3fARB(pdir2_attrib_loc,
00060 static_cast<GLfloat>(pdir2[0]),
00061 static_cast<GLfloat>(pdir2[1]),
00062 static_cast<GLfloat>(pdir2[2]));
00063
00064 glVertexAttrib1fARB(k1_attrib_loc, static_cast<GLfloat>(v->k1()));
00065 glVertexAttrib1fARB(k2_attrib_loc, static_cast<GLfloat>(v->k2()));
00066
00067 double *dcurv = &(v->dcurv_tensor().dcurv[0]);
00068
00069 glVertexAttrib4fARB(dcurv_tensor_attrib_loc,
00070 static_cast<GLfloat>(dcurv[0]),
00071 static_cast<GLfloat>(dcurv[1]),
00072 static_cast<GLfloat>(dcurv[2]),
00073 static_cast<GLfloat>(dcurv[3]));
00074
00075 glNormal3dv(f->vert_normal(v).data());
00076 glVertex3dv(v->loc().data());
00077
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 GLuint MSLDShader::_program = 0;
00087 bool MSLDShader::_did_init = false;
00088
00089
00090
00091
00092 MSLDShader* MSLDShader::_instance(0);
00093
00094
00095 MSLDShader::MSLDShader(Patch* p) : GLSLShader(p), _tone_shader(0)
00096 {
00097 if(debug){
00098 cerr<<"MSLD Debug's working"<<endl;
00099 }
00100
00101 set_tone_shader(get_normal_shader(p));
00102 }
00103
00104 MSLDShader::~MSLDShader()
00105 {
00106 gtextures().delete_all();
00107 }
00108
00109 void
00110 MSLDShader::set_tone_shader(GTexture* g)
00111 {
00112 if (g == _tone_shader)
00113 return;
00114 delete _tone_shader;
00115 _tone_shader = g;
00116 changed();
00117 }
00118
00119 MSLDShader*
00120 MSLDShader::get_instance()
00121 {
00122 if (!_instance) {
00123 _instance = new MSLDShader();
00124 assert(_instance);
00125 }
00126 return _instance;
00127 }
00128
00129 bool
00130 MSLDShader::get_variable_locs()
00131 {
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 get_uniform_loc("tone_map", _tone_tex_loc);
00150 get_uniform_loc("width", _width_loc);
00151 get_uniform_loc("height", _height_loc);
00152
00153 return true;
00154 }
00155
00156 bool
00157 MSLDShader::set_uniform_variables() const
00158 {
00159
00160
00161 if(_patch){
00162
00163 glUniform1i(_tone_tex_loc, TexMemRefImage::lookup_tex_unit() - GL_TEXTURE0);
00164 glUniform1fARB(_width_loc, 1.0/VIEW::peek()->width());
00165 glUniform1fARB(_height_loc, 1.0/VIEW::peek()->height());
00166
00167
00168
00169
00170
00171 return true;
00172 }
00173
00174 return false;
00175 }
00176
00177 GTexture_list
00178 MSLDShader::gtextures() const
00179 {
00180 return GTexture_list(_tone_shader);
00181 }
00182
00183 void
00184 MSLDShader::set_gl_state(GLbitfield mask) const
00185 {
00186 GLSLShader::set_gl_state(mask);
00187
00188 GL_COL(VIEW::peek()->color(), alpha());
00189 }
00190
00191 RefImageClient::ref_img_t
00192 MSLDShader::use_ref_image()
00193 {
00194
00195 return ref_img_t(REF_IMG_TEX_MEM);
00196 }
00197
00198 int
00199 MSLDShader::draw_tex_mem_ref()
00200 {
00201
00202 return _tone_shader->draw(VIEW::peek());
00203 }
00204