
		--This Script renders a polar image from the currently selected camera viewport.
		--Written by Brad Thompson 
		--Spitz, Inc.

		-- Define Globals

			Global glompath
			global Pre_resX
			global Pre_resY
			global XOutputsize
			global Polar
			global Rectangular
			global glomformat
			global prevcheckstate
			global overrenderamt
			global newfov
			global selState

		-- This section checks for an existing glompath
			if glompath == undefined do
				( 
				maxpath = GetDir #maxroot
				Global glompath = getINISetting (maxpath as string + "3dsmax.ini") "Directories" "Glom"
				)
			global XPixelaspect = 1
			
		-- Check for existing XOutputsize, pre_resX and pre_resY values and set defaults

			if Pre_resX == undefined then ( Global Pre_resX = 3600 ) else (Global Pre_resX = Pre_resX)
			if Pre_resY == undefined then ( Global Pre_resY = 3600 ) else (Global Pre_resY = Pre_resY)
			if XOutputsize == undefined then ( Global XOutputsize = 1900 ) else (Global XOutputsize = XOutputsize)
			if glomformat == undefined then ( Global glomformat = 1 ) else (Global glomformat = glomformat)
			if prevcheckstate == undefined then (Global prevcheckstate = false ) else ( Global prevcheckstate = prevcheckstate)
			if overrenderamt == undefined then (Global overrenderamt = 200 ) else (Global overrenderamt = overrenderamt)


		-- Define Glomrender Function and call it Glomrender()

			fn Glomrender = 
				(

				disableSceneRedraw()
				ActvCam = viewport.getCamera()
				copy ActvCam name: "TempFrontCam" isSelected:on 
				SourcePos = $.pos
				SourceRot = $.rotation
				frontcam = $
				$.fov = 90																			--Set the Selected Camera's FOV to 90
					
				instance $ name:"TempGlomCam_Left" isSelected:on									--Make a copy of frontcam
				in coordsys local Rotate $ 90 [0,1,0]												--Rotate it left 90 deg
						
				select frontcam
				instance $ name:"TempGlomCam_Right" isSelected:on
				in coordsys local Rotate $ -90 [0,1,0]
						
				select frontcam
				instance $ name:"TempGlomCam_Back" isSelected:on
				in coordsys local Rotate $ 180 [0,1,0]
						
				select frontcam
				instance $ name:"TempGlomCam_Up" isSelected:on
				in coordsys local Rotate $ 90 [1,0,0]
						
				fn CreateMatMaterial = 
					(															--Create the material for the matting hemi
					meditMaterials[24] = MatteShadow ()
					meditMaterials[24].opaqueAlpha = off	
					meditMaterials[24].name = "GLOMmat"
					)
				CreateMatMaterial()
				
				Sphere name: "TempGlomCameraMaskHemi" radius:2 smooth:on segs:8 chop:0 slice:off sliceFrom:0 sliceTo:0 pos: SourcePos rotation: SourceRot isSelected:on hemisphere:0.5 mapCoords:on recenter:off
				in coordsys local rotate $ (angleaxis 90 [1,0,0])
				$.material = meditMaterials[24]
				modPanel.addModToSelection (Normalmodifier ()) ui:on
				$.modifiers[#Normal].flip = on
				$.pos = SourcePos
				$.castShadows = off
					
				render camera: $TempFrontCam frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_C0000.tga") vfb:true
				render camera: $TempGlomCam_Left frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_L0000.tga") vfb:true
				render camera: $TempGlomCam_Right frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_R0000.tga") vfb:true
				render camera: $TempGlomCam_Back frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_B0000.tga") vfb:true
				render camera: $TempGlomCam_Up frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_U0000.tga") vfb:true

				delete $TempFrontCam
				delete $TempGlomCam_Left
				delete $TempGlomCam_Right
				delete $TempGlomCam_Back
				delete $TempGlomCam_Up
				delete $TempGlomCameraMaskHemi
				enableSceneRedraw()
				)
		-- Create Overrender function (same as glomrender but with different camera settings and render size)
				fn Overrndr =
				(
				newfov = ((atan(( XOutputsize + overrenderamt ) / XOutputsize))*2)
				disableSceneRedraw()
				ActvCam = viewport.getCamera()
				copy ActvCam name: "TempFrontCam" isSelected:on 
				SourcePos = $.pos
				SourceRot = $.rotation
				frontcam = $
				$.fov = newfov																			--Set the Selected Camera's FOV to 90
					
				instance $ name:"TempGlomCam_Left" isSelected:on									--Make a copy of frontcam
				in coordsys local Rotate $ 90 [0,1,0]												--Rotate it left 90 deg
						
				select frontcam
				instance $ name:"TempGlomCam_Right" isSelected:on
				in coordsys local Rotate $ -90 [0,1,0]
						
				select frontcam
				instance $ name:"TempGlomCam_Back" isSelected:on
				in coordsys local Rotate $ 180 [0,1,0]
						
				select frontcam
				instance $ name:"TempGlomCam_Up" isSelected:on
				in coordsys local Rotate $ 90 [1,0,0]
						
				fn CreateMatMaterial = 
					(															--Create the material for the matting hemi
					meditMaterials[24] = MatteShadow ()
					meditMaterials[24].opaqueAlpha = off	
					meditMaterials[24].name = "GLOMmat"
					)
				CreateMatMaterial()
				
				Sphere name: "TempGlomCameraMaskHemi" radius:2 smooth:on segs:8 chop:0 slice:off sliceFrom:0 sliceTo:0 pos: SourcePos rotation: SourceRot isSelected:on hemisphere:0.5 mapCoords:on recenter:off
				$.castShadows = off

				in coordsys local rotate $ (angleaxis 90 [1,0,0])
				$.material = meditMaterials[24]
				modPanel.addModToSelection (Normalmodifier ()) ui:on
				$.modifiers[#Normal].flip = on
				$.pos = SourcePos

						
				render camera: $TempFrontCam frame: #current outputSize: [XOutputsize+overrenderamt,XOutputsize+overrenderamt] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_C0000.tga") vfb:true
				render camera: $TempGlomCam_Left frame: #current outputSize: [XOutputsize+overrenderamt,XOutputsize+overrenderamt] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_L0000.tga") vfb:true
				render camera: $TempGlomCam_Right frame: #current outputSize: [XOutputsize+overrenderamt,XOutputsize+overrenderamt] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_R0000.tga") vfb:true
				render camera: $TempGlomCam_Back frame: #current outputSize: [XOutputsize+overrenderamt,XOutputsize+overrenderamt] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_B0000.tga") vfb:true
				render camera: $TempGlomCam_Up frame: #current outputSize: [XOutputsize+overrenderamt,XOutputsize+overrenderamt] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_U0000.tga") vfb:true

				delete $TempFrontCam
				delete $TempGlomCam_Left
				delete $TempGlomCam_Right
				delete $TempGlomCam_Back
				delete $TempGlomCam_Up
				delete $TempGlomCameraMaskHemi
				enableSceneRedraw()
				)
				
		-- Create Panrender function
		
				fn panrender = 
				(
				disableSceneRedraw()
				ActvCam = viewport.getCamera()
				copy ActvCam name: "TempFrontCam" isSelected:on 
				SourcePos = $.pos
				SourceRot = $.rotation
				frontcam = $
				$.fov = 90																			--Set the Selected Camera's FOV to 90
					
				instance $ name:"TempGlomCam_Left" isSelected:on									--Make a copy of frontcam
				in coordsys local Rotate $ 90 [0,1,0]												--Rotate it left 90 deg
						
				select frontcam
				instance $ name:"TempGlomCam_Right" isSelected:on
				in coordsys local Rotate $ -90 [0,1,0]
						
				select frontcam
				instance $ name:"TempGlomCam_Back" isSelected:on
				in coordsys local Rotate $ 180 [0,1,0]
						
				select frontcam
				instance $ name:"TempGlomCam_Up" isSelected:on
				in coordsys local Rotate $ 90 [1,0,0]
						
				fn CreateMatMaterial = 
					(															--Create the material for the matting hemi
					meditMaterials[24] = MatteShadow ()
					meditMaterials[24].opaqueAlpha = off	
					meditMaterials[24].name = "GLOMmat"
					)
				CreateMatMaterial()
				
				Sphere name: "TempGlomCameraMaskHemi" radius:2 smooth:on segs:8 chop:0 slice:off sliceFrom:0 sliceTo:0 pos: SourcePos rotation: SourceRot isSelected:on hemisphere:0.5 mapCoords:on recenter:off
				in coordsys local rotate $ (angleaxis 90 [1,0,0])
				$.material = meditMaterials[24]
				modPanel.addModToSelection (Normalmodifier ()) ui:on
				$.modifiers[#Normal].flip = on
				$.pos = SourcePos
				$.castShadows = off
						
				render camera: $TempFrontCam frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_C0000.tga") vfb:true
				render camera: $TempGlomCam_Left frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_L0000.tga") vfb:true
				render camera: $TempGlomCam_Right frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_R0000.tga") vfb:true
	--			render camera: $TempGlomCam_Back frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_B0000.tga") vfb:true
				render camera: $TempGlomCam_Up frame: #current outputSize: [XOutputsize,XOutputsize] pixelaspect: XPixelaspect renderfields: false renderType:#normal outputfile:(glompath+"glomin_U0000.tga") vfb:true

				delete $TempFrontCam
				delete $TempGlomCam_Left
				delete $TempGlomCam_Right
				delete $TempGlomCam_Back
				delete $TempGlomCam_Up
				delete $TempGlomCameraMaskHemi
				enableSceneRedraw()
				)

		-- Create the options panel rollout
		
		rollout OptionsRollout "Render Polar Options" width:344 height:209
		(
			spinner hemicubesize "Hemicube Side Size " pos:[24,7] width:216 height:16 range:[0,8000,XOutputsize] scale:1
			spinner Xout "Final Output Width  " pos:[24,24] width:216 height:16 range:[0,8000,Pre_resX] scale:1
			spinner Yout "Final Output Height " pos:[24,40] width:216 height:16 range:[0,8000,Pre_resY] scale:1
			button btn2 "Render" pos:[264,136] width:71 height:26
			edittext Glompathdisplay "Glom Path" text: glompath pos:[16,128] width:224 height:16
			dropdownList formatsel "Format" pos:[16,56] width:224 height:40 items:#("Polar", "Rectangular") selection:glomformat
			bitmap bmp1 "Bitmap" pos:[248,8] width:88 height:62 fileName:"Spitzlogo_small.bmp"
			checkbox panprev "Show Immersa-200 Safe Area" pos:[16,104] width:168 height:16 checked:false
			checkbox overrender "Checkbox" pos:[16,163] width:16 height:16
			label lbl1 "Overrender by" pos:[35,164] width:74 height:21
			spinner ORpxls "" pos:[110,163] width:53 height:16 range:[0,1000,overrenderamt]
			label lbl2 "pixels - even num only" pos:[168,165] width:120 height:17

			
		-- Button actions
		
			on panprev changed theState do (
				if theState then overrender.checked = false
				)
			on overrender changed theState do (
				if theState then panprev.checked = false
				if theState then formatsel.selection = 1
				)
			on btn2 pressed do 
			(
			XOutputsize = hemicubesize.value
			pre_resX = Xout.value
			pre_resY = Yout.value
			overrenderamt = ((ceil(ORpxls.value/2))*2)
			DestroyDialog OptionsRollout
			if glomformat == 1 do (
				if overrender.checked then (
					try Overrndr()
					catch (
					messagebox "Please Select a Camera Viewport"
					enableSceneRedraw()
					 )
				)
				else (
					try Glomrender()
					catch (
					messagebox "Please Select a Camera Viewport"
					enableSceneRedraw()
					 )
				)

				if overrender.checked then (
					glomcmd = glompath + "glom.exe -5viewPolar -ewa -res "+ pre_resX as string + " " + pre_resY as string + " -crop "+ overrenderamt as string + "  " + glompath + "glomin_L0000.tga " + glompath + "glomin_C0000.tga " + glompath + "glomin_R0000.tga " + glompath + "glomin_U0000.tga " +glompath + "glomin_B0000.tga "+ glompath + "glomout.tga"
					)
					else (
						glomcmd = glompath + "glom.exe -5viewPolar -ewa -res "+ pre_resX as string + " " + pre_resY as string + "  " + glompath + "glomin_L0000.tga " + glompath + "glomin_C0000.tga " + glompath + "glomin_R0000.tga " + glompath + "glomin_U0000.tga " +glompath + "glomin_B0000.tga "+ glompath + "glomout.tga"
					)
				DOSCommand (glomcmd)
				if panprev.checked then
					( 
					prevcheckstate = true
					glomcmd = glompath + "glom.exe -4x -azi -100 100 -alt 0 60 -res 1800 540 " + glompath + "glomin_L0000.tga " + glompath + "glomin_C0000.tga " + glompath + "glomin_R0000.tga " + glompath + "glomin_U0000.tga " + glompath + "GlomSafeprev.tga"
					DOSCommand (glomcmd)
					previewsafe = openbitmap (glompath+"GlomSafeprev.tga")
					display previewsafe
					)
				preview = openbitmap (glompath+"glomout.tga")
				display preview
				)
			if glomformat == 2 do (
				try panrender()
				catch (
					messagebox "Please Select a Camera Viewport"
					enableSceneRedraw()
				 )
				glomcmd = glompath + "glom.exe -4x -azi -100 100 -alt 0 60 -res "+ pre_resX as string + " " + pre_resY as string + " " + glompath + "glomin_L0000.tga " + glompath + "glomin_C0000.tga " + glompath + "glomin_R0000.tga " + glompath + "glomin_U0000.tga " + glompath + "glomout.tga"
				DOSCommand (glomcmd)
				preview = openbitmap (glompath+"glomout.tga")
				display preview
				)
			)
			on Glompathdisplay entered text do
			(
			filetest = doesFileExist (Glompathdisplay.text as string + "glom.exe")
			if filetest as string == "true" then
					(
					Global glompath = Glompathdisplay.text
					setINISetting (maxpath as string + "3dsmax.ini") "Directories" "Glom" (glompath as string)
					messagebox ("glom path changed to " + glompath as string )
					)
				else
					(
					messagebox (filetest as string + " Glom.exe Does Not Exist At " + Glompathdisplay.text + "glompath string currently set to " + glompath as string )
					)
				
			)
			on formatsel selected i do
				( glomformat = formatsel.selection )
		)		
	 vptest = viewport.getCamera()
	 if vptest != undefined then ( CreateDialog OptionsRollout ) else ( messagebox "Please Select a Camera Viewport" )

		










		----------------------------------------------------------------------------
					
		--glom -5viewPolar [options] Left-file Center-file Right-file Up-file
		--Back-file [output-file]
					
		--where valid options are:
					
		--   -res #w #h
		--   -domeMaster
		--   -4x

