thinBASIC TBGL Website
by Petr Schreiber
I know being a gardener was one of your greatest secret wishes, but you always been shy to leave computer alone and get lost from within pleasant warm breeze of you graphic card fan.
Don't worry, your time has come. Growing trees! Right on your PC! And with less than 200 lines of code...
Problems with trees
Creating trees as models for PC game was always very tricky. In early games you could see use of bilboarding exclusively, if anything at all.
As time passed, first real 3D trees started to appear. They were usually hand modeled, and sometimes looked quite strange.
Modeling a plant manualy is tough task even for advanced modelers, thats sure. Some programs provide tools for generating vegetations, but target is usually high poly model for movies and high quality stills.
But you want tree usable in real time application, right?
Growing a plant from code
One of approaches which could occur to you is to take random numbers generator and let the branches grow as they ( or better random number generators ) want.
Don't do it, it will be usable in very few cases.
Instead of torturing your friend with modeling a tree, try to get some algoritm. Get it from its author - nature :) At least this is one of authors which does not scare you with patents on technology.
To see how tree branches grow, it is good to watch trees in winter - leafs will not cover the topology.
' Recursive function to create branched branches :) SUB DrawBranch( startRadius AS SINGLE, startlength AS SINGLE, _
levels AS LONG, openangle AS SINGLE, twist AS SINGLE ) LOCAL i AS LONG LOCAL length AS SINGLE LOCAL radiusS AS SINGLE LOCAL radiusE AS SINGLE IF levels < 3 THEN TBGL_BINDTEXTURE 2 ' Hot Alpha handling stuff TBGL_USEALPHATEST 1 TBGL_ALPHAFUNC %TBGL_GREATER, 0.5 FOR i = 1 TO 3 TBGL_ROTATE 120,1,0,1 TBGL_CALLLIST 2' 0.2,0.4,0.1 NEXT TBGL_BINDTEXTURE 1 TBGL_USEALPHATEST 0 IF levels = 0 THEN EXIT SUB END IF radiusS = startRadius radiusE = radiusS - startRadius/levels length = startlength TBGL_PUSHMATRIX FOR i = 1 TO levels ' possible optional optimization
' tbgl_setPrimitiveQuality levels/1.5 TBGL_CYLINDER radiusS , radiusE, length TBGL_TRANSLATE 0, length, 0 TBGL_ROTATE openangle,0,0,1 TBGL_ROTATE twist,0,1,0 TBGL_PUSHMATRIX TBGL_ROTATE -openangle*2,0,0,1 IF i = 1 THEN DrawBranch(radiusE, length, levels-1, -openangle, twist) ELSE DrawBranch(radiusE, length, levels-i, openangle, twist) END IF TBGL_POPMATRIX radiusS = radiusE radiusE = radiusS - startRadius/levels length = length - startlength/levels NEXT TBGL_POPMATRIX END SUB
TBGL_USEALPHATEST 1 TBGL_ALPHAFUNC %TBGL_GREATER, 0.5
|© Petr Schreiber 2012|