SetRenderToImage( 2, -1 )
On Mac it looks like depth buffer is way to small. objects gets cut in half, do strange stuff and a lot of flickering.
On Android (samsung s2) it looks like the screen (keep) changing resolution, but other then that it looks like the objects looks correct.
Works perfectly on Ipad/Iphone and Windows. ( did not test linux ).
Code: just load the sample project “shaders” “bloom” and replace the main code with this ( press key 1 to see it use sync, and it works )
// Full screen bloom shader demo
setvirtualresolution(1024,768)
SetClearColor( 101,120,154 )
SetScissor(0,0,0,0)
SetGenerateMipmaps(1)
SetPrintSize( 20 )
setcamerarange(1,2,15000)
// load a 3D object
LoadImage( 1, "Models/chapel_D.png" )
LoadObject( 1, "Models/chapel.obj", 10 )
SetObjectImage( 1, 1, 0 )
// create some light
CreatePointLight( 1, 0,0,-6, 100, 0,255,0 )
// create a light marker object
CreateObjectSphere( 2, 1, 10, 10 )
SetObjectColor( 2, 0,255,0,128 )
// point camera towards the object
SetCameraPosition( 1, 15,10,0 )
SetCameraLookAt( 1, 0,5,0,0 )
// create some intermediate images for the full screen shader
CreateRenderImage( 2, 1024, 1024, 0, 0 ) // initial render
CreateRenderImage( 3, 512, 512, 0, 0 ) // reduction 1
CreateRenderImage( 4, 256, 256, 0, 0 ) // reduction 2
CreateRenderImage( 5, 128, 128, 0, 0 ) // reduction 3
CreateRenderImage( 6, 128, 128, 0, 0 ) // blur 1
// we then blur a second time with image 5 as the output, as it is the right size and we don't need it anymore
// image 5 (blurry) is then blended with image 2 (sharp) to produce the final output shown on the screen
// create some colored planes to show some light bleeding
CreateObjectPlane( 6, 5,5 )
SetObjectLightMode( 6, 0 )
SetObjectPosition( 6, 0,7.5,7 )
RotateObjectGlobalY( 6, 90 )
SetObjectColor( 6, 255,0,0,255 )
CreateObjectPlane( 7, 5,5 )
SetObjectLightMode( 7, 0 )
SetObjectPosition( 7, 0,1.5,7 )
RotateObjectGlobalY( 7, 90 )
CreateObjectPlane( 17, 75,75 )
SetObjectLightMode( 17, 0 )
SetObjectPosition( 17, 0,0,0 )
SetObjectTransparency(17,1)
SetObjectColor(17,100,100,200,128)
RotateObjectGlobalX( 17, 90 )
CreateObjectPlane( 18, 175,175 )
SetObjectLightMode( 18, 0 )
SetObjectPosition( 18, 0,40,0 )
SetObjectTransparency(18,1)
SetObjectColor(18,100,200,100,128)
RotateObjectGlobalX( 18, 90 )
CreateObjectPlane( 19, 275,275 )
SetObjectLightMode( 19, 0 )
SetObjectPosition( 19, 0,70,0 )
SetObjectTransparency(19,1)
SetObjectColor(19,200,200,100,128)
RotateObjectGlobalX( 19, 90 )
CreateObjectPlane( 20, 375,375 )
SetObjectLightMode( 20, 0 )
SetObjectPosition( 20, 0,-100,0 )
SetObjectColor(20,200,200,100,128)
RotateObjectGlobalX( 20, 90 )
SetObjectTransparency(20,1)
CreateObjectFromHeightMap(22,"Models/chapel_D.png",2000,100,2000,2,16)
SetObjectImage( 22, 1, 0 )
SetObjectPosition(22,-1000,-50,-1000)
// load the shaders for each stage, Quad.vs does a simply vertex transformation to make the 3D plane perfectly fit the entire screen
LoadShader( 1, "Quad.vs", "Initial.ps" )
LoadShader( 2, "Quad.vs", "Reduction.ps" ) // same shader for all three reduction steps
LoadShader( 3, "Quad.vs", "Blur1.ps" )
LoadShader( 4, "Quad.vs", "Blur2.ps" )
LoadShader( 5, "Quad.vs", "Bloom.ps" ) // combines image 5 and 2 into the final output
// make a plane to display images to the screen so they can be modified by shaders.
// planes used for this purpose are generally called quads, AGK has a special Quad primitive
// that means it can take some shortcuts in the rendering pipeline, Quads will never be
// rendered to the screen unless you specifically draw it with DrawObject()
CreateObjectQuad( 8 )
// controls setup
addvirtualbutton( 1, 970,715,100 )
addvirtualbutton( 2, 970,610,100 )
startx# = GetPointerX()
starty# = GetPointerY()
time# = 0
SetCameraPosition(1,2300,4741,-266)
SetCameraLookAt(1,0,0,0,0)
do
// control the camera
if ( GetRawKeyState( 87 ) ) then MoveCameraLocalZ( 1, 10.2 )
if ( GetRawKeyState( 83 ) ) then MoveCameraLocalZ( 1, -10.2 )
if ( GetRawKeyState( 65 ) ) then MoveCameraLocalX( 1, -10.2 )
if ( GetRawKeyState( 68 ) ) then MoveCameraLocalX( 1, 10.2 )
if ( GetRawKeyState( 81 ) ) then MoveCameraLocalY( 1, -10.2 )
if ( GetRawKeyState( 69 ) ) then MoveCameraLocalY( 1, 10.2 )
if ( getvirtualbuttonstate(1) = 1 ) then MoveCameraLocalZ( 1, -10.2 )
if ( getvirtualbuttonstate(2) = 1 ) then MoveCameraLocalZ( 1, 10.2 )
if ( GetPointerPressed() )
startx# = GetPointerX()
starty# = GetPointerY()
angx# = GetCameraAngleX(1)
angy# = GetCameraAngleY(1)
pressed = 1
endif
if ( GetPointerState() = 1 )
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endif
// move the green light
time# = time# + GetFrameTime()
x# = Sin( time#*70 ) * 7
z# = Cos( time#*70 ) * 7
SetPointLightPosition( 1, x#, 2, z# )
SetObjectPosition( 2, x#, 2, z# )
print( "Click and drag to rotate the camera, W, A, S, D, Q, E to move" )
print( screenfps() )
Print("x: " + str(GetCameraX(1))+" y: " + str(GetCameraY(1)) + " z: " + str(GetCameraZ(1)) )
//Print("Depth support: " + str( IsSupportedDepthTexture() ) )
If GetRawKeyState(49) = 1
Print("# Using Sync()")
Sync()
else
// update the scene, this should only be called once
Update(0)
// full screen bloom process
// first Render() as normal to image 2
SetRenderToImage( 2, -1 ) // render into image 2, using an internal depth buffer
ClearScreen() // make sure the image is clean
Render() // draw the scene as normal
// do some post processing on our newly rendered image 2
SetObjectImage( 8, 2, 0 ) // apply image 2 to the quad
SetObjectShader( 8, 1 ) // set our first shader to the quad, this does some processing on the image to make it suitable for bloom
SetRenderToImage( 3, 0 ) // we will output to image 3 which is half the size, so this is also a reduction whilst shader 1 does some processing
ClearScreen() // clean image 3
DrawObject( 8 ) // draw the quad and nothing else, this makes it nice and fast
// take our newly rendered image 3 and reduce it further
SetObjectImage( 8, 3, 0 ) // apply image 3 to the quad, this removes image 2 which will remain unchanged from now on
SetObjectShader( 8, 2 ) // set the reduction shader which does nothing but reduce the image size
SetRenderToImage( 4, 0 ) // output to image 4 which is half the size of image 3
ClearScreen() // clean image 4
DrawObject( 8 ) // draw the quad and nothing else
// take our newly rendered image 4 and reduce it further
SetObjectImage( 8, 4, 0 ) // apply image 4 to the quad
SetObjectShader( 8, 2 ) // added for clarity, the quad is already using shader 2
SetRenderToImage( 5, 0 ) // output to image 5 which is half the size of image 4
ClearScreen() // clean image 5
DrawObject( 8 ) // draw the quad and nothing else
// we now have a low quality 128x128 copy of our scene in image 5, lets blur this so it creates a nice bloom effect
SetObjectImage( 8, 5, 0 ) // apply image 5 to the quad
SetObjectShader( 8, 3 ) // set the first blur shader, this does a horizontal blur across the pixels
SetRenderToImage( 6, 0 ) // output to image 6, which is the same size as image 5
// SetRenderToImage( 5, 0 ) // output to image 6, which is the same size as image 5
ClearScreen() // clean image 6
DrawObject( 8 ) // draw the quad and nothing else
SetObjectImage( 8, 6, 0 ) // apply image 6 to the quad
SetObjectShader( 8, 4 ) // set the second blur shader, this does a vertical blur across the pixels to finish the blur effect
SetRenderToImage( 5, 0 ) // output to image 5 as it is no longer being used and is the right size for this step
ClearScreen() // clean image 5
DrawObject( 8 ) // draw the quad and nothing else
// we now have a very blurry image 5 and a nice sharp image 2 from the very beginning (plus some intermediate
// images 3,4,6 that we don't need anymore), we can combine image 5 and 2 to create a nice bloom effect
SetObjectImage( 8, 2, 0 ) // set image 2 to texture stage 0 on the quad
SetObjectImage( 8, 5, 1 ) // set image 5 to texture stage 1 on the quad
SetObjectShader( 8, 5 ) // apply the bloom shader to the quad, this will do the work of blending image 5 and image 2
SetRenderToScreen() // send this output to the screen, this is the only thing the user will see from the entire process
setvirtualresolution(1024,768)
DrawObject( 8 ) // draw the qaud and nothing else, remember image 2 already contains our original scene so we don't need to draw that again
// don't use Sync() here as it will redraw the entire scene again, we have already drawn the scene and post processed it
Swap()
endif
loop
Screenshots , using Sync and using SetRenderToImage:
best regards Preben Eriksen,