<?xml version="1.0" encoding="utf-8"?>

<!--    SmartReLight Shadertest - http://www.smart-page.net/blog/2007/05/16/smartrelight-relighting-with-pixel-bender    -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" color="#ffffff" viewSourceURL="http://www.smart-page.net/as3/sp_smartrelight/srcview/index.html">
    
     <mx:Script>
        <![CDATA[

            [Embed("assets/shaders/SmartReLight.pbj", mimeType="application/octet-stream")]
            private var SmartReLight:Class;
                        
            private var shader:Shader;
                        
            private function dofilter():void {
                
                shader = new Shader(new SmartReLight() as ByteArray);
                
                var inputrgb:BitmapData = new BitmapData( rgbmap.width, rgbmap.height, true );
                inputrgb.draw( rgbmap );
                var inputnmap:BitmapData = new BitmapData( nmap.width, nmap.height, true );
                inputnmap.draw( nmap );

                shader.data.a_src.input = inputrgb;
                shader.data.b_normals.input = inputnmap;
                
                update();
            }    
            
            private function update():void {
                
                shader.data.A_brightness.value[0]         = A_brightness_slider.value;
                
                shader.data.B_lightpos.value[0]         = B_lightx_slider.value;
                shader.data.B_lightpos.value[1]            = B_lighty_slider.value;
                shader.data.B_lightpos.value[2]            = B_lightz_slider.value;
                
                shader.data.C_lightcolor.value[0]         = C_lightcolr_slider.value;
                shader.data.C_lightcolor.value[1]        = C_lightcolg_slider.value;
                shader.data.C_lightcolor.value[2]        = C_lightcolb_slider.value;
                
                shader_checkbox.selected ? rgbmap.filters = [new ShaderFilter(shader)] : rgbmap.filters =[];    
                
                light.x=rgbmap.x + B_lightx_slider.value - light.width / 2;
                light.y=rgbmap.y + B_lighty_slider.value - light.height / 2;
                
                if(light.x < rgbmap.x){
                    light.x = rgbmap.x; 
                }else if(light.x > rgbmap.x + rgbmap.width - light.width){
                    light.x = rgbmap.x + rgbmap.width - light.width;
                }
                if(light.y < rgbmap.y){
                    light.y = rgbmap.y; 
                }else if(light.y > rgbmap.y + rgbmap.height - light.height){
                    light.y = rgbmap.y + rgbmap.height - light.height;
                }
                            
                nmap.visible=normals_checkbox.selected;
            }                    
        ]]>
    </mx:Script>
    
    <mx:Image 
        id="rgbmap" 
        source="@Embed(source='assets/images/rgb.jpg')" 
    />
    <mx:Image  
        id="nmap" 
        source="@Embed(source='assets/images/normal.jpg')" 
    />
    <mx:Image  
        id="light" 
        source="@Embed(source='assets/icons/light.png')"
        creationComplete="dofilter()" 
    />
    
    <mx:Label x="10" y="10" text="A_brightness:"/>
    <mx:HSlider id="A_brightness_slider" x="100" y="5" value="2.0" minimum="-100.0" maximum="100.0" snapInterval="1" liveDragging="true" change="update()"/>
    
    <mx:Label x="10" y="55" text="B_lightposX:"/>
    <mx:HSlider id="B_lightx_slider" x="100" y="50" value="490.0" minimum="-200.0" maximum="2048.0" snapInterval="1" liveDragging="true" change="update()"/>
    <mx:Label x="10" y="70" text="B_lightposY:"/>
    <mx:HSlider id="B_lighty_slider" x="100" y="65" value="90.0" minimum="-200.0" maximum="2048.0" snapInterval="1" liveDragging="true" change="update()"/>
    <mx:Label x="10" y="85" text="B_lightposZ:"/>
    <mx:HSlider id="B_lightz_slider" x="100" y="80" value="50.0" minimum="0.0" maximum="2048.0" snapInterval="1" liveDragging="true" change="update()"/>
    
    <mx:Label x="10" y="110" text="C_lightcolorR:"/>
    <mx:HSlider id="C_lightcolr_slider" x="100" y="105" value="0.95" minimum="0.0" maximum="1.0" snapInterval="0.01" liveDragging="true" change="update()"/>
    <mx:Label x="10" y="125" text="C_lightcolorG:"/>
    <mx:HSlider id="C_lightcolg_slider" x="100" y="120" value="1.0" minimum="0.0" maximum="1.0" snapInterval="0.01" liveDragging="true" change="update()"/>
    <mx:Label x="10" y="140" text="C_lightcolorB:"/>
    <mx:HSlider id="C_lightcolb_slider" x="100" y="135" value="0.95" minimum="0.0" maximum="1.0" snapInterval="0.01" liveDragging="true" change="update()"/>
    
    <mx:CheckBox id="shader_checkbox" x="10" y="175" width="150" label="show relighting?" selected="true" change="update()"/> 
    <mx:CheckBox id="normals_checkbox" x="10" y="195" width="150" label="show normals?" selected="false" change="update()"/> 
    
</mx:Application>