private void HandleInput() { for (int x = (Params.Width / 2) - 5; x < (Params.Width / 2) + 5; x++) { solver.HeatField[solver.IX(x, Params.Height - 5, 0)] += 25f * Params.Dt; } //solver.setup_sources_and_forces(); if (!fluidControl.MouseIsDown) { return; } Point pos = fluidControl.GridMousePosition(); if (pos.X <= 0 || pos.Y <= 0) { return; } if (MouseButtons == MouseButtons.Left) { Point vel = fluidControl.MouseVelocity(); solver.VelXPrev[solver.IX(pos.X, pos.Y, 0)] = vel.X * Params.Force * Params.Dt; solver.VelYPrev[solver.IX(pos.X, pos.Y, 0)] = vel.Y * Params.Force * Params.Dt; } else if (MouseButtons == MouseButtons.Right) { switch (paintMode) { case PaintMode.Density: solver.DensityFieldPrev[solver.IX(pos.X, pos.Y, 0)] += Params.Source * Params.Dt; break; case PaintMode.Obstacle: solver.Obstacles[solver.IX(pos.X, pos.Y, 0)] = 1; break; } } }
public void Render(Solver solver, RenderMode renderMode, bool drawObstacles = true) { float[] src; switch (renderMode) { default: case RenderMode.Density: src = solver.DensityField; break; case RenderMode.Heat: src = solver.HeatField; break; case RenderMode.Pressure: src = solver.PressureField; break; } bmData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); int pos, x, y; float val; float r, g, b; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { pos = (x << 2) + y * (width << 2); // position in byte array (x*4)+y*(w*4) // obstacles takes precedence if (drawObstacles) { if (solver.Obstacles[solver.IX(x, y, 0)] != 0) { tmpData[pos] = tmpData[pos + 1] = 0; tmpData[pos + 2] = tmpData[pos + 3] = 255; continue; } } val = src[solver.IX(x, y, 0)]; rminVal = Math.Min(val, rminVal); rmaxVal = Math.Max(val, rmaxVal); // get RGB values depending on render mode switch (renderMode) { default: case RenderMode.Density: render_density(val, out r, out g, out b); break; case RenderMode.Heat: render_heat(val, out r, out g, out b); break; case RenderMode.Pressure: render_pressure(val, out r, out g, out b); break; } // update bitmap tmpData[pos] = (byte)(b * 255); tmpData[pos + 1] = (byte)(g * 255); tmpData[pos + 2] = (byte)(r * 255); tmpData[pos + 3] = 255; /*val = (byte)(Math.Max( Math.Min( src[solver.IX(x, y, 0)], 1f), 0f) * 255); * tmpData[pos] = tmpData[pos + 1] = tmpData[pos + 2] = val; // BGR * tmpData[pos + 3] = 255; // alpha*/ } } rminValOld = rminVal; rmaxValOld = rmaxVal; rminVal = float.MaxValue; rmaxVal = float.MinValue; System.Runtime.InteropServices.Marshal.Copy(tmpData, 0, bmData.Scan0, tmpData.Length); bitmap.UnlockBits(bmData); Invalidate(); }