a small addition
this is for all who wants to create huge 3d worlds with tons of objects.
here there are approx 5000 objects and one render Image
yeah , it needs more work but its a good start.
something that still I find troubles me is that render to image dosent care about 3d objects far vs near it will draw objects according to the array rather then their depth. so press Enter to see what I mean.
sorry about my English , the AppGameKit ide dosent correct me
// RENDERING ALOT OF 3D OBJECTS WHILE MAINTAINING HIGH FPS
// Tut by haliop.
// This tutorial will demonsrate that with or without render to image the FPS remains about the same.
// while on 60 fps or more... and off 60 fps or more, one will show all the objects in scene and the other just what is near.
// One thing to remmeber , since Render to Image dose not care about the Distance of objects from the screen
// it will draw one on top of the other.
// so there is a need of Sorting the objects array each time.
// when running this App you will notice that if render to image is On and you are at the start (camera position) of the object creation position it will be messed up
// but if you go to back of all objects you will see it as it should be.
// this happens since the objects array are created linear from the start position so it first draws the objects that was first created rather then the fartest to the nearest.
// as it draws on one texture.
// set window properties
SetWindowTitle( "objectREnderToImage" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetPrintSize(20)
// First create a render Image
a = CreateRenderImage(2048,2048,0,0)
//b = CreateObjectPlane(204.8,204.8) // using the same width and height as the render image just / 10 as long as the ratio is the same it will be ok. you can always scale it later on in real time
c = CreateObjectQuad() // tought to my self this might be faster then a normal object... mostly it is..
type Objects
id as integer
render1 as integer
endtype
global dim objs[1000,10] as objects
// creating objects
z# = 0
x = 1
for i = 1 to 1000
r = random(1,10)
for j =1 to r
objs[i,j].id = CreateObjectBox(300,300,300)
SetObjectcolor(objs[i,j].id,random(25,255),random(25,255),random(25,255),255)
SetObjectPosition(objs[i,j].id,(-5 * 300)+ (600 * x),j * 300,z#)
SetObjectCullMode(objs[i,j].id,1)
next j
inc x
if x > 10
z# = z# + 300.0
x = 0
endif
next i
SetRawMousePosition(1024/2,768/2)
SetCameraPosition(1,0,5000,-3000)
SetcameraRange(1,1,100000)
SetCameraLookAt(1,0,5000,0,0)
ViewDistance# = 12000
StartPosition = 1 // 1 = front , -1 = back // this will show the diffrence about Render Depth issue with Sorting the Array correctly.
renderCycle = 6 // the times the loop will cycle while not using render to target altough it is on. lower means more expensive FPS but less bumpy.
// with a good motion blur shader this could potentialy be very high. since the shader will eliminate the bump.
renderNow = RenderCycle // if this hits 1 it will render all the far objects.
checkingCycle = 12 // The checking cycle acts just like the render cycle, and this is explained easily.
// since we are not flying in the speed of light or even at Mach 5 jet Speed , we dont really need to check everything each time...
// we are moving slow... or slow enough not to even care about most objects..
// so if we just checked an object we dont need to check it again immidiatly we can wait a few seconds.. cause we move kinda slow.
checkingNow = CheckingCycle // when this hits 1 it will check for Visible objects
SetSyncRate(0,0) // setting sync rate to above 60 , actually setting it to 0 will be the highest your device is able to get.
startFix = 1
do
if GetRawKeyPressed(13) = 1 or startFix = 1
startFix = 0
StartPosition = startposition * -1
if StartPosition = 1
SetcameraPosition(1,0,5000,-7500)
SetCameraRotation(1,0,0,0)
else
SetcameraRotation(1,0,180,0)
SetcameraPosition(1,0,5000,30000)
endif
endif
if GetRawKeyPressed(32) = 1
if renderNow > 0
renderNow = 0
else
renderNow = renderCycle
endif
endif
lpx# = px# // LastPointerX
lpy# = py# // LastPointerY
px# = GetPointerX() // CurrentPointerX
py# = GetPointerY() // CurrentPointerY
pmx# = px# - lpx# // pointerMOVeX
pmy# = py# - lpy# // pointerMoveY
frameTime# = GetFrameTime() // for keeping the camera movement at the same paste no matter what the current FPS is.
MoveCameraLocalX(1,GetDirectionX()*frametime#*5000)
MoveCameraLocalZ(1,-GetDirectionY()*frametime#*5000)
cx# = GetCameraX(1)
cy# = GetCameraY(1)
cz# = GetCameraZ(1)
if pmx# <> 0 or pmy# <> 0
SetCameraRotation(1, GetCameraAngleX(1)+pmy#*frametime# *15,GetCameraAngleY(1)+pmx#*frameTime#*15,GetcameraAngleZ(1))
endif
if renderNow > 1
dec renderNow
endif
if checkingNow > 1
dec checkingNow
endif
if checkingnow = 1
checkingNow = checkingCycle
if renderNow = 1
renderNow = renderCycle
SetRenderToImage(a,0)
clearScreen()
// normal object cycling (not so good, this is just for the porpuse of showing how to use RenderImage) method of hide/show
for i = 1 to 1000
for j = 1 to 10
if GetObjectExists(objs[i,j].id) = 1 // since height is not always 10 because of the random creation
if GetObjectZ(objs[i,j].id) > cz# + viewDistance# or GetObjectZ(objs[i,j].id) < cz# - ViewDistance#
if GetObjectVisible(objs[i,j].id) = 0 then SetObjectVisible(objs[i,j].id,1)
drawObject(objs[i,j].id)
SetObjectVisible(objs[i,j].id,0)
else
if GetObjectVisible(objs[i,j].id) = 0
SetObjectVisible(objs[i,j].id,1)
endif
endif
endif
next j
next i
render()
else // if rendernow = 0
for i = 1 to 1000
for j = 1 to 10
if GetObjectExists(objs[i,j].id) = 1 // since height is not always 10 because of the random creation
if GetObjectZ(objs[i,j].id) > cz# + viewDistance# or GetObjectZ(objs[i,j].id) < cz# - ViewDistance#
if GetObjectVisible(objs[i,j].id) = 1
SetObjectVisible(objs[i,j].id,0)
endif
else
if GetObjectVisible(objs[i,j].id) = 0
SetObjectVisible(objs[i,j].id,1)
endif
endif
endif
next j
next i
endif
endif
Print( ScreenFPS() )
//Sync() // Sync is not valid when using Render to Image.. you can use it .. but you should not. instead:
SetRenderToScreen()
print ("Press Space to turn Render To Image on / off")
print ("Press the Enter key to Jump from Start Position to Back Position")
if renderNow > 0
print("Render To Image On")
SetObjectImage(c,a,0)
DrawObject(c)
else
print("Render To Image Off")
endif
render()
swap()
loop