-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·147 lines (121 loc) · 3.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
import sys
from random import *
# import OpenGL
import OpenGL.GL as GL
import OpenGL.GLUT as GLUT
import OpenGL.GLU as GLU
import glfw
from vec3 import *
from scene_objs import *
import config
# width = 1400
# height = 1000
shapes = []
lasttime = 0
def get_last_elapsed_time():
global lasttime
# lasttime = glfwget_time()
actualtime = glfw.get_time()
difference = actualtime - lasttime
lasttime = actualtime
return difference
def update(frametime):
global shapes
for i in range(0, len(shapes)):
shapes[i].update(frametime)
def render():
frametime = get_last_elapsed_time()
domath()
update(frametime)
print("framerate: " + str(round(1 / frametime)) + " ", end = "\r")
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) # Remove everything from screen (i.e. displays all white)
GL.glLoadIdentity() # Reset all graphic/shape's position
GL.glViewport(0, 0, config.width, config.height)
# GL.glMatrixMode(GL.GL_PROJECTION) # no matrices necessary right now
# GL.glLoadIdentity()
GL.glOrtho(0.0, config.width, 0.0, config.height, 0.0, 1.0)
# GL.glMatrixMode (GL.GL_MODELVIEW) # no matrices necessary right now
# GL.glLoadIdentity()
for i in range(0, len(shapes)):
shapes[i].draw()
GLUT.glutSwapBuffers()
def domath():
global shapes
if config.mode == config.GALAXY_DEMO:
for i in range(0, len(shapes)):
acceleration = vec3()
for j in range(0, len(shapes)):
if i != j:
vect = shapes[j].pos - shapes[i].pos
dist = vect.magnitude()
vect.normalize()
mass = 1
if dist > 1:
acceleration = acceleration + vect * (config.G * mass / (dist * dist))
shapes[i].acceleration = acceleration
def init_geom():
global shapes
if config.mode == config.HELLO_TRIANGLE:
a = vec3(100, 100, 0)
b = vec3(300, 100, 0)
c = vec3(200, 300, 0)
velocity = vec3(50,50,0)
color = vec3(0.8, 0.8, 0.0)
shapes.append(triangle(a, b, c, velocity, color))
elif config.mode == config.SQUARES_DEMO:
shapes = make_squares(50)
elif config.mode == config.POINTS_DEMO:
shapes = make_points(1000)
elif config.mode == config.PHYSICAL_POINTS_DEMO:
shapes = make_physical_points(1000)
elif config.mode == config.GALAXY_DEMO:
shapes = make_points(100, 0, 600, 800, 400, 600)
def init():
global lasttime
if not glfw.init():
return False
lasttime = glfw.get_time()
config.width = 1400
config.height = 1000
# default setup
config.scene_name = "squares demo"
config.mode = config.SQUARES_DEMO
if (len(sys.argv) == 2):
if (sys.argv[1] == "0"):
config.scene_name = "hello triangle"
config.mode = config.HELLO_TRIANGLE
elif (sys.argv[1] == "1"):
config.scene_name = "squares demo"
config.mode = config.SQUARES_DEMO
elif (sys.argv[1] == "2"):
config.scene_name = "points demo"
config.mode = config.POINTS_DEMO
elif (sys.argv[1] == "3"):
config.scene_name = "physical points demo"
config.mode = config.PHYSICAL_POINTS_DEMO
elif (sys.argv[1] == "4"):
config.scene_name = "galaxy demo"
config.mode = config.GALAXY_DEMO
else:
print("scene does not exist")
return
init_geom()
if not GLUT.glutInit(): # Initialize a glut instance which will allow us to customize our window
return False
GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA) # Set the display mode to be colored
GLUT.glutInitWindowSize(config.width, config.height) # Set the width and height of your window
GLUT.glutInitWindowPosition(0, 0) # Set the position at which this windows should appear
return True
def main():
if not init():
print("initialization failed")
return
window = GLUT.glutCreateWindow(config.scene_name) # Give your window a title
GLUT.glutDisplayFunc(render) # Tell OpenGL to call the render method continuously
GLUT.glutIdleFunc(render) # Draw any graphics or shapes in the render function at all times
GLUT.glutMainLoop() # Keeps the window created above displaying/running in a loop
main()
# add spin?
# go 3d
# add shaders