Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

glsl_paper.C

Go to the documentation of this file.
00001 /*****************************************************************
00002  * glsl_paper.C
00003  *****************************************************************/
00004 #include "gtex/gl_extensions.H"
00005 #include "gtex/ref_image.H"
00006 #include "glsl_paper.H"
00007 
00008 static bool debug = Config::get_var_bool("DEBUG_GLSL_PAPER", false);
00009 
00010 /*****************************************************************
00011  * Utilities
00012  *****************************************************************/
00013 inline str_ptr
00014 paper_tex_name()
00015 {
00016    return (
00017       Config::JOT_ROOT() +
00018       "nprdata/paper_textures/" +
00019       Config::get_var_str("GLSL_PAPER_FILENAME","basic_paper.png")
00020       );
00021 }
00022 
00023 inline str_ptr
00024 stroke_tex_name()
00025 {
00026    return (
00027       Config::JOT_ROOT() +
00028       "nprdata/stroke_textures/"+
00029       Config::get_var_str("GLSL_PAPER_TEX_FILENAME","one_d.png")
00030       );
00031 }
00032 
00033 /*****************************************************************
00034  * GLSLPaperShader:
00035  *****************************************************************/
00036 GLSLPaperShader* GLSLPaperShader::_instance(0);
00037 TEXTUREglptr     GLSLPaperShader::_paper_tex;
00038 str_ptr          GLSLPaperShader::_paper_tex_name;
00039 
00040 // XXX - bogus:
00041 str_ptr          GLSLPaperShader::_paper_tex_filename = "basic_paper.png";
00042 
00043 TEXTUREglptr     GLSLPaperShader::_tex;
00044 str_ptr          GLSLPaperShader::_tex_name;
00045 double           GLSLPaperShader::_contrast = 4.0;
00046 GLint            GLSLPaperShader::_paper_tex_loc(-1);
00047 GLint            GLSLPaperShader::_tex_loc(-1);
00048 GLint            GLSLPaperShader::_sample_origin(-1);
00049 GLint            GLSLPaperShader::_sample_u_vec(-1);
00050 GLint            GLSLPaperShader::_sample_v_vec(-1);
00051 GLint            GLSLPaperShader::_st_loc(-1);
00052 GLint            GLSLPaperShader::_tex_width_loc(-1);
00053 GLint            GLSLPaperShader::_tex_height_loc(-1);
00054 GLint            GLSLPaperShader::_contrast_loc(-1);
00055 
00056 GLuint           GLSLPaperShader::_program(0);
00057 bool             GLSLPaperShader::_did_init(false);
00058 bool             GLSLPaperShader::_do_texture(false);
00059 /**********************************************************************
00060  * GLSLPaperShader:
00061  *
00062  **********************************************************************/
00063 GLSLPaperShader::GLSLPaperShader(Patch* p) : GLSLShader(p)
00064 {
00065    set_paper(paper_tex_name());
00066    set_tex  (stroke_tex_name());
00067 }
00068 
00069 GLSLPaperShader* 
00070 GLSLPaperShader::instance() 
00071 {
00072    if (!_instance)
00073       _instance = new GLSLPaperShader(0);
00074    return _instance;      
00075 }
00076 
00077 void 
00078 GLSLPaperShader::set_paper(Cstr_ptr& filename)
00079 {
00080    // Set the name of the paper texture to use
00081 
00082    if (_paper_tex) {
00083       _paper_tex->set_texture(filename);
00084    } else {
00085       _paper_tex = new TEXTUREgl(filename);
00086       _paper_tex->set_tex_unit(GL_TEXTURE0 + TexUnit::PAPER);
00087       _paper_tex->set_wrap_s(GL_REPEAT);
00088       _paper_tex->set_wrap_t(GL_REPEAT);
00089       assert(_paper_tex);
00090    }
00091 }
00092 
00093 void 
00094 GLSLPaperShader::set_tex(Cstr_ptr& filename)
00095 {
00096    // name of stroke texture (will be bound to tex unit 3):
00097 
00098    if (_tex) {
00099       _tex->set_texture(filename);
00100    } else {
00101       _tex = new TEXTUREgl(filename);
00102       _tex->set_tex_unit(GL_TEXTURE0 + TexUnit::APP);
00103       _tex->set_wrap_s(GL_REPEAT);
00104       _tex->set_wrap_t(GL_REPEAT);
00105       assert(_tex);
00106    }
00107 }
00108 
00109 void
00110 GLSLPaperShader::init_textures()
00111 {
00112    // no-op after the first time:
00113    if (!load_texture(_paper_tex))
00114       return;
00115 
00116    if (!load_texture(_tex))
00117       return;
00118 }
00119 
00120 void
00121 GLSLPaperShader::activate_textures()
00122 {
00123    activate_texture(_paper_tex);        // GL_ENABLE_BIT
00124    activate_texture(_tex);              // GL_ENABLE_BIT
00125    TexMemRefImage::activate_tex_unit(); // GL_ENABLE_BIT
00126 }
00127 
00128 bool 
00129 GLSLPaperShader::get_variable_locs()
00130 {
00131    get_uniform_loc("paper_tex", _paper_tex_loc);
00132  
00133    if(_do_texture){      
00134       get_uniform_loc("texture", _tex_loc);
00135    }
00136    get_uniform_loc("origin",    _sample_origin);
00137    get_uniform_loc("u_vec",     _sample_u_vec);
00138    get_uniform_loc("v_vec",     _sample_v_vec);
00139 
00140    get_uniform_loc("st",        _st_loc);
00141 
00142    get_uniform_loc("tex_width", _tex_width_loc);
00143    get_uniform_loc("tex_height",_tex_height_loc);
00144    
00145    get_uniform_loc("contrast",  _contrast_loc);
00146 
00147    
00148    return true;
00149 }
00150 
00151 bool
00152 GLSLPaperShader::set_uniform_variables() const
00153 {
00154    //XXX - a hack, but I don't care...:D
00155    GLSLPaperShader* me = (GLSLPaperShader*)this; 
00156    glUniform1i(me->get_uniform_loc("paper_tex"), _paper_tex->get_tex_unit() - GL_TEXTURE0);
00157    
00158    if(_do_texture){      
00159       glUniform1i(me->get_uniform_loc("texture"), _tex->get_tex_unit() - GL_TEXTURE0);
00160    }
00161   
00162    //XXX - should be the size of the paper texture!?
00163    double tex_size = 350;
00164    glUniform1f(me->get_uniform_loc("tex_width"),  float(tex_size));
00165    glUniform1f(me->get_uniform_loc("tex_height"), float(tex_size));
00166 
00167    glUniform1f(me->get_uniform_loc("contrast"),   float(_contrast));
00168 
00169    if (!_patch)
00170      return true;
00171 
00172    _patch->update_dynamic_samples();
00173 
00174    glUniform2fv(me->get_uniform_loc("origin"), 1, float2(_patch->sample_origin()));
00175 
00176    if (_patch->get_do_lod()) {
00177       glUniform1f (me->get_uniform_loc("st"), float(_patch->lod_t()));
00178       glUniform2fv(me->get_uniform_loc("u_vec"),  1, float2(_patch->lod_u()));
00179       glUniform2fv(me->get_uniform_loc("v_vec"),  1, float2(_patch->lod_v()));
00180   
00181    } else {
00182       glUniform1f (_st_loc, float(0));
00183       glUniform2fv(_sample_u_vec,  1, float2(_patch->sample_u_vec()));
00184       glUniform2fv(_sample_v_vec,  1, float2(_patch->sample_v_vec()));
00185    }
00186  
00187    return true;
00188 }
00189 
00190 /**********************************************************************
00191  * Static Functions
00192  **********************************************************************/
00193 
00194 TEXTUREglptr    
00195 GLSLPaperShader::get_texture()
00196 {
00197    if (!_paper_tex) {
00198       // create the TEXTUREgl and check for errors:
00199       _paper_tex = new TEXTUREgl(_paper_tex_name);
00200       assert(_paper_tex);
00201    } else if (_paper_tex->is_valid()) {
00202       return _paper_tex;
00203    } else if (_paper_tex->load_attempt_failed()) {
00204       // we previously tried to load this texture and failed.
00205       // no sense trying again with the same filename.
00206       return 0;
00207    }
00208 
00209    return _paper_tex;
00210 
00211 }
00212 
00213 void    
00214 GLSLPaperShader::begin_glsl_paper(Patch* p)
00215 {
00216    GL_VIEW::print_gl_errors("GLSLPaperShader::begin_glsl_paper: start");
00217 
00218    GLSLPaperShader* paper = GLSLPaperShader::instance();
00219    paper->set_patch(p);
00220 
00221    if (!paper->init())
00222       return;
00223 
00224    GL_VIEW::print_gl_errors("GLSLPaperShader::begin_glsl_paper: init");
00225   
00226    // Load textures (if any) and set their parameters. 
00227    paper->init_textures();
00228     GL_VIEW::print_gl_errors("GLSLPaperShader::begin_glsl_paper: init_textures");
00229 
00230    
00231     // call glPushAttrib() and set desired state 
00232    paper->set_gl_state();
00233    // call glPushAttrib() and set desired state 
00234    //glPushAttrib(GL_ENABLE_BIT);
00235 
00236    // activate textures, if any:
00237    paper->activate_textures(); // GL_ENABLE_BIT
00238    GL_VIEW::print_gl_errors("GLSLPaperShader::begin_glsl_paper: activate_textures");
00239 
00240    // activate program:
00241    paper->activate_program();
00242    GL_VIEW::print_gl_errors("GLSLPaperShader::begin_glsl_paper: activate_program");
00243 
00244      
00245    // send values to uniform variables:
00246    paper->set_uniform_variables();
00247 
00248    GL_VIEW::print_gl_errors("GLSLPaperShader::begin_glsl_paper: end");
00249 }
00250 
00251 void
00252 GLSLPaperShader::end_glsl_paper()
00253 {
00254    GL_VIEW::print_gl_errors("GLSLPaperShader::end_glsl_paper: start");
00255    GLSLPaperShader* paper = GLSLPaperShader::instance();  
00256    paper->deactivate_program();
00257 
00258    // restore gl state:
00259    paper->restore_gl_state();
00260    // restore GL state
00261    //glPopAttrib();
00262   
00263    paper->set_patch(0);
00264    GL_VIEW::print_gl_errors("GLSLPaperShader::end_glsl_paper: end");
00265 }
00266 
00267 // end of file glsl_paper.C

Generated on Mon Sep 18 11:39:31 2006 for jot by  doxygen 1.4.4