Max2D in MiniB3D v0.41
BlitzMax Modules Forums/MiniB3D/Max2D in MiniB3D v0.41
Just replace the GraphicsInit() function (TGlobal.bmx line 311) with these three functions and it's ready to use.
Edit 16 May 07: Sorry about that guys it should be working 100% now.
Function GraphicsInit()
TTexture.TextureFilter("",9)
glewInit() ' required for glActiveTextureARB
'Save the Max2D settings for later
glPushAttrib GL_ALL_ATTRIB_BITS
glPushClientAttrib GL_CLIENT_ALL_ATTRIB_BITS
glMatrixMode GL_MODELVIEW
glPushMatrix
glMatrixMode GL_PROJECTION
glPushMatrix
glMatrixMode GL_TEXTURE
glPushMatrix
glMatrixMode GL_COLOR
glPushMatrix
EnableStates()
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR)
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE)
glClearDepth(1.0)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glAlphaFunc(GL_GEQUAL,0.5)
End Function
'enter Max2D mode
Function BeginMax2D()
glPopClientAttrib
glPopAttrib
glMatrixMode GL_MODELVIEW
glPopMatrix
glMatrixMode GL_PROJECTION
glPopMatrix
glMatrixMode GL_TEXTURE
glPopMatrix
glMatrixMode GL_COLOR
glPopMatrix
End Function
'return to MiniB3D mode
Function EndMax2D()
'Save the Max2D settings for later
glPushAttrib GL_ALL_ATTRIB_BITS
glPushClientAttrib GL_CLIENT_ALL_ATTRIB_BITS
glMatrixMode GL_MODELVIEW
glPushMatrix
glMatrixMode GL_PROJECTION
glPushMatrix
glMatrixMode GL_TEXTURE
glPushMatrix
glMatrixMode GL_COLOR
glPushMatrix
EnableStates()
glDisable GL_TEXTURE_2D
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR)
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE)
glClearDepth(1.0)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glAlphaFunc(GL_GEQUAL,0.5)
End Function
To start using Max2D commands use TGlobal.BeginMax2D() and when you want to go back to using MiniB3D commands simple use TGlobal.EndMax2D() like so.
Import "minib3d.bmx"
Graphics3D 640,480,16,0
'MiniB3d commands go here
TGlobal.BeginMax2D
'Max2D commands go in here
TGlobal.EndMax2D
'Etc...
Enjoy.
Works great. Thanks!
H
Been trying to use 2d command in minib3d, But i'm having no luck. Tried the this mod and it seems to work better. But when I tried the attached code all I get is block instead of text. Any Ideas what i'm doing wrong.
Thanks in advance
-------
Import sidesign.minib3d
Graphics3D 800,600
Global camera=CreateCamera()
Global light=CreateLight()
Global world=CreateSphere(12)
PositionEntity camera,90,0,0
PositionEntity world,0,0,0
RenderWorld
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha(0.5)
SetColor 255,0,0
DrawText("Hello",50,50)
TGlobal.EndMax2D
Flip
WaitKey()
------------
For me the text displays but not the sphere because it is out of the camera's view. However, if I fix the camera's coordinates the sphere still does not display unless I move RenderWorld and Flip into a render loop and disable the 2d stuff, like so:
Import sidesign.minib3d
Graphics3D 800,600
Global camera=CreateCamera()
Global light=CreateLight()
Global world=CreateSphere(12)
PositionEntity camera,-5,0,-10 '***
PositionEntity world,0,0,0
PointEntity camera, world '***
While Not KeyHit(KEY_ESCAPE) '***
RenderWorld
Rem '***
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha(0.5)
SetColor 255,0,0
DrawText("Hello",50,50)
SetBlend SolidBlend
TGlobal.EndMax2D
EndRem '***
Flip
Wend
End
If I uncomment the 2d stuff the sphere will not display but the text will.
I don't know why the hell it isn't working, as I have far more complicated 2d over 3d stuff working without problems (aside from reduced framerates as expected).
If I had more time, I'd investigate further but for now I can't help you. Perhaps Oddball has some ideas?
Looks like it's something to do with lighting/texturing/coloring initialisation. In the above example (with the 2d stuff uncommented) the sphere is there, it's just black on a black background. And the EntityColor command has no effect.
If, however, I apply a texture to the sphere, then it all seems okay.
SuperStrict
Import sidesign.minib3d
Graphics3D 800, 600
Local camera:TCamera = CreateCamera()
Local light:TLight = CreateLight()
Local sphere:TMesh = CreateSphere()
EntityColor sphere, 200, 100, 255
EntityTexture sphere, LoadTexture("c:\program files\blitzmax\samples\firepaint\player.png")
PositionEntity camera, -5.0, 0.0, -10.0
PositionEntity sphere, 0.0, 0.0, 0.0
PointEntity camera, sphere
While Not KeyHit(KEY_ESCAPE)
Cls
RenderWorld
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha 0.5
SetColor 255, 0, 0
DrawText "Hello", 50, 50
TGlobal.EndMax2D
Flip
Wend
End
work!
i change a Import sidesign.minib3d
in Import klepto.minib3d
Oops! Just shows that one line of code can make all the difference. I've edited the code in the first post it should work fine now. Let me know if there are anymore issues. On a side note it is possible to make this work seemlessly with MiniB3D, i.e. Without the BeginMax2D and EndMax2D commands, but it's a little more than just a cut and paste. If I find time, and people are interested, I'll make a tutorial for it.
thanks oddball for code!
mongia
SuperStrict
Import klepto.minib3d
'my problem is
' i have a desktop resolution 1024 x 768
' drawrect is a not a 0,0,50,20
' i dont visible a rect!
'windows is Not a 0,0 but is a 70,50!
' windows not a center
Graphics3D 1024,768
Local camera:TCamera = CreateCamera()
Local light:TLight = CreateLight()
Local sphere:TMesh = CreateSphere()
EntityColor sphere, 200, 100, 255
PositionEntity camera, -5.0, 0.0, -10.0
PositionEntity sphere, 0.0, 0.0, 0.0
PointEntity camera, sphere
While Not KeyHit(KEY_ESCAPE)
Cls
RenderWorld
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha 0.5
SetColor 255, 0, 0
DrawText "Hello", 50, 50
DrawRect 0,0,50,20
TGlobal.EndMax2D
Flip
Wend
End
thanks
This is actually a bug in Max2D. Try this code which contains no MiniB3D code at all and you get the same results.
SuperStrict
SetGraphicsDriver(GLMax2DDriver())
Graphics 1024,768
While Not KeyHit(KEY_ESCAPE)
Cls
SetBlend AlphaBlend
SetAlpha 0.5
SetColor 255, 0, 0
DrawText "Hello", 50, 50
DrawRect 0,0,50,20
Flip
Wend
End
It will work fine in fullscreen. I've posted a
bug report about this.
in full screen work!!
Hi, thanks for the code! I have been experimenting with it and got some strange results when using MaxGUI. I have posted a bare bones version. In MaxGUI after receiving an EVENT_GADGETPAINT you need to use SetGraphics(CanvasGraphics(canvas)) to select the canvas in which drawing should take place. When I do that the rect first shows up momentarily, then becomes invisible. If I comment out SetGraphics() it works, but then I can have only one canvas gadget and have to hold my breath that MaxGUI doesn't decide to redirect drawing somewhere. Now to make things even stranger, try commenting in the second DrawRect()! It solves the problem. First rect is visible again! Could someone confirm this? Or perhaps enlighten me on what the h** is going on here! ;)
Mac PPC G4 1 GHz, GeForce4 MX, MacOS X 10.4.9
Import oddball.MiniB3D 'sidesign with oddball Max2D hack
'set graphics driver
Local flags:Int = GRAPHICS_BACKBUFFER | GRAPHICS_DEPTHBUFFER | GRAPHICS_ACCUMBUFFER
SetGraphicsDriver(GLMax2DDriver(), flags)
'create window
Local window:TGadget = CreateWindow("MiniB3D MaxGUI", ..
0, 21, 800, 600, Null, WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS)
'create canvas
Local canvas:TGadget = CreateCanvas(0, 0, ..
ClientWidth(window), ClientHeight(window), window)
'initialize 3D graphics
TGlobal.width = ClientWidth(window)
TGlobal.height = ClientHeight(window)
TGlobal.depth = 32
TGlobal.mode = 0
TGlobal.rate = 60
SetGraphics(CanvasGraphics(canvas))
TGlobal.GraphicsInit()
'setup scene
camera = CreateCamera()
PositionEntity camera, 0, 0, -10
Local light:TLight = CreateLight(1)
Local cube:TMesh = CreateCube()
PositionEntity cube,-6,0,0
Repeat
If (PollEvent() = EVENT_WINDOWCLOSE) Then Exit
'** this line makes rect invisible **
SetGraphics(CanvasGraphics(canvas))
RenderWorld
TGlobal.BeginMax2D
SetColor(255, 255, 255)
DrawRect(32, 32, 128, 400)
SetColor(255, 0, 0)
DrawText "Hello", 50, 50
'DrawRect(0,0,1,1)
TGlobal.EndMax2D
Flip
Forever
End
Not had chance to test your code, but will take a look when I get a chance. I'm not suprised that MaxGUI makes my code do strange things. I didn't really take MaxGUI into consideration when writing it.
If... people are interested
*raises hand*
@JonasL: I've just had a quick look at your code and the reason it's acting funny is because your SetGraphics is outside the Max2D zone. So in effect it does nothing as when BeginMax2D is called it resets the graphics to it's state at the last EndMax2D. Here's your code with a little alteration.
Import oddball.MiniB3D 'sidesign with oddball Max2D hack
'set graphics driver
Local flags:Int = GRAPHICS_BACKBUFFER | GRAPHICS_DEPTHBUFFER | GRAPHICS_ACCUMBUFFER
SetGraphicsDriver(GLMax2DDriver(), flags)
'create window
Local window:TGadget = CreateWindow("MiniB3D MaxGUI", ..
0, 21, 800, 600, Null, WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS)
'create canvas
Local canvas:TGadget = CreateCanvas(0, 0, ..
ClientWidth(window), ClientHeight(window), window)
'initialize 3D graphics
TGlobal.width = ClientWidth(window)
TGlobal.height = ClientHeight(window)
TGlobal.depth = 32
TGlobal.mode = 0
TGlobal.rate = 60
SetGraphics(CanvasGraphics(canvas))
TGlobal.GraphicsInit()
'setup scene
camera = CreateCamera()
PositionEntity camera, 0, 0, -10
Local light:TLight = CreateLight(1)
Local cube:TMesh = CreateCube()
PositionEntity cube,-6,0,0
Repeat
If (PollEvent() = EVENT_WINDOWCLOSE) Then Exit
TGlobal.BeginMax2D
SetGraphics(CanvasGraphics(canvas))
TGlobal.EndMax2D
RenderWorld
TGlobal.BeginMax2D
SetColor(255, 255, 255)
DrawRect(32, 32, 128, 400)
SetColor(255, 0, 0)
DrawText "Hello", 50, 50
'DrawRect(0,0,1,1)
TGlobal.EndMax2D
Flip
Forever
EndUsing this method you should also be able to have multiple canvases. But I've not tested multiple canvases yet so please feel free to prove me wrong :)
@jhocking: Well there's one vote at least. My integrated version also has the benefit of avoiding issues like the one above as it's always in Max2D mode.
@Oddball: Many thanks for helping me with this one! I'm only using one canvas, but it still feels a lot safer.
The problem with hacking the engine is that it is very much work in progress and new versions are being released rather often. I really think simonh is doing an excellent job with MiniB3D, but IMHO all the forking is a bad idea. There should be one official open source MiniB3D project. That is the only way we will be able to iron out more bugs than we create while adding new features. That project should be tightly managed and we should have a code repository (CVS, Subversion), some bug tracker (Flyspray, Bugzilla) and a proper source documentation tool. However, that is not what simonh had in mind and it is very much still his project, which I respect.
The problem with hacking the engine is that it is very much work in progress and new versions are being released rather often
Well until simonh adds new code to the GraphicsInit function this hack should work. Basically my code just pushes everything possible onto their relivant stacks then pops the values off again when needed. I actually push more data than is needed but ,as you say, with it still being in development I thought it was wise to err on the side of caution.
I don't use MiniB3D personally, I've got my own engine, but I'll pop back each new update to check this code is still valid.
my problem is:
if use a renderworld
in render 2d
i have a problem for graphics position!
Import klepto.minib3d
SuperStrict
Graphics3D 1024 , 768' , 32 , 1
ClearTextureFilters()
Global camera:TCamera = CreateCamera()
Global luce:TLight = CreateLight()
Global piano:TMesh = CreateCube()
ScaleEntity piano,1000,.1,1000
EntityOrder piano,5
PositionEntity camera, 0.0, 100, 0.0
PointEntity camera, piano
While Not KeyHit(KEY_ESCAPE)
PositionEntity camera,0,100,0
RenderWorld()
render_2d()
Flip
Wend
Function render_2d()
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha 1
SetColor 255, 255, 255
DrawRect 0,0,20,20
If KeyDown(KEY_F1)
RenderWorld()
EndIf
TGlobal.EndMax2D
End Function
thanks
I'm not exactly what you're trying to do with that code mongia2, but basically miniB3D code
cannot be used within a BeginMax2D and EndMax2D block. Just move the RenderWorld outside the Begin/EndMax2D block and it works fine. See below.
Import "miniB3D.bmx"
SuperStrict
Graphics3D 1024 , 768' , 32 , 1
ClearTextureFilters()
Global camera:TCamera = CreateCamera()
Global luce:TLight = CreateLight()
Global piano:TMesh = CreateCube()
ScaleEntity piano,1000,.1,1000
EntityOrder piano,5
PositionEntity camera, 0.0, 100, 0.0
PointEntity camera, piano
While Not KeyHit(KEY_ESCAPE)
PositionEntity camera,0,100,0
RenderWorld()
render_2d()
'Flip is shared by miniB3D and Max2D so can be put in or outside of the code block
Flip
Wend
Function render_2d()
'Put Max2D code in here
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha 1
SetColor 255, 255, 255
DrawRect 0,0,20,20
TGlobal.EndMax2D
'Return to using miniB3D code
If KeyDown(KEY_F1)
RenderWorld()
'Remember RenderWorld clears the display so all previous drawing will be overwritten
EndIf
End Function
There are exceptions to this rule but it's safest to just use Max2D commands within the Begin/EndMax2D block and just use miniB3D commands outside the Begin/EndMax2D block. If need be use more than one block, but don't mix the code.
Edit: Thought I'd better add too that this code is designed for the vanilla version of miniB3D. Whilst it may work with other versions, like klepto's, I can't guarantee it.
i send a bug!
i correct thanks!!!
but i have a problem form velocity and slow with a manipolation texture
I can confirm that it works in my version as I have implemnted these extra functions already. These works much better than mine previously included max2d interfacing . So much thx to you Oddball.
So much thx to you Oddball
No problem. Just glad I could help out the miniB3D project.
Well until simonh adds new code to the GraphicsInit function this hack should work. Basically my code just pushes everything possible onto their relivant stacks then pops the values off again when needed. I actually push more data than is needed but ,as you say, with it still being in development I thought it was wise to err on the side of caution.
Again thanks for supporting the project. I agree that your code is a very robus solution and was not particularly talking about your "hack". It would be nice if simonh added your code to the official version or perhaps even your integrated version. I really respect simon's decision to keep thing simple, but IMHO Max2D is really needed and can't be substituted with sprites as he suggests. I really wish that he would change his mind on this one! ;)
Hi Oddball. Is is possible to modify this code to work with other 3d mods, such as Leadwerks or Blitz3dSDK?
Hi Oddball. Is is possible to modify this code to work with other 3d mods, such as Leadwerks or Blitz3dSDK?
The Blitz3DSDK use DirectX and this code uses OpenGL only so they are not compatible. The Leadwerks engine I believe is written in OpenGL so absolutely. However nobody except the author knows exactly what's going on inside the engine so he'd have to write his own version of the code. The code it self is fairly generic and it's a modified version of the code from my own engine, but it did need tweaking to work with miniB3D and so I'd expect the same to be true for the Leadwerks engine.