home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pstoedit.zip / source.zip / pstoedit.2.50 / src / drvlwo.cpp < prev    next >
C/C++ Source or Header  |  1996-10-29  |  5KB  |  186 lines

  1. /* 
  2.    drvlwo.cpp - Driver to output LightWave 3D Objects (LWO)
  3.              - written by Glenn M. Lewis (glewis@c2.net) - 6/18/96
  4.            http://www.c2.net/~glewis/
  5.            Based on... 
  6.  
  7.    drvSAMPL.cpp : This file is part of pstoedit
  8.    Skeleton for the implementation of new backends
  9.  
  10.    Copyright (C) 1993,1994,1995,1996 Wolfgang Glunz, Wolfgang.Glunz@zfe.siemens.de
  11.  
  12.     This program is free software; you can redistribute it and/or modify
  13.     it under the terms of the GNU General Public License as published by
  14.     the Free Software Foundation; either version 2 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software
  24.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "drvlwo.h"
  32.  
  33. drvLWO::drvLWO(const char * driveroptions_p,ostream & theoutStream,ostream & theerrStream): // Constructor
  34.   drvbase(driveroptions_p,theoutStream,theerrStream,
  35.       0, // if backend supports subpathes, else 0
  36.       // if subpathes are supported, the backend must deal with
  37.       // sequences of the following form
  38.       // moveto (start of subpath)
  39.       // lineto (a line segment)
  40.       // lineto 
  41.       // moveto (start of a new subpath)
  42.       // lineto (a line segment)
  43.       // lineto 
  44.       //
  45.       // If this argument is set to 0 each subpath is drawn 
  46.       // individually which might not necessarily represent
  47.       // the original drawing.
  48.  
  49.       0,  // if backend supports curves, else 0
  50.       0  // if backend supports elements with fill and edges
  51.       )
  52. {
  53.   // driver specific initializations
  54.   // and writing of header to output file
  55.   total_vertices = 0;
  56.   polys = 0;
  57.   total_polys = 0;
  58.  
  59.   //    float           scale;
  60.   //    float           x_offset;
  61.   //    float           y_offset;
  62. }
  63.  
  64. drvLWO::~drvLWO()
  65. {
  66.   unsigned long total_bytes = 0;
  67.   unsigned long count = 0;
  68.   LWO_POLY *p;
  69.  
  70.   // driver specific deallocations
  71.   // and writing of trailer to output file
  72.   outf << "FORM";
  73.  
  74.   total_bytes = 12L;  // LWOBPNTS+size
  75.   total_bytes += (12L*total_vertices);  // PNTS section...
  76.   total_bytes += 8L;  // POLS+size
  77.   total_bytes += (4L*total_polys + 2L*total_vertices);  // POLS section...
  78.   out_ulong(outf, total_bytes);  // Total file size (-8)
  79.   outf << "LWOBPNTS";
  80.   out_ulong(outf, 12L*total_vertices);
  81.   // Output vertices...
  82.   if (total_vertices > 65536) {
  83.     errf << "ERROR!  Generated more than 65536 vertices!!!  Abort.";
  84.     return;
  85.   }
  86.   for (p = polys; p; p=p->next) {
  87.     for (unsigned long n = 0; n<p->num; n++) {
  88.       out_float(outf, p->x[n]);
  89.       out_float(outf, p->y[n]);
  90.       out_float(outf, 0.0);
  91.     }
  92.   }
  93.  
  94.   // Now, output polygons...
  95.   outf << "POLS";
  96.   out_ulong(outf, 4L*total_polys + 2L*total_vertices);
  97.   count = 0;
  98.   for (p = polys; p; p=p->next) {
  99.     out_ushort(outf, p->num);
  100.     for (unsigned long n = 0; n<p->num; n++)
  101.       out_ushort(outf, count + n);
  102.     count += p->num;
  103.     out_ushort(outf, 0);  // which surface
  104.   }
  105. }
  106.  
  107. void drvLWO::print_coords()
  108. {
  109.   LWO_POLY *p = new LWO_POLY;
  110.   p->r = (unsigned char)(255.0 * currentR());
  111.   p->g = (unsigned char)(255.0 * currentG());
  112.   p->b = (unsigned char)(255.0 * currentB());
  113.   p->num = numberOfElementsInPath();
  114.   p->x = new double[p->num];
  115.   p->y = new double[p->num];
  116.   p->next = polys;
  117.   polys = p;
  118.   total_polys++;
  119.   total_vertices += p->num;
  120.  
  121.   for (unsigned int n = 0; n < numberOfElementsInPath(); n++) {
  122.     const basedrawingelement & elem = pathElement(n);
  123.     switch (elem.getType()) {
  124.     case moveto: {
  125.       const Point & pe = elem.getPoint(0);
  126.       // outf << "\t\tmoveto ";
  127.       p->x[n] = pe.x_ + x_offset;
  128.       p->y[n] = pe.y_ + y_offset;
  129.     }
  130.     break;
  131.     case lineto: {
  132.       const Point & pe = elem.getPoint(0);
  133.       // outf << "\t\tlineto ";
  134.       p->x[n] = pe.x_ + x_offset;
  135.       p->y[n] = pe.y_ + y_offset;
  136.     }
  137.     break;
  138.     case closepath: // Not supported
  139.       // outf << "\t\tclosepath ";
  140.       break;
  141.     case curveto:{  // Not supported
  142.     }
  143.     break;
  144.     default:
  145.       errf << "\t\tFatal: unexpected case in drvpdf " << endl;
  146.       abort();
  147.       break;
  148.     }
  149.     //    outf << endl;
  150.   }
  151.   // outf << "]" << endl;
  152. }
  153.  
  154.  
  155. void drvLWO::open_page()
  156. {
  157.   //  outf << "Opening page: " << currentPageNumber << endl;
  158. }
  159.  
  160. void drvLWO::close_page()
  161. {
  162.   //  outf << "Closing page: " << (currentPageNumber) << endl;
  163. }
  164.  
  165. void drvLWO::show_text(const TextInfo & textinfo )
  166. {
  167.   unused(&textinfo);
  168.   // Must use the -dt flag for this, since RenderMan doesn't support text
  169. }
  170.  
  171. void drvLWO::show_path()
  172. {
  173.   print_coords();
  174. };
  175.  
  176. void drvLWO::show_rectangle(const float llx, const float lly, const float urx, const float ury)
  177. {
  178.   // outf << "Rectangle ( " << llx << "," << lly << ") (" << urx << "," << ury << ")" << endl;
  179.   // just do show_path for a first guess
  180.   unused(&llx);
  181.   unused(&lly);
  182.   unused(&urx);
  183.   unused(&ury);
  184.   show_path();
  185. }
  186.