brushtokens.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_BRUSHTOKENS_H)
00023 #define INCLUDED_BRUSHTOKENS_H
00024 
00025 #include "stringio.h"
00026 #include "stream/stringstream.h"
00027 #include "brush.h"
00028 
00029 class UFOFaceTokenImporter
00030 {
00031     private:
00032         Face& m_face;
00033 
00040         bool importContentAndSurfaceFlags (FaceShader& faceShader, Tokeniser& tokeniser)
00041         {
00042             return Tokeniser_getInteger(tokeniser, faceShader.m_flags.m_contentFlags) && Tokeniser_getInteger(
00043                     tokeniser, faceShader.m_flags.m_surfaceFlags) && Tokeniser_getInteger(tokeniser,
00044                     faceShader.m_flags.m_value);
00045         }
00046 
00053         bool importTextureDefinition (FaceTexdef& texdef, Tokeniser& tokeniser)
00054         {
00055             ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_importTokens: bad texdef");
00056             return Tokeniser_getFloat(tokeniser, texdef.m_projection.m_texdef.shift[0]) && Tokeniser_getFloat(
00057                     tokeniser, texdef.m_projection.m_texdef.shift[1]) && Tokeniser_getFloat(tokeniser,
00058                     texdef.m_projection.m_texdef.rotate) && Tokeniser_getFloat(tokeniser,
00059                     texdef.m_projection.m_texdef.scale[0]) && Tokeniser_getFloat(tokeniser,
00060                     texdef.m_projection.m_texdef.scale[1]);
00061 
00062         }
00063 
00070         bool importPlane (FacePlane& facePlane, Tokeniser& tokeniser)
00071         {
00072             for (std::size_t i = 0; i < 3; i++) {
00073                 if (!Tokeniser_parseToken(tokeniser, "("))
00074                     return false;
00075                 for (std::size_t j = 0; j < 3; ++j) {
00076                     if (!Tokeniser_getDouble(tokeniser, facePlane.planePoints()[i][j]))
00077                         return false;
00078                 }
00079                 if (!Tokeniser_parseToken(tokeniser, ")"))
00080                     return false;
00081             }
00082             facePlane.MakePlane();
00083             return true;
00084         }
00085 
00086         bool importTextureName (FaceShader& faceShader, Tokeniser& tokeniser)
00087         {
00088             const std::string texture = tokeniser.getToken();
00089             if (texture.empty()) {
00090                 Tokeniser_unexpectedError(tokeniser, texture, "#texture-name");
00091                 return false;
00092             }
00093             if (texture == "NULL") {
00094                 faceShader.setShader(GlobalTexturePrefix_get());
00095             } else {
00096                 faceShader.setShader(GlobalTexturePrefix_get() + texture);
00097             }
00098             return true;
00099         }
00100     public:
00101         UFOFaceTokenImporter (Face& face) :
00102             m_face(face)
00103         {
00104         }
00105         bool importTokens (Tokeniser& tokeniser)
00106         {
00107             if (!importPlane(m_face.getPlane(), tokeniser))
00108                 return false;
00109             if (!importTextureName(m_face.getShader(), tokeniser))
00110                 return false;
00111             if (!importTextureDefinition(m_face.getTexdef(), tokeniser))
00112                 return false;
00113             if (Tokeniser_nextTokenIsDigit(tokeniser)) {
00114                 m_face.getShader().m_flags.m_specified = true;
00115                 if (!importContentAndSurfaceFlags(m_face.getShader(), tokeniser))
00116                     return false;
00117             }
00118             m_face.getTexdef().m_scaleApplied = true;
00119             return true;
00120         }
00121 };
00122 
00126 class UFOFaceTokenExporter
00127 {
00128     private:
00130         const Face& m_face;
00131 
00137         void exportPlane (const FacePlane& facePlane, TokenWriter& writer) const
00138         {
00139             for (std::size_t i = 0; i < 3; i++) {
00140                 writer.writeToken("(");
00141                 for (std::size_t j = 0; j < 3; j++) {
00142                     writer.writeFloat(Face::m_quantise(facePlane.planePoints()[i][j]));
00143                 }
00144                 writer.writeToken(")");
00145             }
00146         }
00147 
00153         void exportTextureDefinition (const FaceTexdef& faceTexdef, TokenWriter& writer) const
00154         {
00155             ASSERT_MESSAGE(texdef_sane(faceTexdef.m_projection.m_texdef), "FaceTexdef_exportTokens: bad texdef");
00156             // write texdef
00157             writer.writeFloat(faceTexdef.m_projection.m_texdef.shift[0]);
00158             writer.writeFloat(faceTexdef.m_projection.m_texdef.shift[1]);
00159             writer.writeFloat(faceTexdef.m_projection.m_texdef.rotate);
00160             writer.writeFloat(faceTexdef.m_projection.m_texdef.scale[0]);
00161             writer.writeFloat(faceTexdef.m_projection.m_texdef.scale[1]);
00162         }
00163 
00169         void exportContentAndSurfaceFlags (const FaceShader& faceShader, TokenWriter& writer) const
00170         {
00171             writer.writeInteger(faceShader.m_flags.m_contentFlags);
00172             writer.writeInteger(faceShader.m_flags.m_surfaceFlags);
00173             writer.writeInteger(faceShader.m_flags.m_value);
00174         }
00175 
00181         void exportTexture (const FaceShader& faceShader, TokenWriter& writer) const
00182         {
00183             // write shader name
00184             const std::string shaderName = shader_get_textureName(faceShader.getShader());
00185             if (shaderName.empty()) {
00186                 writer.writeToken("tex_common/nodraw");
00187             } else {
00188                 writer.writeToken(shaderName.c_str());
00189             }
00190         }
00191 
00192     public:
00196         UFOFaceTokenExporter (const Face& face) :
00197             m_face(face)
00198         {
00199         }
00200         void exportTokens (TokenWriter& writer) const
00201         {
00202             exportPlane(m_face.getPlane(), writer);
00203             exportTexture(m_face.getShader(), writer);
00204             exportTextureDefinition(m_face.getTexdef(), writer);
00205             if (m_face.getShader().m_flags.m_specified || m_face.isDetail()) {
00206                 exportContentAndSurfaceFlags(m_face.getShader(), writer);
00207             }
00208             writer.nextLine();
00209         }
00210 };
00211 
00212 class BrushTokenImporter: public MapImporter
00213 {
00214         Brush& m_brush;
00215 
00216     public:
00217         BrushTokenImporter (Brush& brush) :
00218             m_brush(brush)
00219         {
00220         }
00221         bool importTokens (Tokeniser& tokeniser)
00222         {
00223             while (1) {
00224                 // check for end of brush
00225                 const std::string token = tokeniser.getToken();
00226                 if (token == "}")
00227                     break;
00228 
00229                 tokeniser.ungetToken();
00230 
00231                 m_brush.push_back(FaceSmartPointer(new Face(&m_brush)));
00232 
00233                 Face& face = *m_brush.back();
00234 
00235                 UFOFaceTokenImporter importer(face);
00236                 if (!importer.importTokens(tokeniser))
00237                     return false;
00238                 face.planeChanged();
00239             }
00240 
00241             m_brush.planeChanged();
00242             m_brush.shaderChanged();
00243 
00244             return true;
00245         }
00246 };
00247 
00248 class BrushTokenExporter: public MapExporter
00249 {
00250         const Brush& m_brush;
00251 
00252     public:
00253         BrushTokenExporter (const Brush& brush) :
00254             m_brush(brush)
00255         {
00256         }
00257         void exportTokens (TokenWriter& writer) const
00258         {
00259             m_brush.evaluateBRep(); // ensure b-rep is up-to-date, so that non-contributing faces can be identified.
00260 
00261             if (!m_brush.hasContributingFaces()) {
00262                 return;
00263             }
00264 
00265             writer.writeToken("{");
00266             writer.nextLine();
00267 
00268             for (Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i) {
00269                 const Face& face = *(*i);
00270 
00271                 if (face.contributes()) {
00272                     UFOFaceTokenExporter exporter(face);
00273                     exporter.exportTokens(writer);
00274                 }
00275             }
00276 
00277             writer.writeToken("}");
00278             writer.nextLine();
00279         }
00280 };
00281 
00282 #endif

Generated by  doxygen 1.6.2