glh_obs.h
Go to the documentation of this file.
00001 /*
00002     glh - is a platform-indepenedent C++ OpenGL helper library 
00003 
00004 
00005     Copyright (c) 2000 Cass Everitt
00006         Copyright (c) 2000 NVIDIA Corporation
00007     All rights reserved.
00008 
00009     Redistribution and use in source and binary forms, with or
00010         without modification, are permitted provided that the following
00011         conditions are met:
00012 
00013      * Redistributions of source code must retain the above
00014            copyright notice, this list of conditions and the following
00015            disclaimer.
00016 
00017      * Redistributions in binary form must reproduce the above
00018            copyright notice, this list of conditions and the following
00019            disclaimer in the documentation and/or other materials
00020            provided with the distribution.
00021 
00022      * The names of contributors to this software may not be used
00023            to endorse or promote products derived from this software
00024            without specific prior written permission. 
00025 
00026        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00029            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00030            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00033            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00034            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00035            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00036            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00037            POSSIBILITY OF SUCH DAMAGE. 
00038 
00039 
00040     Cass Everitt - cass@r3.nu
00041 */
00042 
00043 // This is a file for simple GL helper classes...
00044 
00045 #ifndef GLH_OBS_H
00046 #define GLH_OBS_H
00047 
00048 #ifdef _WIN32
00049 # include <windows.h>
00050 #endif
00051 
00052 #ifdef MACOS
00053 #include <OpenGL/gl.h>
00054 #include <OpenGL/glext.h>
00055 #else
00056 #include <GL/gl.h>
00057 #include <GL/glext.h>
00058 #endif
00059 
00060 #include <glh/glh_extensions.h>
00061 
00062 #include <glh/glh_linear.h>
00063 
00064 namespace glh
00065 {
00066         class display_list
00067         {
00068         public:
00069         // set managed to true if you want the class to cleanup objects in the destructor
00070                 display_list(bool managed = false) 
00071                         : valid(false), manageObjects(managed) {}
00072                 
00073                 virtual ~display_list()
00074                 { 
00075             if (manageObjects)
00076                 del();
00077         }
00078                 
00079                 void call_list()
00080                 { if(valid) glCallList(dlist); }
00081                 
00082                 void new_list(GLenum mode)
00083                 { if(!valid) gen(); glNewList(dlist, mode); }
00084                 
00085                 void end_list()
00086                 { glEndList(); }
00087                 
00088                 void del()
00089                 { if(valid) glDeleteLists(dlist, 1); valid = false; }
00090                 
00091                 bool is_valid() const { return valid; }
00092                 
00093         private:
00094                 
00095                 void gen() { dlist = glGenLists(1); valid=true; }
00096                 
00097                 bool valid;
00098         bool manageObjects;
00099                 GLuint dlist;
00100         };
00101         
00102         class lazy_build_display_list
00103         {
00104         public:
00105         // set managed to true if you want the class to cleanup objects in the destructor
00106                 lazy_build_display_list(void (* builder)() = 0, bool managed = false) 
00107                         : valid(false), manageObjects(managed), needs_rebuild(true), build_func(builder) {}
00108                 
00109                 virtual ~lazy_build_display_list()
00110                 { 
00111             if (manageObjects)
00112                 del();
00113         }
00114 
00115                 void set_build_func( void (* builder)())
00116                 { build_func = builder; }
00117 
00118                 void call_list()
00119                 { 
00120                         if(! valid) gen();
00121                         if(needs_rebuild) rebuild_list();
00122                         glCallList(dlist);
00123                 }
00124                 
00125                 void del()
00126                 { if(valid) glDeleteLists(dlist, 1); valid = false; needs_rebuild = true;}
00127                 
00128                 bool is_valid() const { return valid; }
00129 
00130                 // something changed, so rebuild list before next call_list()
00131                 void rebuild() { needs_rebuild = true; }
00132                 
00133         private:
00134                 
00135                 void gen() { dlist = glGenLists(1); valid=true; }
00136                 void rebuild_list()
00137                 {
00138                         glNewList(dlist, GL_COMPILE);
00139                         if(build_func) (* build_func)(); // call list building routine
00140                         glEndList();
00141                 }
00142                 
00143                 bool valid;
00144         bool manageObjects;
00145                 bool needs_rebuild;
00146                 GLuint dlist;
00147                 void (* build_func)();
00148         };
00149         
00150         
00151         class tex_object
00152         {
00153         public:
00154         // set managed to true if you want the class to cleanup objects in the destructor
00155                 tex_object(GLenum tgt, bool managed) 
00156                         : target(tgt), valid(false), manageObjects(managed) {}
00157                 
00158                 virtual ~tex_object()
00159         { 
00160             if (manageObjects)
00161                 del();
00162         }
00163                 
00164                 void bind()
00165                 { if(!valid) gen(); glBindTexture(target, texture); }
00166                 
00167                 // convenience methods
00168 
00169 #ifdef MACOS
00170                 void parameter(GLenum pname, int i)
00171                 { glTexParameteri(target, pname, i); }
00172 #endif
00173 
00174                 void parameter(GLenum pname, GLint i)
00175                 { glTexParameteri(target, pname, i); }
00176 
00177                 void parameter(GLenum pname, GLfloat f)
00178                 { glTexParameterf(target, pname, f); }
00179 
00180                 void parameter(GLenum pname, GLint * ip)
00181                 { glTexParameteriv(target, pname, ip); }
00182 
00183                 void parameter(GLenum pname, GLfloat * fp)
00184                 { glTexParameterfv(target, pname, fp); }
00185 
00186                 void enable() { glEnable(target); }
00187                 void disable() { glDisable(target); }
00188 
00189 
00190         void del()
00191         { if(valid) glDeleteTextures(1, &texture); valid = false; }
00192         bool is_valid() const { return valid; }
00193                 
00194                 void gen() { glGenTextures(1, &texture); valid=true; }
00195                 
00196                 GLenum target;
00197                 bool valid;
00198         bool manageObjects;
00199                 GLuint texture;
00200         };
00201         
00202         class tex_object_1D : public tex_object
00203         { public: tex_object_1D(bool managed = false) : tex_object(GL_TEXTURE_1D, managed) {} };
00204         
00205         class tex_object_2D : public tex_object
00206         { public: tex_object_2D(bool managed = false) : tex_object(GL_TEXTURE_2D, managed) {} };
00207 
00208     class tex_object_3D : public tex_object
00209         { public: tex_object_3D(bool managed = false) : tex_object(GL_TEXTURE_3D, managed) {} };
00210 
00211 
00212 # ifdef GL_ARB_texture_cube_map
00213         class tex_object_cube_map : public tex_object
00214         { public: tex_object_cube_map(bool managed = false) : tex_object(GL_TEXTURE_CUBE_MAP_ARB, managed) {} };
00215 # elif GL_EXT_texture_cube_map
00216         class tex_object_cube_map : public tex_object
00217         { public: tex_object_cube_map(bool managed = false) : tex_object(GL_TEXTURE_CUBE_MAP_EXT, managed) {} };
00218 # endif
00219 
00220 #if defined(GL_EXT_texture_rectangle)
00221         class tex_object_rectangle : public tex_object
00222         { public: tex_object_rectangle(bool managed = false) : tex_object(GL_TEXTURE_RECTANGLE_EXT, managed) {} };
00223 #elif defined(GL_NV_texture_rectangle)
00224         class tex_object_rectangle : public tex_object
00225         { public: tex_object_rectangle(bool managed = false) : tex_object(GL_TEXTURE_RECTANGLE_NV, managed) {} };
00226 #endif
00227 
00228 
00229 # ifdef GL_NV_vertex_program
00230         class vertex_program_base
00231         {
00232         public:
00233         // set managed to true if you want the class to cleanup objects in the destructor
00234                 vertex_program_base(GLenum tgt, bool managed) 
00235                         : valid(false), manageObjects(managed), target(tgt) {}
00236                 
00237                 virtual ~vertex_program_base()
00238         { 
00239             if (manageObjects)
00240                 del();
00241         }
00242                 
00243                 void bind()
00244                 { if(!valid) gen(); glBindProgramNV(target, program); }
00245                 
00246                 void unbind()
00247                 { glBindProgramNV(target, 0); }
00248                 
00249                 void load(GLuint size, const GLubyte * prog_text)
00250                 {
00251                         if(!valid) gen();
00252                         glLoadProgramNV(target, program, size, prog_text);
00253                         GLint errpos;
00254                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &errpos);
00255                         if(errpos != -1)
00256                         {
00257                                 fprintf(stderr, "program error:\n");
00258                                 int bgn = errpos - 10;
00259                                 //bgn < 0 ? 0 : bgn;
00260                                 const char * c = (const char *)(prog_text + bgn);
00261                                 for(int i = 0; i < 30; i++)
00262                                 {
00263                                         if(bgn+i >= int(size-1))
00264                                                 break;
00265                                         fprintf(stderr, "%c", *c++);
00266                                 }
00267                                 fprintf(stderr, "\n");
00268                         }
00269                 }
00270 
00271                 void load(GLuint size, const char * prog_text)
00272                 {
00273                         if(!valid) gen();
00274                         glLoadProgramNV(target, program, size, (const GLubyte *) prog_text);
00275                         GLint errpos;
00276                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &errpos);
00277                         if(errpos != -1)
00278                         {
00279                                 fprintf(stderr, "program error:\n");
00280                                 int bgn = errpos - 10;
00281                                 //bgn < 0 ? 0 : bgn;
00282                                 const char * c = (const char *)(prog_text + bgn);
00283                                 for(int i = 0; i < 30; i++)
00284                                 {
00285                                         if(bgn+i >= int(size-1))
00286                                                 break;
00287                                         fprintf(stderr, "%c", *c++);
00288                                 }
00289                                 fprintf(stderr, "\n");
00290                         }
00291                 }
00292 
00293                 void load(const char * prog_text)
00294                 { load((GLuint)strlen(prog_text), prog_text); }
00295 
00296 
00297                 void del()
00298                 { if(valid) glDeleteProgramsNV(1, &program); valid = false; }
00299                 
00300                 bool is_valid() const { return valid; }
00301                 
00302         private:
00303                 
00304                 void gen() { glGenProgramsNV(1, &program); valid=true; }
00305                 
00306                 bool valid;
00307         bool manageObjects;
00308                 GLenum target;
00309                 GLuint program;
00310         };
00311 
00312         class vertex_program : public vertex_program_base
00313         {
00314         public:
00315                 vertex_program(bool managed = false) 
00316                         : vertex_program_base(GL_VERTEX_PROGRAM_NV, managed) {}
00317         };              
00318 
00319         class vertex_state_program : public vertex_program_base
00320         {
00321         public:
00322                 vertex_state_program(bool managed = false) 
00323                         : vertex_program_base(GL_VERTEX_STATE_PROGRAM_NV, managed) {}
00324         };
00325 
00326         class lazy_load_vertex_program : public vertex_program_base
00327         {
00328                 public:
00329                 lazy_load_vertex_program(void (*vp_loader)() = 0, bool managed = false) 
00330                         : vertex_program_base(GL_VERTEX_PROGRAM_NV, managed), needs_load(true), loader(vp_loader) {}
00331 
00332                 void bind()
00333                 {
00334                         vertex_program_base::bind();
00335                         if(needs_load && loader)
00336                         {
00337                                 (* loader)();
00338                                 needs_load = false;
00339                         }
00340                 }
00341 
00342                 void reload() { needs_load = true; }
00343 
00344                 private:
00345                         bool needs_load;
00346                         void (* loader)();
00347         };              
00348 
00349 
00350 #endif
00351 
00352 # ifdef GL_ARB_vertex_program
00353         class arb_vertex_program_base
00354         {
00355         public:
00356         // set managed to true if you want the class to cleanup objects in the destructor
00357                 arb_vertex_program_base(GLenum tgt, bool managed) 
00358                         : valid(false), manageObjects(managed), target(tgt) {}
00359                 
00360                 virtual ~arb_vertex_program_base()
00361         { 
00362             if (manageObjects)
00363                 del();
00364         }
00365                 
00366                 void bind()
00367                 { if(!valid) gen(); glBindProgramARB(target, program); }
00368                 
00369                 void unbind()
00370                 { glBindProgramARB(target, 0); }
00371                 
00372                 void load(GLuint size, const GLubyte * prog_text)
00373                 {
00374                         if(!valid) gen();
00375                         bind();
00376                         glProgramStringARB(target, GL_PROGRAM_FORMAT_ASCII_ARB, size, prog_text);
00377                         GLint errpos;
00378                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errpos);
00379                         if(errpos != -1)
00380                         {
00381                                 fprintf(stderr, "program error:\n");
00382                                 int bgn = errpos - 10;
00383                                 //bgn < 0 ? 0 : bgn;
00384                                 const char * c = (const char *)(prog_text + bgn);
00385                                 for(int i = 0; i < 30; i++)
00386                                 {
00387                                         if(bgn+i >= int(size-1))
00388                                                 break;
00389                                         fprintf(stderr, "%c", *c++);
00390                                 }
00391                                 fprintf(stderr, "\n");
00392                         }
00393                 }
00394 
00395                 void load(GLuint size, const char * prog_text)
00396                 {
00397                         if(!valid) gen();
00398                         bind();
00399                         glProgramStringARB(target, GL_PROGRAM_FORMAT_ASCII_ARB, size, prog_text);
00400                         GLint errpos;
00401                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errpos);
00402                         if(errpos != -1)
00403                         {
00404                                 fprintf(stderr, "program error:\n");
00405                                 int bgn = errpos - 10;
00406                                 //bgn < 0 ? 0 : bgn;
00407                                 const char * c = (const char *)(prog_text + bgn);
00408                                 for(int i = 0; i < 30; i++)
00409                                 {
00410                                         if(bgn+i >= int(size-1))
00411                                                 break;
00412                                         fprintf(stderr, "%c", *c++);
00413                                 }
00414                                 fprintf(stderr, "\n");
00415                         }
00416                 }
00417 
00418                 void load(const char * prog_text)
00419                 { load((GLuint)strlen(prog_text), prog_text); }
00420 
00421 
00422                 void del() { if(valid) glDeleteProgramsARB(1, &program); valid = false; }
00423                 
00424                 bool is_valid() const { return valid; }
00425                 
00426         private:
00427                 
00428                 void gen() { glGenProgramsARB(1, &program); valid=true; }
00429                 
00430                 bool valid;
00431         bool manageObjects;
00432                 GLenum target;
00433                 GLuint program;
00434         };
00435 
00436         class arb_vertex_program : public arb_vertex_program_base
00437         {
00438         public:
00439                 arb_vertex_program(bool managed = false) 
00440                         : arb_vertex_program_base(GL_VERTEX_PROGRAM_ARB, managed) {}
00441         };              
00442 
00443         class lazy_load_arb_vertex_program : public arb_vertex_program_base
00444         {
00445                 public:
00446                 lazy_load_arb_vertex_program(void (*vp_loader)() = 0, bool managed = false) 
00447                         : arb_vertex_program_base(GL_VERTEX_PROGRAM_ARB, managed), needs_load(true), loader(vp_loader) {}
00448 
00449                 void bind()
00450                 {
00451                         arb_vertex_program_base::bind();
00452                         if(needs_load && loader)
00453                         {
00454                                 (* loader)();
00455                                 needs_load = false;
00456                         }
00457                 }
00458 
00459                 void reload() { needs_load = true; }
00460 
00461                 private:
00462                         bool needs_load;
00463                         void (* loader)();
00464         };              
00465 
00466 #endif
00467 
00468 # ifdef GL_NV_fragment_program
00469 
00470         class fragment_program
00471         {
00472         public:
00473         // set managed to true if you want the class to cleanup objects in the destructor
00474                 fragment_program(bool managed = false) 
00475                         : valid(false), manageObjects(managed) {}
00476                 
00477                 virtual ~fragment_program()
00478         { 
00479             if (manageObjects)
00480                 del();
00481         }
00482                 
00483                 void bind()
00484                 { if(!valid) gen(); glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, program); }
00485                 
00486                 void load(GLuint size, const GLubyte * prog_text)
00487                 {
00488                         if(!valid) gen();
00489                         glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, program, size, prog_text);
00490                         GLint errpos;
00491                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &errpos);
00492                         if(errpos != -1)
00493                         {
00494                                 fprintf(stderr, "program error:\n");
00495                                 int bgn = errpos - 10;
00496                                 //bgn < 0 ? 0 : bgn;
00497                                 const char * c = (const char *)(prog_text + bgn);
00498                                 for(int i = 0; i < 30; i++)
00499                                 {
00500                                         if(bgn+i >= int(size-1))
00501                                                 break;
00502                                         fprintf(stderr, "%c", *c++);
00503                                 }
00504                                 fprintf(stderr, "\n");
00505                         }
00506                 }
00507 
00508                 void load(GLuint size, const char * prog_text)
00509                 {
00510                         if(!valid) gen();
00511                         glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, program, size, (const GLubyte *) prog_text);
00512                         GLint errpos;
00513                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &errpos);
00514                         if(errpos != -1)
00515                         {
00516                                 fprintf(stderr, "program error:\n");
00517                                 int bgn = errpos - 10;
00518 
00519                                 const char * c = (const char *)(prog_text + bgn);
00520                                 for(int i = 0; i < 30; i++)
00521                                 {
00522                                         if(bgn+i >= int(size-1))
00523                                                 break;
00524                                         fprintf(stderr, "%c", *c++);
00525                                 }
00526                                 fprintf(stderr, "\n");
00527                         }
00528                 }
00529 
00530                 void load(const char * prog_text)
00531                 { load((GLuint)strlen(prog_text), prog_text); }
00532 
00533                 void del()
00534                 { if(valid) glDeleteProgramsNV(1, &program); valid = false; }
00535                 
00536                 bool is_valid() const { return valid; }
00537                 
00538                 GLuint program;
00539         private:
00540                 
00541                 void gen() { glGenProgramsNV(1, &program); valid=true; }
00542                 
00543                 bool valid;
00544         bool manageObjects;
00545         };
00546 
00547 
00548 #endif
00549 
00550 #ifdef GL_ARB_fragment_program
00551 
00552         class arb_fragment_program
00553         {
00554         public:
00555         // set managed to true if you want the class to cleanup objects in the destructor
00556                 arb_fragment_program(bool managed = false) 
00557                         : valid(false), manageObjects(managed) {}
00558                 
00559                 virtual ~arb_fragment_program()
00560         { 
00561             if (manageObjects)
00562                 del();
00563         }
00564                 
00565                 void bind()
00566                 { if(!valid) gen(); glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program); }
00567                 
00568                 void load(GLuint size, const GLubyte * prog_text)
00569                 {
00570                         if(!valid) gen();
00571             glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, size, prog_text);
00572                         //glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, program, size, prog_text);
00573 
00574                         GLint errpos;
00575                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errpos);
00576                         if(errpos != -1)
00577                         {
00578                                 fprintf(stderr, "program error:\n");
00579                                 int bgn = errpos - 10;
00580                                 //bgn < 0 ? 0 : bgn;
00581                                 const char * c = (const char *)(prog_text + bgn);
00582                                 for(int i = 0; i < 30; i++)
00583                                 {
00584                                         if(bgn+i >= int(size-1))
00585                                                 break;
00586                                         fprintf(stderr, "%c", *c++);
00587                                 }
00588                                 fprintf(stderr, "\n");
00589                         }
00590                 }
00591 
00592                 void load(GLuint size, const char * prog_text)
00593                 {
00594                         if(!valid) gen();
00595             glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, size, prog_text);
00596                         //glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, program, size, (const GLubyte *) prog_text);
00597                         GLint errpos;
00598                         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errpos);
00599                         if(errpos != -1)
00600                         {
00601                                 fprintf(stderr, "program error:\n");
00602                                 int bgn = errpos - 10;
00603 
00604                                 const char * c = (const char *)(prog_text + bgn);
00605                                 for(int i = 0; i < 30; i++)
00606                                 {
00607                                         if(bgn+i >= int(size-1))
00608                                                 break;
00609                                         fprintf(stderr, "%c", *c++);
00610                                 }
00611                                 fprintf(stderr, "\n");
00612                         }
00613                 }
00614 
00615                 void load(const char * prog_text)
00616                 { load((GLuint)strlen(prog_text), prog_text); }
00617 
00618                 void del()
00619                 { if(valid) glDeleteProgramsARB(1, &program); valid = false; }
00620                 
00621                 bool is_valid() const { return valid; }
00622                 
00623                 GLuint program;
00624         private:
00625                 
00626                 void gen() { glGenProgramsARB(1, &program); valid=true; }
00627                 
00628                 bool valid;
00629         bool manageObjects;
00630         };
00631 
00632 
00633 #endif
00634 
00635 }
00636 #endif


nao_openni
Author(s): Bener SUAY
autogenerated on Mon Jan 6 2014 11:27:50