Skip to content

Commit

Permalink
Fixed GL shading of polygon meshes with "N" primvar.
Browse files Browse the repository at this point in the history
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 GafferHQ/gaffer#110.
  • Loading branch information
johnhaddon committed Nov 25, 2013
1 parent bed7bef commit 1f713b2
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 45 deletions.
45 changes: 45 additions & 0 deletions include/IECoreGL/bindings/MeshPrimitiveBinding.h
Original file line number Diff line number Diff line change
@@ -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
32 changes: 18 additions & 14 deletions src/IECoreGL/MeshPrimitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<Imath::V3f> &p = points->readable();
for( unsigned int i=0; i<p.size(); i++ )
{
const std::vector<Imath::V3f> &p = points->readable();
for( unsigned int i=0; i<p.size(); i++ )
{
m_memberData->bound.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 );
Expand Down
58 changes: 27 additions & 31 deletions src/IECoreGL/ToGLMeshConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -64,41 +65,36 @@ ToGLMeshConverter::~ToGLMeshConverter()
IECore::RunTimeTypedPtr ToGLMeshConverter::doConversion( IECore::ConstObjectPtr src, IECore::ConstCompoundObjectPtr operands ) const
{
IECore::MeshPrimitivePtr mesh = IECore::staticPointerCast<IECore::MeshPrimitive>( src->copy() ); // safe because the parameter validated it for us

if( !mesh->variableData<IECore::V3fVectorData>( "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<const IECore::V3fVectorData>( 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() );

Expand Down
2 changes: 2 additions & 0 deletions src/IECoreGL/bindings/IECoreGLBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -107,6 +108,7 @@ BOOST_PYTHON_MODULE( _IECoreGL )
bindToGLTextureConverter();
bindPrimitive();
bindPointsPrimitive();
bindMeshPrimitive();
bindSelector();
bindToGLMeshConverter();
bindToGLPointsConverter();
Expand Down
53 changes: 53 additions & 0 deletions src/IECoreGL/bindings/MeshPrimitiveBinding.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/python.hpp>

#include "IECorePython/RunTimeTypedBinding.h"

#include "IECoreGL/MeshPrimitive.h"
#include "IECoreGL/bindings/MeshPrimitiveBinding.h"

using namespace boost::python;

namespace IECoreGL
{

void bindMeshPrimitive()
{
IECorePython::RunTimeTypedClass<MeshPrimitive>()
;
}

} // namespace IECoreGL
121 changes: 121 additions & 0 deletions test/IECoreGL/MeshPrimitiveTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) :
Expand Down

0 comments on commit 1f713b2

Please sign in to comment.