Commit 83f5bd60 authored by Maciej Suminski's avatar Maciej Suminski

Moved fields containing information about currently used color, shader and...

Moved fields containing information about currently used color, shader and transformation for vertices from VBO_ITEM to VBO_CONTAINER (OPENGL_GAL).
parent 4de43d7c
...@@ -1543,7 +1543,7 @@ void OPENGL_GAL::Save() ...@@ -1543,7 +1543,7 @@ void OPENGL_GAL::Save()
if( isGrouping ) if( isGrouping )
{ {
transformStack.push( transform ); transformStack.push( transform );
curVboItem->SetTransformMatrix( &transform ); vboContainer->SetTransformMatrix( &transform );
} }
else else
{ {
...@@ -1562,7 +1562,7 @@ void OPENGL_GAL::Restore() ...@@ -1562,7 +1562,7 @@ void OPENGL_GAL::Restore()
if( transformStack.empty() ) if( transformStack.empty() )
{ {
// Disable transforming, as the selected matrix is identity // Disable transforming, as the selected matrix is identity
curVboItem->SetTransformMatrix( NULL ); vboContainer->SetTransformMatrix( NULL );
} }
} }
else else
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
*/ */
#include <gal/opengl/vbo_container.h> #include <gal/opengl/vbo_container.h>
#include <gal/opengl/vbo_item.h>
#include <cstring> #include <cstring>
#include <wx/log.h> #include <wx/log.h>
#ifdef __WXDEBUG__ #ifdef __WXDEBUG__
...@@ -40,8 +39,11 @@ ...@@ -40,8 +39,11 @@
using namespace KiGfx; using namespace KiGfx;
VBO_CONTAINER::VBO_CONTAINER( int aSize ) : VBO_CONTAINER::VBO_CONTAINER( int aSize ) :
m_freeSpace( aSize ), m_currentSize( aSize ), itemStarted( false ) m_freeSpace( aSize ), m_currentSize( aSize ), itemStarted( false ), m_transform( NULL )
{ {
// By default no shader is used
m_shader[0] = 0;
m_vertices = new VBO_VERTEX[aSize]; m_vertices = new VBO_VERTEX[aSize];
// In the beginning there is only free space // In the beginning there is only free space
...@@ -88,6 +90,7 @@ void VBO_CONTAINER::EndItem() ...@@ -88,6 +90,7 @@ void VBO_CONTAINER::EndItem()
void VBO_CONTAINER::Add( VBO_ITEM* aVboItem, const VBO_VERTEX* aVertex, unsigned int aSize ) void VBO_CONTAINER::Add( VBO_ITEM* aVboItem, const VBO_VERTEX* aVertex, unsigned int aSize )
{ {
unsigned int offset; unsigned int offset;
VBO_VERTEX* vertexPtr;
if( itemStarted ) // There is an item being created with an unknown size.. if( itemStarted ) // There is an item being created with an unknown size..
{ {
...@@ -146,7 +149,44 @@ void VBO_CONTAINER::Add( VBO_ITEM* aVboItem, const VBO_VERTEX* aVertex, unsigned ...@@ -146,7 +149,44 @@ void VBO_CONTAINER::Add( VBO_ITEM* aVboItem, const VBO_VERTEX* aVertex, unsigned
offset = getChunkOffset( *it ) + itemSize; offset = getChunkOffset( *it ) + itemSize;
} }
memcpy( &m_vertices[offset], aVertex, aSize * VBO_ITEM::VertByteSize ); for( unsigned int i = 0; i < aSize; ++i )
{
// Pointer to the vertex that we are currently adding
vertexPtr = &m_vertices[offset + i];
// Modify the vertex according to the currently used transformations
if( m_transform != NULL )
{
// Apply transformations
glm::vec4 vertex( aVertex[i].x, aVertex[i].y, aVertex[i].z, 1.0f );
vertex = *m_transform * vertex;
// Replace only coordinates, leave color as it is
vertexPtr->x = vertex.x;
vertexPtr->y = vertex.y;
vertexPtr->z = vertex.z;
}
else
{
// Simply copy coordinates
vertexPtr->x = aVertex[i].x;
vertexPtr->y = aVertex[i].y;
vertexPtr->z = aVertex[i].z;
}
// Apply currently used color
vertexPtr->r = m_color[0];
vertexPtr->g = m_color[1];
vertexPtr->b = m_color[2];
vertexPtr->a = m_color[3];
// Apply currently used shader
for( unsigned int i = 0; i < VBO_ITEM::ShaderStride; ++i )
{
vertexPtr->shader[i] = m_shader[i];
}
}
} }
......
...@@ -37,12 +37,8 @@ VBO_ITEM::VBO_ITEM( VBO_CONTAINER* aContainer ) : ...@@ -37,12 +37,8 @@ VBO_ITEM::VBO_ITEM( VBO_CONTAINER* aContainer ) :
m_offset( 0 ), m_offset( 0 ),
m_size( 0 ), m_size( 0 ),
m_container( aContainer ), m_container( aContainer ),
m_isDirty( true ), m_isDirty( true )
m_transform( NULL )
{ {
// By default no shader is used
m_shader[0] = 0;
// The item's size is not known yet, so we just start an item in the container // The item's size is not known yet, so we just start an item in the container
aContainer->StartItem( this ); aContainer->StartItem( this );
} }
...@@ -54,32 +50,8 @@ VBO_ITEM::~VBO_ITEM() ...@@ -54,32 +50,8 @@ VBO_ITEM::~VBO_ITEM()
} }
void VBO_ITEM::PushVertex( VBO_VERTEX* aVertex ) void VBO_ITEM::PushVertex( const VBO_VERTEX* aVertex )
{ {
if( m_transform != NULL )
{
// Apply transformations
glm::vec4 vertex( aVertex->x, aVertex->y, aVertex->z, 1.0f );
vertex = *m_transform * vertex;
// Replace only coordinates, leave color as it is
aVertex->x = vertex.x;
aVertex->y = vertex.y;
aVertex->z = vertex.z;
}
// Apply currently used color
aVertex->r = m_color[0];
aVertex->g = m_color[1];
aVertex->b = m_color[2];
aVertex->a = m_color[3];
// Apply currently used shader
for( int i = 0; i < ShaderStride; ++i )
{
aVertex->shader[i] = m_shader[i];
}
m_container->Add( this, aVertex ); m_container->Add( this, aVertex );
m_size++; m_size++;
...@@ -87,7 +59,7 @@ void VBO_ITEM::PushVertex( VBO_VERTEX* aVertex ) ...@@ -87,7 +59,7 @@ void VBO_ITEM::PushVertex( VBO_VERTEX* aVertex )
} }
void VBO_ITEM::PushVertices( VBO_VERTEX* aVertices, GLuint aSize ) void VBO_ITEM::PushVertices( const VBO_VERTEX* aVertices, GLuint aSize )
{ {
for( unsigned int i = 0; i < aSize; ++i ) for( unsigned int i = 0; i < aSize; ++i )
{ {
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#define GLM_FORCE_RADIANS #define GLM_FORCE_RADIANS
#include <gal/opengl/glm/gtc/matrix_transform.hpp> #include <gal/opengl/glm/gtc/matrix_transform.hpp>
#include <gal/opengl/vbo_item.h> #include <gal/opengl/vbo_container.h>
#include <gal/opengl/shader.h> #include <gal/opengl/shader.h>
// wxWidgets imports // wxWidgets imports
...@@ -58,7 +58,6 @@ ...@@ -58,7 +58,6 @@
namespace KiGfx namespace KiGfx
{ {
class SHADER; class SHADER;
class VBO_CONTAINER;
/** /**
* @brief Class OpenGL_GAL is the OpenGL implementation of the Graphics Abstraction Layer. * @brief Class OpenGL_GAL is the OpenGL implementation of the Graphics Abstraction Layer.
...@@ -601,7 +600,7 @@ private: ...@@ -601,7 +600,7 @@ private:
{ {
if( isGrouping ) if( isGrouping )
{ {
curVboItem->UseColor( COLOR4D( aRed, aGreen, aBlue, aAlpha ) ); vboContainer->UseColor( aRed, aGreen, aBlue, aAlpha );
} }
else else
{ {
...@@ -620,7 +619,7 @@ private: ...@@ -620,7 +619,7 @@ private:
{ {
if( isGrouping ) if( isGrouping )
{ {
curVboItem->UseColor( aColor ); vboContainer->UseColor( aColor );
} }
else else
{ {
...@@ -642,7 +641,7 @@ private: ...@@ -642,7 +641,7 @@ private:
{ {
const GLfloat shader[] = { aShader, aParam1, aParam2, aParam3 }; const GLfloat shader[] = { aShader, aParam1, aParam2, aParam3 };
curVboItem->UseShader( shader ); vboContainer->UseShader( shader );
} }
} }
}; };
......
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
#define VBO_CONTAINER_H_ #define VBO_CONTAINER_H_
#include <GL/gl.h> #include <GL/gl.h>
#include <gal/opengl/glm/glm.hpp>
#include <gal/opengl/vbo_item.h>
#include <gal/color4d.h>
#include <map> #include <map>
#include <wx/log.h> #include <wx/log.h>
...@@ -118,6 +121,73 @@ public: ...@@ -118,6 +121,73 @@ public:
return m_currentSize; return m_currentSize;
} }
/**
* Function SetTransformMatrix()
* Sets transformation matrix for vertices that are added to VBO_ITEM. If you do not want to
* transform vertices at all, pass NULL as the argument.
* @param aMatrix is the new transform matrix or NULL if you do not want to use transformation
* matrix.
*/
inline void SetTransformMatrix( const glm::mat4* aMatrix )
{
m_transform = aMatrix;
}
/**
* Function UseColor()
* Sets color used for all added vertices.
* @param aColor is the color used for added vertices.
*/
inline void UseColor( const COLOR4D& aColor )
{
m_color[0] = aColor.r;
m_color[1] = aColor.g;
m_color[2] = aColor.b;
m_color[3] = aColor.a;
}
/**
* Function UseColor()
* Sets color used for all added vertices.
* @param aColor is the color used for added vertices.
*/
inline void UseColor( const GLfloat aColor[VBO_ITEM::ColorStride] )
{
for( unsigned int i = 0; i < VBO_ITEM::ColorStride; ++i )
{
m_color[i] = aColor[i];
}
}
/**
* Function UseColor()
* Sets color used for all added vertices.
* @param aR is the red component of the color.
* @param aG is the green component of the color.
* @param aB is the blue component of the color.
* @param aA is the alpha component of the color.
*/
inline void UseColor( GLfloat aR, GLfloat aG, GLfloat aB, GLfloat aA )
{
m_color[0] = aR;
m_color[1] = aG;
m_color[2] = aB;
m_color[3] = aA;
}
/**
* Function UseShader()
* Sets shader and its parameters used for all added vertices.
* @param aShader is the array that contains shader number followed by its parameters.
*/
inline void UseShader( const GLfloat aShader[VBO_ITEM::ShaderStride] )
{
for( unsigned int i = 0; i < VBO_ITEM::ShaderStride; ++i )
{
m_shader[i] = aShader[i];
}
}
private: private:
///< Stores size & offset of free chunks. ///< Stores size & offset of free chunks.
FreeChunkMap m_freeChunks; FreeChunkMap m_freeChunks;
...@@ -233,15 +303,25 @@ private: ...@@ -233,15 +303,25 @@ private:
bool itemStarted; bool itemStarted;
///< Variables holding the state of the item currently being added ///< Variables holding the state of the item currently being added
unsigned int itemSize, itemChunkSize; unsigned int itemSize;
unsigned int itemChunkSize;
VBO_ITEM* item; VBO_ITEM* item;
///< Color used for new vertices pushed.
GLfloat m_color[VBO_ITEM::ColorStride];
///< Shader and its parameters used for new vertices pushed
GLfloat m_shader[VBO_ITEM::ShaderStride];
///< Current transform matrix applied for every new vertex pushed.
const glm::mat4* m_transform;
/** /**
* Function getPowerOf2() * Function getPowerOf2()
* Returns the nearest power of 2, bigger than aNumber. * Returns the nearest power of 2, bigger than aNumber.
* @param aNumber is the number for which we look for a bigger power of 2. * @param aNumber is the number for which we look for a bigger power of 2.
*/ */
unsigned int getPowerOf2( unsigned int aNumber ) unsigned int getPowerOf2( unsigned int aNumber ) const
{ {
unsigned int power = 1; unsigned int power = 1;
......
...@@ -31,9 +31,7 @@ ...@@ -31,9 +31,7 @@
#define VBO_ITEM_H_ #define VBO_ITEM_H_
#include <GL/gl.h> #include <GL/gl.h>
#include <gal/opengl/glm/glm.hpp>
#include <gal/color4d.h> #include <gal/color4d.h>
#include <cstddef> #include <cstddef>
namespace KiGfx namespace KiGfx
...@@ -61,7 +59,7 @@ public: ...@@ -61,7 +59,7 @@ public:
* @param aVertex is a vertex to be added. * @param aVertex is a vertex to be added.
* @param aShader is an attribute for shader. * @param aShader is an attribute for shader.
*/ */
void PushVertex( VBO_VERTEX* aVertex ); void PushVertex( const VBO_VERTEX* aVertex );
/** /**
* Function PushVertices() * Function PushVertices()
...@@ -72,7 +70,7 @@ public: ...@@ -72,7 +70,7 @@ public:
* @param aSize is an amount of vertices to be added. * @param aSize is an amount of vertices to be added.
* @param aShader is an attribute for shader. * @param aShader is an attribute for shader.
*/ */
void PushVertices( VBO_VERTEX* aVertices, GLuint aSize ); void PushVertices( const VBO_VERTEX* aVertices, GLuint aSize );
/** /**
* Function GetVertices() * Function GetVertices()
...@@ -97,7 +95,7 @@ public: ...@@ -97,7 +95,7 @@ public:
* Sets data offset in the VBO. * Sets data offset in the VBO.
* @param aOffset is the offset expressed as a number of vertices. * @param aOffset is the offset expressed as a number of vertices.
*/ */
void SetOffset( unsigned int aOffset ) inline void SetOffset( unsigned int aOffset )
{ {
m_offset = aOffset; m_offset = aOffset;
} }
...@@ -112,18 +110,6 @@ public: ...@@ -112,18 +110,6 @@ public:
return m_offset; return m_offset;
} }
/**
* Function SetTransformMatrix()
* Sets transformation matrix for vertices that are added to VBO_ITEM. If you do not want to
* transform vertices at all, pass NULL as the argument.
* @param aMatrix is the new transform matrix or NULL if you do not want to use transformation
* matrix.
*/
void SetTransformMatrix( const glm::mat4* aMatrix )
{
m_transform = aMatrix;
}
/** /**
* Function ChangeColor() * Function ChangeColor()
* Colors all vertices to the specified color. * Colors all vertices to the specified color.
...@@ -131,57 +117,31 @@ public: ...@@ -131,57 +117,31 @@ public:
*/ */
void ChangeColor( const COLOR4D& aColor ); void ChangeColor( const COLOR4D& aColor );
/**
* Function UseColor()
* Sets color used for all added vertices.
* @param aColor is the color used for added vertices.
*/
void UseColor( const COLOR4D& aColor )
{
m_color[0] = aColor.r;
m_color[1] = aColor.g;
m_color[2] = aColor.b;
m_color[3] = aColor.a;
}
/**
* Function UseShader()
* Sets shader and its parameters used for all added vertices.
* @param aShader is the array that contains shader number followed by its parameters.
*/
inline void UseShader( const GLfloat* aShader )
{
for( int i = 0; i < ShaderStride; ++i )
{
m_shader[i] = aShader[i];
}
}
///< Informs the container that there will be no more vertices for the current VBO_ITEM ///< Informs the container that there will be no more vertices for the current VBO_ITEM
void Finish(); void Finish();
///< Data organization information for vertices {X,Y,Z,R,G,B,A} (@see VBO_VERTEX). ///< Data organization information for vertices {X,Y,Z,R,G,B,A} (@see VBO_VERTEX).
static const int VertByteSize = sizeof(VBO_VERTEX); static const unsigned int VertByteSize = sizeof(VBO_VERTEX);
static const int VertStride = VertByteSize / sizeof(GLfloat); static const unsigned int VertStride = VertByteSize / sizeof(GLfloat);
static const int CoordByteSize = sizeof(VBO_VERTEX().x) + sizeof(VBO_VERTEX().y) + static const unsigned int CoordByteSize = sizeof(VBO_VERTEX().x) + sizeof(VBO_VERTEX().y) +
sizeof(VBO_VERTEX().z); sizeof(VBO_VERTEX().z);
static const int CoordStride = CoordByteSize / sizeof(GLfloat); static const unsigned int CoordStride = CoordByteSize / sizeof(GLfloat);
// Offset of color data from the beginning of each vertex data // Offset of color data from the beginning of each vertex data
static const int ColorByteOffset = offsetof(VBO_VERTEX, r); static const unsigned int ColorByteOffset = offsetof(VBO_VERTEX, r);
static const int ColorOffset = ColorByteOffset / sizeof(GLfloat); static const unsigned int ColorOffset = ColorByteOffset / sizeof(GLfloat);
static const int ColorByteSize = sizeof(VBO_VERTEX().r) + sizeof(VBO_VERTEX().g) + static const unsigned int ColorByteSize = sizeof(VBO_VERTEX().r) + sizeof(VBO_VERTEX().g) +
sizeof(VBO_VERTEX().b) + sizeof(VBO_VERTEX().a); sizeof(VBO_VERTEX().b) + sizeof(VBO_VERTEX().a);
static const int ColorStride = ColorByteSize / sizeof(GLfloat); static const unsigned int ColorStride = ColorByteSize / sizeof(GLfloat);
// Shader attributes // Shader attributes
static const int ShaderByteOffset = offsetof(VBO_VERTEX, shader); static const unsigned int ShaderByteOffset = offsetof(VBO_VERTEX, shader);
static const int ShaderOffset = ShaderByteOffset / sizeof(GLfloat); static const unsigned int ShaderOffset = ShaderByteOffset / sizeof(GLfloat);
static const int ShaderByteSize = sizeof(VBO_VERTEX().shader); static const unsigned int ShaderByteSize = sizeof(VBO_VERTEX().shader);
static const int ShaderStride = ShaderByteSize / sizeof(GLfloat); static const unsigned int ShaderStride = ShaderByteSize / sizeof(GLfloat);
static const int IndByteSize = sizeof(GLuint); static const unsigned int IndByteSize = sizeof(GLuint);
private: private:
///< Offset and size of data stored in the VBO_CONTAINER. ///< Offset and size of data stored in the VBO_CONTAINER.
...@@ -191,17 +151,8 @@ private: ...@@ -191,17 +151,8 @@ private:
///< Storage for vertices. ///< Storage for vertices.
VBO_CONTAINER* m_container; VBO_CONTAINER* m_container;
///< Color used for new vertices pushed.
GLfloat m_color[ColorStride];
///< Shader and its parameters used for new vertices pushed
GLfloat m_shader[ShaderStride];
///< Flag telling if the item should be recached in VBO or not. ///< Flag telling if the item should be recached in VBO or not.
bool m_isDirty; bool m_isDirty;
///< Current transform matrix applied for every new vertex pushed.
const glm::mat4* m_transform;
}; };
} // namespace KiGfx } // namespace KiGfx
......
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