root / src / spin / ImageTexture.cpp @ c38d81dd
History | View | Annotate | Download (6.6 kB)
| 1 |
// -----------------------------------------------------------------------------
|
|---|---|
| 2 |
// | ___ ___ _ _ _ ___ _ |
|
| 3 |
// | / __>| . \| || \ | | __>_ _ ___ ._ _ _ ___ _ _ _ ___ _ _ | |__ |
|
| 4 |
// | \__ \| _/| || | | _>| '_><_> || ' ' |/ ._>| | | |/ . \| '_>| / / |
|
| 5 |
// | <___/|_| |_||_\_| |_| |_| <___||_|_|_|\___.|__/_/ \___/|_| |_\_\ |
|
| 6 |
// | |
|
| 7 |
// |---------------------------------------------------------------------------|
|
| 8 |
//
|
| 9 |
// http://spinframework.sourceforge.net
|
| 10 |
// Copyright (C) 2009 Mike Wozniewski, Zack Settel
|
| 11 |
//
|
| 12 |
// Developed/Maintained by:
|
| 13 |
// Mike Wozniewski (http://www.mikewoz.com)
|
| 14 |
// Zack Settel (http://www.sheefa.net/zack)
|
| 15 |
//
|
| 16 |
// Principle Partners:
|
| 17 |
// Shared Reality Lab, McGill University (http://www.cim.mcgill.ca/sre)
|
| 18 |
// La Societe des Arts Technologiques (http://www.sat.qc.ca)
|
| 19 |
//
|
| 20 |
// Funding by:
|
| 21 |
// NSERC/Canada Council for the Arts - New Media Initiative
|
| 22 |
// Heritage Canada
|
| 23 |
// Ministere du Developpement economique, de l'Innovation et de l'Exportation
|
| 24 |
//
|
| 25 |
// -----------------------------------------------------------------------------
|
| 26 |
// This file is part of the SPIN Framework.
|
| 27 |
//
|
| 28 |
// SPIN Framework is free software: you can redistribute it and/or modify
|
| 29 |
// it under the terms of the GNU Lesser General Public License as published by
|
| 30 |
// the Free Software Foundation, either version 3 of the License, or
|
| 31 |
// (at your option) any later version.
|
| 32 |
//
|
| 33 |
// SPIN Framework is distributed in the hope that it will be useful,
|
| 34 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 35 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 36 |
// GNU Lesser General Public License for more details.
|
| 37 |
//
|
| 38 |
// You should have received a copy of the GNU Lesser General Public License
|
| 39 |
// along with SPIN Framework. If not, see <http://www.gnu.org/licenses/>.
|
| 40 |
// -----------------------------------------------------------------------------
|
| 41 |
|
| 42 |
#include <osg/StateSet> |
| 43 |
#include <osg/StateAttribute> |
| 44 |
#include <osg/TextureRectangle> |
| 45 |
#include <osg/Texture2D> |
| 46 |
#include <osg/TexEnv> |
| 47 |
#include <osgDB/ReadFile> |
| 48 |
#include <osgDB/FileUtils> |
| 49 |
#include <osgDB/FileNameUtils> |
| 50 |
|
| 51 |
#include <iostream> |
| 52 |
|
| 53 |
#include "SceneManager.h" |
| 54 |
#include "ImageTexture.h" |
| 55 |
#include "spinApp.h" |
| 56 |
#include "spinBaseContext.h" |
| 57 |
|
| 58 |
namespace spin
|
| 59 |
{
|
| 60 |
|
| 61 |
// *****************************************************************************
|
| 62 |
// constructor:
|
| 63 |
ImageTexture::ImageTexture (SceneManager *s, const char *initID) : Shader(s, initID) |
| 64 |
{
|
| 65 |
classType = "ImageTexture";
|
| 66 |
|
| 67 |
_path = "NULL";
|
| 68 |
textureMode_ = TEXTURE_2D; |
| 69 |
} |
| 70 |
|
| 71 |
// destructor
|
| 72 |
ImageTexture::~ImageTexture() |
| 73 |
{
|
| 74 |
} |
| 75 |
|
| 76 |
// *****************************************************************************
|
| 77 |
|
| 78 |
void ImageTexture::debug()
|
| 79 |
{
|
| 80 |
Shader::debug(); |
| 81 |
|
| 82 |
std::cout << " ---------" << std::endl;
|
| 83 |
|
| 84 |
// additional info
|
| 85 |
if (_image.valid())
|
| 86 |
{
|
| 87 |
std::cout << " Transparency? " << _image->isImageTranslucent() << std::endl;
|
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
// *****************************************************************************
|
| 93 |
void ImageTexture::setPath (const char* newPath) |
| 94 |
{
|
| 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 |
} |
| 102 |
|
| 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 |
} |
| 112 |
|
| 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 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
// *****************************************************************************
|
| 190 |
std::vector<lo_message> ImageTexture::getState () const
|
| 191 |
{
|
| 192 |
// inherit state from base class
|
| 193 |
std::vector<lo_message> ret = Shader::getState(); |
| 194 |
|
| 195 |
lo_message msg; |
| 196 |
|
| 197 |
msg = lo_message_new(); |
| 198 |
lo_message_add(msg, "si", "setTextureMode", getTextureMode()); |
| 199 |
ret.push_back(msg); |
| 200 |
|
| 201 |
msg = lo_message_new(); |
| 202 |
lo_message_add(msg, "ss", "setPath", getPath()); |
| 203 |
ret.push_back(msg); |
| 204 |
|
| 205 |
return ret;
|
| 206 |
} |
| 207 |
|
| 208 |
bool ImageTexture::isValid() const { return (_image.valid()); } |
| 209 |
|
| 210 |
} // end of namespace spin
|
| 211 |
|
