Revision c38d81dd src/spin/ImageTexture.cpp

b/src/spin/ImageTexture.cpp
65 65
	classType = "ImageTexture";
66 66

  
67 67
	_path = "NULL";
68
	_useTextureRectangle = false;
68
	textureMode_ = TEXTURE_2D;
69 69
}
70 70

  
71 71
// destructor
......
92 92
// *****************************************************************************
93 93
void ImageTexture::setPath (const char* newPath)
94 94
{
95
	// only do this if the id has changed:
96
	if (_path == std::string(newPath)) return;
97

  
98
	_path = std::string(newPath);
99
	
100
	if (sceneManager->isGraphical())
101
	{
102

  
103
		std::string fullPath = getAbsolutePath(_path);
104

  
105
		std::cout << "Loading image: " << fullPath << std::endl;
106

  
107
		//osg::setNotifyLevel(osg::DEBUG_FP);
108
		osg::ref_ptr<osg::Image> test = osgDB::readImageFile(fullPath);
109
		//osg::setNotifyLevel(osg::FATAL);
110
		
111
		if (test.valid())
112
		{
113
			this->setName("ImageTexture("+_path+")");
114

  
115
			// create a texture object
116
			osg::Texture *tex;
117
			if (_useTextureRectangle)
118
			{
119
				tex = new osg::TextureRectangle;
120
			} else {
121
				tex = new osg::Texture2D;
122
			}
123
		
124
			tex->setResizeNonPowerOfTwoHint(false);
125
			tex->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
126
			//tex->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
127
			//tex->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT);
128
			if (textureRepeatS_)
129
				tex->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
130
			else
131
				tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
132
			if (textureRepeatT_)
133
				tex->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
134
			else
135
				tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
136

  
137
			// set the image:
138
			tex->setImage(0,test.get());
139
			
140
			// set transparent border:
141
			tex->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.0f));
142
		
143
			// add texture to stateset:
144
			this->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
145

  
146
			// osg::TexEnv::REPLACE  osg::TexEnv::DECAL  osg::TexEnv::MODULATE osg::TexEnv::BLEND
147
			osg::TexEnv* texenv = new osg::TexEnv();
148
			texenv->setMode(textureBlend_);
149
			this->setTextureAttribute(0, texenv, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
150
	
151
			// set lighting:
152
			if (lightingEnabled_) this->setMode( GL_LIGHTING, osg::StateAttribute::ON );
153
			else this->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
154

  
155
			// set renderbin:
156
			this->setRenderBinDetails( renderBin_, "RenderBin");
157

  
158
			// if image has transparency, enable blending:
159
			if (1)//(_imageStream->isImageTranslucent())
160
			{
161
				this->setMode(GL_BLEND, osg::StateAttribute::ON);
162
				this->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
163
			} else
164
            {
165
                this->setMode(GL_BLEND, osg::StateAttribute::OFF);
166
				this->setRenderingHint(osg::StateSet::DEFAULT_BIN);
167
            }
168
		
169
		}
95
	if (_path != std::string(newPath))
96
    {
97
        _path = std::string(newPath);
98
        if (sceneManager->isGraphical()) this->draw();
99
        BROADCAST(this, "ss", "setPath", getPath());
100
    }
101
}
170 102

  
171
		else {
172
			std::cout << "ImageTexture ERROR. Bad format?: " << _path << std::endl;
173
		}
174
	}
103
void ImageTexture::setTextureMode(TextureMode mode)
104
{
105
    if (this->textureMode_ != (int)mode)
106
    {
107
        this->textureMode_ = mode;
108
        if (sceneManager->isGraphical()) this->draw();
109
        BROADCAST(this, "si", "setTextureMode", (int) this->textureMode_);
110
    }
111
}
175 112

  
176
	BROADCAST(this, "ss", "setPath", getPath());
113
void ImageTexture::draw()
114
{
115
    std::string fullPath = getAbsolutePath(_path);
116
    std::cout << "Loading image: " << fullPath << std::endl;
117

  
118
    //osg::setNotifyLevel(osg::DEBUG_FP);
119
    osg::ref_ptr<osg::Image> test = osgDB::readImageFile(fullPath);
120
    //osg::setNotifyLevel(osg::FATAL);
121
    
122
    if (test.valid())
123
    {
124
        this->setName("ImageTexture("+_path+")");
125

  
126
        // create a texture object
127
        osg::Texture *tex;
128
        if (textureMode_==TEXTURE_RECTANGLE)
129
        {
130
            tex = new osg::TextureRectangle;
131
        } else {
132
            tex = new osg::Texture2D;
133
        }
134
    
135
        tex->setResizeNonPowerOfTwoHint(false);
136
        tex->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
137
        //tex->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
138
        //tex->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT);
139
        if (textureRepeatS_)
140
            tex->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
141
        else
142
            tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
143
        if (textureRepeatT_)
144
            tex->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
145
        else
146
            tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
147

  
148
        // set the image:
149
        tex->setImage(0,test.get());
150
        
151
        // set transparent border:
152
        tex->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.0f));
153
    
154
        // add texture to stateset:
155
        this->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
156

  
157
        // osg::TexEnv::REPLACE  osg::TexEnv::DECAL  osg::TexEnv::MODULATE osg::TexEnv::BLEND
158
        osg::TexEnv* texenv = new osg::TexEnv();
159
        texenv->setMode(textureBlend_);
160
        this->setTextureAttribute(0, texenv, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
161

  
162
        // set lighting:
163
        if (lightingEnabled_) this->setMode( GL_LIGHTING, osg::StateAttribute::ON );
164
        else this->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
165

  
166
        // set renderbin:
167
        this->setRenderBinDetails( renderBin_, "RenderBin");
168

  
169
        // if image has transparency, enable blending:
170
        if (1)//(_imageStream->isImageTranslucent())
171
        {
172
            this->setMode(GL_BLEND, osg::StateAttribute::ON);
173
            this->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
174
        } else
175
        {
176
            this->setMode(GL_BLEND, osg::StateAttribute::OFF);
177
            this->setRenderingHint(osg::StateSet::DEFAULT_BIN);
178
        }
179
    
180
    }
181

  
182
    else {
183
        std::cout << "ImageTexture ERROR. Bad format?: " << _path << std::endl;
184
    }
177 185
}
178 186

  
179 187

  
......
187 195
	lo_message msg;
188 196
	
189 197
	msg = lo_message_new();
198
	lo_message_add(msg, "si", "setTextureMode", getTextureMode());
199
	ret.push_back(msg);
200
	
201
	msg = lo_message_new();
190 202
	lo_message_add(msg, "ss", "setPath", getPath());
191 203
	ret.push_back(msg);
192 204
	

Also available in: Unified diff