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/Quaternion.h" 00038 #include "cinder/Matrix.h" 00039 #include "cinder/Vector.h" 00040 #include "cinder/Color.h" 00041 #include "cinder/Rect.h" 00042 #include "cinder/Font.h" 00043 #include "cinder/PolyLine.h" 00044 #include "cinder/AxisAlignedBox.h" 00045 00046 #if defined( CINDER_MSW ) 00047 #include <windows.h> 00048 #undef min 00049 #undef max 00050 #include <gl/gl.h> 00051 #elif defined( CINDER_COCOA_TOUCH ) 00052 #include <OpenGLES/ES1/gl.h> 00053 #include <OpenGLES/ES1/glext.h> 00054 #elif defined( CINDER_MAC ) 00055 #include <OpenGL/gl.h> 00056 #endif 00057 00058 // forward declarations 00059 namespace cinder { 00060 class Camera; class TriMesh; class Sphere; 00061 namespace gl { 00062 class VboMesh; class Texture; 00063 } 00064 } // namespace cinder 00065 00066 namespace cinder { namespace gl { 00067 00069 bool isExtensionAvailable( const std::string &extName ); 00070 00072 void clear( const ColorA &color = ColorA::black(), bool clearDepthBuffer = true ); 00073 00075 void setMatrices( const Camera &cam ); 00077 void setModelView( const Camera &cam ); 00079 void setProjection( const Camera &cam ); 00081 void pushModelView(); 00083 void popModelView(); 00085 void pushModelView( const Camera &cam ); 00087 void pushProjection( const Camera &cam ); 00089 void pushMatrices(); 00091 void popMatrices(); 00093 void multModelView( const Matrix44f &mtx ); 00095 void multProjection( const Matrix44f &mtx ); 00097 Matrix44f getModelView(); 00099 Matrix44f getProjection(); 00100 00102 void setMatricesWindowPersp( int screenWidth, int screenHeight, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f, bool originUpperLeft = true ); 00104 inline void setMatricesWindowPersp( const Vec2i &screenSize, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f, bool originUpperLeft = true ) 00105 { setMatricesWindowPersp( screenSize.x, screenSize.y, fovDegrees, nearPlane, farPlane ); } 00107 void setMatricesWindow( int screenWidth, int screenHeight, bool originUpperLeft = true ); 00109 inline void setMatricesWindow( const Vec2i &screenSize, bool originUpperLeft = true ) { setMatricesWindow( screenSize.x, screenSize.y, originUpperLeft ); } 00110 00112 Area getViewport(); 00114 void setViewport( const Area &area ); 00115 00117 void translate( const Vec2f &pos ); 00119 inline void translate( float x, float y ) { translate( Vec2f( x, y ) ); } 00121 void translate( const Vec3f &pos ); 00123 inline void translate( float x, float y, float z ) { translate( Vec3f( x, y, z ) ); } 00124 00126 void scale( const Vec3f &scl ); 00128 inline void scale( const Vec2f &scl ) { scale( Vec3f( scl.x, scl.y, 0 ) ); } 00130 inline void scale( float x, float y ) { scale( Vec3f( x, y, 0 ) ); } 00132 inline void scale( float x, float y, float z ) { scale( Vec3f( x, y, z ) ); } 00133 00135 void rotate( const Vec3f &xyz ); 00137 void rotate( const Quatf &quat ); 00139 inline void rotate( float degrees ) { rotate( Vec3f( 0, 0, degrees ) ); } 00140 00141 #if ! defined( CINDER_GLES ) 00142 00143 inline void vertex( const Vec2f &v ) { glVertex2fv( &v.x ); } 00145 inline void vertex( float x, float y ) { glVertex2f( x, y ); } 00147 inline void vertex( const Vec3f &v ) { glVertex3fv( &v.x ); } 00149 inline void vertex( float x, float y, float z ) { glVertex3f( x, y, z ); } 00150 #endif // ! defined( CINDER_GLES ) 00151 00152 inline void color( float r, float g, float b ) { glColor4f( r, g, b, 1.0f ); } 00154 inline void color( float r, float g, float b, float a ) { glColor4f( r, g, b, a ); } 00156 inline void color( const Color8u &c ) { glColor4ub( c.r, c.g, c.b, 255 ); } 00158 inline void color( const ColorA8u &c ) { glColor4ub( c.r, c.g, c.b, c.a ); } 00160 inline void color( const Color &c ) { glColor4f( c.r, c.g, c.b, 1.0f ); } 00162 inline void color( const ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); } 00163 00165 inline void enable( GLenum state ) { glEnable( state ); } 00167 inline void disable( GLenum state ) { glDisable( state ); } 00168 00170 void enableAlphaBlending( bool premultiplied = false ); 00172 void disableAlphaBlending(); 00174 void enableAdditiveBlending(); 00175 00178 void enableAlphaTest( float value = 0.5f, int func = GL_GREATER ); 00180 void disableAlphaTest(); 00181 00182 #if ! defined( CINDER_GLES ) 00183 00184 void enableWireframe(); 00186 void disableWireframe(); 00187 #endif // ! defined( CINDER_GLES ) 00188 00190 void disableDepthRead(); 00192 void disableDepthWrite(); 00194 void enableDepthRead( bool enable = true ); 00196 void enableDepthWrite( bool enable = true ); 00197 00199 void drawLine( const Vec2f &start, const Vec2f &end ); 00201 void drawLine( const Vec3f &start, const Vec3f &end ); 00203 void drawCube( const Vec3f ¢er, const Vec3f &size ); 00205 void drawColorCube( const Vec3f ¢er, const Vec3f &size ); 00207 void drawStrokedCube( const Vec3f ¢er, const Vec3f &size ); 00209 inline void drawStrokedCube( const AxisAlignedBox3f &aab ) { drawStrokedCube( aab.getCenter(), aab.getSize() ); } 00211 void drawSphere( const Vec3f ¢er, float radius, int segments = 12 ); 00213 void draw( const class Sphere &sphere, int segments = 12 ); 00215 void drawSolidCircle( const Vec2f ¢er, float radius, int numSegments = 0 ); 00217 void drawStrokedCircle( const Vec2f ¢er, float radius, int numSegments = 0 ); 00219 void drawSolidRect( const Rectf &rect, bool textureRectangle = false ); 00221 void drawStrokedRect( const Rectf &rect ); 00223 void drawCoordinateFrame( float axisLength = 1.0f, float headLength = 0.2f, float headRadius = 0.05f ); 00225 void drawVector( const Vec3f &start, const Vec3f &end, float headLength = 0.2f, float headRadius = 0.05f ); 00227 void drawFrustum( const Camera &cam ); 00229 void drawTorus( float outterRadius, float innerRadius, int longitudeSegments = 12, int latitudeSegments = 12 ); 00231 void drawCylinder( float baseRadius, float topRadius, float height, int slices = 12, int stacks = 1 ); 00233 void draw( const class PolyLine<Vec2f> &polyLine ); 00235 void draw( const class PolyLine<Vec3f> &polyLine ); 00237 void draw( const class Path2d &path2d, float approximationScale = 1.0f ); 00239 void draw( const class Shape2d &shape2d, float approximationScale = 1.0f ); 00240 00241 #if ! defined( CINDER_GLES ) 00242 00243 void drawSolid( const class Path2d &path2d, float approximationScale = 1.0f ); 00244 00246 void draw( const TriMesh &mesh ); 00248 void drawRange( const TriMesh &mesh, size_t startTriangle, size_t triangleCount ); 00250 void draw( const VboMesh &vbo ); 00252 void drawRange( const VboMesh &vbo, size_t startIndex, size_t indexCount, int vertexStart = -1, int vertexEnd = -1 ); 00254 void drawArrays( const VboMesh &vbo, GLint first, GLsizei count ); 00256 #endif // ! defined( CINDER_GLES ) 00257 00258 void drawBillboard( const Vec3f &pos, const Vec2f &scale, float rotationDegrees, const Vec3f &bbRight, const Vec3f &bbUp ); 00260 void draw( const Texture &texture ); 00262 void draw( const Texture &texture, const Vec2f &pos ); 00264 void draw( const Texture &texture, const Rectf &rect ); 00266 void draw( const Texture &texture, const Area &srcArea, const Rectf &destRect ); 00267 00269 void drawString( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() ); 00271 void drawStringCentered( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() ); 00273 void drawStringRight( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() ); 00274 00275 00277 struct SaveTextureBindState { 00278 SaveTextureBindState( GLint target ); 00279 ~SaveTextureBindState(); 00280 private: 00281 GLint mTarget; 00282 GLint mOldID; 00283 }; 00284 00286 struct BoolState { 00287 BoolState( GLint target ); 00288 ~BoolState(); 00289 private: 00290 GLint mTarget; 00291 GLboolean mOldValue; 00292 }; 00293 00295 struct ClientBoolState { 00296 ClientBoolState( GLint target ); 00297 ~ClientBoolState(); 00298 private: 00299 GLint mTarget; 00300 GLboolean mOldValue; 00301 }; 00302 00304 struct SaveColorState { 00305 SaveColorState(); 00306 ~SaveColorState(); 00307 private: 00308 GLfloat mOldValues[4]; 00309 }; 00310 00312 struct SaveFramebufferBinding { 00313 SaveFramebufferBinding(); 00314 ~SaveFramebufferBinding(); 00315 private: 00316 GLint mOldValue; 00317 }; 00318 00319 #if defined( CINDER_MSW ) 00320 00321 void initializeGlee(); 00322 #endif 00323 00324 } } // namespace cinder::gl 00325 00327 00328 #if ! defined( CINDER_GLES ) 00329 inline void glVertex2f( const cinder::Vec2f &v ) { glVertex2f( v.x, v.y ); } 00330 inline void glVertex3f( const cinder::Vec3f &v ) { glVertex3f( v.x, v.y, v.z ); } 00331 inline void glVertex4f( const cinder::Vec4f &v ) { glVertex4f( v.x, v.y, v.z, v.w ); } 00332 inline void glNormal3f( const cinder::Vec3f &v ) { glNormal3f( v.x, v.y, v.z ); } 00333 inline void glColor3f( const cinder::Color &c ) { glColor3f( c.r, c.g, c.b ); } 00334 inline void glColor4f( const cinder::ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); } 00335 inline void glTexCoord2f( const cinder::Vec2f &v ) { glTexCoord2f( v.x, v.y ); } 00336 inline void glTexCoord3f( const cinder::Vec3f &v ) { glTexCoord3f( v.x, v.y, v.z ); } 00337 inline void glTexCoord4f( const cinder::Vec4f &v ) { glTexCoord4f( v.x, v.y, v.z, v.w ); } 00338 // This style of definition conflicts with GLee 00339 //inline void glMultiTexCoord2f( GLenum target, const cinder::Vec2f &v ) { glMultiTexCoord2f( target, v.x, v.y ); } 00340 //inline void glMultiTexCoord3f( GLenum target, const cinder::Vec3f &v ) { glMultiTexCoord3f( target, v.x, v.y, v.z ); } 00341 //inline void glMultiTexCoord4f( GLenum target, const cinder::Vec4f &v ) { glMultiTexCoord4f( target, v.x, v.y, v.z, v.w ); } 00342 #endif // ! defined( CINDER_GLES ) 00343 inline void glTranslatef( const cinder::Vec3f &v ) { glTranslatef( v.x, v.y, v.z ); } 00344 inline void glScalef( const cinder::Vec3f &v ) { glScalef( v.x, v.y, v.z ); } 00345 inline void glRotatef( float angle, const cinder::Vec3f &v ) { glRotatef( angle, v.x, v.y, v.z ); } 00346 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 ); } 00347 inline void glMultMatrixf( const cinder::Matrix44f &m ) { glMultMatrixf( m.m ); } 00348 inline void glLoadMatrixf( const cinder::Matrix44f &m ) { glLoadMatrixf( m.m ); }