Here is the code.
'$include: 'GL/gl.bi'
'$include: 'GL/glu.bi'
'' Setup our booleans
const FALSE = 0
const TRUE = not FALSE
#include once "bmpload.bi"
declare function LoadGLTextures() as integer
dim shared texture(9) as GLuint '' Storage For One Texture ( NEW )
dim xrot as single '' X Rotation ( NEW )
dim yrot as single '' Y Rotation ( NEW )
dim zrot as single '' Z Rotation ( NEW )
TYPE vector
x AS SINGLE
y AS SINGLE
z AS SINGLE
END TYPE
DIM p1 AS vector
DIM p2 as vector
DIM p3 AS vector
DIM SHARED norm(2) AS SINGLE
FUNCTION getnorm(p1 AS vector,p2 AS vector,p3 AS vector)
v1x# = p1.x - p2.x
v1y# = p1.y - p2.y
v1z# = p1.z - p2.z
v2x# = p2.x - p3.x
v2y# = p2.y - p3.y
v2z# = p2.z - p3.z
nx# = (v1y# * v2z#) - (v1z# * v2y#)
ny# = (v1z# * v2x#) - (v1x# * v2z#)
nz# = (v1x# * v2y#) - (v1y# * v2x#)
lenght# = SQR((nx# * nx#) + (ny# * ny#) + (nz# * nz#))
norm(0) = nx# / lenght#
norm(1) = ny# / lenght#
norm(2) = nz# / lenght#
END FUNCTION
screenwidth = 640
screenheight = 480
SCREEN 18,32,,2
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
gluPerspective(60,screenwidth/screenheight,1,1024)
glMatrixMode (GL_MODELVIEW)
nopts = 2046
dim which(nopts)
dim bt#(nopts)
dim xtemp(nopts)
dim ytemp(nopts)
dim ztemp(nopts)
dim xtemp2(nopts)
dim ytemp2(nopts)
dim mag#(nopts)
dim size#(nopts)
dim ang#(nopts)
dim perb#(nopts)
angle = 0
mult = 1
add = 1
bt#(0) = 90
bt#(index + mult + position) = bt#(0) + angle
bt#(index + mult + position + add) = bt#(0) - angle
'print bt(0)
mult = mult * 2
num2 = 2
a = 1
for j = 0 to 9
num = mult - 1
for i = num to num2
if i < mult then position = 0
if i >= mult then position = position + 1
bt#(i + mult + position) = bt#(a) + angle
bt#(i + mult + position + add) = bt#(a) - angle
'print bt(i);" ";
a = a + 1
next i
print
old = old + mult
mult = mult * 2
num2 = old + mult
next j
'print
Dim As Single LightPos(3) => {-100,100, 1024, 0 }
Dim As Single Ambient(3) => { .7, .7, .7, 1}
glEnable(GL_LIGHTING) ' Turn on OpenGL Lighting
glEnable(GL_LIGHT0) ' Light Source 0 (0-7)
glLightfv(GL_LIGHT0,GL_POSITION,@LightPos(0)) ' Light Position
glLightfv(GL_LIGHT0,GL_AMBIENT,@Ambient(0)) ' Ambient Light Source (Overall Scene)
'glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
'glEnable (GL_BLEND)
'' Jump To Texture Loading Routine
if (not LoadGLTextures()) then
end 1 '' If Texture Didn't Load Quit
end if
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glClearDepth(1)
glEnable(GL_TEXTURE_2D)
'glEnable(GL_CULL_FACE)
glClearColor(0,0,1,1)
do
' Position camera
glLoadIdentity ()
glRotatef (-camang#, 0, 0, 1)
glTranslatef (0,-256,-1024)
randomize timer
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glTranslatef(0,0,512)
glRotatef(y#,0,1,0)
gosub math
gosub rendertrunk
gosub renderbranches
flip
y# = y# + 1
loop until inkey$ = chr$(27)
end
math:
dim a#(nopts)
for i = 0 to nopts
xtemp(i) = 0
ytemp(i) = 0
ztemp(i) = 0
which(i) = 0
next i
a = 1
b = 2
c = 0
mult1 = 2
mult2 = 4
pi# = 3.1415926
x1 = 0
y1 = 0
lenght = screenheight / 4
xtemp(0) = x1 + -cos(bt#(0) * pi# / 180) * lenght
ytemp(0) = y1 + sin(bt#(0) * pi# / 180) * lenght
size#(0) = 12
'gosub trunk1
mag#(0) = lenght / 2
'glLineWidth(size#(0))
xscale = size#(0)
'yscale = size#(0)
zscale = size#(0)
'y1 = size#(0)
Z = 0
gosub bulk
which(0) = 1
for j = 0 to 9
for i = a to b
if j > 0 and d = 0 then c = c + 1
if d = 0 then a1# = rnd(1) * 1
if d = 0 then bt#(i) = bt#(c) + (45*a1#):a#(i) = a#(c) + (45*a1#)
if d = 1 then bt#(i) = bt#(i-1) - 45:a#(i) = a#(i-1) -45
if d = 0 then count# = bt#(i) - bt#(c)
if d = 1 then count# = bt#(c) - bt#(i)
'if d = 1 and count1# > count2# then goto trunk2
'gosub trunk2
l# = count# / 90
mag#(i) = mag#(c) * (1-l#)
xtemp(i) = xtemp(c) + -cos(bt#(i) * pi# / 180) * mag#(i)
ytemp(i) = ytemp(c) + sin(bt#(i) * pi# / 180) * mag#(i)
size#(i) = size#(c) * (1-l#)
if d = 0 then s1# = size#(i)
if d = 1 then s2# = size#(i)
if d = 1 and which(c) = 1 and s1# >= s2# then which(i-1) = 1
if d = 1 and which(c) = 1 and s1# < s2# then which(i) = 1
'if d = 1 and which(c) = 1 and s1# = s2# then which(i-1) = 1:which(i) = 1
glLineWidth(size#(i))
glBegin(GL_LINES)
glColor3f(1,1,1)
'glVertex3f(xtemp(c),ytemp(c),0)
'glVertex3f(xtemp(i),ytemp(i),0)
glEnd()
d = d + 1
if d = 2 then d = 0
next i
if j > 0 then mult1 = mult1 * 2
if j > 0 then mult2 = mult2 * 2
a = a + mult1
b = b + mult2
next j
return
rendertrunk:
a = 1
b = 2
c = 0
mult1 = 2
mult2 = 4
for j = 0 to 9
for i = a to b
if j > 0 and d = 0 then c = c + 1
if which(c) = 1 and which(i) = 1 then gosub trunk2
'gosub trunk2
d = d + 1
if d = 2 then d = 0
next i
if j > 0 then mult1 = mult1 * 2
if j > 0 then mult2 = mult2 * 2
a = a + mult1
b = b + mult2
next j
return
trunk1:
'glRotatef (-90, 1, 0, 0)
'gluCylinder(quadric1,size#(c),size#(c),lenght,16,16)
'glRotatef ( 90, 1, 0, 0)
return
trunk2:
'glLineWidth(size#(i))
'glBegin(GL_LINES)
'glColor3f(1,1,1)
'glVertex3f(xtemp(c),ytemp(c),0)
'glVertex3f(xtemp(i),ytemp(i),0)
xscale = size#(c)
'yscale = 'mag#(c)
zscale = size#(c)
gosub bulk2
'glEnd()
'glTranslatef(xtemp(c),ytemp(c),0)
'glRotatef (-90, 1, 0, 0)
'glRotatef ( a#(i), 0, 1 ,0)
'glColor3f(0,0,1)
'gluCylinder(quadric1,size#(c),size#(c),mag#(c),16,16)
'glRotatef (-a#(i), 0, 1, 0)
'glRotatef ( 90, 1, 0, 0)
'glTranslatef(-xtemp(c),-ytemp(c),0)
return
renderbranches:
for k = 0 to 1
a = 1
b = 2
c = 0
mult1 = 2
mult2 = 4
for j = 0 to 9
for i = a to b
if j > 0 and d = 0 then c = c + 1
if which(c) = 1 and which(i) = 0 then gosub branches
'if which(c) = 2 and which(i) = 0 then gosub more
if which(c) > 1 then gosub more
'gosub branches
d = d + 1
if d = 2 then d = 0
next i
if j > 0 then mult1 = mult1 * 2
if j > 0 then mult2 = mult2 * 2
a = a + mult1
b = b + mult2
next j
next k
return
branches:
ang#(c) = rnd(1) * 360
x3d1 = xtemp(c) + cos(ang#(c) * pi# / 180) * mag#(c)
y3d1 = ytemp(i)
z3d1 = ztemp(c) + -sin(ang#(c) * pi# / 180) * mag#(c)
xscale = size#(c)
'yscale = 'mag#(c)
zscale = size#(c)
gosub bulk3
glLineWidth(size#(c))
glBegin(GL_LINES)
glColor3f(1,1,1)
'glVertex3f(xtemp(c),ytemp(c),ztemp(c))
'glVertex3f(x3d1,y3d1,z3d1)
glEnd()
ba# = 0'rnd(1) * 360
'glTranslatef(xtemp(c),ytemp(c),0)
'glRotatef (-90, 1, 0, 0)
'glRotatef ( ang#(c)+90, 0, 1 ,0)
'glColor3f(1,1,1)
'gluCylinder(quadric1,size#(i),size#(i),mag#(c),16,16)
'glRotatef ( -ang#(c)-90, 0, 1, 0)
'glRotatef ( 90, 1, 0, 0)
'glTranslatef(-xtemp(c),-ytemp(c),0)
ang#(i) = ang#(c)
xtemp(i) = x3d1
ytemp(i) = y3d1
ztemp(i) = z3d1
which(i) = 2
return
more:
rand1 = int(rnd(1) * 2 ) + 1
if d = 0 and rand1 = 1 then ang#(i) = ang#(c) + a#(c)'(a#(c) / 2)
if d = 0 and rand1 = 2 then ang#(i) = ang#(c) - a#(c)'(a#(c) / 2)
if d = 1 and rand1 = 1 then ang#(i) = ang#(c) + a#(c)'(a#(c) / 2)
if d = 1 and rand1 = 2 then ang#(i) = ang#(c) - a#(c)'(a#(c) / 2)
'p# = rnd(1) * 1
'perb#(c) = p# * 5
x3d1 = xtemp(c) + cos(ang#(i) * pi# / 180) * mag#(c)
y3d1 = ytemp(i) - perb#(c)
z3d1 = ztemp(c) + -sin(ang#(i) * pi# / 180) * mag#(c)
'perb#(i) = perb#(c)
'gosub bulk2
xscale = size#(c)
'yscale = 'mag#(c)
zscale = size#(c)
gosub bulk3
glLineWidth(size#(c))
glBegin(GL_LINES)
glColor3f(1,1,1)
'glVertex3f(xtemp(c),ytemp(c),ztemp(c))
'glVertex3f(x3d1,y3d1,z3d1)
glEnd()
'ba# = 0'rnd(1) * 360
'glTranslatef(xtemp(c),ytemp(c),ztemp(c))
'glRotatef ( ang#(i)+90, 0, 1 ,0)
'gluCylinder(quadric1,size#(i),size#(i),mag#(c),16,16)
'glRotatef ( -ang#(i)-90, 0, 1, 0)
'glTranslatef(-xtemp(c),-ytemp(c),-ztemp(c))
xtemp(i) = x3d1
ytemp(i) = y3d1
ztemp(i) = z3d1
which(i) = 2
return
bulk:
glBindTexture(GL_TEXTURE_2D, texture(0))
glBegin(GL_QUADS)
p1.x = x1 + xscale
p1.y = y1
p1.z = z - zscale
p2.x = xtemp(0) + xscale
p2.y = ytemp(0)
p2.z = z - zscale
p3.x = xtemp(0) + xscale
p3.y = ytemp(0)
p3.z = z + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' right face
glTexCoord2f(0,0)
glVertex3f(x1+xscale,y1,z-zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(0)+xscale,ytemp(0),z-zscale)
glTexCoord2f(1,1)
glVertex3f(xtemP(0)+xscale,ytemp(0),z+zscale)
glTexCoord2f(1,0)
glVertex3f(x1+xscale,y1,z+zscale)
p1.x = x1 - xscale
p1.y = y1
p1.z = z - zscale
p2.x = xtemp(0) - xscale
p2.y = ytemp(0)
p2.z = z - zscale
p3.x = xtemp(0) - xscale
p3.y = ytemp(0)
p3.z = z + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' left face
glTexCoord2f(0,0)
glVertex3f(x1-xscale,y1,z-zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(0)-xscale,ytemp(0),z-zscale)
glTexCoord2f(1,1)
glVertex3f(xtemP(0)-xscale,ytemp(0),z+zscale)
glTexCoord2f(1,0)
glVertex3f(x1-xscale,y1,z+zscale)
p1.x = x1 - xscale
p1.y = y1
p1.z = z - zscale
p2.x = x1 + xscale
p2.y = y1
p2.z = z - zscale
p3.x = xtemp(0) + xscale
p3.y = ytemp(0)
p3.z = z - zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' back
glTexCoord2f(0,0)
glVertex3f(x1-xscale,y1,z-zscale)
glTexCoord2f(1,0)
glVertex3f(x1+xscale,y1,z-zscale)
glTexCoord2f(1,1)
glVertex3f(xtemp(0)+xscale,ytemp(0),z-zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(0)-xscale,ytemp(0),z-zscale)
p1.x = x1 - xscale
p1.y = y1
p1.z = z + zscale
p2.x = x1 + xscale
p2.y = y1
p2.z = z + zscale
p3.x = xtemp(0) + xscale
p3.y = ytemp(0)
p3.z = z + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' front
glTexCoord2f(0,0)
glVertex3f(x1-xscale,y1,z+zscale)
glTexCoord2f(1,0)
glVertex3f(x1+xscale,y1,z+zscale)
glTexCoord2f(1,1)
glVertex3f(xtemp(0)+xscale,ytemp(0),z+zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(0)-xscale,ytemp(0),z+zscale)
glEnd()
return
bulk2:
glBindTexture(GL_TEXTURE_2D, texture(0))
glBegin(GL_QUADS)
p1.x = xtemp(c) + xscale
p1.y = ytemp(c)
p1.z = z - zscale
p2.x = xtemp(i) + xscale
p2.y = ytemp(i)
p2.z = z - zscale
p3.x = xtemp(i) + xscale
p3.y = ytemp(i)
p3.z = z + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' right face
glTexCoord2f(0,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),z-zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(i)+xscale,ytemp(i),z-zscale)
glTexCoord2f(1,1)
glVertex3f(xtemP(i)+xscale,ytemp(i),z+zscale)
glTexCoord2f(1,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),z+zscale)
p1.x = xtemp(c) - xscale
p1.y = ytemp(c)
p1.z = z - zscale
p2.x = xtemp(i) - xscale
p2.y = ytemp(i)
p2.z = z - zscale
p3.x = xtemp(i) - xscale
p3.y = ytemp(i)
p3.z = z + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' left face
glTexCoord2f(0,0)
glVertex3f(xtemp(c)-xscale,ytemp(c),z-zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(i)-xscale,ytemp(i),z-zscale)
glTexCoord2f(1,1)
glVertex3f(xtemP(i)-xscale,ytemp(i),z+zscale)
glTexCoord2f(1,0)
glVertex3f(xtemp(c)-xscale,ytemP(c),z+zscale)
p1.x = xtemp(c) - xscale
p1.y = ytemp(c)
p1.z = z - zscale
p2.x = xtemp(c) + xscale
p2.y = ytemp(c)
p2.z = z - zscale
p3.x = xtemp(i) + xscale
p3.y = ytemp(i)
p3.z = z - zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' back
glTexCoord2f(0,0)
glVertex3f(xtemp(c)-xscale,ytemp(c),z-zscale)
glTexCoord2f(1,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),z-zscale)
glTexCoord2f(1,1)
glVertex3f(xtemp(i)+xscale,ytemp(i),z-zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(i)-xscale,ytemp(i),z-zscale)
p1.x = xtemp(c) - xscale
p1.y = ytemp(c)
p1.z = z + zscale
p2.x = xtemp(c) + xscale
p2.y = ytemp(c)
p2.z = z + zscale
p3.x = xtemp(i) + xscale
p3.y = ytemp(i)
p3.z = z + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' front
glTexCoord2f(0,0)
glVertex3f(xtemp(c)-xscale,ytemp(c),z+zscale)
glTexCoord2f(1,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),z+zscale)
glTexCoord2f(1,1)
glVertex3f(xtemp(i)+xscale,ytemp(i),z+zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(i)-xscale,ytemp(i),z+zscale)
glEnd()
return
bulk3:
glBindTexture(GL_TEXTURE_2D, texture(0))
glBegin(GL_QUADS)
p1.x = xtemp(c) + xscale
p1.y = ytemp(c)
p1.z = ztemp(c) - zscale
p2.x = x3d1 + xscale
p2.y = y3d1
p2.z = z3d1 - zscale
p3.x = x3d1 + xscale
p3.y = y3d1
p3.z = z3d1 + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' right face
glTexCoord2f(0,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),ztemp(c)-zscale)
glTexCoord2f(1,0)
glVertex3f(x3d1+xscale,y3d1,z3d1-zscale)
glTexCoord2f(1,1)
glVertex3f(x3d1+xscale,y3d1,z3d1+zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(c)+xscale,ytemp(c),ztemp(c)+zscale)
p1.x = xtemp(c) - xscale
p1.y = ytemp(c)
p1.z = ztemp(c) - zscale
p2.x = x3d1 - xscale
p2.y = y3d1
p2.z = z3d1 - zscale
p3.x = x3d1 - xscale
p3.y = y3d1
p3.z = z3d1 + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' left face
glTexCoord2f(0,0)
glVertex3f(xtemp(c)-xscale,ytemp(c),ztemp(c)-zscale)
glTexCoord2f(1,0)
glVertex3f(x3d1-xscale,y3d1,z3d1-zscale)
glTexCoord2f(1,1)
glVertex3f(x3d1-xscale,y3d1,z3d1+zscale)
glTexCoord2f(0,1)
glVertex3f(xtemp(c)-xscale,ytemp(c),ztemp(c)+zscale)
p1.x = xtemp(c) - xscale
p1.y = ytemp(c)
p1.z = ztemp(c) - zscale
p2.x = xtemp(c) + xscale
p2.y = ytemp(c)
p2.z = ztemp(c) - zscale
p3.x = x3d1 + xscale
p3.y = y3d1
p3.z = z3d1 - zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' back
glTexCoord2f(0,0)
glVertex3f(xtemp(c)-xscale,ytemp(c),ztemp(c)-zscale)
glTexCoord2f(1,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),ztemp(c)-zscale)
glTexCoord2f(1,1)
glVertex3f(x3d1+xscale,y3d1,z3d1-zscale)
glTexCoord2f(0,1)
glVertex3f(x3d1-xscale,y3d1,z3d1-zscale)
p1.x = xtemp(c) - xscale
p1.y = ytemp(c)
p1.z = ztemp(c) + zscale
p2.x = xtemp(c) + xscale
p2.y = ytemp(c)
p2.z = ztemp(c) + zscale
p3.x = x3d1 + xscale
p3.y = y3d1
p3.z = z3d1 + zscale
getnorm(p1,p2,p3)
glNormal3f(norm(0),norm(1),norm(2))
' front
glTexCoord2f(0,0)
glVertex3f(xtemp(c)-xscale,ytemp(c),ztemp(c)+zscale)
glTexCoord2f(1,0)
glVertex3f(xtemp(c)+xscale,ytemp(c),ztemp(c)+zscale)
glTexCoord2f(1,1)
glVertex3f(x3d1+xscale,y3d1,z3d1+zscale)
glTexCoord2f(0,1)
glVertex3f(x3d1-xscale,y3d1,z3d1+zscale)
glEnd()
return
'' Load Bitmaps And Convert To Textures
function LoadGLTextures() as integer
dim Status as integer = FALSE '' Status Indicator
dim TextureImage(9) as BITMAP_RGBImageRec ptr '' Create Storage Space For The Texture
' Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
' TextureImage(0) = LoadBMP(exepath + "/textures/demon.bmp")
TextureImage(0) = LoadBMP("c:\FBFiles\textures\bark03.bmp")
TextureImage(1) = LoadBMP("c:\FBFiles\Maps\venus.bmp")
TextureImage(2) = LoadBMP("c:\FBFiles\Maps\earth.bmp")
'TextureImage(3) = LoadBMP("c:\FBFiles\Maps\moon24.bmp")
'TextureImage(4) = LoadBMP("c:\FBFiles\Maps\earth.bmp")
'TextureImage(5) = LoadBMP("c:\FBFiles\Maps\mars.bmp")
'TextureImage(6) = LoadBMP("c:\FBFiles\Maps\jupiter.bmp") ' also jupiter2
'TextureImage(7) = LoadBMP("c:\FBFiles\Maps\moon24.bmp")
'TextureImage(8) = LoadBMP("c:\FBFiles\Maps\earth.bmp")
TextureImage(9) = LoadBMP("c:\FBFiles\Maps\pluto.bmp")
for i = 0 to 9
if TextureImage(i) then
Status = TRUE '' Set The Status To TRUE
glGenTextures 1, @texture(i) '' Create The Texture
' Typical Texture Generation Using Data From The Bitmap
glBindTexture GL_TEXTURE_2D, texture(i)
glTexImage2D GL_TEXTURE_2D, 0, 3, TextureImage(i)->sizeX, TextureImage(i)->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage(i)->buffer
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
end if
if TextureImage(i) then '' If Texture Exists
if TextureImage(i)->buffer then '' If Texture Image Exist
deallocate(TextureImage(i)->buffer) '' Free The Texture Image Memory
end if
deallocate(TextureImage(i)) '' Free The Image Structure
end if
next
return Status '' Return The Status
end function
********************************************************
Now, How can I do this with glucylinders?