globjects.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8521 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:57:35 +0100 (Mi, 10. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  OpenGL Objects
00048 //
00049 //=============================================================================
00050 
00051 
00052 #ifndef GL_OBJECTS_HH
00053 #define GL_OBJECTS_HH
00054 
00055 
00056 //== INCLUDES =================================================================
00057 
00058 // GL
00059 #include <ACG/GL/gl.hh>
00060 
00061 // C++
00062 #include <iostream>
00063 #include <fstream>
00064 #include <string>
00065 
00066 // C
00067 #include <stdio.h>
00068 #include <string.h>
00069 
00070 #ifdef max
00071 #  undef max
00072 #endif
00073 
00074 #ifdef min
00075 #  undef min
00076 #endif
00077 
00078 
00079 //== NAMESPACES ===============================================================
00080 
00081 namespace ACG {
00082 
00083 
00084 //== CLASS DEFINITION =========================================================
00085 
00086 
00087 class DisplayList
00088 {
00089 public:
00090 
00091   DisplayList() : valid(false) {}
00092 
00093   virtual ~DisplayList() { del(); }
00094 
00095   void call() { if (valid) glCallList(dlist); }
00096 
00097   void new_list(GLenum mode) { if(!valid) gen(); glNewList(dlist, mode); }
00098 
00099   void end_list() { glEndList(); }
00100 
00101   void del() { if(valid) glDeleteLists(dlist, 1); valid = false; }
00102 
00103   bool is_valid() const { return valid; }
00104 
00105 private:
00106 
00107   void gen() { dlist = glGenLists(1); valid=true; }
00108 
00109   bool valid;
00110   GLuint dlist;
00111 };
00112 
00113 
00114 //== CLASS DEFINITION =========================================================
00115 
00116 
00117 #if defined(GL_ARB_vertex_buffer_object)
00118 
00119 class VertexBufferObject
00120 {
00121 public:
00122 
00123   VertexBufferObject(GLenum _target) : target(_target), valid(false) {}
00124   virtual ~VertexBufferObject() { del(); }
00125 
00126   void del() { if(valid) glDeleteBuffersARB(1, &vbo); valid = false; }
00127   bool is_valid() const { return valid; }
00128 
00129   void bind()   { if(!valid) gen(); glBindBufferARB(target, vbo); }
00130   void unbind() { glBindBufferARB(target, 0); }
00131 
00132   void upload(GLsizeiptr size, const GLvoid* data, GLenum usage)
00133   {
00134     if(!valid) gen();
00135     glBufferDataARB(target, size, data, usage);
00136   }
00137 
00138   char* offset(unsigned int _offset) const
00139   {
00140     return ((char*)NULL + _offset);
00141   }
00142 
00143 
00144 private:
00145 
00146   void gen() { glGenBuffersARB(1, &vbo); }
00147 
00148   GLenum target;
00149   bool   valid;
00150   GLuint vbo;
00151 };
00152 
00153 
00154 class GeometryBuffer : public VertexBufferObject
00155 {
00156 public:
00157   GeometryBuffer() : VertexBufferObject(GL_ARRAY_BUFFER_ARB) {}
00158 };
00159 
00160 
00161 class IndexBuffer : public VertexBufferObject
00162 {
00163 public:
00164   IndexBuffer() : VertexBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB) {}
00165 };
00166 
00167 
00168 #endif
00169 
00170 
00171 //== CLASS DEFINITION =========================================================
00172 
00173 
00174 class Texture
00175 {
00176 public:
00177 
00178   Texture(GLenum tgt, GLenum _unit=GL_NONE)
00179     : target(tgt), unit(_unit), valid(false),texture(0)
00180   {}
00181 
00182   virtual ~Texture() { del(); }
00183 
00184   void bind()
00185   {
00186     if(!valid) gen();
00187     activate();
00188     glBindTexture(target, texture);
00189   }
00190 
00191   void activate()
00192   {
00193     if (unit != GL_NONE) glActiveTextureARB(unit);
00194   }
00195 
00196   void parameter(GLenum pname, GLint i)
00197   {
00198     activate();
00199     glTexParameteri(target, pname, i);
00200   }
00201 
00202   void parameter(GLenum pname, GLfloat f)
00203   {
00204     activate();
00205     glTexParameterf(target, pname, f);
00206   }
00207 
00208   void parameter(GLenum pname, GLint * ip)
00209   {
00210     activate();
00211     glTexParameteriv(target, pname, ip);
00212   }
00213 
00214   void parameter(GLenum pname, GLfloat * fp)
00215   {
00216     activate();
00217     glTexParameterfv(target, pname, fp);
00218   }
00219 
00220   void enable()
00221   {
00222     activate();
00223     glEnable(target);
00224   }
00225 
00226   void disable()
00227   {
00228     activate();
00229     glDisable(target);
00230   }
00231 
00232   void del()
00233   {
00234     if(valid) glDeleteTextures(1, &texture);
00235     valid = false;
00236   }
00237 
00238   bool is_valid() const { return valid; }
00239 
00240   void gen() { glGenTextures(1, &texture); valid=true; }
00241   
00242   GLuint id() { return texture; }
00243 
00244 private:
00245 
00246   GLenum target, unit;
00247   bool valid;
00248   GLuint texture;
00249 };
00250 
00251 
00252 //-----------------------------------------------------------------------------
00253 
00254 
00255 class Texture1D : public Texture
00256 {
00257 public:
00258   Texture1D(GLenum unit=GL_NONE) : Texture(GL_TEXTURE_1D, unit) {}
00259 };
00260 
00261 
00262 //-----------------------------------------------------------------------------
00263 
00264 
00265 class Texture2D : public Texture
00266 {
00267 public:
00268   Texture2D(GLenum unit=GL_NONE) : Texture(GL_TEXTURE_2D, unit) {}
00269 };
00270 
00271 
00272 //-----------------------------------------------------------------------------
00273 
00274 
00275 class Texture3D : public Texture
00276 {
00277 public:
00278   Texture3D(GLenum unit=GL_NONE) : Texture(GL_TEXTURE_3D, unit) {}
00279 };
00280 
00281 
00282 //-----------------------------------------------------------------------------
00283 
00284 
00285 #if defined(GL_ARB_texture_cube_map)
00286 
00287 class TextureCubeMap : public Texture
00288 {
00289 public:
00290   TextureCubeMap(GLenum u=GL_NONE) : Texture(GL_TEXTURE_CUBE_MAP_ARB, u) {}
00291 };
00292 
00293 #elif defined(GL_EXT_texture_cube_map)
00294 
00295 class TextureCubeMap : public Texture
00296 {
00297 public:
00298   TextureCubeMap(GLenum u=GL_NONE) : Texture(GL_TEXTURE_CUBE_MAP_EXT, u) {}
00299 };
00300 
00301 #endif
00302 
00303 
00304 //-----------------------------------------------------------------------------
00305 
00306 
00307 #if defined(GL_EXT_texture_rectangle)
00308 
00309 class TextureRectangleEXT : public Texture
00310 {
00311 public:
00312   TextureRectangleEXT(GLenum u=GL_NONE)
00313     : Texture(GL_TEXTURE_RECTANGLE_EXT, u) {}
00314 };
00315 
00316 #endif
00317 
00318 
00319 #if defined(GL_NV_texture_rectangle)
00320 
00321 class TextureRectangleNV : public Texture
00322 {
00323 public:
00324   TextureRectangleNV(GLenum u=GL_NONE)
00325     : Texture(GL_TEXTURE_RECTANGLE_NV, u) {}
00326 };
00327 
00328 #endif
00329 
00330 
00331 
00332 //== CLASS DEFINITION =========================================================
00333 
00334 
00335 class ProgramBase
00336 {
00337 public:
00338 
00339   ProgramBase(GLenum tgt) : valid(false), target(tgt), program(0) {}
00340   virtual ~ProgramBase() {}
00341 
00342   bool   is_valid() const { return valid; }
00343   GLuint id()       const { return program; }
00344 
00345   virtual void bind()   = 0;
00346   virtual void unbind() = 0;
00347   virtual bool load(const char* prog_text) = 0;
00348 
00349   bool load_file(const char* _filename)
00350   {
00351     std::ifstream ifs(_filename);
00352     if (!ifs)
00353     {
00354       std::cerr << "Can't open " << _filename << "\n";
00355       return false;
00356     }
00357     std::string prog;
00358     char line[255];
00359     while (!ifs.eof())
00360     {
00361       if (!ifs.getline(line, 255).bad())
00362       {
00363         prog += std::string(line);
00364         prog += '\n';
00365       }
00366     }
00367     ifs.close();
00368     return load(prog.c_str());
00369   }
00370 
00371 
00372 protected:
00373 
00374   bool valid;
00375   GLenum target;
00376   GLuint program;
00377 };
00378 
00379 
00380 //== CLASS DEFINITION =========================================================
00381 
00382 
00383 #if defined(GL_NV_vertex_program) || defined(GL_NV_fragment_program)
00384 
00385 class ProgramBaseNV : public ProgramBase
00386 {
00387 public:
00388 
00389   ProgramBaseNV(GLenum tgt) : ProgramBase(tgt) {}
00390   ~ProgramBaseNV() { del(); }
00391 
00392   void bind()   { if(!valid) gen(); glBindProgramNV(target, program); }
00393   void unbind() { glBindProgramNV(target, 0); }
00394 
00395   bool load(const char* prog_text)
00396   {
00397     int size=strlen(prog_text);
00398     if(!valid) gen();
00399     glLoadProgramNV(target, program, size, (const GLubyte *) prog_text);
00400     GLint errpos;
00401     glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &errpos);
00402     if(errpos != -1)
00403     {
00404       fprintf(stderr, "\nprogram error:\n");
00405       int bgn=std::max(0, errpos - 10), end=std::min(size, bgn+30);
00406       for(int i=bgn; i<end; ++i) fputc(prog_text[i], stderr);
00407       fputc('\n', stderr);
00408       return false;
00409     }
00410     return true;
00411   }
00412 
00413 
00414 private:
00415   void gen() { glGenProgramsNV(1, &program); valid=true; }
00416   void del() { if(valid) glDeleteProgramsNV(1, &program); valid=false; }
00417 };
00418 
00419 #endif
00420 
00421 
00422 //== CLASS DEFINITION =========================================================
00423 
00424 
00425 #if defined(GL_ARB_vertex_program) || defined(GL_ARB_fragment_program)
00426 
00427 class ProgramBaseARB : public ProgramBase
00428 {
00429 public:
00430 
00431   ProgramBaseARB(GLenum tgt) : ProgramBase(tgt) {}
00432   ~ProgramBaseARB() { del(); }
00433 
00434   void bind()   { if(!valid) gen(); glBindProgramARB(target, program); }
00435   void unbind() { glBindProgramARB(target, 0); }
00436 
00437   bool load(const char* prog_text)
00438   {
00439     int size=strlen(prog_text);
00440     if(!valid) gen();
00441     bind();
00442     glProgramStringARB(target, GL_PROGRAM_FORMAT_ASCII_ARB, size, prog_text);
00443     GLint errpos;
00444     glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errpos);
00445     if(errpos != -1)
00446     {
00447       fprintf(stderr, "\nprogram error:\n");
00448       int bgn=std::max(0, errpos - 10), end=std::min(size, bgn+30);
00449       for(int i=bgn; i<end; ++i) fputc(prog_text[i], stderr);
00450       fputc('\n', stderr);
00451       return false;
00452     }
00453     return true;
00454   }
00455 
00456   void parameter()
00457   {
00458   }
00459 
00460 private:
00461   void gen() { glGenProgramsARB(1, &program); valid=true; }
00462   void del() { if(valid) glDeleteProgramsARB(1, &program); valid=false; }
00463 };
00464 
00465 #endif
00466 
00467 
00468 //-----------------------------------------------------------------------------
00469 
00470 
00471 #if defined(GL_NV_vertex_program)
00472 
00473 class VertexProgramNV : public ProgramBaseNV
00474 {
00475 public:
00476   VertexProgramNV() : ProgramBaseNV(GL_VERTEX_PROGRAM_NV) {}
00477 };
00478 
00479 
00480 class VertexStateProgramNV : public ProgramBaseNV
00481 {
00482 public:
00483   VertexStateProgramNV() : ProgramBaseNV(GL_VERTEX_STATE_PROGRAM_NV) {}
00484 };
00485 
00486 #endif
00487 
00488 
00489 //-----------------------------------------------------------------------------
00490 
00491 
00492 #if defined(GL_NV_fragment_program)
00493 
00494 class FragmentProgramNV : public ProgramBaseNV
00495 {
00496 public:
00497   FragmentProgramNV() : ProgramBaseNV(GL_FRAGMENT_PROGRAM_NV) {}
00498 };
00499 
00500 #endif
00501 
00502 
00503 
00504 //-----------------------------------------------------------------------------
00505 
00506 
00507 #if defined(GL_ARB_vertex_program)
00508 
00509 class VertexProgramARB : public ProgramBaseARB
00510 {
00511 public:
00512   VertexProgramARB() : ProgramBaseARB(GL_VERTEX_PROGRAM_ARB) {}
00513 };
00514 
00515 #endif
00516 
00517 
00518 //-----------------------------------------------------------------------------
00519 
00520 
00521 #if defined(GL_ARB_fragment_program)
00522 
00523 class FragmentProgramARB : public ProgramBaseARB
00524 {
00525 public:
00526   FragmentProgramARB() : ProgramBaseARB(GL_FRAGMENT_PROGRAM_ARB) {}
00527 };
00528 
00529 #endif
00530 
00531 
00532 //=============================================================================
00533 } // namespace ACG
00534 //=============================================================================
00535 #endif // GL_OBJECTS_HH defined
00536 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .