diff options
Diffstat (limited to 'channel_2d_gl_interop.py')
-rw-r--r-- | channel_2d_gl_interop.py | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/channel_2d_gl_interop.py b/channel_2d_gl_interop.py index f85c29b..8e52d78 100644 --- a/channel_2d_gl_interop.py +++ b/channel_2d_gl_interop.py @@ -2,6 +2,7 @@ import numpy from string import Template from simulation import Lattice, Geometry +from utility.opengl import MomentsTexture from utility.particles import Particles from symbolic.generator import LBM @@ -14,6 +15,7 @@ from OpenGL.GL import shaders from pyrr import matrix44 + lattice_x = 480 lattice_y = 300 @@ -83,15 +85,32 @@ lbm = LBM(D2Q9) window = glut_window(fullscreen = False) -vertex_shader = shaders.compileShader(Template(""" +vertex_shader = shaders.compileShader(""" #version 430 -layout (location=0) in vec4 CellMoments; - -out vec3 color; +layout (location=0) in vec4 vertex; + out vec2 frag_pos; uniform mat4 projection; +void main() { + gl_Position = projection * vertex; + frag_pos = vertex.xy; +}""", GL_VERTEX_SHADER) + +fragment_shader = shaders.compileShader(Template(""" +#version 430 + +in vec2 frag_pos; + +uniform sampler2D moments; + +out vec4 result; + +vec2 unit(vec2 v) { + return vec2(v[0] / $size_x, v[1] / $size_y); +} + vec3 blueRedPalette(float x) { return mix( vec3(0.0, 0.0, 1.0), @@ -100,44 +119,34 @@ vec3 blueRedPalette(float x) { ); } -vec2 fluidVertexAtIndex(uint i) { - const float y = floor(float(i) / $size_x); - return vec2( - i - $size_x*y, - y - ); -} - -void main() { - const vec2 idx = fluidVertexAtIndex(gl_VertexID); - - gl_Position = projection * vec4( - idx.x, - idx.y, - 0., - 1. - ); - - if (CellMoments[3] > 0.0) { - color = blueRedPalette(CellMoments[3] / 0.2); +void main(){ + const vec2 sample_pos = unit(frag_pos); + const vec4 data = texture(moments, sample_pos); + if (data.w < 0.0) { + result.a = 1.0; + result.rgb = vec3(0.4); } else { - color = vec3(0.4,0.4,0.4); + result.a = 1.0; + result.rgb = blueRedPalette(data[3] / 0.2); } -}""").substitute({ - 'size_x': lattice_x, - 'inflow': inflow -}), GL_VERTEX_SHADER) +} +""").substitute({ + "size_x": lattice_x, + "size_y": lattice_y +}), GL_FRAGMENT_SHADER) -fragment_shader = shaders.compileShader(""" +particle_fragment_shader = shaders.compileShader(""" #version 430 in vec3 color; +layout(location = 0) out vec4 frag_color; + void main(){ - gl_FragColor = vec4(color.xyz, 0.0); + frag_color = vec4(color.xyz, 0.0); }""", GL_FRAGMENT_SHADER) -particle_shader = shaders.compileShader(Template(""" +particle_vertex_shader = shaders.compileShader(Template(""" #version 430 layout (location=0) in vec4 Particles; @@ -158,9 +167,11 @@ void main() { }""").substitute({}), GL_VERTEX_SHADER) shader_program = shaders.compileProgram(vertex_shader, fragment_shader) -particle_program = shaders.compileProgram(particle_shader, fragment_shader) projection_id = shaders.glGetUniformLocation(shader_program, 'projection') +particle_program = shaders.compileProgram(particle_vertex_shader, particle_fragment_shader) +particle_projection_id = shaders.glGetUniformLocation(particle_program, 'projection') + lattice = Lattice( descriptor = D2Q9, geometry = Geometry(lattice_x, lattice_y), @@ -174,10 +185,10 @@ lattice.apply_material_map( get_channel_material_map(lattice.geometry)) lattice.sync_material() +moments_texture = MomentsTexture(lattice) + particles = Particles( - lattice.context, - lattice.queue, - lattice.memory.float_type, + lattice, numpy.mgrid[ lattice.geometry.size_x//20:2*lattice.geometry.size_x//20:100j, 4*lattice.geometry.size_y//9:5*lattice.geometry.size_y//9:100000/100j @@ -187,39 +198,33 @@ def on_display(): for i in range(0,updates_per_frame): lattice.evolve() - lattice.collect_gl_moments() + lattice.update_moments() + moments_texture.collect() for i in range(0,updates_per_frame): - lattice.update_gl_particles(particles, aging = False) + particles.update(aging = False) lattice.sync() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - lattice.memory.gl_moments.bind() - glEnableClientState(GL_VERTEX_ARRAY) - shaders.glUseProgram(shader_program) - glUniformMatrix4fv(projection_id, 1, False, numpy.ascontiguousarray(projection)) - - glVertexPointer(4, GL_FLOAT, 0, lattice.memory.gl_moments) - - glPointSize(point_size) - glDrawArrays(GL_POINTS, 0, lattice.geometry.volume) - - particles.gl_particles.bind() - glEnableClientState(GL_VERTEX_ARRAY) - - shaders.glUseProgram(particle_program) glUniformMatrix4fv(projection_id, 1, False, numpy.asfortranarray(projection)) + moments_texture.bind() - glVertexPointer(4, GL_FLOAT, 0, particles.gl_particles) + glBegin(GL_POLYGON) + glVertex(0,0,0) + glVertex(lattice.geometry.size_x,0,0) + glVertex(lattice.geometry.size_x,lattice.geometry.size_y,0) + glVertex(0,lattice.geometry.size_y,0) + glEnd() + shaders.glUseProgram(particle_program) + glUniformMatrix4fv(particle_projection_id, 1, False, numpy.asfortranarray(projection)) + particles.bind() glPointSize(point_size) glDrawArrays(GL_POINTS, 0, particles.count) - glDisableClientState(GL_VERTEX_ARRAY) - glutSwapBuffers() def on_reshape(width, height): |