Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- import os, sys
- import traceback
- from math import *
- with open(os.devnull, "w") as f:
- oldstdout=sys.stdout; sys.stdout=f; import pygame; sys.stdout=oldstdout
- from pygame.locals import *
- from OpenGL.GL import *
- from OpenGL.GLU import *
- if sys.platform in ["win32","win64"]: os.environ["SDL_VIDEO_CENTERED"]="1"
- pygame.display.init()
- pygame.font.init()
- screen_size = [1024,1024]
- icon = pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
- pygame.display.set_caption("[Program] - [Author] - [Version] - [Date]")
- multisample = 16
- if multisample:
- pygame.display.gl_set_attribute(GL_MULTISAMPLEBUFFERS,1)
- pygame.display.gl_set_attribute(GL_MULTISAMPLESAMPLES,multisample)
- pygame.display.set_mode(screen_size,OPENGL|DOUBLEBUF)
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
- glEnable(GL_TEXTURE_2D)
- glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE)
- glTexEnvi(GL_POINT_SPRITE,GL_COORD_REPLACE,GL_TRUE)
- glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)
- glEnable(GL_DEPTH_TEST)
- def lerp(x,y,t):
- return x*(1.0-t) + y*t
- data = [ (1/2-sqrt(2)/4,0), ((0.5,0),(3/4,1/4)), (0.5,0), ((0.5,1/8),(1/2,1/4)), (3/8,1/4),
- (1/4,1/8), (3/8,0), ((3/8,-1/8),(3/8,-1/4)), (5/8,-1/4), (5/8,-1/8), (1/2,-1/8),
- ((5/8,-1/8),(3/4,-1/8)), (1,0) ]
- ##data = [ (1/4,0), ((1/2,0),(1/2,1/4)), (1/2,-1/4), ((1/2,0),(3/4,0)), (1,0) ]
- ##data = [ (1/4,0), ((1/2,0),(1/2,1/4)), (3/4,0), (1/2,0), ((3/4,0),(1,-0.001)) ]
- ##data = [ (1/2-sqrt(2)/4,0), ((0.5,0),(3/4,1/4)), (1/2,1/8), (1/2,-1/4), (1,0) ]
- class Line(object):
- def __init__(self,p0,p1):
- self.p0 = p0
- self.p1 = p1
- vec = ( self.p1[0]-self.p0[0], self.p1[1]-self.p0[1] )
- self.angle = degrees(atan2(vec[1],vec[0]))
- self.length = hypot(vec[1],vec[0])
- self.children = None
- def apply_rules(self,axiom):
- if self.children == None:
- self.children = copy.deepcopy(axiom)
- else:
- for child in self.children:
- child.apply_rules(axiom)
- def push(self):
- glPushMatrix()
- glTranslatef(self.p0[0],self.p0[1],0.0)
- glRotatef(self.angle,0,0,1)
- glScalef(*((self.length,)*3))
- def pop(self):
- glPopMatrix()
- def draw(self):
- if self.children == None:
- glColor3f(1,1,0)
- glBegin(GL_LINES)
- glVertex2f(*self.p0)
- glVertex2f(*self.p1)
- glEnd()
- else:
- self.push()
- for child in self.children: child.draw()
- self.pop()
- class Arc(object):
- def __init__(self,p0,center,p1):
- self.center = center
- vec0 = (p0[0]-center[0],p0[1]-center[1])
- vec1 = (p1[0]-center[0],p1[1]-center[1])
- self.radius = hypot(vec0[0],vec0[1])
- angle0 = degrees(atan2(vec0[1],vec0[0]))
- angle1 = degrees(atan2(vec1[1],vec1[0]))
- while angle0<0.0: angle0+=360.0
- while angle1<0.0: angle1+=360.0
- if angle1>angle0 and angle1-angle0>180: angle1-=360.0
- elif angle1<angle0 and angle0-angle1>180: angle1+=360.0
- self.angle0 = radians(angle0)
- self.angle1 = radians(angle1)
- def apply_rules(self,axiom):
- pass
- def draw(self):
- glColor3f(1,0,0)
- glBegin(GL_LINE_STRIP)
- for i in range(100):
- angle = lerp(self.angle0,self.angle1,i/99.0)
- glVertex2f(self.center[0]+self.radius*cos(angle),self.center[1]+self.radius*sin(angle))
- glEnd()
- axiom = []
- pos = (0,0)
- for elem in data:
- if type(elem[0]) == tuple:
- #arc
- axiom.append(Arc( pos, elem[0], elem[1] ))
- pos = elem[1]
- else:
- #line
- axiom.append(Line( pos, elem ))
- pos = elem
- def apply_rule(system):
- for elem in system:
- elem.apply_rules(axiom)
- systems = [copy.deepcopy(axiom)]
- for i in range(3):
- system = copy.deepcopy(systems[-1])
- apply_rule(system)
- systems.append(system)
- dllists = [ glGenLists(1) for i in range(len(systems)) ]
- for i in range(len(systems)):
- glNewList(dllists[i],GL_COMPILE)
- for elem in systems[i]:
- elem.draw()
- glEndList()
- idisplay = 3
- def get_input():
- global idisplay
- for event in pygame.event.get():
- if event.type == QUIT: return False
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE: return False
- elif event.key==K_0: idisplay=0
- elif event.key==K_1: idisplay=1
- elif event.key==K_2: idisplay=2
- elif event.key==K_3: idisplay=3
- elif event.key==K_4: idisplay=4
- elif event.key==K_5: idisplay=5
- elif event.key==K_6: idisplay=6
- elif event.key==K_7: idisplay=7
- elif event.key==K_8: idisplay=8
- elif event.key==K_9: idisplay=9
- return True
- def draw():
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
- glViewport(0,0,screen_size[0],screen_size[1])
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glOrtho( -0.1,1.1, -0.6,0.6, -1.0,1.0 )
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glCallList(dllists[idisplay])
- pygame.display.flip()
- def main():
- clock = pygame.time.Clock()
- while True:
- if not get_input(): break
- draw()
- clock.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- try:
- main()
- except:
- traceback.print_exc()
- pygame.quit()
- input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement