# 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
    00041 #if defined( CINDER_MSW )
    00042     #include <windows.h>
    00043     #undef min
    00044     #undef max
    00045     #include <gl/gl.h>
    00046 #elif defined( CINDER_COCOA_TOUCH )
    00047     #include <OpenGLES/ES1/gl.h>
    00048     #include <OpenGLES/ES1/glext.h>
    00049 #elif defined( CINDER_MAC )
    00050     #include <OpenGL/gl.h>
    00051 #endif
    00052
    00053 // forward declarations
    00054 namespace cinder {
    00055     class Camera; class TriMesh; class Sphere;
    00056     namespace gl {
    00057          class VboMesh; class Texture;
    00058     }
    00059 } // namespace cinder
    00060
    00061 namespace cinder { namespace gl {
    00062
    00064 void clear( const ColorA &color = ColorA::black(), bool clearDepthBuffer = true );
    00065
    00067 void setMatrices( const Camera &cam );
    00069 void setModelView( const Camera &cam );
    00071 void setProjection( const Camera &cam );
    00073 void pushModelView();
    00075 void popModelView();
    00077 void pushModelView( const Camera &cam );
    00079 void pushProjection( const Camera &cam );
    00081 void pushMatrices();
    00083 void popMatrices();
    00085 void multModelView( const Matrix44f &mtx );
    00087 void multProjection( const Matrix44f &mtx );
    00089 Matrix44f getModelView();
    00091 Matrix44f getProjection();
    00092
    00094 void setMatricesWindowPersp( int screenWidth, int screenHeight, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f );
    00096 inline void setMatricesWindowPersp( const Vec2i &screenSize, float fovDegrees = 60.0f, float nearPlane = 1.0f, float farPlane = 1000.0f )
    00097     { setMatricesWindowPersp( screenSize.x, screenSize.y, fovDegrees, nearPlane, farPlane ); }
    00099 void setMatricesWindow( int screenWidth, int screenHeight, bool originUpperLeft = true );
    00101 inline void setMatricesWindow( const Vec2i &screenSize, bool originUpperLeft = true ) { setMatricesWindow( screenSize.x, screenSize.y, originUpperLeft ); }
    00102
    00104 Area getViewport();
    00106 void setViewport( const Area &area );
    00107
    00109 void translate( const Vec2f &pos );
    00111 void translate( const Vec3f &pos );
    00113 void scale( const Vec3f &scale );
    00115 void rotate( const Vec3f &xyz );
    00117 void rotate( const Quatf &quat );
    00119 inline void rotate( float degrees ) { rotate( Vec3f( 0, 0, degrees ) ); }
    00120
    00121 #if ! defined( CINDER_GLES )
    00122
    00123 inline void vertex( const Vec2f &v ) { glVertex2fv( &v.x ); }
    00125 inline void vertex( const Vec3f &v ) { glVertex3fv( &v.x ); }
    00127 inline void color( const Color &c ) { glColor3f( c.r, c.g, c.b ); }
    00129 inline void color( const ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); }
    00130 #endif // ! defined( CINDER_GLES )
    00131
    00133 void enableAlphaBlending( bool premultiplied = false );
    00135 void disableAlphaBlending();
    00137 void enableAdditiveBlending();
    00138
    00141 void enableAlphaTest( float value = 0.5f, int func = GL_GREATER );
    00143 void disableAlphaTest();
    00144
    00145 #if ! defined( CINDER_GLES )
    00146
    00147 void enableWireframe();
    00149 void disableWireframe();
    00150 #endif // ! defined( CINDER_GLES )
    00151
    00153 void disableDepthRead();
    00155 void disableDepthWrite();
    00157 void enableDepthRead( bool enable = true );
    00159 void enableDepthWrite( bool enable = true );
    00160
    00162 void drawCube( const Vec3f &center, const Vec3f &size );
    00164 void drawColorCube( const Vec3f &center, const Vec3f &size );
    00166 void drawSphere( const Vec3f &center, float radius, int segments = 12 );
    00168 void draw( const class Sphere &sphere, int segments = 12 );
    00170 void drawSolidCircle( const Vec2f &center, float radius, int numSegments = 0 );
    00172 void drawSolidRect( const Rectf &rect, bool textureRectangle = false );
    00174 void drawCoordinateFrame( float axisLength = 1.0f, float headLength = 0.2f, float headRadius = 0.05f );
    00176 void drawVector( const Vec3f &start, const Vec3f &end, float headLength = 0.2f, float headRadius = 0.05f );
    00178 void drawFrustum( const Camera &cam );
    00180 void drawTorus( float outterRadius, float innerRadius, int longitudeSegments = 12, int latitudeSegments = 12 );
    00181
    00182 #if ! defined( CINDER_GLES )
    00183
    00184 void draw( const TriMesh &mesh );
    00186 void drawRange( const TriMesh &mesh, size_t startTriangle, size_t triangleCount );
    00188 void draw( const VboMesh &vbo );
    00190 void drawRange( const VboMesh &vbo, size_t startIndex, size_t indexCount, int vertexStart = -1, int vertexEnd = -1 );
    00192 void drawArrays( const VboMesh &vbo, GLint first, GLsizei count );
    00194 #endif // ! defined( CINDER_GLES )
    00195
    00196 void drawBillboard( const Vec3f &pos, const Vec2f &scale, float rotationDegrees, const Vec3f &bbRight, const Vec3f &bbUp );
    00198 void draw( const Texture &texture );
    00200 void draw( const Texture &texture, const Vec2f &pos );
    00202 void draw( const Texture &texture, const Rectf &rect );
    00204 void draw( const Texture &texture, const Area &srcArea, const Rectf &destRect );
    00205
    00206 #if ! defined( CINDER_COCOA_TOUCH )
    00207
    00209 void drawString( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
    00211 void drawStringCentered( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
    00213 void drawStringRight( const std::string &str, const Vec2f &pos, const ColorA &color = ColorA( 1, 1, 1, 1 ), Font font = Font() );
    00214
    00215 #endif // ! defined( CINDER_COCOA_TOUCH )
    00216
    00217
    00219 struct SaveTextureBindState {
    00220     SaveTextureBindState( GLint target );
    00221     ~SaveTextureBindState();
    00222   private:
    00223     GLint   mTarget;
    00224     GLint   mOldID;
    00225 };
    00226
    00228 struct SaveTextureEnabledState {
    00229     SaveTextureEnabledState( GLint target );
    00230     ~SaveTextureEnabledState();
    00231   private:
    00232     GLint       mTarget;
    00233     GLboolean   mOldValue;
    00234 };
    00235
    00237 struct SaveColorState {
    00238     SaveColorState();
    00239     ~SaveColorState();
    00240   private:
    00241     GLfloat     mOldValues[4];
    00242 };
    00243
    00245 void initializeGlee();
    00246
    00247 } } // namespace cinder::gl
    00248
    00250
    00251 #if ! defined( CINDER_GLES )
    00252 inline void glVertex2f( const cinder::Vec2f &v ) { glVertex2f( v.x, v.y ); }
    00253 inline void glVertex3f( const cinder::Vec3f &v ) { glVertex3f( v.x, v.y, v.z ); }
    00254 inline void glVertex4f( const cinder::Vec4f &v ) { glVertex4f( v.x, v.y, v.z, v.w ); }
    00255 inline void glNormal3f( const cinder::Vec3f &v ) { glNormal3f( v.x, v.y, v.z ); }
    00256 inline void glColor3f( const cinder::Color &c ) { glColor3f( c.r, c.g, c.b ); }
    00257 inline void glColor4f( const cinder::ColorA &c ) { glColor4f( c.r, c.g, c.b, c.a ); }
    00258 inline void glTexCoord2f( const cinder::Vec2f &v ) { glTexCoord2f( v.x, v.y ); }
    00259 inline void glTexCoord3f( const cinder::Vec3f &v ) { glTexCoord3f( v.x, v.y, v.z ); }
    00260 inline void glTexCoord4f( const cinder::Vec4f &v ) { glTexCoord4f( v.x, v.y, v.z, v.w ); }
    00261 // This style of definition conflicts with GLee
    00262 //inline void glMultiTexCoord2f( GLenum target, const cinder::Vec2f &v ) { glMultiTexCoord2f( target, v.x, v.y ); }
    00263 //inline void glMultiTexCoord3f( GLenum target, const cinder::Vec3f &v ) { glMultiTexCoord3f( target, v.x, v.y, v.z ); }
    00264 //inline void glMultiTexCoord4f( GLenum target, const cinder::Vec4f &v ) { glMultiTexCoord4f( target, v.x, v.y, v.z, v.w ); }
    00265 #endif // ! defined( CINDER_GLES )
    00266 inline void glTranslatef( const cinder::Vec3f &v ) { glTranslatef( v.x, v.y, v.z ); }
    00267 inline void glScalef( const cinder::Vec3f &v ) { glScalef( v.x, v.y, v.z ); }
    00268 inline void glRotatef( float angle, const cinder::Vec3f &v ) { glRotatef( angle, v.x, v.y, v.z ); }
    00269 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 ); }
    00270 inline void glMultMatrixf( const cinder::Matrix44f &m ) { glMultMatrixf( m.m ); }
    00271 inline void glLoadMatrixf( const cinder::Matrix44f &m ) { glLoadMatrixf( m.m ); }