2013-07-22 08:41:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-05-02 13:56:10 +00:00
|
|
|
* Copyright (C) 2013-2016 CERN
|
2020-12-21 23:42:21 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-07-22 08:41:12 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file vertex_manager.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VERTEX_MANAGER_H_
|
|
|
|
#define VERTEX_MANAGER_H_
|
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
2015-12-08 09:56:53 +00:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/glm.hpp>
|
2013-07-22 08:41:12 +00:00
|
|
|
#include <gal/opengl/vertex_common.h>
|
|
|
|
#include <gal/color4d.h>
|
|
|
|
#include <stack>
|
2016-06-29 15:09:55 +00:00
|
|
|
#include <memory>
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
namespace KIGFX
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
class SHADER;
|
|
|
|
class VERTEX_ITEM;
|
|
|
|
class VERTEX_CONTAINER;
|
|
|
|
class GPU_MANAGER;
|
|
|
|
|
2020-12-21 23:42:21 +00:00
|
|
|
/**
|
|
|
|
* Class to control vertex container and GPU with possibility of emulating old-style OpenGL
|
|
|
|
* 1.0 state machine using modern OpenGL methods.
|
|
|
|
*/
|
2013-07-22 08:41:12 +00:00
|
|
|
class VERTEX_MANAGER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aCached says if vertices should be cached in GPU or system memory. For data that
|
2020-12-21 23:42:21 +00:00
|
|
|
* does not change every frame, it is better to store vertices in GPU memory.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
|
|
|
VERTEX_MANAGER( bool aCached );
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Map vertex buffer.
|
2016-05-02 14:12:16 +00:00
|
|
|
*/
|
|
|
|
void Map();
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Unmap vertex buffer.
|
2016-05-02 14:12:16 +00:00
|
|
|
*/
|
|
|
|
void Unmap();
|
|
|
|
|
2016-05-02 13:56:10 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Allocate space for vertices, so it will be used with subsequent Vertex() calls.
|
2016-05-02 13:56:10 +00:00
|
|
|
*
|
|
|
|
* @param aSize is the number of vertices that should be available in the reserved space.
|
2016-05-02 13:56:11 +00:00
|
|
|
* @return True if successful, false otherwise.
|
2016-05-02 13:56:10 +00:00
|
|
|
*/
|
2016-05-02 13:56:11 +00:00
|
|
|
bool Reserve( unsigned int aSize );
|
2016-05-02 13:56:10 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Add a vertex with the given coordinates to the currently set item.
|
|
|
|
*
|
|
|
|
* Color & shader parameters stored in aVertex are ignored, instead color & shader set
|
|
|
|
* by Color() and Shader() functions are used. Vertex coordinates will have the current
|
|
|
|
* transformation matrix applied.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aVertex contains vertex coordinates.
|
2016-05-02 13:56:11 +00:00
|
|
|
* @return True if successful, false otherwise.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
2016-05-02 13:56:11 +00:00
|
|
|
inline bool Vertex( const VERTEX& aVertex )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2016-05-02 13:56:11 +00:00
|
|
|
return Vertex( aVertex.x, aVertex.y, aVertex.z );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Add a vertex with the given coordinates to the currently set item.
|
|
|
|
*
|
|
|
|
* Vertex coordinates will have the current transformation matrix applied.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aX is the X coordinate of the new vertex.
|
|
|
|
* @param aY is the Y coordinate of the new vertex.
|
|
|
|
* @param aZ is the Z coordinate of the new vertex.
|
2016-05-02 13:56:11 +00:00
|
|
|
* @return True if successful, false otherwise.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
2016-05-02 13:56:11 +00:00
|
|
|
bool Vertex( GLfloat aX, GLfloat aY, GLfloat aZ );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2018-10-12 17:08:25 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Add a vertex with the given coordinates to the currently set item.
|
|
|
|
*
|
|
|
|
* Vertex coordinates will have the current transformation matrix applied.
|
2018-10-12 17:08:25 +00:00
|
|
|
*
|
|
|
|
* @param aXY are the XY coordinates of the new vertex.
|
|
|
|
* @param aZ is the Z coordinate of the new vertex.
|
|
|
|
* @return True if successful, false otherwise.
|
|
|
|
*/
|
|
|
|
bool Vertex( const VECTOR2D& aXY, GLfloat aZ )
|
|
|
|
{
|
|
|
|
return Vertex( aXY.x, aXY.y, aZ );
|
|
|
|
}
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Add one or more vertices to the currently set item.
|
|
|
|
*
|
|
|
|
* It takes advantage of allocating memory in advance, so should be faster than
|
|
|
|
* adding vertices one by one. Color & shader parameters stored in aVertices are
|
|
|
|
* ignored, instead color & shader set by Color() and Shader() functions are used.
|
|
|
|
* All the vertex coordinates will have the current transformation matrix applied.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
2020-12-21 23:42:21 +00:00
|
|
|
* @param aVertices contains vertices to be added.
|
2013-07-22 08:41:12 +00:00
|
|
|
* @param aSize is the number of vertices to be added.
|
2016-05-02 13:56:11 +00:00
|
|
|
* @return True if successful, false otherwise.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
2016-05-02 13:56:11 +00:00
|
|
|
bool Vertices( const VERTEX aVertices[], unsigned int aSize );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Changes currently used color that will be applied to newly added vertices.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aColor is the new color.
|
|
|
|
*/
|
|
|
|
inline void Color( const COLOR4D& aColor )
|
|
|
|
{
|
|
|
|
m_color[0] = aColor.r * 255.0;
|
|
|
|
m_color[1] = aColor.g * 255.0;
|
|
|
|
m_color[2] = aColor.b * 255.0;
|
|
|
|
m_color[3] = aColor.a * 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Change currently used color that will be applied to newly added vertices.
|
|
|
|
*
|
|
|
|
* It is the equivalent of glColor4f() function.
|
|
|
|
*
|
2013-07-22 08:41:12 +00:00
|
|
|
* @param aRed is the red component of the new color.
|
|
|
|
* @param aGreen is the green component of the new color.
|
|
|
|
* @param aBlue is the blue component of the new color.
|
|
|
|
* @param aAlpha is the alpha component of the new color.
|
|
|
|
*/
|
|
|
|
inline void Color( GLfloat aRed, GLfloat aGreen, GLfloat aBlue, GLfloat aAlpha )
|
|
|
|
{
|
|
|
|
m_color[0] = aRed * 255.0;
|
|
|
|
m_color[1] = aGreen * 255.0;
|
|
|
|
m_color[2] = aBlue * 255.0;
|
|
|
|
m_color[3] = aAlpha * 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Change currently used shader and its parameters that will be applied to newly added
|
|
|
|
* vertices.
|
|
|
|
*
|
|
|
|
* Parameters depend on shader, for more information have a look at shaders source code.
|
|
|
|
*
|
2013-07-22 08:41:12 +00:00
|
|
|
* @see SHADER_TYPE
|
|
|
|
*
|
|
|
|
* @param aShaderType is the a shader type to be applied.
|
|
|
|
* @param aParam1 is the optional parameter for a shader.
|
|
|
|
* @param aParam2 is the optional parameter for a shader.
|
|
|
|
* @param aParam3 is the optional parameter for a shader.
|
|
|
|
*/
|
2020-12-21 23:42:21 +00:00
|
|
|
inline void Shader( GLfloat aShaderType, GLfloat aParam1 = 0.0f, GLfloat aParam2 = 0.0f,
|
|
|
|
GLfloat aParam3 = 0.0f )
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
2018-10-13 11:39:09 +00:00
|
|
|
m_shader[0] = aShaderType;
|
2013-07-22 08:41:12 +00:00
|
|
|
m_shader[1] = aParam1;
|
|
|
|
m_shader[2] = aParam2;
|
|
|
|
m_shader[3] = aParam3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Multiply the current matrix by a translation matrix, so newly vertices will be
|
|
|
|
* translated by the given vector.
|
|
|
|
*
|
|
|
|
* It is the equivalent of the glTranslatef() function.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aX is the X coordinate of a translation vector.
|
|
|
|
* @param aY is the X coordinate of a translation vector.
|
|
|
|
* @param aZ is the X coordinate of a translation vector.
|
|
|
|
*/
|
|
|
|
inline void Translate( GLfloat aX, GLfloat aY, GLfloat aZ )
|
|
|
|
{
|
|
|
|
m_transform = glm::translate( m_transform, glm::vec3( aX, aY, aZ ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Multiply the current matrix by a rotation matrix, so the newly vertices will be
|
|
|
|
* rotated by the given angles.
|
|
|
|
*
|
|
|
|
* It is the equivalent of the glRotatef() function.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aAngle is the angle of rotation, in radians.
|
|
|
|
* @param aX is a multiplier for the X axis
|
|
|
|
* @param aY is a multiplier for the Y axis
|
|
|
|
* @param aZ is a multiplier for the Z axis.
|
|
|
|
*/
|
|
|
|
inline void Rotate( GLfloat aAngle, GLfloat aX, GLfloat aY, GLfloat aZ )
|
|
|
|
{
|
|
|
|
m_transform = glm::rotate( m_transform, aAngle, glm::vec3( aX, aY, aZ ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Multiply the current matrix by a scaling matrix, so the newly vertices will be
|
|
|
|
* scaled by the given factors.
|
|
|
|
*
|
|
|
|
* It is the equivalent of the glScalef() function.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aX is the X axis scaling factor.
|
|
|
|
* @param aY is the Y axis scaling factor.
|
|
|
|
* @param aZ is the Z axis scaling factor.
|
|
|
|
*/
|
|
|
|
inline void Scale( GLfloat aX, GLfloat aY, GLfloat aZ )
|
|
|
|
{
|
|
|
|
m_transform = glm::scale( m_transform, glm::vec3( aX, aY, aZ ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Push the current transformation matrix stack.
|
|
|
|
*
|
|
|
|
* It is the equivalent of the glPushMatrix() function.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
|
|
|
inline void PushMatrix()
|
|
|
|
{
|
|
|
|
m_transformStack.push( m_transform );
|
|
|
|
|
|
|
|
// Every transformation starts with PushMatrix
|
|
|
|
m_noTransform = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Pop the current transformation matrix stack.
|
|
|
|
*
|
|
|
|
* It is the equivalent of the glPopMatrix() function.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
|
|
|
void PopMatrix()
|
|
|
|
{
|
|
|
|
wxASSERT( !m_transformStack.empty() );
|
|
|
|
|
|
|
|
m_transform = m_transformStack.top();
|
|
|
|
m_transformStack.pop();
|
|
|
|
|
|
|
|
if( m_transformStack.empty() )
|
|
|
|
{
|
|
|
|
// We return back to the identity matrix, thus no vertex transformation is needed
|
|
|
|
m_noTransform = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Set an item to start its modifications.
|
|
|
|
*
|
|
|
|
* After calling the function it is possible to add vertices using function Add().
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aItem is the item that is going to store vertices in the container.
|
|
|
|
*/
|
|
|
|
void SetItem( VERTEX_ITEM& aItem ) const;
|
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Clean after adding an item.
|
2013-09-12 07:44:57 +00:00
|
|
|
*/
|
|
|
|
void FinishItem() const;
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Free the memory occupied by the item, so it is no longer stored in the container.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aItem is the item to be freed
|
|
|
|
*/
|
|
|
|
void FreeItem( VERTEX_ITEM& aItem ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Change the color of all vertices owned by an item.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aItem is the item to change.
|
|
|
|
* @param aColor is the new color to be applied.
|
|
|
|
*/
|
|
|
|
void ChangeItemColor( const VERTEX_ITEM& aItem, const COLOR4D& aColor ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Change the depth of all vertices owned by an item.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aItem is the item to change.
|
|
|
|
* @param aDepth is the new color to be applied.
|
|
|
|
*/
|
|
|
|
void ChangeItemDepth( const VERTEX_ITEM& aItem, GLfloat aDepth ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Return a pointer to the vertices owned by an item.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aItem is the owner of vertices that are going to be returned.
|
|
|
|
* @return Pointer to the vertices or NULL if the item is not stored at the container.
|
|
|
|
*/
|
|
|
|
VERTEX* GetVertices( const VERTEX_ITEM& aItem ) const;
|
|
|
|
|
|
|
|
const glm::mat4& GetTransformation() const
|
|
|
|
{
|
|
|
|
return m_transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Set a shader program that is going to be used during rendering.
|
|
|
|
*
|
2013-07-22 08:41:12 +00:00
|
|
|
* @param aShader is the object containing compiled and linked shader program.
|
|
|
|
*/
|
|
|
|
void SetShader( SHADER& aShader ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Remove all the stored vertices from the container.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
|
|
|
void Clear() const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Prepare buffers and items to start drawing.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
|
|
|
void BeginDrawing() const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Draw an item to the buffer.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aItem is the item to be drawn.
|
|
|
|
*/
|
|
|
|
void DrawItem( const VERTEX_ITEM& aItem ) const;
|
|
|
|
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Finish drawing operations.
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
|
|
|
void EndDrawing() const;
|
|
|
|
|
2018-10-14 22:09:59 +00:00
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Enable/disable Z buffer depth test.
|
2018-10-14 22:09:59 +00:00
|
|
|
*/
|
|
|
|
void EnableDepthTest( bool aEnabled );
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
protected:
|
|
|
|
/**
|
2020-12-21 23:42:21 +00:00
|
|
|
* Apply all transformation to the given coordinates and store them at the specified target.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
|
|
|
* @param aTarget is the place where the new vertex is going to be stored (it has to be
|
2020-12-21 23:42:21 +00:00
|
|
|
* allocated first).
|
2013-07-22 08:41:12 +00:00
|
|
|
* @param aX is the X coordinate of the new vertex.
|
|
|
|
* @param aY is the Y coordinate of the new vertex.
|
|
|
|
* @param aZ is the Z coordinate of the new vertex.
|
|
|
|
*/
|
|
|
|
void putVertex( VERTEX& aTarget, GLfloat aX, GLfloat aY, GLfloat aZ ) const;
|
|
|
|
|
|
|
|
/// Container for vertices, may be cached or noncached
|
2016-06-29 15:09:55 +00:00
|
|
|
std::shared_ptr<VERTEX_CONTAINER> m_container;
|
2013-07-22 08:41:12 +00:00
|
|
|
/// GPU manager for data transfers and drawing operations
|
2016-06-29 15:09:55 +00:00
|
|
|
std::shared_ptr<GPU_MANAGER> m_gpu;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
/// State machine variables
|
|
|
|
/// True in case there is no need to transform vertices
|
|
|
|
bool m_noTransform;
|
|
|
|
/// Currently used transform matrix
|
|
|
|
glm::mat4 m_transform;
|
|
|
|
/// Stack of transformation matrices, used for Push/PopMatrix
|
|
|
|
std::stack<glm::mat4> m_transformStack;
|
|
|
|
/// Currently used color
|
2017-09-09 18:40:00 +00:00
|
|
|
GLubyte m_color[COLOR_STRIDE];
|
2013-07-22 08:41:12 +00:00
|
|
|
/// Currently used shader and its parameters
|
2017-09-09 18:40:00 +00:00
|
|
|
GLfloat m_shader[SHADER_STRIDE];
|
2016-05-02 13:56:10 +00:00
|
|
|
|
|
|
|
/// Currently reserved chunk to store vertices
|
|
|
|
VERTEX* m_reserved;
|
|
|
|
|
|
|
|
/// Currently available reserved space
|
|
|
|
unsigned int m_reservedSpace;
|
2013-07-22 08:41:12 +00:00
|
|
|
};
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
} // namespace KIGFX
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
#endif /* VERTEX_MANAGER_H_ */
|