Author Topic: Yet Another 3D Viewer  (Read 10905 times)

bdeck

  • Hero Member
  • *****
  • Posts: 896
Yet Another 3D Viewer
« on: April 12, 2014, 10:37:16 PM »
Here's my take on a 3d viewing macro where the viewer never ends up standing on his/her head.

Viewer moves up, down, right and left by using the arrow keys.  The "h" key displays a help menu

Still needs lighting control......maybe someone else will provide that.

bd


Edit 10/24/17  see message 49 in this thread for latest version 

Code: [Select]
' vdd.d3m   macro to manage 3d views   bdeck   4/2014
'
' View Down from Dome
' If any entities have been previously selected, the selection center will be the view center.
' Otherwise the drawing center will be the view center.
' Except for panning, all operations are performed using the keyboard.
'
' Viewer remains upright while moving along the surface of a shere
' without passing north or south poles.
'
' Hit the h key for a menu of key commands.
'

da=5 ' increment or decrement rotation angle when a key is pressed
df=1.25 ' distance factor
zf=1.25  ' zoom factor
sys(470) =1  ' prevent other commands from running during this macro

if sys(80)>0 then  ' rotate viewer about center of selection
gosub Vselectcenter
>Deselect
{
}
else ' rotate viewer about center of drawing
gosub Vdwgcenter
endif

' -------- list of key values ----------
k_r=82
k_spc=32
k_f=70
k_a=65
k_q=81
k_w=87
k_z=90
k_x=88
k_s=83
k_d=68
k_esc=27
k_cr=13
k_g=71
k_h=72
k_c=67
k_k=75
k_l=76
k_m=77
k_n=78
k_p=80
k_t=84
k_e=69
k_v=86
k_b=66
k_u=85
k_up=38
k_dn=40
k_rt=39
k_lft=37
k_spc=32
k_shft=16
k_cntrl=17
k_click=1
k_plus=107
k_minus=109
k_plusA=187    ' use keycode for =
k_minusA=189
k_esc=27
k_cr=13
k_zero=96
k_zeroA=48


' Get starting angles, distance, and type of 3d view

ax=sys(205)
ay=sys(206)
az=sys(207)
d=sys(208)
t=sys(209)

do while k<>k_esc

ax=ax mod 360 ' limit range of viewing angles displayed
ay=ay mod 360 ' limit range of viewing angles displayed

Precision 2
print " ax, ay, az  = ", ax, ay, az, " d = ", d, " iso= ", t, " shade = ",s2 ,"      Hit  h key for menu,    esc to exit"

anykey k

if k=k_up then ax=ax+da
if k=k_dn then ax=ax-da
if k=k_rt then ay=ay+da
if k=k_lft then ay=ay-da

if k=k_spc then gosub panit

if ax>90 then ax=90 'do not go beyond north pole
if ax<-90 then ax=-90 'do not go beyond south pole

if k=k_f then gosub farther 'larger distance
if k=k_n then gosub nearer 'smaller distance

if (k=k_plus or k=k_plusA) then gosub bigger 'narrower lens angle
if (k=k_minus or k=k_minusA) then gosub smaller 'wider lens angle

if k=u then az=0 ' make viewer upright
if (k=k_zero or k=k_zeroA) then gosub zeroit ' zero all viewing angles

if k=k_p then gosub isotoggle ' toggle perspective and isometric view modes
if k=k_h then gosub helper ' show help screen
if k=k_w then gosub winfit ' fit drawing to window

sys(205)=ax
sys(206)=ay
sys(207)=az

if k=k_s then
gosub shadetoggle
else
regen
endif
loop
end

shadeToggle:
s2=(sys(2)+1) mod 5
sys(2)=s2
return

zeroit:
ax=0
ay=0
az=0
return

bigger:
>Zoom
{
<Zoomfactor [zf]
}
return

smaller:
>Zoom
{
<Zoomfactor [1/zf]
}
return

nearer:
d=d/df
sys(208) =d
return

farther:
d=d*df
sys(208) =d
return

isotoggle:
t=(t+1) mod 2
sys(209)=t
return

winfit:
>fittowindow
{
}
return

panit:
'allow user to set points or drag view as desired
>Pan
{
<incomplete
}

return

helper:
sys$(42)="Help Menu"
WINDOW 13, 64

print
TAB 2
print "arrows";
TAB 10
print "move viewer up, down, left right on spherical dome surface"
TAB 2
print "spc";
TAB 10
print "Pan view by setting points or dragging"
TAB 2
print "p";
TAB 10
print "toggle perspective/ isometric view"
TAB 2
print "u";
TAB 10
print  "upright viewer (z angle =0)"
TAB 2
print "0";
TAB 10
print  "set all viewing angles to zero" 
TAB 2
print "w";
TAB 10
print  "fit entire drawing to window"
TAB 2
print "s";
TAB 10
print  "cycle shade modes"
TAB 2
print "+ / -";
TAB 10
print  "zoom bigger / smaller"
TAB 2
print "n / f";
TAB 10
print  "camera nearer / farther"
        TAB 2
print "esc";
TAB 10
print  "exit this macro"
        print
TAB 2
print "Hit any key to exit help menu"
anykey
WCLOSE
return


Vdwgcenter:
xc = 0.5*(sys(120) + sys(122))
yc = 0.5*(sys(121) + sys(123))
zc = 0.5*(Sys(124) + sys(125))
>SetViewerPoints
{
        <Pointxyz [xc, yc, zc]
}
return

Vselectcenter:
xc = 0.5*(sys(196) + sys(198))
yc = 0.5*(sys(197) + sys(199))
zc = 0.5*(Sys(200) + sys(201))
>SetViewerPoints
{
        <Pointxyz [xc, yc, zc]
}
return
« Last Edit: October 24, 2017, 07:22:01 PM by bdeck »

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
Re: Yet Another 3D Viewer
« Reply #1 on: April 13, 2014, 03:10:59 AM »
Bd, now that is a keeper!!  Great job.  I love the arrow keys and the +/-, so intuitive. 

Did the hairy Borland C awake the coding bug?  Whatever the reason, thanks for sharing.




« Last Edit: April 13, 2014, 04:59:42 AM by prl »

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
Re: Yet Another 3D Viewer
« Reply #2 on: April 13, 2014, 04:01:52 AM »
I added some self-playing test movies in the zip file.   Folks, you need to try this routine!  I'm sitting in my chair jumping up and down.  The perspective toggle with the n/f is fun.

« Last Edit: April 13, 2014, 04:43:23 AM by prl »

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
Re: Yet Another 3D Viewer
« Reply #3 on: April 13, 2014, 06:18:47 AM »
Still needs lighting control......maybe someone else will provide that.

Bd, picking your brain.  If you could control lighting, what (generally speaking) would you do?

SFA

  • Full Member
  • ***
  • Posts: 227
  • *
    • Stanley Fyffe Associates
Re: Yet Another 3D Viewer
« Reply #4 on: April 13, 2014, 06:36:44 AM »
I agree.  This is great! :)
SEMPER IN EXCRETIA

bdeck

  • Hero Member
  • *****
  • Posts: 896
Re: Yet Another 3D Viewer
« Reply #5 on: April 13, 2014, 07:41:30 AM »
Hi prl, SFA,

Thanks for the kind words.

[Bd, picking your brain.  If you could control lighting, what (generally speaking) would you do?

I would probably have a toggle between fixed lighting and lighting that moves with the viewer.

I might also add a third mode where lighting can be rotated independently about the y axis.

I'd start with a good default lighting system and maybe add or subtract a few preconfigured key lights.

I added some self-playing test movies in the zip file. 

Nice extrusion.

I always thought poor Huck got a raw deal when Hanna Barbera promoted Yogi to star status. To much anthropomorphization?

bd
« Last Edit: April 13, 2014, 08:07:24 AM by bdeck »

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
Re: Yet Another 3D Viewer
« Reply #6 on: April 13, 2014, 08:25:38 AM »
Your excellent routine has identified a couple of longstanding  DC bugs.  I wasn't sure what viewing mode I was in (perspective or parallel).  Specifically, I notice the view toolbox doesn't update itself.  I was wondering if the update statement would coordinate the view toolbox, but that will cause DesignCAD to crash.


isotoggle:
   t=(t+1) mod 2
   sys(209)=t
   Update
return


So the two bugs

1. DesignCAD doesn't update the view toolbox
2. Update statement in this case causes DesignCAD 17 to 23 to crash.

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
Re: Yet Another 3D Viewer
« Reply #7 on: April 13, 2014, 08:40:47 AM »
Ok, some quick additions from my end so I know what viewing mode I'm in.


.
.
.
da=5    ' increment or decrement rotation angle when a key is pressed
df=1.25    ' distance factor
zf=1.25  ' zoom factor
sys(470) =1  ' prevent other commands from running during this macro

gosub ViewMode

.
.
.
.
print iso$, "ax=", ax, "ay=", ay, "az=", az, "d=", d, "       Hit  h key for menu,    esc to exit"
anykey k
.
.
.
isotoggle:
   t=(t+1) mod 2
   sys(209)=t
        gosub ViewMode
return

ViewMode:
   t=sys(209)
  if t = 1 then
     iso$="I "
  else
     iso$="P "
  endif     
return

bdeck

  • Hero Member
  • *****
  • Posts: 896
Re: Yet Another 3D Viewer
« Reply #8 on: April 13, 2014, 08:58:30 AM »
Hi prl,

You read my mind.  I also hide the toolbar.

OP has been modified to show perspective status in status bar. But I think I like your way better. Next edit.

This project would have been done much sooner if I hadn't kept running into brick walls with DC's  macro commands for 3d viewing. Pan and ViewChange, for example, could not be easily automated because they require the macro writer to do more math than I can justify. Since there is no GetView command, the macro writer must also do the math to find the present window position of the model before he/she can change it. I might have done that a few years ago, but now I lack the patience.

bd
« Last Edit: April 13, 2014, 09:03:54 AM by bdeck »

Rob S

  • Hero Member
  • *****
  • Posts: 4535
    • Construction Estimating Program for General Contractors
Re: Yet Another 3D Viewer
« Reply #9 on: April 13, 2014, 08:23:27 PM »
This is fantastic, already!

Why stop here, if you're not careful - some day they'll try and hire you to program the whole program from scratch!

Perfection may yet be achieved.....
User since Pro-design

minstrel

  • Hero Member
  • *****
  • Posts: 702
Re: Yet Another 3D Viewer
« Reply #10 on: April 13, 2014, 11:07:17 PM »
Thanks for releasing this to the community, Bdeck!

It really enhances our 3D viewing pleasure. 

edit - I took the liberty of 'snapping' a shot of vdd.d3m's helper menu...trust you don't mind.  It might help spark even more interest in the great macro.
« Last Edit: April 13, 2014, 11:36:15 PM by minstrel »

bdeck

  • Hero Member
  • *****
  • Posts: 896
Re: Yet Another 3D Viewer
« Reply #11 on: April 13, 2014, 11:36:41 PM »
RobS and minstrel,

Thanks for your comments. Very gratifying.

I took the liberty of 'snapping' a shot of vvd.d3m's helper menu...trust you don't mind.

minstrel,
No problem. (but I did notice a couple of typos in the menu which I now must correct....)      ;D

Regards,
bd
« Last Edit: April 14, 2014, 04:54:43 AM by bdeck »

bdeck

  • Hero Member
  • *****
  • Posts: 896
Re: Yet Another 3D Viewer
« Reply #12 on: April 14, 2014, 09:40:42 AM »
Attached is a quick update version with a few minor improvements for large drawings:

1) The subroutine "shadetoggle" now skips shade mode 1 (hiddenline mode) in favor of shade mode 4 (quick hidden line mode)

2) A new Quantum leap command "q" toggles the angle increment from 5 degrees to 30 degrees when an arrow key is pressed.

3) A "redo" variable has been added to allow suppression  of regen when it is not needed.

4) Status line now includes indicators of perspective type and shade mode.

bd
« Last Edit: April 14, 2014, 09:45:59 AM by bdeck »

samdavo

  • Hero Member
  • *****
  • Posts: 2588
  • the trick is knowin...^ > v < - which way's up?
Re: Yet Another 3D Viewer
« Reply #13 on: April 19, 2014, 06:19:06 PM »
Brilliant.
Great that you can go to one view, leave it shaded and oriented as you want.
move to the next view, and the hotkeys go there with you.

Great also that rotating about X stops at +/- 90;  so much easier to work out what's going on.

Not sure how you know whether or not the macro is running (or stopped due to "esc") (?)
   Hitting "h" soon tests whether it is active or not etc. 
thanks again.


Doh, status bar soon tells you it's active!! sorry . 

PS Can I assume it's best to start the program having triggered "Quick"render, rather than Gouraud or Phong?.
I notice for instance that, if you start the program whilst in Phong render, then cycle through the various renders using S, then stop with "esc", that:-
a) it is (or seems to be - first impression) pretty slow to render/shade (hence maybe not Flat after all?), and
b) when you open the render icon, the option presented to you is still Phong ( not Flat) -  perhaps suggesting that all the S renders were Phong on this occasion (?)

Having said that , I find I get a different "Quick Flat" when I render manually, as when I use vdd\s,  even if I start vdd with "Quick Flat" chosen. (pretty trivial I know - just that I wonder if the program is in fact choosing Quick Flat (?) )

(ahh - I can even  get shadows with vdd\s - (edit) when I enter vdd with accelerator off and with Quick Flat - hence it must change to Gerrard or Phong imo - at least my version - 2 cents) PS I'm using the latest macro from the previous post.

Final bit of pedantry - but sometimes two of the four toggled views are almost identical. (eg run vdd with accel off and quik flat set,  and cycle through using /s,  and two of them are identical, both with shadows ?)  Moral of the story might be to run vdd with accel on.

 
« Last Edit: April 20, 2014, 04:24:31 AM by samdavo »

prl

  • Hero Member
  • *****
  • Posts: 3389
  • A Bézier Extrusion
Re: Yet Another 3D Viewer
« Reply #14 on: April 20, 2014, 03:36:31 AM »
Bdeck, here is something the Revit folks did that made me wonder if they hacked into my mind.

When I first started working in 3d after spinning around a bit, when I got lost, I would/will (still rely on it all the time) hit the "home" key.   For me, that puts me in a parallel projection fit-to-window view center on model center southeast view of my model for the current active window.

That is my "home base" view.


Edit:  I also have my "7" key on the numeric key pad defined as home, since I rarely care to look if I am in arrow or numeric keypad mode.
« Last Edit: April 20, 2014, 03:45:42 AM by prl »