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

git-svn-id: svn://svn.icculus.org/gtkradiant/GtkRadiant/trunk@172 8a3a26a2-13c4-0310-b231-cf6edde360e5
This commit is contained in:
namespace 2007-06-28 17:01:18 +00:00
parent 83f709aa55
commit 0c07d03e26
9 changed files with 981 additions and 805 deletions

View file

@ -1,6 +1,9 @@
This is the changelog for developers, != changelog for the end user
that we distribute with the binaries. (see changelog)
28/06/2007
- Added material-support to brushexport-plugin (Shaderman)
26/04/2007
- Disabled auto-search for texbrowser and entityinspector even when the option
is turned on in the gtkrc

View file

@ -22,10 +22,12 @@ void OnExportClicked(GtkButton* button, gpointer user_data)
{
GtkWidget* window = lookup_widget(GTK_WIDGET(button), "w_plugplug2");
ASSERT_NOTNULL(window);
const char* path = GlobalRadiant().m_pfnFileDialog(window, false, "Save as Obj", 0, 0);
if(!path)
const char* cpath = GlobalRadiant().m_pfnFileDialog(window, false, "Save as Obj", 0, 0);
if(!cpath)
return;
std::string path(cpath);
// get ignore list from ui
std::set<std::string> ignore;
@ -70,8 +72,35 @@ void OnExportClicked(GtkButton* button, gpointer user_data)
}
}
// export materials?
GtkWidget* toggle = lookup_widget(GTK_WIDGET(button), "t_exportmaterials");
ASSERT_NOTNULL(toggle);
bool exportmat = FALSE;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)))
exportmat = TRUE;
// limit material names?
toggle = lookup_widget(GTK_WIDGET(button), "t_limitmatnames");
ASSERT_NOTNULL(toggle);
bool limitMatNames = FALSE;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)) && exportmat)
limitMatNames = TRUE;
// create objects instead of groups?
toggle = lookup_widget(GTK_WIDGET(button), "t_objects");
ASSERT_NOTNULL(toggle);
bool objects = FALSE;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)) && exportmat)
objects = TRUE;
// export
ExportSelection(ignore, mode, path);
ExportSelection(ignore, mode, exportmat, path, limitMatNames, objects);
}
void OnAddMaterial(GtkButton* button, gpointer user_data)
@ -101,4 +130,19 @@ void OnRemoveMaterial(GtkButton* button, gpointer user_data)
gtk_list_store_remove(list, &iter);
}
void OnExportMatClicked(GtkButton* button, gpointer user_data)
{
GtkWidget* toggleLimit = lookup_widget(GTK_WIDGET(button), "t_limitmatnames");
GtkWidget* toggleObject = lookup_widget(GTK_WIDGET(button), "t_objects");
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
{
gtk_widget_set_sensitive(GTK_WIDGET(toggleLimit), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(toggleObject), TRUE);
} else {
gtk_widget_set_sensitive(GTK_WIDGET(toggleLimit), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(toggleObject), FALSE);
}
}
}// callbacks

View file

@ -7,5 +7,6 @@ void OnDestroy(GtkWidget*, gpointer);
void OnExportClicked(GtkButton*, gpointer);
void OnAddMaterial(GtkButton*, gpointer);
void OnRemoveMaterial(GtkButton*, gpointer);
void OnExportMatClicked(GtkButton* button, gpointer);
}// callbacks

View file

@ -6,6 +6,8 @@
#include "stream/stringstream.h"
#include "stream/textfilestream.h"
#include <map>
// this is very evil, but right now there is no better way
#include "../../radiant/brush.h"
@ -17,14 +19,14 @@
class ExportData
{
public:
ExportData(const std::set<std::string>& ignorelist, collapsemode mode);
ExportData(const std::set<std::string>& ignorelist, collapsemode mode, bool limNames, bool objs);
virtual ~ExportData(void);
virtual void BeginBrush(Brush& b);
virtual void AddBrushFace(Face& f);
virtual void EndBrush(void);
virtual bool WriteToFile(const std::string& path) const = 0;
virtual bool WriteToFile(const std::string& path, collapsemode mode) const = 0;
protected:
@ -48,7 +50,7 @@ private:
const std::set<std::string>& ignorelist;
};
ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode)
ExportData::ExportData(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _limNames, bool _objs)
: mode(_mode),
ignorelist(_ignorelist)
{
@ -101,7 +103,7 @@ void ExportData::AddBrushFace(Face& f)
std::string shadername;
GetShaderNameFromShaderPath(f.GetShader(), shadername);
// faces mit materials auf der ignoreliste ignorieren
// ignore faces from ignore list
if(ignorelist.find(shadername) != ignorelist.end())
return;
@ -146,7 +148,9 @@ void ExportData::GetShaderNameFromShaderPath(const char* path, std::string& name
else
name = tmp.substr(last_slash + 1, tmp.length() - last_slash);
globalOutputStream() << "Last: " << last_slash << " " << "lenght: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
#ifdef _DEBUG
globalOutputStream() << "Last: " << last_slash << " " << "length: " << (const unsigned int)tmp.length() << "Name: " << name.c_str() << "\n";
#endif
}
/*
@ -154,18 +158,31 @@ void ExportData::GetShaderNameFromShaderPath(const char* path, std::string& name
*/
class ExportDataAsWavefront : public ExportData
{
private:
bool expmat;
bool limNames;
bool objs;
public:
ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode)
: ExportData(_ignorelist, _mode)
ExportDataAsWavefront(const std::set<std::string>& _ignorelist, collapsemode _mode, bool _expmat, bool _limNames, bool _objs)
: ExportData(_ignorelist, _mode, _limNames, _objs)
{
expmat = _expmat;
limNames = _limNames;
objs = _objs;
}
bool WriteToFile(const std::string& path) const;
bool WriteToFile(const std::string& path, collapsemode mode) const;
};
bool ExportDataAsWavefront::WriteToFile(const std::string& path) const
bool ExportDataAsWavefront::WriteToFile(const std::string& path, collapsemode mode) const
{
TextFileOutputStream out(path.c_str());
std::string objFile = path.substr(0, path.length() -4) + ".obj";
std::string mtlFile = path.substr(0, path.length() -4) + ".mtl";
std::set<std::string> materials;
TextFileOutputStream out(objFile.c_str());
if(out.failed())
{
@ -173,17 +190,41 @@ bool ExportDataAsWavefront::WriteToFile(const std::string& path) const
return false;
}
out << "# Wavefront Objectfile exported with radiants brushexport plugin 2.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
out << "# Wavefront Objectfile exported with radiants brushexport plugin 3.0 by Thomas 'namespace' Nitschke, spam@codecreator.net\n\n";
if(expmat)
{
size_t last = mtlFile.find_last_of("//");
std::string mtllib = mtlFile.substr(last + 1, mtlFile.size() - last).c_str();
out << "mtllib " << mtllib.c_str() << "\n";
}
unsigned int vertex_count = 0;
const std::list<ExportData::group>::const_iterator gend(groups.end());
for(std::list<ExportData::group>::const_iterator git(groups.begin()); git != gend; ++git)
{
typedef std::multimap<std::string, std::string> bm;
bm brushMaterials;
typedef std::pair<std::string, std::string> String_Pair;
const std::list<const Face*>::const_iterator end(git->faces.end());
// submesh starts here
out << "\ng " << git->name.c_str() << "\n";
if(objs)
{
out << "\no ";
} else {
out << "\ng ";
}
out << git->name.c_str() << "\n";
// material
if(expmat && mode == COLLAPSE_ALL)
{
out << "usemtl material" << "\n\n";
materials.insert("material");
}
for(std::list<const Face*>::const_iterator it(git->faces.begin()); it != end; ++it)
{
@ -191,7 +232,7 @@ bool ExportDataAsWavefront::WriteToFile(const std::string& path) const
// vertices
for(size_t i = 0; i < w.numpoints; ++i)
out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << "\n";
out << "v " << FloatFormat(w[i].vertex.x(), 1, 6) << " " << FloatFormat(w[i].vertex.z(), 1, 6) << " " << FloatFormat(w[i].vertex.y(), 1, 6) << "\n";
}
out << "\n";
@ -209,15 +250,73 @@ bool ExportDataAsWavefront::WriteToFile(const std::string& path) const
const Winding& w((*it)->getWinding());
// faces
out << "\nf";
StringOutputStream faceLine(256);
faceLine << "\nf";
for(size_t i = 0; i < w.numpoints; ++i, ++vertex_count)
{
out << " " << vertex_count+1 << "/" << vertex_count+1;
faceLine << " " << vertex_count+1 << "/" << vertex_count+1;
}
if(mode != COLLAPSE_ALL)
{
materials.insert((*it)->getShader().getShader());
brushMaterials.insert(String_Pair((*it)->getShader().getShader(), faceLine.c_str()));
} else {
out << faceLine.c_str();
}
}
if(mode != COLLAPSE_ALL)
{
std::string lastMat;
std::string mat;
std::string faces;
for(bm::iterator iter = brushMaterials.begin(); iter != brushMaterials.end(); iter++)
{
mat = (*iter).first.c_str();
faces = (*iter).second.c_str();
if(mat != lastMat)
{
if(limNames && mat.size() > 20)
{
out << "\nusemtl " << mat.substr(mat.size() - 20, mat.size()).c_str();
} else {
out << "\nusemtl " << mat.c_str();
}
}
out << faces.c_str();
lastMat = mat;
}
}
out << "\n";
}
if(expmat)
{
TextFileOutputStream outMtl(mtlFile.c_str());
if(outMtl.failed())
{
globalErrorStream() << "Unable to open material file\n";
return false;
}
outMtl << "# Wavefront material file exported with GtkRadiants brushexport plugin.\n";
outMtl << "# Material Count: " << (const Unsigned)materials.size() << "\n\n";
for(std::set<std::string>::const_iterator it(materials.begin()); it != materials.end(); ++it)
{
if(limNames && it->size() > 20)
{
outMtl << "newmtl " << it->substr(it->size() - 20, it->size()).c_str() << "\n";
} else {
outMtl << "newmtl " << it->c_str() << "\n";
}
}
}
return true;
}
@ -263,12 +362,12 @@ private:
ExportData& exporter;
};
bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, const std::string& path)
bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limNames, bool objs)
{
ExportDataAsWavefront exporter(ignorelist, m);
ExportDataAsWavefront exporter(ignorelist, m, exmat, limNames, objs);
ForEachSelected vis(exporter);
GlobalSelectionSystem().foreachSelected(vis);
return exporter.WriteToFile(path);
return exporter.WriteToFile(path, m);
}

View file

@ -10,6 +10,6 @@ enum collapsemode
COLLAPSE_NONE
};
bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, const std::string& path);
bool ExportSelection(const std::set<std::string>& ignorelist, collapsemode m, bool exmat, const std::string& path, bool limitMatNames, bool objects);
#endif

View file

@ -35,10 +35,16 @@ create_w_plugplug2 (void)
GtkWidget *hbox1;
GtkWidget *b_addmaterial;
GtkWidget *b_removematerial;
GtkWidget *t_exportmaterials;
GtkWidget *t_limitmatnames;
GtkWidget *t_objects;
GtkTooltips *tooltips;
tooltips = gtk_tooltips_new();
w_plugplug2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_name (w_plugplug2, "w_plugplug2");
gtk_window_set_title (GTK_WINDOW (w_plugplug2), "BrushExport-Plugin 2.0 by namespace");
gtk_window_set_title (GTK_WINDOW (w_plugplug2), "BrushExport-Plugin 3.0 by namespace");
gtk_window_set_position (GTK_WINDOW (w_plugplug2), GTK_WIN_POS_CENTER);
gtk_window_set_destroy_with_parent (GTK_WINDOW (w_plugplug2), TRUE);
@ -61,6 +67,7 @@ create_w_plugplug2 (void)
r_collapse = gtk_radio_button_new_with_mnemonic (NULL, "Collapse mesh");
gtk_widget_set_name (r_collapse, "r_collapse");
gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), r_collapse, "Collapse all brushes into a single group", "Collapse all brushes into a single group");
gtk_widget_show (r_collapse);
gtk_box_pack_start (GTK_BOX (vbox4), r_collapse, FALSE, FALSE, 0);
gtk_radio_button_set_group (GTK_RADIO_BUTTON (r_collapse), r_collapse_group);
@ -68,6 +75,7 @@ create_w_plugplug2 (void)
r_collapsebymaterial = gtk_radio_button_new_with_mnemonic (NULL, "Collapse by material");
gtk_widget_set_name (r_collapsebymaterial, "r_collapsebymaterial");
gtk_tooltips_set_tip (GTK_TOOLTIPS(tooltips), r_collapsebymaterial, "Collapse into groups by material", "Collapse into groups by material");
gtk_widget_show (r_collapsebymaterial);
gtk_box_pack_start (GTK_BOX (vbox4), r_collapsebymaterial, FALSE, FALSE, 0);
gtk_radio_button_set_group (GTK_RADIO_BUTTON (r_collapsebymaterial), r_collapse_group);
@ -75,6 +83,7 @@ create_w_plugplug2 (void)
r_nocollapse = gtk_radio_button_new_with_mnemonic (NULL, "Don't collapse");
gtk_widget_set_name (r_nocollapse, "r_nocollapse");
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");
gtk_widget_show (r_nocollapse);
gtk_box_pack_start (GTK_BOX (vbox4), r_nocollapse, FALSE, FALSE, 0);
gtk_radio_button_set_group (GTK_RADIO_BUTTON (r_nocollapse), r_collapse_group);
@ -142,6 +151,22 @@ create_w_plugplug2 (void)
gtk_widget_show (b_removematerial);
gtk_box_pack_start (GTK_BOX (hbox1), b_removematerial, FALSE, FALSE, 0);
t_limitmatnames = gtk_check_button_new_with_mnemonic ("Use short material names (max. 20 chars)");
gtk_widget_set_name (t_limitmatnames, "t_limitmatnames");
gtk_widget_show (t_limitmatnames);
gtk_box_pack_end (GTK_BOX (vbox2), t_limitmatnames, FALSE, FALSE, 0);
t_objects = gtk_check_button_new_with_mnemonic ("Create (o)bjects instead of (g)roups");
gtk_widget_set_name (t_objects, "t_objects");
gtk_widget_show (t_objects);
gtk_box_pack_end (GTK_BOX (vbox2), t_objects, FALSE, FALSE, 0);
t_exportmaterials = gtk_check_button_new_with_mnemonic ("Create material information (.mtl file)");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(t_exportmaterials), true);
gtk_widget_set_name (t_exportmaterials, "t_exportmaterials");
gtk_widget_show (t_exportmaterials);
gtk_box_pack_end (GTK_BOX (vbox2), t_exportmaterials, FALSE, FALSE, 10);
using namespace callbacks;
g_signal_connect(G_OBJECT(w_plugplug2), "destroy", G_CALLBACK(OnDestroy), NULL);
g_signal_connect_swapped(G_OBJECT(b_close), "clicked", G_CALLBACK (OnDestroy), NULL);
@ -149,6 +174,7 @@ create_w_plugplug2 (void)
g_signal_connect ((gpointer) b_export, "clicked", G_CALLBACK (OnExportClicked), NULL);
g_signal_connect ((gpointer) b_addmaterial, "clicked", G_CALLBACK (OnAddMaterial), NULL);
g_signal_connect ((gpointer) b_removematerial, "clicked", G_CALLBACK (OnRemoveMaterial), NULL);
g_signal_connect ((gpointer) t_exportmaterials, "clicked", G_CALLBACK (OnExportMatClicked), NULL);
/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (w_plugplug2, w_plugplug2, "w_plugplug2");
@ -169,6 +195,9 @@ create_w_plugplug2 (void)
GLADE_HOOKUP_OBJECT (w_plugplug2, hbox1, "hbox1");
GLADE_HOOKUP_OBJECT (w_plugplug2, b_addmaterial, "b_addmaterial");
GLADE_HOOKUP_OBJECT (w_plugplug2, b_removematerial, "b_removematerial");
GLADE_HOOKUP_OBJECT (w_plugplug2, t_exportmaterials, "t_exportmaterials");
GLADE_HOOKUP_OBJECT (w_plugplug2, t_limitmatnames, "t_limitmatnames");
GLADE_HOOKUP_OBJECT (w_plugplug2, t_objects, "t_objects");
return w_plugplug2;
}