gl.h
Go to the documentation of this file.
00001 /* This file is part of the Pangolin Project.
00002  * http://github.com/stevenlovegrove/Pangolin
00003  *
00004  * Copyright (c) 2011 Steven Lovegrove
00005  *
00006  * Permission is hereby granted, free of charge, to any person
00007  * obtaining a copy of this software and associated documentation
00008  * files (the "Software"), to deal in the Software without
00009  * restriction, including without limitation the rights to use,
00010  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following
00013  * conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be
00016  * included in all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00019  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00020  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00021  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00022  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00023  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00025  * OTHER DEALINGS IN THE SOFTWARE.
00026  */
00027 
00028 #ifndef PANGOLIN_GL_H
00029 #define PANGOLIN_GL_H
00030 
00031 #include "platform.h"
00032 
00033 #ifdef _WIN_
00034 #include <Windows.h>
00035 #endif
00036 
00037 #include <GL/glew.h>
00038 #include <GL/gl.h>
00039 
00040 #include <math.h>
00041 
00042 namespace pangolin
00043 {
00044 
00046 // Interface
00048 
00049 struct GlTexture
00050 {
00052     GlTexture(GLint width, GLint height, GLint internal_format = GL_RGBA8 );
00053     ~GlTexture();
00054 
00055     void Bind() const;
00056     void Unbind() const;
00057 
00060     void Upload(void* image, GLenum data_layout = GL_LUMINANCE, GLenum data_type = GL_FLOAT);
00061 
00062     void SetLinear();
00063     void SetNearestNeighbour();
00064 
00065     void RenderToViewport() const;
00066     void RenderToViewportFlipY() const;
00067 
00068     GLint internal_format;
00069     GLuint tid;
00070     GLint width;
00071     GLint height;
00072 };
00073 
00074 struct GlRenderBuffer
00075 {
00076     GlRenderBuffer(GLint width, GLint height, GLint internal_format = GL_DEPTH_COMPONENT24);
00077     ~GlRenderBuffer();
00078 
00079     GLuint rbid;
00080 };
00081 
00082 struct GlFramebuffer
00083 {
00084     GlFramebuffer();
00085     GlFramebuffer(GlTexture& colour, GlRenderBuffer& depth);
00086     GlFramebuffer(GlTexture& colour0, GlTexture& colour1, GlRenderBuffer& depth);
00087     ~GlFramebuffer();
00088 
00089     void Bind() const;
00090     void Unbind() const;
00091 
00092     GLuint fbid;
00093     unsigned attachments;
00094 };
00095 
00096 void glColorHSV( double hue, double s, double v );
00097 
00098 void glColorBin( int bin, int max_bins, double sat = 1.0, double val = 1.0 );
00099 
00100 void glPixelTransferScale( float r, float g, float b );
00101 void glPixelTransferScale( float scale );
00102 
00104 // Implementation
00106 
00107 const int MAX_ATTACHMENTS = 8;
00108 
00109 const static GLuint attachment_buffers[] =
00110 {
00111     GL_COLOR_ATTACHMENT0_EXT,
00112     GL_COLOR_ATTACHMENT1_EXT,
00113     GL_COLOR_ATTACHMENT2_EXT,
00114     GL_COLOR_ATTACHMENT3_EXT,
00115     GL_COLOR_ATTACHMENT4_EXT,
00116     GL_COLOR_ATTACHMENT5_EXT,
00117     GL_COLOR_ATTACHMENT6_EXT,
00118     GL_COLOR_ATTACHMENT7_EXT
00119 };
00120 
00121 //template<typename T>
00122 //struct GlDataTypeTrait {};
00123 //template<> struct GlDataTypeTrait<float>{ static const GLenum type = GL_FLOAT; };
00124 //template<> struct GlDataTypeTrait<int>{ static const GLenum type = GL_INT; };
00125 //template<> struct GlDataTypeTrait<unsigned char>{ static const GLenum type = GL_UNSIGNED_BYTE; };
00126 
00127 inline GlTexture::GlTexture(GLint width, GLint height, GLint internal_format)
00128     : internal_format(internal_format),width(width),height(height)
00129 {
00130     glGenTextures(1,&tid);
00131     Bind();
00132     // GL_LUMINANCE and GL_FLOAT don't seem to actually affect buffer, but some values are required
00133     // for call to succeed.
00134     glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_LUMINANCE,GL_FLOAT,0);
00135     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00136     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00137 //  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00138 //  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00139     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00140     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00141 }
00142 
00143 inline GlTexture::~GlTexture()
00144 {
00145     glDeleteTextures(1,&tid);
00146 }
00147 
00148 inline void GlTexture::Bind() const
00149 {
00150     glBindTexture(GL_TEXTURE_2D, tid);
00151 }
00152 
00153 inline void GlTexture::Unbind() const
00154 {
00155     glBindTexture(GL_TEXTURE_2D, 0);
00156 }
00157 
00158 inline void GlTexture::Upload(void* image, GLenum data_layout, GLenum data_type )
00159 {
00160     Bind();
00161     glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,data_layout,data_type,image);
00162 }
00163 
00164 inline void GlTexture::SetLinear()
00165 {
00166     Bind();
00167     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00168     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00169     Unbind();
00170 }
00171 
00172 inline void GlTexture::SetNearestNeighbour()
00173 {
00174     Bind();
00175     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00176     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00177     Unbind();
00178 }
00179 
00180 
00181 
00182 inline void GlTexture::RenderToViewport() const
00183 {
00184     glMatrixMode(GL_PROJECTION);
00185     glLoadIdentity();
00186     glMatrixMode(GL_MODELVIEW);
00187     glLoadIdentity();
00188     Bind();
00189     glEnable(GL_TEXTURE_2D);
00190     glBegin(GL_QUADS);
00191     glTexCoord2f(0, 0);
00192     glVertex2d(-1,-1);
00193     glTexCoord2f(1, 0);
00194     glVertex2d(1,-1);
00195     glTexCoord2f(1, 1);
00196     glVertex2d(1,1);
00197     glTexCoord2f(0, 1);
00198     glVertex2d(-1,1);
00199     glEnd();
00200     glDisable(GL_TEXTURE_2D);
00201 }
00202 
00203 inline void GlTexture::RenderToViewportFlipY() const
00204 {
00205     glMatrixMode(GL_PROJECTION);
00206     glLoadIdentity();
00207     glMatrixMode(GL_MODELVIEW);
00208     glLoadIdentity();
00209     Bind();
00210     glEnable(GL_TEXTURE_2D);
00211     glBegin(GL_QUADS);
00212     glTexCoord2f(0, 0);
00213     glVertex2d(-1,1);
00214     glTexCoord2f(1, 0);
00215     glVertex2d(1,1);
00216     glTexCoord2f(1, 1);
00217     glVertex2d(1,-1);
00218     glTexCoord2f(0, 1);
00219     glVertex2d(-1,-1);
00220     glEnd();
00221     glDisable(GL_TEXTURE_2D);
00222 }
00223 
00224 inline GlRenderBuffer::GlRenderBuffer(GLint width, GLint height, GLint internal_format )
00225 {
00226     glGenRenderbuffersEXT(1, &rbid);
00227     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rbid);
00228     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internal_format, width, height);
00229     glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
00230 }
00231 
00232 inline GlRenderBuffer::~GlRenderBuffer()
00233 {
00234     glDeleteRenderbuffersEXT(1, &rbid);
00235 }
00236 
00237 inline GlFramebuffer::GlFramebuffer()
00238 {
00239     glGenFramebuffersEXT(1, &fbid);
00240 }
00241 
00242 inline GlFramebuffer::GlFramebuffer(GlTexture& colour, GlRenderBuffer& depth)
00243 {
00244     glGenFramebuffersEXT(1, &fbid);
00245     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbid);
00246     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colour.tid, 0);
00247     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth.rbid);
00248     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00249     attachments = 1;
00250 }
00251 
00252 inline GlFramebuffer::GlFramebuffer(GlTexture& colour0, GlTexture& colour1, GlRenderBuffer& depth)
00253 {
00254     glGenFramebuffersEXT(1, &fbid);
00255     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbid);
00256     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colour0.tid, 0);
00257     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, colour1.tid, 0);
00258     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth.rbid);
00259     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00260     attachments = 2;
00261 }
00262 
00263 inline GlFramebuffer::~GlFramebuffer()
00264 {
00265     glDeleteFramebuffersEXT(1, &fbid);
00266 }
00267 
00268 inline void GlFramebuffer::Bind() const
00269 {
00270     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbid);
00271     glDrawBuffers( attachments, attachment_buffers );
00272 }
00273 
00274 inline void GlFramebuffer::Unbind() const
00275 {
00276     glDrawBuffers( 1, attachment_buffers );
00277     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00278 }
00279 
00280 // h [0,360)
00281 // s [0,1]
00282 // v [0,1]
00283 inline void glColorHSV( double hue, double s, double v )
00284 {
00285     const double h = hue / 60.0;
00286     const int i = floor(h);
00287     const double f = (i%2 == 0) ? 1-(h-i) : h-i;
00288     const double m = v * (1-s);
00289     const double n = v * (1-s*f);
00290     switch(i)
00291     {
00292     case 0:
00293         glColor3d(v,n,m);
00294         break;
00295     case 1:
00296         glColor3d(n,v,m);
00297         break;
00298     case 2:
00299         glColor3d(m,v,n);
00300         break;
00301     case 3:
00302         glColor3d(m,n,v);
00303         break;
00304     case 4:
00305         glColor3d(n,m,v);
00306         break;
00307     case 5:
00308         glColor3d(v,m,n);
00309         break;
00310     default:
00311         break;
00312     }
00313 
00314 }
00315 
00316 inline void glColorBin( int bin, int max_bins, double sat, double val )
00317 {
00318     if( bin >= 0 )
00319     {
00320         const double hue = (double)(bin%max_bins) * 360.0 / (double)max_bins;
00321         glColorHSV(hue,sat,val);
00322     }
00323     else
00324     {
00325         glColor3f(1,1,1);
00326     }
00327 }
00328 
00329 inline void glPixelTransferScale( float r, float g, float b )
00330 {
00331     glPixelTransferf(GL_RED_SCALE,r);
00332     glPixelTransferf(GL_GREEN_SCALE,g);
00333     glPixelTransferf(GL_BLUE_SCALE,b);
00334 }
00335 
00336 inline void glPixelTransferScale( float scale )
00337 {
00338     glPixelTransferScale(scale,scale,scale);
00339 }
00340 
00341 
00342 
00343 }
00344 
00345 #endif // PANGOLIN_GL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


pangolin_wrapper
Author(s): Todor Stoyanov
autogenerated on Wed Feb 13 2013 14:03:25