include/cinder/gl/gl.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/Cinder.h"
00026 
00027 #if defined( CINDER_MAC )
00028     #include <OpenGL/gl.h>
00029     #include <OpenGL/glext.h>
00030 #elif defined( CINDER_MSW )
00031     #include "cinder/gl/GLee.h"
00032 #else
00033     #define CINDER_GLES
00034     #define CINDER_GLES1
00035 #endif
00036 
00037 #include "cinder/Exception.h"
00038 #include "cinder/Quaternion.h"
00039 #include "cinder/Matrix.h"
00040 #include "cinder/Vector.h"
00041 #include "cinder/Color.h"
00042 #include "cinder/Rect.h"
00043 #include "cinder/Font.h"
00044 #include "cinder/PolyLine.h"
00045 #include "cinder/AxisAlignedBox.h"
00046 
00047 #if defined( CINDER_MSW )
00048     #include <windows.h>
00049     #undef min
00050     #undef max
00051     #include <gl/gl.h>
00052 #elif defined( CINDER_COCOA_TOUCH )
00053     #include <OpenGLES/ES1/gl.h>
00054     #include <OpenGLES/ES1/glext.h>
00055 #elif defined( CINDER_MAC )
00056     #include <OpenGL/gl.h>
00057 #endif
00058 
00059 // forward declarations
00060 namespace cinder {
00061     class Camera; class TriMesh2d; class TriMesh; class Sphere;
00062     namespace gl {
00063          class VboMesh; class Texture;
00064          typedef std::shared_ptr<Texture>   TextureRef;
00065          typedef std::shared_ptr<VboMesh>   VboMeshRef;
00066     }
00067 } // namespace cinder
00068 
00069 namespace cinder { namespace gl {
00070 
00072 bool isExtensionAvailable( const std::string &extName );
00073 
00075 void clear( const ColorA &color = ColorA::black(), bool clearDepthBuffer = true );
00076 
00078 void enableVerticalSync( bool enable = true );
00080 inline void disableVerticalSync() { enableVerticalSync( false ); }
00082 bool isVerticalSyncEnabled();
00083 
00085 void setMatrices( const Camera &cam );
00087 void setModelView( const Camera &cam );
00089 void setProjection( const Camera &cam );
00091 void pushModelView();
00093 void popModelView();
00095 void pushModelView( const Camera &cam );
00097 void pushProjection( const Camera &cam );
00099 void pushMatrices();
00101 void popMatrices();
00103 void multModelView( const Matrix44f &mtx );
00105 void multProjection( const Matrix44f &mtx );
00107 Matrix44f getModelView();
00109 Matrix44f getProjection();
00110 
00112 void setMatricesWindowPersp( int screenWidth, int screenHeight, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f, bool originUpperLeft = true );
00114 inline void setMatricesWindowPersp( const Vec2i &screenSize, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f, bool originUpperLeft = true )
00115     { setMatricesWindowPersp( screenSize.x, screenSize.y, fovDegrees, nearPlane, farPlane ); }
00117 void setMatricesWindow( int screenWidth, int screenHeight, bool originUpperLeft = true );
00119 inline void setMatricesWindow( const Vec2i &screenSize, bool originUpperLeft = true ) { setMatricesWindow( screenSize.x, screenSize.y, originUpperLeft ); }
00120 
00122 Area getViewport();
00124 void setViewport( const Area &area );
00125 
00127 void translate( const Vec2f &pos );
00129 inline void translate( float x, float y ) { translate( Vec2f( x, y ) ); }
00131 void translate( const Vec3f &pos );
00133 inline void translate( float x, float y, float z ) { translate( Vec3f( x, y, z ) ); }
00134 
00136 void scale( const Vec3f &scl );
00138 inline void scale( const Vec2f &scl ) { scale( Vec3f( scl.x, scl.y, 1 ) ); }
00140 inline void scale( float x, float y ) { scale( Vec3f( x, y, 1 ) ); }
00142 inline void scale( float x, float y, float z ) { scale( Vec3f( x, y, z ) ); }
00143 
00145 void rotate( const Vec3f &xyz );
00147 void rotate( const Quatf &quat );
00149 inline void rotate( float degrees ) { rotate( Vec3f( 0, 0, degrees ) ); }
00150 
00151 #if ! defined( CINDER_GLES )
00152 
00153 inline void begin( GLenum mode ) { glBegin( mode ); }
00155 inline void end() { glEnd(); }
00157 inline void vertex( const Vec2f &v ) { glVertex2fv( &v.x ); }
00159 inline void vertex( float x, float y ) { glVertex2f( x, y ); }
00161 inline void vertex( const Vec3f &v ) { glVertex3fv( &v.x ); }
00163 inline void vertex( float x, float y, float z ) { glVertex3f( x, y, z ); }
00165 inline void texCoord( float x, float y ) { glTexCoord2f( x, y ); }
00167 inline void texCoord( const Vec2f &v ) { glTexCoord2f( v.x, v.y ); }
00169 inline void texCoord( float x, float y, float z ) { glTexCoord3f( x, y, z ); }
00171 inline void texCoord( const Vec3f &v ) { glTexCoord3f( v.x, v.y, v.z ); }
00172 #endif // ! defined( CINDER_GLES )
00173 
00174 inline void color( float r, float g, float b ) { glColor4f( r, g, b, 1.0f ); }
00176 inline void color( float r, float g, float b, float a ) { glColor4f( r, g, b, a ); }
00178 inline void color( const Color8u &c ) { glColor4ub( c.r, c.g, c.b, 255 ); }
00180 inline void color( const ColorA8u &c ) { glColor4ub( c.r, c.g, c.b, c.a ); }
00182 inline void color( const Color &c ) { glColor4f( c.r, c.g, c.b, 1.0f ); }
00184 inline void color( const ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); }
00185 
00187 inline void enable( GLenum state ) { glEnable( state ); }
00189 inline void disable( GLenum state ) { glDisable( state ); }
00190 
00192 void enableAlphaBlending( bool premultiplied = false );
00194 void disableAlphaBlending();
00196 void enableAdditiveBlending();
00197 
00200 void enableAlphaTest( float value = 0.5f, int func = GL_GREATER );
00202 void disableAlphaTest();
00203 
00204 #if ! defined( CINDER_GLES )
00205 
00206 void enableWireframe();
00208 void disableWireframe();
00209 #endif // ! defined( CINDER_GLES )
00210 
00212 void disableDepthRead();
00214 void disableDepthWrite();
00216 void enableDepthRead( bool enable = true );
00218 void enableDepthWrite( bool enable = true );
00219 
00221 inline void lineWidth( float width ) { glLineWidth( width ); }
00222 
00224 void drawLine( const Vec2f &start, const Vec2f &end );
00226 void drawLine( const Vec3f &start, const Vec3f &end );
00228 void drawCube( const Vec3f &center, const Vec3f &size );
00230 void drawColorCube( const Vec3f &center, const Vec3f &size );
00232 void drawStrokedCube( const Vec3f &center, const Vec3f &size );
00234 inline void drawStrokedCube( const AxisAlignedBox3f &aab ) { drawStrokedCube( aab.getCenter(), aab.getSize() ); }
00236 void drawSphere( const Vec3f &center, float radius, int segments = 12 );
00238 void draw( const class Sphere &sphere, int segments = 12 );
00240 void drawSolidCircle( const Vec2f &center, float radius, int numSegments = 0 );
00242 void drawStrokedCircle( const Vec2f &center, float radius, int numSegments = 0 );
00244 void drawSolidEllipse( const Vec2f &center, float radiusX, float radiusY, int numSegments = 0 );
00246 void drawStrokedEllipse( const Vec2f &center, float radiusX, float radiusY, int numSegments = 0 );
00248 void drawSolidRect( const Rectf &rect, bool textureRectangle = false );
00250 void drawStrokedRect( const Rectf &rect );
00251 void drawSolidRoundedRect( const Rectf &r, float cornerRadius, int numSegmentsPerCorner = 0 );
00252 void drawStrokedRoundedRect( const Rectf &r, float cornerRadius, int numSegmentsPerCorner = 0 );
00255 void drawSolidTriangle( const Vec2f &pt1, const Vec2f &pt2, const Vec2f &pt3 );
00256 void drawSolidTriangle( const Vec2f pts[3] );
00258 void drawSolidTriangle( const Vec2f &pt1, const Vec2f &pt2, const Vec2f &pt3, const Vec2f &texPt1, const Vec2f &texPt2, const Vec2f &texPt3 );
00259 void drawSolidTriangle( const Vec2f pts[3], const Vec2f texCoord[3] );
00261 void drawStrokedTriangle( const Vec2f &pt1, const Vec2f &pt2, const Vec2f &pt3 );   
00262 void drawStrokedTriangle( const Vec2f pts[3] );
00263 void drawCoordinateFrame( float axisLength = 1.0f, float headLength = 0.2f, float headRadius = 0.05f );
00265 void drawVector( const Vec3f &start, const Vec3f &end, float headLength = 0.2f, float headRadius = 0.05f );
00267 void drawFrustum( const Camera &cam );
00269 void drawTorus( float outterRadius, float innerRadius, int longitudeSegments = 12, int latitudeSegments = 12 );
00271 void drawCylinder( float baseRadius, float topRadius, float height, int slices = 12, int stacks = 1 );
00273 void draw( const class PolyLine<Vec2f> &polyLine );
00275 void draw( const class PolyLine<Vec3f> &polyLine );
00277 void draw( const class Path2d &path2d, float approximationScale = 1.0f );
00279 void draw( const class Shape2d &shape2d, float approximationScale = 1.0f );
00280 
00282 void drawSolid( const class Path2d &path2d, float approximationScale = 1.0f );
00284 void drawSolid( const class Shape2d &shape2d, float approximationScale = 1.0f );
00286 void drawSolid( const PolyLine2f &polyLine );
00287 
00289 void draw( const TriMesh2d &mesh );
00291 void drawRange( const TriMesh2d &mesh, size_t startTriangle, size_t triangleCount );
00293 void draw( const TriMesh &mesh );
00295 void drawRange( const TriMesh &mesh, size_t startTriangle, size_t triangleCount );
00296 
00297 #if ! defined ( CINDER_GLES )
00298 
00299 void draw( const VboMesh &vbo );
00300 inline void draw( const VboMeshRef &vbo ) { draw( *vbo ); }
00302 void drawRange( const VboMesh &vbo, size_t startIndex, size_t indexCount, int vertexStart = -1, int vertexEnd = -1 );
00303 inline void drawRange( const VboMeshRef &vbo, size_t startIndex, size_t indexCount, int vertexStart = -1, int vertexEnd = -1 ) { drawRange( *vbo, startIndex, indexCount, vertexStart, vertexEnd ); }
00305 void drawArrays( const VboMesh &vbo, GLint first, GLsizei count );
00306 inline void drawArrays( const VboMeshRef &vbo, GLint first, GLsizei count ) { drawArrays( *vbo, first, count ); }
00307 #endif
00308 
00310 void drawBillboard( const Vec3f &pos, const Vec2f &scale, float rotationDegrees, const Vec3f &bbRight, const Vec3f &bbUp );
00312 void draw( const Texture &texture );
00313 inline void draw( const TextureRef &texture ) { draw( *texture ); }
00315 void draw( const Texture &texture, const Vec2f &pos );
00316 inline void draw( const TextureRef &texture, const Vec2f &pos ) { draw( *texture, pos ); }
00318 void draw( const Texture &texture, const Rectf &rect );
00319 inline void draw( const TextureRef &texture, const Rectf &rect ) { draw( *texture, rect ); }
00321 void draw( const Texture &texture, const Area &srcArea, const Rectf &destRect );
00322 inline void draw( const TextureRef &texture, const Area &srcArea, const Rectf &destRect ) { draw( *texture, srcArea, destRect ); }
00323 
00325 void drawString( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
00327 void drawStringCentered( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
00329 void drawStringRight( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
00330 
00331 
00333 struct SaveTextureBindState {
00334     SaveTextureBindState( GLint target );
00335     ~SaveTextureBindState();
00336   private:
00337     GLint   mTarget;
00338     GLint   mOldID;
00339 };
00340 
00342 struct BoolState {
00343     BoolState( GLint target );
00344     ~BoolState();
00345   private:
00346     GLint       mTarget;
00347     GLboolean   mOldValue;
00348 };
00349 
00351 struct ClientBoolState {
00352     ClientBoolState( GLint target );
00353     ~ClientBoolState();
00354   private:
00355     GLint       mTarget;
00356     GLboolean   mOldValue;
00357 };
00358 
00360 struct SaveColorState {
00361     SaveColorState();
00362     ~SaveColorState();
00363   private:
00364     GLfloat     mOldValues[4];
00365 };
00366 
00368 struct SaveFramebufferBinding {
00369     SaveFramebufferBinding();
00370     ~SaveFramebufferBinding();
00371   private:
00372     GLint       mOldValue;
00373 };
00374 
00375 #if defined( CINDER_MSW )
00376 
00377 void initializeGlee();
00378 #endif
00379 
00380 class Exception : public cinder::Exception {
00381 };
00382 
00383 class ExceptionUnknownTarget : public Exception {
00384 };
00385 
00386 } } // namespace cinder::gl 
00387 
00389 
00390 #if ! defined( CINDER_GLES )
00391 inline void glVertex2f( const cinder::Vec2f &v ) { glVertex2f( v.x, v.y ); }
00392 inline void glVertex3f( const cinder::Vec3f &v ) { glVertex3f( v.x, v.y, v.z ); }
00393 inline void glVertex4f( const cinder::Vec4f &v ) { glVertex4f( v.x, v.y, v.z, v.w ); }
00394 inline void glNormal3f( const cinder::Vec3f &v ) { glNormal3f( v.x, v.y, v.z ); }
00395 inline void glColor3f( const cinder::Color &c ) { glColor3f( c.r, c.g, c.b ); }
00396 inline void glColor4f( const cinder::ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); }
00397 inline void glTexCoord2f( const cinder::Vec2f &v ) { glTexCoord2f( v.x, v.y ); }
00398 inline void glTexCoord3f( const cinder::Vec3f &v ) { glTexCoord3f( v.x, v.y, v.z ); }
00399 inline void glTexCoord4f( const cinder::Vec4f &v ) { glTexCoord4f( v.x, v.y, v.z, v.w ); }
00400 // This style of definition conflicts with GLee
00401 //inline void glMultiTexCoord2f( GLenum target, const cinder::Vec2f &v ) { glMultiTexCoord2f( target, v.x, v.y ); }
00402 //inline void glMultiTexCoord3f( GLenum target, const cinder::Vec3f &v ) { glMultiTexCoord3f( target, v.x, v.y, v.z ); }
00403 //inline void glMultiTexCoord4f( GLenum target, const cinder::Vec4f &v ) { glMultiTexCoord4f( target, v.x, v.y, v.z, v.w ); }
00404 #endif // ! defined( CINDER_GLES )
00405 inline void glTranslatef( const cinder::Vec3f &v ) { glTranslatef( v.x, v.y, v.z ); }
00406 inline void glScalef( const cinder::Vec3f &v ) { glScalef( v.x, v.y, v.z ); }
00407 inline void glRotatef( float angle, const cinder::Vec3f &v ) { glRotatef( angle, v.x, v.y, v.z ); }
00408 inline void glRotatef( const cinder::Quatf &quat ) { cinder::Vec3f axis; float angle; quat.getAxisAngle( &axis, &angle ); glRotatef( cinder::toDegrees( angle ), axis.x, axis.y, axis.z ); }
00409 inline void glMultMatrixf( const cinder::Matrix44f &m ) { glMultMatrixf( m.m ); }
00410 inline void glLoadMatrixf( const cinder::Matrix44f &m ) { glLoadMatrixf( m.m ); }