-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulate.py
300 lines (226 loc) · 10.7 KB
/
simulate.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import glfw
from OpenGL.GLUT import *
from OpenGL.GL import *
import ShaderLoader
import numpy
import pyrr
from pyrr import matrix44, vector3
import TextureLoader
from Camera import Camera
from PIL import Image
from ObjLoader import *
from properties import planets
import random
import math
cam = Camera()
keys = [False] * 1024
lastX, lastY = 640, 360
first_mouse = True
def key_callback(window, key, scancode, action, mode):
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
glfw.set_window_should_close(window, True)
if key >= 0 and key < 1024:
if action == glfw.PRESS:
keys[key] = True
elif action == glfw.RELEASE:
keys[key] = False
def do_movement():
if keys[glfw.KEY_W]:
cam.process_keyboard("FORWARD", 1)
if keys[glfw.KEY_S]:
cam.process_keyboard("BACKWARD", 1)
if keys[glfw.KEY_A]:
cam.process_keyboard("LEFT", 1)
if keys[glfw.KEY_D]:
cam.process_keyboard("RIGHT", 1)
def mouse_callback(window, xpos, ypos):
global first_mouse, lastX, lastY
if first_mouse:
lastX = xpos
lastY = ypos
first_mouse = False
xoffset = xpos - lastX
yoffset = lastY - ypos
lastX = xpos
lastY = ypos
cam.process_mouse_movement(xoffset, yoffset)
def window_resize(window, width, height):
glViewport(0, 0, width, height)
def main():
# initialize glfw
if not glfw.init():
return
w_width, w_height = 800, 600
aspect_ratio = w_width / w_height
window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
glfw.set_window_size_callback(window, window_resize)
glfw.set_key_callback(window, key_callback)
glfw.set_cursor_pos_callback(window, mouse_callback)
obj = ObjLoader()
obj.load_model("objects/sphere.obj")
texture_offset = len(obj.vertex_index)*12
normal_offset = (texture_offset + len(obj.texture_index)*8)
shader = ShaderLoader.compile_shader("shaders/main_vert.vs", "shaders/main_frag.fs")
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW)
#positions
position = glGetAttribLocation(shader, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
#textures
texCoords = glGetAttribLocation(shader, "inTexCoords")
glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset))
glEnableVertexAttribArray(texCoords)
#normals
normals = glGetAttribLocation(shader, "vertNormal")
glVertexAttribPointer(normals, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset))
glEnableVertexAttribArray(normals)
for planet in planets:
texture = TextureLoader.load_texture(planets[planet]['image_path'])
planets[planet]['texture'] = texture
#initial position
if planet == 'sun' or planet == 'stars':
distance_from_sun = planets[planet]['distance_from_sun']
planets[planet]['initial_position'] = [distance_from_sun, 0.0, distance_from_sun]
else:
distance_from_sun = 30.0 + planets[planet]['distance_from_sun']
x = round(random.uniform(-distance_from_sun, distance_from_sun), 3)
z = round(math.sqrt((distance_from_sun ** 2) - (x ** 2)), 3)
planets[planet]['initial_position'] = [x, 0.0, z]
glEnable(GL_TEXTURE_2D)
glUseProgram(shader)
glClearColor(0.0, 0.2, 0.2, 1.0)
glEnable(GL_DEPTH_TEST)
projection = pyrr.matrix44.create_perspective_projection_matrix(65.0, w_width / w_height, 0.1, 10000.0)
scale = pyrr.matrix44.create_from_scale(pyrr.Vector3([0.1, 0.1, 0.1]))
view_loc = glGetUniformLocation(shader, "view")
proj_loc = glGetUniformLocation(shader, "projection")
model_loc = glGetUniformLocation(shader, "model")
normal_loc = glGetUniformLocation(shader, "normalMatrix")
scale_loc = glGetUniformLocation(shader, "scale")
scale_planet_loc = glGetUniformLocation(shader, "scale_planet")
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
glUniformMatrix4fv(scale_loc, 1, GL_FALSE, scale)
Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient")
Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient")
Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse")
Light_specular_loc = glGetUniformLocation(shader, "Light_specular")
Light_location_loc = glGetUniformLocation(shader, "Light_location")
Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient")
Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse")
Material_specular_loc = glGetUniformLocation(shader, "Material_specular")
Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess")
glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1)
glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0)
glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0)
glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0)
glUniform3f(Light_location_loc, 0, 0, 0)
# *******************************************************************************************
# obj2 = ObjLoader()
# obj2.load_model("objects/cruiser.obj")
#
# shader2 = ShaderLoader.compile_shader("shaders/vert.vs", "shaders/frag.fs")
#
# VBO2 = glGenBuffers(1)
# glBindBuffer(GL_ARRAY_BUFFER, VBO2)
# glBufferData(GL_ARRAY_BUFFER, obj2.model.itemsize * len(obj2.model), obj2.model, GL_STATIC_DRAW)
#
# # positions
# position2 = glGetAttribLocation(shader, "position")
# glVertexAttribPointer(position2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(0))
# glEnableVertexAttribArray(position2)
# # textures
# texCoords2 = glGetAttribLocation(shader2, "inTexCoords")
# glVertexAttribPointer(texCoords2, 2, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 2, ctypes.c_void_p(texture_offset))
# glEnableVertexAttribArray(texCoords2)
# # normals
# normals2 = glGetAttribLocation(shader2, "vertNormal")
# glVertexAttribPointer(normals2, 3, GL_FLOAT, GL_FALSE, obj2.model.itemsize * 3, ctypes.c_void_p(normal_offset))
# glEnableVertexAttribArray(normals2)
#
#
# texture2 = TextureLoader.load_texture(planets['cruiser']['image_path'])
# distance_from_sun = planets['cruiser']['distance_from_sun']
# planets['cruiser']['initial_position'] = [distance_from_sun, 0.0, distance_from_sun]
#
# glUseProgram(shader2)
#
#
# Global_ambient_loc = glGetUniformLocation(shader, "Global_ambient")
# Light_ambient_loc = glGetUniformLocation(shader, "Light_ambient")
# Light_diffuse_loc = glGetUniformLocation(shader, "Light_diffuse")
# Light_specular_loc = glGetUniformLocation(shader, "Light_specular")
# Light_location_loc = glGetUniformLocation(shader, "Light_location")
# Material_ambient_loc = glGetUniformLocation(shader, "Material_ambient")
# Material_diffuse_loc = glGetUniformLocation(shader, "Material_diffuse")
# Material_specular_loc = glGetUniformLocation(shader, "Material_specular")
# Material_shininess_loc = glGetUniformLocation(shader, "Material_shininess")
#
# glUniform4f(Global_ambient_loc, 0.8, 0.8, 0.9, 0.1)
# glUniform4f(Light_ambient_loc, 0.3, 0.3, 0.3, 1.0)
# glUniform4f(Light_diffuse_loc, 0.25, 0.25, 0.25, 1.0)
# glUniform4f(Light_specular_loc, 0.9, 0.9, 0.9, 1.0)
# glUniform3f(Light_location_loc, 0, 0, 0)
# glUniform4f(Material_ambient_loc, 0.4, 0.4, 0.4, 1.0)
# glUniform4f(Material_diffuse_loc, 0.15, 0.15, 0.15, 1.0)
# glUniform4f(Material_specular_loc, 1.0, 1.0, 1.0, 1.0)
# glUniform1f(Material_shininess_loc, .95)
# ******************************************************************************************
while not glfw.window_should_close(window):
glfw.poll_events()
do_movement()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
time = glfw.get_time()
view = cam.get_view_matrix()
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
# **********************************planets****************************************
for planet in planets:
if planet == 'cruiser':
glBindTexture(GL_TEXTURE_2D, texture2)
else:
glBindTexture(GL_TEXTURE_2D, planets[planet]['texture'])
revolution_speed = time * planets[planet]['revolution_ratio_relative_to_earth']
rotation_speed = time * planets[planet]['rotation_ratio_relative_to_earth']
# scale planet
scale_factor = planets[planet]['size_ratio_relative_to_earth']
scale_planet = matrix44.create_from_scale(pyrr.Vector3([scale_factor,scale_factor,scale_factor]))
glUniformMatrix4fv(scale_planet_loc, 1, GL_FALSE, scale_planet)
# translation
model = matrix44.create_from_translation(pyrr.Vector3(planets[planet]['initial_position']))
revolution = matrix44.create_from_y_rotation(revolution_speed)
rotation = matrix44.create_from_y_rotation(rotation_speed)
# revolution about z axis
model = matrix44.multiply(model, revolution)
# rotation about own axis
model = matrix44.multiply(rotation, model)
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
# ----create normalMatrix--
modelView = numpy.matmul(view, model)
# modelView = numpy.matmul(modelView, scale_planet)
# modelView = numpy.matmul(modelView, scale)
modelView33 = modelView[0:-1, 0:-1]
normalMatrix = numpy.transpose(numpy.linalg.inv(modelView33))
# -----------------
glUniformMatrix3fv(normal_loc, 1, GL_FALSE, normalMatrix)
a,b,c,d = planets[planet]['Material_ambient']
glUniform4f(Material_ambient_loc, a,b,c,d)
a,b,c,d = planets[planet]['Material_diffuse']
glUniform4f(Material_diffuse_loc, a,b,c,d)
a,b,c,d = planets[planet]['Material_specular']
glUniform4f(Material_specular_loc, a,b,c,d)
s = planets[planet]['Material_shininess'][0]
glUniform1f(Material_shininess_loc, s)
if planet == 'cruiser':
glDrawArrays(GL_TRIANGLES, 0, len(obj2.vertex_index))
else:
glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index))
# *******************************************************************************
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()