From 18c27ca60bc364d5e6cb811f56ec8c4f72867ac9 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 26 Mar 2020 21:30:05 +0100 Subject: Prototype port to 3D What is it with OpenCL @ GPU and arrays of 3-dimensional vectors?! Luckily I vaguely remembered encountering this problem before when implementing my OpenCL LBM code but why on earth is the compiler not warning about this? I guess the underlying reason has to do with the data alignment requirements on the GPU but still... --- boltzgas/visual/view.py | 90 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 6 deletions(-) (limited to 'boltzgas/visual') diff --git a/boltzgas/visual/view.py b/boltzgas/visual/view.py index e2746e2..327214a 100644 --- a/boltzgas/visual/view.py +++ b/boltzgas/visual/view.py @@ -1,7 +1,7 @@ from OpenGL.GL import * from OpenGL.GLUT import * -from pyrr import matrix44 +from pyrr import matrix44, quaternion import numpy as np @@ -24,26 +24,31 @@ particle_shader = ( """ #version 430 - layout (location=0) in vec2 particles; + layout (location=0) in vec4 particles; out vec3 color; uniform mat4 projection; + uniform mat4 rotation; + uniform vec3 face_color; uniform vec3 trace_color; uniform uint trace_id; void main() { - gl_Position = projection * vec4(particles, 0., 1.); + gl_Position = projection * rotation * vec4(particles.xyz, 1.); - if (gl_VertexID == trace_id) { + if (particles.x < 0.0 || particles.x > 1.0 || + particles.y < 0.0 || particles.y > 1.0 || + particles.z < 0.0 || particles.z > 1.0 + ) { color = trace_color; } else { color = face_color; } } """, - ['projection', 'face_color', 'trace_color', 'trace_id'] + ['projection', 'rotation', 'face_color', 'trace_color', 'trace_id'] ) decoration_shader = ( @@ -106,6 +111,69 @@ texture_shader = ( ['picture','projection','mixing'] ) + +class Projection: + def __init__(self, distance): + self.distance = distance + self.ratio = 4./3. + self.update() + + def update(self): + projection = matrix44.create_perspective_projection(20.0, self.ratio, 0.1, 5000.0) + look = matrix44.create_look_at( + eye = [0, 0, -self.distance], + target = [0, 0, 0], + up = [0,-1, 0]) + + self.matrix = np.matmul(look, projection) + + def update_ratio(self, width, height, update_viewport = True): + if update_viewport: + glViewport(0,0,width,height) + + self.ratio = width/height + self.update() + + def update_distance(self, change): + self.distance += change + self.update() + + def get(self): + return self.matrix + +class Rotation: + def __init__(self, shift, x = np.pi, z = np.pi): + self.matrix = matrix44.create_from_translation(shift), + self.rotation_x = quaternion.Quaternion() + self.update(x,z) + + def shift(self, x, z): + self.matrix = np.matmul( + self.matrix, + matrix44.create_from_translation([x,0,z]) + ) + self.inverse_matrix = np.linalg.inv(self.matrix) + + def update(self, x, z): + rotation_x = quaternion.Quaternion(quaternion.create_from_eulers([x,0,0])) + rotation_z = self.rotation_x.conjugate.cross( + quaternion.Quaternion(quaternion.create_from_eulers([0,0,z]))) + self.rotation_x = self.rotation_x.cross(rotation_x) + + self.matrix = np.matmul( + self.matrix, + matrix44.create_from_quaternion(rotation_z.cross(self.rotation_x)) + ) + self.inverse_matrix = np.linalg.inv(self.matrix) + + def get(self): + return self.matrix + + def get_inverse(self): + return self.inverse_matrix + + + class View: def __init__(self, gas, decorations, windows): self.gas = gas @@ -116,12 +184,17 @@ class View: self.particle_shader = Shader(*particle_shader) self.decoration_shader = Shader(*decoration_shader) + self.projection3d = Projection(distance = 7) + self.rotation3d = Rotation([-1/2, -1/2, -1/2], 5*np.pi/4, np.pi/4) + def reshape(self, width, height): glViewport(0,0,width,height) world_height = 1.4 world_width = world_height / height * width + projection = Projection(10) + projection = matrix44.create_orthogonal_projection(-world_width/2, world_width/2, -world_height/2, world_height/2, -1, 1) translation = matrix44.create_from_translation([-1.05, -1.0/2, 0]) @@ -143,10 +216,15 @@ class View: window.display(self.texture_shader.uniform) self.particle_shader.use() - glUniformMatrix4fv(self.particle_shader.uniform['projection'], 1, False, np.asfortranarray(self.projection)) + #glUniformMatrix4fv(self.particle_shader.uniform['projection'], 1, False, np.asfortranarray(self.projection)) + + glUniformMatrix4fv(self.particle_shader.uniform['projection'], 1, False, np.ascontiguousarray(self.projection3d.get())) + glUniformMatrix4fv(self.particle_shader.uniform['rotation'], 1, False, np.ascontiguousarray(self.rotation3d.get())) + glUniform3f(self.particle_shader.uniform['face_color'], 1., 1., 1.) glUniform3f(self.particle_shader.uniform['trace_color'], 1., 0., 0.) glUniform1ui(self.particle_shader.uniform['trace_id'], -1) + glEnable(GL_POINT_SPRITE) glPointSize(2*self.gas.radius*self.pixels_per_unit) self.gas.gl_draw_particles() -- cgit v1.2.3