Cinder

  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

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_COCOA_TOUCH )
00028     #include "cinder/gl/GLee.h"
00029 #else
00030     #define CINDER_GLES
00031     #define CINDER_GLES1
00032 #endif
00033 
00034 #include "cinder/Quaternion.h"
00035 #include "cinder/Matrix.h"
00036 #include "cinder/Vector.h"
00037 #include "cinder/Color.h"
00038 #include "cinder/Rect.h"
00039 #include "cinder/Font.h"
00040 #include "cinder/PolyLine.h"
00041 
00042 #if defined( CINDER_MSW )
00043     #include <windows.h>
00044     #undef min
00045     #undef max
00046     #include <gl/gl.h>
00047 #elif defined( CINDER_COCOA_TOUCH )
00048     #include <OpenGLES/ES1/gl.h>
00049     #include <OpenGLES/ES1/glext.h>
00050 #elif defined( CINDER_MAC )
00051     #include <OpenGL/gl.h>
00052 #endif
00053 
00054 // forward declarations
00055 namespace cinder {
00056     class Camera; class TriMesh; class Sphere;
00057     namespace gl {
00058          class VboMesh; class Texture;
00059     }
00060 } // namespace cinder
00061 
00062 namespace cinder { namespace gl {
00063 
00065 void clear( const ColorA &color = ColorA::black(), bool clearDepthBuffer = true );
00066 
00068 void setMatrices( const Camera &cam );
00070 void setModelView( const Camera &cam );
00072 void setProjection( const Camera &cam );
00074 void pushModelView();
00076 void popModelView();
00078 void pushModelView( const Camera &cam );
00080 void pushProjection( const Camera &cam );
00082 void pushMatrices();
00084 void popMatrices();
00086 void multModelView( const Matrix44f &mtx );
00088 void multProjection( const Matrix44f &mtx );
00090 Matrix44f getModelView();
00092 Matrix44f getProjection();
00093 
00095 void setMatricesWindowPersp( int screenWidth, int screenHeight, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f, bool originUpperLeft = true );
00097 inline void setMatricesWindowPersp( const Vec2i &screenSize, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f, bool originUpperLeft = true )
00098     { setMatricesWindowPersp( screenSize.x, screenSize.y, fovDegrees, nearPlane, farPlane ); }
00100 void setMatricesWindow( int screenWidth, int screenHeight, bool originUpperLeft = true );
00102 inline void setMatricesWindow( const Vec2i &screenSize, bool originUpperLeft = true ) { setMatricesWindow( screenSize.x, screenSize.y, originUpperLeft ); }
00103 
00105 Area getViewport();
00107 void setViewport( const Area &area );
00108 
00110 void translate( const Vec2f &pos );
00112 void translate( const Vec3f &pos );
00114 void scale( const Vec3f &scale );
00116 void rotate( const Vec3f &xyz );
00118 void rotate( const Quatf &quat );
00120 inline void rotate( float degrees ) { rotate( Vec3f( 0, 0, degrees ) ); }
00121 
00122 #if ! defined( CINDER_GLES )
00123 
00124 inline void vertex( const Vec2f &v ) { glVertex2fv( &v.x ); }
00126 inline void vertex( const Vec3f &v ) { glVertex3fv( &v.x ); }
00127 #endif // ! defined( CINDER_GLES )
00128 
00129 inline void color( const Color8u &c ) { glColor4ub( c.r, c.g, c.b, 255 ); }
00131 inline void color( const ColorA8u &c ) { glColor4ub( c.r, c.g, c.b, c.a ); }
00133 inline void color( const Color &c ) { glColor4f( c.r, c.g, c.b, 1.0f ); }
00135 inline void color( const ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); }
00136 
00137 
00139 void enableAlphaBlending( bool premultiplied = false );
00141 void disableAlphaBlending();
00143 void enableAdditiveBlending();
00144 
00147 void enableAlphaTest( float value = 0.5f, int func = GL_GREATER );
00149 void disableAlphaTest();
00150 
00151 #if ! defined( CINDER_GLES )
00152 
00153 void enableWireframe();
00155 void disableWireframe();
00156 #endif // ! defined( CINDER_GLES )
00157 
00159 void disableDepthRead();
00161 void disableDepthWrite();
00163 void enableDepthRead( bool enable = true );
00165 void enableDepthWrite( bool enable = true );
00166 
00168 void drawLine( const Vec2f &start, const Vec2f &end );
00170 void drawLine( const Vec3f &start, const Vec3f &end );
00172 void drawCube( const Vec3f &center, const Vec3f &size );
00174 void drawColorCube( const Vec3f &center, const Vec3f &size );
00176 void drawSphere( const Vec3f &center, float radius, int segments = 12 );
00178 void draw( const class Sphere &sphere, int segments = 12 );
00180 void drawSolidCircle( const Vec2f &center, float radius, int numSegments = 0 );
00182 void drawStrokedCircle( const Vec2f &center, float radius, int numSegments = 0 );
00184 void drawSolidRect( const Rectf &rect, bool textureRectangle = false );
00186 void drawCoordinateFrame( float axisLength = 1.0f, float headLength = 0.2f, float headRadius = 0.05f );
00188 void drawVector( const Vec3f &start, const Vec3f &end, float headLength = 0.2f, float headRadius = 0.05f );
00190 void drawFrustum( const Camera &cam );
00192 void drawTorus( float outterRadius, float innerRadius, int longitudeSegments = 12, int latitudeSegments = 12 );
00194 void draw( const class PolyLine<Vec2f> &polyLine );
00195 
00196 #if ! defined( CINDER_GLES )
00197 
00198 void draw( const TriMesh &mesh );
00200 void drawRange( const TriMesh &mesh, size_t startTriangle, size_t triangleCount );
00202 void draw( const VboMesh &vbo );
00204 void drawRange( const VboMesh &vbo, size_t startIndex, size_t indexCount, int vertexStart = -1, int vertexEnd = -1 );
00206 void drawArrays( const VboMesh &vbo, GLint first, GLsizei count );
00208 #endif // ! defined( CINDER_GLES )
00209 
00210 void drawBillboard( const Vec3f &pos, const Vec2f &scale, float rotationDegrees, const Vec3f &bbRight, const Vec3f &bbUp );
00212 void draw( const Texture &texture );
00214 void draw( const Texture &texture, const Vec2f &pos );
00216 void draw( const Texture &texture, const Rectf &rect );
00218 void draw( const Texture &texture, const Area &srcArea, const Rectf &destRect );
00219 
00221 void drawString( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
00223 void drawStringCentered( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
00225 void drawStringRight( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
00226 
00227 
00229 struct SaveTextureBindState {
00230     SaveTextureBindState( GLint target );
00231     ~SaveTextureBindState();
00232   private:
00233     GLint   mTarget;
00234     GLint   mOldID;
00235 };
00236 
00238 struct SaveTextureEnabledState {
00239     SaveTextureEnabledState( GLint target );
00240     ~SaveTextureEnabledState();
00241   private:
00242     GLint       mTarget;
00243     GLboolean   mOldValue;
00244 };
00245 
00247 struct SaveColorState {
00248     SaveColorState();
00249     ~SaveColorState();
00250   private:
00251     GLfloat     mOldValues[4];
00252 };
00253 
00255 void initializeGlee();
00256 
00257 } } // namespace cinder::gl 
00258 
00260 
00261 #if ! defined( CINDER_GLES )
00262 inline void glVertex2f( const cinder::Vec2f &v ) { glVertex2f( v.x, v.y ); }
00263 inline void glVertex3f( const cinder::Vec3f &v ) { glVertex3f( v.x, v.y, v.z ); }
00264 inline void glVertex4f( const cinder::Vec4f &v ) { glVertex4f( v.x, v.y, v.z, v.w ); }
00265 inline void glNormal3f( const cinder::Vec3f &v ) { glNormal3f( v.x, v.y, v.z ); }
00266 inline void glColor3f( const cinder::Color &c ) { glColor3f( c.r, c.g, c.b ); }
00267 inline void glColor4f( const cinder::ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); }
00268 inline void glTexCoord2f( const cinder::Vec2f &v ) { glTexCoord2f( v.x, v.y ); }
00269 inline void glTexCoord3f( const cinder::Vec3f &v ) { glTexCoord3f( v.x, v.y, v.z ); }
00270 inline void glTexCoord4f( const cinder::Vec4f &v ) { glTexCoord4f( v.x, v.y, v.z, v.w ); }
00271 // This style of definition conflicts with GLee
00272 //inline void glMultiTexCoord2f( GLenum target, const cinder::Vec2f &v ) { glMultiTexCoord2f( target, v.x, v.y ); }
00273 //inline void glMultiTexCoord3f( GLenum target, const cinder::Vec3f &v ) { glMultiTexCoord3f( target, v.x, v.y, v.z ); }
00274 //inline void glMultiTexCoord4f( GLenum target, const cinder::Vec4f &v ) { glMultiTexCoord4f( target, v.x, v.y, v.z, v.w ); }
00275 #endif // ! defined( CINDER_GLES )
00276 inline void glTranslatef( const cinder::Vec3f &v ) { glTranslatef( v.x, v.y, v.z ); }
00277 inline void glScalef( const cinder::Vec3f &v ) { glScalef( v.x, v.y, v.z ); }
00278 inline void glRotatef( float angle, const cinder::Vec3f &v ) { glRotatef( angle, v.x, v.y, v.z ); }
00279 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 ); }
00280 inline void glMultMatrixf( const cinder::Matrix44f &m ) { glMultMatrixf( m.m ); }
00281 inline void glLoadMatrixf( const cinder::Matrix44f &m ) { glLoadMatrixf( m.m ); }