StartDrawing() crashes with RenderTextures?

Everything related to 3D programming
Khallis
User
User
Posts: 10
Joined: Sun May 05, 2013 7:56 am

StartDrawing() crashes with RenderTextures?

Post by Khallis »

I would like to draw on a RenderTexture. (For example, putting a red "Recording" icon onto the texture of a security camera screen.)

If I do something like,

Code: Select all

  CreateTexture(1,256,256)
  StartDrawing(TextureOutput(1))
  Box(10,10,20,20,RGB(255,0,0))
  StopDrawing()
I get a texture with a small red block at the corner as I would expect.

However, if I do something like this instead:

Code: Select all

  CreateRenderTexture(1, CameraID(0), 256, 256)
  StartDrawing(TextureOutput(1))
  Box(10,10,20,20,RGB(255,0,0))
  StopDrawing()
I get a crash to desktop on every computer I've tried. "The debugged executable quit unexpectedly." No errors reported, and nothing useful in the ogre log. Calling StartDrawing() on a RenderTexture will cause a crash even if no drawing commands follow.

Is this a reproducible crash for others? Is there a workaround for it?

I thought it might be a race condition on the texture, with Ogre perhaps trying to update it during the locked StartDrawing() block - but using #PB_Texture_ManualUpdate on the RenderTexture makes no difference.

If I cannot use drawing commands on a RenderTexture, is there a way to copy the contents of the RenderTexture to a regular Texture so I can draw on it?

Thanks for any assistance!
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: StartDrawing() crashes with RenderTextures?

Post by Comtois »

you can add a layer at your material and draw on a new texture, example :

Code: Select all

   ;- TextureRTT
    ;
    CreateRenderTexture(10, CameraID(1), 800, 600, 1)
    CreateMaterial(10, TextureID(10))
    SetMaterialColor(10, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))
    ;Nouveau !!
    CreateTexture(11, 800, 600)
    StartDrawing(TextureOutput(11))
    DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
    Circle(30, 30, 25, RGBA(255, 0, 0, 255))
    StopDrawing()
    AddMaterialLayer(10, TextureID(11),#PB_Material_AlphaBlend)
You can test it with CheckObjectVisibility2.pb example . Here is the code modified

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - CheckObjectVisibility
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY, Speed = 1.0
Define.i RobotMove

#CameraSpeed = 2

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  
  If Screen3DRequester()
    Add3DArchive("Data/Textures"        , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Models"          , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Scripts"         , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/fonts"           , #PB_3DArchive_FileSystem) 
    Add3DArchive("Data/Packs/desert.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    WorldShadows(#PB_Shadow_Modulative, 3000, RGB(175, 175, 175))
    
    ;- Material
    ;
    GetScriptMaterial(1, "Color/Red")
    GetScriptMaterial(2, "Color/Green")
    GetScriptMaterial(3, "Color/Blue")
    GetScriptMaterial(4, "Color/Yellow")
    CreateMaterial(5, LoadTexture(5, "r2skin.jpg"))
    SetMaterialColor(5, #PB_Material_SelfIlluminationColor, RGB(75, 75, 75))
    
    ;- Ground
    ;
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
    CreatePlane(0, 700, 700, 70, 70, 15, 15)
    CreateEntity(0,MeshID(0),MaterialID(0))
    EntityRenderMode(0, 0)
    
    ;- Mesh
    ;
    LoadMesh(1, "robot.mesh")
    CreateSphere(2, 10)
    
    ;- Entity
    ;
    CreateEntity(1, MeshID(1), MaterialID(5))
    CreateEntity(2, MeshID(2), MaterialID(1), -200, 10,  100)
    CreateEntity(3, MeshID(2), MaterialID(2),  200, 10, 0)
    CreateEntity(4, MeshID(2), MaterialID(3),  200, 10,  100)
    CreateEntity(5, MeshID(2), MaterialID(4), -200, 10, -100)
 
    ScaleEntity(3, 2, 2, 2)
    
    ;- Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 120, 400, #PB_Absolute)
    CameraLookAt(0, EntityX(1), EntityY(1) + 40, EntityZ(1))
    
    CreateCamera(1, 0, 0, 100, 100, 0)
    CameraFOV(1, 60)
    CameraLookAt(1,  EntityX(3), EntityY(3), EntityZ(3))
    
    ;- TextureRTT
    ;
    CreateRenderTexture(10, CameraID(1), 800, 600, 1)
    CreateMaterial(10, TextureID(10))
    SetMaterialColor(10, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))
    CreateTexture(11, 800, 600)
    StartDrawing(TextureOutput(11))
    DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
    Circle(30, 30, 25, RGBA(255, 0, 0, 255))
    StopDrawing()
    AddMaterialLayer(10, TextureID(11),#PB_Material_AlphaBlend)
    
    ;- Billboard group 
    ;
    CreateBillboardGroup(0, MaterialID(10), 68, 48)
    AddBillboard(0, 0, 0, 0, 0)
    
    ;- Attach objects
    ;
    AttachEntityObject(1, "Joint1", CameraID(1), 0, 0, 0, 0, 0, 40)
    AttachEntityObject(1, "Joint1", BillboardGroupID(0), 0, 60, 0, 0, 0, 0)    
    ;-Light
    ;
    CreateLight(0, RGB(255, 255, 255), -40, 100, 80)
    AmbientColor(RGB(80, 80, 80))
    
    ;- Skybox
    ;
    SkyBox("desert07.jpg")
    
    ;- Text3D
    CreateText3D(0, "")
    Text3DColor(0, RGBA(0, 255, 255, 255))
    Text3DAlignment(0, #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)
    
    AttachEntityObject(1, "Joint1", Text3DID(0))
    MoveText3D(0, 0, 30, 0)
    ScaleText3D(0, 6, 6, 0)
    
    Repeat
      Screen3DEvents()
      
      RobotMove = #False    
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          MoveEntity(1, -1 * Speed, 0, 0)
          RotateEntity(1, 0, 180, 0)
          RobotMove = #True
        EndIf  
        
        If KeyboardPushed(#PB_Key_Right)
          MoveEntity(1, 1 * Speed, 0, 0)
          RotateEntity(1, 0, 0, 0)
          RobotMove = #True
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          MoveEntity(1, 0, 0, -1 * Speed)
          RotateEntity(1, 0, 90, 0)
          RobotMove = #True
        EndIf  
        
        If KeyboardPushed(#PB_Key_Down)
          MoveEntity(1, 0, 0, 1 * Speed)
          RotateEntity(1, 0, -90, 0)
          RobotMove = #True
        EndIf
        
        If KeyboardPushed(#PB_Key_PageUp) And Speed < 2.0
          Speed + 0.05
        ElseIf KeyboardPushed(#PB_Key_PageDown) And Speed > 0.1 
          Speed - 0.05
        EndIf
        
      EndIf
      
      If RobotMove
        If EntityAnimationStatus(1, "Walk") = #PB_EntityAnimation_Stopped ; Loop
          StartEntityAnimation(1, "Walk", #PB_EntityAnimation_Manual) ; Start the animation from the beginning
        EndIf
      Else
        StopEntityAnimation(1, "Walk")
      EndIf
      
      AddEntityAnimationTime(1, "Walk", TimeSinceLastFrame)
      
      CameraLookAt(0, EntityX(1), EntityY(1) + 40, EntityZ(1))
      
      ;Check Objects
      Object$ = ""
      
      If CheckObjectVisibility(1, EntityID(2))
        Object$ + "Red Sphere ! " 
      EndIf  
      If CheckObjectVisibility(1, EntityID(3))
        Object$ + "Green Sphere ! " 
      EndIf  
      If CheckObjectVisibility(1, EntityID(4))
        Object$ + "Blue Sphere ! " 
      EndIf  
      If CheckObjectVisibility(1, EntityID(5))
        Object$ + "Yellow Sphere ! " 
      EndIf 
      
      If Object$ = ""
        Object$ = "nothing !"
      EndIf
      
      Text3DCaption(0, "I can see " + Object$)
     
      TimeSinceLastFrame = RenderWorld() * Speed
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
Please correct my english
http://purebasic.developpez.com/
Khallis
User
User
Posts: 10
Joined: Sun May 05, 2013 7:56 am

Re: StartDrawing() crashes with RenderTextures?

Post by Khallis »

Thank you! That's certainly a workable workaround.
Post Reply