home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / basewndw.c next >
Encoding:
C/C++ Source or Header  |  1992-10-20  |  5.2 KB  |  224 lines

  1. /*
  2.  * BaseWindow.C - abstract base class for window support.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include "BaseWindow.h"
  19. #include "list.h"
  20. #include "Error.h"
  21. #include "Options.h"
  22.  
  23. //___________________________________________________________ LineSegment
  24.  
  25. struct LineSegment
  26. {
  27.   LineSegment(const Vector& p1, const Vector& p2)
  28.   : from(p1), to(p2) {}
  29.  
  30.   Vector from, to;
  31. };
  32.  
  33. typedef LineSegment* LineSegmentPtr;
  34. declareList(LineList, LineSegmentPtr);
  35. implementList(LineList, LineSegmentPtr);
  36.  
  37. //___________________________________________________________ BaseWindow
  38.  
  39. BaseWindow::BaseWindow()
  40. : view(NULL), resX(400), resY(400), 
  41.   lines(NULL), polys(NULL),
  42.   windowIsOpen(0), buffering(1)
  43. {}
  44.  
  45. BaseWindow::~BaseWindow()
  46. {
  47.   if (view != NULL)  
  48.     delete view;
  49.   clearBuffer();
  50.   if (lines != NULL)
  51.     delete lines;
  52.   if (polys != NULL)
  53.     delete polys;
  54. }
  55.  
  56. void BaseWindow::open(int width, int height, const rcString& windowName)
  57. {
  58.   if (windowIsOpen)
  59.     Error(ERR_PANIC, "Window is already open");
  60.  
  61.   windowIsOpen = 1;
  62.   resX = width;
  63.   resY = height;
  64.   name = windowName;
  65. }
  66.  
  67. void BaseWindow::close()
  68. {
  69.   if (!windowIsOpen)
  70.     Error(ERR_PANIC, "close window: but window is not open");
  71.   windowIsOpen = 0;
  72. }
  73.  
  74. void BaseWindow::clear()
  75. {
  76.   if (!windowIsOpen)
  77.     Error(ERR_PANIC, "clear window: but window is not open");
  78. }
  79.  
  80. /*
  81.  * If buffering is enabled store the given line segment in the
  82.  * line list. Otherwise transform the segment to screen coordinates
  83.  * and show it on the screen.
  84.  */
  85.  
  86. void BaseWindow::line(const Vector& from, const Vector& to)
  87. {
  88.   if (buffering) {
  89.     if (lines == NULL)
  90.       lines = new LineList(1000);
  91.     lines->append(new LineSegment(from, to));
  92.   }
  93.   else if (view == NULL) {
  94.     Error(ERR_PANIC, "no view parameter set");
  95.   }
  96.  
  97.   /*
  98.    * Transform and draw the line without buffering.
  99.    */
  100.   else { 
  101.     Vector x = view->transformWorld2Screen(from);
  102.     Vector y = view->transformWorld2Screen(to);
  103.     drawLine((int)x[0], (int)x[1], (int)y[0], (int)y[1]);
  104.   }
  105. }
  106.  
  107. /*
  108.  * If buffering is enabled store the given polygon in the
  109.  * poly list. Otherwise transform the polygon to screen coordinates
  110.  * and show it on the screen.
  111.  */
  112.  
  113. void BaseWindow::polygon(Polygon* p)
  114. {
  115.   if (buffering) {
  116.     if (polys == NULL)
  117.       polys = new PolygonList(1000);
  118.     polys->append(p);
  119.   }
  120.   else if (view == NULL) {
  121.     Error(ERR_PANIC, "no view parameter set");
  122.   }
  123.   else if (p->numVertices() >= 3) { 
  124.     Vector x = view->transformWorld2Screen(p->vertex(0));
  125.     Vector y;
  126.  
  127.     for (register long i=1; i<p->numVertices(); i++) {
  128.       y = view->transformWorld2Screen(p->vertex(i));
  129.       drawLine((int)x[0], (int)x[1], (int)y[0], (int)y[1]);
  130.       x = y;
  131.     }
  132.     y = view->transformWorld2Screen(p->vertex(0));
  133.     drawLine((int)x[0], (int)x[1], (int)y[0], (int)y[1]);
  134.   }  
  135.  
  136.   if (!buffering)
  137.     delete p;
  138. }
  139.  
  140. /*
  141.  * For all the line segment in the line list and all the polys in the
  142.  * polygon list, transform and bring dem to screen.
  143.  */
  144.  
  145. void BaseWindow::flush()
  146. {
  147.   if (view == NULL)
  148.     Error(ERR_PANIC, "no view parameter set");
  149.  
  150.   Vector from, to;
  151.   if (lines != NULL) {
  152.     for (register long i=0; i<lines->count(); i++) {
  153.       from = view->transformWorld2Screen(lines->item(i)->from);
  154.       to   = view->transformWorld2Screen(lines->item(i)->to);
  155.       drawLine((int)from[0], (int)from[1], (int)to[0], (int)to[1]);
  156.     }
  157.   }
  158.   
  159.   if (polys != NULL) {
  160.     for (register long i=0; i<polys->count(); i++) {
  161.       Polygon* p = polys->item(i);
  162.       if (p->numVertices() >= 3) {
  163.     from = view->transformWorld2Screen(p->vertex(0));
  164.     for (register long j=1; j<p->numVertices(); j++) {
  165.       to   = view->transformWorld2Screen(p->vertex(j));
  166.       drawLine((int)from[0], (int)from[1], (int)to[0], (int)to[1]);
  167.       from = to;
  168.     }
  169.     to = view->transformWorld2Screen(p->vertex(0));
  170.     drawLine((int)from[0], (int)from[1], (int)to[0], (int)to[1]);
  171.       }
  172.     }
  173.   }
  174. }
  175.  
  176. void BaseWindow::disableBuffering()
  177.   buffering = 0; 
  178. }
  179.  
  180. void BaseWindow::enableBuffering()
  181. {
  182.   buffering = 1; 
  183. }
  184.  
  185. void BaseWindow::setView(const Vector& eye, const Vector& lookat, const Vector& up, 
  186.              real fov)
  187. {
  188.   if (view != NULL)
  189.     delete view;
  190.   view = new ViewTransform(eye, lookat, up, fov, resX, resY);
  191.  
  192.   if (theOptions.verbose)
  193.     cerr << *view << '\n';
  194. }
  195.  
  196. void BaseWindow::setView(const BoundingBox& bbox, const Vector& up, real fov)
  197. {
  198.   if (view != NULL)
  199.     delete view;
  200.   view = new ViewTransform(bbox, up, fov, resX, resY);
  201.  
  202.   if (theOptions.verbose)
  203.     cerr << *view << '\n';
  204. }
  205.  
  206. /*
  207.  * Remove all the line segment und polygon from the buffer.
  208.  */
  209.  
  210. void BaseWindow::clearBuffer()
  211. {
  212.   if (lines != NULL) {
  213.     for (register long i=0; i<lines->count(); i++)
  214.       delete lines->item(i);
  215.     lines->remove_all();
  216.   }
  217.   if (polys != NULL) {
  218.     for (register long i=0; i<polys->count(); i++)
  219.       delete polys->item(i);
  220.     polys->remove_all();
  221.   }
  222. }
  223.