Commit 61a4a645 authored by Frédéric Larue's avatar Frédéric Larue

GPU library updated.

parent 5bdf8425
......@@ -420,48 +420,6 @@ void GPU::Shader::AttribNames( std::vector<std::string> &names ) const
bool GPU::CreateShaderFromSources( GPU::Shader &shader,
const char *vpgSrc,
const char *fpgSrc,
std::string *logs )
{
shader.Release();
if( vpgSrc )
{
GPU::Shader::VertPg vpg;
if( !vpg.CompileSrcString( vpgSrc, logs ) )
{
if( logs )
*logs = std::string(__FUNCTION__) + " - Vertex program compilation error:\n" + *logs + '\n';
return false;
}
shader.Attach( vpg );
}
if( fpgSrc )
{
GPU::Shader::FragPg fpg;
if( !fpg.CompileSrcString( fpgSrc, logs ) )
{
if( logs )
*logs = std::string(__FUNCTION__) + " - Fragment program compilation error:\n" + *logs + '\n';
return false;
}
shader.Attach( fpg );
}
if( !shader.Link( logs ) )
{
if( logs )
*logs = std::string(__FUNCTION__) + " - Shader link error:\n" + *logs + '\n';
return false;
}
return true;
}
bool GPU::CreateShaderFromSources( GPU::Shader &shader,
const char *vpgSrc,
const char *gpgSrc,
......@@ -470,7 +428,7 @@ bool GPU::CreateShaderFromSources( GPU::Shader &shader,
{
shader.Release();
if( vpgSrc )
if( vpgSrc && vpgSrc[0] != '\0' )
{
GPU::Shader::VertPg vpg;
if( !vpg.CompileSrcString( vpgSrc, logs ) )
......@@ -482,7 +440,7 @@ bool GPU::CreateShaderFromSources( GPU::Shader &shader,
shader.Attach( vpg );
}
if( gpgSrc )
if( gpgSrc && gpgSrc[0] != '\0' )
{
GPU::Shader::GeomPg gpg;
if( !gpg.CompileSrcString( gpgSrc, logs ) )
......@@ -494,7 +452,7 @@ bool GPU::CreateShaderFromSources( GPU::Shader &shader,
shader.Attach( gpg );
}
if( fpgSrc )
if( fpgSrc && fpgSrc[0] != '\0' )
{
GPU::Shader::FragPg fpg;
if( !fpg.CompileSrcString( fpgSrc, logs ) )
......
......@@ -98,10 +98,6 @@ namespace GPU
class GeomPg : public PgObject
{
GLint m_InputType;
GLint m_OutputType;
GLint m_NVertMax;
public:
inline bool CompileSrcFile( const std::string &filename,
std::string *logs = NULL ) { return PgObject::CompileSrcFile( filename, GL_GEOMETRY_SHADER_EXT, logs ); }
......@@ -255,31 +251,27 @@ namespace GPU
};
bool CreateShaderFromSources( GPU::Shader &shader,
const char *vpgSrc,
const char *fpgSrc,
std::string *logs = NULL );
bool CreateShaderFromSources( GPU::Shader &shader,
const char *vpgSrc,
const char *gpgSrc,
const char *fpgSrc,
std::string *logs = NULL );
bool CreateShaderFromSources( GPU::Shader &shader,
const char *vpgSrc,
const char *gpgSrc,
const char *fpgSrc,
std::string *logs = NULL );
inline bool CreateShaderFromSources( GPU::Shader &shader,
const char *vpgSrc,
const char *fpgSrc,
std::string *logs = NULL ) { return CreateShaderFromSources( shader, vpgSrc, NULL, fpgSrc, logs ); }
inline bool CreateShaderFromSources( GPU::Shader &shader,
const std::string &vpgSrc,
const std::string &fpgSrc,
std::string *logs = NULL )
{
return CreateShaderFromSources( shader, vpgSrc.c_str(), fpgSrc.c_str(), logs );
}
std::string *logs = NULL ) { return CreateShaderFromSources( shader, vpgSrc.c_str(), fpgSrc.c_str(), logs ); }
inline bool CreateShaderFromSources( GPU::Shader &shader,
const std::string &vpgSrc,
const std::string &gpgSrc,
const std::string &fpgSrc,
std::string *logs = NULL )
{
return CreateShaderFromSources( shader, vpgSrc.c_str(), gpgSrc.c_str(), fpgSrc.c_str(), logs );
}
std::string *logs = NULL ) { return CreateShaderFromSources( shader, vpgSrc.c_str(), gpgSrc.c_str(), fpgSrc.c_str(), logs ); }
};
......
......@@ -169,21 +169,20 @@ namespace GPU
};
class VBO : public InstantiatedObject
class VBO
{
using AttribMap = std::map< std::string, VBOAttribBuffer >;
using AttribBindingMap = std::map< VBOAttribBuffer*, GLint >;
protected:
GLint m_PrimitiveType;
AttribMap m_Attrib;
VBOIndexBuffer *m_Indices;
Shader *m_Shader;
AttribBindingMap m_AttribBinding;
inline bool Allocate() { return true; }
inline bool Unallocate() { m_Attrib.clear(); RemoveIndices(); return true; }
inline void Bind()
{
if( m_Shader )
......@@ -211,8 +210,21 @@ namespace GPU
}
public:
inline VBO() : InstantiatedObject(), m_Indices(NULL), m_Shader(NULL) { Instantiate(); }
inline ~VBO() { Release(); }
inline VBO() :
m_PrimitiveType( GL_NONE ),
m_Indices( NULL ),
m_Shader( NULL ) {}
inline ~VBO() { Release(); }
inline void Release()
{
m_Attrib.clear();
RemoveIndices();
SetShader( NULL );
}
inline void SetPrimitiveType( GLint ptype ) { m_PrimitiveType = ptype; }
inline GLint GetPrimitiveType() const { return m_PrimitiveType; }
inline VBOAttribBuffer* AddAttrib( const std::string &name, GLint dimension, GLenum type, bool normalized = false )
{
......@@ -232,11 +244,23 @@ namespace GPU
if( foundAttrib != m_Attrib.end() )
m_Attrib.erase( foundAttrib );
}
inline void RemoveAllAttrib()
{
m_Attrib.clear();
}
inline VBOAttribBuffer* Attrib( const std::string &name )
{
auto foundAttrib = m_Attrib.find(name);
return foundAttrib!=m_Attrib.end()? &foundAttrib->second : NULL;
}
inline bool HasAttrib() const
{
return !m_Attrib.empty();
}
inline bool HasAttrib( const std::string &name ) const
{
return m_Attrib.find(name) != m_Attrib.end();
}
inline void EnableAllAttributes()
{
for( auto &a : m_Attrib )
......@@ -300,6 +324,8 @@ namespace GPU
m_AttribBinding.clear();
}
inline void DrawArrays() { if( m_PrimitiveType != GL_NONE ) DrawArrays( m_PrimitiveType, 0, m_Attrib.begin()->second.BufferSize() ); }
inline void DrawArrays( GLenum mode ) { DrawArrays( mode, 0, m_Attrib.begin()->second.BufferSize() ); }
inline void DrawArrays( GLenum mode, GLint first, GLsizei count )
{
Bind();
......@@ -307,13 +333,14 @@ namespace GPU
Unbind();
}
inline void DrawArrays( GLenum mode )
inline void DrawElements() { if( m_PrimitiveType != GL_NONE ) DrawElements( (GLenum) m_PrimitiveType ); }
inline void DrawElements( GLenum mode )
{
assert( m_Indices );
Bind();
glDrawArrays( mode, 0, m_Attrib.begin()->second.BufferSize() );
m_Indices->DrawElements( mode );
Unbind();
}
inline void DrawElements( GLenum mode, GLint first, GLsizei count )
{
assert( m_Indices );
......@@ -322,12 +349,160 @@ namespace GPU
Unbind();
}
inline void DrawElements( GLenum mode )
inline void Draw()
{
assert( m_Indices );
Bind();
m_Indices->DrawElements( mode );
Unbind();
if( Indices() )
DrawElements();
else
DrawArrays();
}
};
class VBOBuilder
{
struct Vec3
{
float v[3];
inline Vec3() {}
inline Vec3( float x, float y, float z )
{
v[0] = x;
v[1] = y;
v[2] = z;
}
};
struct Vec4
{
float v[4];
inline Vec4() {}
inline Vec4( float x, float y, float z, float w )
{
v[0] = x;
v[1] = y;
v[2] = z;
v[3] = w;
}
};
VBO& m_VBO;
std::string m_VertexAttribName;
std::string m_TexCoordAttribName;
std::string m_NormalAttribName;
std::string m_ColorAttribName;
std::vector<Vec4> m_Vertices;
std::vector<Vec4> m_TexCoords;
std::vector<Vec3> m_Normals;
std::vector<Vec4> m_Colors;
Vec4 m_CurrentTexCoords;
Vec3 m_CurrentNormal;
Vec4 m_CurrentColor;
bool m_TexCoordsDefined;
bool m_NormalsDefined;
bool m_ColorsDefined;
GLint m_PrimitiveType;
GLenum m_Usage;
public:
inline VBOBuilder( VBO& vbo, GLenum usage ) :
m_VBO( vbo ),
m_Usage( usage ),
m_TexCoordsDefined( false ),
m_NormalsDefined( false ),
m_ColorsDefined( false )
{}
inline void SetVertexAttribName ( const std::string &name ) { m_VertexAttribName = name; }
inline void SetTexCoordAttribName( const std::string &name ) { m_TexCoordAttribName = name; }
inline void SetNormalAttribName ( const std::string &name ) { m_NormalAttribName = name; }
inline void SetColorAttribName ( const std::string &name ) { m_ColorAttribName = name; }
inline void Begin( GLint prim )
{
m_PrimitiveType = prim;
}
inline void End()
{
assert( m_PrimitiveType == GL_QUADS );
assert( !m_VertexAttribName.empty() );
m_VBO.AddAttrib( m_VertexAttribName, 4, GL_FLOAT )->LoadData( m_Usage, m_Vertices );
if( m_TexCoordsDefined && !m_TexCoordAttribName.empty() )
m_VBO.AddAttrib( m_TexCoordAttribName, 4, GL_FLOAT )->LoadData( m_Usage, m_TexCoords );
if( m_NormalsDefined && !m_NormalAttribName.empty() )
m_VBO.AddAttrib( m_NormalAttribName, 3, GL_FLOAT )->LoadData( m_Usage, m_Normals );
if( m_ColorsDefined && !m_ColorAttribName.empty() )
m_VBO.AddAttrib( m_ColorAttribName, 4, GL_FLOAT )->LoadData( m_Usage, m_Colors );
if( m_PrimitiveType == GL_QUADS )
{
std::vector<GLuint> indices;
indices.reserve( 6 * m_Vertices.size() / 4 );
for( unsigned int i=0; i<m_Vertices.size(); i+=4 )
{
indices.push_back( i+0 );
indices.push_back( i+3 );
indices.push_back( i+1 );
indices.push_back( i+3 );
indices.push_back( i+2 );
indices.push_back( i+1 );
}
m_VBO.AddIndices( GL_UNSIGNED_INT )->LoadData( m_Usage, indices );
m_PrimitiveType = GL_TRIANGLES;
}
else if( m_PrimitiveType == GL_QUAD_STRIP )
m_PrimitiveType = GL_TRIANGLE_STRIP;
m_VBO.SetPrimitiveType( m_PrimitiveType );
m_Vertices = std::vector<Vec4>();
m_TexCoords = std::vector<Vec4>();
m_Normals = std::vector<Vec3>();
m_Colors = std::vector<Vec4>();
}
inline void TexCoord( GLfloat x ) { TexCoord( x, 0.0f, 0.0f, 1.0f ); }
inline void TexCoord( GLfloat x, GLfloat y ) { TexCoord( x, y, 0.0f, 1.0f ); }
inline void TexCoord( GLfloat x, GLfloat y, GLfloat z ) { TexCoord( x, y, z, 1.0f ); }
inline void TexCoord( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
{
m_TexCoordsDefined = true;
m_CurrentTexCoords = Vec4( x, y, z, w );
}
inline void Normal( GLfloat x, GLfloat y, GLfloat z )
{
m_NormalsDefined = true;
m_CurrentNormal = Vec3( x, y, z );
}
inline void Color( GLfloat x, GLfloat y, GLfloat z ) { Color( x, y, z, 1.0f ); }
inline void Color( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
{
m_ColorsDefined = true;
m_CurrentColor = Vec4( x, y, z, w );
}
inline void Vertex( GLfloat x ) { Vertex( x, 0.0f, 0.0f, 1.0f ); }
inline void Vertex( GLfloat x, GLfloat y ) { Vertex( x, y, 0.0f, 1.0f ); }
inline void Vertex( GLfloat x, GLfloat y, GLfloat z ) { Vertex( x, y, z, 1.0f ); }
inline void Vertex( GLfloat x, GLfloat y, GLfloat z, GLfloat w )
{
m_Vertices .push_back( Vec4(x,y,z,w) );
m_TexCoords.push_back( m_CurrentTexCoords );
m_Normals .push_back( m_CurrentNormal );
m_Colors .push_back( m_CurrentColor );
}
};
}; // namespace GPU
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment