home *** CD-ROM | disk | FTP | other *** search
- REM @(#)CADKEY SOLIDS light.cdl 2.1 10/18/88
- REM
- REM " Program to recalculate lighting for Solids Synthesis display"
- REM " Color Table values start at 16 to 255 "
- pi = 180.0
- lvx = 0.5733
- lvy = 0.5733
- lvz = 0.5733
- array polydat[120]
- array colors[3][1]
- array nan[16]
- array color[5]
- array nv[5]
- array used[16]
- max_class = 16
- npolys = 5
- REM " Default color ranges -- use sizeof when available "
- if (sizeof(colran) != 0 )
- goto nod
- array colran[16][6] = { \
- -1, 2, 15, 1.0, 1.0,1.0,\
- 1, 16, 55, 1.0, 0.0,0.0,\
- 2, 56, 95, 0.0, 1.0,0.0,\
- 3, 96, 135 , 0.0, 0.0,1.0,\
- 4, 136, 175, 0.0, 1.0,1.0,\
- 5, 176, 215, 1.0, 0.0,1.0,\
- 6, 216, 255, 1.0, 1.0,0.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0,\
- -1, 1, 15, 1.0, 1.0,1.0 }
- :nod
- REM calculate color table mapping values
- thetastart = -0.001
- thetaend = 2.0*pi + 0.001
- REM thetainc = ( thetaend - thetastart ) / ( ntheta - 1 )
- phistart = 5.0
- phiend = pi /2.0
- done = 0
- zmxx = cos(phistart)
- REM phiinc = ( phiend - phistart ) / ( nphi - 1 )
- REM "Calculate the color values based on range of the color class "
- lval = 0
- :classloop
- used[lval] = 0
- if ( colran[lval][1] == -1 || colran[lval][1] == -2 )
- goto not_def
- ncl = colran[lval][2] - colran[lval][1]
- REM "Find the largest perfect square that matches "
- n = 0
- :fsq
- n = n + 1
- if ( n^2 < ncl )
- goto fsq
- nan[lval] = n - 1
- :not_def
- lval = lval + 1
- if ( lval < max_class )
- goto classloop
- REM Select polygon entities
- :select
- np = 0
- class = 1
- coef = 0.7
- ambient = 1.0 - coef
- REM set mask, 6
- getentm 6, nument
- if (@key == -3)
- goto leave_it
- if ( nument != 0 )
- goto okents
- prompt "No data for light program, normal mode required."
- wait 3
- goto leave_it
- :okents
- if (@error)
- goto select
- if (@key == -2)
- goto ls
- if (nument <= 0)
- goto select
- REM Get the data for entities in the selection list
-
- :loop
- ipol = 0
- ist = 0
- :loop1
- getnext etype
- if (@error != 0 )
- done = 1
- if (done != 0)
- goto enddat
- if (etype != 6)
- goto loop1
- np = np+1
- nv[ipol] = @intdat[10]
- ocolor = @intdat[9]
- i = 0
- :ploop
- ip = i*3
- polydat[ip+ist] = @fltdat[ip]
- polydat[ip+1+ist] = @fltdat[ip+1]
- polydat[ip+2+ist] = @fltdat[ip+2]
- i = i + 1
- if (i < nv[ipol])
- goto ploop
-
- REM Calculate normal for polygon
-
- ux = polydat[3+ist] - polydat[0+ist]
- uy = polydat[4+ist] - polydat[1+ist]
- uz = polydat[5+ist] - polydat[2+ist]
- vx = polydat[6+ist] - polydat[3+ist]
- vy = polydat[7+ist] - polydat[4+ist]
- vz = polydat[8+ist] - polydat[5+ist]
- ist = ist + 3 * nv[ipol]
- nx = uy*vz-vy*uz
- ny = uz*vx-vz*ux
- nz = ux*vy-vx*uy
- s = sqrt ((nx*nx)+(ny*ny)+(nz*nz))
- nx = nx / s
- ny = ny / s
- nz = nz / s
- if ( nz >= 0.0 )
- goto okz
- nx = -nx
- ny = -ny
- nz = -nz
- :okz
- if ( nz >= zmxx)
- goto lastentry
- thnorm = atan2(ny,nx)
- if ( thnorm < 0.0 )
- thnorm = 2*pi + thnorm
- phnorm = acos(nz)
- delent
- i = 0
- ith = -1
- th = thetastart
- iclp = 1
- :clloop
- if ( (ocolor >= colran[iclp][1]) && (ocolor <= colran[iclp][2]))
- goto lloop
- iclp = iclp + 1
- if ( iclp == 16 )
- goto lloop
- goto clloop
- :lloop
- ocolor = iclp
- if ( iclp == 16 )
- ocolor = 0
- if (( colran[ocolor][1] == -1 ) || (colran[ocolor][1] == -2 ) )
- ocolor = 0
- used[ocolor] = 1
- ntheta = nan[ocolor]
- nphi = nan[ocolor]
- thetainc = ( thetaend - thetastart ) / ( nan[ocolor] - 1 )
- :normloop1
- if (( thnorm > th ) && ( thnorm <= th + thetainc))
- ith = i
- if (ith > -1 )
- goto donetheta
- i = i + 1
- th = th + thetainc
- if ( i < ntheta )
- goto normloop1
- :donetheta
- i = 0
- iph = -1
- ph = phistart
- phiinc = ( phiend - phistart ) / ( nan[ocolor] - 1 )
- :normloop2
- if (( phnorm > ph ) && ( phnorm <= ph + phiinc ))
- iph = i
- if (iph > -1 )
- goto found
- i = i + 1
- ph = ph + phiinc
- if ( i < nphi )
- goto normloop2
- :lastentry
- color[ipol] = ( nphi * ntheta) + colran[ocolor][1]
- goto calccol
- :found
- color[ipol] = ( ith * nphi ) + iph + colran[ocolor][1]
- :calccol
- if ( color[ipol] > colran[ocolor][2] )
- color[ipol] = colran[ocolor][2]
- ipol = ipol + 1
- if ( ipol < npolys )
- goto loop1
- :enddat
- if ( ipol < 0)
- goto first_time
- i = 0
- ist = 0
- :loop2
- if ( i == 0 )
- goto f1
- ist = ist + 3 * nv[i-1]
- it = 0
- :loop3
- polydat[it] = polydat[ist+it]
- ip = it*3
- polydat[ip] = polydat[ip + ist ]
- polydat[ip+1] = polydat[ip+1 + ist ]
- polydat[ip+2] = polydat[ip+2 + ist ]
- it = it + 1
- if ( it < nv[i] )
- goto loop3
- :f1
- REM polygon 2, color[i], nv[i], polydat[ist]
- polygon 2, color[i], nv[i], polydat
- i = i + 1
- if ( i < ipol )
- goto loop2
- REM "Go get the next entity "
- if ( done == 1 )
- goto first_time
- goto loop
- :end
- REM
- REM Get light source position
- REM
- :ls
- getmenu "Choose method of entering light coordinates",\
- "WORLD",\
- "VIEW"
- on (@key + 3) goto leave_it,leave_it,ls,,\
- dolight,\
- dovlight
- goto ls
- :dolight
- tmp = lvx
- getflt "Enter light source direction X (%f) =>",tmp,lvx
- on (@key + 3 ) goto leave_it,ls,dolight,
-
- :dolt2
- tmp = lvy
- getflt "Enter light source direction Y (%f) =>",tmp,lvy
- on (@key + 3 ) goto leave_it,dolight,dolt2,
-
- :dolt3
- tmp = lvz
- getflt "Enter light source direction Z (%f) =>",tmp,lvz
- on (@key + 3 ) goto leave_it,dolt2,dolt3,
- goto normal
-
- :dovlight
- tmp = lvx
- getflt "Enter view light source direction XV (%f) =>",tmp,lvx
- on (@key + 3 ) goto leave_it,ls,dovlight,
-
- :dovlt2
- tmp = lvy
- getflt "Enter view light source direction YV (%f) =>",tmp,lvy
- on (@key + 3 ) goto leave_it,dovlight,dovlt2,
-
- :dovlt3
- tmp = lvz
- getflt "Enter view light source direction ZV (%f) =>",tmp,lvz
- on (@key + 3 ) goto leave_it,dovlt2,dovlt3,
- getview @view
- C1 = @fltdat[0] * lvx + @fltdat[1] * lvy + @fltdat[2] * lvz
- C2 = @fltdat[3] * lvx + @fltdat[4] * lvy + @fltdat[5] * lvz
- C3 = @fltdat[6] * lvx + @fltdat[7] * lvy + @fltdat[8] * lvz
- lvx = C1
- lvy = C2
- lvz = C3
- REM Calculate new color for polygon
- :normal
- s = sqrt ((lvx*lvx)+(lvy*lvy)+(lvz*lvz))
- lvx = lvx / s
- lvy = lvy / s
- lvz = lvz / s
- :first_time
- REM Calculate new color table light source values
- lval = 0
- :recalc
- if ( colran[lval][0] == -1 || colran[lval][0] == -2 )
- goto notdefed
- if ( used[lval] == 0 )
- goto notdefed
- ith = 0
- icol = colran[lval][1]
- red = colran[lval][3]
- green = colran[lval][4]
- blue = colran[lval][5]
- colors[0][0] = 0
- colors[1][0] = 0
- colors[2][0] = 0
- thetainc = ( thetaend - thetastart ) / ( nan[lval] - 1 )
- phiinc = ( phiend - phistart ) / ( nan[lval] - 1 )
- ntheta = nan[lval]
- nphi = nan[lval]
- th = thetastart + 0.5 * thetainc
- :thloop
- sth = sin(th)
- cth = cos(th)
- ph = phistart + 0.5 * phiinc
- iph = 0
- :phloop
- sph = sin(ph)
- cph = cos(ph)
- xnorm = sph * cth
- ynorm = sph * sth
- znorm = cph
- diff = coef*((xnorm*lvx)+(ynorm*lvy)+(znorm*lvz))
- if ( diff < 0.0 )
- diff = 0.0
- colors[0][0] = ( diff + ambient ) * red
- colors[1][0] = ( diff + ambient ) * green
- colors[2][0] = ( diff + ambient ) * blue
- palette icol,1,colors
- icol = icol + 1
- ph = ph + phiinc
- iph = iph + 1
- if ( iph < nphi )
- goto phloop
- ith = ith + 1
- th = th + thetainc
- if ( ith < ntheta )
- goto thloop
- xnorm = 0.0
- ynorm = 0.0
- znorm = 1.0
- diff = coef*((xnorm*lvx)+(ynorm*lvy)+(znorm*lvz))
- if ( diff < 0.0 )
- diff = 0.0
- colors[0][0] = ( diff + ambient ) * red
- colors[1][0] = ( diff + ambient ) * green
- colors[2][0] = ( diff + ambient ) * blue
- palette icol,1,colors
- :notdefed
- lval = lval + 1
- if ( lval < max_class )
- goto recalc
- if ( @numpal < 17 )
- REDRAW
- REM Try another light source
- goto ls
- :leave_it
- clear xnorm,ynorm,znorm,diff,th,ith,thetainc,lval,max_class,colors
- clear red,green,blue,ambient,sph,cph,cth,sth,ntheta,nphi,phiinc
- clear tmp,lvx,lvy,lvz,ist,i,it,ocolor,iclp,maxclass,npolys
- clear polydat,colors, nan, color, nv, used,thetastart,thetaend,phistart
- clear phiend,zmxx,np,class,coef,iph,ipol
- clear ux,uy,uz,vx,vy,vz,nx,ny,nz
-