From b770e7452c11cf0acdccf824c9c9304e9de3f08b Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 21 Sep 2019 18:19:22 +0200 Subject: Extract GL moments, particle buffers and add texture buffer --- ldc_2d_gl_interop.py | 93 ++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 47 deletions(-) (limited to 'ldc_2d_gl_interop.py') diff --git a/ldc_2d_gl_interop.py b/ldc_2d_gl_interop.py index 631b5c8..b7223e0 100644 --- a/ldc_2d_gl_interop.py +++ b/ldc_2d_gl_interop.py @@ -13,6 +13,8 @@ from OpenGL.GL import shaders from pyrr import matrix44 +from utility.opengl import MomentsTexture + lattice_x = 480 lattice_y = 300 @@ -70,15 +72,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), @@ -87,39 +106,17 @@ 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. - ); - - color = blueRedPalette(CellMoments[3] / $lid_speed); -}""").substitute({ - 'size_x' : lattice_x, - 'lid_speed': lid_speed -}), GL_VERTEX_SHADER) - -fragment_shader = shaders.compileShader(""" -#version 430 - -in vec3 color; - void main(){ - gl_FragColor = vec4(color.xyz, 0.0); -}""", GL_FRAGMENT_SHADER) - + const vec2 sample_pos = unit(frag_pos); + const vec4 data = texture(moments, sample_pos); + result.a = 1.0; + result.rgb = blueRedPalette(data[3] / $lid_speed); +} +""").substitute({ + "size_x": lattice_x, + "size_y": lattice_y, + "lid_speed": lid_speed +}), GL_FRAGMENT_SHADER) shader_program = shaders.compileProgram(vertex_shader, fragment_shader) projection_id = shaders.glGetUniformLocation(shader_program, 'projection') @@ -137,26 +134,28 @@ lattice.apply_material_map( get_cavity_material_map(lattice.geometry)) lattice.sync_material() +moments_texture = MomentsTexture(lattice) + +cube_vertices, cube_edges = lattice.geometry.wireframe() + def on_display(): for i in range(0,updates_per_frame): lattice.evolve() - lattice.collect_gl_moments() + moments_texture.collect() 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.asfortranarray(projection)) - - glVertexPointer(4, GL_FLOAT, 0, lattice.memory.gl_moments) - - glPointSize(point_size) - glDrawArrays(GL_POINTS, 0, lattice.geometry.volume) - - glDisableClientState(GL_VERTEX_ARRAY) + moments_texture.bind() + + 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() glutSwapBuffers() -- cgit v1.2.3