Commit b46dd5d1 authored by Jan Möbius's avatar Jan Möbius

Combined support for single and multitexture modes

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@5720 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 4b52d635
......@@ -49,14 +49,11 @@
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension , int _id)
{
std::cerr << "slotLocalTextureAdded " << _textureName.toStdString() << std::endl;
// TODO: Load texture?! ...
// Get the new object
BaseObjectData* obj;
if (! PluginFunctions::getObject( _id , obj ) ) {
emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
return;
}
// Get Texture data for this object or create one if it does not exist
......@@ -66,15 +63,41 @@ void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _fil
obj->setObjectData(TEXTUREDATA, texData);
}
// Add this texture to the list of global textures
if ( ! texData->textureExists(_textureName) ) {
texData->addTexture(_textureName,_filename,_dimension,0);
texData->texture(_textureName).enabled = false;
} else {
if ( texData->textureExists(_textureName) ) {
emit log(LOGERR,"Trying to add already existing texture " + _textureName + " for object " + QString::number(_id) );
return;
}
// ================================================================================
// Get the image file
// ================================================================================
QImage textureImage;
getImage(_filename,textureImage);
// ================================================================================
// Add the texture to the texture node and get the corresponding id
// ================================================================================
GLuint glName = 0;
//inform textureNode about the new texture
if( obj->dataType( DATA_TRIANGLE_MESH ) )
glName = PluginFunctions::triMeshObject(obj)->textureNode()->add_texture(textureImage);
if ( obj->dataType( DATA_POLY_MESH ) )
glName = PluginFunctions::polyMeshObject(obj)->textureNode()->add_texture(textureImage);
// ================================================================================
// Store texture information in objects metadata
// ================================================================================
if (glName == 0) {
emit log(LOGERR,"Unable to bind texture!");
return;
}
texData->addTexture(_textureName,_filename,_dimension,glName);
texData->texture(_textureName).enabled = false;
}
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension)
......@@ -84,19 +107,8 @@ void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _fil
globalTextures_.addTexture(_textureName,_filename,_dimension,0);
globalTextures_.texture(_textureName).enabled = false;
QString loadFilename;
if ( _filename.startsWith("/") )
loadFilename = _filename;
else
loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _filename;
QImage textureImage;
if ( !textureImage.load( loadFilename ) ){
emit log(LOGERR, "Cannot load texture " + _filename );
textureImage.load(OpenFlipper::Options::textureDirStr() + QDir::separator() + "unknown.png");
}
getImage(_filename,textureImage);
globalTextures_.texture(_textureName).textureImage = textureImage;
} else {
......@@ -116,7 +128,65 @@ void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _fil
}
void TextureControlPlugin::slotMultiTextureAdded( QString _textureGroup , QString _name , QString _filename , int _id , int& _textureId ) {
std::cerr << "slotMultiTextureAdded" << std::endl;
// Get the new object
BaseObjectData* obj;
if (! PluginFunctions::getObject( _id , obj ) ) {
emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
}
// Check if we support this kind of data
if ( !obj->dataType(DATA_TRIANGLE_MESH) && !obj->dataType(DATA_POLY_MESH) ) {
emit log(LOGERR,"Trying to add textures to object failed because of unsupported object type");
return;
}
// Get Texture data for this object or create one if it does not exist
TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
if (texData == 0){
texData = new TextureData();
obj->setObjectData(TEXTUREDATA, texData);
}
if ( !texData->textureExists( _textureGroup ) )
texData->addMultiTexture( _textureGroup );
// Add the texture
slotTextureAdded( _name , _filename , 2 , _id);
// Get the id of the new texture
_textureId = -1;
_textureId = texData->texture(_name).id;
QImage textureImage;
getImage(_filename,textureImage);
texData->texture(_name).textureImage = textureImage;
// Store the new texture in the list of this textureGroup
if ( _textureId != -1 ) {
texData->texture(_textureGroup).multiTextureList << _name ;
} else {
emit log(LOGERR,"Error when getting internal id of new multitexture!");
}
}
void TextureControlPlugin::getImage( QString& _fileName, QImage& _image ) {
QString loadFilename;
if ( _fileName.startsWith("/") )
loadFilename = _fileName;
else
loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;
if ( !_image.load( loadFilename ) ){
emit log(LOGERR, "Cannot load texture " + _fileName + " at : " + loadFilename);
_image.load(OpenFlipper::Options::textureDirStr() + QDir::separator() + "unknown.png");
}
}
void TextureControlPlugin::addedEmptyObject( int _id ) {
fileOpened(_id);
}
void TextureControlPlugin::fileOpened( int _id ) {
......@@ -147,20 +217,9 @@ void TextureControlPlugin::fileOpened( int _id ) {
// ================================================================================
// Get the image file
// ================================================================================
// TODO: support arbitrary paths!
QString loadFilename;
if ( globalTextures_.textures()[i].filename.startsWith("/") )
loadFilename = globalTextures_.textures()[i].filename;
else
loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + globalTextures_.textures()[i].filename;
QImage textureImage;
if ( !textureImage.load( loadFilename ) ){
emit log(LOGERR, "Cannot load texture " + globalTextures_.textures()[i].filename );
textureImage.load(OpenFlipper::Options::textureDirStr() + QDir::separator() + "unknown.png");
}
getImage(globalTextures_.textures()[i].filename,textureImage);
// ================================================================================
// Add the texture to the texture node and get the corresponding id
......@@ -192,19 +251,13 @@ void TextureControlPlugin::fileOpened( int _id ) {
// Update texture mapping in meshNode
// ================================================================================
if( obj->dataType( DATA_TRIANGLE_MESH ) ){
// PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
// PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
// TODO: Multitexturing has to set the right map here!
}
if ( obj->dataType( DATA_POLY_MESH ) ){
PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
// TODO: Multitexturing has to set the right map here!
}
}
......@@ -230,21 +283,25 @@ void TextureControlPlugin::slotTextureUpdated( QString _textureName , int _ident
return;
}
// ================================================================================
// Check if texture exists
// ================================================================================
if ( ! texData->textureExists(_textureName) ) {
std::cerr << "Texture not found on object" << std::endl;
emit log(LOGERR,"Texture " + _textureName + " not found on object " + QString::number(_identifier) );
return;
}
// ================================================================================
// If texture is not enabled, mark it as dirty and defer update to visualization update
// ================================================================================
if ( ! texData->texture(_textureName).enabled ) {
texData->texture(_textureName).dirty = true;
std::cerr << "Texture is not active on object" << std::endl;
return;
}
// ================================================================================
// As the current texture is active, update it
// ================================================================================
if( obj->dataType( DATA_TRIANGLE_MESH ) ) {
TriMesh* mesh = PluginFunctions::triMesh(obj);
doUpdateTexture(texData->texture(_textureName), *mesh);
......@@ -255,10 +312,15 @@ void TextureControlPlugin::slotTextureUpdated( QString _textureName , int _ident
PluginFunctions::polyMeshObject(obj)->textureNode()->set_repeat(texData->texture(_textureName).parameters.repeat);
}
// Mark the texture as not dirty
// ================================================================================
// Mark texture as not dirty
// ================================================================================
texData->texture(_textureName).dirty = false;
emit updateView();
// ================================================================================
// Enable the right draw mode and update
// ================================================================================
switchDrawMode(false);
}
template< typename MeshT >
......@@ -771,9 +833,6 @@ void TextureControlPlugin::slotTextureMenu(QAction* _action) {
void TextureControlPlugin::doSwitchTexture( QString _textureName , int _id ) {
std::cerr << "TextureControlPlugin::doSwitchTexture : " << _textureName.toStdString();
std::cerr << " object=" << _id << std::endl;
// Get the new object
BaseObjectData* obj;
if (! PluginFunctions::getObject( _id , obj ) ) {
......@@ -790,20 +849,42 @@ void TextureControlPlugin::doSwitchTexture( QString _textureName , int _id ) {
}
// ================================================================================
// Enable the given texture exclusively
// Check for requested Texture
// ================================================================================
if ( texData->enableTexture( _textureName , true ) ) {
std::cerr << "Enabled Texture " << _textureName.toStdString() << std::endl;
} else {
std::cerr << "Failed to enabled Texture" << std::endl;
if ( !texData->textureExists(_textureName) ) {
emit log(LOGERR, "Texture not available! " + _textureName );
return;
}
// ================================================================================
// Enable the given texture exclusively or use multitexture setting
// ================================================================================
bool multiTextureMode = ( texData->texture(_textureName).type == MULTITEXTURE );
if ( !multiTextureMode ) {
if ( ! texData->enableTexture( _textureName , true ) ) {
emit log(LOGERR, "Failed to enabled Texture " + _textureName );
return;
}
} else {
// get the list of textures for this mode
QStringList textureList = texData->texture(_textureName).multiTextureList;
for ( uint i = 0 ; i < texData->textures().size() ; ++i ) {
if ( textureList.contains( texData->textures()[i].name ) )
texData->enableTexture( texData->textures()[i].name , false );
else
texData->disableTexture( texData->textures()[i].name );
}
std::cerr << "Trying to enable multitexture" << std::endl;
}
// ================================================================================
// If texture is flagged dirty, update it ( this jumps to texture updated
// which will update the visualization )
// ================================================================================
if ( texData->texture( _textureName).dirty ) {
if ( !multiTextureMode && texData->texture( _textureName).dirty ) {
// TODO: maybe introduce lock to prevent extra redraws if updating all objects
emit updateTexture( texData->texture( _textureName ).name , obj->id() );
return;
......@@ -813,92 +894,73 @@ void TextureControlPlugin::doSwitchTexture( QString _textureName , int _id ) {
// Update texture map from meshNode and activate it
// ================================================================================
if( obj->dataType( DATA_TRIANGLE_MESH ) ){
doUpdateTexture(texData->texture(_textureName), *PluginFunctions::triMeshObject(obj)->mesh());
PluginFunctions::triMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
// PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
// PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
// TODO: Multitexturing has to set the right map here!
if (!multiTextureMode) {
doUpdateTexture(texData->texture(_textureName), *PluginFunctions::triMeshObject(obj)->mesh());
PluginFunctions::triMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
} else {
PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
}
}
if ( obj->dataType( DATA_POLY_MESH ) ){
doUpdateTexture(texData->texture(_textureName), *PluginFunctions::polyMeshObject(obj)->mesh());
PluginFunctions::polyMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
// PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
// PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
// TODO: Multitexturing has to set the right map here!
if (!multiTextureMode) {
doUpdateTexture(texData->texture(_textureName), *PluginFunctions::polyMeshObject(obj)->mesh());
PluginFunctions::polyMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
} else {
PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
}
}
}
void TextureControlPlugin::slotSwitchTexture( QString _textureName , int _id ) {
std::cerr << "TextureControlPlugin::slotSwitchTexture : " << _textureName.toStdString();
std::cerr << "object=" << _id << std::endl;
doSwitchTexture(_textureName, _id);
// ================================================================================
// Switch to a texture drawMode
// ================================================================================
switchDrawMode(multiTextureMode);
}
void TextureControlPlugin::switchDrawMode( bool _multiTexture ) {
bool textureMode = false;
for ( int j = 0 ; j < PluginFunctions::viewers() ; ++j ) {
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED );
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
if ( _multiTexture ) {
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_2DTEXTURED_FACE );
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_2DTEXTURED_FACE_SHADED );
} else {
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED );
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
}
}
if ( !textureMode )
PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
if ( !textureMode ) {
if ( _multiTexture )
PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_2DTEXTURED_FACE_SHADED );
else
PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
}
emit updateView();
}
// TODO: Update menu
// QList<QAction *> menuEntries = actionGroup_->actions();
//
// for ( int i = 0 ; i < menuEntries.size(); ++i ) {
// if ( menuEntries[i]->text() == _textureName )
// menuEntries[i]->setChecked(true);
// }
void TextureControlPlugin::slotSwitchTexture( QString _textureName , int _id ) {
// updateDialog();
doSwitchTexture(_textureName, _id);
}
void TextureControlPlugin::slotSwitchTexture( QString _textureName ) {
std::cerr << "TextureControlPlugin::slotSwitchTexture : " << _textureName.toStdString() << std::endl;
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
o_it != PluginFunctions::objectsEnd();
++o_it)
doSwitchTexture(_textureName, o_it->id() );
// ================================================================================
// Switch to a texture drawMode
// ================================================================================
bool textureMode = false;
for ( int j = 0 ; j < PluginFunctions::viewers() ; ++j ) {
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED );
textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
}
if ( !textureMode )
PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
emit updateView();
// TODO: Update menu
// QList<QAction *> menuEntries = actionGroup_->actions();
//
// for ( int i = 0 ; i < menuEntries.size(); ++i ) {
// if ( menuEntries[i]->text() == _textureName )
// menuEntries[i]->setChecked(true);
// }
// updateDialog();
}
......
......@@ -129,8 +129,7 @@ class TextureControlPlugin : public QObject, BaseInterface, TextureInterface, Me
// LoadSaveInterface
void fileOpened( int _id );
// TODO: Empty object added has to create textures as well!
void addedEmptyObject( int _id );
private slots:
......@@ -155,6 +154,12 @@ class TextureControlPlugin : public QObject, BaseInterface, TextureInterface, Me
bool StringToBool(QString _value);
/// Load an Image from disk ( if filename contains leading "/ " its consedered global otherwise in texturedir)
void getImage( QString& _fileName, QImage& _image );
/// Checks for a correct drawmode and changes if necessary
void switchDrawMode( bool _multiTexture );
QAction* AC_Texture_Settings;
TextureData globalTextures_;
......
......@@ -33,8 +33,12 @@
//-----------------------------------------------------------------------------------
TextureData::TextureData(){
nextInternalID_ = 0;
TextureData::TextureData() :
nextInternalID_(0)
{
// map 0 to no texture
textureMap_[0] = 0;
propertyMap_[0] = "No Texture";;
}
//-----------------------------------------------------------------------------------
......@@ -125,14 +129,14 @@ int TextureData::addTexture(QString _textureName, QString _filename, uint _dimen
{
//generate texture object
Texture tex;
tex.id = nextInternalID_++;
tex.name = _textureName;
tex.glName = _glName;
tex.filename = _filename;
tex.dimension = _dimension;
tex.enabled = true;
tex.dirty = false;
tex.type = VERTEXBASED;
tex.id = nextInternalID_++;
tex.name = _textureName;
tex.glName = _glName;
tex.filename = _filename;
tex.dimension = _dimension;
tex.enabled = true;
tex.dirty = false;
tex.type = VERTEXBASED;
// tex.parameters = TexParameters;
textures_.push_back( tex );
......@@ -151,10 +155,36 @@ int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
textureMap_[ _texture.id ] = _texture.glName;
propertyMap_[ _texture.id ] = _texture.name.toStdString();
std::cerr << "Added texture " << _texture.name.toStdString() << std::endl;
return _texture.id;
}
bool TextureData::addMultiTexture( QString _textureName ) {
int textureid = -1;
textureid = getTextureIndex(_textureName);
if ( textureid != -1) {
std::cerr << "Texture exists!" << std::endl;
return false;
}
std::cerr << "TextureData::addMultiTexture" << std::endl;
//generate texture object
Texture tex;
tex.id = nextInternalID_++;
tex.name = _textureName;
tex.glName = 0;
tex.filename = "MultiTexture";
tex.dimension = 0;
tex.enabled = false;
tex.dirty = false;
tex.type = MULTITEXTURE;
textures_.push_back( tex );
return true;
}
/// Stores the given image in the texture information
bool TextureData::setImage( QString _textureName , QImage& _image ) {
int textureid = -1;
......
......@@ -35,6 +35,7 @@
#include <OpenFlipper/common/perObjectData.hh>
#include <QString>
#include <QStringList>
#include <QImage>
#include <ACG/GL/gl.hh>
#include <vector>
......@@ -42,7 +43,7 @@
#include <float.h>
#include <iostream>
enum TextureType { VERTEXBASED = 1 << 0, HALFEDGEBASED = 1 << 1};
enum TextureType { VERTEXBASED = 1 << 0, HALFEDGEBASED = 1 << 1, MULTITEXTURE = 1 << 2};
class TexParameters
{
......@@ -55,7 +56,7 @@ class TexParameters
repeat ( false ),
center ( false ),
abs ( false ),
max_val ( 1.0 ) {std::cerr << "Constructor for parameters" << std::endl;};
max_val ( 1.0 ) {};
bool scale;
......@@ -93,6 +94,10 @@ struct Texture {
TexParameters parameters;
QImage textureImage;
/// If this is a multiTexture, the list will contain all textures for this multi Texture node.
QStringList multiTextureList;
};
class TextureData : public PerObjectData
......@@ -126,6 +131,9 @@ class TextureData : public PerObjectData
/// Add a Texture ( Based on an existing specification )
int addTexture ( Texture _texture, GLuint _glName );
/// Adds a new multiTexture ( This texture will only contain a list of enabled textures for multitexturing )
bool addMultiTexture( QString _textureName );
/// Stores the given image in the texture information
bool setImage( QString _textureName , QImage& _image );
......@@ -147,27 +155,32 @@ class TextureData : public PerObjectData
std::vector< Texture >& textures();
/// Get reference to the textureMap
/** This map maps all available textures for the object which this
* class belongs to to their GLuint. The MeshNode will use this
* map to activate one texture for each face.
*
*/
std::map< int, GLuint >* textureMap();
/// Get reference to the propertyMap
/** This map is used to store the available Textures and map them to their
* corresponding properties.
*/
std::map< int, std::string >* propertyMap();
private :
std::map< int, GLuint> textureMap_;
std::map< int, std::string> propertyMap_;
// internal id for the next texture
/// internal id for the next texture
int nextInternalID_;
//vector containing all textures of an object
/// vector containing all textures of an object
std::vector< Texture > textures_;
// Get the index of a given texture
/// Get the index of a given texture
int getTextureIndex(QString _textureName);
Texture noTexture;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment