63 #include "TextureNode.hh" 77 const std::string& _name,
79 GLint _texture_filter )
83 texture_repeat_( _texture_repeat ),
84 tex_mode_( GL_MODULATE ),
85 texture_filter_( _texture_filter ),
86 mipmapping_globally_active_(true),
87 last_mipmapping_status_(true),
90 open_volume_mesh_texture_draw_modes_(DrawModes::getDrawMode(
"Faces (textured)") | DrawModes::getDrawMode(
"Faces (textured and shaded)") )
102 for (std::vector<TextureInfo>::iterator texturesIt = textures_.begin(); texturesIt != textures_.end(); ++texturesIt) {
103 delete texturesIt->tex;
118 if ( !image.load( _filename ) )
120 std::cerr <<
"Cannot load texture " << _filename <<
"\n";
137 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
138 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
139 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
140 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
141 glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
142 glPixelStorei( GL_PACK_SKIP_ROWS, 0 );
143 glPixelStorei( GL_PACK_SKIP_PIXELS, 0 );
144 glPixelStorei( GL_PACK_ALIGNMENT, 1 );
150 if ( texture_repeat_ ) {
151 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
152 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
154 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
155 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
158 const float borderColor[4] = {1.0, 1.0, 1.0, 1.0};
159 glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
162 if((mipmapping_globally_active_ && mipmapping_ && textures_[_id].mipmapAvailable) &&
163 (texture_filter_ == GL_LINEAR_MIPMAP_NEAREST ||
164 texture_filter_ == GL_LINEAR_MIPMAP_LINEAR ||
165 texture_filter_ == GL_NEAREST_MIPMAP_LINEAR ||
166 texture_filter_ == GL_NEAREST_MIPMAP_NEAREST)) {
168 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
169 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture_filter_ );
170 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
172 }
else if(texture_filter_ == GL_LINEAR) {
174 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
175 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
178 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
179 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
206 void TextureNode::setTextureDataGL ( GLuint _textureId,
212 const void * _data) {
216 Texture2D* tex = textures_[_textureId].tex;
221 textures_[_textureId].mipmapAvailable =
true;
223 textures_[_textureId].mipmapAvailable =
false;
228 if ( mipmapping_globally_active_ && mipmapping_ )
229 tex->autogenerateMipMaps();
253 for(
unsigned int i = 1; i < textures_.size(); ++i) {
261 size_t bufferSize = textures_[i].tex->getWidth() * textures_[i].tex->getHeight()*4;
263 if(_mipmap && bufferSize) {
266 GLubyte* buffer =
new GLubyte[bufferSize];
268 tex->getData(0, buffer);
271 tex->autogenerateMipMaps();
272 tex->setData(0, tex->getInternalFormat(), tex->getWidth(), tex->getHeight(),
273 tex->getFormat(), tex->getType(), buffer);
279 textures_[i].mipmapAvailable = _mipmap;
295 if ( !Texture2D::checkTextureMem(GL_RGBA, _width, _height, GL_RGBA) ) {
296 std::cerr <<
"Can't load texture";
300 setTextureDataGL(
activeTexture_,GL_TEXTURE_2D,_width,_height,GL_RGBA,GL_UNSIGNED_BYTE,_image);
314 int tex_w, w( _image.width() );
315 int tex_h, h( _image.height() );
317 for (tex_w=1; tex_w < w; tex_w <<= 1) {};
318 for (tex_h=1; tex_h < h; tex_h <<= 1) {};
319 if (5 * tex_w > 7 *w)
321 if (5 * tex_h > 7 *h)
325 bool isNPOT = ( tex_w != w ) || ( tex_h != h );
334 textureGL = QGLWidget::convertToGLFormat ( _image.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
341 textureGL = QGLWidget::convertToGLFormat ( _image );
345 if ( !Texture2D::checkTextureMem(GL_RGBA, tex_w, tex_h, GL_RGBA) ) {
346 std::cerr <<
"Can't load texture TextureNode::set_texture" << std::endl;
351 setTextureDataGL(
activeTexture_ ,GL_TEXTURE_2D,tex_w,tex_h,GL_RGBA,GL_UNSIGNED_BYTE,textureGL.bits());
363 if ( !Texture2D::checkTextureMem(GL_RGBA, _width, _height, GL_RGBA) ) {
364 std::cerr <<
"Can't load texture TextureNode::set_texture" << std::endl;
369 setTextureDataGL(
activeTexture_,GL_TEXTURE_2D,_width,_height,GL_RGBA,GL_FLOAT,_image);
377 if ( textures_.empty() ) {
392 for ( uint i = 0 ; i < textures_.size(); ++i )
393 if ( textures_[i].tex->id() == _id )
404 return read(_filename);
406 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
425 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
442 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
459 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
473 textures_.resize(textures_.size()+1);
484 return textures_.back().tex->id();
517 if ( !textures_.empty() ) {
520 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_ );
580 if ( search == -1 ) {
581 std::cerr <<
"Texture to activate not found!" << std::endl;
TextureNode(BaseNode *_parent=0, const std::string &_name="<TextureNode>", bool _texture_repeat=true, GLint _texture_filter=GL_LINEAR)
Default constructor. Applies all properties.
static void bindTexture(GLenum _target, GLuint _buffer)
replaces glBindTexture, supports locking
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode)
Unbind Texture.
void disable_mipmapping()
Disable mipmapping.
bool read(const char *_filename)
Uses a QImage to load the texture from the given file.
void checkEmpty()
Check if a texture is already generated by this Node.
DrawMode SOLID_2DTEXTURED_FACE
draw per halfedge textured faces
DrawMode SOLID_2DTEXTURED_FACE_SHADED
draw per halfedge textured faces
void enterPick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode)
Do nothing in picking.
PickTarget
What target to use for picking.
void applyTextureParameters(int _id)
bool openGLVersion(const int _major, const int _minor)
DrawMode SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED
draw per halfedge texture faces modulated with face colors with smooth shading
bool activateTexture(GLuint _id)
Set active Texture.
static void enable(GLenum _cap)
replaces glEnable, but supports locking
DrawMode SOLID_TEXTURED
draw textured faces
DrawModes::DrawMode open_volume_mesh_texture_draw_modes_
OpenVolumeMesh DrawModes using textures.
static void disable(GLenum _cap)
replaces glDisable, but supports locking
GLuint activeTexture()
Get active Texture.
Namespace providing different geometric functions concerning angles.
void set_texture(const QImage &_image)
Uses a QImage to set the texture.
void updateMipmaps(bool _mipmap)
Build mip maps of textures that don't have one yet.
DrawMode SOLID_ENV_MAPPED
draw environment mapped
GLuint add_texture(const QImage &_image)
Add a texture to this node.
virtual ~TextureNode()
Destructor.
void enable_mipmapping()
Enable mipmapping.
DrawMode SOLID_TEXTURED_SHADED
draw smooth shaded textured faces
int activeTexture_
currently active texture
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode)
set default texture and states for the nodes children
int available(GLuint _id)
check this node for a texture
bool mipmapping_allowed() const
Get current global mipmapping state.
void leavePick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode)
Do nothing in picking.