Function addcubemapobject(mesh,detail,Texturelayer=0,blendmode=2)Local t.cubemapobject=New cubemapobjecttentity=meshTx=CreateTexture(detail,detail,1+128+256)EntityTexture mesh,tx,0,TexturelayerTextureBlend tx,blendmodetTexture=txtdetail=detailIf cubemapcameraent=0 Then cubemapcameraent=CreateCamera():CameraProjMode cubemapcameraent,0tupdatefrequency=1tLastupdate=LastcubemapcountCameraProjMode cubemapcameraent,0tcammin=1tcammax=1000CameraRange cubemapcameraent,tcammin,tcammaxupdatecubemap(tTexture,cubemapcameraent,tentity)Lastcubemapcount=Lastcubemapcount+1;MilliSecs()End Function
SuperStrictImport sidesign.minib3dGlobal scx:Int = 640, scy:Int = 480Graphics3D scx, scy, 16, 2Local Light:TLight = createLight() Local PLANE:TMesh = createCube() 'substitute for createplane - as it does not exist in klepto's or simons version ScaleEntity PLANE, 5, 0, 5 PositionEntity PLANE, - 6, - 6, 0 Local gld:TTexture = CreateTexture(255, 255) For Local X:Int = 0 To 128 SetColor X, X * 2, X * 3 DrawRect X, X, 255 - X / 2, 255 - X / 2 NextFlipBackBufferToTex(gld) ClsscaleTexture gld, 100, 100EntityTexture PLANE, gldLocal CAMERA:TCamera = createCamera() CameraClsColor CAMERA, 10, 20, 250 Local PIVOT:TPivot = createPivot() EntityParent CAMERA, PIVOTPositionEntity CAMERA, 0, 20, - 30'Create some entities To cubemapLocal sphere:TMesh = createSphere(20) PositionEntity sphere, 0, 20, 10 ScaleEntity sphere, 10, 10, 10 Local cube:TMesh = createCube() PositionEntity cube, 0, 20, - 10 ScaleEntity cube, 5, 5, 5'Cubemap them!addcubemapobject(cube, 256, 0) addcubemapobject(sphere, 128, 0) 'You dont need To do the Next couple of lines, but its good For efficiency;setcubemapupdatefrequency(cube, 2, 1) 'Update cube every 15 frames, And enable "face per render" (update only a part of the cubemap every update)setcubemapupdatefrequency(sphere, 2, 1) 'Update sphere every 15 frames, And enable "face per render" (update only a part of the cubemap every update)'change the camera range of these two To speed things upcubemeshCameraRange(cube, 10, 250) cubemeshCameraRange(sphere, 10, 250) 'do a render loopLocal mlt:Float = 1.0Repeat If KeyDown(57) Then mlt = 10 TurnEntity PIVOT, 0, .1 * mlt, 0 TurnEntity cube, .1 * mlt, 0, 0 updatecubemaps() 'Update all cubemaps! 'Or you can use ; updatenextcubemap() 'Update Next cubemap! RenderWorld FlipUntil KeyHit(KEY_ESCAPE) Deletecubemapobject(cube) 'Delete the cubemap stuff from "cube" Deletecubemapobject(sphere) 'Delete the cubemap stuff from "sphere" Deleteallcubemapobjects() 'Alternatively, call this at the End To wipe all cubemap stuffEndType cubemapobject Global _list:TList = New TList Field entity:TEntity Field TEXTURE:TTexture Field detail:Int Field cammin:Float, cammax:Float Field Lastupdate:Int, updatefrequency:Int Field Lastcamupdate:Int Field faceperrender:Int Field Lastface:Int = 1 Method New() _list.AddLast(Self) End Method Method remove() entity.FreeEntity() ; entity = Null TEXTURE = Null detail = Null cammin = Null ; cammax = Null lastupdate = Null ; updatefrequency = Null lastcamupdate = Null ; faceperrender = Null lastface = Null _list.remove(Self) End Method End TypeGlobal cubemapcameraent:TCamera = NullGlobal Lastcubemapcount:Int, cubemapcount:IntFunction clearcubemapsystem() Deleteallcubemapobjects() If cubemapcameraent <> Null Then FreeEntity cubemapcameraent ; cubemapcameraent = Null End FunctionFunction addcubemapobject(MESH:TMesh, detail:Int, Texturelayer:Int = 0, blendmode:Int = 2) Local t:cubemapobject = New cubemapobject, Tx:TTexture t.entity = MESH Tx = CreateTexture(detail, detail, 1 + 128 + 256) EntityTexture MESH, tx, 0, Texturelayer TextureBlend tx, blendmode t.TEXTURE = tx t.detail = detail If cubemapcameraent = Null Then cubemapcameraent = createCamera() ; CameraProjMode cubemapcameraent, 0 t.updatefrequency = 1 t.Lastupdate = Lastcubemapcount CameraProjMode cubemapcameraent, 0 t.cammin = 1 t.cammax = 1000 CameraRange cubemapcameraent, t.cammin, t.cammax _UpdateCubemap(t.TEXTURE, cubemapcameraent, t.entity) Lastcubemapcount = Lastcubemapcount + 1 End FunctionFunction cubemeshCameraRange(MESH:TEntity, CameraRangemin:Float, CameraRangemax:Float) 'sets how a cubemaped texture is rendered. Local n:cubemapobject For n = EachIn cubemapobject._list If n.entity = MESH Then n.cammin = CameraRangemin n.cammax = CameraRangemax EndIf Next End FunctionFunction Deleteallcubemapobjects() Local n:cubemapobject For n = EachIn cubemapobject._list Deletecubemapobject n.entity Next If cubemapcameraent <> Null Then FreeEntity cubemapcameraent ; cubemapcameraent = NullEnd FunctionFunction Deletecubemapobject(MESH:TEntity) Local n:cubemapobject For n = EachIn cubemapobject._list If n.entity = MESH Then If n.TEXTURE <> Null Then FreeTexture n.TEXTURE n.remove() EndIf Next 'checkifanycubemapsleft() If cubemapobject._list.count() = 0 Then cubemapobject._list.clear() End FunctionremFunction checkifanycubemapsleft() Local cnt:Int = 0, n:cubemapobject For n = EachIn cubemapobject._list cnt = cnt + 1 Next If cnt = 0 Then clearcubemapsystem() End FunctionendremFunction updatecubemaps() Local o:cubemapobject For o = EachIn cubemapobject._list If (cubemapcount - o.Lastupdate) > o.updatefrequency Then CameraProjMode cubemapcameraent, 0 CameraRange cubemapcameraent, o.cammin, o.cammax _UpdateCubemap(o.TEXTURE, cubemapcameraent, o.entity, o.Lastface * o.faceperrender) ; o.Lastface = (o.Lastface Mod 6) + 1 o.Lastupdate = cubemapcount EndIf Next cubemapcount = cubemapcount + 1 End FunctionFunction updatenextcubemap() Local o:cubemapobject Local cnt:Int = 0, cnt2:Int = 0 For o = EachIn cubemapobject._list cnt2 = cnt2 + 1 Next For o = EachIn cubemapobject._list cnt = cnt + 1 If cubemapcount Mod (cnt2 + 1) = cnt Then'If (cubemapcount-oLastupdate)>oupdatefrequency Then CameraProjMode cubemapcameraent, 0 CameraRange cubemapcameraent, o.cammin, o.cammax _UpdateCubemap(o.TEXTURE, cubemapcameraent, o.entity, o.Lastface * o.faceperrender) ; o.Lastface = (o.Lastface Mod 6) + 1 o.Lastupdate = cubemapcount EndIf Next cubemapcount = cubemapcount + 1 End FunctionFunction setcubemapupdatefrequency(MESH:TEntity, updaterate:Int, faceperrender:Int = 0) Local o:cubemapobject For o = EachIn cubemapobject._list If o.entity = MESH Then o.updatefrequency = updaterate ; o.faceperrender = faceperrender Next End FunctionFunction _UpdateCubemap(tex:TTexture, CAMERA:TCamera, entity:TEntity, side:Int = 0) 'the bit ripped from the Blitz demo Local tex_sz:Int = TextureWidth(tex) ' Show the camera we have specifically created For updating the cubemap CameraProjMode CAMERA, 1 ' Hide entity that will have cubemap applied To it. This is so we can get cubemap from its position, without it blocking the view HideEntity entity ' Position camera where the entity is - this is where we will be rendering views from For cubemap PositionEntity CAMERA, EntityX(entity, 1), EntityY(entity, 1), EntityZ(entity, 1) CameraClsMode CAMERA, False, True ' Set the camera's viewport so it is the same size as our texture - so we can fit entire screen contents into texture CameraViewport CAMERA, 0, 0, tex_sz, tex_sz ' Update cubemap If side = 0 Or side = 1 Then ' do Left view SetCubeFace tex, 0 RotateEntity CAMERA, 0, 90, 0 RenderWorld 'CopyRect 0, 0, tex_sz, tex_sz, 0, 0, BackBuffer(), TextureBuffer(tex) BackBufferToTex(tex) EndIf If side = 0 Or side = 2 Then ' do forward view SetCubeFace tex, 1 RotateEntity CAMERA, 0, 0, 0 RenderWorld 'CopyRect 0, 0, tex_sz, tex_sz, 0, 0, BackBuffer(), TextureBuffer(tex) BackBufferToTex(tex) EndIf If side = 0 Or side = 3 Then ' do Right view SetCubeFace tex, 2 RotateEntity CAMERA, 0, - 90, 0 RenderWorld 'CopyRect 0, 0, tex_sz, tex_sz, 0, 0, BackBuffer(), TextureBuffer(tex) BackBufferToTex(tex) EndIf If side = 0 Or side = 4 Then ' do backward view SetCubeFace tex, 3 RotateEntity CAMERA, 0, 180, 0 RenderWorld 'CopyRect 0, 0, tex_sz, tex_sz, 0, 0, BackBuffer(), TextureBuffer(tex) BackBufferToTex(tex) EndIf If side = 0 Or side = 5 Then ' do up view SetCubeFace tex, 4 RotateEntity CAMERA, - 90, 0, 0 RenderWorld 'CopyRect 0, 0, tex_sz, tex_sz, 0, 0, BackBuffer(), TextureBuffer(tex) BackBufferToTex(tex) EndIf If side = 0 Or side = 6 Then ' do down view SetCubeFace tex, 5 RotateEntity CAMERA, 90, 0, 0 RenderWorld 'CopyRect 0, 0, tex_sz, tex_sz, 0, 0, BackBuffer(), TextureBuffer(tex) BackBufferToTex(tex) EndIf ' Show entity again ShowEntity entity ' Hide the cubemap camera CameraProjMode CAMERA, 0 End Function