From 1f713b203505c9bb4d99825420aa155836fd7930 Mon Sep 17 00:00:00 2001 From: John Haddon Date: Mon, 25 Nov 2013 15:45:28 +0000 Subject: [PATCH] Fixed GL shading of polygon meshes with "N" primvar. This is achieved by computing a Uniform "N" in the ToGLMeshConverter. Because up till now the GL MeshPrimitive didn't support Uniform data, the "N" is immediately converted to FaceVarying also in the ToGLMeshConverter. This new conversion makes the conversions taking place inside the IECoreGL::MeshPrimitive totally redundant unless someone is using the MeshPrimitive class directly without a converter. Note that the MeshPrimitive actually doesn't have enough information to support Uniform data directly because it doesn't have the faces prior to triangulation. For this reason, I think there's a very good case for ditching the internal conversions in MeshPrimitive, and making the MeshPrimitive constructor need only the number of triangles, and not the vertex ids (which are used purely for conversion). Fixes #118 and ImageEngine/gaffer#110. --- .../IECoreGL/bindings/MeshPrimitiveBinding.h | 45 +++++++ src/IECoreGL/MeshPrimitive.cpp | 32 +++-- src/IECoreGL/ToGLMeshConverter.cpp | 58 ++++----- src/IECoreGL/bindings/IECoreGLBinding.cpp | 2 + .../bindings/MeshPrimitiveBinding.cpp | 53 ++++++++ test/IECoreGL/MeshPrimitiveTest.py | 121 ++++++++++++++++++ 6 files changed, 266 insertions(+), 45 deletions(-) create mode 100644 include/IECoreGL/bindings/MeshPrimitiveBinding.h create mode 100644 src/IECoreGL/bindings/MeshPrimitiveBinding.cpp diff --git a/include/IECoreGL/bindings/MeshPrimitiveBinding.h b/include/IECoreGL/bindings/MeshPrimitiveBinding.h new file mode 100644 index 0000000000..d6b488c2f2 --- /dev/null +++ b/include/IECoreGL/bindings/MeshPrimitiveBinding.h @@ -0,0 +1,45 @@ +////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2013, Image Engine Design Inc. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of Image Engine Design nor the names of any +// other contributors to this software may be used to endorse or +// promote products derived from this software without specific prior +// written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////// + +#ifndef IECOREGL_MESHPRIMITIVEBINDING_H +#define IECOREGL_MESHPRIMITIVEBINDING_H + +namespace IECoreGL +{ + +void bindMeshPrimitive(); + +} + +#endif // IECOREGL_MESHPRIMITIVEBINDING_H diff --git a/src/IECoreGL/MeshPrimitive.cpp b/src/IECoreGL/MeshPrimitive.cpp index c9057f6bc8..a77c14a975 100644 --- a/src/IECoreGL/MeshPrimitive.cpp +++ b/src/IECoreGL/MeshPrimitive.cpp @@ -60,8 +60,12 @@ struct MeshPrimitive::MemberData : public IECore::RefCounted IECore::ConstIntVectorDataPtr vertIds; Imath::Box3f bound; - /// \todo This could be removed and the ToGLMeshConverter could use FaceVaryingPromotionOp - /// to convert everything to FaceVarying before being added. + /// \todo This could be removed now the ToGLMeshConverter uses FaceVaryingPromotionOp + /// to convert everything to FaceVarying before being added. The only reason we're even + /// doing this still is in case client code is creating MeshPrimitives directly rather + /// than using the converter. We should actually be able to remove the code here, and + /// instead of accept vertIds in the MeshPrimitive constructor, just accept the number + /// of triangles instead. class ToFaceVaryingConverter { public: @@ -118,23 +122,23 @@ IECore::ConstIntVectorDataPtr MeshPrimitive::vertexIds() const void MeshPrimitive::addPrimitiveVariable( const std::string &name, const IECore::PrimitiveVariable &primVar ) { - if ( primVar.interpolation==IECore::PrimitiveVariable::Vertex || primVar.interpolation==IECore::PrimitiveVariable::Varying ) + if( name == "P" ) { - if ( name == "P" ) + // update the bounding box. + m_memberData->bound.makeEmpty(); + IECore::ConstV3fVectorDataPtr points = IECore::runTimeCast< IECore::V3fVectorData >( primVar.data ); + if( points ) { - // update the bounding box. - m_memberData->bound.makeEmpty(); - IECore::ConstV3fVectorDataPtr points = IECore::runTimeCast< IECore::V3fVectorData >( primVar.data ); - if ( points ) + const std::vector &p = points->readable(); + for( unsigned int i=0; i &p = points->readable(); - for( unsigned int i=0; ibound.extendBy( p[i] ); - } + m_memberData->bound.extendBy( p[i] ); } } - + } + + if ( primVar.interpolation==IECore::PrimitiveVariable::Vertex || primVar.interpolation==IECore::PrimitiveVariable::Varying ) + { MemberData::ToFaceVaryingConverter primVarConverter( m_memberData->vertIds ); // convert to facevarying IECore::DataPtr newData = IECore::despatchTypedData< MemberData::ToFaceVaryingConverter, IECore::TypeTraits::IsVectorTypedData >( primVar.data, primVarConverter ); diff --git a/src/IECoreGL/ToGLMeshConverter.cpp b/src/IECoreGL/ToGLMeshConverter.cpp index 83f2b1b070..f1bd81ce8b 100644 --- a/src/IECoreGL/ToGLMeshConverter.cpp +++ b/src/IECoreGL/ToGLMeshConverter.cpp @@ -36,14 +36,15 @@ #include "boost/format.hpp" -#include "IECoreGL/ToGLMeshConverter.h" -#include "IECoreGL/MeshPrimitive.h" - #include "IECore/MeshPrimitive.h" #include "IECore/TriangulateOp.h" #include "IECore/MeshNormalsOp.h" #include "IECore/DespatchTypedData.h" #include "IECore/MessageHandler.h" +#include "IECore/FaceVaryingPromotionOp.h" + +#include "IECoreGL/ToGLMeshConverter.h" +#include "IECoreGL/MeshPrimitive.h" using namespace IECoreGL; @@ -64,41 +65,36 @@ ToGLMeshConverter::~ToGLMeshConverter() IECore::RunTimeTypedPtr ToGLMeshConverter::doConversion( IECore::ConstObjectPtr src, IECore::ConstCompoundObjectPtr operands ) const { IECore::MeshPrimitivePtr mesh = IECore::staticPointerCast( src->copy() ); // safe because the parameter validated it for us + + if( !mesh->variableData( "P", IECore::PrimitiveVariable::Vertex ) ) + { + throw IECore::Exception( "Must specify primitive variable \"P\", of type V3fVectorData and interpolation type Vertex." ); + } - if( mesh->interpolation() != "linear" ) + if( mesh->variables.find( "N" )==mesh->variables.end() ) { - // it's a subdivision mesh. in the absence of a nice subdivision algorithm to display things with, - // we can at least make things look a bit nicer by calculating some smooth shading normals. - // if interpolation is linear and no normals are provided then we assume the faceted look is intentional. - if( mesh->variables.find( "N" )==mesh->variables.end() ) - { - IECore::MeshNormalsOpPtr normalOp = new IECore::MeshNormalsOp(); - normalOp->inputParameter()->setValue( mesh ); - normalOp->copyParameter()->setTypedValue( false ); - normalOp->operate(); - } + // the mesh has no normals - we need to explicitly add some. if it's a polygon + // mesh (interpolation==linear) then we add per-face normals for a faceted look + // and if it's a subdivision mesh we add smooth per-vertex normals. + IECore::MeshNormalsOpPtr normalOp = new IECore::MeshNormalsOp(); + normalOp->inputParameter()->setValue( mesh ); + normalOp->copyParameter()->setTypedValue( false ); + normalOp->interpolationParameter()->setNumericValue( + mesh->interpolation() == "linear" ? IECore::PrimitiveVariable::Uniform : IECore::PrimitiveVariable::Vertex + ); + normalOp->operate(); } - + IECore::TriangulateOpPtr op = new IECore::TriangulateOp(); op->inputParameter()->setValue( mesh ); op->throwExceptionsParameter()->setTypedValue( false ); // it's better to see something than nothing + op->copyParameter()->setTypedValue( false ); + op->operate(); - mesh = IECore::runTimeCast< IECore::MeshPrimitive > ( op->operate() ); - assert( mesh ); - - IECore::ConstV3fVectorDataPtr p = 0; - IECore::PrimitiveVariableMap::const_iterator pIt = mesh->variables.find( "P" ); - if( pIt!=mesh->variables.end() ) - { - if( pIt->second.interpolation==IECore::PrimitiveVariable::Vertex ) - { - p = IECore::runTimeCast( pIt->second.data ); - } - } - if( !p ) - { - throw IECore::Exception( "Must specify primitive variable \"P\", of type V3fVectorData and interpolation type Vertex." ); - } + IECore::FaceVaryingPromotionOpPtr faceVaryingOp = new IECore::FaceVaryingPromotionOp; + faceVaryingOp->inputParameter()->setValue( mesh ); + faceVaryingOp->copyParameter()->setTypedValue( false ); + faceVaryingOp->operate(); MeshPrimitivePtr glMesh = new MeshPrimitive( mesh->vertexIds() ); diff --git a/src/IECoreGL/bindings/IECoreGLBinding.cpp b/src/IECoreGL/bindings/IECoreGLBinding.cpp index 8a115c51d3..9952d60b51 100644 --- a/src/IECoreGL/bindings/IECoreGLBinding.cpp +++ b/src/IECoreGL/bindings/IECoreGLBinding.cpp @@ -64,6 +64,7 @@ #include "IECoreGL/bindings/ToGLTextureConverterBinding.h" #include "IECoreGL/bindings/PrimitiveBinding.h" #include "IECoreGL/bindings/PointsPrimitiveBinding.h" +#include "IECoreGL/bindings/MeshPrimitiveBinding.h" #include "IECoreGL/bindings/SelectorBinding.h" #include "IECoreGL/bindings/FontBinding.h" #include "IECoreGL/bindings/FontLoaderBinding.h" @@ -107,6 +108,7 @@ BOOST_PYTHON_MODULE( _IECoreGL ) bindToGLTextureConverter(); bindPrimitive(); bindPointsPrimitive(); + bindMeshPrimitive(); bindSelector(); bindToGLMeshConverter(); bindToGLPointsConverter(); diff --git a/src/IECoreGL/bindings/MeshPrimitiveBinding.cpp b/src/IECoreGL/bindings/MeshPrimitiveBinding.cpp new file mode 100644 index 0000000000..d8022a7282 --- /dev/null +++ b/src/IECoreGL/bindings/MeshPrimitiveBinding.cpp @@ -0,0 +1,53 @@ +////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2013, Image Engine Design Inc. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of Image Engine Design nor the names of any +// other contributors to this software may be used to endorse or +// promote products derived from this software without specific prior +// written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////// + +#include + +#include "IECorePython/RunTimeTypedBinding.h" + +#include "IECoreGL/MeshPrimitive.h" +#include "IECoreGL/bindings/MeshPrimitiveBinding.h" + +using namespace boost::python; + +namespace IECoreGL +{ + +void bindMeshPrimitive() +{ + IECorePython::RunTimeTypedClass() + ; +} + +} // namespace IECoreGL diff --git a/test/IECoreGL/MeshPrimitiveTest.py b/test/IECoreGL/MeshPrimitiveTest.py index e1215adeda..5e6bbb12d6 100644 --- a/test/IECoreGL/MeshPrimitiveTest.py +++ b/test/IECoreGL/MeshPrimitiveTest.py @@ -106,6 +106,127 @@ def testVertexAttributes( self ) : self.assertEqual( IECore.ImageDiffOp()( imageA = expectedImage, imageB = actualImage, maxError = 0.05 ).value, False ) + def testUniformCs( self ) : + + fragmentSource = """ + #include "IECoreGL/FragmentShader.h" + + IECOREGL_FRAGMENTSHADER_IN vec3 fragmentCs; + + void main() + { + gl_FragColor = vec4( fragmentCs, 1.0 ); + } + """ + + r = IECoreGL.Renderer() + r.setOption( "gl:mode", IECore.StringData( "immediate" ) ) + + r.camera( "main", { + "projection" : IECore.StringData( "orthographic" ), + "resolution" : IECore.V2iData( IECore.V2i( 256 ) ), + "clippingPlanes" : IECore.V2fData( IECore.V2f( 1, 1000 ) ), + "screenWindow" : IECore.Box2fData( IECore.Box2f( IECore.V2f( -1 ), IECore.V2f( 1 ) ) ) + } + ) + r.display( self.outputFileName, "tif", "rgba", {} ) + + with IECore.WorldBlock( r ) : + + r.concatTransform( IECore.M44f.createTranslated( IECore.V3f( 0, 0, -15 ) ) ) + + r.shader( "surface", "test", { "gl:fragmentSource" : IECore.StringData( fragmentSource ) } ) + + m = IECore.MeshPrimitive.createPlane( IECore.Box2f( IECore.V2f( -1 ), IECore.V2f( 1 ) ), IECore.V2i( 2 ) ) + m["Cs"] = IECore.PrimitiveVariable( + IECore.PrimitiveVariable.Interpolation.Uniform, + IECore.Color3fVectorData( [ + IECore.Color3f( 1, 0, 0 ), + IECore.Color3f( 0, 1, 0 ), + IECore.Color3f( 0, 0, 1 ), + IECore.Color3f( 1, 1, 1, ), + ] ) + ) + + m.render( r ) + + image = IECore.Reader.create( self.outputFileName ).read() + e = IECore.ImagePrimitiveEvaluator( image ) + r = e.createResult() + + e.pointAtUV( IECore.V2f( 0.25, 0.75 ), r ) + self.assertEqual( r.floatPrimVar( image["R"] ), 1 ) + self.assertEqual( r.floatPrimVar( image["G"] ), 0 ) + self.assertEqual( r.floatPrimVar( image["B"] ), 0 ) + + e.pointAtUV( IECore.V2f( 0.75, 0.75 ), r ) + self.assertEqual( r.floatPrimVar( image["R"] ), 0 ) + self.assertEqual( r.floatPrimVar( image["G"] ), 1 ) + self.assertEqual( r.floatPrimVar( image["B"] ), 0 ) + + e.pointAtUV( IECore.V2f( 0.75, 0.25 ), r ) + self.assertEqual( r.floatPrimVar( image["R"] ), 1 ) + self.assertEqual( r.floatPrimVar( image["G"] ), 1 ) + self.assertEqual( r.floatPrimVar( image["B"] ), 1 ) + + e.pointAtUV( IECore.V2f( 0.25, 0.25 ), r ) + self.assertEqual( r.floatPrimVar( image["R"] ), 0 ) + self.assertEqual( r.floatPrimVar( image["G"] ), 0 ) + self.assertEqual( r.floatPrimVar( image["B"] ), 1 ) + + def testBound( self ) : + + m = IECore.MeshPrimitive.createPlane( IECore.Box2f( IECore.V2f( -0.5 ), IECore.V2f( 0.5 ) ) ) + m2 = IECoreGL.ToGLMeshConverter( m ).convert() + + self.assertEqual( m.bound(), m2.bound() ) + + def testFaceNormals( self ) : + + # when a polygon mesh has no normals, we must calculate face normals so we can + # shade it in a faceted manner. + + fragmentSource = """ + #include "IECoreGL/FragmentShader.h" + IECOREGL_FRAGMENTSHADER_IN vec3 fragmentN; + + void main() + { + gl_FragColor = vec4( fragmentN, 1.0 ); + } + """ + + r = IECoreGL.Renderer() + r.setOption( "gl:mode", IECore.StringData( "immediate" ) ) + + r.camera( "main", { + "projection" : IECore.StringData( "orthographic" ), + "resolution" : IECore.V2iData( IECore.V2i( 256 ) ), + "clippingPlanes" : IECore.V2fData( IECore.V2f( 1, 1000 ) ), + "screenWindow" : IECore.Box2fData( IECore.Box2f( IECore.V2f( -1 ), IECore.V2f( 1 ) ) ) + } + ) + r.display( self.outputFileName, "tif", "rgba", {} ) + + with IECore.WorldBlock( r ) : + + r.concatTransform( IECore.M44f.createTranslated( IECore.V3f( 0, 0, -15 ) ) ) + + r.shader( "surface", "test", { "gl:fragmentSource" : IECore.StringData( fragmentSource ) } ) + + m = IECore.MeshPrimitive.createPlane( IECore.Box2f( IECore.V2f( -0.5 ), IECore.V2f( 0.5 ) ) ) + self.assertTrue( "N" not in m ) + m.render( r ) + + image = IECore.Reader.create( self.outputFileName ).read() + e = IECore.ImagePrimitiveEvaluator( image ) + r = e.createResult() + + e.pointAtUV( IECore.V2f( 0.5, 0.5 ), r ) + self.assertEqual( r.floatPrimVar( image["R"] ), 0 ) + self.assertEqual( r.floatPrimVar( image["G"] ), 0 ) + self.assertEqual( r.floatPrimVar( image["B"] ), 1 ) + def setUp( self ) : if not os.path.isdir( "test/IECoreGL/output" ) :