1 | //
|
---|
2 | // render.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | #ifdef __GNUC__
|
---|
29 | #pragma implementation
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <util/misc/formio.h>
|
---|
33 |
|
---|
34 | #include <util/render/render.h>
|
---|
35 | #include <util/render/object.h>
|
---|
36 | #include <util/render/animate.h>
|
---|
37 | #include <util/render/find.h>
|
---|
38 | #include <util/render/polygons.h>
|
---|
39 | #include <util/render/polysphere.h>
|
---|
40 |
|
---|
41 | using namespace std;
|
---|
42 | using namespace sc;
|
---|
43 |
|
---|
44 | /////////////////////////////////////////////////////////////////////////////
|
---|
45 | // Render
|
---|
46 |
|
---|
47 | static ClassDesc Render_cd(
|
---|
48 | typeid(Render),"Render",1,"public DescribedClass",
|
---|
49 | 0, 0, 0);
|
---|
50 |
|
---|
51 | Render::Render()
|
---|
52 | {
|
---|
53 | }
|
---|
54 |
|
---|
55 | Render::Render(const Ref<KeyVal>& keyval)
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | Render::~Render()
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 | void
|
---|
64 | Render::push_material(const Ref<Material>& m)
|
---|
65 | {
|
---|
66 | material_stack_.push(m);
|
---|
67 | }
|
---|
68 |
|
---|
69 | void
|
---|
70 | Render::push_appearance(const Ref<Appearance>& a)
|
---|
71 | {
|
---|
72 | appearance_stack_.push(a);
|
---|
73 | }
|
---|
74 |
|
---|
75 | void
|
---|
76 | Render::push_transform(const Ref<Transform>& t)
|
---|
77 | {
|
---|
78 | transform_stack_.push(t);
|
---|
79 | }
|
---|
80 |
|
---|
81 | Ref<Material>
|
---|
82 | Render::pop_material()
|
---|
83 | {
|
---|
84 | return material_stack_.pop();
|
---|
85 | }
|
---|
86 |
|
---|
87 | Ref<Appearance>
|
---|
88 | Render::pop_appearance()
|
---|
89 | {
|
---|
90 | return appearance_stack_.pop();
|
---|
91 | }
|
---|
92 |
|
---|
93 | Ref<Transform>
|
---|
94 | Render::pop_transform()
|
---|
95 | {
|
---|
96 | return transform_stack_.pop();
|
---|
97 | }
|
---|
98 |
|
---|
99 | void
|
---|
100 | Render::render(const Ref<RenderedObject>& object)
|
---|
101 | {
|
---|
102 | if (object->material().nonnull()) push_material(object->material());
|
---|
103 | if (object->transform().nonnull()) push_transform(object->transform());
|
---|
104 | if (object->appearance().nonnull()) push_appearance(object->appearance());
|
---|
105 | object->render(this);
|
---|
106 | if (object->material().nonnull()) pop_material();
|
---|
107 | if (object->transform().nonnull()) pop_transform();
|
---|
108 | if (object->appearance().nonnull()) pop_appearance();
|
---|
109 | }
|
---|
110 |
|
---|
111 | void
|
---|
112 | Render::set(const Ref<RenderedObjectSet>& set)
|
---|
113 | {
|
---|
114 | for (int i=0; i<set->n(); i++) {
|
---|
115 | render(set->element(i));
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | // This renders spheres by creating a RenderedPolygon object
|
---|
120 | void
|
---|
121 | Render::sphere(const Ref<RenderedSphere>& sphere)
|
---|
122 | {
|
---|
123 | // find the level of accuracy which should be used to render the sphere
|
---|
124 | int level = 1;
|
---|
125 | find_int_parameter_in_appearance_stack(appearance_stack_,
|
---|
126 | &Appearance::level,
|
---|
127 | level);
|
---|
128 | Ref<RenderedPolygons> poly(new RenderedPolygons);
|
---|
129 |
|
---|
130 | polysphere(level, poly.pointer());
|
---|
131 |
|
---|
132 | render(poly.pointer());
|
---|
133 | }
|
---|
134 |
|
---|
135 | void
|
---|
136 | Render::animate(const Ref<AnimatedObject> &animated_object)
|
---|
137 | {
|
---|
138 | for (int i=0; i<animated_object->nobject(); i++) {
|
---|
139 | Ref<RenderedObject> object = animated_object->object(i);
|
---|
140 | render(object);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /////////////////////////////////////////////////////////////////////////////
|
---|
145 | // FileRender
|
---|
146 |
|
---|
147 | static ClassDesc FileRender_cd(
|
---|
148 | typeid(FileRender),"FileRender",1,"public Render",
|
---|
149 | 0, 0, 0);
|
---|
150 |
|
---|
151 | FileRender::FileRender(const char * filename)
|
---|
152 | {
|
---|
153 | filename_ = 0;
|
---|
154 | basename_ = 0;
|
---|
155 | depth_ = 0;
|
---|
156 | sbuf_ = 0;
|
---|
157 | delete_sbuf_ = 0;
|
---|
158 | set_filename(filename);
|
---|
159 | }
|
---|
160 |
|
---|
161 | FileRender::FileRender(ostream &o)
|
---|
162 | {
|
---|
163 | filename_ = 0;
|
---|
164 | basename_ = 0;
|
---|
165 | depth_ = 0;
|
---|
166 | sbuf_ = o.rdbuf();
|
---|
167 | delete_sbuf_ = 0;
|
---|
168 | }
|
---|
169 |
|
---|
170 | FileRender::FileRender(const Ref<KeyVal>& keyval):
|
---|
171 | Render(keyval)
|
---|
172 | {
|
---|
173 | char *filename = keyval->pcharvalue("filename");
|
---|
174 | char *basename = keyval->pcharvalue("basename");
|
---|
175 | if (!filename && !basename) {
|
---|
176 | const char *cbasename = SCFormIO::default_basename();
|
---|
177 | if (cbasename) {
|
---|
178 | basename = strcpy(new char[strlen(cbasename)+1],cbasename);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | depth_ = 0;
|
---|
182 | sbuf_ = 0;
|
---|
183 | delete_sbuf_ = 0;
|
---|
184 | filename_ = 0;
|
---|
185 | basename_ = 0;
|
---|
186 | if (basename) {
|
---|
187 | set_basename(basename);
|
---|
188 | delete[] basename;
|
---|
189 | }
|
---|
190 | // filename overrides basename
|
---|
191 | if (filename) {
|
---|
192 | set_filename(filename);
|
---|
193 | delete[] filename;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | void
|
---|
198 | FileRender::set_filename(const char *filename)
|
---|
199 | {
|
---|
200 | delete[] basename_;
|
---|
201 | delete[] filename_;
|
---|
202 | filename_ = 0;
|
---|
203 | basename_ = 0;
|
---|
204 | if (filename) filename_ = strcpy(new char[strlen(filename)+1],filename);
|
---|
205 | }
|
---|
206 |
|
---|
207 | void
|
---|
208 | FileRender::set_basename(const char *basename)
|
---|
209 | {
|
---|
210 | delete[] filename_;
|
---|
211 | delete[] basename_;
|
---|
212 | filename_ = 0;
|
---|
213 | basename_ = 0;
|
---|
214 | if (basename) basename_ = strcpy(new char[strlen(basename)+1],basename);
|
---|
215 | }
|
---|
216 |
|
---|
217 | FileRender::~FileRender()
|
---|
218 | {
|
---|
219 | delete[] filename_;
|
---|
220 | delete[] basename_;
|
---|
221 | if (delete_sbuf_) delete sbuf_;
|
---|
222 | }
|
---|
223 |
|
---|
224 | void
|
---|
225 | FileRender::clear()
|
---|
226 | {
|
---|
227 | }
|
---|
228 |
|
---|
229 | char *
|
---|
230 | FileRender::get_filename(const char *objectname)
|
---|
231 | {
|
---|
232 | char *file = 0;
|
---|
233 |
|
---|
234 | if (filename_) {
|
---|
235 | // if a file name is given then it is the entire name of the file
|
---|
236 | file = strcpy(new char[strlen(filename_) + 1],filename_);
|
---|
237 | }
|
---|
238 | else if (basename_) {
|
---|
239 | // if we have a base name, it is used to construct a filename
|
---|
240 | const char *ext = file_extension();
|
---|
241 | int lenobjectname;
|
---|
242 | if (objectname == 0) lenobjectname = 0;
|
---|
243 | else lenobjectname = strlen(objectname);
|
---|
244 | if (lenobjectname) lenobjectname++;
|
---|
245 | file = new char[strlen(basename_)+lenobjectname+strlen(ext)+1];
|
---|
246 | strcpy(file, basename_);
|
---|
247 | if (lenobjectname) {
|
---|
248 | strcat(file, ".");
|
---|
249 | strcat(file, objectname);
|
---|
250 | }
|
---|
251 | strcat(file, ext);
|
---|
252 | }
|
---|
253 | else {
|
---|
254 | if (!objectname) objectname = "renderedobject";
|
---|
255 | const char *ext = file_extension();
|
---|
256 | file = new char[strlen(objectname)+strlen(ext)+1];
|
---|
257 | strcpy(file, objectname);
|
---|
258 | strcat(file, ext);
|
---|
259 | }
|
---|
260 |
|
---|
261 | return file;
|
---|
262 | }
|
---|
263 |
|
---|
264 | void
|
---|
265 | FileRender::open_sbuf(const char *objectname)
|
---|
266 | {
|
---|
267 | if (depth_++) return;
|
---|
268 |
|
---|
269 | char *file = get_filename(objectname);
|
---|
270 |
|
---|
271 | if (file) {
|
---|
272 | filebuf *fbuf = new filebuf();
|
---|
273 | fbuf->open(file,ios::out);
|
---|
274 | if (!fbuf->is_open()) {
|
---|
275 | ExEnv::errn() << scprintf("FileRender: couldn't open \"%s\"\n", filename_);
|
---|
276 | abort();
|
---|
277 | }
|
---|
278 | sbuf_ = fbuf;
|
---|
279 | delete_sbuf_ = 1;
|
---|
280 | delete[] file;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | void
|
---|
285 | FileRender::close_sbuf()
|
---|
286 | {
|
---|
287 | if (--depth_ > 0) return;
|
---|
288 |
|
---|
289 | if (delete_sbuf_) {
|
---|
290 | delete sbuf_;
|
---|
291 | sbuf_ = 0;
|
---|
292 | delete_sbuf_ = 0;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | const char *
|
---|
297 | FileRender::file_extension()
|
---|
298 | {
|
---|
299 | return ".ren";
|
---|
300 | }
|
---|
301 |
|
---|
302 | /////////////////////////////////////////////////////////////////////////////
|
---|
303 |
|
---|
304 | // Local Variables:
|
---|
305 | // mode: c++
|
---|
306 | // c-file-style: "CLJ"
|
---|
307 | // End:
|
---|