NGL  6.5
The NCCA Graphics Library
RibExport.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "RibExport.h"
18 #include "fmt/format.h"
19 #include <iostream>
20 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 namespace ngl
24 {
25 
26 //----------------------------------------------------------------------------------------------------------------------
27 RibExport::RibExport(const std::string& _fileName, bool _oneShot)
28 {
29  m_attribCount = 0;
30  m_transformCount = 0;
31  m_worldCount = 0;
32  m_ribFileName = _fileName;
33  m_frameNumber = 0;
34  m_tabs = 0;
35  m_isOpen = false;
36  m_oneShot = _oneShot;
37 }
38 
39 //----------------------------------------------------------------------------------------------------------------------
41 {
42  if (m_attribCount != 0)
43  {
44  std::cerr << "Warning Mismatched AttributeBegin / AttributeEnd block" << std::endl;
45  }
46  if (m_transformCount != 0)
47  {
48  std::cerr << "Warning Mismatched TransformBegin / TransformEnd block" << std::endl;
49  }
50  if (m_worldCount != 0)
51  {
52  std::cerr << "Warning Mismatched WorldBegin / WorldEnd block" << std::endl;
53  }
54  if (m_ribFile.is_open())
55  {
56  m_ribFile.close();
57  m_isOpen = false;
58  }
59 }
60 
61 // Writes the Comment parameters to
62 // the rib file.
63 //----------------------------------------------------------------------------------------------------------------------
64 void RibExport::comment(const std::string& _sText)
65 {
66  // Append Comment
67  m_ribFile << "\n#======================================================\n";
68  m_ribFile << "# " << _sText << "\n";
69  m_ribFile << "#======================================================\n";
70 }
71 
72 
73 //----------------------------------------------------------------------------------------------------------------------
75 {
76  std::string fName;
77  if (m_oneShot)
78  {
79  fName = fmt::format("{0}.%03d.{1}", m_ribFileName, m_frameNumber);
80  }
81  else
82  {
83  fName = m_ribFileName;
84  }
85  m_ribFile.open(fName.c_str(), std::ios::out);
86  if (!m_ribFile.is_open())
87  {
88  std::cerr << "problems Opening File" << std::endl;
89  }
90  m_ribFile << "# Rib file generated using RibExporter" << std::endl;
91  m_isOpen = true;
92 }
93 
94 //----------------------------------------------------------------------------------------------------------------------
96 {
97  m_ribFile.close();
98  ++m_frameNumber;
99 }
100 
101 //----------------------------------------------------------------------------------------------------------------------
103 {
104  for (int i = 0; i < m_tabs; ++i)
105  {
106  m_ribFile << "\t";
107  }
108 }
109 
110 //----------------------------------------------------------------------------------------------------------------------
112 {
113  writeTabs();
114  m_ribFile << "AttributeBegin " << std::endl;
115  ++m_tabs;
116  ++m_attribCount;
117 }
118 
119 //----------------------------------------------------------------------------------------------------------------------
121 {
122  --m_tabs;
123  writeTabs();
124  m_ribFile << "AttributeEnd " << std::endl;
125  m_attribCount--;
126 }
127 
128 //----------------------------------------------------------------------------------------------------------------------
130 {
131  writeTabs();
132  m_ribFile << "TransformBegin " << std::endl;
133  ++m_tabs;
135 }
136 
137 //----------------------------------------------------------------------------------------------------------------------
139 {
140  --m_tabs;
141  writeTabs();
142  m_ribFile << "TransformEnd " << std::endl;
144 }
145 
146 //----------------------------------------------------------------------------------------------------------------------
148 {
149  writeTabs();
150  m_ribFile << "WorldBegin " << std::endl;
151  ++m_tabs;
152  ++m_worldCount;
153 }
154 
155 //----------------------------------------------------------------------------------------------------------------------
157 {
158  --m_tabs;
159  writeTabs();
160  m_ribFile << "WorldEnd " << std::endl;
161  --m_worldCount;
162 }
163 
164 //----------------------------------------------------------------------------------------------------------------------
166 {
167  writeTabs();
168  m_ribFile << _string << std::endl;
169 }
170 
171 
172 //----------------------------------------------------------------------------------------------------------------------
173 void RibExport::Translate(const Real _x, const Real _y, const Real _z)
174 {
175  writeTabs();
176  m_ribFile << "Translate " << _x << " " << _y << " " << _z << std::endl;
177 }
178 
179 //----------------------------------------------------------------------------------------------------------------------
180 void RibExport::Rotate(const Real _angle, const Real _x, const Real _y, const Real _z)
181 {
182  writeTabs();
183  m_ribFile << "Rotate " << _angle << " " << _x << " " << _y << " " << _z << std::endl;
184 }
185 
186 //----------------------------------------------------------------------------------------------------------------------
187 void RibExport::Scale(const Real _x, const Real _y, const Real _z)
188 {
189  writeTabs();
190  m_ribFile << "Scale " << _x << " " << _y << " " << _z << std::endl;
191 }
192 
193 //----------------------------------------------------------------------------------------------------------------------
194 void RibExport::Sphere(const Real _radius, const Real _zMin, const Real _zMax, const Real _sweep)
195 {
196  writeTabs();
197  m_ribFile << "Sphere " << _radius << " " << _zMin << " " << _zMax << " " << _sweep << std::endl;
198 }
199 
200 //----------------------------------------------------------------------------------------------------------------------
201 void RibExport::Cylinder(const Real _radius, const Real _zMin, const Real _zMax, const Real _sweep)
202 {
203  writeTabs();
204  m_ribFile << "Cylinder " << _radius << " " << _zMin << " " << _zMax << " " << _sweep << std::endl;
205 }
206 
207 //----------------------------------------------------------------------------------------------------------------------
208 void RibExport::Cone(const Real _height, const Real _radius, const Real _sweep)
209 {
210  writeTabs();
211  m_ribFile << "Cone " << _height << " " << _radius << " " << _sweep << std::endl;
212 }
213 
214 //----------------------------------------------------------------------------------------------------------------------
215 void RibExport::Paraboloid(const Real _topRad, const Real _zMin, const Real _zMax, const Real _sweep)
216 {
217  writeTabs();
218  m_ribFile << "Paraboloid " << _topRad << " " << _zMin << " " << _zMax << " " << _sweep << std::endl;
219 }
220 
221 //----------------------------------------------------------------------------------------------------------------------
222 void RibExport::Hyperboloid(const Real _p1, const Real _p2, const Real _sweep)
223 {
224  writeTabs();
225  m_ribFile << "Hyperboloid " << _p1 << " " << _p2 << " " << _sweep << std::endl;
226 }
227 
228 //----------------------------------------------------------------------------------------------------------------------
229 void RibExport::Disk(const Real _height, const Real _radius, const Real _sweep)
230 {
231  writeTabs();
232  m_ribFile << "Disk " << _height << " " << _radius << " " << _sweep << std::endl;
233 }
234 
235 //----------------------------------------------------------------------------------------------------------------------
236 void RibExport::Torus(const Real _major, const Real _minor, const Real _phiMin, const Real _phiMax, const Real _sweep)
237 {
238  writeTabs();
239  m_ribFile << "Torus " << _major << " " << _minor << " " << _phiMin << " " << _phiMax << " " << _sweep << std::endl;
240 }
241 
242 } // end ngl namespace
243 
244 //----------------------------------------------------------------------------------------------------------------------
int m_tabs
the current tab level for auto tabing of rib file
Definition: RibExport.h:213
void comment(const std::string &_sText)
write a comment to the rib stream
Definition: RibExport.cpp:64
void writeToFile(std::string _string)
Write text to rib file.
Definition: RibExport.cpp:165
void AttributeBegin()
write attribute begin and tab in
Definition: RibExport.cpp:111
int m_frameNumber
Hold the frame number for calls to Frame.
Definition: RibExport.h:205
int m_worldCount
count calls to WorldBegin to ensure matching
Definition: RibExport.h:201
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3175
void Disk(const Real _height, const Real _radius, const Real _sweep)
draw an RiDisk to the stream
Definition: RibExport.cpp:229
bool m_isOpen
if the stream is open
Definition: RibExport.h:217
implementation files for RibExport class
Definition: AABB.cpp:22
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
std::fstream m_ribFile
the RibFile stream opened in the ctor
Definition: RibExport.h:189
void Paraboloid(const Real _topRad, const Real _zMin, const Real _zMax, const Real _sweep)
write an RiParaboloid to the stream
Definition: RibExport.cpp:215
void Cone(const Real _height, const Real _radius, const Real _sweep)
write an RiCone to the stream
Definition: RibExport.cpp:208
void close()
Close the rib file.
Definition: RibExport.cpp:95
void Torus(const Real _major, const Real _minor, const Real _phiMin, const Real _phiMax, const Real _sweep)
draw an RiTorus to the stream
Definition: RibExport.cpp:236
void open()
open a rib file for writing
Definition: RibExport.cpp:74
void WorldBegin()
write world begin and auto tab
Definition: RibExport.cpp:147
std::string m_ribFileName
The name of the RibFile.
Definition: RibExport.h:209
bool m_oneShot
we can use this once or multiple times
Definition: RibExport.h:221
void Translate(const Real _x, const Real _y, const Real _z)
write an RiTranslate to file
Definition: RibExport.cpp:173
void Rotate(const Real _angle, const Real _x, const Real _y, const Real _z)
write an RiRotate
Definition: RibExport.cpp:180
void Sphere(const Real _radius, const Real _zMin, const Real _zMax, const Real _sweep)
write an RiSphere
Definition: RibExport.cpp:194
int m_transformCount
count calls to TransformBegin to ensure matching
Definition: RibExport.h:197
a simple rib exporter function
void TransformBegin()
write transform begin and tab in
Definition: RibExport.cpp:129
RibExport(const std::string &_fileName, bool _oneShot=false)
ctor passing in the name of the rib file to open
Definition: RibExport.cpp:27
~RibExport()
destructor
Definition: RibExport.cpp:40
void writeTabs()
method to auto write tabs
Definition: RibExport.cpp:102
void AttributeEnd()
write attribute end and un tab
Definition: RibExport.cpp:120
void WorldEnd()
write world begin and un tab
Definition: RibExport.cpp:156
void Cylinder(const Real _radius, const Real _zMin, const Real _zMax, const Real _sweep)
write an RiCylinder
Definition: RibExport.cpp:201
void Scale(const Real _x, const Real _y, const Real _z)
write an RiScale
Definition: RibExport.cpp:187
void TransformEnd()
write transform end and un tab
Definition: RibExport.cpp:138
void Hyperboloid(const Real _p1, const Real _p2, const Real _sweep)
draw a hyperboloid to the stream
Definition: RibExport.cpp:222
GLsizei const GLchar *const * string
Definition: glew.h:1847
int m_attribCount
count calls to AttributeBegin to ensure matching
Definition: RibExport.h:193