max2d on MaxGui canvas - resize trouble

BlitzMax Modules Forums/MiniB3D/max2d on MaxGui canvas - resize trouble

Thanks for a great v.0.45 release.

I'm trying to hack together a working example of max2d on a resizable canvas with minib3d.

I can't seem to make max2D resize to fit the canvas.
I was thinking SetViewport 0,0, TGlobal.width, TGlobal.height in the EVENT_WINDOWSIZE event should do it, but it doesn't seem to work.

Can somebody help?

(resize window larger and hit spacebar to see clipping of fireworks)

'Import "../minib3d.bmx"
Strict
Import sidesign.minib3d
Import maxgui.win32maxguiex

Global width ,height 


SetGraphicsDriver GLMax2DDriver(),GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER

Local win:TGadget=CreateWindow("MiniB3D with Max 2d in a GUI window", 10, 10, 512, 512 )

Local can:TGadget=CreateCanvas(0,0,ClientWidth(win),ClientHeight(win),win,0)
SetGadgetLayout can,1,1,1,1

TGlobal.width=ClientWidth(win)
TGlobal.height=ClientHeight(win)

TGlobal.depth=16
TGlobal.mode=0
TGlobal.rate=60

SetGraphics CanvasGraphics(can)

TGlobal.GraphicsInit()

Local cam:TCamera=CreateCamera()
PositionEntity cam,0,0,-10

Local light:TLight=CreateLight(1)

Local tex:TTexture=LoadTexture("media/test.png")

Local cube:TMesh=CreateCube()
Local sphere:TMesh=CreateSphere()
Local cylinder:TMesh=CreateCylinder()
Local cone:TMesh=CreateCone() 

PositionEntity cube,-6,0,0
PositionEntity sphere,-2,0,0
PositionEntity cylinder,2,0,0
PositionEntity cone,6,0,0

EntityTexture cube,tex
EntityTexture sphere,tex
EntityTexture cylinder,tex
EntityTexture cone,tex

Local cx#=0
Local cy#=0
Local cz#=0

Local pitch#=0
Local yaw#=0
Local roll#=0

' used by fps code
Local old_ms:Int=MilliSecs()
Local renders:Int
Local fps:Int

Local up_key:Int
Local down_key:Int
Local left_key:Int
Local right_key:Int

CreateTimer( 60 )

' Create a spark type
Type spark
	Field x#,y#,z#,vy#,xd#,yd#,zd#,r#,g#,b#,alpha#
End Type

' Load spark image
Global sparki:TImage=LoadImage("media/spark.png")

' Set no. of sparks to be created per firework
Global no_sparks=500

' Create spark list
Global spark_list:TList=New TList

' Load and set font
Global font:TImageFont=LoadImageFont("Arial.ttf",1)
SetImageFont font



While True

	WaitEvent()

	Select EventID()

		Case EVENT_KEYDOWN
		
			Select EventData()
			
				Case KEY_SPACE
					AddFireWork()
			
				Case KEY_ESCAPE
					End
				Case KEY_UP
					up_key=True
				Case KEY_DOWN
					down_key=True			
				Case KEY_LEFT
					left_key=True
				Case KEY_RIGHT
					right_key=True	
			EndSelect
			
		Case EVENT_KEYUP
		
			Select EventData()
				Case KEY_UP
					up_key=False
				Case KEY_DOWN
					down_key=False			
				Case KEY_LEFT
					left_key=False
				Case KEY_RIGHT
					right_key=False	
			EndSelect

		Case EVENT_WINDOWCLOSE
		
            End

		Case EVENT_WINDOWSIZE
		
			TGlobal.width=ClientWidth(win)
			TGlobal.height=ClientHeight(win)

			cam.CameraViewport(0,0,TGlobal.width,TGlobal.height)
							
			width=ClientWidth(win) 				
			height=ClientHeight(win)
									
									
			' alas no effect ?												
			SetViewport 0,0, 	TGlobal.width, TGlobal.height
									
							
			DebugLog "EVENT_WINDOWSIZE" 

		Case EVENT_TIMERTICK

			If up_key Then cz#=cz#+1.0
			If left_key Then cx#=cx#-1.0
			If right_key Then cx#=cx#+1.0
			If down_key Then cz#=cz#-1.0

			MoveEntity cam,cx#*0.5,cy#*0.5,cz#*0.5
			RotateEntity cam,pitch#,yaw#,roll#
			
			cx#=0
			cy#=0
			cz#=0

			RedrawGadget can
              
		Case EVENT_GADGETPAINT
			
			SetGraphics CanvasGraphics(can)

			TurnEntity cube,0,1,0

			RenderWorld
			
	BeginMax2D() ' MiniB3D function
	UpdateMax2D() ' your function
	EndMax2D() ' MiniB3D function

	Flip 0
	'SetClsColor 200,0,0
	'Cls
                                
	EndSelect
	
Wend




End


Function AddFireWork()

	' If space key pressed then create new set of sparks (new firework)

	'If KeyHit(KEY_SPACE) 'Or  2=2

		Local x#=Rand(-100,100)
		Local y#=Rand(-100,100)
		Local z#=200

		Local r#=Rand(255)
		Local g#=Rand(255)
		Local b#=Rand(255)

		For Local i=1 To no_sparks

			Local speed# = 0.1

			Local ang1# = Rnd!(360)
			Local ang2# = Rnd!(360)

			Local sp:spark=New Spark
			spark_list.AddLast sp

			sp.x=x#
			sp.y=y#
			sp.z=z#

			sp.xd=Cos(ang1#)*Cos(ang2#)*speed#
			sp.yd=Cos(ang1#)*Sin(ang2#)*speed#
			sp.zd=Sin(ang1#)*speed#
	
			sp.r=r
			sp.g=g
			sp.b=b
	
			sp.alpha=1
	
		Next

'	EndIf

End Function

Function UpdateMax2D()


	' Draw all sparks

	For Local sp:spark=EachIn spark_list

		' If spark alpha is above 0 then draw it...

		If sp.alpha>0

			sp.x=sp.x+sp.xd*10.0
			sp.y=sp.y+sp.yd*10.0
			sp.z=sp.z+sp.zd*10.0
			sp.y=sp.y+sp.vy#
			sp.vy=sp.vy+0.02

			' Calculate x and y draw values based on x,y,z co-ordinates
			Local x#=(width/2.0)+((sp.x/sp.z)*500)
			Local y#=(height/2.0)+((sp.y/sp.z)*500)

			sp.alpha=sp.alpha-0.01

			SetColor sp.r#,sp.g#,sp.b#
			SetBlend LIGHTBLEND
			SetAlpha sp.alpha
			SetScale 20/sp.z,20/sp.z
			DrawImage sparki,x#,y#

		'...else remove spark from spark list

		Else

			spark_list.Remove sp

		EndIf

	Next

	SetBlend SOLIDBLEND
	SetScale 1,1
	SetColor 255,255,255
	DrawText "Press space to ignite firework",0,20
	
End Function


Sorry for the delay: here is a solution for your problem:

Replace the BeginMax2D() code block in your code with this:

		
	BeginMax2D() ' MiniB3D function
	glViewport(0,0,TGlobal.Width,TGlobal.Height)
		glScissor(0,0,TGlobal.Width,TGlobal.Height)


ah looks like i may need to do that as well.

LOL i just tried it without BeginMax2D and it draws the 2d in 3d.

just tested this, it does align the 2d however its scaled instead of repositioned, so when i try to draw something like a wireframe its offcentered.

Thanks for looking into this.

Yes, get what Leon gets too.
F.ex. the text get stretched with the example above.

Yeah, getting this also with the new minib3d 045, will have a closer look tomorrow.

Function BeginMax2D()

Local x,y,w,h
		GetViewport(x,y,w,h)
		
		glDisable(GL_LIGHTING)
		glDisable(GL_DEPTH_TEST)
		glDisable(GL_SCISSOR_TEST)
		glDisable(GL_FOG)
		glDisable(GL_CULL_FACE)

		glMatrixMode GL_TEXTURE
		glLoadIdentity
		
		glMatrixMode GL_PROJECTION
		glLoadIdentity
		glOrtho 0,GraphicsWidth(),GraphicsHeight(),0,-1,1
		
		glMatrixMode GL_MODELVIEW
		glLoadIdentity
		
		SetViewport x,y,w,h
		
		
		Local MaxTex:Int 
		glGetIntegerv(GL_MAX_TEXTURE_UNITS, Varptr(MaxTex))

		
		For Local Layer = 0 Until MaxTex
			glActiveTexture(GL_TEXTURE0+Layer)
					
			glDisable(GL_TEXTURE_CUBE_MAP)
			glDisable(GL_TEXTURE_GEN_S)
			glDisable(GL_TEXTURE_GEN_T)
			glDisable(GL_TEXTURE_GEN_R)
	
			glDisable(GL_TEXTURE_2D)
		Next
		
		glActiveTexture(GL_TEXTURE0)
		
		'DrawRect - 10 , - 10 , 5 , 5
		
		glViewport(0,0,TGlobal.Width,TGlobal.Height)
		glScissor(0,0,TGlobal.Width,TGlobal.Height)
		

End Function

Function EndMax2D()

		glDisable(GL_TEXTURE_CUBE_MAP)
		glDisable(GL_TEXTURE_GEN_S)
		glDisable(GL_TEXTURE_GEN_T)
		glDisable(GL_TEXTURE_GEN_R)
	
		glDisable(GL_TEXTURE_2D)

		TGlobal.EnableStates()

End Function


Replace these Functions with the original ones or just insert them in your App to override the original ones. This should work.

YES! Those work. Thanks!

awesome