def lin_solve(N, a, c): for k in range(20): #for i in range(1, N+1): # for j in range(1, N+1): # x[i, j] = (x0[i, j] + a * (x[i-1, j] + x[i+1, j] + x[i, j-1] + x[i, j+1]))/ c # double the frame rate, but it's not exactly the same, never find, close # enough #x[1:-1, 1:-1] = (x0[1:-1, 1:-1] + a * (conv2d(x, np.array([[0,1,0],[1,0,1],[0,1,0]])))) / c #set_bnd(N, b, x) renderer = plus.GetRenderer() renderer.SetRenderTarget(uvdens_render_target) renderer.SetShader(shader_lin_solve) renderer.SetShaderFloat2(renderer.GetShaderVariable("size_pixel"), 1.0 / size, 1.0 / size) renderer.SetShaderFloat(renderer.GetShaderVariable("a"), a) renderer.SetShaderFloat(renderer.GetShaderVariable("c"), c) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_in"), uvdens_tex_prev) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_out"), uvdens_tex) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout)
def project(N, u, v, p, div): renderer = plus.GetRenderer() #for i in range(1, N + 1): # for j in range(1, N + 1): # div[i, j] = -0.5 * (u[i+1, j] - u[i-1, j] + v[i, j+1] - v[i, j-1]) / N # p[i, j] = 0 #div[1:-1, 1:-1] = -0.5 * (conv2d(u, np.array([[0,-1,0],[0,0,0],[0,1,0]])) + conv2d(v, np.array([[0,0,0],[-1,0,1],[0,0,0]]))) / N #p[:] = 0 renderer.SetRenderTarget(uvdens_render_target_prev) renderer.SetShader(shader_projectA) renderer.SetShaderFloat2(renderer.GetShaderVariable("size_pixel"), 1.0 / size, 1.0 / size) renderer.SetShaderFloat(renderer.GetShaderVariable("N"), N) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_in"), uvdens_tex) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_out"), uvdens_tex_prev) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout) #set_bnd(N, 0, div) #set_bnd(N, 0, p) lin_solve(N, 1, 4) #for i in range(1, N + 1): # for j in range(1, N + 1): # u[i, j] -= 0.5 * N * (p[i+1, j] - p[i-1, j]) # v[i, j] -= 0.5 * N * (p[i, j+1] - p[i, j-1]) #v[1:-1, 1:-1] -= 0.5 * N * (conv2d(p, np.array([[0,0,0],[-1,0,1],[0,0,0]]))) #u[1:-1, 1:-1] -= 0.5 * N * (conv2d(p, np.array([[0,-1,0],[0,0,0],[0,1,0]]))) renderer.SetRenderTarget(uvdens_render_target) renderer.SetShader(shader_projectB) renderer.SetShaderFloat2(renderer.GetShaderVariable("size_pixel"), 1.0 / size, 1.0 / size) renderer.SetShaderFloat(renderer.GetShaderVariable("N"), N) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_in"), uvdens_tex_prev) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_out"), uvdens_tex) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout)
def add_source(N, dt): renderer = plus.GetRenderer() renderer.SetRenderTarget(uvdens_render_target) renderer.SetShader(shader_add_sources) renderer.SetShaderFloat(renderer.GetShaderVariable("dt"), dt) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_in"), uvdens_tex_prev) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_out"), uvdens_tex) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout)
def draw_dens(simple_graphic_scene_overlay, N, dens, u, v): #scale = 2 #for i in range(1, N + 1): # for j in range(1, N + 1): # p = vec3(i / (N) - 0.5, j / (N) - 0.5, 0) # #helper_2d.draw_line(simple_graphic_scene_overlay, p, p + vec3(u[i, j], v[i,j], 0)) # helper_2d.draw_quad(simple_graphic_scene_overlay, mat4.TransformationMatrix(p + vec3(0, 0, 0.1), vec3(0, 0, 0)), 1 / N, 1 / N, false_texture, col(dens[i, j], 0, 0)) renderer = plus.GetRenderer() renderer.SetRenderTarget(uvdens_render_target) renderer.SetShader(shader_add_sources) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_in"), uvdens_tex) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout)
def advect(N, b, d, d0, u, v, dt): dt0 = dt * N #for i in range(1, N + 1): # for j in range(1, N + 1): # x = i - dt0 * u[i, j] # y = j - dt0 * v[i, j] # if x < 0.5: # x = 0.5 # if x > N + 0.5: # x = N + 0.5 # i0 = int(x) # i1 = i0 + 1 # if y < 0.5: # y = 0.5 # if y > N + 0.5: # y = N + 0.5 # j0 = int(y) # j1 = j0 + 1 # s1 = x - i0 # s0 = 1 - s1 # t1 = y - j0 # t0 = 1 - t1 # d[i, j] = s0 * (t0 * d0[i0,j0] + t1 * d0[i0,j1]) + s1 * (t0 * d0[i1,j0] + t1 * d0[i1,j1]) #set_bnd(N, b, d) renderer = plus.GetRenderer() renderer.SetRenderTarget(uvdens_render_target) renderer.SetShader(shader_advect) renderer.SetShaderFloat2(renderer.GetShaderVariable("size_pixel"), 1.0 / size, 1.0 / size) renderer.SetShaderFloat(renderer.GetShaderVariable("dt0"), dt0) renderer.SetShaderFloat(renderer.GetShaderVariable("N"), N) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_in"), uvdens_tex_prev) renderer.SetShaderTexture(renderer.GetShaderVariable("tex_xydens_out"), uvdens_tex) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout)
def get_from_UI(dens_prev, u_prev, v_prev): #dens_prev[:] = 0.0 #u_prev[:] = 0.0 #v_prev[:] = 0.0 #if hg.GetPlus().KeyDown(hg.KeyF1): # dens_prev[1, 1] = base_force_dens #if hg.GetPlus().KeyDown(hg.KeyF2): # for i in range(2, 5): # v_prev[i, 5] = base_force_v #if hg.GetPlus().KeyDown(hg.KeyF3): # u_prev[3, 3] = base_force_u #if hg.GetPlus().KeyDown(hg.KeyF4): # u_prev[int(N / 2.0), int(N / 2.0)] = base_force_u # v_prev[int(N / 2.0), int(N / 2.0)] = base_force_v #for i in range(1, N+1): # v_prev[IX(1, i)] = base_force_v if int(N/3) < i < int(N/3*2) else 0 # u_prev[IX(1, i)] = 0 renderer = plus.GetRenderer() prev_viewport = renderer.GetViewport() renderer.SetRenderTarget(uvdens_render_target_prev) renderer.SetViewport(hg.Rect(0, 0, size, size)) renderer.EnableDepthTest(False) renderer.Set2DMatrices() renderer.SetShader(shader_set_inputs) renderer.SetShaderFloat2(renderer.GetShaderVariable("size_pixel"), 1.0 / size, 1.0 / size) mouse_pos = plus.GetMousePos() renderer.SetShaderFloat3(renderer.GetShaderVariable("left_button_pressed"), mouse_pos[0] / prev_viewport.ex, 1.0 - mouse_pos[1] / prev_viewport.ey, 1 if plus.MouseButtonDown(hg.Button0) else 0) hg.SetShaderEngineValues(plus.GetRenderSystem()) hg.DrawBuffers(renderer, 6, idx, vtx, vtx_layout)
def draw(self, plus): hg.DrawBuffers(plus.GetRenderer(), 6, self.indices, self.vertex, self.vertex_layout)
# draw loop print("Close the renderer window or press Ctrl+C in this window to end") while hg.IsWindowOpen(win) and (not keyboard.WasPressed(hg.KeyEscape)): # first render a blue triangle on a green background to the texture using the # render target renderer.SetRenderTarget(tgt_rt) renderer.SetViewport(hg.Rect(0, 0, 64, 64)) # fit viewport to texture dimensions renderer.Clear(hg.Color.Green) renderer.SetShader(shader_color) renderer.SetShaderFloat4("u_color", 0, 0, 1, 1) # blue hg.DrawBuffers(renderer, 3, idx, vtx, vtx_layout) # switch back to the default render target and display the texture we just # rendered to using a triangle on a red background # note that the UVs are skewed so that the triangle drawn into the texture # does not exactly overlap with the triangle used to display the texture renderer.ClearRenderTarget() _, wx, wy = hg.GetWindowClientSize(win) renderer.SetViewport(hg.Rect(0, 0, wx, wy)) # fit viewport to window dimensions renderer.Clear(hg.Color.Red) renderer.SetShader(shader_texture) renderer.SetShaderTexture("u_tex", tgt_tex)