private unsafe void BlitVoxelToSurface(DrawingSurface ds, DrawingSurface vxl_ds, GameObject obj, DrawProperties props) { Point d = new Point(obj.Tile.Dx * TileWidth / 2, (obj.Tile.Dy - obj.Tile.Z) * TileHeight / 2); d.Offset(props.GetOffset(obj)); d.Offset(-vxl_ds.BitmapData.Width / 2, -vxl_ds.BitmapData.Height / 2); // rows inverted! var w_low = (byte*)ds.BitmapData.Scan0; byte* w_high = w_low + ds.BitmapData.Stride * ds.BitmapData.Height; var zBuffer = ds.GetZBuffer(); var shadowBufVxl = vxl_ds.GetShadows(); var shadowBuf = ds.GetShadows(); // int rowsTouched = 0; // short firstRowTouched = short.MaxValue; for (int y = 0; y < vxl_ds.Height; y++) { byte* src_row = (byte*)vxl_ds.BitmapData.Scan0 + vxl_ds.BitmapData.Stride * (vxl_ds.Height - y - 1); byte* dst_row = ((byte*)ds.BitmapData.Scan0 + (d.Y + y) * ds.BitmapData.Stride + d.X * 3); int zIdx = (d.Y + y) * ds.Width + d.X; if (dst_row < w_low || dst_row >= w_high) continue; for (int x = 0; x < vxl_ds.Width; x++) { // only non-transparent pixels if (*(src_row + x * 4 + 3) > 0) { *(dst_row + x * 3) = *(src_row + x * 4); *(dst_row + x * 3 + 1) = *(src_row + x * 4 + 1); *(dst_row + x * 3 + 2) = *(src_row + x * 4 + 2); // if (y < firstRowTouched) // firstRowTouched = (short)y; short zBufVal = (short)((obj.Tile.Rx + obj.Tile.Ry + obj.Tile.Z) * TileHeight / 2); if (zBufVal >= zBuffer[zIdx]) zBuffer[zIdx] = zBufVal; } // or shadows else if (shadowBufVxl[x + y * vxl_ds.Height]) { int shadIdx = (d.Y + y) * ds.Width + d.X + x; if (!shadowBuf[shadIdx]) { *(dst_row + x * 3) /= 2; *(dst_row + x * 3 + 1) /= 2; *(dst_row + x * 3 + 2) /= 2; shadowBuf[shadIdx] = true; } } zIdx++; } } }
public DrawingSurface Render(VxlFile vxl, HvaFile hva, GameObject obj, DrawProperties props) { if (!_isInit) { Initialize(); } if (!_canRender) { Logger.Warn("Not rendering {0} because no OpenGL context could be obtained", vxl.FileName); return(null); } Logger.Debug("Rendering voxel {0}", vxl.FileName); vxl.Initialize(); hva.Initialize(); GL.Viewport(0, 0, _surface.BitmapData.Width, _surface.BitmapData.Height); GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); // RA2 uses dimetric projection with camera elevated 30° off the ground GL.MatrixMode(MatrixMode.Projection); var persp = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(30), _surface.BitmapData.Width / (float)_surface.BitmapData.Height, 1, _surface.BitmapData.Height); GL.LoadMatrix(ref persp); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); var lookat = Matrix4.LookAt(0, 0, -10, 0, 0, 0, 0, 1, 0); GL.MultMatrix(ref lookat); var trans = Matrix4.CreateTranslation(0, 0, 10); GL.MultMatrix(ref trans); // align and zoom var world = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(60)); world = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(180)) * world; world = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(-45)) * world; world = Matrix4.Scale(0.028f, 0.028f, 0.028f) * world; GL.MultMatrix(ref world); // DrawAxes(); // determine tilt vectors Matrix4 tilt = Matrix4.Identity; int tiltPitch = 0, tiltYaw = 0; if (obj.Tile.Drawable != null) { var img = (obj.Tile.Drawable as TileDrawable).GetTileImage(obj.Tile); int ramp = img?.RampType ?? 0; if (ramp == 0 || ramp >= 17) { tiltPitch = tiltYaw = 0; } else if (ramp <= 4) { // screen-diagonal facings (perpendicular to axes) tiltPitch = 25; tiltYaw = -90 * ramp; } else { // world-diagonal facings (perpendicular to screen) tiltPitch = 25; tiltYaw = 225 - 90 * ((ramp - 1) % 4); } tilt *= Matrix4.CreateRotationX(MathHelper.DegreesToRadians(tiltPitch)); tilt *= Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(tiltYaw)); /*// show tilt direction * GL.Color3(Color.Black); * GL.Begin(BeginMode.Lines); * GL.Vertex3(Vector3.Zero); * var tiltVec = Vector3.UnitZ; * tiltVec = Vector3.Transform(tiltVec, tilt); * tiltVec = Vector3.Multiply(tiltVec, 1000f); * GL.Vertex3(tiltVec); * GL.End();*/ } /*// draw slope normals * GL.LineWidth(2); * var colors = new[] { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Orange, Color.Black, Color.Purple, Color.SlateBlue, Color.DimGray, Color.White, Color.Teal, Color.Tan }; * for (int i = 0; i < 8; i++) { * GL.Color3(colors[i]); * * const float roll = 25f; * float syaw = 45f * i; * var slopeNormal = Vector3.UnitZ; * slopeNormal = Vector3.Transform(slopeNormal, Matrix4.CreateRotationX(MathHelper.DegreesToRadians(roll))); * slopeNormal = Vector3.Transform(slopeNormal, Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(syaw))); * GL.Begin(BeginMode.Lines); * GL.Vertex3(0, 0, 0); * GL.Vertex3(Vector3.Multiply(slopeNormal, 1000f)); * GL.End(); * }*/ // object rotation around Z float direction = (obj is OwnableObject) ? (obj as OwnableObject).Direction : 0; float objectRotation = 90 - direction / 256f * 360f - tiltYaw; // convert game rotation to world degrees Matrix4 @object = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(objectRotation)) * tilt; // object facing // art.ini TurretOffset value positions some voxel parts over our x-axis @object = Matrix4.CreateTranslation(0.18f * props.TurretVoxelOffset, 0, 0) * @object; GL.MultMatrix(ref @object); // DrawAxes(); float pitch = MathHelper.DegreesToRadians(210); float yaw = MathHelper.DegreesToRadians(120); /*// helps to find good pitch/yaw * // direction of light vector given by pitch & yaw * for (int i = 0; i < 360; i += 30) { * for (int j = 0; j < 360; j += 30) { * GL.Color3(colors[i / 30]); * var shadowTransform2 = * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(i)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(j)); * GL.LineWidth(2); * GL.Begin(BeginMode.Lines); * GL.Vertex3(0, 0, 0); * GL.Vertex3(Vector3.Multiply(ExtractRotationVector(ToOpenGL(Matrix4.Invert(world * shadowTransform2))), 100f)); * GL.End(); * } * }*/ var shadowTransform = Matrix4.CreateRotationZ(pitch) * Matrix4.CreateRotationY(yaw); // clear shadowbuf var shadBuf = _surface.GetShadows(); Array.Clear(shadBuf, 0, shadBuf.Length); foreach (var section in vxl.Sections) { GL.PushMatrix(); var frameRot = hva.LoadGLMatrix(section.Index); frameRot.M41 *= section.HVAMultiplier * section.ScaleX; frameRot.M42 *= section.HVAMultiplier * section.ScaleY; frameRot.M43 *= section.HVAMultiplier * section.ScaleZ; var frameTransl = Matrix4.CreateTranslation(section.MinBounds); var frame = frameTransl * frameRot; GL.MultMatrix(ref frame); var shadowScale = Matrix4.Scale(0.5f); //var shadowTilt = null; var shadowToScreen = frameTransl * shadowScale * frameRot * (@object * world) * trans * lookat; // undo world transformations on light direction var v = @object * world * frame * shadowTransform; var lightDirection = (v.Determinant != 0.0) ? ExtractRotationVector(ToOpenGL(Matrix4.Invert(v))) : Vector3.Zero; // draw line in direction light comes from /*GL.Color3(Color.Red); * GL.LineWidth(4f); * GL.Begin(BeginMode.Lines); * GL.Vertex3(0, 0, 0); * GL.Vertex3(Vector3.Multiply(lightDirection, 100f)); * GL.End();*/ GL.Begin(BeginMode.Quads); for (uint x = 0; x != section.SizeX; x++) { for (uint y = 0; y != section.SizeY; y++) { foreach (VxlFile.Voxel vx in section.Spans[x, y].Voxels) { Color color = obj.Palette.Colors[vx.ColorIndex]; Vector3 normal = section.GetNormal(vx.NormalIndex); // shader function taken from https://github.com/OpenRA/OpenRA/blob/bleed/cg/vxl.fx // thanks to pchote for a LOT of help getting it right Vector3 colorMult = Vector3.Add(Ambient, Diffuse * Math.Max(Vector3.Dot(normal, lightDirection), 0f)); GL.Color3( (byte)Math.Min(255, color.R * colorMult.X), (byte)Math.Min(255, color.G * colorMult.Y), (byte)Math.Min(255, color.B * colorMult.Z)); Vector3 vxlPos = Vector3.Multiply(new Vector3(x, y, vx.Z), section.Scale); RenderVoxel(vxlPos); var shadpos = new Vector3(x, y, 0); var screenPos = Vector3.Transform(shadpos, shadowToScreen); screenPos = Vector3.Transform(screenPos, persp); screenPos.X /= screenPos.Z; screenPos.Y /= screenPos.Z; screenPos.X = (screenPos.X + 1) * _surface.Width / 2; screenPos.Y = (screenPos.Y + 1) * _surface.Height / 2; if (0 <= screenPos.X && screenPos.X < _surface.Width && 0 <= screenPos.Y && screenPos.Y < _surface.Height) { shadBuf[(int)screenPos.X + (_surface.Height - 1 - (int)screenPos.Y) * _surface.Width] = true; } /* draw line in normal direction * if (r.Next(100) == 4) { * float m = Math.Max(Vector3.Dot(normal, lightDirection), 0f); * GL.Color3(m, m, m); * GL.LineWidth(1); * GL.Begin(BeginMode.Lines); * GL.Vertex3(new Vector3(x, y, vx.Z)); * GL.Vertex3(new Vector3(x, y, vx.Z) + Vector3.Multiply(normal, 100f)); * GL.End(); * }*/ } } } GL.End(); GL.PopMatrix(); } // read pixels back to surface GL.ReadPixels(0, 0, _surface.BitmapData.Width, _surface.BitmapData.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, _surface.BitmapData.Scan0); return(_surface); }
private void canvas_MouseMove(object sender, MouseEventArgs e) { StringBuilder sb = new StringBuilder(); var canvas = sender as ZoomableCanvas; var pixelLocationF = canvas.PointToImagePixel(e.Location); var location = new Point((int)Math.Round(pixelLocationF.X, 0), (int)Math.Round(pixelLocationF.Y, 0)); if (location.X < 0 || location.Y < 0 || location.X >= canvas.ImageSize.Width || location.Y >= canvas.ImageSize.Height) { return; } int rIdx = location.X + location.Y * _drawingSurface.Width; var tile = _tiles.GetTileScreen(location); if (tile == null || !(tile.Drawable is TileDrawable)) { sb.Append("No valid tile under mouse"); } else { sb.AppendFormat("Cells: {0} Coords (X, Y / H): {1}, {2} / {3}", _cells, tile.Rx, tile.Ry, tile.Z); var objs = _map.GetObjectsAt(tile.Dx, tile.Dy / 2); if (objs.Any()) { sb.Append(" Objects:"); foreach (var obj in objs) { sb.Append(" " + obj); if (obj is OverlayObject) { var ovl = (obj as OverlayObject); if (ovl.IsGeneratedVeins) { sb.Append("(gen)"); } } sb.Append(" "); } } var tileFile = (tile.Drawable as TileDrawable).GetTileFile(tile); if (tileFile != null) { sb.AppendFormat("\nTile: {0}", (tileFile?.FileName ?? "").ToLower()); sb.AppendFormat(" TileNum: {0} SubTile: {1}", tile.TileNum, tile.SubTile); if (tileFile.Images[tile.SubTile].RampType != 0) { sb.AppendFormat(" Ramp: {0}", tileFile.Images[tile.SubTile].RampType); } if (tileFile.Images[tile.SubTile].TerrainType != 0) { sb.AppendFormat(" Terrain: {0}", tileFile.Images[tile.SubTile].TerrainType); } if (tile.IceGrowth > 0) { sb.Append(" IceGrowth"); } } #if DEBUG sb.AppendFormat("\nMouse: ({0},{1}) ", location.X, location.Y); sb.AppendFormat(": d({0},{1}) ", tile.Dx, tile.Dy); var gridTilenoZ = _tiles.GetTileScreen(location, true, true); sb.AppendFormat(" Touched: {0}", _tiles.GridTouched[gridTilenoZ.Dx, gridTilenoZ.Dy / 2]); if (_tiles.GridTouchedBy[gridTilenoZ.Dx, gridTilenoZ.Dy / 2] != null) { sb.AppendFormat(" by {0} ", _tiles.GridTouchedBy[gridTilenoZ.Dx, gridTilenoZ.Dy / 2]); } sb.AppendFormat(" Z-buf: {0}", _drawingSurface.GetZBuffer()[rIdx]); sb.AppendFormat(" S-buf: {0}", _drawingSurface.GetShadows()[rIdx]); #endif } toolStripStatusLabel1.Text = sb.ToString(); if (e.Button == MouseButtons.Right) { Point newPoint = new Point(location.X - _oldPoint.X, location.Y - _oldPoint.Y); panel1.AutoScrollPosition = new Point(-panel1.AutoScrollPosition.X - newPoint.X, -panel1.AutoScrollPosition.Y - newPoint.Y); } }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { StringBuilder sb = new StringBuilder(); int rIdx = e.Location.X + e.Location.Y * _drawingSurface.Width; sb.AppendFormat("Mouse: ({0},{1})", e.Location.X, e.Location.Y); var tile = _tiles.GetTileScreen(e.Location); if (tile == null || !(tile.Drawable is TileDrawable)) { sb.Append("No valid tile under mouse"); } else { var tileFile = (tile.Drawable as TileDrawable).GetTileFile(tile); sb.AppendFormat(" Tile {4}: d({0},{1}) r({2},{3})", tile.Dx, tile.Dy, tile.Rx, tile.Ry, (tileFile?.FileName ?? "").ToUpper()); if (tileFile != null) { if (tileFile.Images[tile.SubTile].RampType != 0) { sb.AppendFormat(" ramp {0}", tileFile.Images[tile.SubTile].RampType); } if (tileFile.Images[tile.SubTile].TerrainType != 0) { sb.AppendFormat(" terrain {0}", tileFile.Images[tile.SubTile].TerrainType); } } var gridTilenoZ = _tiles.GetTileScreen(e.Location, true, true); sb.AppendFormat(" Touched: {0}", _tiles.GridTouched[gridTilenoZ.Dx, gridTilenoZ.Dy / 2]); if (_tiles.GridTouchedBy[gridTilenoZ.Dx, gridTilenoZ.Dy / 2] != null) { sb.AppendFormat(" by {0} ", _tiles.GridTouchedBy[gridTilenoZ.Dx, gridTilenoZ.Dy / 2]); } sb.AppendFormat(" Z-buf: {0}", _drawingSurface.GetZBuffer()[rIdx]); sb.AppendFormat(" S-buf: {0}", _drawingSurface.GetShadows()[rIdx]); var objs = _map.GetObjectsAt(tile.Dx, tile.Dy / 2); if (objs.Any()) { sb.Append(" Objects: "); foreach (var obj in objs) { sb.Append(obj); if (obj is OverlayObject) { var ovl = (obj as OverlayObject); if (ovl.IsGeneratedVeins) { sb.Append("(gen)"); } } sb.Append(" "); } } } toolStripStatusLabel1.Text = sb.ToString(); }
public unsafe void DrawShadow(GameObject obj, ShpFile shp, DrawProperties props, DrawingSurface ds) { shp.Initialize(); int frameIndex = props.FrameDecider(obj); if (obj.Drawable.IsActualWall) { frameIndex = ((StructureObject)obj).WallBuildingFrame; } frameIndex = DecideFrameIndex(frameIndex, shp.NumImages); frameIndex += shp.Images.Count / 2; // latter half are shadow Images if (frameIndex >= shp.Images.Count) { return; } var img = shp.GetImage(frameIndex); var imgData = img.GetImageData(); if (imgData == null || img.Width * img.Height != imgData.Length) { return; } Point offset = props.GetShadowOffset(obj); offset.X += obj.Tile.Dx * _config.TileWidth / 2 - shp.Width / 2 + img.X; offset.Y += (obj.Tile.Dy - obj.Tile.Z) * _config.TileHeight / 2 - shp.Height / 2 + img.Y; Logger.Trace("Drawing SHP shadow {0} (frame {1}) at ({2},{3})", shp.FileName, frameIndex, offset.X, offset.Y); int stride = ds.BitmapData.Stride; var shadows = ds.GetShadows(); var zBuffer = ds.GetZBuffer(); var heightBuffer = ds.GetHeightBuffer(); var w_low = (byte *)ds.BitmapData.Scan0; byte *w_high = (byte *)ds.BitmapData.Scan0 + stride * ds.BitmapData.Height; byte *w = (byte *)ds.BitmapData.Scan0 + offset.X * 3 + stride * offset.Y; int zIdx = offset.X + offset.Y * ds.Width; int rIdx = 0; short zOffset = (short)((obj.Tile.Rx + obj.Tile.Ry) * _config.TileHeight / 2 - shp.Height / 2 + img.Y); int castHeight = obj.Tile.Z * _config.TileHeight / 2; if (obj.Drawable != null && !obj.Drawable.Flat) { castHeight += shp.Height; castHeight += obj.Drawable.TileElevation * _config.TileHeight / 2; } for (int y = 0; y < img.Height; y++) { if (offset.Y + y < 0) { w += stride; rIdx += img.Width; zIdx += ds.Width; continue; // out of bounds } short zBufVal = zOffset; if (obj.Drawable.Flat) { zBufVal += (short)y; } else { zBufVal += img.Height; } for (int x = 0; x < img.Width; x++) { if (0 <= offset.X + x && offset.X + x < ds.Width && 0 <= y + offset.Y && y + offset.Y < ds.Height && imgData[rIdx] != 0 && !shadows[zIdx] && // zBufVal >= zBuffer[zIdx] && castHeight >= heightBuffer[zIdx]) { *(w + 0) /= 2; *(w + 1) /= 2; *(w + 2) /= 2; shadows[zIdx] = true; } // Up to the next pixel rIdx++; zIdx++; w += 3; } w += stride - 3 * img.Width; // ... and if we're no more on the same row, zIdx += ds.Width - img.Width; // adjust the writing pointer accordingy } }
unsafe public static void DrawShadow(GameObject obj, ShpFile shp, DrawProperties props, DrawingSurface ds) { int frameIndex = props.FrameDecider(obj); frameIndex = DecideFrameIndex(frameIndex, shp.NumImages); frameIndex += shp.Images.Count / 2; // latter half are shadow Images if (frameIndex >= shp.Images.Count) return; var img = shp.GetImage(frameIndex); var imgData = img.GetImageData(); if (imgData == null || img.Width * img.Height != imgData.Length) return; Point offset = props.GetShadowOffset(obj); offset.X += obj.Tile.Dx * Drawable.TileWidth / 2 - shp.Width / 2 + img.X; offset.Y += (obj.Tile.Dy - obj.Tile.Z) * Drawable.TileHeight / 2 - shp.Height / 2 + img.Y; Logger.Trace("Drawing SHP shadow {0} (frame {1}) at ({2},{3})", shp.FileName, frameIndex, offset.X, offset.Y); int stride = ds.BitmapData.Stride; var shadows = ds.GetShadows(); var zBuffer = ds.GetZBuffer(); var heightBuffer = ds.GetHeightBuffer(); var w_low = (byte*)ds.BitmapData.Scan0; byte* w_high = (byte*)ds.BitmapData.Scan0 + stride * ds.BitmapData.Height; byte* w = (byte*)ds.BitmapData.Scan0 + offset.X * 3 + stride * offset.Y; int zIdx = offset.X + offset.Y * ds.Width; int rIdx = 0; short zOffset = (short)((obj.Tile.Rx + obj.Tile.Ry) * Drawable.TileHeight / 2 - shp.Height / 2 + img.Y); int castHeight = obj.Tile.Z * Drawable.TileHeight / 2; if (obj.Drawable != null && !obj.Drawable.Flat) { castHeight += shp.Height; castHeight += obj.Drawable.TileElevation * Drawable.TileHeight / 2; } for (int y = 0; y < img.Height; y++) { if (offset.Y + y < 0) { w += stride; rIdx += img.Width; zIdx += ds.Width; continue; // out of bounds } short zBufVal = zOffset; if (obj.Drawable.Flat) zBufVal += (short)y; else zBufVal += img.Height; for (int x = 0; x < img.Width; x++) { if (w_low <= w && w < w_high && imgData[rIdx] != 0 && !shadows[zIdx] //&& zBufVal >= zBuffer[zIdx] && castHeight >= heightBuffer[zIdx] ) { *(w + 0) /= 2; *(w + 1) /= 2; *(w + 2) /= 2; shadows[zIdx] = true; } // Up to the next pixel rIdx++; zIdx++; w += 3; } w += stride - 3 * img.Width; // ... and if we're no more on the same row, zIdx += ds.Width - img.Width; // adjust the writing pointer accordingy } }