[cortex-vfx] r4611 committed - Added renderer attributes for alpha test in IECoreGL. See docs for IEC...

0 views
Skip to first unread message

corte...@googlecode.com

unread,
Feb 29, 2012, 2:04:23 PM2/29/12
to cortex...@googlegroups.com
Revision: 4611
Author: david.im...@gmail.com
Date: Wed Feb 29 11:03:27 2012
Log: Added renderer attributes for alpha test in IECoreGL. See docs
for IECoreGL::Renderer for details.
http://code.google.com/p/cortex-vfx/source/detail?r=4611

Modified:
/trunk/include/IECoreGL/Renderer.h
/trunk/include/IECoreGL/TypeIds.h
/trunk/include/IECoreGL/TypedStateComponent.h
/trunk/src/IECoreGL/Renderer.cpp
/trunk/src/IECoreGL/TypedStateComponent.cpp
/trunk/test/IECoreGL/Renderer.py

=======================================
--- /trunk/include/IECoreGL/Renderer.h Wed Jan 11 15:04:34 2012
+++ /trunk/include/IECoreGL/Renderer.h Wed Feb 29 11:03:27 2012
@@ -308,6 +308,25 @@
/// "min"<br>
/// "max"<br>
///
+ /// \li <b>"gl:alphaTest" BoolData false</b><br>
+ /// When this is true, gl alpha testing will be enabled.
+ ///
+ /// \li <b>"gl:alphaTest:value" FloatData 0</b><br>
+ /// Alpha test comparison value for glAlphaFunc
+ ///
+ /// \li <b>"gl:alphaTest:mode" StringData "always"</b><br>
+ /// Alpha test comparison mode for glAlphaFunc.
+ /// Valid values are listed below, and map directly onto the
corresponding
+ /// GLenum values.<br><br>
+ /// "never"<br>
+ /// "less"<br>
+ /// "equal"<br>
+ /// "lequal"<br>
+ /// "greater"<br>
+ /// "notequal"<br>
+ /// "gequal"<br>
+ /// "always"<br>
+ ///
/// \par Implementation specific antialiasing attributes :
///////////////////////////////////////////////////////////
///
=======================================
--- /trunk/include/IECoreGL/TypeIds.h Wed Jan 11 11:19:01 2012
+++ /trunk/include/IECoreGL/TypeIds.h Wed Feb 29 11:03:27 2012
@@ -114,6 +114,8 @@
DepthTestStateComponentTypeId = 105071,
CameraVisibilityStateComponentTypeId = 105072,
DepthMaskStateComponentTypeId = 105073,
+ AlphaTestStateComponentTypeId = 105074,
+ AlphaFuncStateComponentTypeId = 105075,
LastCoreGLTypeId = 105999,
};

=======================================
--- /trunk/include/IECoreGL/TypedStateComponent.h Wed Jan 11 11:19:01 2012
+++ /trunk/include/IECoreGL/TypedStateComponent.h Wed Feb 29 11:03:27 2012
@@ -149,6 +149,14 @@
GLenum dst;
};

+struct AlphaFunc
+{
+ AlphaFunc( GLenum m, GLfloat value );
+ AlphaFunc( const AlphaFunc &other );
+ GLenum mode;
+ GLfloat value;
+};
+
typedef TypedStateComponent<BlendFactors, BlendFuncStateComponentTypeId>
BlendFuncStateComponent;
template<>
void BlendFuncStateComponent::bind() const;
@@ -161,6 +169,14 @@
template<>
void BlendEquationStateComponent::bind() const;

+typedef TypedStateComponent<bool, AlphaTestStateComponentTypeId>
AlphaTestStateComponent;
+template<>
+void AlphaTestStateComponent::bind() const;
+
+typedef TypedStateComponent<AlphaFunc, AlphaFuncStateComponentTypeId>
AlphaFuncStateComponent;
+template<>
+void AlphaFuncStateComponent::bind() const;
+
/// Used to specify enable state of GL_CULL_FACE
typedef TypedStateComponent<bool, DoubleSidedStateComponentTypeId>
DoubleSidedStateComponent;
template<>
=======================================
--- /trunk/src/IECoreGL/Renderer.cpp Wed Jan 11 11:19:01 2012
+++ /trunk/src/IECoreGL/Renderer.cpp Wed Feb 29 11:03:27 2012
@@ -911,6 +911,117 @@
}
memberData->implementation->addState( new BlendFuncStateComponent( bf ) );
}
+
+static void alphaFuncSetter( const std::string &name, IECore::ConstDataPtr
value, IECoreGL::Renderer::MemberData *memberData )
+{
+
+ const AlphaFuncStateComponent *a =
memberData->implementation->getState<AlphaFuncStateComponent>();
+ AlphaFunc af = a->value();
+
+ if( name == "gl:alphaTest:mode" )
+ {
+ ConstStringDataPtr d = castWithWarning<const StringData>( value,
name, "Renderer::setAttribute" );
+ if( !d )
+ {
+ return;
+ }
+
+ GLenum m;
+ const std::string &v = d->readable();
+ if( v=="never" )
+ {
+ m = GL_NEVER;
+ }
+ else if( v=="less" )
+ {
+ m = GL_LESS;
+ }
+ else if( v=="equal" )
+ {
+ m = GL_EQUAL;
+ }
+ else if( v=="lequal" )
+ {
+ m = GL_LEQUAL;
+ }
+ else if( v=="greater" )
+ {
+ m = GL_GREATER;
+ }
+ else if( v=="notequal" )
+ {
+ m = GL_NOTEQUAL;
+ }
+ else if( v=="gequal" )
+ {
+ m = GL_GEQUAL;
+ }
+ else if( v=="always" )
+ {
+ m = GL_ALWAYS;
+ }
+ else
+ {
+ msg( Msg::Error, "Renderer::setAttribute", boost::format( "Unsupported
value \"%s\" for attribute \"%s\"." ) % v % name );
+ return;
+ }
+ af.mode = m;
+ }
+ else if( name == "gl:alphaTest:value" )
+ {
+ ConstFloatDataPtr d = castWithWarning<const FloatData>( value,
name, "Renderer::setAttribute" );
+ if( !d )
+ {
+ return;
+ }
+ af.value = d->readable();
+ }
+ else
+ {
+ return;
+ }
+
+ memberData->implementation->addState( new AlphaFuncStateComponent( af ) );
+}
+
+static IECore::ConstDataPtr alphaFuncGetter( const std::string &name,
const IECoreGL::Renderer::MemberData *memberData )
+{
+ const AlphaFuncStateComponent *b =
memberData->implementation->getState<AlphaFuncStateComponent>();
+
+ if( name == "gl:alphaTest:mode" )
+ {
+ GLenum m = b->value().mode;
+ switch( m )
+ {
+ case GL_NEVER:
+ return new StringData( "never" );
+ case GL_LESS:
+ return new StringData( "less" );
+ case GL_EQUAL:
+ return new StringData( "equal" );
+ case GL_LEQUAL:
+ return new StringData( "lequal" );
+ case GL_GREATER:
+ return new StringData( "greater" );
+ case GL_NOTEQUAL:
+ return new StringData( "notequal" );
+ case GL_GEQUAL:
+ return new StringData( "gequal" );
+ case GL_ALWAYS:
+ return new StringData( "always" );
+ default :
+ msg( Msg::Warning, "Renderer::getAttribute", boost::format( "Invalid
state for \"%s\"." ) % name );
+ return new StringData( "invalid" );
+ }
+ }
+ else if( name == "gl:alphaTest:value" )
+ {
+ return new FloatData( b->value().value );
+ }
+
+ msg( Msg::Warning, "Renderer::getAttribute", boost::format( "Invalid
state for \"%s\"." ) % name );
+ return 0;
+}

static IECore::ConstDataPtr blendEquationGetter( const std::string &name,
const IECoreGL::Renderer::MemberData *memberData )
{
@@ -1182,6 +1293,9 @@
(*a)["gl:visibility:camera"] =
typedAttributeSetter<CameraVisibilityStateComponent>;
(*a)["gl:depthTest"] = typedAttributeSetter<DepthTestStateComponent>;
(*a)["gl:depthMask"] = typedAttributeSetter<DepthMaskStateComponent>;
+ (*a)["gl:alphaTest"] = typedAttributeSetter<AlphaTestStateComponent>;
+ (*a)["gl:alphaTest:mode"] = alphaFuncSetter;
+ (*a)["gl:alphaTest:value"] = alphaFuncSetter;
}
return a;
}
@@ -1230,6 +1344,9 @@
(*a)["gl:visibility:camera"] =
typedAttributeGetter<CameraVisibilityStateComponent>;
(*a)["gl:depthTest"] = typedAttributeGetter<DepthTestStateComponent>;
(*a)["gl:depthMask"] = typedAttributeGetter<DepthMaskStateComponent>;
+ (*a)["gl:alphaTest"] = typedAttributeGetter<AlphaTestStateComponent>;
+ (*a)["gl:alphaTest:mode"] = alphaFuncGetter;
+ (*a)["gl:alphaTest:value"] = alphaFuncGetter;
}
return a;
}
=======================================
--- /trunk/src/IECoreGL/TypedStateComponent.cpp Wed Jan 11 11:19:01 2012
+++ /trunk/src/IECoreGL/TypedStateComponent.cpp Wed Feb 29 11:03:27 2012
@@ -96,6 +96,49 @@

template class TypedStateComponent<GLenum,
BlendEquationStateComponentTypeId>;

+
+IECOREGL_TYPEDSTATECOMPONENT_SPECIALISE( AlphaTestStateComponent, bool,
false );
+
+template<>
+void AlphaTestStateComponent::bind() const
+{
+ if( m_value )
+ {
+ glEnable( GL_ALPHA_TEST );
+ }
+ else
+ {
+ glDisable( GL_ALPHA_TEST );
+ }
+}
+
+template class TypedStateComponent<bool, AlphaTestStateComponentTypeId>;
+
+
+
+AlphaFunc::AlphaFunc( GLenum m, GLfloat v )
+ : mode( m ), value( v )
+{
+}
+
+AlphaFunc::AlphaFunc( const AlphaFunc &other )
+ : mode( other.mode ), value( other.value )
+{
+}
+
+IECOREGL_TYPEDSTATECOMPONENT_SPECIALISE( AlphaFuncStateComponent,
AlphaFunc, AlphaFunc( GL_ALWAYS, 0.0f ) );
+
+template<>
+void AlphaFuncStateComponent::bind() const
+{
+ glAlphaFunc( m_value.mode, m_value.value );
+}
+
+template class TypedStateComponent<AlphaFunc,
AlphaFuncStateComponentTypeId>;
+
+
+
+
// doubleSided specialisations and instantiations
//////////////////////////////////////////////////////////////////////

=======================================
--- /trunk/test/IECoreGL/Renderer.py Mon Aug 15 16:13:32 2011
+++ /trunk/test/IECoreGL/Renderer.py Wed Feb 29 11:03:27 2012
@@ -119,6 +119,12 @@
if withFreeType() :
self.assertEqual( r.getAttribute( "gl:textPrimitive:type" ),
StringData( "mesh" ) )
self.assertEqual( r.getAttribute( "gl:depthTest" ), BoolData( True ) )
+ self.assertEqual( r.getAttribute( "gl:depthMask" ), BoolData( True ) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest" ), BoolData( False ) )
+
+ self.assertEqual( r.getAttribute( "gl:alphaTest:mode" ),
StringData( "always" ) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest:value" ), FloatData(
0.0 ) )
+
self.assertEqual( r.getAttribute( "gl:visibility:camera" ), BoolData(
True ) )

r.setAttribute( "color", Color3fData( Color3f( 0, 1, 2 ) ) )
@@ -181,7 +187,28 @@

r.setAttribute( "gl:depthTest", BoolData( False ) )
self.assertEqual( r.getAttribute( "gl:depthTest" ), BoolData( False ) )
-
+
+ r.setAttribute( "gl:depthMask", BoolData( False ) )
+ self.assertEqual( r.getAttribute( "gl:depthMask" ), BoolData( False ) )
+
+ r.setAttribute( "gl:alphaTest", BoolData( True ) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest" ), BoolData( True ) )
+
+ alphaTestModes =
[ "never", "less", "equal", "lequal", "greater", "notequal", "gequal", "always"
]
+ value = 0.1
+ for m in alphaTestModes :
+ last = r.getAttribute( "gl:alphaTest:value" )
+ r.setAttribute( "gl:alphaTest:mode", StringData( m ) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest:mode" ), StringData( m
) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest:value" ), last )
+
+ last = r.getAttribute( "gl:alphaTest:mode" )
+ r.setAttribute( "gl:alphaTest:value", FloatData( value ) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest:value" ), FloatData(
value ) )
+ self.assertEqual( r.getAttribute( "gl:alphaTest:mode" ), last )
+
+ value += 0.05
+
r.setAttribute( "gl:visibility:camera", BoolData( False ) )
self.assertEqual( r.getAttribute( "gl:visibility:camera" ), BoolData(
False ) )

Reply all
Reply to author
Forward
0 new messages