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

glsl_toon.C

Go to the documentation of this file.
00001 /*****************************************************************
00002  * glsl_toon.C
00003  *****************************************************************/
00004 #include "gtex/gl_extensions.H"
00005 #include "glsl_toon.H"
00006 
00007 static bool debug = Config::get_var_bool("DEBUG_GLSL_TOON", false);
00008 
00009 /**********************************************************************
00010  * GLSLToonShader:
00011  *
00012  *   Does 1D Toon shading in GLSL.
00013  **********************************************************************/
00014 GLuint          GLSLToonShader::_program = 0;
00015 bool            GLSLToonShader::_did_init = false;
00016 GLint           GLSLToonShader::_tex_loc = -1;
00017 GLSLToonShader* GLSLToonShader::_instance(0);
00018 
00019 GLSLToonShader::GLSLToonShader(Patch* p) : GLSLShader(p)
00020 {
00021    set_tex(GtexUtil::toon_name(
00022       Config::get_var_str("GLSL_TOON_FILENAME","clear-black.png")
00023       ));
00024 }
00025 
00026 GLSLToonShader* 
00027 GLSLToonShader::get_instance()
00028 {
00029    if (!_instance) {
00030       _instance = new GLSLToonShader();
00031       assert(_instance);
00032    }
00033    return _instance;
00034 }
00035 
00036 // If this isn't declared static, it never gets called!!!!!!!!!
00037 static void
00038 init_params(TEXTUREglptr& tex)
00039 {
00040    // set parameters for 1D toon shading
00041    if (tex) {
00042       tex->set_wrap_s(GL_CLAMP_TO_EDGE);
00043       tex->set_wrap_t(GL_CLAMP_TO_EDGE);
00044       tex->set_mipmap(false);
00045    }
00046 }
00047 
00048 void 
00049 GLSLToonShader::set_tex(Cstr_ptr& filename)
00050 {
00051    // Set the name of the texture to use
00052    if (_tex) {
00053       _tex->set_texture(filename);
00054    } else {
00055       set_tex(new TEXTUREgl(filename));
00056       assert(_tex);
00057    }
00058 }
00059 
00060 void 
00061 GLSLToonShader::set_tex(CTEXTUREglptr& tex)
00062 {
00063    // set the TEXTUREgl to use:
00064    init_params(_tex = tex);
00065 }
00066 
00067 void
00068 GLSLToonShader::init_textures()
00069 {
00070    // no-op after the first time:
00071    if (!load_texture(_tex))
00072       return;
00073 }
00074 
00075 void
00076 GLSLToonShader::activate_textures()
00077 {
00078    activate_texture(_tex);      // GL_ENABLE_BIT
00079 /*
00080    if (_tex->wrap_r() == GL_CLAMP_TO_EDGE)
00081       cerr << "wrap r is correct" << endl;
00082    else
00083       cerr << "wrap r is incorrect" << endl;
00084 
00085    if (_tex->wrap_s() == GL_CLAMP_TO_EDGE)
00086       cerr << "wrap s is correct" << endl;
00087    else
00088       cerr << "wrap s is incorrect" << endl;
00089 
00090    if (_tex->wrap_t() == GL_CLAMP_TO_EDGE)
00091       cerr << "wrap t is correct" << endl;
00092    else
00093       cerr << "wrap t is incorrect" << endl;
00094 */
00095 }
00096 
00097 bool 
00098 GLSLToonShader::get_variable_locs()
00099 {
00100    get_uniform_loc("toon_tex", _tex_loc);
00101 
00102    // other variables here as needed...
00103 
00104    return true;
00105 }
00106 
00107 bool
00108 GLSLToonShader::set_uniform_variables() const
00109 {
00110    // send uniform variable values to the program
00111 
00112    if (_tex) {
00113       glUniform1i(_tex_loc, _tex->get_tex_unit() - GL_TEXTURE0);
00114       return true;
00115    }
00116    return false;
00117 }
00118 
00119 void
00120 GLSLToonShader::draw_start(
00121    TEXTUREglptr toon_tex,       // the 1D toon texture
00122    CCOLOR& base_color,          // base color for triangles
00123    double  base_alpha,          // base alpha for triangles
00124    bool    do_culling           // enable backface culling
00125    )
00126 {
00127    // init program
00128    get_instance()->init();
00129  
00130    // set toon texture on static instance:
00131    get_instance()->set_tex(toon_tex);
00132 
00133    // set gl state
00134    glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
00135    if (do_culling) glEnable (GL_CULL_FACE);     // GL_ENABLE_BIT
00136    else            glDisable(GL_CULL_FACE);
00137    GL_COL(base_color, base_alpha);              // GL_CURRENT_BIT
00138 
00139    // activate texture
00140    get_instance()->activate_textures();
00141 
00142    // activate program
00143    get_instance()->activate_program();
00144 
00145    // set uniform variables
00146    get_instance()->set_uniform_variables();
00147 }
00148 
00149 void
00150 GLSLToonShader::draw_end()
00151 {
00152    // deactivate program
00153    get_instance()->deactivate_program();
00154 
00155    // resture gl state
00156    get_instance()->restore_gl_state();
00157 }
00158 
00159 // end of file glsl_toon.C

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