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 void translate( const Vec3f &pos ); 00121 void scale( const Vec3f &scale ); 00123 void rotate( const Vec3f &xyz ); 00125 void rotate( const Quatf &quat ); 00127 inline void rotate( float degrees ) { rotate( Vec3f( 0, 0, degrees ) ); } 00128 00129 #if ! defined( CINDER_GLES ) 00130 00131 inline void vertex( const Vec2f &v ) { glVertex2fv( &v.x ); } 00133 inline void vertex( const Vec3f &v ) { glVertex3fv( &v.x ); } 00134 #endif // ! defined( CINDER_GLES ) 00135 00136 inline void color( const Color8u &c ) { glColor4ub( c.r, c.g, c.b, 255 ); } 00138 inline void color( const ColorA8u &c ) { glColor4ub( c.r, c.g, c.b, c.a ); } 00140 inline void color( const Color &c ) { glColor4f( c.r, c.g, c.b, 1.0f ); } 00142 inline void color( const ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); } 00143 00145 inline void enable( GLenum state ) { glEnable( state ); } 00147 inline void disable( GLenum state ) { glDisable( state ); } 00148 00150 void enableAlphaBlending( bool premultiplied = false ); 00152 void disableAlphaBlending(); 00154 void enableAdditiveBlending(); 00155 00158 void enableAlphaTest( float value = 0.5f, int func = GL_GREATER ); 00160 void disableAlphaTest(); 00161 00162 #if ! defined( CINDER_GLES ) 00163 00164 void enableWireframe(); 00166 void disableWireframe(); 00167 #endif // ! defined( CINDER_GLES ) 00168 00170 void disableDepthRead(); 00172 void disableDepthWrite(); 00174 void enableDepthRead( bool enable = true ); 00176 void enableDepthWrite( bool enable = true ); 00177 00179 void drawLine( const Vec2f &start, const Vec2f &end ); 00181 void drawLine( const Vec3f &start, const Vec3f &end ); 00183 void drawCube( const Vec3f ¢er, const Vec3f &size ); 00185 void drawColorCube( const Vec3f ¢er, const Vec3f &size ); 00187 void drawStrokedCube( const Vec3f ¢er, const Vec3f &size ); 00189 inline void drawStrokedCube( const AxisAlignedBox3f &aab ) { drawStrokedCube( aab.getCenter(), aab.getSize() ); } 00191 void drawSphere( const Vec3f ¢er, float radius, int segments = 12 ); 00193 void draw( const class Sphere &sphere, int segments = 12 ); 00195 void drawSolidCircle( const Vec2f ¢er, float radius, int numSegments = 0 ); 00197 void drawStrokedCircle( const Vec2f ¢er, float radius, int numSegments = 0 ); 00199 void drawSolidRect( const Rectf &rect, bool textureRectangle = false ); 00201 void drawCoordinateFrame( float axisLength = 1.0f, float headLength = 0.2f, float headRadius = 0.05f ); 00203 void drawVector( const Vec3f &start, const Vec3f &end, float headLength = 0.2f, float headRadius = 0.05f ); 00205 void drawFrustum( const Camera &cam ); 00207 void drawTorus( float outterRadius, float innerRadius, int longitudeSegments = 12, int latitudeSegments = 12 ); 00209 void draw( const class PolyLine<Vec2f> &polyLine ); 00210 00211 #if ! defined( CINDER_GLES ) 00212 00213 void draw( const TriMesh &mesh ); 00215 void drawRange( const TriMesh &mesh, size_t startTriangle, size_t triangleCount ); 00217 void draw( const VboMesh &vbo ); 00219 void drawRange( const VboMesh &vbo, size_t startIndex, size_t indexCount, int vertexStart = -1, int vertexEnd = -1 ); 00221 void drawArrays( const VboMesh &vbo, GLint first, GLsizei count ); 00223 #endif // ! defined( CINDER_GLES ) 00224 00225 void drawBillboard( const Vec3f &pos, const Vec2f &scale, float rotationDegrees, const Vec3f &bbRight, const Vec3f &bbUp ); 00227 void draw( const Texture &texture ); 00229 void draw( const Texture &texture, const Vec2f &pos ); 00231 void draw( const Texture &texture, const Rectf &rect ); 00233 void draw( const Texture &texture, const Area &srcArea, const Rectf &destRect ); 00234 00236 void drawString( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() ); 00238 void drawStringCentered( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() ); 00240 void drawStringRight( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() ); 00241 00242 00244 struct SaveTextureBindState { 00245 SaveTextureBindState( GLint target ); 00246 ~SaveTextureBindState(); 00247 private: 00248 GLint mTarget; 00249 GLint mOldID; 00250 }; 00251 00253 struct SaveTextureEnabledState { 00254 SaveTextureEnabledState( GLint target ); 00255 ~SaveTextureEnabledState(); 00256 private: 00257 GLint mTarget; 00258 GLboolean mOldValue; 00259 }; 00260 00262 struct SaveColorState { 00263 SaveColorState(); 00264 ~SaveColorState(); 00265 private: 00266 GLfloat mOldValues[4]; 00267 }; 00268 00270 struct SaveFramebufferBinding { 00271 SaveFramebufferBinding(); 00272 ~SaveFramebufferBinding(); 00273 private: 00274 GLint mOldValue; 00275 }; 00276 00277 #if defined( CINDER_MSW ) 00278 00279 void initializeGlee(); 00280 #endif 00281 00282 } } // namespace cinder::gl 00283 00285 00286 #if ! defined( CINDER_GLES ) 00287 inline void glVertex2f( const cinder::Vec2f &v ) { glVertex2f( v.x, v.y ); } 00288 inline void glVertex3f( const cinder::Vec3f &v ) { glVertex3f( v.x, v.y, v.z ); } 00289 inline void glVertex4f( const cinder::Vec4f &v ) { glVertex4f( v.x, v.y, v.z, v.w ); } 00290 inline void glNormal3f( const cinder::Vec3f &v ) { glNormal3f( v.x, v.y, v.z ); } 00291 inline void glColor3f( const cinder::Color &c ) { glColor3f( c.r, c.g, c.b ); } 00292 inline void glColor4f( const cinder::ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); } 00293 inline void glTexCoord2f( const cinder::Vec2f &v ) { glTexCoord2f( v.x, v.y ); } 00294 inline void glTexCoord3f( const cinder::Vec3f &v ) { glTexCoord3f( v.x, v.y, v.z ); } 00295 inline void glTexCoord4f( const cinder::Vec4f &v ) { glTexCoord4f( v.x, v.y, v.z, v.w ); } 00296 // This style of definition conflicts with GLee 00297 //inline void glMultiTexCoord2f( GLenum target, const cinder::Vec2f &v ) { glMultiTexCoord2f( target, v.x, v.y ); } 00298 //inline void glMultiTexCoord3f( GLenum target, const cinder::Vec3f &v ) { glMultiTexCoord3f( target, v.x, v.y, v.z ); } 00299 //inline void glMultiTexCoord4f( GLenum target, const cinder::Vec4f &v ) { glMultiTexCoord4f( target, v.x, v.y, v.z, v.w ); } 00300 #endif // ! defined( CINDER_GLES ) 00301 inline void glTranslatef( const cinder::Vec3f &v ) { glTranslatef( v.x, v.y, v.z ); } 00302 inline void glScalef( const cinder::Vec3f &v ) { glScalef( v.x, v.y, v.z ); } 00303 inline void glRotatef( float angle, const cinder::Vec3f &v ) { glRotatef( angle, v.x, v.y, v.z ); } 00304 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 ); } 00305 inline void glMultMatrixf( const cinder::Matrix44f &m ) { glMultMatrixf( m.m ); } 00306 inline void glLoadMatrixf( const cinder::Matrix44f &m ) { glLoadMatrixf( m.m ); }