#include "GL_ImageTexture.h" #include "X3D_ImageTexture.h" #include "Image.h" #include using namespace std; namespace X3DTK { namespace GL { std::map ImageTexture::_texNameMap; ImageTexture::ImageTexture() : X3DTexture2DNode(), _texName(0) { define(Recorder::getTypeName("ImageTexture", "Texturing")); _textureTransform[0] = 1.0f; _textureTransform[1] = 0.0f; _textureTransform[2] = 0.0f; _textureTransform[3] = 0.0f; _textureTransform[4] = 0.0f; _textureTransform[5] = 1.0f; _textureTransform[6] = 0.0f; _textureTransform[7] = 0.0f; _textureTransform[8] = 0.0f; _textureTransform[9] = 0.0f; _textureTransform[10] = 1.0f; _textureTransform[11] = 0.0f; _textureTransform[12] = 0.0f; _textureTransform[13] = 0.0f; _textureTransform[14] = 0.0f; _textureTransform[15] = 1.0f; } ImageTexture::~ImageTexture() { if (x3dReference == 0) return; X3D::ImageTexture *I = static_cast(x3dReference); MFString url = I->getUrl(); if (url.empty()) return; SFString firstUrl(SFString(url.front())); if (_texNameMap.find(firstUrl) != _texNameMap.end()) { TexInfo &info = _texNameMap[firstUrl]; --info.refCount; if (info.refCount == 0) { glDeleteTextures(1, &_texName); _texNameMap.erase(firstUrl); } } } void ImageTexture::update() { if (x3dReference == 0) return; X3D::ImageTexture *I = static_cast(x3dReference); MFString url = I->getUrl(); if (url.empty()) return; SFString firstUrl(SFString(url.front())); if (_texNameMap.find(firstUrl) == _texNameMap.end()) { const char *file = firstUrl; Image texImage(file); if (!texImage.isLoaded()) { _texName = 0; cx3d << "cannot load the image " << file << "!" << endl; return; } texImage.resizeGL(_textureTransform); // GL commands glGenTextures(1, &_texName); TexInfo info; info.texName = _texName; info.textureTransform[0] = _textureTransform[0]; info.textureTransform[1] = _textureTransform[1]; info.textureTransform[2] = _textureTransform[2]; info.textureTransform[3] = _textureTransform[3]; info.textureTransform[4] = _textureTransform[4]; info.textureTransform[5] = _textureTransform[5]; info.textureTransform[6] = _textureTransform[6]; info.textureTransform[7] = _textureTransform[7]; info.textureTransform[8] = _textureTransform[8]; info.textureTransform[9] = _textureTransform[9]; info.textureTransform[10] = _textureTransform[10]; info.textureTransform[11] = _textureTransform[11]; info.textureTransform[12] = _textureTransform[12]; info.textureTransform[13] = _textureTransform[13]; info.textureTransform[14] = _textureTransform[14]; info.textureTransform[15] = _textureTransform[15]; info.refCount = 1; _texNameMap[firstUrl] = info; glBindTexture(GL_TEXTURE_2D, _texName); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); if (I->getRepeatS()) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); if (I->getRepeatT()) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texImage.getWidth(), texImage.getHeight(), 0, texImage.getGLFormat(), texImage.getGLType(), texImage.getData()); } else { TexInfo &info = _texNameMap[firstUrl]; ++info.refCount; _texName = info.texName; _textureTransform[0] = info.textureTransform[0]; _textureTransform[1] = info.textureTransform[1]; _textureTransform[2] = info.textureTransform[2]; _textureTransform[3] = info.textureTransform[3]; _textureTransform[4] = info.textureTransform[4]; _textureTransform[5] = info.textureTransform[5]; _textureTransform[6] = info.textureTransform[6]; _textureTransform[7] = info.textureTransform[7]; _textureTransform[8] = info.textureTransform[8]; _textureTransform[9] = info.textureTransform[9]; _textureTransform[10] = info.textureTransform[10]; _textureTransform[11] = info.textureTransform[11]; _textureTransform[12] = info.textureTransform[12]; _textureTransform[13] = info.textureTransform[13]; _textureTransform[14] = info.textureTransform[14]; _textureTransform[15] = info.textureTransform[15]; } } } }