Page 1 of 1

renderworld to image

Posted: Mon Aug 13, 2007 1:13 am
by citystate
Here's a tough one for you...

I'd like to know how someone would code the following:
  • 1. render a small scene to an image
    2. create a texture from the new image
    3. paint texture onto a billboard in the main scene
    4. render the main scene to the screen
really the final 3 steps are trivial, the big problem is the first one - does anyone know if it's even possible?

Posted: Mon Aug 13, 2007 12:09 pm
by Thalius
really the final 3 steps are trivial, the big problem is the first one - does anyone know if it's even possible?
it should be.

Just don't do a flipbuffers yet ( build a render-stage switch ) then make a screenshot from the backbuffer to a texture or memorybuffer.
use that as a texture for the next stage. render again and flipbuffer.

Thalius

Posted: Mon Aug 13, 2007 2:46 pm
by netmaestro
Do a GrabSpriteEx and pass 0 for buffer right after you do RenderWorld() and before Flipbuffers(), this will allow you to grab a sprite from the Renderworlded backbuffer before it's displayed on the screen:

Code: Select all

Procedure.l GrabSpriteEx(sprite, buffer, x, y, width, height, mode=0) 
  ; netmaestro 2007
  ; Grab a Sprite from the backbuffer or visible screen
  ; buffer values: 0=backbuffer, 1=visible screen
  If buffer 
    srcDC = GetDC_(ScreenID()) 
  Else 
    srcDC = StartDrawing(ScreenOutput()) 
  EndIf 
  trgDC = CreateCompatibleDC_(srcDC) 
  BMPHandle = CreateCompatibleBitmap_(srcDC, Width, Height) 
  SelectObject_( trgDC, BMPHandle) 
  BitBlt_( trgDC, 0, 0, Width, Height, srcDC, x, y, #SRCCOPY) 
  DeleteDC_( trgDC) 
  If buffer 
    ReleaseDC_(ScreenID(), srcDC) 
  Else 
    StopDrawing() 
  EndIf 
  result = CreateSprite(sprite, width, height, mode) 
  If sprite = #PB_Any 
    output = result 
  Else 
    output = sprite 
  EndIf 
  StartDrawing(SpriteOutput(output)) 
    DrawImage(BMPHandle, 0, 0) 
  StopDrawing() 
  DeleteObject_(BMPHandle) 
  ProcedureReturn output 
EndProcedure  

Posted: Tue Aug 14, 2007 4:41 am
by citystate
Thanks guys, I'll check it out once I'm back from work

Cheers

Posted: Tue Mar 03, 2009 6:30 pm
by Blobby
netmaestro wrote:Do a GrabSpriteEx and pass 0 for buffer right after you do RenderWorld() and before Flipbuffers(), this will allow you to grab a sprite from the Renderworlded backbuffer before it's displayed on the screen:
Oooh it works!

But I had to pass 1 as the buffer or computer not happy.

Re: renderworld to image

Posted: Sat Mar 06, 2010 8:59 am
by dobro
@Fred

tu compte intégrer cette procedure (GrabspriteEX())a PureBasic ?
parce que cette procedure marche (sur nvidia 7600/ et sur NC10 )
apres un Renderworld() alors que la version actuel GrabSprite() ne marche pas ..

Merci a Netmaestro :)

Web Translate
you incorporate this procedure into account (GrabspriteEX ()) has PureBasic
because this procedure works (on nvidia 7600 / and NC10)
Renderworld after a () while the current version GrabSprite () does not work ..

Netmaestro : thank you:)

Code: Select all

; "GrabSprite" doesn't work after "RenderWorld" - Sample code
; March 05 2010
; PB v4.41, Windows XP SP2, GPU intel 82915G


Procedure.l GrabSpriteEx(sprite, buffer, x, y, width, height, mode=0)
    ; netmaestro 2007
    ; Grab a Sprite from the backbuffer or visible screen
    ; buffer values: 0=backbuffer, 1=visible screen
    If buffer
        srcDC = GetDC_(ScreenID())
    Else
        srcDC = StartDrawing(ScreenOutput())
        EndIf
        trgDC = CreateCompatibleDC_(srcDC)
        BMPHandle = CreateCompatibleBitmap_(srcDC, width, height)
        SelectObject_( trgDC, BMPHandle)
        BitBlt_( trgDC, 0, 0, width, height, srcDC, x, y, #SRCCOPY)
        DeleteDC_( trgDC)
        If buffer
            ReleaseDC_(ScreenID(), srcDC)
        Else
        StopDrawing()
    EndIf
    result = CreateSprite(sprite, width, height, mode)
    If sprite = #PB_Any
        output = result
    Else
        output = sprite
    EndIf
    StartDrawing(SpriteOutput(output))
        DrawImage(BMPHandle, 0, 0)
    StopDrawing()
    DeleteObject_(BMPHandle)
    ProcedureReturn output
EndProcedure 



;- Initialisation
If InitEngine3D() = 0
    MessageRequester( "Error" , "Can't initialize 3D, check if engine3D.dll is available" , 0 )
    End
ElseIf InitSprite() = 0 Or InitKeyboard() = 0
    MessageRequester( "Error" , "Can't find DirectX 7.0 or above" , 0 )
    End
EndIf

result = MessageRequester("GrabSprite & RenderWorld","Full Screen ?",#PB_MessageRequester_YesNo)
If result = 6     
    FullScreen=#True
    OpenScreen(800,600,32,"GrabSprite & RenderWorld")
Else           
    FullScreen=#False
    OpenWindow(0,0, 0, 800 , 600 ,"GrabSprite & RenderWorld",#PB_Window_ScreenCentered)
    OpenWindowedScreen(WindowID(0),0,0, 800 , 600,0,0,0)
EndIf

;- Mesh
nummesh = CreateMesh(#PB_Any,100)
SetMeshData(nummesh, #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate, ?PlanVertices, 4)
SetMeshData(nummesh, #PB_Mesh_Face, ?PlanFaces, 2)

;- Material
Add3DArchive(".",#PB_3DArchive_FileSystem)

CreateImage(0,32,32)
StartDrawing(ImageOutput(0))
    Box(0, 0, ImageWidth(0), ImageHeight(0), $FFFFFF)
    Line(0,0,32,32,$0000FF)
StopDrawing()
SaveImage(0,"temp.bmp")
FreeImage(0)
LoadTexture(0,"temp.bmp")
DeleteFile("temp.bmp")

CreateMaterial(0,TextureID(0))

;- Entity
numEntity = CreateEntity(#PB_Any,MeshID(nummesh),MaterialID(0))
EntityLocate(numEntity,0,0,0)

;- Camera
CreateCamera(0, 0, 0 , 100 , 100)
CameraBackColor(0,$FF0000)
MoveCamera(0,0,0,-5)
CameraLookAt(0,0,0,0)

;- Main loop
Repeat
    If fullscreen = #False
        While WindowEvent() : Wend
    EndIf
    
    ; Spin entity
    RotateEntity(numEntity,0,0,0.5,#PB_Relative)
    
    ; Render
    RenderWorld()
    
    ; Grab the screen's center in the sprite #0
    ;GrabSprite(0,400,300,32,32)      <= this doesn't work :(
    GrabSpriteEx(0,1,400,300,32,32) ; <= this seems to work :)
    
    ; Display sprite
    DisplaySprite(0,10,10)
    
    ; Flip buffers
    FlipBuffers()
    
    ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)

End


;- data Section
DataSection
    
    PlanVertices:
    Data.f -0.5, -0.5, 0.0
    Data.f 1, 1, 1
    Data.f 0, 1
    Data.f 0.5, -0.5, 0.0
    Data.f 1,1,1
    Data.f 1, 1
    Data.f 0.5, 0.5, 0.0
    Data.f 1,1,1
    Data.f 1, 0
    Data.f -0.5, 0.5, 0.0
    Data.f 1,1,1
    Data.f 0,0
    
    PlanFaces:
    Data.w 2, 1, 0
    Data.w 0, 3, 2
    
EndDataSection