PDA

View Full Version : Fractal Fern



Michael Clease
03-08-2007, 23:41
Heres my first go at a fractal Fern.

wait for it or just make %number smaller.

i was trying to work out how to pass workload off to an external .dll using PB but i dont see how to pass arrays to it.

Feel free to fix it or should I say please fix it so I can see how its done.

thx

Mike




FUNCTION EXEC_MakeFern(BYREF PTS() AS DOUBLE) AS EXT
LOCAL imax AS EXT
LOCAL Loops AS DWORD
LOCAL ColCount AS DWORD
LOCAL counter AS DWORD
LOCAL e1, e2, e3, e4, f1, f2, f3, f4, x, y, xn, yn, z AS DOUBLE
e1 = 0.5
e2 = 0.57
e3 = 0.408
e4 = 0.1075
f1 = 0
f2 = -0.036
f3 = 0.0893
f4 = 0.27
x = e1
y = 0
colcount = 1
Counter = 1

IF thinBasic_CheckOpenParens() THEN
' thinBasic_ParseNumber PTs
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber imax
IF thinBasic_CheckCloseParens() THEN

FOR loops = 1 TO imax
z = RND/1^12
IF z<= 0.02 THEN
xn = e1
yn = 0.27*y+f1
ELSEIF z<= 0.17 THEN
xn = -0.139*x+0.263*y+e2
yn = 0.246*x+0.224*y+f2
ELSEIF z<= 0.3 THEN
xn = 0.17*x-0.215*y+e3
yn = 0.222*x+0.176*y+f3
ELSE
xn = 0.781*x+0.034*y+e4
yn = -0.032*x+0.739*y+f4
END IF
pts(counter) = xn
pts(counter+1) = yn
Counter = Counter + 2
x = xn
y = yn
NEXT

END IF
END IF
END IF

END FUNCTION

kryton9
04-08-2007, 01:25
looks nice, and was fast too.

You need to look at the SDK folder in the thinBasic folder for a supported language. You can also look in the SDK section of the forums for more info.
But basically take one of those files, rename it to your own dll and make changes to fit your function. That would be the easiest way to go about it.

Take a look and if you need more information ask away.

Petr Schreiber
04-08-2007, 10:27
Nice sample !

I think you have to check out thinBasic_Array* functions in thinCore.inc. It seems your function would get array name in string as parameter. Then you can retrieve pointer (thinBasic_ArrayGetPtr) for it and so it is easy to fill.
To make it general ( not DOUBLE and fixed size only ) you should use thinBasic_ArrayGetInfo with %Array_ElementsCount, %Array_ElementsType and according to type make a steps with pointer. Info on "steps" ( size of datatype ) you can get in PB manual.


Bye,
Petr

ErosOlmi
06-08-2007, 11:32
Abraxas,

sorry I didn't reply before. I was out for 2 days.
I will check your interface this evening when back to home and will get a reply on interfacing with script arrays.
In any case your example is so nice and fast already. Using an interface module it will fly I think.

Ciao
Eros

Michael Clease
06-08-2007, 13:15
thanks.

Ive found a few different examples of how to do a fern will try them tonight if I get time.

Mike

ErosOlmi
06-08-2007, 22:32
Abraxas,

here it is an example using a module.
Functions to get direct access to script variable are: thinBasic_VariableParsePtr(...), thinBasic_DirectPtrToDataPtr(...)
thinBasic_VariableParsePtr(...) get back a pointer to the internal structure used by thinBasic to store variable info
thinBasic_DirectPtrToDataPtr(...) get back a pointer to variable data
Having the pointer to the data, the data type (here assumed they are double) and the number of elements, you can create a dummy array overimposing it to script variable data memory are.

Speed has improved but still there is the loop to create TBGL model.

Let me know is all is clear for you.

Ciao
Eros

kryton9
06-08-2007, 23:02
There is one command that will really be useful and solve a lot of problems...
thinBasic_DirectPtrToTheOracle("ProblemToSolve")

:)

Michael Clease
07-08-2007, 00:09
thanks for the help doesnt seem to make that much difference to the speed though.

perhaps you could add something to the SDK section in the help file or where ever you feel is correct.

thanks

Mike

Petr Schreiber
07-08-2007, 09:10
Abraxas !,

we dont need no oracle, even no DLL optimization !

Do you want to know main bottle neck ? What about assigning green color to 10000 vertices 5000 times ;)
It is just one line, and so much troubles, you probably forgot it there.

Your version:


FUNCTION CreateLevelModel( modelSlot as long)
local Loops AS DWORD
tbgl_m15SetModelVertexcount( modelSlot, %Number*2 ) '
tbgl_m15SetVertexRGB( modelSlot, 1, %Number*2, 255, 255, 255) ' -- Assigns white color to all vertices
local vertexID as long ' -- To temporary store vertex index
For Loops = 1 to %Number*2 step 2
incr vertexID ' -- Increases vertex index to write in this cycle
'TBGL_m15SetVertexTexXY( modelSlot, vertexID, 0, 0 ) ' -- UV coords
tbgl_m15SetVertexRGB( modelSlot, 1, %Number*2, 0,255,0) ' -- Assigns white color to all vertices
TBGL_m15SetVertexXYZ ( modelSlot, vertexID, pts(Loops),pts(Loops+1), pts(Loops+2) ) ' bottom left-- XYZ coords
Next
TBGL_m15SetVertexPStop( modelSlot, vertexID+1, 1 ) ' End of polygon

END FUNCTION


Tuned up version


FUNCTION CreateLevelModel( modelSlot as long)
local Loops AS DWORD
local vertexID as long ' -- To temporary store vertex index

tbgl_m15SetModelVertexcount( modelSlot, %Number*2 ) '

tbgl_m15SetVertexRGB( modelSlot, 1, %Number*2, 0, 255, 0) ' -- Assigns GREEN color to all vertices

For Loops = 1 to %Number*2 step 2
incr vertexID ' -- Increases vertex index to write in this cycle
TBGL_m15SetVertexXYZ ( modelSlot, vertexID, pts(Loops),pts(Loops+1), pts(Loops+2) ) ' bottom left-- XYZ coords
Next

TBGL_m15SetVertexPStop( modelSlot, vertexID+1, 1 ) ' End of polygon

END FUNCTION


Tuned version does absolutely no delay on my PC, while original takes 5 seconds :)


Bye,
Petr

ErosOlmi
07-08-2007, 09:29
Zero delay here too. Wow!

Michael Clease
07-08-2007, 10:14
Petr is was there because I was going to workout how to shade it from an array but the didnt have time and forgot i left it in.

thanks

mike

Petr Schreiber
07-08-2007, 10:21
Hi,

sure, I realize lot of "stubs" when now cleaning up thinEdge too ;D

Only problem was that you were setting the color for interval of vertices, not just for current one.
Try this mod:
( does not look good, but shows no performance impact even for coloring each vertex separately )


FUNCTION CreateLevelModel( modelSlot as long)
local Loops AS DWORD
local vertexID as long ' -- To temporary store vertex index

tbgl_m15SetModelVertexcount( modelSlot, %Number*2 ) '

For Loops = 1 to %Number*2 step 2
incr vertexID ' -- Increases vertex index to write in this cycle
tbgl_m15SetVertexRGB( modelSlot, vertexID, rnd(0,255), rnd(0,128), 0) ' -- Assigns random color to THIS vertex
TBGL_m15SetVertexXYZ ( modelSlot, vertexID, pts(Loops),pts(Loops+1), pts(Loops+2) ) ' bottom left-- XYZ coords
Next

TBGL_m15SetVertexPStop( modelSlot, vertexID+1, 1 ) ' End of polygon

END FUNCTION


Bye,
Petr

Michael Clease
07-08-2007, 13:47
Thanks again Petr

I was assigning a colour to a vertice that didnt exist (10000) the total number of dots is %Number and I was setting %Number*2 ::)

Your fix is correct, thats a my fault should go to bed before 1.30 am everynight and double check my code.

Thx

Mike

ErosOlmi
07-08-2007, 13:57
thanks for the help doesnt seem to make that much difference to the speed though.

perhaps you could add something to the SDK section in the help file or where ever you feel is correct.

thanks

Mike


:-[ Yes, SDK documentation is very bad, I recognize. It was done at the very beginning. I tried to add comments into thinBasic.inc include file used to create modules, but it can be time to improve that help in official help file.

In my reply on using a Power Basic module (I didn't know you had Power Basic compiler :D ) I just wanted to show how to get a pointer to a thinBasic variable and variable data in order to directly get/set data value.

In any case, thanks a lot for you script examples. They are a great opportunity for us to find bugs, integrate help or explain something more about thinBasic potentialities.

Ciao
Eros

kryton9
08-08-2007, 02:44
Ok, so if Petr is the Oracle, does that make Eros the architect (in case you think I lost it all, I am referring in fun to The Matrix Movies :) )

ErosOlmi
08-08-2007, 02:46
;D
I'm not sure if I'm the architet but Petr is for sure the Oracle.
:P

Petr Schreiber
08-08-2007, 09:05
That was good Kent :D

... and please: Don't worry about the vase ;)
I am going to bake some cookies meanwhile ;D


Bye,
Petr

kryton9
08-08-2007, 11:58
he he he :)