00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "mesh.h"
00024 #include "materialinstance.h"
00025
00026 #include <QtGui/QMatrix4x4>
00027 #include <QtGui/QColor>
00028
00029 #include "math.h"
00030 #include "glheaders.h"
00031
00032 using namespace GluonGraphics;
00033
00034 class Mesh::MeshPrivate
00035 {
00036 public:
00037 MeshPrivate()
00038 {
00039 buffer = 0;
00040 vertexLoc = -1;
00041 colorLoc = -1;
00042 uvLoc = -1;
00043 }
00044
00045 MaterialInstance* material;
00046
00047 GLuint buffer;
00048 int colorOffset;
00049 int uvOffset;
00050
00051 int vertexLoc;
00052 int colorLoc;
00053 int uvLoc;
00054 };
00055
00056 Mesh::Mesh( QObject* parent )
00057 : QObject( parent ),
00058 d( new MeshPrivate )
00059 {
00060
00061 }
00062
00063 Mesh::~Mesh()
00064 {
00065 delete d;
00066 }
00067
00068 void
00069 Mesh::load( const QString& filename )
00070 {
00071 if( isLoaded() )
00072 return;
00073
00074 #ifdef __GNUC__
00075 #warning Todo: Move vertex buffer related stuff to a VertexBuffer class.
00076 #endif
00077
00078 QVector<float> vertices;
00079 vertices << -1.f << -1.f << 0.f;
00080 vertices << -1.f << 1.f << 0.f;
00081 vertices << 1.f << 1.f << 0.f;
00082
00083 vertices << -1.f << -1.f << 0.f;
00084 vertices << 1.f << 1.f << 0.f;
00085 vertices << 1.f << -1.f << 0.f;
00086
00087 QVector<float> colors;
00088 colors << 1.f << 1.f << 1.f << 1.f;
00089 colors << 1.f << 1.f << 1.f << 1.f;
00090 colors << 1.f << 1.f << 1.f << 1.f;
00091
00092 colors << 1.f << 1.f << 1.f << 1.f;
00093 colors << 1.f << 1.f << 1.f << 1.f;
00094 colors << 1.f << 1.f << 1.f << 1.f;
00095
00096 QVector<float> uvs;
00097 uvs << 0.f << 0.f;
00098 uvs << 0.f << 1.f;
00099 uvs << 1.f << 1.f;
00100
00101 uvs << 0.f << 0.f;
00102 uvs << 1.f << 1.f;
00103 uvs << 1.f << 0.f;
00104
00105 createBuffer( vertices, colors, uvs );
00106 }
00107
00108 void
00109 Mesh::render( MaterialInstance* material )
00110 {
00111 renderBuffer( GL_TRIANGLES, 6, material );
00112 }
00113
00114 bool
00115 Mesh::isLoaded()
00116 {
00117 return d->buffer != 0;
00118 }
00119
00120 void
00121 Mesh::createBuffer( const QVector<float>& vertices, const QVector<float>& colors, const QVector<float>& uvs )
00122 {
00123 glGenBuffers( 1, &d->buffer );
00124 glBindBuffer( GL_ARRAY_BUFFER, d->buffer );
00125 glBufferData( GL_ARRAY_BUFFER, ( vertices.size() * 4 ) + ( colors.size() * 4 ) + ( uvs.size() * 4 ), 0, GL_STATIC_DRAW );
00126 glBufferSubData( GL_ARRAY_BUFFER, 0, vertices.size() * 4, vertices.data() );
00127 d->colorOffset = vertices.size() * 4;
00128 glBufferSubData( GL_ARRAY_BUFFER, d->colorOffset, colors.size() * 4, colors.data() );
00129 d->uvOffset = d->colorOffset + colors.size() * 4;
00130 glBufferSubData( GL_ARRAY_BUFFER, d->uvOffset, uvs.size() * 4, uvs.data() );
00131 glBindBuffer( GL_ARRAY_BUFFER, 0 );
00132 }
00133
00134 void
00135 Mesh::renderBuffer( uint mode, int count, MaterialInstance* material )
00136 {
00137 if( d->buffer == 0 )
00138 return;
00139
00140 if( d->vertexLoc == -1 )
00141 d->vertexLoc = material->attributeLocation( "vertex" );
00142
00143 if( d->colorLoc == -1 )
00144 d->colorLoc = material->attributeLocation( "color" );
00145
00146 if( d->uvLoc == -1 )
00147 d->uvLoc = material->attributeLocation( "uv0" );
00148
00149 glBindBuffer( GL_ARRAY_BUFFER, d->buffer );
00150 glVertexAttribPointer( d->vertexLoc, 3, GL_FLOAT, 0, 0, 0 );
00151 glVertexAttribPointer( d->colorLoc, 4, GL_FLOAT, 0, 0, ( void* )( d->colorOffset ) );
00152 glVertexAttribPointer( d->uvLoc, 2, GL_FLOAT, 0, 0, ( void* )( d->uvOffset ) );
00153
00154 glEnableVertexAttribArray( d->vertexLoc );
00155 glEnableVertexAttribArray( d->colorLoc );
00156 glEnableVertexAttribArray( d->uvLoc );
00157
00158 glDrawArrays( mode, 0, count );
00159
00160 glDisableVertexAttribArray( d->vertexLoc );
00161 glDisableVertexAttribArray( d->colorLoc );
00162 glDisableVertexAttribArray( d->uvLoc );
00163 glBindBuffer( GL_ARRAY_BUFFER, 0 );
00164 }
00165
00166 #include "mesh.moc"