void ShaderParamTextureCubeMap::AttachToRenderState(osg::StateSet &stateSet)
   {
      dtCore::RefPtr<osg::TextureCubeMap> texCube;

      if (GetTexture().empty() && GetTextureSourceType() != ShaderParamTexture::TextureSourceType::AUTO)
      {
         throw dtUtil::Exception(ShaderParameterException::INVALID_ATTRIBUTE,"Cannot attach to render state.  Texture "
            "for parameter " + GetName() + " has not been specified.", __FILE__, __LINE__);
      }

      osg::Uniform *uniform = NULL;

      if (IsShared())
         uniform = GetUniformParam();
      // Create a new one if unshared or if shared but not set yet
      if (uniform == NULL)
      {
         uniform = new osg::Uniform(osg::Uniform::SAMPLER_CUBE,GetName());
         uniform->set((int)GetTextureUnit());
         SetUniformParam(*uniform);
      }

      stateSet.addUniform(uniform);

      // Load (if necessary) and Set the Tex cube map on the StateSet
      if(GetTextureSourceType() == ShaderParamTexture::TextureSourceType::CUBEMAP_IMAGE_POSITIVE_X ||
         GetTextureSourceType() == ShaderParamTexture::TextureSourceType::CUBEMAP_IMAGE_NEGATIVE_X ||
         GetTextureSourceType() == ShaderParamTexture::TextureSourceType::CUBEMAP_IMAGE_POSITIVE_Y ||
         GetTextureSourceType() == ShaderParamTexture::TextureSourceType::CUBEMAP_IMAGE_NEGATIVE_Y ||
         GetTextureSourceType() == ShaderParamTexture::TextureSourceType::CUBEMAP_IMAGE_POSITIVE_Z ||
         GetTextureSourceType() == ShaderParamTexture::TextureSourceType::CUBEMAP_IMAGE_NEGATIVE_Z
      )
      {
         texCube = static_cast<osg::TextureCubeMap*>(GetTextureObject());

         // load or reload the image - allows caching from the template
         // Note - ImageSourceDirty may not be relevant anymore cause it now loads the image when you call SetTexture().
         // note - If shared, load only happens the first time it is assigned.
         // check only if face 0 exists. Is this sufficient?
         if (texCube->getImage(osg::TextureCubeMap::POSITIVE_X) == NULL || IsImageSourceDirty())
         {
            LoadImage();
            ApplyTextureCubeMapValues();
         }

         //Assign the completed texture attribute to the render state.
         stateSet.setTextureAttributeAndModes(GetTextureUnit(), texCube.get(), osg::StateAttribute::ON);
      }

      SetDirty(false);
   }
示例#2
0
文件: test.cpp 项目: gisair/EAView
	void createShaders(osg::StateSet& ss)
	{
		osg::ref_ptr<osg::Shader> verShader=
			new osg::Shader(osg::Shader::VERTEX,vertSource);
		osg::ref_ptr<osg::Shader> fragShader=
			new osg::Shader(osg::Shader::FRAGMENT,fragSource);

		osg::ref_ptr<osg::Program> program=new osg::Program;
		program->addShader(verShader.get());
		program->addShader(fragShader.get());

		osg::ref_ptr<osg::Uniform> mainColor=
			new osg::Uniform("mainColor",osg::Vec4(1,0.5,0.5,1));
		mainColor->setUpdateCallback(new ColorCallback);

		ss.addUniform(mainColor.get());
		ss.setAttributeAndModes(program.get());
	}