Commit a8e782f7 authored by Frédéric Larue's avatar Frédéric Larue
Browse files

Management of redundant stops while exporting GLSL function in GradientWidget improved.

parent b5010f02
......@@ -238,23 +238,17 @@ void GradientWidget::setStops( const QList<QColor>& colors, const QList<float>&
}
QString GradientWidget::getShaderFunctionBodyGLSL( int beg, int end, const QString& indent ) const
QString GradientWidget::getShaderFunctionBodyGLSL( const QVector< QPair<int,int> >& uniqueStops, int beg, int end, const QString& indent ) const
{
QString res;
if( end != beg )
{
int p = beg + (end-beg)/2;
int p1 = p, p2 = p;
while( p1>0 && m_Stops[p1-1].value() == m_Stops[p].value() )
-- p1;
while( p2<m_Stops.size()-1 && m_Stops[p2+1].value() == m_Stops[p].value() )
++ p2;
res += indent + "if( value <= " + QString::number( m_Stops[p].value() ) + " )\n";
res += getShaderFunctionBodyGLSL( beg, p1, indent+" " );
res += indent + "if( value <= " + QString::number( m_Stops[ uniqueStops[p].first ].value() ) + " )\n";
res += getShaderFunctionBodyGLSL( uniqueStops, beg, p, indent+" " );
res += indent + "else\n";
res += getShaderFunctionBodyGLSL( p2+1, end, indent+" " );
res += getShaderFunctionBodyGLSL( uniqueStops, p+1, end, indent+" " );
}
else
{
......@@ -263,15 +257,15 @@ QString GradientWidget::getShaderFunctionBodyGLSL( int beg, int end, const QStri
QColor c = m_Stops.front().color();
res = indent + "return vec3(" + QString::number(c.redF()) + "," + QString::number(c.greenF()) + "," + QString::number(c.blueF()) + ");\n";
}
else if( beg == m_Stops.size() )
else if( beg == uniqueStops.size() )
{
QColor c = m_Stops.back().color();
res = indent + "return vec3(" + QString::number(c.redF()) + "," + QString::number(c.greenF()) + "," + QString::number(c.blueF()) + ");\n";
}
else
{
const Stop &s0 = m_Stops[beg-1];
const Stop &s1 = m_Stops[beg ];
const Stop &s0 = m_Stops[ uniqueStops[beg-1].second ];
const Stop &s1 = m_Stops[ uniqueStops[beg ].first ];
const QColor &c0 = s0.color();
const QColor &c1 = s1.color();
......@@ -290,10 +284,22 @@ QString GradientWidget::getShaderFunctionBodyGLSL( int beg, int end, const QStri
QString GradientWidget::getShaderFunctionGLSL( const QString& funcName ) const
{
QVector< QPair<int,int> > uniqueStops;
for( int left=0; left<m_Stops.size(); ++left )
{
int right = left;
while( right<m_Stops.size()-1 && m_Stops[right+1].value() == m_Stops[left].value() )
++ right;
uniqueStops.push_back( QPair<int,int>(left,right) );
left = right;
}
QString func =
"vec3 " + funcName + "( float value )\n"
"{\n"
+ getShaderFunctionBodyGLSL( 0, m_Stops.size(), " " ) +
+ getShaderFunctionBodyGLSL( uniqueStops, 0, uniqueStops.size(), " " ) +
"}\n";
return func;
......
......@@ -47,7 +47,7 @@ private:
int getInterval( float value ) const;
QColor getColor( int interval, float value ) const;
float clampValueToStopInterval( int n, float value ) const;
QString getShaderFunctionBodyGLSL( int beg, int end, const QString& indent ) const;
QString getShaderFunctionBodyGLSL( const QVector< QPair<int,int> >& uniqueStops, int beg, int end, const QString& indent ) const;
protected:
void resizeEvent( QResizeEvent *evt );
......
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