Recursive rendering to texture

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Recursive rendering to texture

Post by applePi »

in example createRenderTexture.pb in purebasic examples:
CreateRenderTexture(1, CameraID(0), 800, 600)
means : render what camera(0) see as a texture, then we can use this texture as we want.
in the example the camera(0) look at the robot, so the mirror (the plane) show the robot because it is textured with what the camera(0) see.
but if camera(0) look at the mirror then we have a recursive thing.
camera(1) is our eyes.
in the following demo the plane is replaced by a cube, we see several cubes inside cubes, but it is lost in fuzziness at the end, the first cube textured by the cube itself and the sky and the blue ground, but this second cube is textured with another cube and the sky, but not with the blue ground. this is what i can't imagine why ...!!!
with the keys and mouse go inside the cube to feel dizziness from the several mirrors

Code: Select all

Define.f KeyX, KeyY, MouseX, MouseY
#cameraSpeed = 0.1
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0, 0, 0, 800, 600, "recursive render to texture .... mouse/arrows: move camera ...... 'Space': saveRenderTexture", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()

CreateMaterial(6, LoadTexture(6, "ground_diffuse.png"))
CreatePlane(6, 200, 200, 40, 40, 15, 15)
CreateEntity(6,MeshID(6),MaterialID(6), 0, -10,0)
    
LoadTexture(1, "Wood.jpg")

CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 2.5)
;CameraBackColor(0, RGB(0,220,0))

CreateLight(0, RGB(200,200,200), 50,50, 100)

CreateCube(0, 1)

CreateRenderTexture(0, CameraID(0), 256, 256);, #PB_Texture_AutomaticUpdate )
CreateMaterial(0, TextureID(0))
;AddMaterialLayer(0, TextureID(1), #PB_Material_Add)

CreateEntity(0, MeshID(0), MaterialID(0), 0, 0, 0)

CreateCamera(1, 0, 0, 100, 100)
MoveCamera(1, 0, 0, 3)
CameraLookAt(1,0,0,0)
;CameraBackColor(1, RGB(100,200,100))


MaterialCullingMode(0, #PB_Material_NoCulling)

CameraLookAt(0, EntityX(0), EntityY(0), EntityZ(0))
SkyBox("desert07.jpg")


quit.i = 0
Repeat
  Repeat
    event = WindowEvent()
    If event = #PB_Event_CloseWindow
      quit = 1
    EndIf
  Until event = 0
  
  If ExamineMouse()
        MouseX = -MouseDeltaX()/10 
        MouseY = -MouseDeltaY()/10
      EndIf
      
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_Space)
          count + 1
          SaveRenderTexture(0, "pic_" + Str(count) + ".jpg")  
        EndIf
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#cameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #cameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#cameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #cameraSpeed
        Else
          KeyY = 0
        EndIf
        
                                
      EndIf
             
      RotateCamera(1, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (1, KeyX, 0, KeyY)
  
  
  RotateEntity(0, 1/2,1/2,1/2, #PB_Relative)
  RenderWorld()
  
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or quit = 1
the second demo is render to texture in opengl as posted with vs2008 project here: http://lousodrome.net/opengl/#rendertotexture
there is a description here http://stackoverflow.com/questions/1732 ... -recursion
it is almost the same line to line copy to purebasic with OpenGLGadget which is much shorter of course, you can see cubes inside cubes inside cubes ...etc

Code: Select all

Procedure Cube()

glBegin_(#GL_QUADS)

glTexCoord2i_(0, 0) : glVertex3f_(-1, -1, -1) 
glTexCoord2i_(0, 1) : glVertex3f_(-1, -1, 1) 
glTexCoord2i_(1, 1) : glVertex3f_(-1, 1, 1) 
glTexCoord2i_(1, 0) : glVertex3f_(-1, 1, -1) 

glTexCoord2i_(0, 0) : glVertex3f_( 1, -1, -1) 
glTexCoord2i_(0, 1) : glVertex3f_( 1, -1, 1) 
glTexCoord2i_(1, 1) : glVertex3f_( 1, 1, 1) 
glTexCoord2i_(1, 0) : glVertex3f_( 1, 1, -1) 

glTexCoord2i_(0, 0) : glVertex3f_(-1, -1, -1) 
glTexCoord2i_(0, 1) : glVertex3f_(-1, -1, 1) 
glTexCoord2i_(1, 1) : glVertex3f_( 1, -1, 1) 
glTexCoord2i_(1, 0) : glVertex3f_( 1, -1, -1) 

glTexCoord2i_(0, 0) : glVertex3f_(-1, 1, -1) 
glTexCoord2i_(0, 1) : glVertex3f_(-1, 1, 1) 
glTexCoord2i_(1, 1) : glVertex3f_( 1, 1, 1) 
glTexCoord2i_(1, 0) : glVertex3f_( 1, 1, -1) 

glTexCoord2i_(0, 0) : glVertex3f_(-1, -1, -1) 
glTexCoord2i_(0, 1) : glVertex3f_(-1, 1, -1) 
glTexCoord2i_(1, 1) : glVertex3f_( 1, 1, -1) 
glTexCoord2i_(1, 0) : glVertex3f_( 1, -1, -1) 

glTexCoord2i_(0, 0) : glVertex3f_(-1, -1, 1) 
glTexCoord2i_(0, 1) : glVertex3f_(-1, 1, 1) 
glTexCoord2i_(1, 1) : glVertex3f_( 1, 1, 1) 
glTexCoord2i_(1, 0) : glVertex3f_( 1, -1, 1) 

glEnd_() 
EndProcedure


UseJPEGImageDecoder()
UsePNGImageDecoder()

Define *Buffer
Define TextureID.I
Define window_width.l = 500
Define window_height.l = 500

#size = 256

;#ImagePath = #PB_Compiler_Home + "Examples/3D/Data/Textures/"
;LoadImage(0, #ImagePath+"Geebee2.bmp")
;LoadImage(0, #ImagePath+"Wood.jpg")
;*Buffer = EncodeImage(0)
*Buffer = AllocateMemory(3*256*256) ; static unsigned char texture[3 * SIZE * SIZE];

OpenWindow(0, 1, 1, window_width, window_height, "recursive render to texture")
SetWindowColor(0, 0)
OpenGLGadget(0, 0, 0, window_height , window_height)


glEnable_(#GL_TEXTURE_2D)  
glEnable_(#GL_DEPTH_TEST)

; ----- Generate texture
glGenTextures_(1, @TextureID)
glBindTexture_(#GL_TEXTURE_2D, TextureID)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_S, #GL_CLAMP);
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_WRAP_T, #GL_CLAMP);
glTexImage2D_(#GL_TEXTURE_2D, 0, 3, #size, #size, 0, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, *Buffer);*Buffer+57)
FreeMemory(*Buffer)


Repeat

  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) 
  glLoadIdentity_()                                     
  glTranslatef_(0.0, 0.0, -10)
  glRotatef_(30, 1, 0, 0)
  
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glLoadIdentity_()
glTranslatef_(0, 0, -11) 
; 
ro.f+1
glRotatef_(ro, 1, 1, 1)

;'/* Define a view-port adapted to the texture */
glMatrixMode_(#GL_PROJECTION) 
glLoadIdentity_() 
gluPerspective_(20, 1, 5, 15) 
glViewport_(0, 0, #size, #size) 
glMatrixMode_(#GL_MODELVIEW) 

;'/* Render to buffer */
glClearColor_(255, 175, 1, 0) 
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) 

cube()

glFlush_() 

glCopyTexSubImage2D_(#GL_TEXTURE_2D, 0, 5, 5, 0, 0, #size - 10, #size - 10) 

;'/* Render to screen */
glMatrixMode_(#GL_PROJECTION) 
glLoadIdentity_() 
gluPerspective_(20, window_width / window_height, 5, 15) 
glViewport_(0, 0, window_width, window_height) 
glMatrixMode_(#GL_MODELVIEW) 
glClearColor_(0, 0, 0, 0) 

glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) 

cube()
;SetGadgetAttribute(0,#PB_OpenGL_FlipBuffers, #True)

  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)

  If WaitWindowEvent(10) = #PB_Event_CloseWindow
    glDeleteTextures_(1, @TextureID)
    Break
  EndIf
ForEver
Last edited by applePi on Sat Nov 12, 2016 11:19 am, edited 1 time in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Recursive rendering to texture

Post by Kwai chang caine »

Nice one, like always :D
Just a little flickering on the second code, but it's normal because i have a notebook with poor graphical card :|
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Recursive rendering to texture

Post by firace »

Kwai chang caine wrote:Nice one, like always :D
Just a little flickering on the second code, but it's normal because i have a notebook with poor graphical card :|
Thanks for sharing 8)
I also had some (heavy) flickering on the second one but could resolve it by removing this line:
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)

(Why flip buffers twice in a row?)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Recursive rendering to texture

Post by applePi »

thank you Kwai chang caine and firace
(Why flip buffers twice in a row?)
you are right, i have commented the additional flip buffers, and now it is speedier than before.
Post Reply