home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / macview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  11.2 KB  |  461 lines

  1. /*
  2.  * Macview.c
  3.  */
  4. #include <MacHeaders>
  5. #include <Windows.h>
  6. #include <QuickDraw.h>
  7. #include <Dialogs.h>
  8.  
  9. #include "common.h"
  10. #include "light.h"
  11. #include "surface.h"
  12. #include "geom.h"
  13. #include "list.h"
  14. #include "grid.h"
  15. #include "sampling.h"
  16. #include "csg.h"
  17. #include "sphere.h"
  18. #include "triangle.h"
  19. #include "box.h"
  20. #include "disc.h"
  21. #include "cone.h"
  22. #include "cylinder.h"
  23. #include "plane.h"
  24. #include "sampling.h"
  25. #include "hf.h"
  26. #include "blob.h"
  27. #include "poly.h"
  28. #include "csg.h"
  29. #include "torus.h"
  30. #include "instance.h"
  31. #include "viewing.h"
  32.  
  33. #include "light.h"
  34. #include "point.h"
  35. #include "infinite.h"
  36. #include "spot.h"
  37. #include "jittered.h"
  38. #include "extended.h"
  39.  
  40. #include "macobject.h"
  41. #include "maceditor.h"
  42.  
  43. #define ZOOMVAL 60.
  44.  
  45. extern DialogPtr editorDialog ;
  46.  
  47. extern Geom *World, *hiliteobject ;
  48. extern Light *Lights ;
  49. extern RSCamera Camera ;
  50. extern WireObject *theObject ;
  51. extern Trans *transforms[MAXTRANSFORMS] ;
  52. extern short transdepth ;
  53. extern unsigned long FilterFlags ;
  54.  
  55. void DisplayWireObject(register char viewdir,register Geom *obj,register Surface *surf);
  56.  
  57. Vector crosshair, old_crosshair = {0.,0.,0.};
  58. static short ox,oy ;  /* The screen x,y position of the origin */
  59. Float zoom = ZOOMVAL ;
  60.  
  61. void DrawCoordinateInfo(Float x, Float y, Float z)
  62. {
  63.     short theItem, theType ;
  64.     DialogPtr theDialog ;
  65.     ControlHandle itemHandle ;
  66.     Rect itemRect ;
  67.     Point point ;
  68.     short currFont, currSize ;
  69.     GrafPtr currPort ;
  70.     Str255 value ;
  71.     char draw = 0 ;
  72.     
  73.     crosshair.x = x ; crosshair.y = y ; crosshair.z = z ;
  74.     
  75.     if(!editorDialog) return ;
  76.     
  77.     GetPort(&currPort) ;
  78.     SetPort(editorDialog) ;
  79.     currFont = editorDialog->txFont ;
  80.     currSize = editorDialog->txSize ;
  81.     TextSize(9) ;
  82.     TextFont(6) ;
  83.     GetDItem(editorDialog,coordinfoUI,&theType,&itemHandle,&itemRect) ;
  84.     EraseRect(&itemRect) ;
  85.     MoveTo(itemRect.left, itemRect.top+9) ;
  86.     sprintf(&value[1],"X:   %.2f",crosshair.x) ;
  87.     value[0] = strlen(&value[1]) ;
  88.     DrawString(value) ;
  89.     MoveTo(itemRect.left, itemRect.top+18) ;
  90.     sprintf(&value[1],"Y:   %.2f",crosshair.y) ; value[0] = strlen(&value[1]) ;
  91.     DrawString(value) ;
  92.     MoveTo(itemRect.left, itemRect.top+27) ;
  93.     sprintf(&value[1],"Z:   %.2f",crosshair.z) ; value[0] = strlen(&value[1]) ;
  94.     DrawString(value) ;
  95.     MoveTo(itemRect.left, itemRect.top+36) ;
  96.     sprintf(&value[1],"z:   %.2f",zoom) ; value[0] = strlen(&value[1]) ;
  97.     DrawString(value) ;
  98.  
  99.     TextFont(currFont) ;
  100.     TextSize(currSize) ;
  101.     SetPort(currPort) ;
  102. }
  103.  
  104.  
  105.  
  106.  
  107. void DrawCrossHair()
  108. {
  109.     ControlHandle itemHandle ;
  110.     short itype, theItem ;
  111.     Rect itemRect ;
  112.     GrafPtr currPort ;
  113.     short cx,cy, j, dx, dy ;
  114.     short penMode ;
  115.  
  116.     GetPort(&currPort) ;
  117.     SetPort(editorDialog) ;
  118.     penMode = editorDialog->pnMode ;
  119.     PenMode(patXor) ;
  120.  
  121.  
  122.     GetDItem(editorDialog,topUI,&itype,&itemHandle,&itemRect) ;
  123.     ClipRect(&itemRect) ;
  124.     for(j = 0 ; j < 2 ; j++) {
  125.         if(j==0)
  126.             ProjectCoords(&dx,&dy,old_crosshair.x,old_crosshair.y,old_crosshair.z,VIEW_TOP)
  127.         else
  128.             ProjectCoords(&dx,&dy,crosshair.x,crosshair.y,crosshair.z,VIEW_TOP)
  129.         cx = dx + (itemRect.left+(itemRect.right-itemRect.left)/2) ;
  130.         cy = dy + (itemRect.top+(itemRect.bottom-itemRect.top)/2) ;
  131.         MoveTo(cx,cy-5) ; LineTo(cx,cy+5) ;
  132.         MoveTo(cx-5,cy) ; LineTo(cx+5,cy) ;
  133.     }
  134.  
  135.     GetDItem(editorDialog,sideUI,&itype,&itemHandle,&itemRect) ;
  136.     ClipRect(&itemRect) ;
  137.     for(j = 0 ; j < 2 ; j++) {
  138.         if(j==0)
  139.             ProjectCoords(&dx,&dy,old_crosshair.x,old_crosshair.y,old_crosshair.z,VIEW_SIDE)
  140.         else
  141.             ProjectCoords(&dx,&dy,crosshair.x,crosshair.y,crosshair.z,VIEW_SIDE)
  142.         cx = dx + (itemRect.left+(itemRect.right-itemRect.left)/2) ;
  143.         cy = dy + (itemRect.top+(itemRect.bottom-itemRect.top)/2) ;
  144.         MoveTo(cx,cy-5) ; LineTo(cx,cy+5) ;
  145.         MoveTo(cx-5,cy) ; LineTo(cx+5,cy) ;
  146.     }
  147.  
  148.     GetDItem(editorDialog,frontUI,&itype,&itemHandle,&itemRect) ;
  149.     ClipRect(&itemRect) ;
  150.     for(j = 0 ; j < 2 ; j++) {
  151.         if(j==0)
  152.             ProjectCoords(&dx,&dy,old_crosshair.x,old_crosshair.y,old_crosshair.z,VIEW_FRONT)
  153.         else
  154.             ProjectCoords(&dx,&dy,crosshair.x,crosshair.y,crosshair.z,VIEW_FRONT)
  155.         cx = dx + (itemRect.left+(itemRect.right-itemRect.left)/2) ;
  156.         cy = dy + (itemRect.top+(itemRect.bottom-itemRect.top)/2) ;
  157.         MoveTo(cx,cy-5) ; LineTo(cx,cy+5) ;
  158.         MoveTo(cx-5,cy) ; LineTo(cx+5,cy) ;
  159.     }
  160.     old_crosshair = crosshair ;
  161.  
  162.     ClipRect(&editorDialog->portRect) ;
  163.     PenMode(penMode) ;
  164.     SetPort(currPort) ;
  165. }
  166.  
  167. void DrawLights(char mode)
  168. {
  169.     register Light *l ;
  170.     short cx,cy ;
  171.     Vector pos ;
  172.     Rect rect ;
  173.  
  174.     if(FILTER_SET(F_LIGHTS))
  175.         return ;
  176.     for(l = Lights ; l ; l = l->next) {
  177.         switch(LightType(l)) {
  178.         case L_POINT:
  179.             pos = ((Pointlight *)l->light)->pos ;
  180.             ProjectCoords(&cx,&cy,pos.x,pos.y,pos.z,mode) ;
  181.             SetRect(&rect,ox+cx-6,oy+cy-6,ox+cx+6,oy+cy+6) ;
  182.             FrameOval(&rect) ;
  183.             MoveTo(ox+cx-2,oy+cy+3) ;
  184.             DrawString("\pP") ;
  185.             break ;
  186.         case L_INFINITE:
  187.             break ;
  188.         case L_SPOT:
  189.             pos = ((Spotlight *)l->light)->pos ;
  190.             ProjectCoords(&cx,&cy,pos.x,pos.y,pos.z,mode) ;
  191.             SetRect(&rect,ox+cx-6,oy+cy-6,ox+cx+6,oy+cy+6) ;
  192.             FrameOval(&rect) ;
  193.             MoveTo(ox+cx-2,oy+cy+3) ;
  194.             DrawString("\pS") ;
  195.             break ;
  196.         case L_JITTERED:
  197.             pos = ((Jittered  *)l->light)->pos ;
  198.             ProjectCoords(&cx,&cy,pos.x,pos.y,pos.z,mode) ;
  199.             SetRect(&rect,ox+cx-6,oy+cy-6,ox+cx+6,oy+cy+6) ;
  200.             FrameOval(&rect) ;
  201.             MoveTo(ox+cx-2,oy+cy+3) ;
  202.             DrawString("\pJ") ;
  203.             break ;
  204.         case L_EXTENDED:
  205.             pos = ((Extended *)l->light)->pos ;
  206.             ProjectCoords(&cx,&cy,pos.x,pos.y,pos.z,mode) ;
  207.             SetRect(&rect,ox+cx-6,oy+cy-6,ox+cx+6,oy+cy+6) ;
  208.             FrameOval(&rect) ;
  209.             MoveTo(ox+cx-2,oy+cy+3) ;
  210.             DrawString("\pE") ;
  211.             break ;
  212.         }
  213.     }
  214. }
  215.  
  216. void DrawCamera(char mode)
  217. {
  218.     short cx,cy ;
  219.  
  220.  
  221.     if(FILTER_SET(F_CAMERA))
  222.         return;
  223.     ProjectCoords(&cx,&cy,Camera.pos.x,Camera.pos.y,Camera.pos.z,mode) ;
  224.     MoveTo(ox+cx-2,oy+cy+3) ;
  225.     DrawString("\po") ;
  226.     ProjectCoords(&cx,&cy,Camera.lookp.x,Camera.lookp.y,Camera.lookp.z,mode) ;
  227.     MoveTo(ox+cx-2,oy+cy+3) ;
  228.     DrawString("\pt") ;
  229. }
  230.  
  231. pascal void DrawTopView(WindowPtr window, short item)
  232. {
  233.     short theItem, theType ;
  234.     ControlHandle theHandle ;
  235.     Rect itemRect ;
  236.     GrafPtr currPort ;
  237.     short cx, cy, dx,dy, penMode, currFont, currSize ;
  238.     
  239.     GetPort(&currPort) ;
  240.     SetPort(window) ;
  241.     penMode = window->pnMode ;
  242.     currFont = editorDialog->txFont ;
  243.     currSize = editorDialog->txSize ;
  244.     TextSize(9) ;
  245.     TextFont(6) ;
  246.  
  247.     GetDItem(window,item,&theType,&theHandle,&itemRect) ;
  248.     ClipRect(&itemRect) ;
  249.     EraseRect(&itemRect) ;
  250.     ox = itemRect.left+(itemRect.right-itemRect.left)/2 ;
  251.     oy = itemRect.top+(itemRect.bottom-itemRect.top)/2 ;
  252.     transdepth = 0 ;
  253.     DrawObject(World,World->surf,VIEW_TOP) ;
  254.     DrawLights(VIEW_TOP) ;
  255.     DrawCamera(VIEW_TOP) ;
  256.  
  257.     PenMode(patXor) ;
  258.     ProjectCoords(&dx,&dy,crosshair.x,crosshair.y,crosshair.z,VIEW_TOP)
  259.     cx = ox + dx;
  260.     cy = oy + dy;
  261.     MoveTo(cx,cy-5) ; LineTo(cx,cy+5) ;
  262.     MoveTo(cx-5,cy) ; LineTo(cx+5,cy) ;
  263.  
  264.     MoveTo(itemRect.left+3, itemRect.bottom-3) ;
  265.     DrawString("\pTop") ;
  266.  
  267.     TextFont(currFont) ;
  268.     TextSize(currSize) ;
  269.     PenMode(penMode) ;
  270.     ClipRect(&window->portRect) ;    
  271.     SetPort(currPort) ;    
  272. }
  273.  
  274.  
  275. pascal void DrawSideView(WindowPtr window, short item)
  276. {
  277.     short theItem, theType ;
  278.     ControlHandle theHandle ;
  279.     Rect itemRect ;
  280.     GrafPtr currPort ;
  281.     short cx, cy, dx, dy, penMode, currFont, currSize ;
  282.     
  283.     GetPort(&currPort) ;
  284.     SetPort(window) ;
  285.     penMode = window->pnMode ;
  286.     currFont = editorDialog->txFont ;
  287.     currSize = editorDialog->txSize ;
  288.     TextSize(9) ;
  289.     TextFont(6) ;
  290.  
  291.     GetDItem(window,item,&theType,&theHandle,&itemRect) ;
  292.     ClipRect(&itemRect) ;
  293.     EraseRect(&itemRect) ;
  294.     ox = itemRect.left+(itemRect.right-itemRect.left)/2 ;
  295.     oy = itemRect.top+(itemRect.bottom-itemRect.top)/2 ;
  296.     transdepth = 0 ;
  297.     DrawObject(World,World->surf,VIEW_SIDE) ;
  298.     DrawLights(VIEW_SIDE) ;
  299.     DrawCamera(VIEW_SIDE) ;
  300.  
  301.     PenMode(patXor) ;
  302.     ProjectCoords(&dx,&dy,crosshair.x,crosshair.y,crosshair.z,VIEW_SIDE)
  303.     cx = ox + dx;
  304.     cy = oy + dy;
  305.     MoveTo(cx,cy-5) ; LineTo(cx,cy+5) ;
  306.     MoveTo(cx-5,cy) ; LineTo(cx+5,cy) ;
  307.     
  308.     MoveTo(itemRect.left+3, itemRect.bottom-3) ;
  309.     DrawString("\pSide") ;
  310.  
  311.     TextFont(currFont) ;
  312.     TextSize(currSize) ;
  313.     PenMode(penMode) ;
  314.     ClipRect(&window->portRect) ;    
  315.     SetPort(currPort) ;    
  316. }
  317.  
  318. pascal void DrawFrontView(WindowPtr window, short item)
  319. {
  320.     short theItem, theType ;
  321.     ControlHandle theHandle ;
  322.     Rect itemRect ;
  323.     GrafPtr currPort ;
  324.     short cx, cy, dx, dy, penMode, currFont, currSize ;
  325.     
  326.     GetPort(&currPort) ;
  327.     SetPort(window) ;
  328.     penMode = window->pnMode ;
  329.     currFont = editorDialog->txFont ;
  330.     currSize = editorDialog->txSize ;
  331.     TextSize(9) ;
  332.     TextFont(6) ;
  333.  
  334.     GetDItem(window,item,&theType,&theHandle,&itemRect) ;
  335.     ClipRect(&itemRect) ;
  336.     EraseRect(&itemRect) ;
  337.     ox = itemRect.left+(itemRect.right-itemRect.left)/2 ;
  338.     oy = itemRect.top+(itemRect.bottom-itemRect.top)/2 ;
  339.     transdepth = 0 ;
  340.     DrawObject(World,World->surf,VIEW_FRONT) ;
  341.     DrawLights(VIEW_FRONT) ;
  342.     DrawCamera(VIEW_FRONT) ;
  343.  
  344.     PenMode(patXor) ;
  345.     ProjectCoords(&dx,&dy,crosshair.x,crosshair.y,crosshair.z,VIEW_FRONT)
  346.     cx = ox + dx;
  347.     cy = oy + dy;
  348.     MoveTo(cx,cy-5) ; LineTo(cx,cy+5) ;
  349.     MoveTo(cx-5,cy) ; LineTo(cx+5,cy) ;
  350.  
  351.     MoveTo(itemRect.left+3, itemRect.bottom-3) ;
  352.     DrawString("\pFront") ;
  353.     TextFont(currFont) ;
  354.     TextSize(currSize) ;
  355.     PenMode(penMode) ;
  356.     ClipRect(&window->portRect) ;    
  357.     SetPort(currPort) ;    
  358. }
  359.  
  360. void DisplayView()
  361. {
  362.     GrafPtr currPort ;
  363.     
  364.     GetPort(&currPort) ;
  365.     DrawSideView(editorDialog,sideUI) ;
  366.     DrawTopView(editorDialog,topUI) ;
  367.     DrawFrontView(editorDialog,frontUI) ;
  368.     SetPort(currPort) ;    
  369. }
  370.  
  371.  
  372. /* Displays a heightfield object */
  373.  
  374. void DisplayHfObject(Hf *hf, char viewdir)
  375. {
  376.     RSMatrix mat ;
  377.     short x,y, i,j ;
  378.     Vector pos ;
  379.     Trans *trans ;
  380.  
  381.  
  382.     /* Combine all transformations together */
  383.     MatrixInit(&mat) ;
  384.     for(i = transdepth-1 ; i >= 0 ; i--) {
  385.         trans = transforms[i] ;
  386.         while(trans) {
  387.             MatrixMult(&mat,&trans->trans,&mat) ;
  388.             trans = trans->next ;    
  389.         }
  390.     }
  391.     
  392.     for(i = 0 ; i < hf->size; i++) {
  393.         for(j = 0 ; j < hf->size; j++) {
  394.             pos.x = (Float) i / hf->size ;
  395.             pos.y = (Float) j / hf->size ;
  396.             pos.z = hf->data[i][j] ;
  397.             if(pos.z > HF_UNSET) {    
  398.                 PointTransform(&pos,&mat) ;
  399.                 ProjectCoords(&x, &y, pos.x,pos.y,pos.z,viewdir) ;
  400.                 MoveTo(ox+x,oy+y) ;
  401.                 Line(0,0) ;
  402.             }
  403.         }
  404.     }
  405. }
  406.  
  407. /* Display a wireframe object */
  408.  
  409. void DisplayWireObject(register char viewdir,register Geom *obj,register Surface *surf)
  410. {
  411.     register short i,j ;
  412.     register Trans *trans ;
  413.     GrafPtr currPort ;
  414.     RGBColor currCol, newCol ;
  415.     RSMatrix mat ;
  416.  
  417.     GetPort(&currPort) ;
  418.     SetPort(editorDialog) ;
  419.     GetForeColor(&currCol) ;
  420.     if(obj->surf) {
  421.         surf = obj->surf ;
  422.         newCol.red = (unsigned short) (surf->amb.r * (Float) 65535.0) ;
  423.         newCol.green = (unsigned short) (surf->amb.g * (Float) 65535.0) ;
  424.         newCol.blue = (unsigned short) (surf->amb.b * (Float) 65535.0) ;
  425.     }
  426.     else {
  427.         newCol.red = 0 ;
  428.         newCol.green = 0 ;
  429.         newCol.blue = 0 ;
  430.     }
  431.     RGBForeColor(&newCol) ;
  432.     
  433.     /* Combine all transformations together */
  434.     MatrixInit(&mat) ;
  435.     for(i = transdepth-1 ; i >= 0 ; i--) {
  436.         trans = transforms[i] ;
  437.         while(trans) {
  438.             MatrixMult(&mat,&trans->trans,&mat) ;
  439.             trans = trans->next ;    
  440.         }
  441.     }
  442.     
  443.     /* Now transform each point in our matrix and convert into the correct 2d screen coordinate */
  444.     for(i = 0 ; i < theObject->numpoints ; i++) {
  445.         PointTransform(&theObject->points[i],&mat) ;
  446.         ProjectCoords(&theObject->sxpoints[i], &theObject->sypoints[i], theObject->points[i].x,theObject->points[i].y,theObject->points[i].z,viewdir) ;
  447.     }
  448.  
  449.     /* Display each edge in the object */
  450.     for(i = 0 ; i < theObject->numpoints ; i++)
  451.         for(j = 0 ; j < theObject->numpoints ; j++) {
  452.             if(EDGE_BETWEEN(i,j)) {
  453.                 MoveTo(theObject->sxpoints[i]+ox,theObject->sypoints[i]+oy) ;
  454.                 LineTo(theObject->sxpoints[j]+ox,theObject->sypoints[j]+oy) ;
  455.             }
  456.         }
  457.     RGBForeColor(&currCol) ;
  458.     SetPort(currPort) ;
  459. }
  460.  
  461.