Post by chris857 on Aug 1, 2010 21:21:29 GMT -5
Over the past couple of days I've made several fractals. Enjoy.
(Note: Play around with the values of maxit and width. Just don't make them too large, then the programs go REALLY slowly.)
Koch Curve
2D Menger Sponge
2D "inverse" Menger Sponge
3D Menger Sponge
3D "inverse" Menger Sponge
Sierpinski Triangle
Sierpinski Pyramid
If you'll notice, they are all constructed very similarly (as a list of lists). One ironic thing is that I've found a way to make glCallLists slow.
(Note: Play around with the values of maxit and width. Just don't make them too large, then the programs go REALLY slowly.)
Koch Curve
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 100000)
glMatrixMode(GL_MODELVIEW)
dim i
const maxit = 6
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glBegin(GL_LINES)
glColor3f(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
glCallList(FractalIteration(i-1))
glTranslatef(pow(3,i-1),0,0)
glRotatef(60,0,0,1)
glCallList(FractalIteration(i-1))
glTranslatef(pow(3,i-1),0,0)
glRotatef(-120,0,0,1)
glCallList(FractalIteration(i-1))
glTranslatef(pow(3,i-1),0,0)
glRotatef(60,0,0,1)
glCallList(FractalIteration(i-1))
glTranslatef(-pow(3,i)+pow(3,i-1),0,0)
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(3,maxit)
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(3,maxit)/2,pow(3,maxit)/3.45,-pow(3,maxit)/2)
glCallList(FractalIteration(maxit))
glTranslatef(pow(3,maxit),0,0)
glRotatef(-120,0,0,1)
glCallList(FractalIteration(maxit))
glTranslatef(pow(3,maxit),0,0)
glRotatef(-120,0,0,1)
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xpos = xpos + 1*maxit
endif
if ScanKeyDown(VK_RIGHT) then
xpos = xpos - 1*maxit
endif
if ScanKeyDown(VK_UP) then
ypos = ypos - 1*maxit
endif
if ScanKeyDown(VK_DOWN) then
ypos = ypos + 1*maxit
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1*maxit
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1*maxit
endif
wend
wend
2D Menger Sponge
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 100000)
glMatrixMode(GL_MODELVIEW)
dim i
dim x,y
const maxit = 5
dim width = 3
if width < 3 then width = 3 endif
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glBegin(GL_QUADS)
glColor3f(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(0,1,0)
glColor3f(.8,.8,.8)
glVertex3f(0,0,1)
glVertex3f(1,0,1)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.6,.6,.6)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,0,1)
glVertex3f(0,0,1)
glColor3f(.4,.4,.4)
glVertex3f(0,1,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.2,.2,.2)
glVertex3f(0,0,0)
glVertex3f(0,1,0)
glVertex3f(0,1,1)
glVertex3f(0,0,1)
glColor3f(.0,.0,.0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(1,0,1)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
for x = 0 to width-1
for y = 0 to width-1
if (x = 0 or x = width-1 or y = 0 or y = width-1) then
glTranslatef(x*pow(width,i-1),y*pow(width,i-1),0)
glCallList(FractalIteration(i-1))
glTranslatef(-x*pow(width,i-1),-y*pow(width,i-1),0)
endif
next
next
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(3,maxit)*pow(3,width-2)*.5
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(width,maxit)/2,-pow(width,maxit)/2,0)
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xrot = xrot - 1
endif
if ScanKeyDown(VK_RIGHT) then
xrot = xrot + 1
endif
if ScanKeyDown(VK_UP) then
yrot = yrot - 1
endif
if ScanKeyDown(VK_DOWN) then
yrot = yrot + 1
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1
endif
if mouse_button(mouse_lbutton) then
xpos = xpos - mouse_xd()*zoom
ypos = ypos + mouse_yd()*zoom
else
xrot = xrot + mouse_xd()*5
yrot = yrot + mouse_yd()*5
endif
zoom = zoom + mouse_wheel() * .5*width
wend
wend
2D "inverse" Menger Sponge
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 100000)
glMatrixMode(GL_MODELVIEW)
dim i
dim x,y,z
const maxit = 5
dim width = 3
if width < 3 then width = 3 endif
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glBegin(GL_QUADS)
glColor3f(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(0,1,0)
glColor3f(.8,.8,.8)
glVertex3f(0,0,1)
glVertex3f(1,0,1)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.6,.6,.6)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,0,1)
glVertex3f(0,0,1)
glColor3f(.4,.4,.4)
glVertex3f(0,1,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.2,.2,.2)
glVertex3f(0,0,0)
glVertex3f(0,1,0)
glVertex3f(0,1,1)
glVertex3f(0,0,1)
glColor3f(.0,.0,.0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(1,0,1)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
for x = 0 to width-1
for y = 0 to width-1
for z = 0 to width-1
if not((x = 0 or x = width-1 or y = 0 or y = width-1) and (z = 0 or z = width-1) or ((x = 0 or x = width-1) and (y = 0 or y = width-1)))then
glTranslatef(x*pow(width,i-1),y*pow(width,i-1),0*z*pow(width,i-1))
glCallList(FractalIteration(i-1))
glTranslatef(-x*pow(width,i-1),-y*pow(width,i-1),-0*z*pow(width,i-1))
endif
next
next
next
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(3,maxit)*pow(3,width-2)*.5
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(width,maxit)/2,-pow(width,maxit)/2,0)
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xrot = xrot - 1
endif
if ScanKeyDown(VK_RIGHT) then
xrot = xrot + 1
endif
if ScanKeyDown(VK_UP) then
yrot = yrot - 1
endif
if ScanKeyDown(VK_DOWN) then
yrot = yrot + 1
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1*maxit
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1*maxit
endif
if mouse_button(mouse_lbutton) then
xpos = xpos - mouse_xd()*zoom
ypos = ypos + mouse_yd()*zoom
else
xrot = xrot + mouse_xd()*5
yrot = yrot + mouse_yd()*5
endif
zoom = zoom + mouse_wheel() * .5*width*maxit
wend
wend
3D Menger Sponge
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 100000)
glMatrixMode(GL_MODELVIEW)
dim i
dim x,y,z
const maxit = 3
dim width = 3
if width < 3 then width = 3 endif
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glBegin(GL_QUADS)
glColor3f(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(0,1,0)
glColor3f(.8,.8,.8)
glVertex3f(0,0,1)
glVertex3f(1,0,1)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.6,.6,.6)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,0,1)
glVertex3f(0,0,1)
glColor3f(.4,.4,.4)
glVertex3f(0,1,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.2,.2,.2)
glVertex3f(0,0,0)
glVertex3f(0,1,0)
glVertex3f(0,1,1)
glVertex3f(0,0,1)
glColor3f(.0,.0,.0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(1,0,1)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
for x = 0 to width-1
for y = 0 to width-1
for z = 0 to width-1
if (x = 0 or x = width-1 or y = 0 or y = width-1) and (z = 0 or z = width-1) or ((x = 0 or x = width-1) and (y = 0 or y = width-1))then
glTranslatef(x*pow(width,i-1),y*pow(width,i-1),z*pow(width,i-1))
glCallList(FractalIteration(i-1))
glTranslatef(-x*pow(width,i-1),-y*pow(width,i-1),-z*pow(width,i-1))
endif
next
next
next
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(3,maxit)*pow(3,width-2)*.5
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(width,maxit)/2,-pow(width,maxit)/2,-pow(width,maxit)/2)
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xrot = xrot - 1
endif
if ScanKeyDown(VK_RIGHT) then
xrot = xrot + 1
endif
if ScanKeyDown(VK_UP) then
yrot = yrot - 1
endif
if ScanKeyDown(VK_DOWN) then
yrot = yrot + 1
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1
endif
if mouse_button(mouse_lbutton) then
xpos = xpos - mouse_xd()*zoom
ypos = ypos + mouse_yd()*zoom
else
xrot = xrot + mouse_xd()*5
yrot = yrot + mouse_yd()*5
endif
zoom = zoom + mouse_wheel() * .5*width
wend
wend
3D "inverse" Menger Sponge
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 100000)
glMatrixMode(GL_MODELVIEW)
dim i
dim x,y,z
const maxit = 5
dim width = 3
if width < 3 then width = 3 endif
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glBegin(GL_QUADS)
glColor3f(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(0,1,0)
glColor3f(.8,.8,.8)
glVertex3f(0,0,1)
glVertex3f(1,0,1)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.6,.6,.6)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,0,1)
glVertex3f(0,0,1)
glColor3f(.4,.4,.4)
glVertex3f(0,1,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(0,1,1)
glColor3f(.2,.2,.2)
glVertex3f(0,0,0)
glVertex3f(0,1,0)
glVertex3f(0,1,1)
glVertex3f(0,0,1)
glColor3f(.0,.0,.0)
glVertex3f(1,0,0)
glVertex3f(1,1,0)
glVertex3f(1,1,1)
glVertex3f(1,0,1)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
for x = 0 to width-1
for y = 0 to width-1
for z = 0 to width-1
if not((x = 0 or x = width-1 or y = 0 or y = width-1) and (z = 0 or z = width-1) or ((x = 0 or x = width-1) and (y = 0 or y = width-1)))then
glTranslatef(x*pow(width,i-1),y*pow(width,i-1),z*pow(width,i-1))
glCallList(FractalIteration(i-1))
glTranslatef(-x*pow(width,i-1),-y*pow(width,i-1),-z*pow(width,i-1))
endif
next
next
next
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(3,maxit)*pow(3,width-2)*.5
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(width,maxit)/2,-pow(width,maxit)/2,-pow(width,maxit)/2)
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xrot = xrot - 1
endif
if ScanKeyDown(VK_RIGHT) then
xrot = xrot + 1
endif
if ScanKeyDown(VK_UP) then
yrot = yrot - 1
endif
if ScanKeyDown(VK_DOWN) then
yrot = yrot + 1
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1*maxit
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1*maxit
endif
if mouse_button(mouse_lbutton) then
xpos = xpos - mouse_xd()*zoom
ypos = ypos + mouse_yd()*zoom
else
xrot = xrot + mouse_xd()*5
yrot = yrot + mouse_yd()*5
endif
zoom = zoom + mouse_wheel() * .5*width*maxit
wend
wend
Sierpinski Triangle
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 10000)
glMatrixMode(GL_MODELVIEW)
dim i
const maxit = 9
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glColor3f(1,1,1)
glBegin(GL_TRIANGLES)
glVertex3f(0,0,0)
glVertex3f(.5,1,0)
glVertex3f(1,0,0)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
glCallList(FractalIteration(i-1))
glTranslatef(pow(2,i-1),0,0)
glCallList(FractalIteration(i-1))
glTranslatef(-pow(2,i-1)*.5,pow(2,i-1),0)
glCallList(FractalIteration(i-1))
glTranslatef(-pow(2,i-1)*.5,-pow(2,i-1),0)
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(2,maxit)*.5
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(2,maxit)/2,-pow(2,maxit)/2,-pow(2,maxit)/2)
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xpos = xpos + 1*maxit
endif
if ScanKeyDown(VK_RIGHT) then
xpos = xpos - 1*maxit
endif
if ScanKeyDown(VK_UP) then
ypos = ypos - 1*maxit
endif
if ScanKeyDown(VK_DOWN) then
ypos = ypos + 1*maxit
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1*maxit
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1*maxit
endif
wend
wend
Sierpinski Pyramid
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluPerspective(60, (1.0*windowwidth())/windowheight(), 1, 100000)
glMatrixMode(GL_MODELVIEW)
dim i
dim x,y,z
const maxit = 6
dim FractalIteration(maxit)
FractalIteration(0) = glGenLists(1)
glNewList(FractalIteration(0) , GL_COMPILE)
glBegin(GL_TRIANGLES)
glColor3f(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,1,1)
glVertex3f(2,0,0)
glColor3f(.5,.5,.5)
glVertex3f(0,0,0)
glVertex3f(1,1,1)
glVertex3f(0,0,2)
glColor3f(.7,.7,.7)
glVertex3f(2,0,0)
glVertex3f(1,1,1)
glVertex3f(2,0,2)
glColor3f(.3,.3,.3)
glVertex3f(0,0,2)
glVertex3f(1,1,1)
glVertex3f(2,0,2)
glEnd()
glBegin(GL_QUADS)
glColor3f(.1,.1,.1)
glVertex3f(0,0,0)
glVertex3f(0,0,2)
glVertex3f(2,0,2)
glVertex3f(2,0,0)
glEnd()
glEndList()
for i = 1 to maxit
FractalIteration(i) = glGenLists(1)
glNewList(FractalIteration(i) , GL_COMPILE)
glCallList(FractalIteration(i-1))
glTranslatef(pow(2,i),0,0)
glCallList(FractalIteration(i-1))
glTranslatef(0,0,pow(2,i))
glCallList(FractalIteration(i-1))
glTranslatef(-pow(2,i)*.5,pow(2,i)*.5,-pow(2,i)*.5)
glCallList(FractalIteration(i-1))
glTranslatef(-pow(2,i)*.5,-pow(2,i)*.5,pow(2,i)*.5)
glCallList(FractalIteration(i-1))
glTranslatef(0,0,-pow(2,i))
glEndList()
next
dim xrot as single
dim yrot as single
dim xpos as single
dim ypos as single
dim zoom as single = -pow(2,maxit)*3
while true
glClearColor(.2,.2,.8,1)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(xpos,ypos,zoom)
glRotatef(xrot,0,1,0)
glRotatef(yrot,1,0,0)
glTranslatef(-pow(2,maxit),-pow(2,maxit)/2,-pow(2,maxit))
glCallList(FractalIteration(maxit))
SwapBuffers()
while synctimer(20)
if ScanKeyDown(VK_LEFT) then
xrot = xrot - 1
endif
if ScanKeyDown(VK_RIGHT) then
xrot = xrot + 1
endif
if ScanKeyDown(VK_UP) then
yrot = yrot - 1
endif
if ScanKeyDown(VK_DOWN) then
yrot = yrot + 1
endif
if ScanKeyDown(VK_ADD) then
zoom = zoom + 1*maxit
endif
if ScanKeyDown(VK_SUBTRACT) then
zoom = zoom - 1*maxit
endif
if mouse_button(mouse_lbutton) then
xpos = xpos - mouse_xd()*zoom
ypos = ypos + mouse_yd()*zoom
else
xrot = xrot + mouse_xd()*5
yrot = yrot + mouse_yd()*5
endif
zoom = zoom + mouse_wheel() * .5*maxit
wend
wend
If you'll notice, they are all constructed very similarly (as a list of lists). One ironic thing is that I've found a way to make glCallLists slow.