|
| 1 | +////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) 2014, Image Engine Design Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer. |
| 11 | +// |
| 12 | +// * Redistributions in binary form must reproduce the above copyright |
| 13 | +// notice, this list of conditions and the following disclaimer in the |
| 14 | +// documentation and/or other materials provided with the distribution. |
| 15 | +// |
| 16 | +// * Neither the name of Image Engine Design nor the names of any |
| 17 | +// other contributors to this software may be used to endorse or |
| 18 | +// promote products derived from this software without specific prior |
| 19 | +// written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | +////////////////////////////////////////////////////////////////////////// |
| 34 | + |
| 35 | +#include "IECore/CompoundObject.h" |
| 36 | +#include "IECore/SimpleTypedData.h" |
| 37 | +#include "IECore/Shader.h" |
| 38 | +#include "IECore/ObjectVector.h" |
| 39 | + |
| 40 | +#include "IECoreGL/ToGLStateConverter.h" |
| 41 | +#include "IECoreGL/Primitive.h" |
| 42 | +#include "IECoreGL/State.h" |
| 43 | +#include "IECoreGL/PointsPrimitive.h" |
| 44 | +#include "IECoreGL/CurvesPrimitive.h" |
| 45 | +#include "IECoreGL/ShaderStateComponent.h" |
| 46 | +#include "IECoreGL/ShaderLoader.h" |
| 47 | +#include "IECoreGL/TextureLoader.h" |
| 48 | + |
| 49 | +using namespace IECore; |
| 50 | +using namespace IECoreGL; |
| 51 | + |
| 52 | +////////////////////////////////////////////////////////////////////////// |
| 53 | +// Individual state converters |
| 54 | +////////////////////////////////////////////////////////////////////////// |
| 55 | + |
| 56 | +namespace |
| 57 | +{ |
| 58 | + |
| 59 | +template<class T> |
| 60 | +StateComponentPtr attributeToTypedState( const IECore::Object *attribute ) |
| 61 | +{ |
| 62 | + typedef IECore::TypedData<typename T::ValueType> DataType; |
| 63 | + const DataType *d = runTimeCast<const DataType>( attribute ); |
| 64 | + if( !d ) |
| 65 | + { |
| 66 | + throw Exception( boost::str( boost::format( "Expected data of type \"%s\"" ) % DataType::staticTypeName() ) ); |
| 67 | + } |
| 68 | + |
| 69 | + return new T( d->readable() ); |
| 70 | +} |
| 71 | + |
| 72 | +StateComponentPtr attributeToUseGLPointsState( const IECore::Object *attribute ) |
| 73 | +{ |
| 74 | + const StringData *d = runTimeCast<const StringData>( attribute ); |
| 75 | + if( !d ) |
| 76 | + { |
| 77 | + throw Exception( "Expected data of type StringData" ); |
| 78 | + } |
| 79 | + |
| 80 | + GLPointsUsage u; |
| 81 | + const std::string &v = d->readable(); |
| 82 | + if( v=="forGLPoints" ) |
| 83 | + { |
| 84 | + u = ForPointsOnly; |
| 85 | + } |
| 86 | + else if( v=="forParticlesAndDisks" ) |
| 87 | + { |
| 88 | + u = ForPointsAndDisks; |
| 89 | + } |
| 90 | + else if( v=="forAll" ) |
| 91 | + { |
| 92 | + u = ForAll; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + throw IECore::Exception( boost::str( boost::format( "Unsupported value \"%s\"." ) % v ) ); |
| 97 | + } |
| 98 | + |
| 99 | + return new PointsPrimitive::UseGLPoints( u ); |
| 100 | +} |
| 101 | + |
| 102 | +StateComponentPtr attributeToShaderState( const IECore::Object *attribute ) |
| 103 | +{ |
| 104 | + const IECore::Shader *shader = runTimeCast<const IECore::Shader>( attribute ); |
| 105 | + if( !shader ) |
| 106 | + { |
| 107 | + const ObjectVector *o = runTimeCast<const ObjectVector>( attribute ); |
| 108 | + if( o && o->members().size() ) |
| 109 | + { |
| 110 | + shader = runTimeCast<IECore::Shader>( o->members()[0].get() ); |
| 111 | + } |
| 112 | + } |
| 113 | + if( !shader ) |
| 114 | + { |
| 115 | + throw Exception( "Expected a Shader" ); |
| 116 | + } |
| 117 | + |
| 118 | + const StringData *vertexSourceData = shader->parametersData()->member<StringData>( "gl:vertexSource" ); |
| 119 | + const StringData *geometrySourceData = shader->parametersData()->member<StringData>( "gl:geometrySource" ); |
| 120 | + const StringData *fragmentSourceData = shader->parametersData()->member<StringData>( "gl:fragmentSource" ); |
| 121 | + |
| 122 | + std::string vertexSource = vertexSourceData ? vertexSourceData->readable() : ""; |
| 123 | + std::string geometrySource = geometrySourceData ? geometrySourceData->readable() : ""; |
| 124 | + std::string fragmentSource = fragmentSourceData ? fragmentSourceData->readable() : ""; |
| 125 | + |
| 126 | + if( vertexSource == "" && geometrySource == "" && fragmentSource == "" ) |
| 127 | + { |
| 128 | + ShaderLoader::defaultShaderLoader()->loadSource( shader->getName(), vertexSource, geometrySource, fragmentSource ); |
| 129 | + } |
| 130 | + |
| 131 | + CompoundObjectPtr parametersData = new CompoundObject; |
| 132 | + for( CompoundDataMap::const_iterator it=shader->parameters().begin(); it!=shader->parameters().end(); it++ ) |
| 133 | + { |
| 134 | + if( it->first!="gl:fragmentSource" && it->first!="gl:geometrySource" && it->first!="gl:vertexSource" ) |
| 135 | + { |
| 136 | + parametersData->members()[it->first] = it->second; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return new ShaderStateComponent( ShaderLoader::defaultShaderLoader(), TextureLoader::defaultTextureLoader(), vertexSource, geometrySource, fragmentSource, parametersData ); |
| 141 | +} |
| 142 | + |
| 143 | +typedef StateComponentPtr (*AttributeToState)( const IECore::Object *attribute ); |
| 144 | +typedef std::map<IECore::InternedString, AttributeToState> AttributeToStateMap; |
| 145 | + |
| 146 | +const AttributeToStateMap &attributeToStateMap() |
| 147 | +{ |
| 148 | + static AttributeToStateMap m; |
| 149 | + if( !m.size() ) |
| 150 | + { |
| 151 | + m["gl:primitive:wireframe"] = attributeToTypedState<IECoreGL::Primitive::DrawWireframe>; |
| 152 | + m["gl:primitive:wireframeWidth"] = attributeToTypedState<IECoreGL::Primitive::WireframeWidth>; |
| 153 | + m["gl:primitive:bound"] = attributeToTypedState<IECoreGL::Primitive::DrawBound>; |
| 154 | + m["gl:primitive:solid"] = attributeToTypedState<IECoreGL::Primitive::DrawSolid>; |
| 155 | + m["gl:primitive:outline"] = attributeToTypedState<IECoreGL::Primitive::DrawOutline>; |
| 156 | + m["gl:primitive:outlineWidth"] = attributeToTypedState<IECoreGL::Primitive::OutlineWidth>; |
| 157 | + m["gl:primitive:points"] = attributeToTypedState<IECoreGL::Primitive::DrawPoints>; |
| 158 | + m["gl:primitive:pointWidth"] = attributeToTypedState<IECoreGL::Primitive::PointWidth>; |
| 159 | + m["gl:primitive:wireframeColor"] = attributeToTypedState<WireframeColorStateComponent>; |
| 160 | + m["gl:primitive:boundColor"] = attributeToTypedState<BoundColorStateComponent>; |
| 161 | + m["gl:primitive:outlineColor"] = attributeToTypedState<OutlineColorStateComponent>; |
| 162 | + m["gl:primitive:pointColor"] = attributeToTypedState<PointColorStateComponent>; |
| 163 | + m["gl:pointsPrimitive:useGLPoints"] = attributeToUseGLPointsState; |
| 164 | + m["gl:pointsPrimitive:glPointWidth"] = attributeToTypedState<IECoreGL::PointsPrimitive::GLPointWidth>; |
| 165 | + m["doubleSided"] = attributeToTypedState<DoubleSidedStateComponent>; |
| 166 | + m["gl:curvesPrimitive:useGLLines"] = attributeToTypedState<IECoreGL::CurvesPrimitive::UseGLLines>; |
| 167 | + m["gl:curvesPrimitive:glLineWidth"] = attributeToTypedState<IECoreGL::CurvesPrimitive::GLLineWidth>; |
| 168 | + m["gl:curvesPrimitive:ignoreBasis"] = attributeToTypedState<IECoreGL::CurvesPrimitive::IgnoreBasis>; |
| 169 | + m["gl:smoothing:points"] = attributeToTypedState<PointSmoothingStateComponent>; |
| 170 | + m["gl:smoothing:lines"] = attributeToTypedState<LineSmoothingStateComponent>; |
| 171 | + m["gl:smoothing:polygons"] = attributeToTypedState<PolygonSmoothingStateComponent>; |
| 172 | + m["gl:surface"] = attributeToShaderState; |
| 173 | + } |
| 174 | + return m; |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace |
| 178 | + |
| 179 | +////////////////////////////////////////////////////////////////////////// |
| 180 | +// ToGLStateConverter implementation |
| 181 | +////////////////////////////////////////////////////////////////////////// |
| 182 | + |
| 183 | +IE_CORE_DEFINERUNTIMETYPED( ToGLStateConverter ); |
| 184 | + |
| 185 | +ToGLConverter::ConverterDescription<ToGLStateConverter> ToGLStateConverter::g_description; |
| 186 | + |
| 187 | +ToGLStateConverter::ToGLStateConverter( IECore::ConstCompoundObjectPtr toConvert ) |
| 188 | + : ToGLConverter( "Converts IECore::CompoundObject objects to IECoreGL::State objects.", IECore::CompoundObjectTypeId ) |
| 189 | +{ |
| 190 | + srcParameter()->setValue( boost::const_pointer_cast<IECore::CompoundObject>( toConvert ) ); |
| 191 | +} |
| 192 | + |
| 193 | +ToGLStateConverter::~ToGLStateConverter() |
| 194 | +{ |
| 195 | +} |
| 196 | + |
| 197 | +IECore::RunTimeTypedPtr ToGLStateConverter::doConversion( IECore::ConstObjectPtr src, IECore::ConstCompoundObjectPtr operands ) const |
| 198 | +{ |
| 199 | + const CompoundObject *co = runTimeCast<const CompoundObject>( src.get() ); |
| 200 | + if( !co ) |
| 201 | + { |
| 202 | + throw Exception( "Expected a CompoundObject" ); |
| 203 | + } |
| 204 | + |
| 205 | + const AttributeToStateMap &m = attributeToStateMap(); |
| 206 | + |
| 207 | + const StatePtr result = new State( false ); |
| 208 | + for( CompoundObject::ObjectMap::const_iterator it = co->members().begin(), eIt = co->members().end(); it != eIt; ++it ) |
| 209 | + { |
| 210 | + AttributeToStateMap::const_iterator mIt = m.find( it->first ); |
| 211 | + if( mIt != m.end() ) |
| 212 | + { |
| 213 | + StateComponentPtr s = mIt->second( it->second.get() ); |
| 214 | + result->add( s ); |
| 215 | + } |
| 216 | + } |
| 217 | + return result; |
| 218 | +} |
0 commit comments