aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--channel_2d_gl_interop.py43
1 files changed, 27 insertions, 16 deletions
diff --git a/channel_2d_gl_interop.py b/channel_2d_gl_interop.py
index 377bbd0..f85c29b 100644
--- a/channel_2d_gl_interop.py
+++ b/channel_2d_gl_interop.py
@@ -12,9 +12,11 @@ from OpenGL.GLUT import *
from OpenGL.GL import shaders
-screen_x = 1920
-screen_y = 1200
-pixels_per_cell = 4
+from pyrr import matrix44
+
+lattice_x = 480
+lattice_y = 300
+
updates_per_frame = 80
inflow = 0.01
@@ -53,11 +55,16 @@ boundary = Template("""
'inflow': inflow
})
-def get_projection():
- scale = numpy.diag([(2.0*pixels_per_cell)/screen_x, (2.0*pixels_per_cell)/screen_y, 1.0, 1.0])
- translation = numpy.matrix(numpy.identity(4))
- translation[3,0:3] = [-1.0, -1.0, 0.0]
- return scale * translation;
+def get_projection(width, height):
+ world_width = lattice_x
+ world_height = world_width / width * height
+
+ projection = matrix44.create_orthogonal_projection(-world_width/2, world_width/2, -world_height/2, world_height/2, -1, 1)
+ translation = matrix44.create_from_translation([-lattice_x/2, -lattice_y/2, 0])
+
+ point_size = width / world_width
+
+ return numpy.matmul(translation, projection), point_size
def glut_window(fullscreen = False):
glutInit(sys.argv)
@@ -66,7 +73,7 @@ def glut_window(fullscreen = False):
if fullscreen:
window = glutEnterGameMode()
else:
- glutInitWindowSize(screen_x, screen_y)
+ glutInitWindowSize(800, 600)
glutInitWindowPosition(0, 0)
window = glutCreateWindow("LBM")
@@ -117,7 +124,7 @@ void main() {
color = vec3(0.4,0.4,0.4);
}
}""").substitute({
- 'size_x': screen_x//pixels_per_cell,
+ 'size_x': lattice_x,
'inflow': inflow
}), GL_VERTEX_SHADER)
@@ -156,7 +163,7 @@ projection_id = shaders.glGetUniformLocation(shader_program, 'projection')
lattice = Lattice(
descriptor = D2Q9,
- geometry = Geometry(screen_x//pixels_per_cell, screen_y//pixels_per_cell),
+ geometry = Geometry(lattice_x, lattice_y),
moments = lbm.moments(optimize = False),
collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = relaxation_time),
boundary_src = boundary,
@@ -167,8 +174,6 @@ lattice.apply_material_map(
get_channel_material_map(lattice.geometry))
lattice.sync_material()
-projection = get_projection()
-
particles = Particles(
lattice.context,
lattice.queue,
@@ -195,11 +200,11 @@ def on_display():
glEnableClientState(GL_VERTEX_ARRAY)
shaders.glUseProgram(shader_program)
- glUniformMatrix4fv(projection_id, 1, False, numpy.asfortranarray(projection))
+ glUniformMatrix4fv(projection_id, 1, False, numpy.ascontiguousarray(projection))
glVertexPointer(4, GL_FLOAT, 0, lattice.memory.gl_moments)
- glPointSize(pixels_per_cell)
+ glPointSize(point_size)
glDrawArrays(GL_POINTS, 0, lattice.geometry.volume)
particles.gl_particles.bind()
@@ -210,18 +215,24 @@ def on_display():
glVertexPointer(4, GL_FLOAT, 0, particles.gl_particles)
- glPointSize(1)
+ glPointSize(point_size)
glDrawArrays(GL_POINTS, 0, particles.count)
glDisableClientState(GL_VERTEX_ARRAY)
glutSwapBuffers()
+def on_reshape(width, height):
+ global projection, point_size
+ glViewport(0,0,width,height)
+ projection, point_size = get_projection(width, height)
+
def on_timer(t):
glutTimerFunc(t, on_timer, t)
glutPostRedisplay()
glutDisplayFunc(on_display)
+glutReshapeFunc(on_reshape)
glutTimerFunc(10, on_timer, 10)
glutMainLoop()