| [0b990d] | 1 | // | 
|---|
|  | 2 | // oogl.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 | #include <util/misc/formio.h> | 
|---|
|  | 29 | #include <util/keyval/keyval.h> | 
|---|
|  | 30 | #include <util/render/oogl.h> | 
|---|
|  | 31 | #include <util/render/object.h> | 
|---|
|  | 32 | #include <util/render/sphere.h> | 
|---|
|  | 33 | #include <util/render/polygons.h> | 
|---|
|  | 34 | #include <util/render/polylines.h> | 
|---|
|  | 35 | #include <util/render/material.h> | 
|---|
|  | 36 | #include <util/render/animate.h> | 
|---|
|  | 37 |  | 
|---|
|  | 38 | using namespace std; | 
|---|
|  | 39 | using namespace sc; | 
|---|
|  | 40 |  | 
|---|
|  | 41 | static ClassDesc OOGLRender_cd( | 
|---|
|  | 42 | typeid(OOGLRender),"OOGLRender",1,"public FileRender", | 
|---|
|  | 43 | 0, create<OOGLRender>, 0); | 
|---|
|  | 44 |  | 
|---|
|  | 45 | OOGLRender::OOGLRender(const char * filename): | 
|---|
|  | 46 | FileRender(filename) | 
|---|
|  | 47 | { | 
|---|
|  | 48 | oogl_spheres_ = 0; | 
|---|
|  | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | OOGLRender::OOGLRender(ostream &o): | 
|---|
|  | 52 | FileRender(o) | 
|---|
|  | 53 | { | 
|---|
|  | 54 | oogl_spheres_ = 0; | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | OOGLRender::OOGLRender(const Ref<KeyVal>& keyval): | 
|---|
|  | 58 | FileRender(keyval) | 
|---|
|  | 59 | { | 
|---|
|  | 60 | oogl_spheres_ = keyval->booleanvalue("oogl_spheres"); | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 | OOGLRender::~OOGLRender() | 
|---|
|  | 64 | { | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | const char * | 
|---|
|  | 68 | OOGLRender::file_extension() | 
|---|
|  | 69 | { | 
|---|
|  | 70 | return ".oogl"; | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
|  | 73 | void | 
|---|
|  | 74 | OOGLRender::animate(const Ref<AnimatedObject>& animated_object) | 
|---|
|  | 75 | { | 
|---|
|  | 76 | // save the old filename_ and basename_ | 
|---|
|  | 77 | char *old_filename = filename_; | 
|---|
|  | 78 | char *old_basename = basename_; | 
|---|
|  | 79 |  | 
|---|
|  | 80 | // construct a script name based on the animated object name | 
|---|
|  | 81 | const char *base; | 
|---|
|  | 82 | if (old_filename) base = old_filename; | 
|---|
|  | 83 | else if (old_basename) base = old_basename; | 
|---|
|  | 84 | else base = "anim"; | 
|---|
|  | 85 | int lenobjname; | 
|---|
|  | 86 | if (animated_object->name() != 0) { | 
|---|
|  | 87 | lenobjname = strlen(animated_object->name()); | 
|---|
|  | 88 | } | 
|---|
|  | 89 | else lenobjname = 0; | 
|---|
|  | 90 | if (lenobjname) lenobjname++; | 
|---|
|  | 91 | const char *suf = ".scr"; | 
|---|
|  | 92 | char *file = new char[strlen(base)+lenobjname+strlen(suf)+1]; | 
|---|
|  | 93 | strcpy(file, base); | 
|---|
|  | 94 | if (lenobjname) { | 
|---|
|  | 95 | strcat(file,"."); | 
|---|
|  | 96 | strcat(file,animated_object->name()); | 
|---|
|  | 97 | } | 
|---|
|  | 98 | strcat(file,suf); | 
|---|
|  | 99 |  | 
|---|
|  | 100 | // construct a base name based on the animated object name | 
|---|
|  | 101 | filename_ = 0; | 
|---|
|  | 102 | basename_ = new char[strlen(base)+lenobjname]; | 
|---|
|  | 103 | strcpy(basename_, base); | 
|---|
|  | 104 | if (lenobjname) { | 
|---|
|  | 105 | strcat(basename_,"."); | 
|---|
|  | 106 | strcat(basename_,animated_object->name()); | 
|---|
|  | 107 | } | 
|---|
|  | 108 |  | 
|---|
|  | 109 | ofstream anim(file); | 
|---|
|  | 110 | delete[] file; | 
|---|
|  | 111 | for (int i=0; i<animated_object->nobject(); i++) { | 
|---|
|  | 112 | Ref<RenderedObject> object = animated_object->object(i); | 
|---|
|  | 113 | if (object->name() == 0) { | 
|---|
|  | 114 | char ic[64]; | 
|---|
|  | 115 | sprintf(ic,"%03d",i); | 
|---|
|  | 116 | object->set_name(ic); | 
|---|
|  | 117 | } | 
|---|
|  | 118 | file = get_filename(object->name()); | 
|---|
|  | 119 | anim << file << endl; | 
|---|
|  | 120 | delete[] file; | 
|---|
|  | 121 | render(object); | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | delete[] filename_; | 
|---|
|  | 125 | delete[] basename_; | 
|---|
|  | 126 | filename_ = old_filename; | 
|---|
|  | 127 | basename_ = old_basename; | 
|---|
|  | 128 | } | 
|---|
|  | 129 |  | 
|---|
|  | 130 | void | 
|---|
|  | 131 | OOGLRender::render(const Ref<RenderedObject>& object) | 
|---|
|  | 132 | { | 
|---|
|  | 133 | open_sbuf(object->name()); | 
|---|
|  | 134 | ostream o(sbuf_); | 
|---|
|  | 135 | o << "{" << endl; | 
|---|
|  | 136 | if (object->name()) { | 
|---|
|  | 137 | o << "define " << object->name() << endl; | 
|---|
|  | 138 | } | 
|---|
|  | 139 | if (object->transform().nonnull()) { | 
|---|
|  | 140 | o << "= INST" << endl; | 
|---|
|  | 141 | o << "transform {" << endl; | 
|---|
|  | 142 | for (int i=0; i<4; i++) { | 
|---|
|  | 143 | for (int j=0; j<4; j++) { | 
|---|
|  | 144 | o << scprintf(" %10.4f", object->transform()->transform()[j][i]); | 
|---|
|  | 145 | } | 
|---|
|  | 146 | o << endl; | 
|---|
|  | 147 | } | 
|---|
|  | 148 | o << "}" << endl; | 
|---|
|  | 149 | o << "geom {" << endl; | 
|---|
|  | 150 | } | 
|---|
|  | 151 | if (object->material().nonnull() | 
|---|
|  | 152 | ||object->appearance().nonnull()) { | 
|---|
|  | 153 | o << "appearance {" << endl; | 
|---|
|  | 154 | if (object->material().nonnull()) { | 
|---|
|  | 155 | o << "material {" << endl; | 
|---|
|  | 156 | if (object->material()->ambient().is_set()) { | 
|---|
|  | 157 | if (object->material()->ambient().overrides()) o << "*"; | 
|---|
|  | 158 | o << scprintf("ambient %10.4f %10.4f %10.4f", | 
|---|
|  | 159 | object->material()->ambient().value().red(), | 
|---|
|  | 160 | object->material()->ambient().value().green(), | 
|---|
|  | 161 | object->material()->ambient().value().blue()) | 
|---|
|  | 162 | << endl; | 
|---|
|  | 163 | } | 
|---|
|  | 164 | if (object->material()->diffuse().is_set()) { | 
|---|
|  | 165 | if (object->material()->diffuse().overrides()) o << "*"; | 
|---|
|  | 166 | o << scprintf("diffuse %10.4f %10.4f %10.4f", | 
|---|
|  | 167 | object->material()->diffuse().value().red(), | 
|---|
|  | 168 | object->material()->diffuse().value().green(), | 
|---|
|  | 169 | object->material()->diffuse().value().blue()) | 
|---|
|  | 170 | << endl; | 
|---|
|  | 171 | } | 
|---|
|  | 172 | o << "}" << endl; | 
|---|
|  | 173 | } | 
|---|
|  | 174 | o << "}" << endl; | 
|---|
|  | 175 | } | 
|---|
|  | 176 |  | 
|---|
|  | 177 | Render::render(object); | 
|---|
|  | 178 |  | 
|---|
|  | 179 | if (object->transform().nonnull()) { | 
|---|
|  | 180 | o << "}" << endl; | 
|---|
|  | 181 | } | 
|---|
|  | 182 | o << "}" << endl; | 
|---|
|  | 183 | close_sbuf(); | 
|---|
|  | 184 | } | 
|---|
|  | 185 |  | 
|---|
|  | 186 | void | 
|---|
|  | 187 | OOGLRender::set(const Ref<RenderedObjectSet>& set) | 
|---|
|  | 188 | { | 
|---|
|  | 189 | ostream o(sbuf_); | 
|---|
|  | 190 | o << "LIST" << endl; | 
|---|
|  | 191 | for (int i=0; i<set->n(); i++) { | 
|---|
|  | 192 | render(set->element(i)); | 
|---|
|  | 193 | } | 
|---|
|  | 194 | } | 
|---|
|  | 195 |  | 
|---|
|  | 196 | void | 
|---|
|  | 197 | OOGLRender::sphere(const Ref<RenderedSphere>& sphere) | 
|---|
|  | 198 | { | 
|---|
|  | 199 | if (oogl_spheres_) { | 
|---|
|  | 200 | ostream o(sbuf_); | 
|---|
|  | 201 | o << " = SPHERE 1.0 0.0 0.0 0.0" << endl; | 
|---|
|  | 202 | } | 
|---|
|  | 203 | else { | 
|---|
|  | 204 | Render::sphere(sphere); | 
|---|
|  | 205 | } | 
|---|
|  | 206 | } | 
|---|
|  | 207 |  | 
|---|
|  | 208 | void | 
|---|
|  | 209 | OOGLRender::polygons(const Ref<RenderedPolygons>& poly) | 
|---|
|  | 210 | { | 
|---|
|  | 211 | ostream o(sbuf_); | 
|---|
|  | 212 | if (poly->have_vertex_rgb()) { | 
|---|
|  | 213 | o << " = COFF" << endl; | 
|---|
|  | 214 | } | 
|---|
|  | 215 | else { | 
|---|
|  | 216 | o << " = OFF" << endl; | 
|---|
|  | 217 | } | 
|---|
|  | 218 | o << poly->nvertex() << " " | 
|---|
|  | 219 | << poly->nface() << " 0" << endl; | 
|---|
|  | 220 | int i; | 
|---|
|  | 221 | for (i=0; i<poly->nvertex(); i++) { | 
|---|
|  | 222 | o << scprintf(" %10.4f %10.4f %10.4f", | 
|---|
|  | 223 | poly->vertex(i,0), | 
|---|
|  | 224 | poly->vertex(i,1), | 
|---|
|  | 225 | poly->vertex(i,2)); | 
|---|
|  | 226 | if (poly->have_vertex_rgb()) { | 
|---|
|  | 227 | // The 1.0 is alpha | 
|---|
|  | 228 | o << scprintf(" %10.4f %10.4f %10.4f 1.0", | 
|---|
|  | 229 | poly->vertex_rgb(i,0), | 
|---|
|  | 230 | poly->vertex_rgb(i,1), | 
|---|
|  | 231 | poly->vertex_rgb(i,2)); | 
|---|
|  | 232 | } | 
|---|
|  | 233 | o << endl; | 
|---|
|  | 234 | } | 
|---|
|  | 235 | for (i=0; i<poly->nface(); i++) { | 
|---|
|  | 236 | o << " " << poly->nvertex_in_face(i); | 
|---|
|  | 237 | for (int j=0; j<poly->nvertex_in_face(i); j++) { | 
|---|
|  | 238 | o << " " << poly->face(i,j); | 
|---|
|  | 239 | } | 
|---|
|  | 240 | o << endl; | 
|---|
|  | 241 | } | 
|---|
|  | 242 | } | 
|---|
|  | 243 |  | 
|---|
|  | 244 | void | 
|---|
|  | 245 | OOGLRender::polylines(const Ref<RenderedPolylines>& poly) | 
|---|
|  | 246 | { | 
|---|
|  | 247 | int i; | 
|---|
|  | 248 | ostream o(sbuf_); | 
|---|
|  | 249 |  | 
|---|
|  | 250 | int nvertex= 0; | 
|---|
|  | 251 | for (i=0; i<poly->npolyline(); i++) nvertex += poly->nvertex_in_polyline(i); | 
|---|
|  | 252 | o << " = VECT" << endl; | 
|---|
|  | 253 | o << poly->npolyline() | 
|---|
|  | 254 | << " " << nvertex | 
|---|
|  | 255 | << " " << (poly->have_vertex_rgb()? nvertex:0) | 
|---|
|  | 256 | << endl; | 
|---|
|  | 257 | for (i=0; i<poly->npolyline(); i++) { | 
|---|
|  | 258 | o << " " << poly->nvertex_in_polyline(i); | 
|---|
|  | 259 | } | 
|---|
|  | 260 | o << endl; | 
|---|
|  | 261 | if (poly->have_vertex_rgb()) { | 
|---|
|  | 262 | for (i=0; i<poly->npolyline(); i++) { | 
|---|
|  | 263 | o << " " << poly->nvertex_in_polyline(i); | 
|---|
|  | 264 | } | 
|---|
|  | 265 | } | 
|---|
|  | 266 | else { | 
|---|
|  | 267 | for (i=0; i<poly->npolyline(); i++) { | 
|---|
|  | 268 | o << " 0"; | 
|---|
|  | 269 | } | 
|---|
|  | 270 | } | 
|---|
|  | 271 | o << endl; | 
|---|
|  | 272 | for (i=0; i<poly->npolyline(); i++) { | 
|---|
|  | 273 | for (int j=0; j<poly->nvertex_in_polyline(i); j++) { | 
|---|
|  | 274 | int ivertex = poly->polyline(i,j); | 
|---|
|  | 275 | o << scprintf(" %10.4f %10.4f %10.4f", | 
|---|
|  | 276 | poly->vertex(ivertex,0), | 
|---|
|  | 277 | poly->vertex(ivertex,1), | 
|---|
|  | 278 | poly->vertex(ivertex,2)) | 
|---|
|  | 279 | << endl; | 
|---|
|  | 280 | } | 
|---|
|  | 281 | } | 
|---|
|  | 282 | o << endl; | 
|---|
|  | 283 | if (poly->have_vertex_rgb()) { | 
|---|
|  | 284 | for (i=0; i<poly->npolyline(); i++) { | 
|---|
|  | 285 | for (int j=0; j<poly->nvertex_in_polyline(i); j++) { | 
|---|
|  | 286 | int ivertex = poly->polyline(i,j); | 
|---|
|  | 287 | o << scprintf(" %10.4f %10.4f %10.4f 1.0", | 
|---|
|  | 288 | poly->vertex_rgb(ivertex,0), | 
|---|
|  | 289 | poly->vertex_rgb(ivertex,1), | 
|---|
|  | 290 | poly->vertex_rgb(ivertex,2)) | 
|---|
|  | 291 | << endl; | 
|---|
|  | 292 | } | 
|---|
|  | 293 | } | 
|---|
|  | 294 | o << endl; | 
|---|
|  | 295 | } | 
|---|
|  | 296 | } | 
|---|
|  | 297 |  | 
|---|
|  | 298 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 299 |  | 
|---|
|  | 300 | // Local Variables: | 
|---|
|  | 301 | // mode: c++ | 
|---|
|  | 302 | // c-file-style: "CLJ" | 
|---|
|  | 303 | // End: | 
|---|
|  | 304 |  | 
|---|