void StandardLightVisualiser::addEnvLightVisualiser( GroupPtr &output, Color3f multiplier, const std::string &textureName )
{
    IECoreGL::GroupPtr sphereGroup = new IECoreGL::Group();

    Imath::M44f trans;
    trans.scale( V3f( 1, 1, -1 ) );
    trans.rotate( V3f( -0.5 * M_PI, -0.5 * M_PI, 0 ) );
    sphereGroup->setTransform( trans );

    IECoreGL::SpherePrimitivePtr sphere = new IECoreGL::SpherePrimitive();
    sphereGroup->addChild( sphere );

    IECore::CompoundObjectPtr parameters = new CompoundObject;
    parameters->members()["lightMultiplier"] = new Color3fData( multiplier );
    parameters->members()["previewOpacity"] = new FloatData( 1 );
    parameters->members()["mapSampler"] = new StringData( textureName );
    parameters->members()["defaultColor"] = new Color3fData( Color3f( textureName == "" ? 1.0f : 0.0f ) );
    sphereGroup->getState()->add(
        new IECoreGL::ShaderStateComponent( ShaderLoader::defaultShaderLoader(), TextureLoader::defaultTextureLoader(), IECoreGL::Shader::defaultVertexSource(), "", environmentLightDrawFragSource(), parameters )
    );
    sphereGroup->getState()->add(
        new IECoreGL::DoubleSidedStateComponent( false )
    );

    output->addChild( sphereGroup );
}
Esempio n. 2
0
Imath::M44f Transform::computeProcessedTransform( const ScenePath &path, const Gaffer::Context *context, const Imath::M44f &inputTransform ) const
{
	const Space space = static_cast<Space>( spacePlug()->getValue() );
	const Imath::M44f matrix = transformPlug()->matrix();

	switch( space )
	{
		case Local :
			return matrix * inputTransform;
		case Parent :
			return inputTransform * matrix;
		case World :
		{
			bool matchingAncestorFound = false;
			const Imath::M44f parentMatrix = relativeParentTransform( path, context, matchingAncestorFound );
			if( matchingAncestorFound )
			{
				// The ancestor will have the relative world matrix applied,
				// and we will inherit it, so we don't need to do anything at all.
				return inputTransform;
			}
			else
			{
				// We're all on our own, so we invert the parent
				// matrix to get back to world space, apply the matrix
				// we want to get a new world space, and then reapply
				// all of our original transform.
				return inputTransform * parentMatrix * matrix * parentMatrix.inverse();
			}
		}
		case ResetLocal :
			return matrix;
		case ResetWorld :
		{
			bool matchingAncestorFound = false;
			const Imath::M44f parentMatrix = relativeParentTransform( path, context, matchingAncestorFound );
			if( matchingAncestorFound )
			{
				// We will be giving the ancestor the absolute
				// world space matrix we want, so we just have to
				// cancel out the relative matrix between us and
				// that ancestor.
				return parentMatrix.inverse();
			}
			else
			{
				// We're all on our own, so we invert the parent
				// matrix to get back to world space, and then
				// apply the world space matrix we want.
				return matrix * parentMatrix.inverse();
			}

		}
		default :
			// Should never get here.
			return Imath::M44f();
	}
}
void ofxAlembic::Reader::setTime(double time)
{
	if (!m_root) return;

	Imath::M44f m;
	m.makeIdentity();
	m_root->updateWithTime(time, m);

	current_time = time;
}
Esempio n. 4
0
void Viewport::roll(int deltaSide){
	if(deltaSide != 0){
	
		_roll += deltaSide;
	
		Imath::M44f mRoll;
		mRoll.setAxisAngle(Imath::V3f(0.f, 0.f, 1.f), (float)DEGTORAD(deltaSide));
	
		_modelMatrix.setValue(mRoll * _modelMatrix);
	}
}
void StandardLightVisualiser::addBasicLightVisualiser( ConstStringDataPtr type, GroupPtr &output, Color3f multiplier, float coneAngle, float penumbraAngle, const std::string *penumbraType, float lensRadius )
{
    bool indicatorFaceCamera = false;
    if( !type || type->readable() == "point" )
    {
        output->addChild( const_pointer_cast<IECoreGL::Renderable>( pointRays() ) );
        indicatorFaceCamera = true;
    }
    else if( type->readable() == "spot" )
    {
        float innerAngle = 0;
        float outerAngle = 0;

        if( !penumbraType || *penumbraType == "inset" )
        {
            outerAngle = coneAngle;
            innerAngle = coneAngle - 2.0f * penumbraAngle;
        }
        else if( *penumbraType == "outset" )
        {
            outerAngle = coneAngle + 2.0f * penumbraAngle;
            innerAngle = coneAngle ;
        }
        else if( *penumbraType == "absolute" )
        {
            outerAngle = coneAngle;
            innerAngle = penumbraAngle;
        }

        output->addChild( const_pointer_cast<IECoreGL::Renderable>( spotlightCone( innerAngle, outerAngle, lensRadius ) ) );
        output->addChild( const_pointer_cast<IECoreGL::Renderable>( ray() ) );
    }
    else if( type->readable() == "distant" )
    {
        for ( int i = 0; i < 3; i++ )
        {
            IECoreGL::GroupPtr rayGroup = new IECoreGL::Group();

            Imath::M44f trans;
            trans.rotate( V3f( 0, 0, 2.0 * M_PI / 3.0 * i ) );
            trans.translate( V3f( 0, 0.4, 0.5 ) );
            rayGroup->addChild( const_pointer_cast<IECoreGL::Renderable>( ray() ) );
            rayGroup->setTransform( trans );

            output->addChild( rayGroup );
        }
    }

    output->addChild( const_pointer_cast<IECoreGL::Renderable>( colorIndicator( multiplier, indicatorFaceCamera ) ) );
}
Esempio n. 6
0
Imath::M44f AimConstraint::computeConstraint( const Imath::M44f &fullTargetTransform, const Imath::M44f &fullInputTransform ) const
{
	// decompose into scale, shear, rotate and translate
	V3f s, h, r, t;
	extractSHRT( fullInputTransform, s, h, r, t );

	// figure out the aim matrix
	const V3f toDir = ( fullTargetTransform.translation() - t ).normalized();
	const M44f rotationMatrix = rotationMatrixWithUpDir( aimPlug()->getValue(), toDir, upPlug()->getValue() );

	// rebuild, replacing rotate with the aim matrix
	M44f result;
	result.translate( t );
	result.shear( h );
	result = rotationMatrix * result;
	result.scale( s );

	return result;
}
void ofxAlembic::IXform::updateWithTimeInternal(double time, Imath::M44f& transform)
{
	ISampleSelector ss(time, ISampleSelector::kNearIndex);

	M44f mat;
	M44d m = m_xform.getSchema().getValue(ss).getMatrix();
	double *src = m.getValue();
	float *dst = mat.getValue();

	for (int i = 0; i < 16; i++)
		dst[i] = src[i];

	transform = mat * transform;
	
	for (int i = 0; i < 16; i++)
	{
		xform.local_matrix.getPtr()[i] = mat.getValue()[i];
		xform.global_matrix.getPtr()[i] = transform.getValue()[i];
	}
}
Esempio n. 8
0
Imath::M44f PointConstraint::computeConstraint( const Imath::M44f &fullTargetTransform, const Imath::M44f &fullInputTransform ) const
{
	const V3f worldPosition = fullTargetTransform.translation() + offsetPlug()->getValue();
	M44f result = fullInputTransform;
	
	if( xEnabledPlug()->getValue() )
	{
		result[3][0] = worldPosition[0];
	}
	
	if( yEnabledPlug()->getValue() )
	{
		result[3][1] = worldPosition[1];
	}
	
	if( zEnabledPlug()->getValue() )
	{
		result[3][2] = worldPosition[2];
	}
	
	return result;
}
Esempio n. 9
0
void ViewportGadget::SelectionScope::begin( const ViewportGadget *viewportGadget, const Imath::Box2f &rasterRegion, const Imath::M44f &transform, IECoreGL::Selector::Mode mode )
{
	V2f viewport = viewportGadget->getViewport();
	Box2f ndcRegion( rasterRegion.min / viewport, rasterRegion.max / viewport );
		
	IECoreGL::ToGLConverterPtr converter = new IECoreGL::ToGLCameraConverter(
 		const_cast<CameraController &>( viewportGadget->m_cameraController ).getCamera()
 	);
 	IECoreGL::CameraPtr camera = staticPointerCast<IECoreGL::Camera>( converter->convert() );
 	/// \todo It would be better to base this on whether we have a depth buffer or not, but
 	/// we don't have access to that information right now.
 	m_depthSort = camera->isInstanceOf( IECoreGL::PerspectiveCamera::staticTypeId() );
 	camera->render( 0 );
	
	glClearColor( 0.3f, 0.3f, 0.3f, 0.0f );
	glClearDepth( 1.0f );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	m_selector = SelectorPtr( new IECoreGL::Selector( ndcRegion, mode, m_selection ) );
	 
	glPushMatrix();
	glMultMatrixf( transform.getValue() );
}
Esempio n. 10
0
void SkeletonPrimitive::render( const State *state, IECore::TypeId style ) const
{
	Imath::V3f from_vec(0.0, 0.0, 1.0), up(0.0, 1.0, 0.0);
	JointPrimitive jointPrimitive( m_jointsRadius, 1.0 );

	// loop over global transforms
	for (unsigned int i=0; i<m_globalMatrices->readable().size(); i++)
	{
		Imath::M44f child_mtx;

		unsigned int numChildren = m_childrenIds[i].size();
		if (numChildren > 0)
		{
			for (unsigned int j=0; j< numChildren; j++ )
			{
				child_mtx = m_globalMatrices->readable()[ m_childrenIds[i][j] ];

				Imath::V3f aim_vec = child_mtx.translation() - m_globalMatrices->readable()[i].translation();
				float bone_length = aim_vec.length();
				jointPrimitive.setLength( bone_length );

				Imath::V3f up_vec = up*m_globalMatrices->readable()[i] - m_globalMatrices->readable()[i].translation();

				Imath::M44f bone_mtx = Imath::rotationMatrixWithUpDir( from_vec, aim_vec.normalize(), up_vec );
				Imath::M44f bone_offset_mtx;
				bone_offset_mtx.translate( m_globalMatrices->readable()[i].translation() );

				// draw the jointPrimitive
				glPushMatrix();
					glMultMatrixf( bone_offset_mtx.getValue() );
					glMultMatrixf( bone_mtx.getValue() );
					jointPrimitive.render( state, style );
				glPopMatrix();
			}
		}
		else
		{
			// a Null or Locator shape when the joint has no children
			glPushMatrix();
				Imath::M44f mat = m_globalMatrices->readable()[i];
				Imath::removeScaling(mat, false);
				glMultMatrixf( mat.getValue() );
				glBegin( GL_LINES );
					glVertex3f(-m_jointsRadius, 0.0, 0.0);
					glVertex3f( m_jointsRadius, 0.0, 0.0);
				glEnd();
				glBegin( GL_LINES );
					glVertex3f(0.0, -m_jointsRadius, 0.0);
					glVertex3f(0.0,  m_jointsRadius, 0.0);
				glEnd();
				glBegin( GL_LINES );
					glVertex3f(0.0, 0.0, -m_jointsRadius);
					glVertex3f(0.0, 0.0,  m_jointsRadius);
				glEnd();
			glPopMatrix();
		}

		if ( m_jointsAxis == true)
		{
			float l = m_jointsRadius*3.0;

			//// draw the axis lines for debug porpose /////
			glPushMatrix();
				Imath::M44f matNoSCale = m_globalMatrices->readable()[i];
				Imath::removeScaling(matNoSCale, false);
				glMultMatrixf( matNoSCale.getValue() );

				///// store the current color and lighting mode /////
				GLboolean light;
				float color[4];
				glGetBooleanv(GL_LIGHTING, &light);
				glGetFloatv(GL_CURRENT_COLOR, color);

				glDisable(GL_LIGHTING);
				glBegin( GL_LINES );
					glColor3ub(255, 0, 0);
					glVertex3f(0.0, 0.0, 0.0);
					glVertex3f(l, 0.0, 0.0);
				glEnd();
				glBegin( GL_LINES );
					glColor3ub(0, 255, 0);
					glVertex3f(0.0, 0.0, 0.0);
					glVertex3f(0.0, l, 0.0);
				glEnd();
				glBegin( GL_LINES );
					glColor3ub(0, 0, 255);
					glVertex3f(0.0, 0.0, 0.0);
					glVertex3f(0.0, 0.0, l);
				glEnd();

				//// restore the color and the lighting modo to their initial state /////
				glColor4f(color[0], color[1], color[2], color[3]);
				if (light==true) { glEnable(GL_LIGHTING); }
			glPopMatrix();
		}
	}
}
void ImmediateRendererImplementation::concatTransform( const Imath::M44f &matrix )
{
	glMultMatrixf( matrix.getValue() );
}
Esempio n. 12
0
Imath::M44f SceneNode::getGlobalTransFloat(double time)
{
   Imath::M44f ret;
   ret.makeIdentity();
   return ret;
}
Esempio n. 13
0
void Renderer::loadMesh(const std::string& file)
{
	Imath::M44f meshTransform;

#if VOXELIZE_GPU
	Mesh* mesh = MeshLoader::loadFromOBJ(file.c_str());

	if (mesh == NULL) return;

	createVoxelDataTexture(Imath::V3i(64));

	meshTransform = computeMeshTransform(mesh->bounds(), m_glResources.m_volumeResolution);
	GPUVoxelizer voxelizer(m_shaderPath, m_logger);
	voxelizer.voxelizeMesh(mesh, 
						   meshTransform, 
						   m_glResources.m_volumeResolution, 
						   GLResourceConfiguration::TEXTURE_UNIT_MATERIAL_OFFSET); 	

	delete(mesh);

#else 
	std::vector<float> vertices;
	std::vector<unsigned int> indices;
	MeshLoader::loadFromOBJ(file.c_str(), vertices, indices);
	Imath::Box3f bounds = computeBounds(&vertices[0], vertices.size() / 3);

	meshTransform = computeMeshTransform(bounds, m_glResources.m_volumeResolution);

	// FIXME: I must be having a mismatch in the way I upload the matrices to
	// GLSL -- this transpose should not be necessary if the above matrix is
	// valid for the GPU voxelization.
	meshTransform.transpose();

	Imath::V3f* verts = reinterpret_cast<Imath::V3f*>(&vertices[0]);
	for(size_t i = 0; i < vertices.size() / 3; ++i)
	{
		meshTransform.multVecMatrix(verts[i], verts[i]);
		// now transform the vertices from world space into voxel space, this would
		// be done by the vertex shader
		verts[i] *= m_glResources.m_volumeResolution;
	}

	const size_t numVoxels = (size_t)(m_glResources.m_volumeResolution.x * m_glResources.m_volumeResolution.y * m_glResources.m_volumeResolution.z);
	const size_t numTriangles = indices.size() / 3;
	GLint* materialOffsetTexels = (GLint*)malloc(numVoxels * sizeof(GLint));
	for(size_t i = 0; i < numVoxels; ++i) materialOffsetTexels[i] = -1;

	CPUVoxelizer::voxelizeMesh(verts, &indices[0], numTriangles, m_glResources.m_volumeResolution, materialOffsetTexels);

	createVoxelDataTexture(m_glResources.m_volumeResolution);
	glBindTexture(GL_TEXTURE_3D, m_glResources.m_materialOffsetTexture);
	glTexImage3D(GL_TEXTURE_3D,
				 0,
				 GL_R32I,
				 m_glResources.m_volumeResolution.x,
				 m_glResources.m_volumeResolution.y,
				 m_glResources.m_volumeResolution.z,
				 0,
				 GL_RED,
				 GL_INT,
				 materialOffsetTexels);
	free(materialOffsetTexels);
#endif

	resetRender();
}
IECoreGL::ConstRenderablePtr StandardLightVisualiser::visualise( const IECore::InternedString &attributeName, const IECore::ObjectVector *shaderVector, IECoreGL::ConstStatePtr &state ) const
{
    if( !shaderVector || shaderVector->members().size() == 0 )
    {
        return NULL;
    }

    IECore::InternedString metadataTarget;
    const IECore::CompoundData *shaderParameters = NULL;
    if( const IECore::Shader *shader = IECore::runTimeCast<const IECore::Shader>( shaderVector->members().back().get() ) )
    {
        metadataTarget = attributeName.string() + ":" + shader->getName();
        shaderParameters = shader->parametersData();
    }
    else if( const IECore::Light *light = IECore::runTimeCast<const IECore::Light>( shaderVector->members().back().get() ) )
    {
        /// \todo Remove once all Light node derived classes are
        /// creating only shaders.
        metadataTarget = attributeName.string() + ":" + light->getName();
        shaderParameters = light->parametersData().get();
    }

    if( !shaderParameters )
    {
        return NULL;
    }

    ConstStringDataPtr type = Metadata::value<StringData>( metadataTarget, "type" );
    ConstM44fDataPtr orientation = Metadata::value<M44fData>( metadataTarget, "visualiserOrientation" );

    const Color3f color = parameter<Color3f>( metadataTarget, shaderParameters, "colorParameter", Color3f( 1.0f ) );
    const float intensity = parameter<float>( metadataTarget, shaderParameters, "intensityParameter", 1 );
    const float exposure = parameter<float>( metadataTarget, shaderParameters, "exposureParameter", 0 );

    const Color3f finalColor = color * intensity * pow( 2.0f, exposure );

    if( type && type->readable() == "area" )
    {
        const std::string textureName = parameter<std::string>( metadataTarget, shaderParameters, "textureNameParameter", "" );
        const bool flipNormal = parameter<bool>( metadataTarget, shaderParameters, "flipNormalParameter", 0 );
        const bool doubleSided = parameter<bool>( metadataTarget, shaderParameters, "doubleSidedParameter", 0 );
        const bool sphericalProjection = parameter<bool>( metadataTarget, shaderParameters, "sphericalProjectionParameter", 0 );

        M44f projectionTransform = parameter<M44f>( metadataTarget, shaderParameters, "projectionTransformParameter", M44f() );
        const std::vector<float> projectionTransformVector = parameter<std::vector<float> >( metadataTarget, shaderParameters, "projectionTransformParameter", std::vector<float>() );
        if( projectionTransformVector.size() == 16 )
        {
            projectionTransform = M44f( (float(*)[4])(&projectionTransformVector[0]) );
        }
        addAreaLightVisualiser( state, finalColor, textureName, flipNormal, doubleSided,
                                sphericalProjection, projectionTransform );
        return NULL;
    }

    GroupPtr result = new Group;

    const float locatorScale = parameter<float>( metadataTarget, shaderParameters, "locatorScaleParameter", 1 );
    Imath::M44f topTrans;
    if( orientation )
    {
        topTrans = orientation->readable();
    }
    topTrans.scale( V3f( locatorScale ) );
    result->setTransform( topTrans );

    if( type && type->readable() == "environment" )
    {
        const std::string textureName = parameter<std::string>( metadataTarget, shaderParameters, "textureNameParameter", "" );
        addEnvLightVisualiser( result, finalColor, textureName );
    }
    else
    {
        float coneAngle = parameter<float>( metadataTarget, shaderParameters, "coneAngleParameter", 0.0f );
        float penumbraAngle = parameter<float>( metadataTarget, shaderParameters, "penumbraAngleParameter", 0.0f );

        if( ConstStringDataPtr angleUnit = Metadata::value<StringData>( metadataTarget, "angleUnit" ) )
        {
            if( angleUnit->readable() == "radians" )
            {
                coneAngle *= 180.0 / M_PI;
                penumbraAngle *= 180 / M_PI;
            }
        }

        const std::string *penumbraType = NULL;
        ConstStringDataPtr penumbraTypeData = Metadata::value<StringData>( metadataTarget, "penumbraType" );
        if( penumbraTypeData )
        {
            penumbraType = &penumbraTypeData->readable();
        }


        float lensRadius = 0.0f;
        if( parameter<bool>( metadataTarget, shaderParameters, "lensRadiusEnableParameter", true ) )
        {
            lensRadius = parameter<float>( metadataTarget, shaderParameters, "lensRadiusParameter", 0.0f );
        }

        addBasicLightVisualiser( type, result, finalColor, coneAngle, penumbraAngle, penumbraType, lensRadius / locatorScale );
    }
    return result;
}
bool RATDeepImageReader::open( bool throwOnFailure )
{
	if ( m_inputFile && fileName() == m_inputFileName )
	{
		// we already opened the right file successfully
		return true;
	}
	
	delete m_ratPixel;
	delete m_inputFile;
	m_inputFile = new IMG_DeepShadow;
	m_inputFileName = "";
	m_channelNames = "";
	m_ratPixel = 0;
	m_depthChannel = 0;
	m_opacityChannel = 0;
	m_colorChannel = 0;
	m_dataWindow.max.x = 0;
	m_dataWindow.max.y = 0;
	m_worldToCamera = Imath::M44f();
	m_worldToNDC = Imath::M44f();
	
	bool success = true;
	if ( m_inputFile->open( fileName().c_str() ) && m_inputFile->depthInterp() == IMG_COMPRESS_DISCRETE )
	{
		m_inputFileName = fileName();
		m_ratPixel = new IMG_DeepPixelReader( *m_inputFile );
		
		const IMG_DeepShadowChannel *channel = 0;
		unsigned numChannels = m_inputFile->getChannelCount();
		for ( unsigned c=0; c < numChannels; ++c )
		{
			channel = m_inputFile->getChannel( c );
			std::string name = channel->getName();

			if ( name == "Pz" )
			{
				m_depthChannel = channel;
			}
			else if ( name == "Of" )
			{
				m_opacityChannel = channel;
			}
			else if ( name == "C" )
			{
				m_colorChannel = channel;
			}
		}

		if ( m_colorChannel )
		{
			m_channelNames = "RGBA";
		}
		else
		{
			m_colorChannel = m_opacityChannel;
			m_channelNames = "A";
		}
		
		if ( !m_depthChannel || !m_opacityChannel || !m_colorChannel )
		{
			success = false;
		}
		
		m_inputFile->resolution( m_dataWindow.max.x, m_dataWindow.max.y );
		m_dataWindow.max.x -= 1;
		m_dataWindow.max.y -= 1;
		
		UT_Matrix4 wToC( 1 );
		m_inputFile->getWorldToCamera( wToC );
		m_worldToCamera = IECore::convert<Imath::M44f>( wToC );
		
		UT_Matrix4 wToNDC( 1 );
		m_inputFile->getWorldToNDC( wToNDC, true );
		
		// wToNDC has flipped values and a scaling issue related to the far clipping plane
		// applying a matrix that seems to fix the issues in several examples
		Imath::M44f fix;
		fix.makeIdentity();
		UT_Options *options = m_inputFile->getTBFOptions();
		if ( options->hasOption( "camera:clip" ) )
		{
			const UT_Vector2D clip = options->getOptionV2( "camera:clip" );
			fix[2][2] = clip[1];
			fix[3][3] = -1;
		}
		
		m_worldToNDC = IECore::convert<Imath::M44f>( wToNDC ) * fix;
	}
	else
	{
		success = false;
	}
	
	if ( !success )
	{
		delete m_ratPixel;
		delete m_inputFile;
		m_inputFile = 0;
		m_inputFileName = "";
		m_channelNames = "";
		m_ratPixel = 0;
		m_depthChannel = 0;
		m_opacityChannel = 0;
		m_colorChannel = 0;
		m_dataWindow.max.x = 0;
		m_dataWindow.max.y = 0;
		m_worldToCamera = Imath::M44f();
		m_worldToNDC = Imath::M44f();
		
		if ( !throwOnFailure )
		{
			return false;
		}
		else
		{
			throw IOException( std::string( "Failed to open file \"" ) + fileName() + "\"" );
		}
	}

	return true;
}