Changeset 172

Show
Ignore:
Timestamp:
06/28/07 12:01:18 (1 year ago)
Author:
namespace
Message:

- Added material-support to brushexport-plugin (Shaderman)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • GtkRadiant/trunk/CHANGES

    r166 r172  
    11This is the changelog for developers, != changelog for the end user  
    22that we distribute with the binaries. (see changelog) 
     3 
     428/06/2007 
     5- Added material-support to brushexport-plugin (Shaderman) 
    36 
    4726/04/2007 
  • GtkRadiant/trunk/contrib/brushexport/callbacks.cpp

    r143 r172  
    2323        GtkWidget* window = lookup_widget(GTK_WIDGET(button), "w_plugplug2"); 
    2424        ASSERT_NOTNULL(window); 
    25         const char* path = GlobalRadiant().m_pfnFileDialog(window, false, "Save as Obj", 0, 0); 
    26         if(!path) 
     25        const char* cpath = GlobalRadiant().m_pfnFileDialog(window, false, "Save as Obj", 0, 0); 
     26        if(!cpath) 
    2727                return; 
    28    
     28 
     29        std::string path(cpath); 
     30 
    2931  // get ignore list from ui 
    3032  std::set<std::string> ignore; 
     
    7072        } 
    7173  } 
     74 
     75  // export materials? 
     76  GtkWidget* toggle = lookup_widget(GTK_WIDGET(button), "t_exportmaterials"); 
     77  ASSERT_NOTNULL(toggle); 
     78 
     79  bool exportmat = FALSE; 
     80 
     81  if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle))) 
     82          exportmat = TRUE; 
     83 
     84  // limit material names? 
     85  toggle = lookup_widget(GTK_WIDGET(button), "t_limitmatnames"); 
     86  ASSERT_NOTNULL(toggle); 
     87 
     88  bool limitMatNames = FALSE; 
     89 
     90  if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)) && exportmat) 
     91          limitMatNames = TRUE; 
     92 
     93  // create objects instead of groups? 
     94  toggle = lookup_widget(GTK_WIDGET(button), "t_objects"); 
     95  ASSERT_NOTNULL(toggle); 
     96 
     97  bool objects = FALSE; 
     98 
     99  if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle))  && exportmat) 
     100          objects = TRUE; 
    72101   
    73102  // export 
    74   ExportSelection(ignore, mode, path); 
     103  ExportSelection(ignore, mode, exportmat, path, limitMatNames, objects); 
    75104} 
    76105 
     
    102131} 
    103132 
     133void OnExportMatClicked(GtkButton* button, gpointer user_data) 
     134{ 
     135        GtkWidget* toggleLimit = lookup_widget(GTK_WIDGET(button), "t_limitmatnames"); 
     136        GtkWidget* toggleObject = lookup_widget(GTK_WIDGET(button), "t_objects"); 
     137 
     138        if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) 
     139        { 
     140                gtk_widget_set_sensitive(GTK_WIDGET(toggleLimit), TRUE); 
     141                gtk_widget_set_sensitive(GTK_WIDGET(toggleObject), TRUE); 
     142        } else { 
     143                gtk_widget_set_sensitive(GTK_WIDGET(toggleLimit), FALSE); 
     144                gtk_widget_set_sensitive(GTK_WIDGET(toggleObject), FALSE); 
     145        } 
     146} 
     147 
    104148}// callbacks 
  • GtkRadiant/trunk/contrib/brushexport/callbacks.h

    r128 r172  
    88void OnAddMaterial(GtkButton*, gpointer); 
    99void OnRemoveMaterial(GtkButton*, gpointer); 
     10void OnExportMatClicked(GtkButton* button, gpointer); 
    1011 
    1112}// callbacks 
  • GtkRadiant/trunk/contrib/brushexport/export.cpp

    r162 r172  
    77#include "stream/textfilestream.h" 
    88 
     9#include <map> 
     10 
    911// this is very evil, but right now there is no better way 
    1012#include "../../radiant/brush.h" 
     
    1820{ 
    1921public: 
    20         ExportData(const std::set<std::string>& ignorelist, collapsemode mode); 
     22        ExportData(const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs); 
    2123        virtual ~ExportData(void); 
    2224         
     
    2527        virtual void EndBrush(void); 
    2628         
    27         virtual bool WriteToFile(const std::string& path) const = 0; 
     29        virtual bool WriteToFile(const std::string& path, collapsemode mode) const = 0; 
    2830         
    2931protected: 
     
    4951}; 
    5052 
    51 ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode
     53ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs
    5254        :       mode(_mode), 
    5355                ignorelist(_ignorelist) 
     
    102104        GetShaderNameFromShaderPath(f.GetShader(), shadername); 
    103105         
    104         // faces mit materials auf der ignoreliste ignorieren 
     106        // ignore faces from ignore list 
    105107        if(ignorelist.find(shadername) != ignorelist.end()) 
    106108                return; 
     
    146148        else 
    147149                name = tmp.substr(last_slash + 1, tmp.length() - last_slash); 
    148                  
    149         globalOutputStream() << "Last: " << last_slash << " " << "lenght: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n"; 
     150 
     151#ifdef _DEBUG 
     152        globalOutputStream() << "Last: " << last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n"; 
     153#endif 
    150154} 
    151155 
     
    155159class ExportDataAsWavefront : public ExportData 
    156160{ 
     161private: 
     162        bool expmat; 
     163        bool limNames; 
     164        bool objs; 
     165 
    157166public: 
    158         ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode) 
    159                 : ExportData(_ignorelist, _mode) 
    160         { 
    161         } 
    162          
    163         bool WriteToFile(const std::string& path) const; 
     167        ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs) 
     168                : ExportData(_ignorelist, _mode, _limNames, _objs) 
     169        { 
     170                expmat = _expmat; 
     171                limNames = _limNames; 
     172                objs = _objs; 
     173        } 
     174         
     175        bool WriteToFile(const std::string& path, collapsemode mode) const; 
    164176}; 
    165177 
    166 bool ExportDataAsWavefront::WriteToFile(const std::string& path) const 
    167 
    168         TextFileOutputStream out(path.c_str()); 
     178bool ExportDataAsWavefront::WriteToFile(const std::string& path, collapsemode mode) const 
     179
     180        std::string objFile = path.substr(0, path.length() -4) + ".obj"; 
     181        std::string mtlFile = path.substr(0, path.length() -4) + ".mtl"; 
     182 
     183        std::set<std::string> materials; 
     184 
     185        TextFileOutputStream out(objFile.c_str()); 
    169186         
    170187        if(out.failed()) 
     
    174191        } 
    175192                 
    176         out << "# Wavefront Objectfile exported with radiants brushexport plugin 2.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n"; 
     193        out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n"; 
     194 
     195        if(expmat) 
     196        { 
     197                size_t last = mtlFile.find_last_of("//"); 
     198                std::string mtllib = mtlFile.substr(last + 1, mtlFile.size() - last).c_str(); 
     199                out << "mtllib " << mtllib.c_str() << "\n"; 
     200        } 
    177201         
    178202        unsigned int vertex_count = 0; 
     
    181205        for(std::list<ExportData::group>::const_iterator git(groups.begin()); git != gend; ++git) 
    182206        { 
     207                typedef std::multimap<std::string, std::string> bm; 
     208                bm brushMaterials; 
     209                typedef std::pair<std::string, std::string> String_Pair; 
     210 
    183211                const std::list<const Face*>::const_iterator end(git->faces.end()); 
    184212                 
    185213                // submesh starts here 
    186                 out << "\ng " << git->name.c_str() << "\n"; 
    187                  
     214                if(objs) 
     215                { 
     216                        out << "\no "; 
     217                } else { 
     218                        out << "\ng "; 
     219                } 
     220                out << git->name.c_str() << "\n"; 
     221 
     222                // material 
     223                if(expmat && mode == COLLAPSE_ALL) 
     224                { 
     225                        out << "usemtl material" << "\n\n"; 
     226                        materials.insert("material"); 
     227                } 
     228 
    188229                for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it) 
    189230                { 
     
    192233                        // vertices 
    193234                        for(size_t i = 0; i < w.numpoints; ++i) 
    194                                out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << "\n"; 
     235                                       out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << "\n"; 
    195236                } 
    196237                out << "\n";     
     
    210251                         
    211252                        // faces 
    212                         out << "\nf"; 
     253                        StringOutputStream faceLine(256); 
     254                        faceLine << "\nf"; 
    213255                        for(size_t i = 0; i < w.numpoints; ++i, ++vertex_count) 
    214256                        { 
    215                                 out << " " << vertex_count+1 << "/" << vertex_count+1; 
     257                                faceLine << " " << vertex_count+1 << "/" << vertex_count+1; 
    216258                        } 
    217                 } 
     259 
     260                        if(mode != COLLAPSE_ALL) 
     261                        { 
     262                                materials.insert((*it)->getShader().getShader()); 
     263                                brushMaterials.insert(String_Pair((*it)->getShader().getShader(), faceLine.c_str())); 
     264                        } else { 
     265                                out << faceLine.c_str(); 
     266                        } 
     267                } 
     268 
     269                if(mode != COLLAPSE_ALL) 
     270                { 
     271                        std::string lastMat; 
     272                        std::string mat; 
     273                        std::string faces; 
     274 
     275                        for(bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++) 
     276                        { 
     277                                mat = (*iter).first.c_str(); 
     278                                faces = (*iter).second.c_str(); 
     279 
     280                                if(mat != lastMat) 
     281                                { 
     282                                        if(limNames && mat.size() > 20) 
     283                                        { 
     284                                                out << "\nusemtl " << mat.substr(mat.size() - 20, mat.size()).c_str(); 
     285                                        } else { 
     286                                                out << "\nusemtl " << mat.c_str(); 
     287                                        } 
     288                                } 
     289 
     290                                out << faces.c_str(); 
     291                                lastMat = mat; 
     292                        } 
     293                } 
     294 
    218295                out << "\n"; 
     296        } 
     297 
     298        if(expmat) 
     299        { 
     300                TextFileOutputStream outMtl(mtlFile.c_str()); 
     301                if(outMtl.failed()) 
     302                { 
     303                        globalErrorStream() << "Unable to open material file\n"; 
     304                        return false; 
     305                } 
     306 
     307                outMtl << "# Wavefront material file exported with GtkRadiants brushexport plugin.\n"; 
     308                outMtl << "# Material Count: " << (const Unsigned)materials.size() << "\n\n"; 
     309                for(std::set<std::string>::const_iterator it(materials.begin()); it != materials.end(); ++it) 
     310                { 
     311                        if(limNames && it->size() > 20) 
     312                        { 
     313                                outMtl << "newmtl " << it->substr(it->size() - 20, it->size()).c_str() << "\n"; 
     314                        } else { 
     315                                outMtl << "newmtl " << it->c_str() << "\n"; 
     316                        } 
     317                } 
    219318        } 
    220319         
     
    264363}; 
    265364  
    266 bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, const std::string& path
    267 { 
    268         ExportDataAsWavefront exporter(ignorelist, m); 
     365bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limNames, bool objs
     366{ 
     367        ExportDataAsWavefront exporter(ignorelist, m, exmat, limNames, objs); 
    269368         
    270369        ForEachSelected vis(exporter); 
    271370        GlobalSelectionSystem().foreachSelected(vis); 
    272371         
    273         return exporter.WriteToFile(path); 
    274 } 
     372        return exporter.WriteToFile(path, m); 
     373} 
  • GtkRadiant/trunk/contrib/brushexport/export.h

    r128 r172  
    1111}; 
    1212 
    13 bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, const std::string& path); 
     13bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limitMatNames, bool objects); 
    1414 
    1515#endif  
  • GtkRadiant/trunk/contrib/brushexport/interface.cpp

    r128 r172  
    3636  GtkWidget *b_addmaterial; 
    3737  GtkWidget *b_removematerial; 
     38  GtkWidget *t_exportmaterials; 
     39  GtkWidget *t_limitmatnames; 
     40  GtkWidget *t_objects; 
     41  GtkTooltips *tooltips; 
     42 
     43  tooltips = gtk_tooltips_new(); 
    3844 
    3945  w_plugplug2 = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
    4046  gtk_widget_set_name (w_plugplug2, "w_plugplug2"); 
    41   gtk_window_set_title (GTK_WINDOW (w_plugplug2), "BrushExport-Plugin 2.0 by namespace"); 
     47  gtk_window_set_title (GTK_WINDOW (w_plugplug2), "BrushExport-Plugin 3.0 by namespace"); 
    4248  gtk_window_set_position (GTK_WINDOW (w_plugplug2), GTK_WIN_POS_CENTER); 
    4349  gtk_window_set_destroy_with_parent (GTK_WINDOW (w_plugplug2), TRUE); 
     
    6268  r_collapse = gtk_radio_button_new_with_mnemonic (NULL, "Collapse mesh"); 
    6369  gtk_widget_set_name (r_collapse, "r_collapse"); 
     70  gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), r_collapse, "Collapse all brushes into a single group", "Collapse all brushes into a single group"); 
    6471  gtk_widget_show (r_collapse); 
    6572  gtk_box_pack_start (GTK_BOX (vbox4), r_collapse, FALSE, FALSE, 0); 
     
    6976  r_collapsebymaterial = gtk_radio_button_new_with_mnemonic (NULL, "Collapse by material"); 
    7077  gtk_widget_set_name (r_collapsebymaterial, "r_collapsebymaterial"); 
     78  gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), r_collapsebymaterial, "Collapse into groups by material", "Collapse into groups by material"); 
    7179  gtk_widget_show (r_collapsebymaterial); 
    7280  gtk_box_pack_start (GTK_BOX (vbox4), r_collapsebymaterial, FALSE, FALSE, 0); 
     
    7684  r_nocollapse = gtk_radio_button_new_with_mnemonic (NULL, "Don't collapse"); 
    7785  gtk_widget_set_name (r_nocollapse, "r_nocollapse"); 
     86  gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), r_nocollapse, "Every brush is stored in its own group", "Every brush is stored in its own group"); 
    7887  gtk_widget_show (r_nocollapse); 
    7988  gtk_box_pack_start (GTK_BOX (vbox4), r_nocollapse, FALSE, FALSE, 0); 
     
    142151  gtk_widget_show (b_removematerial); 
    143152  gtk_box_pack_start (GTK_BOX (hbox1), b_removematerial, FALSE, FALSE, 0); 
     153 
     154  t_limitmatnames = gtk_check_button_new_with_mnemonic ("Use short material names (max. 20 chars)"); 
     155  gtk_widget_set_name (t_limitmatnames, "t_limitmatnames"); 
     156  gtk_widget_show (t_limitmatnames); 
     157  gtk_box_pack_end (GTK_BOX (vbox2), t_limitmatnames, FALSE, FALSE, 0); 
     158 
     159  t_objects = gtk_check_button_new_with_mnemonic ("Create (o)bjects instead of (g)roups"); 
     160  gtk_widget_set_name (t_objects, "t_objects"); 
     161  gtk_widget_show (t_objects); 
     162  gtk_box_pack_end (GTK_BOX (vbox2), t_objects, FALSE, FALSE, 0); 
     163 
     164  t_exportmaterials = gtk_check_button_new_with_mnemonic ("Create material information (.mtl file)"); 
     165  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(t_exportmaterials), true); 
     166  gtk_widget_set_name (t_exportmaterials, "t_exportmaterials"); 
     167  gtk_widget_show (t_exportmaterials); 
     168  gtk_box_pack_end (GTK_BOX (vbox2), t_exportmaterials, FALSE, FALSE, 10); 
    144169 
    145170  using namespace callbacks; 
     
    150175  g_signal_connect ((gpointer) b_addmaterial, "clicked", G_CALLBACK (OnAddMaterial), NULL); 
    151176  g_signal_connect ((gpointer) b_removematerial, "clicked", G_CALLBACK (OnRemoveMaterial), NULL); 
     177  g_signal_connect ((gpointer) t_exportmaterials, "clicked", G_CALLBACK (OnExportMatClicked), NULL); 
    152178 
    153179  /* Store pointers to all widgets, for use by lookup_widget(). */ 
     
    170196  GLADE_HOOKUP_OBJECT (w_plugplug2, b_addmaterial, "b_addmaterial"); 
    171197  GLADE_HOOKUP_OBJECT (w_plugplug2, b_removematerial, "b_removematerial"); 
     198  GLADE_HOOKUP_OBJECT (w_plugplug2, t_exportmaterials, "t_exportmaterials"); 
     199  GLADE_HOOKUP_OBJECT (w_plugplug2, t_limitmatnames, "t_limitmatnames"); 
     200  GLADE_HOOKUP_OBJECT (w_plugplug2, t_objects, "t_objects"); 
    172201 
    173202  return w_plugplug2;