public static void DrawCylinder(FloatPoint3 center, float topHorizontalRadius, float topVerticalRadius, float bottomHorizontalRadius, float bottomVerticalRadius, float height, Color?fillColor = null, Color?borderColor = null) { var segments = 360; var topPoints = new float[segments * 2]; var bottomPoints = new float[segments * 2]; var arrayLength = topPoints.Length; var count = 0; for (var angle = 0f; angle < 360; angle += segments / 360f) { topPoints[count] = (float)(center.X + Math.Cos(Math.PI / 180f * angle) * topHorizontalRadius); topPoints[count + 1] = (float)(center.Z + Math.Sin(Math.PI / 180f * angle) * topVerticalRadius); bottomPoints[count] = (float)(center.X + Math.Cos(Math.PI / 180f * angle) * bottomHorizontalRadius); bottomPoints[count + 1] = (float)(center.Z + Math.Sin(Math.PI / 180f * angle) * bottomVerticalRadius); count += 2; } DrawCylinderBase(center - (0f, height / 2, 0f), bottomPoints, fillColor, borderColor); DrawCylinderBase(center + (0f, height / 2, 0f), topPoints, fillColor, borderColor); GL.Begin(PrimitiveType.Quads); GL.Color4(fillColor == null ? Settings.Drawing.FillColor : (Color)fillColor); for (var i = 0; i < arrayLength / 2 - 1; i++) { GL.Vertex3(topPoints[i * 2], center.Y + height / 2, topPoints[i * 2 + 1]); GL.Vertex3(bottomPoints[i * 2], center.Y - height / 2, bottomPoints[i * 2 + 1]); GL.Vertex3(bottomPoints[i * 2 + 2], center.Y - height / 2, bottomPoints[i * 2 + 3]); GL.Vertex3(topPoints[i * 2 + 2], center.Y + height / 2, topPoints[i * 2 + 3]); } GL.Vertex3(topPoints[0], center.Y + height / 2, topPoints[1]); GL.Vertex3(bottomPoints[0], center.Y - height / 2, bottomPoints[1]); GL.Vertex3(bottomPoints[arrayLength - 2], center.Y - height / 2, bottomPoints[arrayLength - 1]); GL.Vertex3(topPoints[arrayLength - 2], center.Y + height / 2, topPoints[arrayLength - 1]); GL.End(); }
public static void DrawCylinder(FloatPoint3 center, float radius, float height, Color?fillColor = null, Color?borderColor = null) { var segments = 360; var points = new float[segments * 2]; var count = 0; for (var angle = 0f; angle < 360; angle += segments / 360f) { points[count++] = (float)(center.X + Math.Cos(Math.PI / 180f * angle) * radius); points[count++] = (float)(center.Z + Math.Sin(Math.PI / 180f * angle) * radius); } DrawCylinderBase(center - (0f, height / 2, 0f), points, fillColor, borderColor); DrawCylinderBase(center + (0f, height / 2, 0f), points, fillColor, borderColor); GL.Begin(PrimitiveType.Quads); GL.Color4(fillColor == null ? Settings.Drawing.FillColor : (Color)fillColor); for (var i = 0; i < points.Length / 2 - 1; i++) { GL.Vertex3(points[i * 2], center.Y + height / 2, points[i * 2 + 1]); GL.Vertex3(points[i * 2], center.Y - height / 2, points[i * 2 + 1]); GL.Vertex3(points[i * 2 + 2], center.Y - height / 2, points[i * 2 + 3]); GL.Vertex3(points[i * 2 + 2], center.Y + height / 2, points[i * 2 + 3]); } GL.Vertex3(points[0], center.Y + height / 2, points[1]); GL.Vertex3(points[0], center.Y - height / 2, points[1]); GL.Vertex3(points[points.Length - 2], center.Y - height / 2, points[points.Length - 1]); GL.Vertex3(points[points.Length - 2], center.Y + height / 2, points[points.Length - 1]); GL.End(); }
private static void DrawCylinderBase(FloatPoint3 center, float[] points, Color?fillColor = null, Color?borderColor = null) { GL.Begin(PrimitiveType.TriangleFan); GL.Color4(fillColor == null ? Settings.Drawing.FillColor : (Color)fillColor); GL.Vertex3(center.X, center.Y, center.Z); for (var i = 0; i < points.Length / 2; i++) { GL.Vertex3(points[i * 2], center.Y, points[i * 2 + 1]); } GL.Vertex3(points[0], center.Y, points[1]); GL.End(); GL.Begin(PrimitiveType.LineLoop); GL.Color4(borderColor == null ? Settings.Drawing.BorderColor : (Color)borderColor); for (var i = 0; i < points.Length / 2; i++) { GL.Vertex3(points[i * 2], center.Y, points[i * 2 + 1]); } GL.End(); }
public static void DrawPyramid(FloatPoint3 center, float baseWidth, float baseHeight, float pyramidHeight, Color?fillColor = null, Color?borderColor = null) { GL.Begin(PrimitiveType.Quads); GL.Color4(fillColor == null ? Settings.Drawing.FillColor : (Color)fillColor); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.End(); GL.Begin(PrimitiveType.TriangleFan); GL.Vertex3(center.X, center.Y + pyramidHeight / 2, center.Z); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.End(); GL.Begin(PrimitiveType.LineLoop); GL.Color4(borderColor == null ? Settings.Drawing.BorderColor : (Color)borderColor); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.End(); GL.Begin(PrimitiveType.Lines); GL.Vertex3(center.X, center.Y + pyramidHeight / 2, center.Z); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.Vertex3(center.X, center.Y + pyramidHeight / 2, center.Z); GL.Vertex3(center.X - baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X, center.Y + pyramidHeight / 2, center.Z); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z - baseHeight / 2); GL.Vertex3(center.X, center.Y + pyramidHeight / 2, center.Z); GL.Vertex3(center.X + baseWidth / 2, center.Y - pyramidHeight / 2, center.Z + baseHeight / 2); GL.End(); }
public static void DrawSphere(FloatPoint3 center, float radius, Color?fillColor = null, Color?normalColor = null) { int nx = 64, ny = 64; int ix, iy; double x, y, z, sy, cy, sy1, cy1, sx, cx, piy, pix, ay, ay1, ax, ty, dnx, dny, diy; dnx = 1.0 / (double)nx; dny = 1.0 / (double)ny; // Рисуем полигональную модель сферы, формируем нормали и задаем коодинаты текстуры // Каждый полигон - это трапеция. Трапеции верхнего и нижнего слоев вырождаются в треугольники GL.Begin(PrimitiveType.QuadStrip); GL.Color4(fillColor == null ? Settings.Drawing.FillColor : (Color)fillColor); piy = Math.PI * dny; pix = Math.PI * dnx; for (iy = 0; iy < ny; iy++) { diy = (double)iy; ay = diy * piy; sy = Math.Sin(ay); cy = Math.Cos(ay); ty = diy * dny; ay1 = ay + piy; sy1 = Math.Sin(ay1); cy1 = Math.Cos(ay1); for (ix = 0; ix <= nx; ix++) { ax = 2.0 * ix * pix; sx = Math.Sin(ax); cx = Math.Cos(ax); x = center.X + radius * sy * cx; y = center.Y + radius * sy * sx; z = center.Z + radius * cy; // Координаты нормали в текущей вершине GL.Normal3(x, y, z); // Нормаль направлена от центра GL.Vertex3(x, y, z); x = center.X + radius * sy1 * cx; y = center.Y + radius * sy1 * sx; z = center.Z + radius * cy1; GL.Normal3(x, y, z); GL.Vertex3(x, y, z); } } GL.End(); if (Window.ShowNormals) { double rv = 1.15 * radius; // Толщина линии, отображающей нормаль GL.LineWidth(1); GL.Color4(normalColor == null ? Settings.Drawing.NormalColor : (Color)normalColor); GL.Begin(PrimitiveType.Lines); piy = Math.PI * dny; pix = Math.PI * dnx; for (iy = 0; iy < ny; iy++) { diy = (double)iy; ay = diy * piy; sy = Math.Sin(ay); cy = Math.Cos(ay); for (ix = 0; ix <= nx; ix++) { ax = 2.0 * ix * pix; sx = Math.Sin(ax); cx = Math.Cos(ax); x = center.X + radius * sy * cx; y = center.Y + radius * sy * sx; z = center.Z + radius * cy; GL.Vertex3(x, y, z); x = center.X + rv * sy * cx; y = center.Y + rv * sy * sx; z = center.Z + rv * cy; GL.Vertex3(x, y, z); } } GL.End(); GL.LineWidth(1); GL.Color4(Color.LightGray); } }
public static void DrawParallelepiped(FloatPoint3 firstPoint, FloatPoint3 secondPoint, Color?fillColor = null, Color?borderColor = null) { GL.Begin(PrimitiveType.Quads); GL.Color4(fillColor == null ? Settings.Drawing.FillColor : (Color)fillColor); //front GL.Vertex3(firstPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, firstPoint.Z); //back GL.Vertex3(secondPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(firstPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, secondPoint.Z); //top GL.Vertex3(firstPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, firstPoint.Z); //bottom GL.Vertex3(firstPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, firstPoint.Z); //right GL.Vertex3(secondPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, secondPoint.Z); //left GL.Vertex3(firstPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, firstPoint.Z); GL.End(); GL.Color4(borderColor == null ? Settings.Drawing.BorderColor : (Color)borderColor); GL.Begin(PrimitiveType.LineLoop); GL.Vertex3(firstPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, firstPoint.Z); GL.End(); GL.Begin(PrimitiveType.LineLoop); GL.Vertex3(firstPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, secondPoint.Z); GL.End(); GL.Begin(PrimitiveType.Lines); GL.Vertex3(firstPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, firstPoint.Y, secondPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(firstPoint.X, secondPoint.Y, secondPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, firstPoint.Z); GL.Vertex3(secondPoint.X, secondPoint.Y, secondPoint.Z); GL.End(); }