00001
00002 #ifndef _TP_ATTRIBUTES_H
00003 #define _TP_ATTRIBUTES_H
00004
00005 #include "GL/GLPlatform.h"
00006 #include "GL/TPProgramName.h"
00007
00008 namespace gk {
00009
00010 enum
00011 {
00012 INDEX= 0,
00013
00014 ATTRIBUTE,
00015 ATTRIBUTE0,
00016 ATTRIBUTE1,
00017 ATTRIBUTE2,
00018 ATTRIBUTE3,
00019 ATTRIBUTE4,
00020 ATTRIBUTE5,
00021 ATTRIBUTE6,
00022 ATTRIBUTE7,
00023
00024 FEEDBACK,
00025 FEEDBACK0,
00026 FEEDBACK1,
00027 FEEDBACK2,
00028 FEEDBACK3,
00029
00030 UNIFORM,
00031 UNIFORM0,
00032 UNIFORM1,
00033 UNIFORM2,
00034 UNIFORM3,
00035 UNIFORM4,
00036 UNIFORM5,
00037 UNIFORM6,
00038 UNIFORM7,
00039
00040 INDIRECT,
00041
00042 BUFFER_STATE_LAST
00043 };
00044
00045
00046 class GLBuffer;
00047 class GLAttributeBuffer;
00048 class GLIndexBuffer;
00049
00050 struct BufferLayout
00051 {
00052 int size;
00053 GLenum type;
00054 unsigned long int stride;
00055 unsigned long int offset;
00056 int divisor;
00057 bool normalize_flag;
00058 bool integer_flag;
00059
00060 enum
00061 {
00062 NORMALIZE_BIT= 1,
00063 INTEGER_BIT= 2
00064 };
00065
00066 BufferLayout( )
00067 :
00068 size(0),
00069 type(GL_NONE),
00070 stride(0),
00071 offset(0),
00072 divisor(0),
00073 normalize_flag(false),
00074 integer_flag(false)
00075 {}
00076
00077 BufferLayout( const int _size, const GLenum _type, const unsigned long int _stride= 0, const unsigned long int _offset= 0,
00078 const int _divisor= 0, const unsigned int flags= 0 )
00079 :
00080 size(_size),
00081 type(_type),
00082 stride(_stride),
00083 offset(_offset),
00084 divisor(_divisor),
00085 normalize_flag((flags & NORMALIZE_BIT)),
00086 integer_flag((flags & INTEGER_BIT))
00087 {}
00088
00089 bool operator==( const BufferLayout& b )
00090 {
00091 if(stride != b.stride)
00092 return false;
00093 if(offset != b.offset)
00094 return false;
00095 if(size != b.size || type != b.type)
00096 return false;
00097 if(divisor != b.divisor)
00098 return false;
00099
00100 return true;
00101 }
00102
00103 bool operator!= ( const BufferLayout& b )
00104 {
00105 return !operator==(b);
00106 }
00107 };
00108
00109 class BufferState
00110 {
00111 GLBuffer *m_buffer;
00112 BufferLayout m_layout;
00113 GLenum m_target;
00114 int m_index;
00115 int m_active;
00116
00117 BufferState( const BufferState& );
00118 BufferState( const GLenum target, const int active, const int index= -1)
00119 :
00120 m_buffer(NULL),
00121 m_layout(),
00122 m_target(target),
00123 m_index(index),
00124 m_active(active)
00125 {}
00126
00127 public:
00128 BufferState( )
00129 :
00130 m_buffer(NULL),
00131 m_layout(),
00132 m_index(-1)
00133 {}
00134
00135 ~BufferState( )
00136 {}
00137
00138 BufferState& operator=( const BufferState& b )
00139 {
00140 m_buffer= b.m_buffer;
00141 m_layout= b.m_layout;
00142 m_target= b.m_target;
00143 m_index= b.m_index;
00144 m_active= b.m_active;
00145 return *this;
00146 }
00147
00148 BufferState& setBufferState( const ProgramAttribute& attribute, GLAttributeBuffer *buffer, const BufferLayout& layout );
00149 BufferState& setBufferState( GLIndexBuffer *buffer, const BufferLayout& layout );
00150
00151 BufferState& resetVertexBuffer( );
00152 BufferState& resetIndexBuffer( );
00153
00154 BufferState& reset( );
00155
00156 BufferState& setBuffer( GLBuffer *buffer );
00157
00158 GLBuffer *getBuffer( )
00159 {
00160 return m_buffer;
00161 }
00162
00163 const GLBuffer *getBuffer( ) const
00164 {
00165 return m_buffer;
00166 }
00167
00168 BufferState& setLayout( const BufferLayout& layout );
00169
00170 BufferLayout& getLayout( )
00171 {
00172 return m_layout;
00173 }
00174
00175 const BufferLayout& getLayout( ) const
00176 {
00177 return m_layout;
00178 }
00179
00180 GLenum target( ) const
00181 {
00182 return m_target;
00183 }
00184
00185 int index( ) const
00186 {
00187 return m_index;
00188 }
00189
00190 int active( ) const
00191 {
00192 return m_active;
00193 }
00194
00195 static int init( );
00196 };
00197
00198 extern BufferState Attributes[BUFFER_STATE_LAST];
00199 extern BufferState ActiveAttributes[BUFFER_STATE_LAST];
00200 extern int AttributeCount;
00201 extern bool AttributesDirty;
00202
00203 BufferState& setVertexBuffer( const ProgramAttribute& attribute, GLAttributeBuffer *buffer, const BufferLayout& layout );
00204 BufferState& setVertexBuffer( const ProgramAttribute& attribute, GLAttributeBuffer *buffer,
00205 const int size, const GLenum type, const unsigned int stride= 0, const unsigned int offset= 0, const unsigned int flags= 0 );
00206 BufferState& resetVertexBuffer( const ProgramAttribute& attribute );
00207
00208 BufferState& setInstanceBuffer( const ProgramAttribute& attribute, GLAttributeBuffer *buffer, const BufferLayout& layout );
00209 BufferState& setInstanceBuffer( const ProgramAttribute& attribute, GLAttributeBuffer *buffer,
00210 const int size, const GLenum type, const unsigned int stride= 0, const unsigned int offset= 0, const unsigned int flags= 0, const int divisor= 1 );
00211 BufferState& resetInstanceBuffer( const ProgramAttribute& attribute );
00212
00213
00214 inline
00215 BufferState& setIntegerVertexBuffer( const ProgramAttribute& attribute, GLAttributeBuffer *buffer,
00216 const int size, const GLenum type, const unsigned int stride= 0, const unsigned int offset= 0, const unsigned int flags= 0 )
00217 {
00218 return setVertexBuffer(attribute, buffer, size, type, stride, offset, flags | BufferLayout::INTEGER_BIT);
00219 }
00220
00221
00222 inline
00223 BufferState& setIntegerInstanceBuffer( const ProgramAttribute& attribute, GLAttributeBuffer *buffer,
00224 const int size, const GLenum type, const unsigned int stride= 0, const unsigned int offset= 0, const unsigned int flags= 0, const int divisor= 1 )
00225 {
00226 return setInstanceBuffer(attribute, buffer, size, type, stride, offset, flags | BufferLayout::INTEGER_BIT, divisor);
00227 }
00228
00229 BufferState& setIndexBuffer( GLIndexBuffer *buffer, const BufferLayout& layout );
00230 BufferState& setIndexBuffer( GLIndexBuffer *buffer, const GLenum type= GL_UNSIGNED_INT, const unsigned int offset= 0 );
00231 BufferState& resetIndexBuffer( );
00232
00233
00234 int DrawArrays( const GLenum mode, const int first, const unsigned int count );
00235 int DrawArraysInstanced( const GLenum mode, const int first, const unsigned int count, const unsigned int instance_count= 1 );
00236 int DrawElements( const GLenum mode, const unsigned int count= 0, const int base= 0 );
00237 int DrawElementsInstanced(const GLenum mode, const unsigned int instance_count= 1, const unsigned int count= 0, const int base= 0 );
00238
00239 }
00240
00241 #endif