• Welcome to Dark Bit Factory & Gravity.
 

Bmax OGL framebuffer 2d Metaballs[BMAX]

Started by TinDragon, March 05, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

TinDragon

Been along time since I posted something, havent been coding much lately but decided to have a dig into some code today and coverted a routine from my FB experiments into bmax with the ogl ptc like frame buffer lib i made. Anyway here's the code.

Lib incude first  ;)

'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer Lib By Joncom2000
'
' Based on code by Jim Shaw & Rbaz, Inspired by TinyPTC by Gaffer.
'
'-----------------------------------------------------------------------------------------------------

'----------------------------------------------------------------------------------------------
' Globals used by the lib to store screen size
'----------------------------------------------------------------------------------------------
Global g_w:Int
Global g_h:Int
'-----------------------------------------------------------------------------------------------------
' Place Functions after this
'-----------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------
' NAME : ptc_open()
' PURPOSE : This function opens the display
' INPUTS : Title, width and height for screen Also hertz
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_open(Title$,width=640,height=480,Hertz=60)
'Set globals
g_w=width
g_h=height
'open screen
GLGraphics(g_w,g_h,32,Hertz,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER)
AppTitle$=Title$
glViewport(0, 0, g_w,g_h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,g_w,g_h,0,0,1)
glPixelZoom(1,-1)
glRasterPos2i(0,0)
End Function
'------------------------------------------------------------------------
' NAME : ptc_update()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_update(pBuffer:Byte Ptr)
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
Flip
End Function
'------------------------------------------------------------------------
' NAME : ptc_draw()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_draw(pBuffer:Byte Ptr)
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
End Function
'------------------------------------------------------------------------
' NAME : ptc_close()
' PURPOSE : This function closes the display
' INPUTS : none
' RETURNS : update
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_close()
' Doesnt seem needed but I might add cleanup code at some point if needed
End Function


And the Metaballs code  :)


'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer
' Metaballs example
' By Joncom2000
'
'
'-----------------------------------------------------------------------------------------------------
Include "bmaxptc.bmx"
'-------------------------------------------------------------------
' Setup Variables
'-------------------------------------------------------------------
Global XRES=640
Global YRES=480
Global CPalette[256]
Global table:Int[YRES,XRES]
Global alfa,pixel
Global x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6
Global halfx=XRES/2
Global halfy=YRES/2
Global pixels:Int[640*480]
'-------------------------------------------------------------------
' Setup Palette
'-------------------------------------------------------------------
For i=0 To 63
CPalette[i]= ( 0 Shl 16 ) + ( 0 Shl 8 ) + (i*4)
Next
For i=0 To 127
CPalette[i+64]= ( 0 Shl 16 ) + ( ((i/2)*4) Shl 8 ) + (63*4)
Next
For i=0 To 63
CPalette[i+192]= ( (i*4) Shl 16 ) + ( (63*4) Shl 8 ) + (63*4)
Next
'-------------------------------------------------------------------
' Setup color table
'-------------------------------------------------------------------
For y=0 To YRES-1
For x=0 To XRES-1
If (x=0 And y=0) Then
table[y,x]=255
Else
table[y,x]=(9000.0 / (Sqr(x*x + y*y) * 7.0))
EndIf
Next
Next
'-------------------------------------------------------------------
' Open Screen
'-------------------------------------------------------------------
ptc_open("Metaballs by J2K",XRES,YRES,60)'last number is refresh rate set to 60 so compatible with lcd's
Repeat   
x1 = 60 * Cos(alfa)    + 30 * Sin(-alfa)  + halfx
y1 = 30 * Cos(-alfa*2) + 60 * Sin(alfa)   + halfy
x2 = 30 * Cos(alfa)    + 60 * Sin(alfa*2) + halfx
    y2 = 60 * Cos(alfa)    + 30 * Sin(alfa)   + halfy
    x3 = 45 * Cos(-alfa)   + 45 * Sin(alfa)   + halfx
    y3 = 45 * Cos(alfa*2)  + 45 * Sin(-alfa)  + halfy
    x4 = 75 * Cos(alfa)    + 15 * Sin(alfa*2) + halfx
    y4 = 15 * Cos(-alfa)   + 75 * Sin(alfa*2) + halfy
    x5 = 35 * Cos(alfa)    + 10 * Sin(alfa)   + halfx
    y5 = 10 * Cos(alfa*2)  + 35 * Sin(-alfa)  + halfy
    x6 = 40 * Cos(-alfa)   + 30 * Sin(alfa*2) + halfx
    y6 = 40 * Cos(alfa)    + 10 * Sin(alfa)   + halfy
alfa=alfa+3

For y=0 To YRES-1
For x=0 To XRES-1
pixel=table[Abs(y1-y),Abs(x1-x)]+table[Abs(y2-y),Abs(x2-x)]+table[Abs(y3-y),Abs(x3-x)]+table[Abs(y4-y),Abs(x4-x)]+table[Abs(y5-y),Abs(x5-x)]+table[Abs(y6-y),Abs(x6-x)]
If pixel>255 Then pixel=255
TTDPlotRGB(x,y,CPalette[pixel])
Next
Next
ptc_update(Varptr(Pixels[0]))
Until KeyDown(KEY_ESCAPE)
End
'-------------------------------------------------------------------
' End Test
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' NAME : TTDPlotRGB()
' PURPOSE : This function plots a pixel in RGB color
' INPUTS : x,y position and RGB.
' RETURNS : nothing.
'-------------------------------------------------------------------
Function TTDPlotRGB(x,y,PlotColor)
    If (x>-1) And (x<XRES) And (y>-1) And (y<YRES) Then
        pixels[(y*XRES+x)] = PlotColor     
    End If
End Function


Hope you like it, I still havent solved the framework issue so exe's are still rather bloated but it works :)

Shockwave

Eek! Looks cool, need exe though mate :) By the way, it's very nice to see you here again Jon  :)  :hi: back.
Shockwave ^ Codigos
Challenge Trophies Won:

TinDragon

At present I cant get the exe compressed and packed below 440kb, it's realy anoying as the framework system is meant to remove un-used modules from your code but for some reason it fails to actually move when i use a framework. Its been such a long time since i messed about with any coding let alone bmax that I am sure theres a simple solution so if anyone else can find it feel free to post an exe and how you got it working :)

:inspired:

zawran

Hi Jon, great to see you around. I have been trying to figure out, why this doesn't work when using framework, but I just can't figure it out. Only conclusion that I have reached so far, is that it has to do with the glDrawPixels command. The reason I have come to this conclusion is that if you replace it with other opengl code like a triangle that rotates, then it works just fine with framework, but nothing is drawn from the pixels array.

Clyde

Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

TinDragon

Thats a very interesting point there Zawran, it does seem to be the gldrawpixels command causing the problem, other gl commands work fine, maybe I can bodge a work around. Will have to have a dig thru some more of the code in the bmax modules see if I can spot anything that might be causing it. On the plus side at least I coded something, been so long I had to keep looking up commands  :2funny:

zawran

Quotebeen so long I had to keep looking up commands

Hehe, I know how that feels. I was having a bit of problems when I started on those scrollers I did for the last competition, but its coming back really fast now. :)

JumpMan

@TinDragon
you should use Superstrict when coding. it helps find syntax errors. I was trying to convert your  code to superstrict but I don't know muck about OGL so I stopped. One thing though, blitz max don't seem to recognize what "GL_BGRA_EXT" means.

One of this days I will give myself time to learn OGL.

zawran

When I compile it, bmax doesn't complain about "GL_BGRA_EXT", it should be a const defined in the opengl module.

I have taken the liberty to make the changes needed to get it to compile using superstrict and here follows the two sources:


'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer Lib By Joncom2000
'
' Based on code by Jim Shaw & Rbaz, Inspired by TinyPTC by Gaffer.
'
'-----------------------------------------------------------------------------------------------------

'----------------------------------------------------------------------------------------------
' Globals used by the lib to store screen size
'----------------------------------------------------------------------------------------------
Global g_w:Int
Global g_h:Int
'-----------------------------------------------------------------------------------------------------
' Place Functions after this
'-----------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------
' NAME : ptc_open()
' PURPOSE : This function opens the display
' INPUTS : Title, width and height for screen Also hertz
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_open:Int(title:String,width:Int=640,height:Int=480,hertz:Int=60,full:Int=0)
'Set globals
g_w=width
g_h=height
AppTitle=Title
'open screen
If full = 0 Then
GLGraphics(g_w,g_h,32,Hertz,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER)
Else
GLGraphics(g_w,g_h,0,Hertz,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER)
End If

glViewport(0, 0, g_w,g_h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,g_w,g_h,0,0,1)
glPixelZoom(1,-1)
glRasterPos2i(0,0)
End Function
'------------------------------------------------------------------------
' NAME : ptc_update()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_update(pBuffer:Byte Ptr)
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
Flip
End Function
'------------------------------------------------------------------------
' NAME : ptc_draw()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_draw(pBuffer:Byte Ptr)
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
End Function
'------------------------------------------------------------------------
' NAME : ptc_close()
' PURPOSE : This function closes the display
' INPUTS : none
' RETURNS : update
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_close()
' Doesnt seem needed but I might add cleanup code at some point if needed
End Function



'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer
' Metaballs example
' By Joncom2000
'
'
'-----------------------------------------------------------------------------------------------------
SuperStrict
Include "bmaxptc.bmx"
'-------------------------------------------------------------------
' Setup Variables
'-------------------------------------------------------------------
Global XRES:Int=640
Global YRES:Int=480
Global CPalette:Int[256]
Global table:Int[YRES,XRES]
Global alfa:Int,pixel:Int
Global x1:Int,y1:Int,x2:Int,y2:Int,x3:Int,y3:Int,x4:Int,y4:Int,x5:Int,y5:Int,x6:Int,y6:Int
Global halfx:Int=XRES/2
Global halfy:Int=YRES/2
Global pixels:Int[640*480]
'-------------------------------------------------------------------
' Setup Palette
'-------------------------------------------------------------------
Local i:Int
For i=0 To 63
CPalette[i]= ( 0 Shl 16 ) + ( 0 Shl 8 ) + (i*4)
Next
For i=0 To 127
CPalette[i+64]= ( 0 Shl 16 ) + ( ((i/2)*4) Shl 8 ) + (63*4)
Next
For i=0 To 63
CPalette[i+192]= ( (i*4) Shl 16 ) + ( (63*4) Shl 8 ) + (63*4)
Next
'-------------------------------------------------------------------
' Setup color table
'-------------------------------------------------------------------
Local x:Int,y:Int
For y=0 To YRES-1
For x=0 To XRES-1
If (x=0 And y=0) Then
table[y,x]=255
Else
table[y,x]=(9000.0 / (Sqr(x*x + y*y) * 7.0))
EndIf
Next
Next
'-------------------------------------------------------------------
' Open Screen
'-------------------------------------------------------------------
ptc_open("Metaballs by J2K",XRES,YRES,60,1)'last number is refresh rate set to 60 so compatible with lcd's
Repeat   
x1 = 60 * Cos(alfa)    + 30 * Sin(-alfa)  + halfx
y1 = 30 * Cos(-alfa*2) + 60 * Sin(alfa)   + halfy
x2 = 30 * Cos(alfa)    + 60 * Sin(alfa*2) + halfx
    y2 = 60 * Cos(alfa)    + 30 * Sin(alfa)   + halfy
    x3 = 45 * Cos(-alfa)   + 45 * Sin(alfa)   + halfx
    y3 = 45 * Cos(alfa*2)  + 45 * Sin(-alfa)  + halfy
    x4 = 75 * Cos(alfa)    + 15 * Sin(alfa*2) + halfx
    y4 = 15 * Cos(-alfa)   + 75 * Sin(alfa*2) + halfy
    x5 = 35 * Cos(alfa)    + 10 * Sin(alfa)   + halfx
    y5 = 10 * Cos(alfa*2)  + 35 * Sin(-alfa)  + halfy
    x6 = 40 * Cos(-alfa)   + 30 * Sin(alfa*2) + halfx
    y6 = 40 * Cos(alfa)    + 10 * Sin(alfa)   + halfy
alfa=alfa+3

For y=0 To YRES-1
For x=0 To XRES-1
pixel=table[Abs(y1-y),Abs(x1-x)]+table[Abs(y2-y),Abs(x2-x)]+table[Abs(y3-y),Abs(x3-x)]+table[Abs(y4-y),Abs(x4-x)]+table[Abs(y5-y),Abs(x5-x)]+table[Abs(y6-y),Abs(x6-x)]
If pixel>255 Then pixel=255
TTDPlotRGB(x,y,CPalette[pixel])
Next
Next
ptc_update(Varptr(Pixels[0]))
Until KeyDown(KEY_ESCAPE)
End
'-------------------------------------------------------------------
' End Test
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' NAME : TTDPlotRGB()
' PURPOSE : This function plots a pixel in RGB color
' INPUTS : x,y position and RGB.
' RETURNS : nothing.
'-------------------------------------------------------------------
Function TTDPlotRGB:Int(x:Int,y:Int,PlotColor:Int)
    If (x>-1) And (x<XRES) And (y>-1) And (y<YRES) Then
        pixels[(y*XRES+x)] = PlotColor     
    End If
End Function


I haven't had more time tonight to look into why gldrawpixels won't work when using framework, I might be able to set some time aside tomorrow to give it another look.

QuoteOne of this days I will give myself time to learn OGL.

If I can find time, I might write up some kind of tutorial on how to get started with bmax and raw opengl.

TinDragon

I have never used superstrict in bmax, probably should alwasy use it but never had such a thing in the older blitz's which i used alot before bmax came about. Anyway thanks Zawran for changing it and I will make use of the changes from now on would be good coding practice for me :)

TinDragon

woot Found out that GL_BGRA_EXT isnt defines in the modules imported when using a framework. Now if we chane the line
glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
to
glDrawPixels(g_w,g_h,GL_RGBA,GL_UNSIGNED_BYTE,pBuffer)

It works, the main difference is now the color order in the arrays is Red,Green,Blue,Alpha Which I think is more like old blitz instead of Blue,Green,Red,Alpha which I only originally used to keep it like the DBF lib version Jim originally made. So as long as you rework the colors it works and we can use a framework. The other option is we find out the value for GL_BGRA_EXT and define it ourselves but this could cause problems if later versions of bmax define it at some point
.

Anyway the Fixed lib is as follows

'-----------------------------------------------------------------------------------------------------
'
' The Tin Dragons - OpenGL Frame Buffer Lib By Joncom2000
'
' Based on code by Jim Shaw & Rbaz, Inspired by TinyPTC by Gaffer.
'
'-----------------------------------------------------------------------------------------------------

'----------------------------------------------------------------------------------------------
' Globals used by the lib to store screen size
'----------------------------------------------------------------------------------------------
Global g_w:Int
Global g_h:Int
'-----------------------------------------------------------------------------------------------------
' Place Functions after this
'-----------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------
' NAME : ptc_open()
' PURPOSE : This function opens the display
' INPUTS : Title, width and height for screen Also hertz
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_open:Int(title:String,width:Int=640,height:Int=480,hertz:Int=60,full:Int=0)
'Set globals
g_w=width
g_h=height
AppTitle=Title
'open screen
If full = 0 Then
GLGraphics(g_w,g_h,32,Hertz,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER)
Else
GLGraphics(g_w,g_h,0,Hertz,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER)
End If

glViewport(0, 0, g_w,g_h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,g_w,g_h,0,0,1)
glPixelZoom(1,-1)
glRasterPos2i(0,0)
End Function
'------------------------------------------------------------------------
' NAME : ptc_update()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_update(pBuffer:Byte Ptr)
'glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
glDrawPixels(g_w,g_h,GL_RGBA,GL_UNSIGNED_BYTE,pBuffer)
Flip
End Function
'------------------------------------------------------------------------
' NAME : ptc_draw()
' PURPOSE : This function updates the display
' INPUTS : pixel buffer pointer
' RETURNS : nothing
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_draw(pBuffer:Byte Ptr)
'glDrawPixels(g_w,g_h,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pBuffer)
glDrawPixels(g_w,g_h,GL_RGBA,GL_UNSIGNED_BYTE,pBuffer)
End Function
'------------------------------------------------------------------------
' NAME : ptc_close()
' PURPOSE : This function closes the display
' INPUTS : none
' RETURNS : update
' NOTES : N/A
'------------------------------------------------------------------------
Function ptc_close()
' Doesnt seem needed but I might add cleanup code at some point if needed
End Function


I left the old lines there so you can see the changes, note the ptc_draw function doesnt do a flip, this means other ogl drawing could be done to the back buffer before flipping so additional effects could be done.

Oh and now I can attach the exe for none bmax users :)

zawran

#11
I have just tested, and using Const GL_BGRA_EXT:int=$80E1 it works when using the framework and import functionality. I still don't know why they didn't include that const in the official module, but at least now we know why it didn't work. :)

Superstrict might seem like a little extra work to begin with, but once you start using it, you will be glad because it catches a lot of the mistakes in the code, which might not prevent your code from compiling, but might prevent it from running like intended.

TinDragon

Think I will go with the rgba version, for some reason that seems more logical to me but what I dont understand is why the code worked if we didnt use a framework, makes me think that the bgra value is defined somewhere in the blitz modules but not in the main opengl one. Still we have a fix for it and now I can make some pixel stuff should I find the time and inspiration. Hope fully this will help others using bmax trying to do this kind of stuff as well  O0

Clyde

Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

zawran

Yes, only explaination for it working when not using framework, is that its defined somewhere else, but I think that your are right in that RGBA seems more logical, so I would stick with that also.

Shockwave

It looks nice, good colours :) I like it!
Shockwave ^ Codigos
Challenge Trophies Won:

TinDragon

Thanks Shockwave, I am glad that I was able to upload an exe for the none bmax users to see it working. If I get some more time to code I will have a look at converting a few of the FB examples and stuff on the forum over to get the grey matter ticking over and maybe help some more people get into pixel pushing with bmax :)

:cheers: