TFrameBuffer Type

BlitzMax Modules Forums/MiniB3D/TFrameBuffer Type

i have writen a FBO Class for MiniB3D

here is the code:

Type TFrameBuffer
	Field Texture:TTexture
	Field depthbuffer:Int[1]
	Field fbo:Int[1]

	Function SetBuffer:TFrameBuffer(Texture:TTexture)
		Local FB:TFrameBuffer = New TFrameBuffer
		FB.Texture = Texture
		FB.CreateFBO()
		Return FB
	End Function
   
   
	Method CreateFBO()

		glGenFramebuffersEXT(1, fbo)
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[0])
		glGenRenderbuffersEXT(1, depthbuffer)
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer[0])
		glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, Texture.Width, Texture.Height)
		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer[0]) 
		
		
		glBindTexture(GL_TEXTURE_2D, Texture.gltex[0])
		'	  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  Texture.Width, Texture.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Null);
		'	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
		'	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
		glGenerateMipmapEXT(GL_TEXTURE_2D)
		
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,GL_TEXTURE_2D, Texture.gltex[0], 0)
		
		
		Local status:Int =  glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)
	 
		Select status
			Case GL_FRAMEBUFFER_COMPLETE_EXT
				Print "FBO created"
			Case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
			  	Print "Incomplete attachment"
			Case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
			  	Print "Missing attachment"
			Case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
			  	Print "Incomplete dimensions"
			Case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
			  	Print "Incomplete formats"
			Case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
			 	Print "Incomplete draw buffer"
			Case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
			  	Print "Incomplete read buffer"
			Case GL_FRAMEBUFFER_UNSUPPORTED_EXT
				Print "Type is not Supported"
			Default
			      Print "oops"
		EndSelect
	End Method

	Method Destroy()	
		glDeleteFramebuffersEXT(1, fbo)
		glDeleteRenderbuffersEXT(1, depthbuffer)
	End Method
	   
	Method BindBuffer()
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT , fbo[0])
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
	End Method
	
	Method UnBindBuffer()
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT , 0)
		glBindTexture(GL_TEXTURE_2D,texture.gltex[0]) ' wrong?
		glGenerateMipmapEXT(GL_TEXTURE_2D) ' wrong?
		glBindTexture(GL_TEXTURE_2D,0) ' wrong?

	End Method

End Type


to test use it with this one:
Import "../MiniB3D.bmx"
Include "TFrameBuffer.bmx"

Graphics3D 1024,768
'ClearTextureFilters

Local pivot:TPivot=CreatePivot()
PositionEntity pivot,0,2,0

Local t_sphere:TMesh=CreateSphere( 8 )
EntityShininess t_sphere,.2
For Local t=0 To 359 Step 36
	Local sphere:TMesh=TMesh(CopyEntity( t_sphere,pivot ))
	EntityColor sphere,Rnd(256),Rnd(256),Rnd(256)
	TurnEntity sphere,0,t,0
	MoveEntity sphere,0,0,10
Next
FreeEntity t_sphere

Local FB_texture:TTexture=CreateTexture( 1024,1024 )
ScaleTexture FB_texture,-1,1
Local FB:TFrameBuffer = TFrameBuffer.SetBuffer(FB_texture)

Local cube:TMesh=CreateCube()
EntityTexture cube,FB_texture
PositionEntity cube,0,7,0
ScaleEntity cube,3,3,3

Local light:TLight=CreateLight()
TurnEntity light,45,45,0

Local camera:TCamera=CreateCamera()

Local plan_cam:TCamera=CreateCamera()

'CameraViewport plan_cam,0,0,FB_Texture.Width,FB_Texture.Height
plan_cam.vx=0
plan_cam.vy=0
plan_cam.vwidth=FB_Texture.Width
plan_cam.vheight=FB_Texture.Height

TurnEntity plan_cam,90,0,0
PositionEntity plan_cam,0,20,0

Local d#=-20

While Not KeyHit(KEY_ESCAPE)

	If KeyDown(KEY_UP) d=d+1
	If KeyDown(KEY_DOWN) d=d-1
	If KeyDown(KEY_LEFT) TurnEntity camera,0,-3,0
	If KeyDown(KEY_RIGHT) TurnEntity camera,0,+3,0
	
	PositionEntity camera,0,7,0
	MoveEntity camera,0,0,d
	
	TurnEntity cube,.1,.2,.3
	TurnEntity pivot,0,1,0
	
	UpdateWorld

		HideEntity camera
		ShowEntity plan_cam
	FB.BindBuffer()
		RenderWorld
      FB.UnBindBuffer()

'	BackBufferToTex(texture)

	ShowEntity camera
	HideEntity plan_cam
	RenderWorld

	Flip 1
Wend


i have problems on ATI to generate the Mipmaps after FB creation with nvidia its working fine. Who can help?

2 things i have to add to this ;)

Try your testcode with 2 cameras or test it in general and you may notice that you don't render to the fully texture area. At least this was a problem I had have.


the mipmapgeneration should be done only once while you create the framebuffer. if you want i can send you my current Texturebuffer extension with cubemap support.

the mipmap generation (once at FBO creation) works only with my nvidia card, both Ati's dont.

what do you mean with "dont render to the fully textured area"?



Texturebuffer extension with cubemap support? sounds good! yes